|
1 | 1 | import {addDelegatedEventListener, hideElem, queryElemSiblings, showElem, toggleElem} from '../utils/dom.ts';
|
2 | 2 |
|
3 | 3 | export function initUnicodeEscapeButton() {
|
| 4 | + // buttons might appear on these pages: file view (code view), diff, blame, rendered content (markdown, etc.) |
4 | 5 | addDelegatedEventListener(document, 'click', '.escape-button, .unescape-button, .toggle-escape-button', (btn, e) => {
|
5 | 6 | e.preventDefault();
|
6 | 7 |
|
7 |
| - const fileContentElemId = btn.getAttribute('data-file-content-elem-id'); |
8 |
| - const fileContent = fileContentElemId ? |
9 |
| - document.querySelector(`#${fileContentElemId}`) : |
| 8 | + const unicodeContentSelector = btn.getAttribute('data-unicode-content-selector'); |
| 9 | + const container = unicodeContentSelector ? |
| 10 | + document.querySelector(unicodeContentSelector) : |
10 | 11 | btn.closest('.file-content, .non-diff-file-content');
|
11 |
| - const fileView = fileContent?.querySelectorAll('.file-code, .file-view'); |
| 12 | + const fileView = container.querySelector('.file-code, .file-view') || container; |
12 | 13 | if (btn.matches('.escape-button')) {
|
13 |
| - for (const el of fileView) el.classList.add('unicode-escaped'); |
| 14 | + fileView.classList.add('unicode-escaped'); |
14 | 15 | hideElem(btn);
|
15 | 16 | showElem(queryElemSiblings(btn, '.unescape-button'));
|
16 | 17 | } else if (btn.matches('.unescape-button')) {
|
17 |
| - for (const el of fileView) el.classList.remove('unicode-escaped'); |
| 18 | + fileView.classList.remove('unicode-escaped'); |
18 | 19 | hideElem(btn);
|
19 | 20 | showElem(queryElemSiblings(btn, '.escape-button'));
|
20 | 21 | } else if (btn.matches('.toggle-escape-button')) {
|
21 |
| - const isEscaped = fileView[0]?.classList.contains('unicode-escaped'); |
22 |
| - for (const el of fileView) el.classList.toggle('unicode-escaped', !isEscaped); |
23 |
| - toggleElem(fileContent.querySelectorAll('.unescape-button'), !isEscaped); |
24 |
| - toggleElem(fileContent.querySelectorAll('.escape-button'), isEscaped); |
| 22 | + const isEscaped = fileView.classList.contains('unicode-escaped'); |
| 23 | + fileView.classList.toggle('unicode-escaped', !isEscaped); |
| 24 | + toggleElem(container.querySelectorAll('.unescape-button'), !isEscaped); |
| 25 | + toggleElem(container.querySelectorAll('.escape-button'), isEscaped); |
25 | 26 | }
|
26 | 27 | });
|
27 | 28 | }
|
0 commit comments