mirror of
https://github.com/sehugg/8bitworkshop.git
synced 2026-03-13 10:41:59 +00:00
src/ide/views/filters.ts
This commit is contained in:
23
src/ide/views/filters.ts
Normal file
23
src/ide/views/filters.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { EditorState, Transaction, Compartment } from "@codemirror/state";
|
||||
|
||||
function createTextTransformFilter(textMapFunctions: { input: ((text: string) => string) | null }) {
|
||||
console.log("createTextTransformFilter", textMapFunctions);
|
||||
return EditorState.transactionFilter.of((tr: Transaction) => {
|
||||
if (!tr.docChanged || !tr.isUserEvent("input") || !textMapFunctions.input) return tr;
|
||||
|
||||
let changes: { from: number; to: number; insert: string }[] = [];
|
||||
tr.changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
|
||||
changes.push({ from: fromA, to: toA, insert: textMapFunctions.input(inserted.toString()) });
|
||||
});
|
||||
|
||||
return { changes, selection: tr.selection };
|
||||
});
|
||||
}
|
||||
|
||||
const textTransformFilterCompartment = new Compartment();
|
||||
|
||||
function createTextTransformFilterEffect(textMapFunctions: { input: ((text: string) => string) | null }) {
|
||||
return textTransformFilterCompartment.reconfigure(createTextTransformFilter(textMapFunctions));
|
||||
}
|
||||
|
||||
export { textTransformFilterCompartment, createTextTransformFilterEffect };
|
||||
Reference in New Issue
Block a user