1
0
silverbullet/plugs/core/navigate.ts

49 lines
1.4 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-03-29 15:02:28 +00:00
import { updateMaterializedQueriesCommand } from "./materialized_queries";
const materializedQueryPrefix = /<!--\s*#query\s+/;
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":
2022-03-28 13:25:05 +00:00
let pageLink = syntaxNode.text;
let pos = 0;
if (pageLink.includes("@")) {
[pageLink, pos] = syntaxNode.text.split("@");
}
await syscall("editor.navigate", pageLink, +pos);
break;
case "URL":
await syscall("editor.openUrl", syntaxNode.text);
break;
2022-03-29 15:02:28 +00:00
case "CommentBlock":
if (syntaxNode.text.match(materializedQueryPrefix)) {
await updateMaterializedQueriesCommand();
}
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
}
}