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 <cursoragent@cursor.com>
This commit is contained in:
204
admin/package-lock.json
generated
204
admin/package-lock.json
generated
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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<string, string>
|
||||
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 },
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,215 @@
|
||||
<script setup lang="ts">
|
||||
// Stub for TDD RED (35-03 Task 4): loading, lazy CodeMirror, fallback and
|
||||
// read-only rendering land in GREEN. Emits nothing and shows an empty box so
|
||||
// the planned behavior assertions fail intentionally.
|
||||
import type { FieldControlProps } from '../control'
|
||||
import { computed, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue'
|
||||
import { Info } from '@lucide/vue'
|
||||
import { t } from '../../../app/i18n'
|
||||
import { controlClass, type FieldControlProps } from '../control'
|
||||
import type { CodeEditorHandle, CodeEditorLanguage } from '../codeeditor/editor'
|
||||
|
||||
defineProps<FieldControlProps>()
|
||||
defineEmits<{ 'update:modelValue': [value: string] }>()
|
||||
// type: codeeditor (D-09, UI-SPEC C1): HTML/CSS/plain-text source on CodeMirror 6,
|
||||
// loaded as its own chunk (skeleton meanwhile; when the chunk fails, a monospace
|
||||
// textarea with codeeditor.load_failed). Value is a string sent unchanged until
|
||||
// the administrator edits. Read-only shows a pre and loads no CodeMirror.
|
||||
const props = defineProps<FieldControlProps>()
|
||||
const emit = defineEmits<{ 'update:modelValue': [value: string] }>()
|
||||
|
||||
type EditorModule = typeof import('../codeeditor/editor')
|
||||
type Status = 'loading' | 'ready' | 'failed'
|
||||
|
||||
const SIZE_HEIGHT: Record<string, number> = {
|
||||
tiny: 128,
|
||||
small: 192,
|
||||
large: 320,
|
||||
huge: 480,
|
||||
giant: 640,
|
||||
}
|
||||
|
||||
const readOnly = computed(() => !!props.field.readOnly)
|
||||
const stored = computed(() => (props.modelValue === null || props.modelValue === undefined ? '' : String(props.modelValue)))
|
||||
const language = computed<CodeEditorLanguage>(() => {
|
||||
const value = props.field.language
|
||||
return value === 'html' || value === 'css' || value === 'text' ? value : 'html'
|
||||
})
|
||||
const height = computed(() => SIZE_HEIGHT[props.field.size ?? ''] ?? 320)
|
||||
const mailImages = computed(() => !!props.field.mailImages)
|
||||
const labelId = computed(() => `${props.controlId}-label`)
|
||||
|
||||
const status = ref<Status>('loading')
|
||||
const fallbackText = ref('')
|
||||
const mountEl = ref<HTMLElement | null>(null)
|
||||
const editor = shallowRef<CodeEditorHandle | null>(null)
|
||||
|
||||
let module: EditorModule | null = null
|
||||
let disposed = false
|
||||
let lastEmitted: string | null = null
|
||||
let suppressChange = false
|
||||
|
||||
const wrapperClass = computed(() => [
|
||||
'rounded-control border bg-surface overflow-hidden',
|
||||
'focus-within:border-primary focus-within:shadow-[0_0_0_3px_var(--c-ring)]',
|
||||
props.invalid ? 'border-danger' : 'border-border-strong',
|
||||
])
|
||||
|
||||
function contentAttributes(): Record<string, string> {
|
||||
const out: Record<string, string> = {
|
||||
id: props.controlId,
|
||||
'aria-labelledby': labelId.value,
|
||||
'aria-multiline': 'true',
|
||||
}
|
||||
if (props.describedBy) {
|
||||
out['aria-describedby'] = props.describedBy
|
||||
}
|
||||
if (props.invalid) {
|
||||
out['aria-invalid'] = 'true'
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
function setValue(value: string): void {
|
||||
lastEmitted = value
|
||||
emit('update:modelValue', value)
|
||||
}
|
||||
|
||||
function onEditorChange(doc: string): void {
|
||||
if (suppressChange) {
|
||||
return
|
||||
}
|
||||
setValue(doc)
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
if (readOnly.value) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
module = await import('../codeeditor/editor')
|
||||
if (disposed) {
|
||||
return
|
||||
}
|
||||
status.value = 'ready'
|
||||
await nextTick()
|
||||
if (disposed || !mountEl.value || !module) {
|
||||
return
|
||||
}
|
||||
editor.value = module.createCodeEditor({
|
||||
parent: mountEl.value,
|
||||
doc: stored.value,
|
||||
language: language.value,
|
||||
height: height.value,
|
||||
attributes: contentAttributes(),
|
||||
onChange: onEditorChange,
|
||||
})
|
||||
} catch {
|
||||
if (disposed) {
|
||||
return
|
||||
}
|
||||
editor.value?.destroy()
|
||||
editor.value = null
|
||||
status.value = 'failed'
|
||||
fallbackText.value = stored.value
|
||||
}
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
disposed = true
|
||||
editor.value?.destroy()
|
||||
editor.value = null
|
||||
})
|
||||
|
||||
watch(stored, (value) => {
|
||||
if (value === lastEmitted) {
|
||||
return
|
||||
}
|
||||
lastEmitted = null
|
||||
if (status.value === 'failed') {
|
||||
fallbackText.value = value
|
||||
return
|
||||
}
|
||||
if (editor.value) {
|
||||
suppressChange = true
|
||||
editor.value.setDoc(value)
|
||||
suppressChange = false
|
||||
}
|
||||
})
|
||||
|
||||
function onFallbackInput(event: Event): void {
|
||||
const value = (event.target as HTMLTextAreaElement).value
|
||||
fallbackText.value = value
|
||||
setValue(value)
|
||||
}
|
||||
|
||||
/** Inserts text at the selection (or appends); used by the mail image toolbar. */
|
||||
function insert(text: string): void {
|
||||
if (editor.value) {
|
||||
editor.value.insert(text)
|
||||
return
|
||||
}
|
||||
if (status.value === 'failed') {
|
||||
const el = document.getElementById(props.controlId) as HTMLTextAreaElement | null
|
||||
if (!el) {
|
||||
fallbackText.value = fallbackText.value + text
|
||||
setValue(fallbackText.value)
|
||||
return
|
||||
}
|
||||
const start = el.selectionStart ?? fallbackText.value.length
|
||||
const end = el.selectionEnd ?? start
|
||||
const next = fallbackText.value.slice(0, start) + text + fallbackText.value.slice(end)
|
||||
fallbackText.value = next
|
||||
setValue(next)
|
||||
void nextTick(() => {
|
||||
el.focus()
|
||||
const caret = start + text.length
|
||||
el.setSelectionRange(caret, caret)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
defineExpose({ insert })
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div data-codeeditor-stub class="rounded-control border border-border bg-surface" />
|
||||
<pre
|
||||
v-if="readOnly"
|
||||
:id="controlId"
|
||||
data-codeeditor-readonly
|
||||
class="max-h-[480px] overflow-auto rounded-control border border-border bg-subtle px-3.5 py-2.5 font-mono text-[13px] whitespace-pre-wrap"
|
||||
>{{ stored !== '' ? stored : t('backend::lang.list.empty_value') }}</pre>
|
||||
<div
|
||||
v-else-if="status === 'loading'"
|
||||
data-codeeditor-loading
|
||||
aria-busy="true"
|
||||
class="rounded-control bg-skel"
|
||||
:style="{ height: `${height}px` }"
|
||||
/>
|
||||
<div v-else-if="status === 'failed'" class="flex flex-col gap-1.5">
|
||||
<p data-codeeditor-load-failed class="flex items-center gap-2 text-[13px] text-muted">
|
||||
<Info :size="14" aria-hidden="true" />
|
||||
{{ t('backend::lang.codeeditor.load_failed') }}
|
||||
</p>
|
||||
<textarea
|
||||
:id="controlId"
|
||||
data-codeeditor-fallback
|
||||
:value="fallbackText"
|
||||
:aria-labelledby="labelId"
|
||||
:aria-describedby="describedBy"
|
||||
:aria-invalid="invalid ? 'true' : undefined"
|
||||
spellcheck="false"
|
||||
:class="[controlClass(invalid), 'font-mono text-[13px] resize-y px-3.5 py-2.5']"
|
||||
:style="{ height: `${height}px` }"
|
||||
@input="onFallbackInput"
|
||||
/>
|
||||
</div>
|
||||
<div v-else :data-codeeditor="field.name" :class="wrapperClass">
|
||||
<div
|
||||
v-if="mailImages"
|
||||
role="toolbar"
|
||||
:aria-label="t('backend::lang.codeeditor.toolbar')"
|
||||
:aria-controls="controlId"
|
||||
class="flex flex-wrap items-center gap-1 border-b border-border bg-subtle p-1"
|
||||
data-codeeditor-toolbar
|
||||
>
|
||||
<!-- Insert image button wired in Task 5 -->
|
||||
<slot name="toolbar" :insert="insert" />
|
||||
</div>
|
||||
<div ref="mountEl" data-codeeditor-mount />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -57,6 +57,7 @@ import TextField from './fields/TextField.vue'
|
||||
import TextareaField from './fields/TextareaField.vue'
|
||||
import MarkdownField from './fields/MarkdownField.vue'
|
||||
import MailPreviewField from './fields/MailPreviewField.vue'
|
||||
import CodeEditorField from './fields/CodeEditorField.vue'
|
||||
import MoneyField from './fields/MoneyField.vue'
|
||||
import TaglistField from './fields/TaglistField.vue'
|
||||
import MLTextField from './fields/MLTextField.vue'
|
||||
@@ -103,6 +104,7 @@ const renderers = new Map<string, Component>([
|
||||
['money', MoneyField],
|
||||
['taglist', TaglistField],
|
||||
['mailpreview', MailPreviewField],
|
||||
['codeeditor', CodeEditorField],
|
||||
])
|
||||
|
||||
/** Types whose control shows the label itself (toggle cards, relation manager). */
|
||||
|
||||
@@ -34,11 +34,14 @@ function field(overrides: Partial<FormField> = {}): FormField {
|
||||
}
|
||||
|
||||
async function editorReady(root: HTMLElement): Promise<HTMLElement> {
|
||||
return vi.waitFor(() => {
|
||||
const content = root.querySelector<HTMLElement>('.cm-content')
|
||||
expect(content).not.toBeNull()
|
||||
return content!
|
||||
})
|
||||
return vi.waitFor(
|
||||
() => {
|
||||
const content = root.querySelector<HTMLElement>('.cm-content')
|
||||
expect(content, root.innerHTML.slice(0, 500)).not.toBeNull()
|
||||
return content!
|
||||
},
|
||||
{ timeout: 15000 },
|
||||
)
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -85,7 +88,7 @@ describe('codeeditor keyboard in a real browser engine', () => {
|
||||
attachTo: host,
|
||||
})
|
||||
await flushPromises()
|
||||
const content = await editorReady(wrapper.element as HTMLElement)
|
||||
const content = await editorReady(form)
|
||||
content.focus()
|
||||
expect(document.activeElement).toBe(content)
|
||||
|
||||
@@ -120,16 +123,20 @@ describe('codeeditor keyboard in a real browser engine', () => {
|
||||
attachTo: document.body,
|
||||
})
|
||||
await flushPromises()
|
||||
const content = await editorReady(wrapper.element as HTMLElement)
|
||||
const content = await editorReady(document.body)
|
||||
content.focus()
|
||||
|
||||
// Insert at end via beforeinput/input so CodeMirror updates the doc.
|
||||
document.execCommand('selectAll')
|
||||
// Replace the document through the contenteditable surface CodeMirror owns.
|
||||
const selection = window.getSelection()
|
||||
const range = document.createRange()
|
||||
range.selectNodeContents(content)
|
||||
selection?.removeAllRanges()
|
||||
selection?.addRange(range)
|
||||
document.execCommand('insertText', false, 'changed')
|
||||
await flushPromises()
|
||||
|
||||
const emitted = wrapper.emitted('update:modelValue')
|
||||
expect(emitted).toBeTruthy()
|
||||
expect(emitted!.at(-1)).toEqual(['changed'])
|
||||
await vi.waitFor(() => {
|
||||
const emitted = wrapper.emitted('update:modelValue')
|
||||
expect(emitted?.at(-1)).toEqual(['changed'])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user