Refactoring of builtins

pull/3/head
Zef Hemel 2022-02-26 17:50:50 +01:00
parent 03e1eb2353
commit bfd77de2d5
6 changed files with 80 additions and 35 deletions

View File

@ -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);
}
}
}

View File

@ -9,8 +9,8 @@
"mac": "Cmd-Enter" "mac": "Cmd-Enter"
}, },
"Insert Current Date": { "Insert Current Date": {
"invoke": "insert_nice_date", "invoke": "insertToday",
"slashCommand": "/insert-today" "slashCommand": "/today"
}, },
"Toggle : Heading 1": { "Toggle : Heading 1": {
"invoke": "toggle_h1", "invoke": "toggle_h1",
@ -24,23 +24,22 @@
} }
}, },
"events": { "events": {
"app:ready": ["welcome"], "page:click": ["taskToggle", "clickNavigate"]
"page:click": ["click"]
}, },
"functions": { "functions": {
"welcome": { "linkNavigate": {
"path": "./welcome.ts" "path": "./navigate.ts:linkNavigate"
}, },
"click": { "clickNavigate": {
"path": "./click.ts" "path": "./navigate.ts:clickNavigate"
},
"taskToggle": {
"path": "./task.ts:taskToggle"
}, },
"word_count_command": { "word_count_command": {
"path": "./word_count_command.ts:wordCount" "path": "./word_count_command.ts:wordCount"
}, },
"link_navigate": { "insertToday": {
"path": "./link_navigate.ts:linkNavigate"
},
"insert_nice_date": {
"path": "./dates.ts:insertToday" "path": "./dates.ts:insertToday"
}, },
"toggle_h1": { "toggle_h1": {

View File

@ -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);
}
}

21
plugins/core/navigate.ts Normal file
View File

@ -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;
}
}
}

31
plugins/core/task.ts Normal file
View File

@ -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,
},
});
}
}
}

View File

@ -1,6 +1,14 @@
import { Editor } from "../editor"; import { Editor } from "../editor";
import { SyscallContext } from "../plugins/runtime"; import { SyscallContext } from "../plugins/runtime";
import { syntaxTree } from "@codemirror/language"; import { syntaxTree } from "@codemirror/language";
import { Transaction } from "@codemirror/state";
type SyntaxNode = {
name: string;
text: string;
from: number;
to: number;
};
export default (editor: Editor) => ({ export default (editor: Editor) => ({
"editor.getText": (ctx: SyscallContext) => { "editor.getText": (ctx: SyscallContext) => {
@ -56,7 +64,7 @@ export default (editor: Editor) => ({
}, },
"editor.getSyntaxNodeUnderCursor": ( "editor.getSyntaxNodeUnderCursor": (
ctx: SyscallContext ctx: SyscallContext
): { name: string; text: string } | undefined => { ): SyntaxNode | undefined => {
const editorState = editor.editorView!.state; const editorState = editor.editorView!.state;
let selection = editorState.selection.main; let selection = editorState.selection.main;
if (selection.empty) { if (selection.empty) {
@ -65,6 +73,8 @@ export default (editor: Editor) => ({
return { return {
name: node.name, name: node.name,
text: editorState.sliceDoc(node.from, node.to), text: editorState.sliceDoc(node.from, node.to),
from: node.from,
to: node.to,
}; };
} }
} }
@ -72,14 +82,19 @@ export default (editor: Editor) => ({
"editor.getSyntaxNodeAtPos": ( "editor.getSyntaxNodeAtPos": (
ctx: SyscallContext, ctx: SyscallContext,
pos: number pos: number
): { name: string; text: string } | undefined => { ): SyntaxNode | undefined => {
const editorState = editor.editorView!.state; const editorState = editor.editorView!.state;
let node = syntaxTree(editorState).resolveInner(pos); let node = syntaxTree(editorState).resolveInner(pos);
if (node) { if (node) {
return { return {
name: node.name, name: node.name,
text: editorState.sliceDoc(node.from, node.to), text: editorState.sliceDoc(node.from, node.to),
from: node.from,
to: node.to,
}; };
} }
}, },
"editor.dispatch": (ctx: SyscallContext, change: Transaction) => {
editor.editorView!.dispatch(change);
},
}); });