From ae26d897b18edd446d360286a33de35662500fe6 Mon Sep 17 00:00:00 2001 From: lorenzovaccarini Date: Thu, 3 Aug 2023 21:02:42 +0200 Subject: [PATCH] Implement date highlighting --- .nosync | 0 src/controllers/nplController.ts | 21 ++++++++++------- src/misc/misc.ts | 8 +++++++ src/plugin/main.ts | 4 +++- src/plugin/nplExtension.ts | 39 +++++++++++++++++++------------- 5 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 .nosync create mode 100644 src/misc/misc.ts diff --git a/.nosync b/.nosync new file mode 100644 index 0000000..e69de29 diff --git a/src/controllers/nplController.ts b/src/controllers/nplController.ts index d277d53..d6f8778 100644 --- a/src/controllers/nplController.ts +++ b/src/controllers/nplController.ts @@ -1,22 +1,23 @@ import wink from "wink-nlp"; import model from "wink-eng-lite-web-model"; import {readFileSync} from "fs"; -import {iCloudServiceStatus} from "../iCloudJs"; -import EventEmitter from "events"; -export default class NPLController{ +class NPLController{ private _customPatterns: {name, patterns}[]; private _pluginPath: string; private _nlp; private _ready: boolean; - constructor(pluginPath: string) { + constructor() { this._ready = false; + this._nlp = wink( model ); + this._ready = true; + } + + init(pluginPath: string){ this._pluginPath = pluginPath; this.loadPatterns(); - this._nlp = wink( model ); this._nlp.learnCustomEntities(this._customPatterns); - this._ready = true; } loadPatterns(){ @@ -32,14 +33,15 @@ export default class NPLController{ this._customPatterns.push({name: "duration", patterns: ["DURATION"]}); } - async process(text: string){ + process(text: string): string | null { if(!this._ready){ console.log("NPL not ready"); return; } - text = text.replaceAll("\n", "."); const its = this._nlp.its; const doc = this._nlp.readDoc(text); + const match = doc.customEntities().out(its.value); + return match.length == 0 ? null : match[0]; //doc.sentences().each(sentence => sentence.customEntities().each(entity => console.log(entity.out(its.detail)))); //doc.customEntities().each(e => e.markup(``, ``)); //console.log(doc.out(its.markedUpText)) @@ -61,3 +63,6 @@ export default class NPLController{ //console.log(doc.out(its.markedUpText)); } } + +const nplController = new NPLController(); +export default nplController; diff --git a/src/misc/misc.ts b/src/misc/misc.ts new file mode 100644 index 0000000..77d9c1f --- /dev/null +++ b/src/misc/misc.ts @@ -0,0 +1,8 @@ +export class Misc { + pluginPath: string; + setPath(pluginPath: string){ + this.pluginPath = pluginPath; + } +} +const misc = new Misc(); +export default misc; diff --git a/src/plugin/main.ts b/src/plugin/main.ts index ff8b7f5..1db0dc7 100644 --- a/src/plugin/main.ts +++ b/src/plugin/main.ts @@ -3,6 +3,7 @@ import iCloudService, {iCloudServiceStatus} from "../iCloudJs"; import PluginController from "../controllers/pluginController"; import SafeController from "../controllers/safeController"; import {iCloudStatusModal} from "./modal"; +import nplController from "../controllers/nplController"; import {NPLStateField} from "./nplExtension"; interface Settings { @@ -28,7 +29,8 @@ export default class iCalObsidianSync extends Plugin { const basePath = (this.app.vault.adapter as any).basePath const pluginPath =`${basePath}/.obsidian/plugins/obsidian-ical-sync`; - const nplStateField = new NPLStateField(pluginPath); + nplController.init(pluginPath); + const nplStateField = new NPLStateField(); this.registerEditorExtension(nplStateField.stateField); safeController.injectPath(pluginPath); diff --git a/src/plugin/nplExtension.ts b/src/plugin/nplExtension.ts index 42ae2ca..ae3501e 100644 --- a/src/plugin/nplExtension.ts +++ b/src/plugin/nplExtension.ts @@ -1,41 +1,48 @@ import { Extension, RangeSetBuilder, StateField, Transaction, } from "@codemirror/state"; import { Decoration, DecorationSet, EditorView } from "@codemirror/view"; -import NPLController from "../controllers/nplController"; +import nplController from "../controllers/nplController"; +import { syntaxTree } from "@codemirror/language"; +//class testWidget export class NPLStateField { stateField: StateField; - private _npl: NPLController; - constructor(pluginPath: string) { + constructor() { this.stateField = StateField.define({ create: this.create, update: this.update, provide: this.provide }); - this._npl = new NPLController(pluginPath); - this._npl.loadPatterns(); } create(state): DecorationSet { return Decoration.none; } - builderAdd(builder, listCharFrom, listCharTo){ - builder.add( - listCharFrom, - listCharTo, - Decoration.mark({ - tagName: "mark" - }) - ); - } update(oldState: DecorationSet, transaction: Transaction): DecorationSet { const builder = new RangeSetBuilder(); const sentences = transaction.state.doc.toJSON(); - //sentences.forEach() - console.log(sentences); + sentences.forEach((sentence, i) => { + const match = nplController.process(sentence); + if(match == null) return; + console.log("Match!"); + let previousChars = 0; + for (let j=0; j < i; j++){ + previousChars += sentences[j].length + 1; + } + const startsFrom = previousChars + sentence.indexOf(match) + const endsTo = startsFrom + match.length; + console.log(startsFrom, endsTo); + builder.add( + startsFrom, + endsTo, + Decoration.mark({ + tagName: "mark" + }) + ); + }); return builder.finish(); }