1
0
silverbullet/plugs/core/navigate.ts

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-03-25 11:03:06 +00:00
import { ClickEvent } from "../../webapp/app_event";
2022-03-29 15:02:28 +00:00
import { updateMaterializedQueriesCommand } from "./materialized_queries";
2022-04-01 15:07:08 +00:00
import {
getSyntaxNodeAtPos,
getSyntaxNodeUnderCursor,
navigate as navigateTo,
openUrl,
} from "plugos-silverbullet-syscall/editor";
2022-03-29 15:02:28 +00:00
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("@");
}
2022-04-01 15:07:08 +00:00
await navigateTo(pageLink, +pos);
break;
case "URL":
2022-04-01 15:07:08 +00:00
await 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) {
2022-04-01 15:07:08 +00:00
await 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-04-01 15:07:08 +00:00
await navigate(await 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) {
2022-04-01 15:07:08 +00:00
let syntaxNode = await getSyntaxNodeAtPos(event.pos);
2022-03-15 13:03:00 +00:00
await navigate(syntaxNode);
2022-02-26 16:50:50 +00:00
}
}