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