1
0
silverbullet/plugs/core/navigate.ts
2022-03-15 14:03:00 +01:00

67 lines
1.7 KiB
TypeScript

import { ClickEvent } from "../../webapp/src/app_event";
import { syscall } from "./lib/syscall";
async function navigate(syntaxNode: any) {
if (!syntaxNode) {
return;
}
console.log("Attempting to navigate based on syntax node", syntaxNode);
switch (syntaxNode.name) {
case "WikiLinkPage":
case "AtMention":
await syscall("editor.navigate", syntaxNode.text);
break;
case "URL":
await syscall("editor.openUrl", syntaxNode.text);
break;
case "Link":
// Markdown link: [bla](URLHERE) needs extraction
let match = /\[[^\\]+\]\(([^\)]+)\)/.exec(syntaxNode.text);
if (match) {
await syscall("editor.openUrl", match[1]);
}
break;
}
}
export async function linkNavigate() {
await navigate(await syscall("editor.getSyntaxNodeUnderCursor"));
}
export async function clickNavigate(event: ClickEvent) {
if (event.ctrlKey || event.metaKey) {
let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos);
await navigate(syntaxNode);
}
}
export async function pageComplete() {
let prefix = await syscall(
"editor.matchBefore",
"(\\[\\[[\\w\\s]*|@[\\w\\.]*)"
);
if (!prefix) {
return null;
}
let allPages = await syscall("space.listPages");
if (prefix.text[0] === "@") {
return {
from: prefix.from,
options: allPages
.filter((page: any) => page.name.startsWith(prefix.text))
.map((pageMeta: any) => ({
label: pageMeta.name,
type: "page",
})),
};
} else {
return {
from: prefix.from + 2,
options: allPages.map((pageMeta: any) => ({
label: pageMeta.name,
type: "page",
})),
};
}
}