mirror of https://github.com/go-gitea/gitea.git
Fix "unicode escape" JS error (#32806)
<details>     </details> --------- Co-authored-by: silverwind <me@silverwind.io>pull/32799/head^2
parent
566f5356db
commit
00e2b339b6
@ -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 New Issue