This library provides a CodeMirror-based editor for editing Skir value in readable JSON format.
Demo: npx skir-studio-demo
- A ready-to-use
createEditorState(...)factory for CodeMirror. - Schema-driven JSON template generation (when no JSON value is provided).
- JSON validation and lint diagnostics.
- In-editor hints and completions informed by the Skir type schema.
- Read-only mode support for response viewing.
- Built-in theme support:
- all presets from
@uiw/codemirror-themes-all - custom theme object
- all presets from
- Optional
otherExtensionhook to append your own CodeMirror extension(s).
Theme previews and names: https://www.npmjs.com/package/@uiw/codemirror-themes-all?activeTab=readme
npm install skir-codemirror-pluginThe package root exports:
createEditorStateensureJsonStatetoJsonCreateEditorStateParams(type)BuiltinThemeName(type)CustomTheme(type)JsonState(type)- all types from
./json/types
import { EditorView } from "@codemirror/view";
import {
createEditorState,
type CreateEditorStateParams,
} from "skir-codemirror-plugin";
const params: CreateEditorStateParams = {
schema: {
type: { kind: "primitive", value: "string" },
records: [],
},
// Optional:
// readOnly: true,
// json: "hello",
// theme: "github-light",
// otherExtension: EditorView.lineWrapping,
};
const state = createEditorState(params);
new EditorView({
state,
parent: document.getElementById("editor")!,
});Use ensureJsonState(view, schema) to force parse/validation against the current
document and retrieve the latest state. Then call toJson(...) on
parseResult.value when it exists.
import { EditorView } from "@codemirror/view";
import {
createEditorState,
ensureJsonState,
toJson,
type TypeDefinition,
} from "skir-codemirror-plugin";
const schema: TypeDefinition = {
type: { kind: "primitive", value: "string" },
records: [],
};
const view = new EditorView({
state: createEditorState({ schema }),
parent: document.getElementById("editor")!,
});
const jsonState = ensureJsonState(view, schema);
if (jsonState.parseResult.value) {
const jsonValue = toJson(jsonState.parseResult.value);
console.log("Current JSON value:", jsonValue);
} else {
console.log("Cannot convert to JSON:", jsonState.parseResult.errors);
}{
schema: TypeDefinition,
readOnly?: true,
json?: Json,
theme?: BuiltinThemeName | CustomTheme,
otherExtension?: Extension,
}Behavior:
schemais required and drives validation/completion.- If
jsonis omitted, a JSON template is generated from the schema. readOnly: trueenables non-editable mode.themedefaults totokyo-night.themeaccepts any built-in preset name fromBuiltinThemeName(powered by@uiw/codemirror-themes-all) or a custom theme object. See available theme previews in the package README: https://www.npmjs.com/package/@uiw/codemirror-themes-all?activeTab=readmeotherExtensionappends extra CodeMirror extension(s) at the end of the editor extension list.
This repository includes a minimal local dev page for previewing the editor state produced by createEditorState.
npm run devThis does three things:
- Builds TypeScript sources into
dist/. - Builds the dev entry point from
dev/main.tsintodev-dist/main.js. - Starts
web-dev-serverand opens/dev/index.html.
Dev URL:
http://localhost:8080/dev/index.html
dev/index.html: minimal host page containing only#editorand module import.dev/main.ts: createsEditorViewwithcreateEditorState(...).dev-dist/main.js: generated browser output fromdev/main.ts.
To try your own schema/JSON inputs, edit dev/main.ts and change the params object.
npm run dev watches both:
src/**/*.ts-> rebuilds the library intodist/dev/**/*.ts-> rebuilds the dev entry intodev-dist/
web-dev-server --watch reloads the page when served files change.
This flow is for local development only.
- Dev sources are in
dev/. - Dev compiled output is in
dev-dist/. - Package distribution uses
dist/as the runtime entrypoint.