gitea源码

QuickSubmit.ts 1.1KB

1234567891011121314151617181920212223242526
  1. import {querySingleVisibleElem} from '../../utils/dom.ts';
  2. export function handleGlobalEnterQuickSubmit(target: HTMLElement) {
  3. let form = target.closest('form');
  4. if (form) {
  5. if (!form.checkValidity()) {
  6. form.reportValidity();
  7. } else {
  8. // here use the event to trigger the submit event (instead of calling `submit()` method directly)
  9. // otherwise the `areYouSure` handler won't be executed, then there will be an annoying "confirm to leave" dialog
  10. form.dispatchEvent(new SubmitEvent('submit', {bubbles: true, cancelable: true}));
  11. }
  12. return true;
  13. }
  14. form = target.closest('.ui.form');
  15. if (form) {
  16. // A form should only have at most one "primary" button to do quick-submit.
  17. // Here we don't use a special class to mark the primary button,
  18. // because there could be a lot of forms with a primary button, the quick submit should work out-of-box,
  19. // but not keeps asking developers to add that special class again and again (it could be forgotten easily)
  20. querySingleVisibleElem<HTMLButtonElement>(form, '.ui.primary.button')?.click();
  21. return true;
  22. }
  23. return false;
  24. }