From b4940bd875220c5d7a951683a98cbb77801ce365 Mon Sep 17 00:00:00 2001 From: rivea0 <58330360+rivea0@users.noreply.github.com> Date: Wed, 2 Jul 2025 17:57:29 +0300 Subject: [PATCH] refactor: use a class to get/set a matchsyntax string --- src/main.ts | 8 +++++++- src/syntaxObj.ts | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/syntaxObj.ts diff --git a/src/main.ts b/src/main.ts index 437f87a..74245b8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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'); } diff --git a/src/syntaxObj.ts b/src/syntaxObj.ts new file mode 100644 index 0000000..9dc8191 --- /dev/null +++ b/src/syntaxObj.ts @@ -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 = ''; + } +}