better-word-count/src/editor/EditorPlugin.ts

44 lines
925 B
TypeScript
Raw Normal View History

2022-11-10 15:23:23 +00:00
import {
ViewUpdate,
PluginValue,
EditorView,
ViewPlugin,
} from "@codemirror/view";
import type BetterWordCount from "src/main";
class EditorPlugin implements PluginValue {
hasPlugin: boolean;
view: EditorView;
private plugin: BetterWordCount;
constructor(view: EditorView) {
this.view = view;
this.hasPlugin = false;
}
update(update: ViewUpdate): void {
if (!this.hasPlugin) {
return;
}
const tr = update.transactions[0];
if (tr.isUserEvent("select")) {
let text = "";
const selection = tr.newSelection.main;
const textIter = tr.newDoc.iterRange(selection.from, selection.to);
while (!textIter.done) {
text = text + textIter.next().value;
}
}
}
addPlugin(plugin: BetterWordCount) {
this.plugin = plugin;
this.hasPlugin = true;
}
destroy() {}
}
export const editorPlugin = ViewPlugin.fromClass(EditorPlugin);