fix: update highlights when there is a change in editor

This commit is contained in:
rivea0 2025-07-02 18:05:01 +03:00
parent b4940bd875
commit 126f1c9d0a

View file

@ -3,7 +3,7 @@ import { EditorView, ViewPlugin, Decoration } from '@codemirror/view';
import { MatchTextModal } from './modal';
import { getMatchRanges } from './utils';
import MatchSyntaxString from './syntaxObj';
import type { PluginValue, DecorationSet, PluginSpec } from '@codemirror/view';
import type { PluginValue, DecorationSet, PluginSpec, ViewUpdate } from '@codemirror/view';
import type { MatchSyntaxPluginSettings, IRange } from './types';
const DEFAULT_SETTINGS: MatchSyntaxPluginSettings = {
@ -116,6 +116,17 @@ class HighlighterPlugin implements PluginValue {
constructor(view: EditorView) {
this.decorations = Decoration.none;
}
update(viewUpdate: ViewUpdate) {
if (viewUpdate.docChanged) {
if (this.decorations !== Decoration.none) {
const docEl = viewUpdate.view.state.doc;
const ranges = getMatchRanges(docEl, SYNTAX.getValue());
this.makeDeco(ranges);
}
}
}
makeDeco(ranges: IRange[]) {
const deco = [];
const highlightDeco = Decoration.mark({
@ -132,6 +143,10 @@ class HighlighterPlugin implements PluginValue {
clearDeco() {
this.decorations = Decoration.none;
}
destroy() {
this.clearDeco();
}
}
const pluginSpec: PluginSpec<HighlighterPlugin> = {