diff --git a/src/law-editor-processor.ts b/src/law-editor-processor.ts index afe9f65..eb3d771 100644 --- a/src/law-editor-processor.ts +++ b/src/law-editor-processor.ts @@ -18,58 +18,41 @@ import { } from '@codemirror/state'; import { syntaxTree } from '@codemirror/language'; -import { MarkdownEditView } from 'obsidian'; -// ยง\s\d+[a-z]?\s(I+\s)?(\d\s)?(Nr.\s\d\s)?\w+ -const placeholderMatcher = new MatchDecorator({ - regexp: /\[\[(\w+)\]\]/g, - decoration: match => Decoration.replace({ - }) +const LawRefMatcher = new MatchDecorator({ + regexp: /\(\((\w+)\)\)/g, + decoration: match => Decoration.mark({ + class: "lr-underline" }) +}) + +const underlineTheme = EditorView.baseTheme({ + ".lr-underline": { textDecoration: "underline 3px red"} +}) + + + + class LawRefPluginEditorProcessor implements PluginValue { decorations: DecorationSet; constructor(view: EditorView) { - this.decorations = this.buildDecorations(view); + this.decorations = LawRefMatcher.createDeco(view); + console.log("halloooo") } update(update: ViewUpdate) { if (update.docChanged || update.viewportChanged) { - this.decorations = this.buildDecorations(update.view); + this.decorations = LawRefMatcher.updateDeco(update, this.decorations); + console.log("elllooooo") } } destroy() { - // ... + } - buildDecorations(view: EditorView): DecorationSet { - const builder = new RangeSetBuilder(); - - for (let { from, to } of view.visibleRanges) { - syntaxTree(view.state).iterate({ - from, - to, - enter(node: any) { - if (node.type.name.startsWith('list')) { - // Position of the '-' or the '*'. - const listCharFrom = node.from - 2; - - builder.add( - listCharFrom, - listCharFrom + 1, - Decoration.replace({ - - }) - ); - } - }, - }); - } - - return builder.finish(); - } -} +} export const lawRefPluginEditorProcessor = ViewPlugin.fromClass(LawRefPluginEditorProcessor); @@ -82,41 +65,3 @@ export const lawRefPluginEditorProcessor = ViewPlugin.fromClass(LawRefPluginEdit -const addUnderline = StateEffect.define<{from: number, to: number}>({ - map: ({from, to}, change) => ({from: change.mapPos(from), to: change.mapPos(to)}) - }) - - const underlineField = StateField.define({ - create() { - return Decoration.none - }, - update(underlines, tr) { - underlines = underlines.map(tr.changes) - for (let e of tr.effects) if (e.is(addUnderline)) { - underlines = underlines.update({ - add: [underlineMark.range(e.value.from, e.value.to)] - }) - } - return underlines - }, - provide: f => EditorView.decorations.from(f) - }) - - const underlineMark = Decoration.mark({class: "cm-underline"}) - const underlineTheme = EditorView.baseTheme({ - ".cm-underline": { textDecoration: "underline 3px red" } - }) - - export function underlineSelection(view: EditorView) { - let effects: StateEffect[] = view.state.selection.ranges - .filter(r => !r.empty) - .map(({from, to}) => addUnderline.of({from, to})) - if (!effects.length) return false - - if (!view.state.field(underlineField, false)) - effects.push(StateEffect.appendConfig.of([underlineField, - underlineTheme])) - view.dispatch({effects}) - return true - } - \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 18b2908..f268469 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,7 @@ import { App, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault, Workspace import { ExampleView, VIEW_TYPE_EXAMPLE } from './law-sidebar'; import { OldpApi } from './api/opld'; import LawSuggester from './lawSuggester'; -import { lawRefPluginEditorProcessor, underlineSelection } from './law-editor-processor'; +import { lawRefPluginEditorProcessor, } from './law-editor-processor'; // Remember to rename these classes and interfaces! @@ -33,9 +33,6 @@ export default class LawRefPlugin extends Plugin { const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => { // Called when the user clicks the icon. - // @ts-expect-error, not typed - const CoMieditorView = this.app.workspace.activeEditor.editor.cm as EditorView; - underlineSelection(CoMieditorView); this.activateView(); });