gitea源码

common-form.ts 1.5KB

123456789101112131415161718192021222324252627282930313233343536
  1. import {applyAreYouSure, initAreYouSure} from '../vendor/jquery.are-you-sure.ts';
  2. import {handleGlobalEnterQuickSubmit} from './comp/QuickSubmit.ts';
  3. import {queryElems, type DOMEvent} from '../utils/dom.ts';
  4. import {initComboMarkdownEditor} from './comp/ComboMarkdownEditor.ts';
  5. export function initGlobalFormDirtyLeaveConfirm() {
  6. initAreYouSure(window.jQuery);
  7. // Warn users that try to leave a page after entering data into a form.
  8. // Except on sign-in pages, and for forms marked as 'ignore-dirty'.
  9. if (!document.querySelector('.page-content.user.signin')) {
  10. applyAreYouSure('form:not(.ignore-dirty)');
  11. }
  12. }
  13. export function initGlobalEnterQuickSubmit() {
  14. document.addEventListener('keydown', (e: DOMEvent<KeyboardEvent>) => {
  15. if (e.key !== 'Enter') return;
  16. const hasCtrlOrMeta = ((e.ctrlKey || e.metaKey) && !e.altKey);
  17. if (hasCtrlOrMeta && e.target.matches('textarea')) {
  18. if (handleGlobalEnterQuickSubmit(e.target as HTMLElement)) {
  19. e.preventDefault();
  20. }
  21. } else if (e.target.matches('input') && !e.target.closest('form')) {
  22. // input in a normal form could handle Enter key by default, so we only handle the input outside a form
  23. // eslint-disable-next-line unicorn/no-lonely-if
  24. if (handleGlobalEnterQuickSubmit(e.target as HTMLElement)) {
  25. e.preventDefault();
  26. }
  27. }
  28. });
  29. }
  30. export function initGlobalComboMarkdownEditor() {
  31. queryElems<HTMLElement>(document, '.combo-markdown-editor:not(.custom-init)', (el) => initComboMarkdownEditor(el));
  32. }