1
0

Refactoring of builtins

This commit is contained in:
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"
},
"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": {

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