Files

29 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

import {addDelegatedEventListener, hideElem, queryElemSiblings, showElem, toggleElem} from '../utils/dom.ts';
export function initUnicodeEscapeButton() {
2024-12-13 02:37:44 +08:00
// 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) => {
e.preventDefault();
2024-12-13 02:37:44 +08:00
const unicodeContentSelector = btn.getAttribute('data-unicode-content-selector');
const container = unicodeContentSelector ?
2025-12-03 03:13:16 +01:00
document.querySelector(unicodeContentSelector)! :
btn.closest('.file-content, .non-diff-file-content')!;
2024-12-13 02:37:44 +08:00
const fileView = container.querySelector('.file-code, .file-view') ?? container;
if (btn.matches('.escape-button')) {
2024-12-13 02:37:44 +08:00
fileView.classList.add('unicode-escaped');
hideElem(btn);
showElem(queryElemSiblings(btn, '.unescape-button'));
} else if (btn.matches('.unescape-button')) {
2024-12-13 02:37:44 +08:00
fileView.classList.remove('unicode-escaped');
hideElem(btn);
showElem(queryElemSiblings(btn, '.escape-button'));
} else if (btn.matches('.toggle-escape-button')) {
2024-12-13 02:37:44 +08:00
const isEscaped = fileView.classList.contains('unicode-escaped');
fileView.classList.toggle('unicode-escaped', !isEscaped);
toggleElem(container.querySelectorAll('.unescape-button'), !isEscaped);
toggleElem(container.querySelectorAll('.escape-button'), isEscaped);
}
});
}