mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 20:45:00 -05:00
Stop handled Enter events before the enclosing checklist submits them again. Await the insert, scope the textarea to its form, and reject duplicate submits while the write is pending. Keep failed edits available for retry. Thanks to rmb82 and xet7 ! Fixes #6700,
119 lines
3.7 KiB
JavaScript
119 lines
3.7 KiB
JavaScript
// A inlined form is used to provide a quick edition of single field for a given
|
|
// document. Clicking on a edit button should display the form to edit the field
|
|
// value. The form can then be submited, or just closed.
|
|
//
|
|
// When the form is closed we save non-submitted values in memory to avoid any
|
|
// data loss.
|
|
//
|
|
// Usage:
|
|
//
|
|
// +inlineForm
|
|
// // the content when the form is open
|
|
// else
|
|
// // the content when the form is close (optional)
|
|
|
|
import { Template } from 'meteor/templating';
|
|
import { ReactiveVar } from 'meteor/reactive-var';
|
|
import { Tracker } from 'meteor/tracker';
|
|
import { EscapeActions } from '/client/lib/escapeActions';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import { isSubmitKey } from '/models/lib/editorSubmitKey';
|
|
import {
|
|
openForm,
|
|
closeForm,
|
|
forgetForm,
|
|
} from '/client/lib/inlinedFormManager';
|
|
|
|
// We can only have one inlined form element opened at a time
|
|
const currentlyOpenedForm = new ReactiveVar(null);
|
|
|
|
Template.inlinedForm.onCreated(function () {
|
|
this.isOpen = new ReactiveVar(false);
|
|
});
|
|
|
|
Template.inlinedForm.onRendered(function () {
|
|
const tpl = this;
|
|
tpl.autorun(() => {
|
|
if (tpl.isOpen.get()) {
|
|
Tracker.afterFlush(() => {
|
|
const input = tpl.find('textarea,input[type=text]');
|
|
if (input && typeof input.focus === 'function') {
|
|
setTimeout(() => {
|
|
input.focus();
|
|
if (input.value && input.select) {
|
|
input.select();
|
|
}
|
|
}, 50);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
Template.inlinedForm.onDestroyed(function () {
|
|
// Only reset the tracker if THIS instance is the tracked one; destroying
|
|
// an unrelated instance must not orphan another still-open form.
|
|
forgetForm(currentlyOpenedForm, this);
|
|
});
|
|
|
|
Template.inlinedForm.helpers({
|
|
isOpen() {
|
|
return Template.instance().isOpen;
|
|
},
|
|
});
|
|
|
|
Template.inlinedForm.events({
|
|
'click .js-close-inlined-form'(evt, tpl) {
|
|
closeForm(currentlyOpenedForm, tpl);
|
|
},
|
|
'click .js-open-inlined-form'(evt, tpl) {
|
|
evt.preventDefault();
|
|
EscapeActions.clickExecute(evt.target, 'inlinedForm');
|
|
// #2418: clickExecute above does NOT close the previously opened form:
|
|
// the 'inlinedForm' escape action is registered with
|
|
// `enabledOnClick: false`, so click executions skip it. With the old
|
|
// form left open, submitting the new one located the wrong textarea
|
|
// (template-wide first match) and overwrote the item with the previous
|
|
// item's text. Close the previous form explicitly instead (this keeps
|
|
// popups open, which is why clickExecute replaced executeUpTo here).
|
|
openForm(currentlyOpenedForm, tpl);
|
|
},
|
|
'keydown form textarea'(evt, tpl) {
|
|
// Ctrl/Cmd+Enter submits by default; a user who enabled "submit on Enter"
|
|
// in Member Settings submits on plain Enter (Shift+Enter for a newline).
|
|
const submitOnEnter = !!ReactiveCache.getCurrentUser()?.hasSubmitOnEnter?.();
|
|
if (isSubmitKey(evt, { submitOnEnter })) {
|
|
// #6700: the enclosing checklist has its own Enter handler. A handled
|
|
// submit key must not bubble to it and create a second document.
|
|
evt.preventDefault();
|
|
evt.stopPropagation();
|
|
tpl.find('button[type=submit]').click();
|
|
}
|
|
},
|
|
submit(evt, tpl) {
|
|
const data = Template.currentData();
|
|
if (data.autoclose !== false) {
|
|
Tracker.afterFlush(() => {
|
|
closeForm(currentlyOpenedForm, tpl);
|
|
});
|
|
}
|
|
},
|
|
});
|
|
|
|
// Press escape to close the currently opened inlinedForm
|
|
EscapeActions.register(
|
|
'inlinedForm',
|
|
() => {
|
|
const form = currentlyOpenedForm.get();
|
|
if (form) {
|
|
closeForm(currentlyOpenedForm, form);
|
|
}
|
|
},
|
|
() => {
|
|
return currentlyOpenedForm.get() !== null;
|
|
},
|
|
{
|
|
enabledOnClick: false,
|
|
},
|
|
);
|