2024-11-26 09:24:56 +08:00
|
|
|
import {addDelegatedEventListener, hideElem, queryElemSiblings, showElem, toggleElem} from '../utils/dom.ts';
|
2022-01-29 05:00:11 +08:00
|
|
|
|
2022-01-07 09:18:52 +08:00
|
|
|
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
|
2024-11-26 09:24:56 +08:00
|
|
|
addDelegatedEventListener(document, 'click', '.escape-button, .unescape-button, .toggle-escape-button', (btn, e) => {
|
2022-01-07 09:18:52 +08:00
|
|
|
e.preventDefault();
|
2024-02-25 03:11:51 +08:00
|
|
|
|
2024-12-13 02:37:44 +08:00
|
|
|
const unicodeContentSelector = btn.getAttribute('data-unicode-content-selector');
|
|
|
|
const container = unicodeContentSelector ?
|
|
|
|
document.querySelector(unicodeContentSelector) :
|
2024-11-26 09:24:56 +08:00
|
|
|
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;
|
2024-02-25 03:11:51 +08:00
|
|
|
if (btn.matches('.escape-button')) {
|
2024-12-13 02:37:44 +08:00
|
|
|
fileView.classList.add('unicode-escaped');
|
2024-02-25 03:11:51 +08:00
|
|
|
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');
|
2024-02-25 03:11:51 +08:00
|
|
|
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);
|
2022-01-07 09:18:52 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|