-
Notifications
You must be signed in to change notification settings - Fork 8
/
state.ts
31 lines (26 loc) · 832 Bytes
/
state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { type EditorState, StateEffect, StateField } from "@codemirror/state";
import type { EditorView } from "@codemirror/view";
import type { JSONSchema7 } from "json-schema";
const schemaEffect = StateEffect.define<JSONSchema7 | undefined>();
export const schemaStateField = StateField.define<JSONSchema7 | void>({
create() {},
update(schema, tr) {
for (const e of tr.effects) {
if (e.is(schemaEffect)) {
return e.value;
}
}
return schema;
},
});
export const updateSchema = (view: EditorView, schema?: JSONSchema7) => {
view.dispatch({
effects: schemaEffect.of(schema),
});
};
export const getJSONSchema = (state: EditorState) => {
return state.field(schemaStateField);
};
export const stateExtensions = (schema?: JSONSchema7) => [
schemaStateField.init(() => schema),
];