Repository navigation
v1.9.0
v1.9.0 — Script Editor: Form Integration, Named Slots, Constrained Templates
This release introduces a new package @cocoar/vue-script-editor — a Vue 3 wrapper around Monaco Editor — and promotes it from "embeddable code editor" to a first-class Cocoar form input. You can drop it inside CoarFormField, bind it to a DTO property via v-model, and get labels, errors, disabled state, and ARIA wiring for free — the same way CoarTextInput and CoarSelect behave.
The release also ships the constrained-mode / locked-line system that was the original motivation: templates where signatures stay fixed and users fill in bodies, with per-region identification via named slots so your backend knows which function body is empty vs. filled.
Highlights
New package: @cocoar/vue-script-editor — Monaco-based code editor for TypeScript, JavaScript, and JSON. Peer-dependency on monaco-editor so consumer apps control worker bundling and language selection. v-model, syntax highlighting, auto-complete, minimap, read-only mode, Cocoar light/dark themes that react to your theme switcher.
Form integration (CoarFormField) — the editor auto-inherits id, error, describedBy, and disabled from a surrounding CoarFormField via a shared injection key (Symbol.for('coar:form-field')). Label, error message, required marker, and ARIA attributes wire up without extra plumbing:
<CoarFormField label="Handler script" :error="scriptError" required>
<CoarScriptEditor v-model="form.script" variant="inline" height="180px" />
</CoarFormField>New props for form-citizen behaviour: disabled, error, placeholder, required, autofocus, id, name, height (CSS string or pixel number). New events: focused, blurred (for touched-state tracking). New exposed method: focus().
variant: 'editor' | 'inline' — 'editor' (default) keeps the full IDE chrome. 'inline' switches to a compact form-field look with tight padding, word-wrap, and a hover/focus ring matching CoarTextInput — ideal for forms where the script is one field among many.
lineNumbers: boolean — explicit toggle that overrides the variant default ('editor' → on, 'inline' → off). When line numbers are off, a small decoration column stays visible so the text is not flush with the left border.
Constrained mode via // @locked markers — any line of the source containing // @locked is protected from edits, deletion, and line-merging. Everything else is freely editable, including the file top, so TypeScript's Auto-Import quickfix works naturally. The markers are ordinary line comments, so v-model is the persistence format — save and reload the exact same string, no separate schema for "template" vs. "body".
function describeOrder(order: Order): string { // @locked
return `Order ${order.id}`;
} // @lockedNamed slots (@slot:NAME) — place @slot:fn1 on a // @locked line to name the editable segment that follows.
function describeOrder(order: Order): string { // @locked @slot:fn1
return `Order ${order.id}`;
} // @lockedThen:
import { getSlots, getSlot } from '@cocoar/vue-script-editor';
const slots = getSlots(savedSource);
// { fn1: ' return x + 1;', fn2: '', fn3: ' return x * 2;' }
getSlot(savedSource, 'fn2'); // '' — declared but empty
getSlot(savedSource, 'fn4'); // undefined — not declaredThe marker sits on a locked line, so the user cannot delete it. Auto-Import inserts at file top move the marker down with the rest of the text; its slot mapping is re-derived on each call. The matching regex is exported as SLOT_MARKER_PATTERN so server-side parsers (e.g. a C# Jint host) can mirror the same extraction without shipping JS.
preamble: string — hidden + auto-locked prefix providing per-editor type context. Typical use: your runtime executes the user's script as a function body with injected bindings (query: TodoQuery), and you want IntelliSense to match that shape exactly:
<CoarScriptEditor
v-model="form.script"
preamble="declare const query: TodoQuery;"
:extra-libs="[{ filePath: 'file:///types/todo-query.d.ts', content: queryTypes }]"
/>The preamble renders invisibly above the user script (Monaco setHiddenAreas), cannot be edited or cursored into (internal preamble guard), and is stripped from the emitted modelValue — it never round-trips through persistence.
scriptMode: boolean — suppresses the diagnostic codes Monaco emits for "script body" code: top-level return, top-level await, export in non-module, implicit any on injected globals, unreachable code. Global side-effect on typescriptDefaults/javascriptDefaults, so affects every TS/JS editor on the page.
Authoring mode (authoring prop) — template authors need to edit the protected parts too. Flip authoring to true and lock enforcement suspends: markers render full-size in a warm accent colour, the author can modify the signature or add/remove // @locked lines, then flip back and enforcement resumes with whatever marker state is now in the text.
TypeScript type injection via extraLibs — inject .d.ts contents into Monaco's TypeScript service so user code gets IntelliSense, hover types, and diagnostics for your domain types. First-class use case for coding-challenge / template-based editing products.
Cocoar-native dark mode — theme="auto" detects .dark-mode class on <html>/<body> (Cocoar convention), data-theme attribute, or OS prefers-color-scheme — reactively via MutationObserver + matchMedia. Toggle your app-level theme switcher and the Monaco theme flips in the same frame.
Bundled Cascadia Code font — @cocoar/vue-ui/fonts now also loads Cascadia Code (weights 400, 600, 700). Both CoarCodeBlock and CoarScriptEditor prefer it over the previous Consolas, Monaco, Courier New stack (still the fallback). Monaco has fontLigatures: true so !=, =>, === render as combined glyphs. If your app imports @cocoar/vue-ui/fonts you pick up the upgrade automatically.