From 9975cf4b52f1bd1918bc061320cb3183367ed6eb Mon Sep 17 00:00:00 2001 From: Jakub Zych Date: Fri, 13 Nov 2026 13:28:02 +0100 Subject: [PATCH] feat(admin): codeeditor field on CodeMirror 6, loaded lazily - Pin the seven approved CodeMirror/lezer packages exactly; lockfile integrity matches the decision note - Lazy createCodeEditor with Go {{ }} highlighting, form hotkeys kept, Tab unbound - Loading skeleton, chunk-failure textarea fallback, read-only pre Co-authored-by: Cursor --- admin/package-lock.json | 204 +++++++++++++++++ admin/package.json | 7 + .../src/components/form/codeeditor/editor.ts | 161 ++++++++++++- .../form/fields/CodeEditorField.vue | 216 +++++++++++++++++- admin/src/components/form/registry.ts | 2 + admin/tests/browser/codeeditor-smoke.test.ts | 33 +-- 6 files changed, 597 insertions(+), 26 deletions(-) diff --git a/admin/package-lock.json b/admin/package-lock.json index 294d90af..f28b788c 100644 --- a/admin/package-lock.json +++ b/admin/package-lock.json @@ -6,9 +6,16 @@ "": { "name": "summercms-admin", "dependencies": { + "@codemirror/commands": "6.11.1", + "@codemirror/lang-css": "6.3.1", + "@codemirror/lang-html": "6.4.12", + "@codemirror/language": "6.13.1", + "@codemirror/state": "6.7.6", + "@codemirror/view": "6.43.15", "@fontsource/dm-mono": "5.3.0", "@fontsource/dm-sans": "5.3.0", "@internationalized/date": "3.12.4", + "@lezer/highlight": "1.2.5", "@lucide/vue": "1.17.0", "@tiptap/core": "3.31.4", "@tiptap/pm": "3.31.4", @@ -171,6 +178,128 @@ "node": ">=6.9.0" } }, + "node_modules/@codemirror/autocomplete": { + "version": "6.20.4", + "resolved": "https://registry.npmjs.org/@codemirror/autocomplete/-/autocomplete-6.20.4.tgz", + "integrity": "sha512-yF8RhOf2SXlg00+qtupzLhrglcas61boqaONiNcbPIebHCpJocpqTBA6dyrtUrQO47hqyTdeob/3XnYilT7TaQ==", + "license": "MIT", + "dependencies": { + "@codemirror/language": "^6.0.0", + "@codemirror/state": "^6.0.0", + "@codemirror/view": "^6.17.0", + "@lezer/common": "^1.0.0" + } + }, + "node_modules/@codemirror/commands": { + "version": "6.11.1", + "resolved": "https://registry.npmjs.org/@codemirror/commands/-/commands-6.11.1.tgz", + "integrity": "sha512-O/4hG3SC1YwcmQ0d2UVNDs+AsaNWd1iHVxbTeEBuqH+6bExAiPK3iS/BvpY6rZGURALv4ZD3sIgcCmRvw3ehBg==", + "license": "MIT", + "dependencies": { + "@codemirror/language": "^6.0.0", + "@codemirror/state": "^6.7.0", + "@codemirror/view": "^6.27.0", + "@lezer/common": "^1.1.0" + } + }, + "node_modules/@codemirror/lang-css": { + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/@codemirror/lang-css/-/lang-css-6.3.1.tgz", + "integrity": "sha512-kr5fwBGiGtmz6l0LSJIbno9QrifNMUusivHbnA1H6Dmqy4HZFte3UAICix1VuKo0lMPKQr2rqB+0BkKi/S3Ejg==", + "license": "MIT", + "dependencies": { + "@codemirror/autocomplete": "^6.0.0", + "@codemirror/language": "^6.0.0", + "@codemirror/state": "^6.0.0", + "@lezer/common": "^1.0.2", + "@lezer/css": "^1.1.7" + } + }, + "node_modules/@codemirror/lang-html": { + "version": "6.4.12", + "resolved": "https://registry.npmjs.org/@codemirror/lang-html/-/lang-html-6.4.12.tgz", + "integrity": "sha512-pw2ReWKUqSkbvh76RAT4NYxiogRu+PWkR2ukAwO9uOgrm8uipkzjtKKtNpyeAQwHOqxEeSvAXZ6vr3AfyB9y/w==", + "license": "MIT", + "dependencies": { + "@codemirror/autocomplete": "^6.0.0", + "@codemirror/lang-css": "^6.0.0", + "@codemirror/lang-javascript": "^6.0.0", + "@codemirror/language": "^6.4.0", + "@codemirror/state": "^6.0.0", + "@codemirror/view": "^6.17.0", + "@lezer/common": "^1.0.0", + "@lezer/css": "^1.1.0", + "@lezer/html": "^1.3.12" + } + }, + "node_modules/@codemirror/lang-javascript": { + "version": "6.2.5", + "resolved": "https://registry.npmjs.org/@codemirror/lang-javascript/-/lang-javascript-6.2.5.tgz", + "integrity": "sha512-zD4e5mS+50htS7F+TYjBPsiIFGanfVqg4HyUz6WNFikgOPf2BgKlx+TQedI1w6n/IqRBVBbBWmGFdLB/7uxO4A==", + "license": "MIT", + "dependencies": { + "@codemirror/autocomplete": "^6.0.0", + "@codemirror/language": "^6.6.0", + "@codemirror/lint": "^6.0.0", + "@codemirror/state": "^6.0.0", + "@codemirror/view": "^6.17.0", + "@lezer/common": "^1.0.0", + "@lezer/javascript": "^1.0.0" + } + }, + "node_modules/@codemirror/language": { + "version": "6.13.1", + "resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.13.1.tgz", + "integrity": "sha512-zfvU2r3TyylQLMj/te+5CnQ0rSqZi8B1oTCBbQkagcLW4Xc6imab6qN+pEJhsuNOLaJqiRyBXp9UhWA1DhVvDw==", + "license": "MIT", + "dependencies": { + "@codemirror/state": "^6.0.0", + "@codemirror/streamparser": "^6.0.0", + "@codemirror/view": "^6.23.0", + "@lezer/common": "^1.5.0", + "@lezer/highlight": "^1.0.0", + "@lezer/lr": "^1.0.0", + "style-mod": "^4.0.0" + } + }, + "node_modules/@codemirror/lint": { + "version": "6.9.8", + "resolved": "https://registry.npmjs.org/@codemirror/lint/-/lint-6.9.8.tgz", + "integrity": "sha512-qNvtcE73xUto/D1RvtgSRI4A1Hg8ekxlvtZpjHpdpisLBnhyu82EUEJMEgaxkjuQCA8vqnrylENWKOsEDCOHcw==", + "license": "MIT", + "dependencies": { + "@codemirror/state": "^6.0.0", + "@codemirror/view": "^6.42.0", + "crelt": "^1.0.5" + } + }, + "node_modules/@codemirror/state": { + "version": "6.7.6", + "resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.7.6.tgz", + "integrity": "sha512-kAz+AncRtKuIknedxT1bq4XwXv4UowhbkHU1myPrtVb/jZtImWuV5BXzv5vK6i3kYACsdiZiQKFQQ5Mq7elW8w==", + "license": "MIT", + "dependencies": { + "@marijn/find-cluster-break": "^1.0.0" + } + }, + "node_modules/@codemirror/streamparser": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/@codemirror/streamparser/-/streamparser-6.0.0.tgz", + "integrity": "sha512-JTFPF6xrfjt0mnfGPgzcLiMwYmvIyNaoizp8BYzv8FE8KrT6GBGDZ190rtt+O7Wke3gG/MFHyiY9YGNeimof4w==", + "license": "MIT" + }, + "node_modules/@codemirror/view": { + "version": "6.43.15", + "resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.43.15.tgz", + "integrity": "sha512-HHsfpqFeHyXLs983pSRe38Xajs0vFWZQ2BuX0m7Ki8w3jZ5iD8cuR3lBz2/5/X53Dhf32SBHKh6rtSTB6tKeGw==", + "license": "MIT", + "dependencies": { + "@codemirror/state": "^6.7.0", + "crelt": "^1.0.6", + "style-mod": "^4.1.0", + "w3c-keyname": "^2.2.4" + } + }, "node_modules/@esbuild/aix-ppc64": { "version": "0.27.7", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.27.7.tgz", @@ -758,6 +887,63 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@lezer/common": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.5.5.tgz", + "integrity": "sha512-Pw7PVCndruF1pb3ebCxpSVpkMHSDjwRCLcTRw+RizUKco5QbWylwq2g4lnG/RYN7yTTQDXE9F77MwtkAyphrjg==", + "license": "MIT" + }, + "node_modules/@lezer/css": { + "version": "1.3.9", + "resolved": "https://registry.npmjs.org/@lezer/css/-/css-1.3.9.tgz", + "integrity": "sha512-ZmzgvTOBdJWGIsY/ItAfN7UW8DV802oZG+jeG6E508SwEzz5YhuAajptpWPICvIyqYAD2+VjMdOS40GgGkyEOQ==", + "license": "MIT", + "dependencies": { + "@lezer/common": "^1.2.0", + "@lezer/highlight": "^1.0.0", + "@lezer/lr": "^1.3.0" + } + }, + "node_modules/@lezer/highlight": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.2.5.tgz", + "integrity": "sha512-O1GMVKgtf5YspFaRzpmqkVgMtIk2HG9uMAbH6wdAIdNh3TVzCsch/+cRi+6l2//UGDKe6NB9BW3cvuR5ezMQ0w==", + "license": "MIT", + "dependencies": { + "@lezer/common": "^1.3.0" + } + }, + "node_modules/@lezer/html": { + "version": "1.3.13", + "resolved": "https://registry.npmjs.org/@lezer/html/-/html-1.3.13.tgz", + "integrity": "sha512-oI7n6NJml729m7pjm9lvLvmXbdoMoi2f+1pwSDJkl9d68zGr7a9Btz8NdHTGQZtW2DA25ybeuv/SyDb9D5tseg==", + "license": "MIT", + "dependencies": { + "@lezer/common": "^1.2.0", + "@lezer/highlight": "^1.0.0", + "@lezer/lr": "^1.0.0" + } + }, + "node_modules/@lezer/javascript": { + "version": "1.5.6", + "resolved": "https://registry.npmjs.org/@lezer/javascript/-/javascript-1.5.6.tgz", + "integrity": "sha512-sEvOqpl4vJQca0NugSQK02vQPbc5KrvoojnYGoqqFgFO0MtcYhwGiHKVEB+LuXYJnfyXo4QPA8ztbrEa4Fw5NA==", + "license": "MIT", + "dependencies": { + "@lezer/common": "^1.2.0", + "@lezer/highlight": "^1.1.3", + "@lezer/lr": "^1.3.0" + } + }, + "node_modules/@lezer/lr": { + "version": "1.4.12", + "resolved": "https://registry.npmjs.org/@lezer/lr/-/lr-1.4.12.tgz", + "integrity": "sha512-On9HU4oMMiHPMkQDgoTnGahjgG5n/MkN38CgG77sMraKLL+3JBbeQNi1FcCXV9QRqwF9/BUXP3caHyJC1fBOTQ==", + "license": "MIT", + "dependencies": { + "@lezer/common": "^1.0.0" + } + }, "node_modules/@lucide/vue": { "version": "1.17.0", "resolved": "https://registry.npmjs.org/@lucide/vue/-/vue-1.17.0.tgz", @@ -767,6 +953,12 @@ "vue": ">=3.0.1" } }, + "node_modules/@marijn/find-cluster-break": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@marijn/find-cluster-break/-/find-cluster-break-1.0.4.tgz", + "integrity": "sha512-Wy0V7+SGUjnF9/TkiM1hKVDPj7jKXduPNboMVtHTA8dySMURWqfg/JZ9E2Sq8JgSJmkl7k7Qe9FLeMSrSraWmQ==", + "license": "MIT" + }, "node_modules/@napi-rs/lzma-linux-x64-gnu": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/@napi-rs/lzma-linux-x64-gnu/-/lzma-linux-x64-gnu-1.5.1.tgz", @@ -2776,6 +2968,12 @@ "proto-list": "~1.2.1" } }, + "node_modules/crelt": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/crelt/-/crelt-1.0.7.tgz", + "integrity": "sha512-aK6BbWfhf4U/wCcLHKPJl/xa6VkVstRaPywWtMKGwuOLc/wZTyQYuoxgvZnNsBvv7Kg3YTBQYYBCggcviQczuA==", + "license": "MIT" + }, "node_modules/cross-spawn": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", @@ -4502,6 +4700,12 @@ "dev": true, "license": "MIT" }, + "node_modules/style-mod": { + "version": "4.1.4", + "resolved": "https://registry.npmjs.org/style-mod/-/style-mod-4.1.4.tgz", + "integrity": "sha512-XXWIQt633/EpAFx8aZDOTjBzrCaGmhvEQlQo6MVPfa2OzO2cWo+4hV9h+6UkHYlXGfy+ODXKUdP7Pthmcu5ATw==", + "license": "MIT" + }, "node_modules/supports-color": { "version": "10.2.2", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-10.2.2.tgz", diff --git a/admin/package.json b/admin/package.json index f6b0bec8..c2391cd9 100644 --- a/admin/package.json +++ b/admin/package.json @@ -14,9 +14,16 @@ "gen:api": "openapi-typescript openapi/admin.json -o src/api/schema.d.ts" }, "dependencies": { + "@codemirror/commands": "6.11.1", + "@codemirror/lang-css": "6.3.1", + "@codemirror/lang-html": "6.4.12", + "@codemirror/language": "6.13.1", + "@codemirror/state": "6.7.6", + "@codemirror/view": "6.43.15", "@fontsource/dm-mono": "5.3.0", "@fontsource/dm-sans": "5.3.0", "@internationalized/date": "3.12.4", + "@lezer/highlight": "1.2.5", "@lucide/vue": "1.17.0", "@tiptap/core": "3.31.4", "@tiptap/pm": "3.31.4", diff --git a/admin/src/components/form/codeeditor/editor.ts b/admin/src/components/form/codeeditor/editor.ts index 346a3aa3..7368e400 100644 --- a/admin/src/components/form/codeeditor/editor.ts +++ b/admin/src/components/form/codeeditor/editor.ts @@ -1,21 +1,170 @@ -// Stub for TDD RED (35-03 Task 4): real CodeMirror factory lands in GREEN. +// CodeMirror 6 setup for the codeeditor field (Phase 35, D-09). CodeEditorField +// loads this module with a dynamic import, so CodeMirror is its own chunk and a +// form without a codeeditor never downloads it. Decision note: +// .planning/notes/2026-11-06-admin-code-editor-codemirror.md +import { EditorState } from '@codemirror/state' +import { + EditorView, + keymap, + lineNumbers, + Decoration, + MatchDecorator, + ViewPlugin, + type ViewUpdate, +} from '@codemirror/view' +import { defaultKeymap, history, historyKeymap } from '@codemirror/commands' +import { bracketMatching, HighlightStyle, syntaxHighlighting } from '@codemirror/language' +import { html } from '@codemirror/lang-html' +import { css } from '@codemirror/lang-css' +import { tags } from '@lezer/highlight' + +export type CodeEditorLanguage = 'html' | 'css' | 'text' + export interface CodeEditorOptions { parent: HTMLElement doc: string - language: 'html' | 'css' | 'text' + language: CodeEditorLanguage height: number attributes: Record onChange: (doc: string) => void } export interface CodeEditorHandle { - view: unknown + view: EditorView insert(text: string): void destroy(): void setDoc(doc: string): void } -/** Builds the field's CodeMirror editor on options.parent. */ -export function createCodeEditor(_options: CodeEditorOptions): CodeEditorHandle { - throw new Error('codeeditor not implemented') +const actionMatcher = new MatchDecorator({ + regexp: /\{\{[\s\S]*?\}\}/g, + decoration: () => Decoration.mark({ class: 'cm-summer-action' }), +}) + +const goActions = ViewPlugin.fromClass( + class { + decorations + + constructor(view: EditorView) { + this.decorations = actionMatcher.createDeco(view) + } + + update(update: ViewUpdate) { + this.decorations = actionMatcher.updateDeco(update, this.decorations) + } + }, + { decorations: (v) => v.decorations }, +) + +const syntaxColors = HighlightStyle.define([ + { tag: [tags.tagName, tags.propertyName, tags.keyword], color: 'var(--c-primary)', fontStyle: 'normal', fontWeight: '400' }, + { tag: [tags.string, tags.attributeValue], color: 'var(--c-ok-text)', fontStyle: 'normal', fontWeight: '400' }, + { tag: [tags.attributeName, tags.name, tags.variableName], color: 'var(--c-text)', fontStyle: 'normal', fontWeight: '400' }, + { tag: [tags.comment, tags.punctuation, tags.bracket, tags.paren], color: 'var(--c-muted)', fontStyle: 'normal', fontWeight: '400' }, +]) + +function languageExtension(language: CodeEditorLanguage) { + if (language === 'html') { + return html() + } + if (language === 'css') { + return css() + } + return [] +} + +function theme(height: number) { + return EditorView.theme({ + '&': { + height: `${height}px`, + fontFamily: 'var(--font-mono)', + fontSize: '13px', + lineHeight: '1.5', + color: 'var(--c-text)', + backgroundColor: 'var(--c-surface)', + }, + '.cm-scroller': { overflow: 'auto', fontFamily: 'inherit' }, + '.cm-content': { + padding: '10px 14px', + caretColor: 'var(--c-text)', + fontFamily: 'inherit', + }, + '.cm-gutters': { + backgroundColor: 'var(--c-subtle)', + color: 'var(--c-muted)', + border: 'none', + }, + '.cm-activeLine': { backgroundColor: 'var(--c-hover)' }, + '.cm-activeLineGutter': { backgroundColor: 'var(--c-hover)' }, + '&.cm-focused .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection': { + backgroundColor: 'var(--c-sel)', + }, + '.cm-cursor, .cm-dropCursor': { borderLeftColor: 'var(--c-text)' }, + '.cm-summer-action': { + color: 'var(--c-primary)', + backgroundColor: 'var(--c-subtle)', + borderRadius: '4px', + }, + }) +} + +/** Builds the field's CodeMirror editor on options.parent. */ +export function createCodeEditor(options: CodeEditorOptions): CodeEditorHandle { + // Pitfall 5: drop Mod-Enter so the form's save hotkey still fires. + const keys = keymap.of([...historyKeymap, ...defaultKeymap.filter((binding) => binding.key !== 'Mod-Enter')]) + // No indentWithTab: Tab moves focus out (WCAG 2.1.2). + + let hadFocus = false + const view = new EditorView({ + parent: options.parent, + state: EditorState.create({ + doc: options.doc, + extensions: [ + lineNumbers(), + bracketMatching(), + history(), + languageExtension(options.language), + goActions, + syntaxHighlighting(syntaxColors), + theme(options.height), + keys, + ...(options.language === 'text' ? [EditorView.lineWrapping] : []), + EditorView.contentAttributes.of(options.attributes), + EditorView.updateListener.of((update) => { + if (update.view.hasFocus) { + hadFocus = true + } + if (update.docChanged) { + options.onChange(update.state.doc.toString()) + } + }), + ], + }), + }) + + return { + view, + insert(text: string) { + const { state } = view + const selection = hadFocus ? state.selection.main : null + const from = selection ? selection.from : state.doc.length + const to = selection ? selection.to : state.doc.length + view.dispatch({ + changes: { from, to, insert: text }, + selection: { anchor: from + text.length }, + }) + view.focus() + }, + destroy() { + view.destroy() + }, + setDoc(doc: string) { + if (view.state.doc.toString() === doc) { + return + } + view.dispatch({ + changes: { from: 0, to: view.state.doc.length, insert: doc }, + }) + }, + } } diff --git a/admin/src/components/form/fields/CodeEditorField.vue b/admin/src/components/form/fields/CodeEditorField.vue index cacfd076..47d7133b 100644 --- a/admin/src/components/form/fields/CodeEditorField.vue +++ b/admin/src/components/form/fields/CodeEditorField.vue @@ -1,13 +1,215 @@