mirror of
https://github.com/go-gitea/gitea
synced 2024-12-29 14:54:32 +01:00
Fix "unicode escape" JS error (#32806)
<details> ![image](https://github.com/user-attachments/assets/98aef2fb-791e-4b4a-b2ac-e880f8a52040) ![image](https://github.com/user-attachments/assets/532673ae-c4cf-4d84-a5c6-93e6eacd341c) ![image](https://github.com/user-attachments/assets/2a241a3d-b7f6-44ca-89d9-9d68386fbf3e) ![image](https://github.com/user-attachments/assets/1251e43d-41f2-42d1-a23b-3182e3812c3d) </details> --------- Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
parent
566f5356db
commit
00e2b339b6
3 changed files with 16 additions and 15 deletions
|
@ -167,8 +167,8 @@
|
||||||
<button class="btn diff-header-popup-btn tw-p-1">{{svg "octicon-kebab-horizontal" 18}}</button>
|
<button class="btn diff-header-popup-btn tw-p-1">{{svg "octicon-kebab-horizontal" 18}}</button>
|
||||||
<div class="tippy-target">
|
<div class="tippy-target">
|
||||||
{{if not (or $file.IsIncomplete $file.IsBin $file.IsSubmodule)}}
|
{{if not (or $file.IsIncomplete $file.IsBin $file.IsSubmodule)}}
|
||||||
<button class="unescape-button item" data-file-content-elem-id="diff-{{$file.NameHash}}">{{ctx.Locale.Tr "repo.unescape_control_characters"}}</button>
|
<button class="unescape-button item" data-unicode-content-selector="#diff-{{$file.NameHash}}">{{ctx.Locale.Tr "repo.unescape_control_characters"}}</button>
|
||||||
<button class="escape-button tw-hidden item" data-file-content-elem-id="diff-{{$file.NameHash}}">{{ctx.Locale.Tr "repo.escape_control_characters"}}</button>
|
<button class="escape-button tw-hidden item" data-unicode-content-selector="#diff-{{$file.NameHash}}">{{ctx.Locale.Tr "repo.escape_control_characters"}}</button>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if and (not $file.IsSubmodule) (not $.PageIsWiki)}}
|
{{if and (not $file.IsSubmodule) (not $.PageIsWiki)}}
|
||||||
{{if $file.IsDeleted}}
|
{{if $file.IsDeleted}}
|
||||||
|
|
|
@ -44,13 +44,13 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="repo-button-row">
|
<div class="repo-button-row">
|
||||||
{{if .EscapeStatus.Escaped}}
|
{{if .EscapeStatus.Escaped}}
|
||||||
<a class="ui small button unescape-button tw-m-0 tw-hidden">{{ctx.Locale.Tr "repo.unescape_control_characters"}}</a>
|
<a class="ui small button unescape-button tw-hidden" data-unicode-content-selector=".wiki-content-parts">{{ctx.Locale.Tr "repo.unescape_control_characters"}}</a>
|
||||||
<a class="ui small button escape-button tw-m-0">{{ctx.Locale.Tr "repo.escape_control_characters"}}</a>
|
<a class="ui small button escape-button" data-unicode-content-selector=".wiki-content-parts">{{ctx.Locale.Tr "repo.escape_control_characters"}}</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if and .CanWriteWiki (not .Repository.IsMirror)}}
|
{{if and .CanWriteWiki (not .Repository.IsMirror)}}
|
||||||
<a class="ui small button" href="{{.RepoLink}}/wiki/{{.PageURL}}?action=_edit">{{ctx.Locale.Tr "repo.wiki.edit_page_button"}}</a>
|
<a class="ui small button" href="{{.RepoLink}}/wiki/{{.PageURL}}?action=_edit">{{ctx.Locale.Tr "repo.wiki.edit_page_button"}}</a>
|
||||||
<a class="ui small primary button" href="{{.RepoLink}}/wiki?action=_new">{{ctx.Locale.Tr "repo.wiki.new_page_button"}}</a>
|
<a class="ui small primary button" href="{{.RepoLink}}/wiki?action=_new">{{ctx.Locale.Tr "repo.wiki.new_page_button"}}</a>
|
||||||
<a class="ui small red button tw-m-0 delete-button" href="" data-url="{{.RepoLink}}/wiki/{{.PageURL}}?action=_delete" data-id="{{.PageURL}}">{{ctx.Locale.Tr "repo.wiki.delete_page_button"}}</a>
|
<a class="ui small red button delete-button" href="" data-url="{{.RepoLink}}/wiki/{{.PageURL}}?action=_delete" data-id="{{.PageURL}}">{{ctx.Locale.Tr "repo.wiki.delete_page_button"}}</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,27 +1,28 @@
|
||||||
import {addDelegatedEventListener, hideElem, queryElemSiblings, showElem, toggleElem} from '../utils/dom.ts';
|
import {addDelegatedEventListener, hideElem, queryElemSiblings, showElem, toggleElem} from '../utils/dom.ts';
|
||||||
|
|
||||||
export function initUnicodeEscapeButton() {
|
export function initUnicodeEscapeButton() {
|
||||||
|
// buttons might appear on these pages: file view (code, rendered markdown), diff (commit, pr conversation, pr diff), blame, wiki
|
||||||
addDelegatedEventListener(document, 'click', '.escape-button, .unescape-button, .toggle-escape-button', (btn, e) => {
|
addDelegatedEventListener(document, 'click', '.escape-button, .unescape-button, .toggle-escape-button', (btn, e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
const fileContentElemId = btn.getAttribute('data-file-content-elem-id');
|
const unicodeContentSelector = btn.getAttribute('data-unicode-content-selector');
|
||||||
const fileContent = fileContentElemId ?
|
const container = unicodeContentSelector ?
|
||||||
document.querySelector(`#${fileContentElemId}`) :
|
document.querySelector(unicodeContentSelector) :
|
||||||
btn.closest('.file-content, .non-diff-file-content');
|
btn.closest('.file-content, .non-diff-file-content');
|
||||||
const fileView = fileContent?.querySelectorAll('.file-code, .file-view');
|
const fileView = container.querySelector('.file-code, .file-view') ?? container;
|
||||||
if (btn.matches('.escape-button')) {
|
if (btn.matches('.escape-button')) {
|
||||||
for (const el of fileView) el.classList.add('unicode-escaped');
|
fileView.classList.add('unicode-escaped');
|
||||||
hideElem(btn);
|
hideElem(btn);
|
||||||
showElem(queryElemSiblings(btn, '.unescape-button'));
|
showElem(queryElemSiblings(btn, '.unescape-button'));
|
||||||
} else if (btn.matches('.unescape-button')) {
|
} else if (btn.matches('.unescape-button')) {
|
||||||
for (const el of fileView) el.classList.remove('unicode-escaped');
|
fileView.classList.remove('unicode-escaped');
|
||||||
hideElem(btn);
|
hideElem(btn);
|
||||||
showElem(queryElemSiblings(btn, '.escape-button'));
|
showElem(queryElemSiblings(btn, '.escape-button'));
|
||||||
} else if (btn.matches('.toggle-escape-button')) {
|
} else if (btn.matches('.toggle-escape-button')) {
|
||||||
const isEscaped = fileView[0]?.classList.contains('unicode-escaped');
|
const isEscaped = fileView.classList.contains('unicode-escaped');
|
||||||
for (const el of fileView) el.classList.toggle('unicode-escaped', !isEscaped);
|
fileView.classList.toggle('unicode-escaped', !isEscaped);
|
||||||
toggleElem(fileContent.querySelectorAll('.unescape-button'), !isEscaped);
|
toggleElem(container.querySelectorAll('.unescape-button'), !isEscaped);
|
||||||
toggleElem(fileContent.querySelectorAll('.escape-button'), isEscaped);
|
toggleElem(container.querySelectorAll('.escape-button'), isEscaped);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue