// 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') { // What the field held when the form opened. Selecting it lets the // first key replace an existing value (a title being edited); but // text typed or pasted in the 50 ms before the timer ran is the // user's, and selecting THAT let the next key - Enter for a new // line - replace it, leaving only "\n" (editor-enter-preference // e2e: the add-card and card-title editors). Leave it alone then. const opened = input.value; setTimeout(() => { if (input.value !== opened) return; 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, }, );