skir-codemirror-plugin
v1.0.2
Published
This library provides a CodeMirror-based JSON editor for Skir values. It helps users edit Skir request/response payloads in a human-readable JSON form while preserving schema-aware guidance and validation.
Readme
skir-codemirror-plugin
This library provides a CodeMirror-based JSON editor for Skir values. It helps users edit Skir request/response payloads in a human-readable JSON form while preserving schema-aware guidance and validation.
Demo: npx skir-studio-demo
What It Provides
- 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:
tokyo-nighttokyo-night-day- custom theme object
Install
npm install skir-codemirror-pluginPublic API
The package root exports:
createEditorStateensureJsonStatetoJsonCreateEditorStateParams(type)CustomTheme(type)JsonState(type)- all types from
./json/types
Usage
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: "tokyo-night-day",
};
const state = createEditorState(params);
new EditorView({
state,
parent: document.getElementById("editor")!,
});Read Current JSON Value
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);
}createEditorState Parameters
{
schema: TypeDefinition,
readOnly?: true,
json?: Json,
theme?: "tokyo-night" | "tokyo-night-day" | CustomTheme,
}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.
Local Dev Flow
This repository includes a minimal local dev page for previewing the editor state produced by createEditorState.
Run It
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.htmlDev Files
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.
Auto Rebuild and Reload
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.
Dev-Only Note
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.
