Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion web/pgadmin/static/js/SchemaView/SchemaDialogView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ export default function SchemaDialogView({
</>, [schema._id, viewHelperProps.mode, resetKey]
);

return <StyledBox onKeyDown={onKeyDown}>{dialogContent}</StyledBox>;
// 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 <StyledBox onKeyDown={onKeyDown} data-save-on-mod-enter>
{dialogContent}
</StyledBox>;
}

SchemaDialogView.propTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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());
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<div data-save-on-mod-enter>
<ThemedCM value={'select 1;'} currEditor={(obj) => { editor = obj; }} />
</div>
);
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;'});
Expand Down
Loading