mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-06-04 04:48:55 +00:00
feat: cv2
This commit is contained in:
44
src/lib/chord-editor/chord-sync-plugin.ts
Normal file
44
src/lib/chord-editor/chord-sync-plugin.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { CharaChordFile } from "$lib/share/chara-file";
|
||||
import { StateEffect, StateField } from "@codemirror/state";
|
||||
import { actionMetaPlugin } from "./action-meta-plugin";
|
||||
import { syncCharaChords } from "./chord-sync";
|
||||
import type { EditorView } from "@codemirror/view";
|
||||
|
||||
const chordSyncEffect = StateEffect.define<CharaChordFile["chords"]>();
|
||||
|
||||
export function editorSyncChords(
|
||||
view: EditorView,
|
||||
newDeviceChords: CharaChordFile["chords"],
|
||||
) {
|
||||
const { ids, codes } = view.state.field(actionMetaPlugin.field);
|
||||
const oldDeviceChords = view.state.field(deviceChordField);
|
||||
const changes = syncCharaChords(
|
||||
oldDeviceChords,
|
||||
newDeviceChords,
|
||||
ids,
|
||||
codes,
|
||||
view.state.doc.toString(),
|
||||
);
|
||||
view.dispatch({
|
||||
effects: chordSyncEffect.of(newDeviceChords),
|
||||
changes,
|
||||
});
|
||||
}
|
||||
|
||||
export const deviceChordField = StateField.define<CharaChordFile["chords"]>({
|
||||
create() {
|
||||
return [];
|
||||
},
|
||||
update(value, transaction) {
|
||||
return (
|
||||
transaction.effects.findLast((it) => it.is(chordSyncEffect))?.value ??
|
||||
value
|
||||
);
|
||||
},
|
||||
toJSON(value) {
|
||||
return value;
|
||||
},
|
||||
fromJSON(value) {
|
||||
return value;
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user