1
0
silverbullet/plugs/core/navigate.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-03-25 11:03:06 +00:00
import { ClickEvent } from "../../webapp/app_event";
import { syscall } from "../lib/syscall";
2022-02-26 16:50:50 +00:00
async function navigate(syntaxNode: any) {
if (!syntaxNode) {
return;
}
console.log("Attempting to navigate based on syntax node", syntaxNode);
switch (syntaxNode.name) {
case "WikiLinkPage":
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]);
}
2022-03-07 09:21:02 +00:00
break;
2022-02-26 16:50:50 +00:00
}
}
export async function linkNavigate() {
2022-03-15 13:03:00 +00:00
await navigate(await syscall("editor.getSyntaxNodeUnderCursor"));
}
2022-02-26 16:50:50 +00:00
export async function clickNavigate(event: ClickEvent) {
2022-02-26 16:50:50 +00:00
if (event.ctrlKey || event.metaKey) {
let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos);
2022-03-15 13:03:00 +00:00
await navigate(syntaxNode);
2022-02-26 16:50:50 +00:00
}
}
2022-02-27 09:17:43 +00:00
export async function pageComplete() {
2022-03-25 11:03:06 +00:00
let prefix = await syscall("editor.matchBefore", "\\[\\[[\\w\\s]*");
2022-02-27 09:17:43 +00:00
if (!prefix) {
return null;
}
let allPages = await syscall("space.listPages");
2022-03-25 11:03:06 +00:00
return {
from: prefix.from + 2,
options: allPages.map((pageMeta: any) => ({
label: pageMeta.name,
type: "page",
})),
};
2022-02-27 09:17:43 +00:00
}