From 96e45374da39d5bd42ac573cb66281d8e030969f Mon Sep 17 00:00:00 2001 From: Dave Page Date: Thu, 26 Nov 2026 21:04:59 +0000 Subject: [PATCH] Stop Ctrl/Cmd+Enter adding a newline to a SQL field before a dialog saves it. #7167 The shared CodeMirror editor kept the default Mod-Enter binding (insertBlankLine), so with focus in a SQL field of a dialog the newline was inserted before the dialog's Ctrl/Cmd+Enter handler saved, and was saved with the value. The dialog now marks itself with data-save-on-mod-enter and the editor claims Mod-Enter, without editing the text, when it sits inside such an element. Editors elsewhere, such as the Query Tool's, are unaffected. Closes #7167 --- .../static/js/SchemaView/SchemaDialogView.jsx | 6 ++- .../ReactCodeMirror/components/Editor.jsx | 12 +++++- .../SchemaDialogViewKeyboard.spec.js | 40 +++++++++++++++++++ .../components/CodeMirrorCustomEditor.spec.js | 26 ++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) diff --git a/web/pgadmin/static/js/SchemaView/SchemaDialogView.jsx b/web/pgadmin/static/js/SchemaView/SchemaDialogView.jsx index 6c68ae804d5..d1a27771ea7 100644 --- a/web/pgadmin/static/js/SchemaView/SchemaDialogView.jsx +++ b/web/pgadmin/static/js/SchemaView/SchemaDialogView.jsx @@ -296,7 +296,11 @@ export default function SchemaDialogView({ , [schema._id, viewHelperProps.mode, resetKey] ); - return {dialogContent}; + // data-save-on-mod-enter tells a SQL editor in the dialog not to insert a + // newline for Ctrl/Cmd+Enter, which would otherwise be saved (issue #7167). + return + {dialogContent} + ; } SchemaDialogView.propTypes = { diff --git a/web/pgadmin/static/js/components/ReactCodeMirror/components/Editor.jsx b/web/pgadmin/static/js/components/ReactCodeMirror/components/Editor.jsx index 69ff2a7f45f..5bf0da8fc49 100644 --- a/web/pgadmin/static/js/components/ReactCodeMirror/components/Editor.jsx +++ b/web/pgadmin/static/js/components/ReactCodeMirror/components/Editor.jsx @@ -334,7 +334,17 @@ export default function Editor({ if (!checkIsMounted()) return; const keys = keymap.of([ // Filtering out the default keymaps so that it uses the custom keymaps. - customKeyMap??[], ...completionKeymap.filter(k => k.key != 'Ctrl-Space'), + customKeyMap??[], + // In a dialog, Ctrl/Cmd+Enter saves and closes it (issue #7167), so + // claim the key there to stop defaultKeymap's Mod-Enter + // (insertBlankLine) adding a newline that then gets saved. The keydown + // still bubbles up to the dialog's handler. Elsewhere, such as the + // Query Tool, this returns false and Mod-Enter behaves as before. + { + key: 'Mod-Enter', + run: (view) => Boolean(view.dom.closest('[data-save-on-mod-enter]')), + }, + ...completionKeymap.filter(k => k.key != 'Ctrl-Space'), defaultKeymap.filter(k => k.key != 'Mod-/'), closeBracketsKeymap, historyKeymap, foldKeymap ].flat()); diff --git a/web/regression/javascript/SchemaView/SchemaDialogViewKeyboard.spec.js b/web/regression/javascript/SchemaView/SchemaDialogViewKeyboard.spec.js index 5eb3f26e67e..28a119a7cd8 100644 --- a/web/regression/javascript/SchemaView/SchemaDialogViewKeyboard.spec.js +++ b/web/regression/javascript/SchemaView/SchemaDialogViewKeyboard.spec.js @@ -9,6 +9,7 @@ import { act, fireEvent, render, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; +import { EditorView } from '@codemirror/view'; import BaseUISchema from 'sources/SchemaView/base_schema.ui'; import SchemaView from '../../../pgadmin/static/js/SchemaView'; @@ -33,6 +34,19 @@ class MinimalSchema extends BaseUISchema { } } +class SQLFieldSchema extends BaseUISchema { + constructor() { + super({sqlfield: 'a = 1'}); + } + + get baseFields() { + return [{ + id: 'sqlfield', label: 'SQL', type: 'sql', group: null, + mode: ['create'], + }]; + } +} + // Escape closes a dialog rendered as a dockable panel (issue #5691). The // handler sits on the dialog wrapper, which must not be memoized along with // the dialog body: the body only changes with the schema, the mode or the @@ -188,4 +202,30 @@ describe('SchemaDialogView keyboard handling', () => { await act(async () => { resolveSave(); }); await waitFor(() => expect(onClose).toHaveBeenCalledTimes(1)); }); + + // With focus in a SQL editor, CodeMirror's own Mod-Enter binding + // (insertBlankLine) used to add a newline before the dialog's handler + // saved, so the newline was saved along with the rest of the value. + it('does not add a newline to a SQL field on Ctrl/Cmd+Enter', async () => { + const onClose = jest.fn(); + const onSave = jest.fn(() => Promise.resolve()); + const ctrl = await renderDialog(onClose, onSave, new SQLFieldSchema()); + const editor = EditorView.findFromDOM( + ctrl.container.querySelector('.cm-editor') + ); + editor.dispatch({selection: {anchor: editor.state.doc.length}}); + + await act(async () => { + // jsdom does not report a Mac platform, so Mod is Ctrl here. + fireEvent.keyDown(editor.contentDOM, { + key: 'Enter', code: 'Enter', keyCode: 13, ctrlKey: true, + }); + }); + + expect(editor.state.doc.toString()).toBe('a = 1'); + expect(onSave).toHaveBeenCalledWith( + true, expect.objectContaining({sqlfield: 'a = 1'}) + ); + await waitFor(() => expect(onClose).toHaveBeenCalled()); + }); }); diff --git a/web/regression/javascript/components/CodeMirrorCustomEditor.spec.js b/web/regression/javascript/components/CodeMirrorCustomEditor.spec.js index cf2e3b15b38..3dd5bc9f059 100755 --- a/web/regression/javascript/components/CodeMirrorCustomEditor.spec.js +++ b/web/regression/javascript/components/CodeMirrorCustomEditor.spec.js @@ -355,6 +355,32 @@ describe('CodeMirrorCustomEditorView', ()=>{ 'select 1;\nselect 2;\nselect 1;\nselect 2;\nselect 3;'); }); + // Outside a dialog, Mod-Enter keeps CodeMirror's default insertBlankLine, + // which the Query Tool relies on unless a shortcut of its own claims the + // key. Inside a dialog it saves instead, so must not edit the text (#7167). + it('Mod-Enter inserts a blank line outside a dialog',()=>{ + cmRerender({value: 'select 1;'}); + editor.dispatch({selection: {anchor: 9}}); + runScopeHandlers(editor, new KeyboardEvent('keydown', { + key: 'Enter', code: 'Enter', keyCode: 13, ctrlKey: true, + }), 'editor'); + expect(editor.state.doc.toString()).toEqual('select 1;\n'); + }); + + it('Mod-Enter leaves the text alone inside a dialog',()=>{ + cmInstance.rerender( +
+ { editor = obj; }} /> +
+ ); + editor.dispatch({selection: {anchor: 9}}); + const handled = runScopeHandlers(editor, new KeyboardEvent('keydown', { + key: 'Enter', code: 'Enter', keyCode: 13, ctrlKey: true, + }), 'editor'); + expect(handled).toBe(true); + expect(editor.state.doc.toString()).toEqual('select 1;'); + }); + describe('replaceSelection', ()=>{ it('keeps the replaced selection selected', ()=>{ cmRerender({value: 'select 1;'});