Files
wekan/client/lib/inlinedform.js
T
Lauri Ojansivu 42b4e51f85 Fix #2418: one inlined edit form at a time; no cross-item overwrite.
Since 8c3ce4bb7 (2021) the "close the previous inlined form" call was a
silent no-op: the inlinedForm escape action registers with
enabledOnClick: false and EscapeActions.clickExecute skips such actions,
so clicking another checklist item left both edit forms open - and the
submit handlers find the template's FIRST textarea, so the wrong form's
text was saved over the submitted item. Subtasks reproduced the full
bug; the checklists workaround corrupted the shared open-form tracker so
Escape fell through and closed the whole card details pane. A pure state
manager (client/lib/inlinedFormManager.js) now enforces the single-open
invariant: opening a form closes the tracked previous one (popups
untouched, preserving the 2021 intent); close/destroy only reset the
tracker for the form they own.

Tests: tests/inlinedFormSingleOpen.test.cjs (9, positive and negative,
including the exact reported A->B->C repro).

Thanks to Beebo89 and xet7.
2026-07-17 03:30:07 +03:00

110 lines
3.2 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 {
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) {
if (evt.keyCode === 13 && (evt.metaKey || evt.ctrlKey)) {
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,
},
);