mirror of
https://github.com/rivea0/obsidian-match-syntax.git
synced 2026-07-22 05:46:49 +00:00
refactor: use a class to get/set a matchsyntax string
This commit is contained in:
parent
6991d6433f
commit
b4940bd875
2 changed files with 26 additions and 1 deletions
|
|
@ -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
19
src/syntaxObj.ts
Normal 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 = '';
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue