From 0695d6abe6b5663e1e07dd71bd2802ab2c1c6499 Mon Sep 17 00:00:00 2001 From: Alexey Cherepanov Date: Sun, 29 Nov 2026 12:37:07 +0300 Subject: [PATCH] #19 Add Ctrl/Cmd+S save shortcut in Monaco code editors Co-authored-by: Alexey Cherepanov --- .../form-level-actions-in-settings.md | 7 +- .../content/help/common-problems-and-fixes.md | 7 +- .../content/help/faq-for-form-authors.md | 4 +- .../content/help/keyboard-shortcuts.md | 28 ++++++-- .../save-export-and-import-forms.md | 3 +- ...llapse-panels-and-resize-the-code-panel.md | 6 ++ .../calculation/GenericCodeEditor.tsx | 22 +++--- .../components/CodeEditor/CodeEditorProps.ts | 5 ++ .../CodeEditor/ConstrainedCodeEditor.tsx | 22 +++++- .../CodeEditor/UnconstrainedCodeEditor.tsx | 9 ++- .../registerCodeEditorSaveAction.ts | 29 ++++++++ .../tests/generic-code-editor-save.spec.tsx | 56 ++++++++++++++- tests/module/tests/helpers/monacoFakes.ts | 3 +- .../register-code-editor-save-action.spec.ts | 68 +++++++++++++++++++ 14 files changed, 246 insertions(+), 23 deletions(-) create mode 100644 packages/designer/src/features/components/CodeEditor/registerCodeEditorSaveAction.ts create mode 100644 tests/module/tests/register-code-editor-save-action.spec.ts diff --git a/designer-guide/content/behavior-and-validation/form-level-actions-in-settings.md b/designer-guide/content/behavior-and-validation/form-level-actions-in-settings.md index 9861b4e7c..1a1e26cb2 100644 --- a/designer-guide/content/behavior-and-validation/form-level-actions-in-settings.md +++ b/designer-guide/content/behavior-and-validation/form-level-actions-in-settings.md @@ -36,7 +36,9 @@ Field-level validation still lives on each field's **Rules** tab. Bottom panel code editor open for a new form-level code action. -3. Enter a **Name**, write or paste the script, and click **Save**. +3. Enter a **Name**, write or paste the script, then click **Save** (or press + Ctrl+S / + ⌘+S while the cursor is in the editor). 4. Select the button or other component that should run the action. 5. Open its **Actions** tab and click **Add an action** under the event you want to use, such as **onClick**. 6. Open **Code** and select the saved action by name. @@ -52,7 +54,7 @@ Field-level validation still lives on each field's **Rules** tab. Bottom panel code editor open for Edit form validation code. 3. Edit validator code only when your team assigns you that task. Many teams restrict this to developers. -4. Click **Save** when finished. +4. Click **Save** when finished (or use the code editor save shortcut). ## Test @@ -69,5 +71,6 @@ trigger that event in **Preview**. ## See also +- [Keyboard shortcuts](/help/keyboard-shortcuts) - [Actions basics](/behavior-and-validation/actions-basics) - [Actions (developer depth)](docs:formengine-designer/features/actions) diff --git a/designer-guide/content/help/common-problems-and-fixes.md b/designer-guide/content/help/common-problems-and-fixes.md index 138b267e1..32ae50a1c 100644 --- a/designer-guide/content/help/common-problems-and-fixes.md +++ b/designer-guide/content/help/common-problems-and-fixes.md @@ -11,9 +11,12 @@ Quick fixes for common issues that form authors encounter. :::warning -**Symptom:** Delete or Ctrl+S has no effect. +**Symptom:** Delete or Ctrl+S has no effect on the form. -**Fix:** Click the canvas background so focus is not inside a text field. See [Keyboard shortcuts](/help/keyboard-shortcuts). +**Fix:** Click the canvas background so focus is not inside a text field. If the bottom **code editor** is open, +Ctrl+S / +⌘+S saves the script, not the form. See +[Keyboard shortcuts](/help/keyboard-shortcuts). ::: diff --git a/designer-guide/content/help/faq-for-form-authors.md b/designer-guide/content/help/faq-for-form-authors.md index d86eaf350..f4ea0b4bd 100644 --- a/designer-guide/content/help/faq-for-form-authors.md +++ b/designer-guide/content/help/faq-for-form-authors.md @@ -35,7 +35,9 @@ See [Preview and Edit](/meet-the-designer/preview-and-edit). Most shortcuts need focus on the page background, not inside a text box. Click the canvas margin first. **Print** (Ctrl+P / -⌘+P) still works inside a field. +⌘+P) still works inside a field. In the bottom +**code editor**, Ctrl+S / +⌘+S saves the script instead of the form. See [Keyboard shortcuts](/help/keyboard-shortcuts). ## Validation does not run on the canvas diff --git a/designer-guide/content/help/keyboard-shortcuts.md b/designer-guide/content/help/keyboard-shortcuts.md index 42ee49df0..5d29a684e 100644 --- a/designer-guide/content/help/keyboard-shortcuts.md +++ b/designer-guide/content/help/keyboard-shortcuts.md @@ -1,12 +1,14 @@ --- title: "Keyboard shortcuts" -description: "Designer keyboard shortcuts for save, print, undo, redo, duplicate, and delete. Windows/Linux and macOS." +description: "Designer keyboard shortcuts for save, print, undo, redo, duplicate, delete, and saving code in the bottom panel editor." --- # Keyboard shortcuts -Shortcuts apply when focus is on the **page background** (not while typing inside a field). Click an empty area of the canvas first. -**Print** is the exception: it still works with focus inside a field. +Most shortcuts apply when focus is on the **page background** (not while typing inside a field). Click an empty area of the canvas first. +**Print** still works with focus inside a field. In the bottom **code editor**, +Ctrl+S / +⌘+S saves the script instead of the whole form. ## Windows and Linux @@ -35,6 +37,22 @@ Shortcuts apply when focus is on the **page background** (not while typing insid | Undo | ⌘+Z | | Redo | ⌘+Shift+Z | +## Code editor + +The bottom panel opens a code editor for calculable values, validation **Code**, form actions, and similar scripts. While the cursor is in +that editor, the same chord as **Save form** saves the **script** (same as clicking **Save** under the editor). It does not save the form +to storage. + +Bottom panel code editor with Save and Close under the script. + +| Action | Windows / Linux | macOS | +| ----------- | -------------------------------------------------------------- | ----------------------------------------------------------- | +| Save script | Ctrl+S | ⌘+S | + +Close the code panel or click the canvas when you want +Ctrl+S / +⌘+S to save the form again. + ## Tips - Select exactly one component to duplicate with Ctrl+D (Windows/Linux) or @@ -44,6 +62,8 @@ Shortcuts apply when focus is on the **page background** (not while typing insid ## See also -- [Select and edit components](/meet-the-designer/select-and-edit-components) - selection basics +- [Collapse panels and resize the code panel](/meet-the-designer/collapse-panels-and-resize-the-code-panel) +- [Form-level actions in Settings](/behavior-and-validation/form-level-actions-in-settings) +- [Select and edit components](/meet-the-designer/select-and-edit-components) - [Print your form](/preview-and-handoff/print-your-form) - [Export and share your form](/preview-and-handoff/export-and-share-your-form) diff --git a/designer-guide/content/manage-forms/save-export-and-import-forms.md b/designer-guide/content/manage-forms/save-export-and-import-forms.md index 42d7965d4..cb98e5778 100644 --- a/designer-guide/content/manage-forms/save-export-and-import-forms.md +++ b/designer-guide/content/manage-forms/save-export-and-import-forms.md @@ -22,7 +22,8 @@ For a short overview, see also [Export and share your form](/preview-and-handoff | **Save as...** | You need a copy under a new name | The save shortcut is Ctrl+S on Windows/Linux or -⌘+S on macOS when focus is on the page background. +⌘+S on macOS when focus is on the page background. The same chord +in the bottom **code editor** saves the script, not the form. See [Keyboard shortcuts](/help/keyboard-shortcuts). Header menu open with Save highlighted. diff --git a/designer-guide/content/meet-the-designer/collapse-panels-and-resize-the-code-panel.md b/designer-guide/content/meet-the-designer/collapse-panels-and-resize-the-code-panel.md index c13c174f5..ddf967226 100644 --- a/designer-guide/content/meet-the-designer/collapse-panels-and-resize-the-code-panel.md +++ b/designer-guide/content/meet-the-designer/collapse-panels-and-resize-the-code-panel.md @@ -35,6 +35,11 @@ The bottom panel opens when you edit items that use code or JSON, for example: 5. Drag the **top edge** of the bottom panel up or down to change height. 6. Close or finish editing to hide it when your workflow allows (use the panel **Close** control or finish the property dialog). +While the cursor is in the editor, +Ctrl+S / +⌘+S saves the script (same as **Save** under the editor). See +[Keyboard shortcuts](/help/keyboard-shortcuts). + Height may be remembered during your session. ## Summary @@ -44,5 +49,6 @@ Expand the side panels again before changing properties. ## See also +- [Keyboard shortcuts](/help/keyboard-shortcuts) - [The Designer workspace](/meet-the-designer/the-designer-workspace) - [Right panel](/meet-the-designer/use-the-right-properties-panel) diff --git a/packages/designer/src/features/calculation/GenericCodeEditor.tsx b/packages/designer/src/features/calculation/GenericCodeEditor.tsx index 8e065764a..adbdc4bc2 100644 --- a/packages/designer/src/features/calculation/GenericCodeEditor.tsx +++ b/packages/designer/src/features/calculation/GenericCodeEditor.tsx @@ -123,15 +123,20 @@ export const GenericCodeEditor = (props: GenericCodeEditorProps) => { [getSourceForSave] ) - const handleSave = useCallback(() => { - const valueToSave = resolveSourceForSave(sourceCode) - const testResult = validateBeforeSave?.(valueToSave) === 'skip-test' ? undefined : testingEditorRef.current.test?.() - if (resolveGenericCodeEditorSave(valueToSave, {validateBeforeSave, testResult}) === 'abort') return + const handleSaveValue = useCallback( + (value: string) => { + const valueToSave = resolveSourceForSave(value) + const testResult = validateBeforeSave?.(valueToSave) === 'skip-test' ? undefined : testingEditorRef.current.test?.() + if (resolveGenericCodeEditorSave(valueToSave, {validateBeforeSave, testResult}) === 'abort') return - action(() => onSave(valueToSave))() - setInitialValue(valueToSave) - setSourceCode(valueToSave) - }, [onSave, resolveSourceForSave, sourceCode, validateBeforeSave]) + action(() => onSave(valueToSave))() + setInitialValue(valueToSave) + setSourceCode(valueToSave) + }, + [onSave, resolveSourceForSave, validateBeforeSave] + ) + + const handleSave = useCallback(() => handleSaveValue(sourceCode), [handleSaveValue, sourceCode]) const testingFunction = useCallback( (formData: IFormData) => onCompile(resolveSourceForSave(sourceCode), formData), @@ -153,6 +158,7 @@ export const GenericCodeEditor = (props: GenericCodeEditorProps) => { autoformat={props.autoformat} onMount={props.onEditorMount} onChange={setSourceCode} + onSave={handleSaveValue} />
diff --git a/packages/designer/src/features/components/CodeEditor/CodeEditorProps.ts b/packages/designer/src/features/components/CodeEditor/CodeEditorProps.ts index 4c98fde8c..d22d09e12 100644 --- a/packages/designer/src/features/components/CodeEditor/CodeEditorProps.ts +++ b/packages/designer/src/features/components/CodeEditor/CodeEditorProps.ts @@ -19,6 +19,11 @@ export interface UnconstrainedCodeEditorProps { * @param value the source code. */ onChange: (value: string) => void + /** + * The callback function that is called when save is requested (Ctrl/Cmd+S). + * @param value the source code to save. + */ + onSave?: (value: string) => void /** * The code editor language. * @default 'javascript' diff --git a/packages/designer/src/features/components/CodeEditor/ConstrainedCodeEditor.tsx b/packages/designer/src/features/components/CodeEditor/ConstrainedCodeEditor.tsx index 08123ee89..ea94cf5bd 100644 --- a/packages/designer/src/features/components/CodeEditor/ConstrainedCodeEditor.tsx +++ b/packages/designer/src/features/components/CodeEditor/ConstrainedCodeEditor.tsx @@ -4,6 +4,7 @@ import {useCallback, useEffect, useMemo, useRef} from 'react' import type {CodeEditorProps} from './CodeEditorProps' import {buildConstrainedDocument, extractEditableBody, resolveNextConstrainedDocument} from './constrainedDocument' import {MonacoCodeEditor} from './MonacoCodeEditor' +import {registerCodeEditorSaveAction} from './registerCodeEditorSaveAction' import type {ConstrainedMonacoEditorModel} from './setupConstrainedMonaco' import {setupConstrainedMonaco, withSuppressedConstrainedValidation} from './setupConstrainedMonaco' @@ -43,7 +44,8 @@ const applyConstrainedEditorValue = ( * @returns the React element. */ export const ConstrainedCodeEditor = (props: CodeEditorProps) => { - const {initialValue, onChange, onMount} = props + const {initialValue, onChange, onMount, onSave} = props + const onSaveRef = useRef(onSave) const context = useMemo( () => ({ beginContextLine: props.beginContextLine, @@ -52,6 +54,15 @@ export const ConstrainedCodeEditor = (props: CodeEditorProps) => { }), [props.beginContextLine, props.canEditBeginContextLine, props.endContextLine] ) + const contextRef = useRef(context) + + useEffect(() => { + onSaveRef.current = onSave + }, [onSave]) + + useEffect(() => { + contextRef.current = context + }, [context]) const documentValue = useMemo( () => buildConstrainedDocument(initialValue, context.beginContextLine, context.canEditBeginContextLine, context.endContextLine), @@ -93,6 +104,15 @@ export const ConstrainedCodeEditor = (props: CodeEditorProps) => { onMount?.(editor, monaco) editorRef.current = editor constrainedModelRef.current = setupConstrainedMonaco(editor, monaco, context, onChange) + registerCodeEditorSaveAction( + editor, + monaco, + value => onSaveRef.current?.(value), + e => { + const {beginContextLine, canEditBeginContextLine, endContextLine} = contextRef.current + return extractEditableBody(e.getValue(), beginContextLine, canEditBeginContextLine, endContextLine) + } + ) appliedSourceRef.current = {initialValue, context} }, [context, initialValue, onChange, onMount] diff --git a/packages/designer/src/features/components/CodeEditor/UnconstrainedCodeEditor.tsx b/packages/designer/src/features/components/CodeEditor/UnconstrainedCodeEditor.tsx index 5b2164549..1fcb6abcd 100644 --- a/packages/designer/src/features/components/CodeEditor/UnconstrainedCodeEditor.tsx +++ b/packages/designer/src/features/components/CodeEditor/UnconstrainedCodeEditor.tsx @@ -3,6 +3,7 @@ import type {editor} from 'monaco-editor' import {useCallback, useEffect, useMemo, useRef} from 'react' import type {UnconstrainedCodeEditorProps} from './CodeEditorProps' import {MonacoCodeEditor} from './MonacoCodeEditor' +import {registerCodeEditorSaveAction} from './registerCodeEditorSaveAction' import {useMonacoAutoformat} from './useMonacoAutoformat' /** @@ -12,7 +13,12 @@ import {useMonacoAutoformat} from './useMonacoAutoformat' */ export const UnconstrainedCodeEditor = (props: UnconstrainedCodeEditorProps) => { const editorRef = useRef(null) - const {onChange, onMount} = props + const {onChange, onMount, onSave} = props + const onSaveRef = useRef(onSave) + + useEffect(() => { + onSaveRef.current = onSave + }, [onSave]) useEffect(() => { const editor = editorRef.current @@ -26,6 +32,7 @@ export const UnconstrainedCodeEditor = (props: UnconstrainedCodeEditorProps) => (editor, monaco) => { editor.onDidChangeModelContent(() => onChange(editor.getValue())) editorRef.current = editor + registerCodeEditorSaveAction(editor, monaco, value => onSaveRef.current?.(value)) onMount?.(editor, monaco) }, diff --git a/packages/designer/src/features/components/CodeEditor/registerCodeEditorSaveAction.ts b/packages/designer/src/features/components/CodeEditor/registerCodeEditorSaveAction.ts new file mode 100644 index 000000000..57ed58541 --- /dev/null +++ b/packages/designer/src/features/components/CodeEditor/registerCodeEditorSaveAction.ts @@ -0,0 +1,29 @@ +import type {Monaco} from '@monaco-editor/react' +import type {editor, IDisposable} from 'monaco-editor' + +/** + * Registers Ctrl/Cmd+S on a Monaco editor instance to invoke {@link onSave}. + * Uses {@link editor.IStandaloneCodeEditor.addAction} so the chord is scoped to this editor + * (same approach as constrained Ctrl/Cmd+A). + * @param editorInstance the Monaco standalone editor. + * @param monaco the Monaco namespace. + * @param onSave the callback that receives the value to save. + * @param getSaveValue resolves the string passed to {@link onSave}; defaults to the full editor value. + * @returns the action disposable. + */ +export function registerCodeEditorSaveAction( + editorInstance: editor.IStandaloneCodeEditor, + monaco: Monaco, + onSave: (value: string) => void, + getSaveValue: (ed: editor.ICodeEditor) => string = ed => ed.getValue() +): IDisposable { + return editorInstance.addAction({ + id: 'code-editor.save', + label: 'Save', + keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS], + precondition: 'editorTextFocus', + run: ed => { + onSave(getSaveValue(ed)) + }, + }) +} diff --git a/tests/component/tests/generic-code-editor-save.spec.tsx b/tests/component/tests/generic-code-editor-save.spec.tsx index 6d1d8b909..b4d497c51 100644 --- a/tests/component/tests/generic-code-editor-save.spec.tsx +++ b/tests/component/tests/generic-code-editor-save.spec.tsx @@ -58,12 +58,25 @@ vi.mock('../../../packages/designer/src/features/ui/BottomPanel/TestingEditor', }) vi.mock('../../../packages/designer/src/features/components/CodeEditor/CodeEditor', () => ({ - CodeEditor: ({onChange, initialValue}: {onChange: (value: string) => void; initialValue: string}) => { + CodeEditor: ({ + onChange, + onSave, + initialValue, + }: { + onChange: (value: string) => void + onSave?: (value: string) => void + initialValue: string + }) => { useEffect(() => { onChange(initialValue) }, [initialValue, onChange]) - return