mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-01-22 01:42:47 +00:00
feat: cv2
This commit is contained in:
35
src/lib/chord-editor/store-state-field.ts
Normal file
35
src/lib/chord-editor/store-state-field.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { StateEffect, StateField } from "@codemirror/state";
|
||||
import { EditorView, ViewPlugin } from "@codemirror/view";
|
||||
import { get, type Readable } from "svelte/store";
|
||||
|
||||
export function reactiveStateField<T>(store: Readable<T>) {
|
||||
const effect = StateEffect.define<T>();
|
||||
const field = StateField.define<T>({
|
||||
create() {
|
||||
return get(store);
|
||||
},
|
||||
update(value, transaction) {
|
||||
return (
|
||||
transaction.effects.findLast((it) => it.is(effect))?.value ?? value
|
||||
);
|
||||
},
|
||||
});
|
||||
const plugin = ViewPlugin.fromClass(
|
||||
class {
|
||||
unsubscribe: () => void;
|
||||
|
||||
constructor(readonly view: EditorView) {
|
||||
this.unsubscribe = store.subscribe((value) => {
|
||||
setTimeout(() => {
|
||||
view.dispatch({ effects: effect.of(value) });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.unsubscribe();
|
||||
}
|
||||
},
|
||||
);
|
||||
return { field, plugin: [field, plugin] };
|
||||
}
|
||||
Reference in New Issue
Block a user