refactor: use a class to get/set a matchsyntax string

This commit is contained in:
rivea0 2025-07-02 17:57:29 +03:00
parent 6991d6433f
commit b4940bd875
2 changed files with 26 additions and 1 deletions

View file

@ -2,6 +2,7 @@ import { App, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
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 { MatchSyntaxPluginSettings, IRange } from './types';
@ -9,6 +10,8 @@ const DEFAULT_SETTINGS: MatchSyntaxPluginSettings = {
showNumberOfMatchesNotification: true,
};
const SYNTAX = new MatchSyntaxString();
export default class MatchSyntaxPlugin extends Plugin {
settings: MatchSyntaxPluginSettings;
@ -31,7 +34,8 @@ export default class MatchSyntaxPlugin extends Plugin {
const plugin = cm6Editor.plugin(highlightViewPlugin);
const docEl = cm6Editor.state.doc;
new MatchTextModal(this.app, (textToMatch) => {
const ranges = getMatchRanges(docEl, textToMatch);
SYNTAX.setValue(textToMatch);
const ranges = getMatchRanges(docEl, SYNTAX.getValue());
if (plugin) {
plugin.makeDeco(ranges);
}
@ -54,6 +58,7 @@ export default class MatchSyntaxPlugin extends Plugin {
if (plugin.decorations === Decoration.none) {
new Notice('No decorations found!');
} else {
SYNTAX.clearValue();
plugin.clearDeco();
}
}
@ -62,6 +67,7 @@ export default class MatchSyntaxPlugin extends Plugin {
}
onunload() {
SYNTAX.clearValue();
if (import.meta.env.MODE === 'development') {
console.log('unloading plugin');
}

19
src/syntaxObj.ts Normal file
View file

@ -0,0 +1,19 @@
export default class MatchSyntaxString {
str: string;
constructor(str = '') {
this.str = str;
}
getValue() {
return this.str;
}
setValue(newValue: string) {
this.str = newValue;
}
clearValue() {
this.str = '';
}
}