From bfd77de2d5f3fce6716e4720ba086b618b683b3b Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sat, 26 Feb 2022 17:50:50 +0100 Subject: [PATCH] Refactoring of builtins --- plugins/core/click.ts | 13 ----------- plugins/core/core.plugin.json | 23 ++++++++++---------- plugins/core/link_navigate.ts | 8 ------- plugins/core/navigate.ts | 21 ++++++++++++++++++ plugins/core/task.ts | 31 +++++++++++++++++++++++++++ webapp/src/syscalls/editor.browser.ts | 19 ++++++++++++++-- 6 files changed, 80 insertions(+), 35 deletions(-) delete mode 100644 plugins/core/click.ts delete mode 100644 plugins/core/link_navigate.ts create mode 100644 plugins/core/navigate.ts create mode 100644 plugins/core/task.ts diff --git a/plugins/core/click.ts b/plugins/core/click.ts deleted file mode 100644 index 4a30afa..0000000 --- a/plugins/core/click.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { ClickEvent } from "../../webapp/src/app_event.ts"; -import { syscall } from "./lib/syscall.ts"; - -export default async function click(event: ClickEvent) { - console.log("Event", event); - if (event.ctrlKey || event.metaKey) { - let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos); - console.log("Here", syntaxNode); - if (syntaxNode && syntaxNode.name === "WikiLinkPage") { - await syscall("editor.navigate", syntaxNode.text); - } - } -} diff --git a/plugins/core/core.plugin.json b/plugins/core/core.plugin.json index 9f8e31d..a7c8f36 100644 --- a/plugins/core/core.plugin.json +++ b/plugins/core/core.plugin.json @@ -9,8 +9,8 @@ "mac": "Cmd-Enter" }, "Insert Current Date": { - "invoke": "insert_nice_date", - "slashCommand": "/insert-today" + "invoke": "insertToday", + "slashCommand": "/today" }, "Toggle : Heading 1": { "invoke": "toggle_h1", @@ -24,23 +24,22 @@ } }, "events": { - "app:ready": ["welcome"], - "page:click": ["click"] + "page:click": ["taskToggle", "clickNavigate"] }, "functions": { - "welcome": { - "path": "./welcome.ts" + "linkNavigate": { + "path": "./navigate.ts:linkNavigate" }, - "click": { - "path": "./click.ts" + "clickNavigate": { + "path": "./navigate.ts:clickNavigate" + }, + "taskToggle": { + "path": "./task.ts:taskToggle" }, "word_count_command": { "path": "./word_count_command.ts:wordCount" }, - "link_navigate": { - "path": "./link_navigate.ts:linkNavigate" - }, - "insert_nice_date": { + "insertToday": { "path": "./dates.ts:insertToday" }, "toggle_h1": { diff --git a/plugins/core/link_navigate.ts b/plugins/core/link_navigate.ts deleted file mode 100644 index 864bede..0000000 --- a/plugins/core/link_navigate.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { syscall } from "./lib/syscall.ts"; - -export async function linkNavigate() { - let syntaxNode = await syscall("editor.getSyntaxNodeUnderCursor"); - if (syntaxNode && syntaxNode.name === "WikiLinkPage") { - await syscall("editor.navigate", syntaxNode.text); - } -} diff --git a/plugins/core/navigate.ts b/plugins/core/navigate.ts new file mode 100644 index 0000000..5ef68d2 --- /dev/null +++ b/plugins/core/navigate.ts @@ -0,0 +1,21 @@ +import { ClickEvent } from "../../webapp/src/app_event.ts"; +import { syscall } from "./lib/syscall.ts"; + +export async function linkNavigate() { + let syntaxNode = await syscall("editor.getSyntaxNodeUnderCursor"); + if (syntaxNode && syntaxNode.name === "WikiLinkPage") { + await syscall("editor.navigate", syntaxNode.text); + } +} + +export async function clickNavigate(event: ClickEvent) { + let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos); + + if (event.ctrlKey || event.metaKey) { + console.log("Here", syntaxNode); + if (syntaxNode && syntaxNode.name === "WikiLinkPage") { + await syscall("editor.navigate", syntaxNode.text); + return; + } + } +} diff --git a/plugins/core/task.ts b/plugins/core/task.ts new file mode 100644 index 0000000..d5d618f --- /dev/null +++ b/plugins/core/task.ts @@ -0,0 +1,31 @@ +import { ClickEvent } from "../../webapp/src/app_event.ts"; +import { syscall } from "./lib/syscall.ts"; + +export async function taskToggle(event: ClickEvent) { + let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos); + if (syntaxNode && syntaxNode.name === "TaskMarker") { + if (syntaxNode.text === "[x]" || syntaxNode.text === "[X]") { + await syscall("editor.dispatch", { + changes: { + from: syntaxNode.from, + to: syntaxNode.to, + insert: "[ ]", + }, + selection: { + anchor: event.pos, + }, + }); + } else { + await syscall("editor.dispatch", { + changes: { + from: syntaxNode.from, + to: syntaxNode.to, + insert: "[x]", + }, + selection: { + anchor: event.pos, + }, + }); + } + } +} diff --git a/webapp/src/syscalls/editor.browser.ts b/webapp/src/syscalls/editor.browser.ts index e9073f6..046de22 100644 --- a/webapp/src/syscalls/editor.browser.ts +++ b/webapp/src/syscalls/editor.browser.ts @@ -1,6 +1,14 @@ import { Editor } from "../editor"; import { SyscallContext } from "../plugins/runtime"; import { syntaxTree } from "@codemirror/language"; +import { Transaction } from "@codemirror/state"; + +type SyntaxNode = { + name: string; + text: string; + from: number; + to: number; +}; export default (editor: Editor) => ({ "editor.getText": (ctx: SyscallContext) => { @@ -56,7 +64,7 @@ export default (editor: Editor) => ({ }, "editor.getSyntaxNodeUnderCursor": ( ctx: SyscallContext - ): { name: string; text: string } | undefined => { + ): SyntaxNode | undefined => { const editorState = editor.editorView!.state; let selection = editorState.selection.main; if (selection.empty) { @@ -65,6 +73,8 @@ export default (editor: Editor) => ({ return { name: node.name, text: editorState.sliceDoc(node.from, node.to), + from: node.from, + to: node.to, }; } } @@ -72,14 +82,19 @@ export default (editor: Editor) => ({ "editor.getSyntaxNodeAtPos": ( ctx: SyscallContext, pos: number - ): { name: string; text: string } | undefined => { + ): SyntaxNode | undefined => { const editorState = editor.editorView!.state; let node = syntaxTree(editorState).resolveInner(pos); if (node) { return { name: node.name, text: editorState.sliceDoc(node.from, node.to), + from: node.from, + to: node.to, }; } }, + "editor.dispatch": (ctx: SyscallContext, change: Transaction) => { + editor.editorView!.dispatch(change); + }, });