Skip to content
Open
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 @@ -19,7 +19,14 @@ on the canvas and the **Main** / **Rules** tabs.

<img src="/assets/guide/workspace/highlight-json-view-close.png" alt="Edit JSON modal open with a red arrow pointing to the close button." />

The exact editor setup depends on configuration, including the Monaco editor and schema validation.
The exact editor setup depends on configuration, including the Monaco editor and schema validation. JSON in the editor is pretty-printed on
open and formats on paste and while typing. To reformat the whole document manually, use Monaco **Format Document**:

| OS | Shortcut |
| ------- | --------------------------------------------------------------------------------- |
| Windows | <span class="fe-key-combo"><kbd>Shift</kbd>+<kbd>Alt</kbd>+<kbd>F</kbd></span> |
| macOS | <span class="fe-key-combo"><kbd>Shift</kbd>+<kbd>Option</kbd>+<kbd>F</kbd></span> |
| Linux | <span class="fe-key-combo"><kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>I</kbd></span> |

## When authors use it

Expand Down
13 changes: 11 additions & 2 deletions documentation/docs/formengine-designer/features/forms-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,17 @@ screenshot below.

![img.png](/assets/forms-json/forms-json-03.png)

This opens the built-in JSON form code editor. After you close the form by clicking the close button in the top right corner of the screen,
the JSON with the form code will be applied in the form designer.
This opens the built-in JSON form code editor (Monaco). The buffer is pretty-printed on open, and the editor formats JSON on paste and while
typing. You can also format the whole document with the Monaco **Format Document** shortcut:

| OS | Shortcut |
| ------- | ----------------------------------------------- |
| Windows | <kbd>Shift</kbd>+<kbd>Alt</kbd>+<kbd>F</kbd> |
| macOS | <kbd>Shift</kbd>+<kbd>Option</kbd>+<kbd>F</kbd> |
| Linux | <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>I</kbd> |

After you close the form by clicking the close button in the top right corner of the screen, the JSON with the form code will be applied in
the form designer.

