@dodona/papyros
Version:
Scratchpad for multiple programming languages in the browser.
48 lines • 1.59 kB
JavaScript
import { StateEffect, StateField } from "@codemirror/state";
import { Decoration, EditorView } from "@codemirror/view";
export class LineEffectExtension {
constructor(view) {
this.addEffect = StateEffect.define();
this.clearEffect = StateEffect.define();
this.setEffect = StateEffect.define();
this.editorView = view;
}
add(range) {
this.editorView.dispatch({
effects: this.addEffect.of(range)
});
}
clear() {
this.editorView.dispatch({
effects: this.clearEffect.of(null)
});
}
set(range) {
this.editorView.dispatch({
effects: this.setEffect.of(range)
});
}
toExtension() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
return StateField.define({
create() {
return Decoration.none;
},
update(value, transaction) {
let v = value.map(transaction.changes);
for (const effect of transaction.effects) {
if (effect.is(self.clearEffect) || effect.is(self.setEffect)) {
v = Decoration.none;
}
if (effect.is(self.addEffect) || effect.is(self.setEffect)) {
v = v.update({ add: effect.value });
}
}
return v;
},
provide: f => EditorView.decorations.from(f)
});
}
}
//# sourceMappingURL=LineEffectExtension.js.map