Skip to content

Commit c86d033

Browse files
Remove jQuery from the Unicode escape button (go-gitea#29369)
- Switched to plain JavaScript - Tested the Unicode escape button functionality and it works as before # Demo using JavaScript without jQuery ![action](https://github.com/go-gitea/gitea/assets/20454870/664f0ced-876b-4cb7-a668-bd62169fc843) --------- Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 4197e28 commit c86d033

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed
Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
import $ from 'jquery';
2-
import {hideElem, showElem} from '../utils/dom.js';
1+
import {hideElem, queryElemSiblings, showElem, toggleElem} from '../utils/dom.js';
32

43
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+
188
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);
2925
}
3026
});
3127
}

web_src/js/utils/dom.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export function isElemHidden(el) {
5151
return res[0];
5252
}
5353

54+
export function queryElemSiblings(el, selector) {
55+
return Array.from(el.parentNode.children).filter((child) => child !== el && child.matches(selector));
56+
}
57+
5458
export function onDomReady(cb) {
5559
if (document.readyState === 'loading') {
5660
document.addEventListener('DOMContentLoaded', cb);

0 commit comments

Comments
 (0)