From 0ff94606d41634e5327233efd87c40cd9870c910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Argentina=20Ortega=20S=C3=A1inz?= Date: Mon, 26 Oct 2020 21:22:01 +0100 Subject: [PATCH] Migration from volcano --- main.ts | 215 ++++++++++++++++++++++++++++++++------------------ manifest.json | 6 +- package.json | 7 +- tsconfig.json | 2 + 4 files changed, 149 insertions(+), 81 deletions(-) diff --git a/main.ts b/main.ts index d1c2a32..7cfb253 100644 --- a/main.ts +++ b/main.ts @@ -1,77 +1,140 @@ -import { App, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; - -export default class MyPlugin extends Plugin { - onInit() { - - } - - onload() { - console.log('loading plugin'); - - this.addRibbonIcon('dice', 'Sample Plugin', () => { - new Notice('This is a notice!'); - }); - - this.addStatusBarItem().setText('Status Bar Text'); - - this.addCommand({ - id: 'open-sample-modal', - name: 'Open Sample Modal', - // callback: () => { - // console.log('Simple Callback'); - // }, - checkCallback: (checking: boolean) => { - let leaf = this.app.workspace.activeLeaf; - if (leaf) { - if (!checking) { - new SampleModal(this.app).open(); - } - return true; - } - return false; - } - }); - - this.addSettingTab(new SampleSettingTab(this.app, this)); - } - - onunload() { - console.log('unloading plugin'); - } -} - -class SampleModal extends Modal { - constructor(app: App) { - super(app); - } - - onOpen() { - let {contentEl} = this; - contentEl.setText('Woah!'); - } - - onClose() { - let {contentEl} = this; - contentEl.empty(); - } -} - -class SampleSettingTab extends PluginSettingTab { - display(): void { - let {containerEl} = this; - - containerEl.empty(); - - containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'}); - - new Setting(containerEl) - .setName('Setting #1') - .setDesc('It\'s a secret') - .addText(text => text.setPlaceholder('Enter your secret') - .setValue('') - .onChange((value) => { - console.log('Secret: ' + value); - })); - - } +import { + App, + Modal, + Notice, + Plugin, + PluginSettingTab, + Setting, +} from "obsidian"; + +import chrono from "chrono-node"; + +var getLastDayOfMonth = function (y: any, m: any) { + return new Date(y, m, 0).getDate(); +}; + +var nextDate = new RegExp(/next\s(w+)/, "gi"); + +const custom = chrono.casual.clone(); + +custom.parsers.push({ + pattern: () => { + return /\bChristmas\b/i; + }, + extract: (context: any, match: RegExpMatchArray) => { + return { + day: 25, + month: 12, + }; + }, +}); + +export default class NaturalLanguageDates extends Plugin { + onInit() {} + + onload() { + console.log("Loading natural language date parser plugin"); + + this.addCommand({ + id: "nlp-dates", + name: "Parse natural language date", + callback: () => this.onTrigger(), + hotkeys: [{ modifiers: ["Mod"], key: "y" }], + }); + + } + + onunload() { + console.log("Unloading natural language date parser plugin"); + } + + getDateString(selectedText: string) { + var nextDateMatch = selectedText.match(/next\s([\w]+)/i); + var lastDayOfMatch = selectedText.match( + /(last day of|end of)\s*([^\n\r]*)/i + ); + var midOf = selectedText.match(/mid\s([\w]+)/i); + + if (nextDateMatch && nextDateMatch[1] === "week") { + return custom.parseDate("next monday", new Date(), { + forwardDate: true, + }); + } else if (nextDateMatch && nextDateMatch[1] === "month") { + var thisMonth = custom.parseDate("this month", new Date(), { + forwardDate: true, + }); + return custom.parseDate(selectedText, thisMonth, { + forwardDate: true, + }); + } else if (nextDateMatch && nextDateMatch[1] === "year") { + var thisYear = custom.parseDate("this year", new Date(), { + forwardDate: true, + }); + return custom.parseDate(selectedText, thisYear, { + forwardDate: true, + }); + } else if (lastDayOfMatch) { + var tempDate = custom.parse(lastDayOfMatch[2]); + var year = tempDate[0].start.get("year"), + month = tempDate[0].start.get("month"); + var lastDay = getLastDayOfMonth(year, month); + return custom.parseDate(`${year}-${month}-${lastDay}`, new Date(), { + forwardDate: true, + }); + } else if (midOf) { + return custom.parseDate(`${midOf[1]} 15th`, new Date(), { + forwardDate: true, + }); + } else { + return custom.parseDate(selectedText, new Date(), { + forwardDate: true, + }); + } + } + + getSelectedText(editor: any) { + if (editor.somethingSelected()) { + return editor.getSelection(); + } else { + var wordBoundaries = this.getWordBoundaries(editor); + editor.getDoc().setSelection(wordBoundaries.start, wordBoundaries.end); + return editor.getSelection(); + } + } + + getWordBoundaries(editor: any) { + var cursor = editor.getCursor(); + var line = cursor.line; + var word = editor.findWordAt({ line: line, ch: cursor.ch }); + var wordStart = word.anchor.ch; + var wordEnd = word.head.ch; + + return { + start: { line: line, ch: wordStart }, + end: { line: line, ch: wordEnd }, + }; + } + + onTrigger() { + let activeLeaf: any = this.app.workspace.activeLeaf; + let editor = activeLeaf.view.sourceMode.cmEditor; + var cursor = editor.getCursor(); + var selectedText = this.getSelectedText(editor); + + var date = this.getDateString(selectedText); + + if (date) { + var isodate = date.toISOString().substring(0, 10); + var newStr = `[[${isodate}]]`; + editor.replaceSelection(newStr); + this.adjustCursor(editor, cursor, newStr, selectedText); + } else { + editor.setCursor({ line: cursor.line, ch: cursor.ch }); + } + } + + adjustCursor(editor: any, cursor: any, newStr: string, oldStr: string) { + var cursorOffset = newStr.length - oldStr.length; + editor.setCursor({ line: cursor.line, ch: cursor.ch + cursorOffset }); + } } diff --git a/manifest.json b/manifest.json index ea4bf3d..d8323e7 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { - "id": "obsidian-sample-plugin", - "name": "Sample Plugin", - "description": "This is a sample plugin for Obsidian (https://obsidian.md)", + "id": "nldates-obsidian", + "name": "Natural Language Dates Plugin", + "description": "Create date-links based on natural language", "isDesktopOnly": false, "js": "main.js", "css": "styles.css" diff --git a/package.json b/package.json index c0573df..43e9caa 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "obsidian-sample-plugin", + "name": "nldates-obsidian", "version": "0.9.7", - "description": "This is a sample plugin for Obsidian (https://obsidian.md)", + "description": "Plugin to parse natural language dates in Obsidian", "main": "main.js", "scripts": { "dev": "rollup --config rollup.config.js -w", @@ -19,5 +19,8 @@ "rollup": "^2.32.1", "tslib": "^2.0.3", "typescript": "^4.0.3" + }, + "dependencies": { + "chrono-node": "^2.1.9" } } diff --git a/tsconfig.json b/tsconfig.json index d148fe0..55b3b34 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,8 @@ "inlineSourceMap": true, "inlineSources": true, "module": "ESNext", + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, "target": "es5", "allowJs": true, "noImplicitAny": true,