keine Ahnung tbh

This commit is contained in:
TheOnlySilas 2024-11-07 09:00:13 +01:00
parent 3708d648de
commit 21a5b02325
2 changed files with 20 additions and 78 deletions

View File

@ -18,58 +18,41 @@ import {
} from '@codemirror/state'; } from '@codemirror/state';
import { syntaxTree } from '@codemirror/language'; 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 { class LawRefPluginEditorProcessor implements PluginValue {
decorations: DecorationSet; decorations: DecorationSet;
constructor(view: EditorView) { constructor(view: EditorView) {
this.decorations = this.buildDecorations(view); this.decorations = LawRefMatcher.createDeco(view);
console.log("halloooo")
} }
update(update: ViewUpdate) { update(update: ViewUpdate) {
if (update.docChanged || update.viewportChanged) { if (update.docChanged || update.viewportChanged) {
this.decorations = this.buildDecorations(update.view); this.decorations = LawRefMatcher.updateDeco(update, this.decorations);
console.log("elllooooo")
} }
} }
destroy() { destroy() {
// ...
} }
buildDecorations(view: EditorView): DecorationSet { }
const builder = new RangeSetBuilder<Decoration>();
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); 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<DecorationSet>({
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<unknown>[] = 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
}

View File

@ -2,7 +2,7 @@ import { App, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault, Workspace
import { ExampleView, VIEW_TYPE_EXAMPLE } from './law-sidebar'; import { ExampleView, VIEW_TYPE_EXAMPLE } from './law-sidebar';
import { OldpApi } from './api/opld'; import { OldpApi } from './api/opld';
import LawSuggester from './lawSuggester'; import LawSuggester from './lawSuggester';
import { lawRefPluginEditorProcessor, underlineSelection } from './law-editor-processor'; import { lawRefPluginEditorProcessor, } from './law-editor-processor';
// Remember to rename these classes and interfaces! // 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) => { const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
// Called when the user clicks the icon. // 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(); this.activateView();
}); });