![img_1.png](/assets/forms-json/forms-json-04.png)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export interface GenericCodeEditorProps {
* Language mode.
*/
language?: CodeEditorLanguage
/**
* Formats the document on open and when the model value is replaced, and enables format on paste/type.
*/
autoformat?: boolean
/**
* The source code.
*/
Expand Down Expand Up @@ -146,6 +150,7 @@ export const GenericCodeEditor = (props: GenericCodeEditorProps) => {
canEditBeginContextLine={props.canEditBeginContextLine}
initialValue={initialValue}
language={props.language}
autoformat={props.autoformat}
onMount={props.onEditorMount}
onChange={setSourceCode}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,28 @@ const RawCodeEditor = (props: CodeEditorProps) => {
const theme = props.theme ?? themeFromBuilder

const codeEditorProps = {...props, theme}
let editor = <ConstrainedCodeEditor {...codeEditorProps} />

if (!props.beginContextLine && !props.endContextLine) {
const unconstrainedProps = {...codeEditorProps}
// remove unnecessary properties
delete unconstrainedProps.beginContextLine
delete unconstrainedProps.endContextLine
delete unconstrainedProps.canEditBeginContextLine
editor = <UnconstrainedCodeEditor {...unconstrainedProps} />
return (
<LtrDiv>
<UnconstrainedCodeEditor {...unconstrainedProps} />
</LtrDiv>
)
}

return <LtrDiv>{editor}</LtrDiv>
const constrainedProps = {...codeEditorProps}
delete constrainedProps.autoformat

return (
<LtrDiv>
<ConstrainedCodeEditor {...constrainedProps} />
</LtrDiv>
)
}

export const CodeEditor = namedObserver('CodeEditor', RawCodeEditor)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export interface UnconstrainedCodeEditorProps {
* @default 'javascript'
*/
language?: CodeEditorLanguage
/**
* Formats the document on open and when the model value is replaced, and enables format on paste/type.
*/
autoformat?: boolean
/**
* The code editor height.
*/
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 {useMonacoAutoformat} from './useMonacoAutoformat'

/**
* The React component that displays monaco code editor.
Expand Down Expand Up @@ -31,23 +32,26 @@ export const UnconstrainedCodeEditor = (props: UnconstrainedCodeEditorProps) =>
[onChange, onMount]
)

const options: editor.IStandaloneEditorConstructionOptions = useMemo(
() => ({
wordWrap: 'on',
...props.options,
}),
const options = useMemo(
() =>
({
wordWrap: 'on',
...props.options,
}) satisfies editor.IStandaloneEditorConstructionOptions,
[props.options]
)

const {monacoOptions, onMount: autoformatOnMount} = useMonacoAutoformat(props.autoformat, options, handleMount)

return (
<MonacoCodeEditor
onMount={handleMount}
onMount={autoformatOnMount}
className={props.className}
defaultValue={props.initialValue}
height={props.height ?? '100%'}
width={props.width ?? '100%'}
language={props.language ?? 'javascript'}
options={options}
options={monacoOptions}
theme={props.theme}
/>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type {OnMount} from '@monaco-editor/react'
import type {editor, IDisposable} from 'monaco-editor'
import {useCallback, useMemo, useRef} from 'react'

const autoFormatOptions: editor.IStandaloneEditorConstructionOptions = {
autoIndent: 'advanced',
formatOnPaste: true,
formatOnType: true,
}

/**
* Merges optional autoformat Monaco options and an onMount handler that formats on open / value flush.
* Does not own unrelated editor defaults (e.g. wordWrap); callers merge those into `options`.
* @param autoformat whether autoformat is enabled.
* @param options the caller Monaco construction options.
* @param onMount the optional mount callback from the caller.
* @returns the merged Monaco options and mount handler.
*/
export const useMonacoAutoformat = (
autoformat: boolean | undefined,
options: editor.IStandaloneEditorConstructionOptions | undefined,
onMount?: OnMount
) => {
const editorRef = useRef<editor.IStandaloneCodeEditor>(null)

const formatDocument = useCallback(() => {
void editorRef.current?.getAction('editor.action.formatDocument')?.run()
}, [])

const monacoOptions = useMemo(
() => ({
...options,
...(autoformat ? autoFormatOptions : {}),
}),
[autoformat, options]
)

const handleMount: OnMount = useCallback(
(monacoEditor, monaco) => {
if (!autoformat) {
onMount?.(monacoEditor, monaco)
return
}

editorRef.current = monacoEditor

const disposables: IDisposable[] = []
let formattedFromFlushDuringMount = false

disposables.push(
monacoEditor.onDidChangeModelContent(({isFlush}) => {
if (!isFlush) return
formattedFromFlushDuringMount = true
formatDocument()
})
)
// Editor-scoped: dispose listeners with the editor so remounts (e.g. Edit JSON modal) do not leak.
disposables.push(
monacoEditor.onDidDispose(() => {
for (const disposable of disposables) {
disposable.dispose()
}
disposables.length = 0
})
)

// Caller onMount may swap the model (JsonView) or setValue (flush). Format after that.
// Skip the microtask when a flush already formatted during onMount (one trigger).
// Avoid onDidLayoutChange: formatDocument can change layout and loop.
onMount?.(monacoEditor, monaco)
if (!formattedFromFlushDuringMount) {
queueMicrotask(formatDocument)
}
},
[autoformat, formatDocument, onMount]
)

return {monacoOptions, onMount: handleMount}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const RawJsonCodeEditor = (props: JsonCode) => {
onClose={handleClose}
onCompile={handleCompile}
language="json"
autoformat
compact
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ const RawJsonViewButton = ({className, ...props}: JsonViewButtonProps) => {
<Modal.Header closeButton onClose={handleClose}>
<Modal.Title>{t('editJson')}</Modal.Title>
</Modal.Header>
<Modal.Body className={styles.modalBody}>
<JsonView />
</Modal.Body>
<Modal.Body className={styles.modalBody}>{open ? <JsonView /> : null}</Modal.Body>
</Modal>
</>
)
Expand Down
2 changes: 1 addition & 1 deletion packages/designer/src/features/ui/JsonView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const RawJsonView = () => {
return () => builderStore.parseForm(sourceRef.current)
}, [builderStore])

return <CodeEditor language="json" initialValue={initialValue} onChange={onSetSource} onMount={handleMount} />
return <CodeEditor language="json" initialValue={initialValue} onChange={onSetSource} onMount={handleMount} autoformat />
}

export const JsonView = namedObserver('JsonView', RawJsonView)
Loading