Files
wekan/client/lib/inlinedform.js
T
Lauri Ojansivu bb6b0d5477 Member Settings: optional "Submit editors with Enter" (per-user, off by default).
#4236 made every multi-line editor submit only on Ctrl/Cmd+Enter (plain Enter
= newline) so titles could be multi-line, but that removed the fast
Enter-to-save title workflow some users rely on (v10 email feedback). Add a
per-user preference in the Member Settings (Change Settings) popup, saved to
profile.submitOnEnter and OFF by default: when on, plain Enter submits the card
title, description and other inlined forms, and Shift+Enter inserts a newline;
when off, behaviour is unchanged (Ctrl/Cmd+Enter saves).

The rule stays in the single pure isSubmitKey(event, {submitOnEnter}); the card
title, the card-description override and the shared client/lib/inlinedform.js
all route through it and read the preference from the current user. New toggle
+ helper + toggleSubmitOnEnter method + schema field, i18n (en, fi), and
editorSubmitKey.test.cjs extended with positive + negative opt-in cases.

Thanks to xet7 !

Related to #4236.
2026-07-18 18:14:33 +03:00

115 lines
3.5 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 })) {
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,
},
);