Tons of progress

This commit is contained in:
Zef Hemel
2022-02-24 17:24:49 +01:00
parent aa7929ea29
commit 5e5968f09e
38 changed files with 1039 additions and 144 deletions
+48
View File
@@ -0,0 +1,48 @@
{
"commands": {
"Count Words": {
"invoke": "word_count_command",
"requiredContext": {
"text": true
}
},
"Navigate To page": {
"invoke": "link_navigate",
"key": "Ctrl-Enter",
"mac": "Cmd-Enter",
"requiredContext": {
}
},
"Insert Current Date": {
"invoke": "insert_nice_date"
},
"Toggle : Heading 1": {
"invoke": "toggle_h1",
"mac": "Cmd-1",
"key": "Ctrl-1"
},
"Toggle : Heading 2": {
"invoke": "toggle_h2",
"mac": "Cmd-2",
"key": "Ctrl-2"
}
},
"events": {},
"functions": {
"word_count_command": {
"path": "./word_count_command.ts:wordCount"
},
"link_navigate": {
"path": "./link_navigate.ts:linkNavigate"
},
"insert_nice_date": {
"path": "./dates.ts:insertToday"
},
"toggle_h1": {
"path": "./markup.ts:toggleH1"
},
"toggle_h2": {
"path": "./markup.ts:toggleH2"
}
}
}
+6
View File
@@ -0,0 +1,6 @@
import { syscall } from "./lib/syscall.ts";
export async function insertToday() {
let niceDate = new Date().toISOString().split("T")[0];
await syscall("editor.insertAtCursor", niceDate);
}
+9
View File
@@ -0,0 +1,9 @@
import {syscall} from "./syscall.ts";
export async function put(key: string, value: any) {
return await syscall("db.put", key, value);
}
export async function get(key: string) {
return await syscall("db.get", key);
}
+5
View File
@@ -0,0 +1,5 @@
import {syscall} from "./syscall.ts";
export async function publish(event: string, data?: object) {
return await syscall("event.publish", event, data);
}
+16
View File
@@ -0,0 +1,16 @@
export function syscall(name: string, ...args: Array<any>): any {
let reqId = Math.floor(Math.random() * 1000000);
// console.log("Syscall", name, reqId);
return new Promise((resolve, reject) => {
self.dispatchEvent(
new CustomEvent("syscall", {
detail: {
id: reqId,
name: name,
args: args,
callback: resolve,
},
}),
);
});
}
+8
View File
@@ -0,0 +1,8 @@
import { syscall } from "./lib/syscall.ts";
export async function linkNavigate({ text }: { text: string }) {
let syntaxNode = await syscall("editor.getSyntaxNodeUnderCursor");
if (syntaxNode && syntaxNode.name === "WikiLinkPage") {
await syscall("editor.navigate", syntaxNode.text);
}
}
+33
View File
@@ -0,0 +1,33 @@
import { syscall } from "./lib/syscall.ts";
export async function toggleH1() {
await togglePrefix("# ");
}
export async function toggleH2() {
await togglePrefix("## ");
}
function lookBack(s: string, pos: number, backString: string): boolean {
return s.substring(pos - backString.length, pos) === backString;
}
async function togglePrefix(prefix: string) {
let text = (await syscall("editor.getText")) as string;
let pos = (await syscall("editor.getCursor")) as number;
if (text[pos] === "\n") {
pos--;
}
while (pos > 0 && text[pos] !== "\n") {
if (lookBack(text, pos, prefix)) {
// Already has this prefix, let's flip it
await syscall("editor.replaceRange", pos - prefix.length, pos, "");
return;
}
pos--;
}
if (pos) {
pos++;
}
await syscall("editor.insertAtPos", prefix, pos);
}
+20
View File
@@ -0,0 +1,20 @@
function countWords(str: string): number {
var matches = str.match(/[\w\d\'\'-]+/gi);
return matches ? matches.length : 0;
}
function readingTime(wordCount: number): number {
// 225 is average word reading speed for adults
return Math.ceil(wordCount / 225);
}
import { syscall } from "./lib/syscall.ts";
export async function wordCount({ text }: { text: string }) {
let sysCallText = (await syscall("editor.getText")) as string;
const count = countWords(sysCallText);
console.log("Word count", count);
let syntaxNode = await syscall("editor.getSyntaxNodeUnderCursor");
console.log("Syntax node", syntaxNode);
return count;
}