Skip to content

Commit

Permalink
Migration from volcano
Browse files Browse the repository at this point in the history
  • Loading branch information
argenos committed Oct 26, 2020
1 parent a577988 commit 0ff9460
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 81 deletions.
215 changes: 139 additions & 76 deletions main.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -19,5 +19,8 @@
"rollup": "^2.32.1",
"tslib": "^2.0.3",
"typescript": "^4.0.3"
},
"dependencies": {
"chrono-node": "^2.1.9"
}
}
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"inlineSourceMap": true,
"inlineSources": true,
"module": "ESNext",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es5",
"allowJs": true,
"noImplicitAny": true,
Expand Down

0 comments on commit 0ff9460

Please sign in to comment.