Code editor: CodeMirror 6 replaces Monaco

Node source (Python) and the JSON dialog share one CodeMirror editor with
find and replace, autocomplete, folding, indent guides, comment toggle and a
JSON lint, themed from the CSS tokens so light and dark follow the .dark class
without a second palette. Programmatic value changes no longer fire onChange,
blur ignores focus moving into the find bar, readOnly keeps selection and copy.

The editor chunk is 470 kB (157 kB gzipped) against 2.7 MB (704 kB) for Monaco,
which also pulled in a 1.2 MB JSON mode, two workers and a font. At 4x CPU on
slow 4G a node panel is ready in 5.7 s instead of 10.8 s, the JSON dialog in
1.5 s instead of 6.7 s. Typing without a key delay keeps every character.

monaco-editor, @monaco-editor/react and the dompurify override are removed.
Specs: typed code reaches the node source, a JSON setting saves only text that
parses. Docs name Tab and Ctrl-M.

Claude-Session: https://claude.ai/code/session_01SSi5AvZ7pFmySK8zy3NPMh
This commit is contained in:
2026-10-28 17:32:49 +01:00
parent 688658fa09
commit b8a15e4133
12 changed files with 531 additions and 143 deletions
+84
View File
@@ -221,3 +221,87 @@ test("a port's code name is a field only when it differs from its message", asyn
await api(page, `/flows/${name}`, { method: "DELETE" })
}
})
/**
* The code editor is a contenteditable the canvas must leave alone, and what is
* typed into it reaches the node's source through the panel's own debounce.
* Typed with no key delay at all, which is the pace that used to drop letters.
*/
test("code typed into a node's editor is saved", async ({ page }) => {
const name = `test_code_${Date.now().toString(36)}`
const text = "# level, other = 1, 2"
await page.goto("/")
const saved = await api(page, `/flows/${name}`, {
method: "PUT",
data: { name, nodes: [{ id: "writer", type: "python" }], inputs: [] },
})
expect(saved.ok()).toBeTruthy()
try {
await page.goto(`/flows/${name}?node=writer`)
const editor = page.getByTestId("code-editor").locator(".cm-content")
await editor.click()
await page.keyboard.press("ControlOrMeta+End")
await page.keyboard.type(`\n${text}`)
await expect(editor).toContainText(text)
await expect
.poll(
async () =>
(await (await api(page, `/flows/${name}/nodes/writer/source`)).json())
.code,
)
.toContain(text)
} finally {
await api(page, `/flows/${name}`, { method: "DELETE" })
}
})
/**
* A JSON setting opens in a dialog that edits a draft: the node only changes
* once the text parses and Save is pressed.
*/
test("a JSON setting saves only text that parses", async ({ page }) => {
const name = `test_json_${Date.now().toString(36)}`
await page.goto("/")
const saved = await api(page, `/flows/${name}`, {
method: "PUT",
data: {
name,
nodes: [
{
id: "gate",
type: "switch",
params: { rules: [], otherwise: "" },
requires: [{ name: "level", dtype: "float" }],
provides: [{ name: "high", dtype: "float" }],
},
],
inputs: [],
},
})
expect(saved.ok()).toBeTruthy()
try {
await page.goto(`/flows/${name}?node=gate`)
await page.getByTestId("json-field").first().click()
const dialog = page.getByTestId("json-dialog")
const editor = dialog.locator(".cm-content")
const save = dialog.getByTestId("json-save")
await expect(editor).toBeFocused()
await expect(save).toBeEnabled()
// Brackets close themselves, so this leaves `[1,]`: a trailing comma.
await page.keyboard.press("ControlOrMeta+a")
await page.keyboard.press("Backspace")
await page.keyboard.type("[1,")
await expect(save).toBeDisabled()
await expect(dialog.getByRole("alert")).toBeVisible()
await expect(dialog.locator(".cm-lint-marker-error")).toBeVisible()
await page.keyboard.press("Backspace")
await expect(save).toBeEnabled()
await save.click()
await expect(dialog).toHaveCount(0)
await expect(page.getByTestId("json-field").first()).toContainText("[1]")
} finally {
await api(page, `/flows/${name}`, { method: "DELETE" })
}
})