Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Field-level validation still lives on each field's **Rules** tab.

<img src="/assets/guide/behavior/highlight-form-action-code-editor.png" alt="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
<span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>S</kbd></span> /
<span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>S</kbd></span> 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.
Expand All @@ -52,7 +54,7 @@ Field-level validation still lives on each field's **Rules** tab.
<img src="/assets/guide/behavior/highlight-form-validator-code-editor.png" alt="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

Expand All @@ -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)
7 changes: 5 additions & 2 deletions designer-guide/content/help/common-problems-and-fixes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ Quick fixes for common issues that form authors encounter.

:::warning

**Symptom:** <kbd>Delete</kbd> or <span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>S</kbd></span> has no effect.
**Symptom:** <kbd>Delete</kbd> or <span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>S</kbd></span> 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,
<span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>S</kbd></span> /
<span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>S</kbd></span> saves the script, not the form. See
[Keyboard shortcuts](/help/keyboard-shortcuts).

:::

Expand Down
4 changes: 3 additions & 1 deletion designer-guide/content/help/faq-for-form-authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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** (<span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>P</kbd></span> /
<span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>P</kbd></span>) still works inside a field.
<span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>P</kbd></span>) still works inside a field. In the bottom
**code editor**, <span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>S</kbd></span> /
<span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>S</kbd></span> saves the script instead of the form.
See [Keyboard shortcuts](/help/keyboard-shortcuts).

## Validation does not run on the canvas
Expand Down
28 changes: 24 additions & 4 deletions designer-guide/content/help/keyboard-shortcuts.md
Original file line number Diff line number Diff line change
@@ -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**,
<span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>S</kbd></span> /
<span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>S</kbd></span> saves the script instead of the whole form.

## Windows and Linux

Expand Down Expand Up @@ -35,6 +37,22 @@ Shortcuts apply when focus is on the **page background** (not while typing insid
| Undo | <span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>Z</kbd></span> |
| Redo | <span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>Shift</kbd>+<kbd>Z</kbd></span> |

## 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.

<img src="/assets/guide/behavior/highlight-form-action-code-editor.png" alt="Bottom panel code editor with Save and Close under the script." />

| Action | Windows / Linux | macOS |
| ----------- | -------------------------------------------------------------- | ----------------------------------------------------------- |
| Save script | <span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>S</kbd></span> | <span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>S</kbd></span> |

Close the code panel or click the canvas when you want
<span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>S</kbd></span> /
<span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>S</kbd></span> to save the form again.

## Tips

- Select exactly one component to duplicate with <span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>D</kbd></span> (Windows/Linux) or
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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 <span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>S</kbd></span> on Windows/Linux or
<span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>S</kbd></span> on macOS when focus is on the page background.
<span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>S</kbd></span> 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).

<img src="/assets/guide/manage-forms/highlight-header-menu-save.png" alt="Header menu open with Save highlighted." />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
<span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>S</kbd></span> /
<span class="fe-key-combo"><kbd>⌘</kbd>+<kbd>S</kbd></span> saves the script (same as **Save** under the editor). See
[Keyboard shortcuts](/help/keyboard-shortcuts).

Height may be remembered during your session.

## Summary
Expand All @@ -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)
22 changes: 14 additions & 8 deletions packages/designer/src/features/calculation/GenericCodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -153,6 +158,7 @@ export const GenericCodeEditor = (props: GenericCodeEditorProps) => {
autoformat={props.autoformat}
onMount={props.onEditorMount}
onChange={setSourceCode}
onSave={handleSaveValue}
/>
<div className={styles.toolbar}>
<ButtonGroup size={'sm'}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand All @@ -12,7 +13,12 @@ import {useMonacoAutoformat} from './useMonacoAutoformat'
*/
export const UnconstrainedCodeEditor = (props: UnconstrainedCodeEditorProps) => {
const editorRef = useRef<editor.IStandaloneCodeEditor>(null)
const {onChange, onMount} = props
const {onChange, onMount, onSave} = props
const onSaveRef = useRef(onSave)

useEffect(() => {
onSaveRef.current = onSave
}, [onSave])

useEffect(() => {
const editor = editorRef.current
Expand All @@ -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)
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
},
})
}
56 changes: 54 additions & 2 deletions tests/component/tests/generic-code-editor-save.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <textarea data-testid="mock-code-editor" defaultValue={initialValue} />
return (
<>
<textarea data-testid="mock-code-editor" defaultValue={initialValue} />
<button type="button" data-testid="mock-code-editor-save" onClick={() => onSave?.('from-editor')} />
</>
)
},
}))

Expand Down Expand Up @@ -149,6 +162,45 @@ describe('GenericCodeEditor save behavior', () => {
expect(onSave).toHaveBeenCalledWith('Persisted')
})

it('persists the editor-supplied value when CodeEditor onSave is invoked', () => {
const onSave = vi.fn()
render(
<GenericCodeEditor
source="Hello"
onSave={onSave}
onClose={vi.fn()}
onCompile={() => CalculableResult.success('from-editor')}
compact={false}
/>
)

fireEvent.click(screen.getByTestId('mock-code-editor-save'))

expect(testSpy).toHaveBeenCalledOnce()
expect(onSave).toHaveBeenCalledExactlyOnceWith('from-editor')
})

it('applies getSourceForSave to the editor-supplied value on CodeEditor onSave', () => {
const onSave = vi.fn()
const getSourceForSave = vi.fn((fallback: string) => `wrapped:${fallback}`)
render(
<GenericCodeEditor
source="Hello"
getSourceForSave={getSourceForSave}
validateBeforeSave={resolveValidateBeforeSave}
onSave={onSave}
onClose={vi.fn()}
onCompile={() => CalculableResult.success('wrapped:from-editor')}
compact
/>
)

fireEvent.click(screen.getByTestId('mock-code-editor-save'))

expect(getSourceForSave).toHaveBeenCalledWith('from-editor')
expect(onSave).toHaveBeenCalledExactlyOnceWith('wrapped:from-editor')
})

it('renders toolbarExtra content', () => {
render(
<GenericCodeEditor
Expand Down
3 changes: 2 additions & 1 deletion tests/module/tests/helpers/monacoFakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function createFakeMonaco(): Monaco {
return {
Range: FakeRange as unknown as Monaco['Range'],
KeyMod: {CtrlCmd: 1 << 11},
KeyCode: {KeyA: 31},
KeyCode: {KeyA: 31, KeyS: 49},
} as unknown as Monaco
}

Expand Down Expand Up @@ -149,6 +149,7 @@ export function createFakeEditor(model: any) {
const editor = {
getId: () => 'fake-editor-1',
getModel: () => model,
getValue: () => model.getValue() as string,
getUnscopedCommands: () => addCommandCalls.filter(call => !call.context),
addCommand: (keybinding: number, handler: () => void, context?: string) => {
addCommandCalls.push({keybinding, handler, context})
Expand Down
Loading
Loading