Skip to content

Code editor

The designers’ XML, SQL, C# and template editors are one component, and the package exports it. A host app can use the same editor on its own pages and give it its own data for completion, hover and validation. It has the same toolbar, formatter, theming and dialog as the designers. Monaco is lazy-loaded the first time an editor mounts. Where Monaco can’t run (no DOM, e.g. tests), the editor falls back to a plain textarea with the same props.

import {
CodeEditor,
CodeEditorDialog,
CodeEditorDialogButton,
type CodeIntellisense,
type CodeEditorProblem,
type XmlSchema,
} from "@genie/genie-engine-ui";

The styles ship in the package stylesheet (@genie/genie-engine-ui/styles.css). They are scoped under .genie-shell and follow the theme tokens, so the editor picks up the host’s light/dark theme and accent.

<CodeEditor mode="xml" value={xml} onChange={setXml} />
Prop Purpose
mode "xml", "sql", "csharp", "json", "handlebars" or "text".
value / onChange Controlled text. External changes are applied as an undoable edit.
readOnly, placeholder, className As usual. className sizes the host.
intellisense Completion / hover / validation sources (see below).
diagnostics Problems your app found, shown as squiggles (see below).
toolbar Shows the full toolbar above the editor, for a page whose main surface is the editor.
expandable The Open in dialog pop-out button (default true).
title Heading of the pop-out dialog.

By default the editor’s height follows its content, up to the host’s CSS max-height. To fill a fixed box instead, set --code-editor-autosize: 0 and a definite height on the class you pass:

.my-editor { --code-editor-autosize: 0; height: 60vh; }

CodeEditorDialog is the dialog the workflow, wizard and report designers open from their XML button. It has a slim title bar, the editor toolbar, an edge-to-edge editor, an optional pane under the editor, and a status bar showing the mode, the cursor position, your hint and your actions.

{open && (
<CodeEditorDialog
title="Policy XML"
mode="xml"
value={text}
onChange={setText}
intellisense={intellisense}
diagnostics={problems}
onClose={() => setOpen(false)} // ✕, Esc and a click outside all call it
hint="Load replaces the designer"
actions={
<>
<CodeEditorDialogButton icon="fas fa-download" label="Export" onClick={exportXml} />
<CodeEditorDialogButton icon="fas fa-upload" label="Load" tone="primary" onClick={load} />
</>
}
>
{/* optional pane under the editor, e.g. a validation list or a diff */}
</CodeEditorDialog>
)}

CodeEditorDialogButton takes tone ("default", "primary" or "danger"), plus disabled, busy (shows a spinner) and title.

intellisense is a plain object. Fill only the parts your mode needs:

interface CodeIntellisense {
xml?: XmlSchema; // XML: elements, attributes, values, hover
sql?: { schema(): Promise<SqlSchemaTable[]>; validate?(sql: string): Promise<SqlValidationProblem[]> };
script?: { complete(...); diagnose(...); hover(...) }; // C#: a Roslyn-style service
variables?: string[]; sqlParameters?: string[]; templateRoots?: string[];
secrets?: string[]; values?: Record<string, unknown>;
}

Keep the object stable, for example with useMemo. The editor always reads the latest one, so updating it (say, after your schema loads) takes effect without remounting.

An XmlSchema is keyed by element name. An attribute can be a bare name, or an object that tells the editor more:

const schema: XmlSchema = {
Policy: { root: true, hint: "A correlation policy", children: ["Rule"] },
Rule: {
children: ["Condition"],
attributes: [
{ name: "Effect", required: true, values: ["Allow", "Deny"], hint: "What the rule does" },
{ name: "Priority", hint: "integer, 1–100", default: "50" },
"Description",
],
},
};

With it, the editor:

  • offers the root elements when nothing is open, and the open element’s children after <;
  • inserts an element together with its required attributes, as tab stops with value choices;
  • lists attributes not yet written, required ones first, with their docs;
  • inside Attr=", offers the attribute’s values;
  • on hover over an element or attribute, shows its hint, whether it is required, its default and its allowed values.

Element and attribute names already used in the document are offered too, so a partial schema still helps. Build the schema however suits your app. A common approach is to fetch your XSD from your API and convert it once.

Pass problems from your own checks (for example a server-side validate endpoint) and they appear as squiggles, with the message on hover and in Next problem (Alt+F8):

const problems: CodeEditorProblem[] = issues.map((i) => ({
line: i.line, // 1-based; no column marks the whole line
message: i.message,
severity: i.severity === "Error" ? "error" : "warning",
}));

They are kept separate from the editor’s own checks: its instant XML/SQL syntax check and the SQL/C# server diagnostics from intellisense. Updating one set never clears the others.