|
1 |
| -import $ from 'jquery'; |
2 |
| -import {hideElem, showElem} from '../utils/dom.js'; |
| 1 | +import {hideElem, queryElemSiblings, showElem, toggleElem} from '../utils/dom.js'; |
3 | 2 |
|
4 | 3 | export function initUnicodeEscapeButton() {
|
5 |
| - $(document).on('click', '.escape-button', (e) => { |
6 |
| - e.preventDefault(); |
7 |
| - $(e.target).parents('.file-content, .non-diff-file-content').find('.file-code, .file-view').addClass('unicode-escaped'); |
8 |
| - hideElem($(e.target)); |
9 |
| - showElem($(e.target).siblings('.unescape-button')); |
10 |
| - }); |
11 |
| - $(document).on('click', '.unescape-button', (e) => { |
12 |
| - e.preventDefault(); |
13 |
| - $(e.target).parents('.file-content, .non-diff-file-content').find('.file-code, .file-view').removeClass('unicode-escaped'); |
14 |
| - hideElem($(e.target)); |
15 |
| - showElem($(e.target).siblings('.escape-button')); |
16 |
| - }); |
17 |
| - $(document).on('click', '.toggle-escape-button', (e) => { |
| 4 | + document.addEventListener('click', (e) => { |
| 5 | + const btn = e.target.closest('.escape-button, .unescape-button, .toggle-escape-button'); |
| 6 | + if (!btn) return; |
| 7 | + |
18 | 8 | e.preventDefault();
|
19 |
| - const fileContent = $(e.target).parents('.file-content, .non-diff-file-content'); |
20 |
| - const fileView = fileContent.find('.file-code, .file-view'); |
21 |
| - if (fileView.hasClass('unicode-escaped')) { |
22 |
| - fileView.removeClass('unicode-escaped'); |
23 |
| - hideElem(fileContent.find('.unescape-button')); |
24 |
| - showElem(fileContent.find('.escape-button')); |
25 |
| - } else { |
26 |
| - fileView.addClass('unicode-escaped'); |
27 |
| - showElem(fileContent.find('.unescape-button')); |
28 |
| - hideElem(fileContent.find('.escape-button')); |
| 9 | + |
| 10 | + const fileContent = btn.closest('.file-content, .non-diff-file-content'); |
| 11 | + const fileView = fileContent?.querySelectorAll('.file-code, .file-view'); |
| 12 | + if (btn.matches('.escape-button')) { |
| 13 | + for (const el of fileView) el.classList.add('unicode-escaped'); |
| 14 | + hideElem(btn); |
| 15 | + showElem(queryElemSiblings(btn, '.unescape-button')); |
| 16 | + } else if (btn.matches('.unescape-button')) { |
| 17 | + for (const el of fileView) el.classList.remove('unicode-escaped'); |
| 18 | + hideElem(btn); |
| 19 | + showElem(queryElemSiblings(btn, '.escape-button')); |
| 20 | + } 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); |
29 | 25 | }
|
30 | 26 | });
|
31 | 27 | }
|
0 commit comments