2024-11-26 01:24:56 +00:00
|
|
|
import {addDelegatedEventListener, hideElem, queryElemSiblings, showElem, toggleElem} from '../utils/dom.ts';
|
2022-01-28 21:00:11 +00:00
|
|
|
|
2022-01-07 01:18:52 +00:00
|
|
|
export function initUnicodeEscapeButton() {
|
2024-12-12 18:37:44 +00:00
|
|
|
// buttons might appear on these pages: file view (code, rendered markdown), diff (commit, pr conversation, pr diff), blame, wiki
|
2024-11-26 01:24:56 +00:00
|
|
|
addDelegatedEventListener(document, 'click', '.escape-button, .unescape-button, .toggle-escape-button', (btn, e) => {
|
2022-01-07 01:18:52 +00:00
|
|
|
e.preventDefault();
|
2024-02-24 19:11:51 +00:00
|
|
|
|
2024-12-12 18:37:44 +00:00
|
|
|
const unicodeContentSelector = btn.getAttribute('data-unicode-content-selector');
|
|
|
|
const container = unicodeContentSelector ?
|
|
|
|
document.querySelector(unicodeContentSelector) :
|
2024-11-26 01:24:56 +00:00
|
|
|
btn.closest('.file-content, .non-diff-file-content');
|
2024-12-12 18:37:44 +00:00
|
|
|
const fileView = container.querySelector('.file-code, .file-view') ?? container;
|
2024-02-24 19:11:51 +00:00
|
|
|
if (btn.matches('.escape-button')) {
|
2024-12-12 18:37:44 +00:00
|
|
|
fileView.classList.add('unicode-escaped');
|
2024-02-24 19:11:51 +00:00
|
|
|
hideElem(btn);
|
|
|
|
showElem(queryElemSiblings(btn, '.unescape-button'));
|
|
|
|
} else if (btn.matches('.unescape-button')) {
|
2024-12-12 18:37:44 +00:00
|
|
|
fileView.classList.remove('unicode-escaped');
|
2024-02-24 19:11:51 +00:00
|
|
|
hideElem(btn);
|
|
|
|
showElem(queryElemSiblings(btn, '.escape-button'));
|
|
|
|
} else if (btn.matches('.toggle-escape-button')) {
|
2024-12-12 18:37:44 +00: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 01:18:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|