gitea源码

repo-unicode-escape.ts 1.4KB

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