1
0
silverbullet/plugs/editor/navigate.ts

117 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-10-14 13:11:33 +00:00
import type { ClickEvent } from "$sb/app_event.ts";
2023-08-28 15:12:15 +00:00
import { editor, markdown, system } from "$sb/syscalls.ts";
import {
addParentPointers,
2022-12-29 11:53:42 +00:00
findNodeOfType,
findParentMatching,
nodeAtPos,
ParseTree,
} from "$sb/lib/tree.ts";
2023-07-29 21:41:37 +00:00
import { resolvePath } from "$sb/lib/resolve.ts";
2022-03-29 15:02:28 +00:00
async function actionClickOrActionEnter(
mdTree: ParseTree | null,
inNewWindow = false,
) {
if (!mdTree) {
return;
}
const navigationNodeFinder = (t: ParseTree) =>
2022-12-19 12:42:20 +00:00
[
"WikiLink",
"Link",
"Image",
"URL",
"NakedURL",
"Link",
"CommandLink",
"PageRef",
]
2022-12-19 12:16:22 +00:00
.includes(
t.type!,
);
if (!navigationNodeFinder(mdTree)) {
mdTree = findParentMatching(mdTree, navigationNodeFinder);
if (!mdTree) {
return;
}
}
const currentPage = await editor.getCurrentPage();
switch (mdTree.type) {
case "WikiLink": {
let pageLink = mdTree.children![1]!.children![0].text!;
2022-08-30 08:44:20 +00:00
let pos;
2023-11-03 11:01:33 +00:00
if (pageLink.includes("@") || pageLink.includes("$")) {
[pageLink, pos] = pageLink.split(/[@$]/);
2022-08-30 08:44:20 +00:00
if (pos.match(/^\d+$/)) {
pos = +pos;
}
2022-03-28 13:25:05 +00:00
}
2023-07-29 21:41:37 +00:00
pageLink = resolvePath(currentPage, pageLink);
2022-08-30 08:44:20 +00:00
if (!pageLink) {
pageLink = currentPage;
2022-08-30 08:44:20 +00:00
}
await editor.navigate(pageLink, pos, false, inNewWindow);
break;
}
2022-12-19 12:42:20 +00:00
case "PageRef": {
const bracketedPageRef = mdTree.children![0].text!;
// Slicing off the initial [[ and final ]]
const pageName = bracketedPageRef.substring(
2,
bracketedPageRef.length - 2,
2022-12-19 12:42:20 +00:00
);
await editor.navigate(pageName, 0, false, inNewWindow);
2022-12-19 12:42:20 +00:00
break;
}
case "NakedURL":
await editor.openUrl(mdTree.children![0].text!);
break;
2022-12-19 12:16:22 +00:00
case "Image":
case "Link": {
2022-12-29 11:53:42 +00:00
const urlNode = findNodeOfType(mdTree, "URL");
if (!urlNode) {
return;
}
const url = urlNode.children![0].text!;
2022-08-29 13:40:38 +00:00
if (url.length <= 1) {
2022-10-14 13:11:33 +00:00
return editor.flashNotification("Empty link, ignoring", "error");
2022-08-29 13:40:38 +00:00
}
2023-02-23 15:09:39 +00:00
if (url.indexOf("://") === -1 && !url.startsWith("mailto:")) {
2023-07-29 21:41:37 +00:00
return editor.openUrl(resolvePath(currentPage, decodeURI(url)));
} else {
await editor.openUrl(url);
}
2022-03-07 09:21:02 +00:00
break;
}
case "CommandLink": {
const commandName = mdTree.children![1]!.children![0].text!;
await system.invokeCommand(commandName);
break;
}
2022-02-26 16:50:50 +00:00
}
}
export async function linkNavigate() {
2022-10-14 13:11:33 +00:00
const mdTree = await markdown.parseMarkdown(await editor.getText());
const newNode = nodeAtPos(mdTree, await editor.getCursor());
addParentPointers(mdTree);
await actionClickOrActionEnter(newNode);
}
2022-02-26 16:50:50 +00:00
export async function clickNavigate(event: ClickEvent) {
// Navigate by default, don't navigate when Alt is held
if (event.altKey) {
2022-04-11 18:34:09 +00:00
return;
2022-02-26 16:50:50 +00:00
}
2022-10-14 13:11:33 +00:00
const mdTree = await markdown.parseMarkdown(await editor.getText());
addParentPointers(mdTree);
const newNode = nodeAtPos(mdTree, event.pos);
await actionClickOrActionEnter(newNode, event.ctrlKey || event.metaKey);
2022-02-26 16:50:50 +00:00
}
export async function navigateCommand(cmdDef: any) {
2022-10-14 13:11:33 +00:00
await editor.navigate(cmdDef.page);
}