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";
|
2022-11-18 15:04:37 +00:00
|
|
|
import {
|
|
|
|
addParentPointers,
|
2022-12-29 11:53:42 +00:00
|
|
|
findNodeOfType,
|
2022-11-18 15:04:37 +00:00
|
|
|
findParentMatching,
|
|
|
|
nodeAtPos,
|
|
|
|
ParseTree,
|
|
|
|
} from "$sb/lib/tree.ts";
|
2023-12-19 16:55:11 +00:00
|
|
|
import { resolveAttachmentPath, resolvePath } from "$sb/lib/resolve.ts";
|
2024-01-24 10:58:33 +00:00
|
|
|
import { parsePageRef } from "$sb/lib/page.ts";
|
2022-03-29 15:02:28 +00:00
|
|
|
|
2022-10-29 07:23:12 +00:00
|
|
|
async function actionClickOrActionEnter(
|
|
|
|
mdTree: ParseTree | null,
|
|
|
|
inNewWindow = false,
|
|
|
|
) {
|
2022-04-03 16:42:12 +00:00
|
|
|
if (!mdTree) {
|
2022-02-28 13:58:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-18 15:04:37 +00:00
|
|
|
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!,
|
|
|
|
);
|
2022-11-18 15:04:37 +00:00
|
|
|
if (!navigationNodeFinder(mdTree)) {
|
|
|
|
mdTree = findParentMatching(mdTree, navigationNodeFinder);
|
|
|
|
if (!mdTree) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-07-02 09:25:32 +00:00
|
|
|
const currentPage = await editor.getCurrentPage();
|
2022-04-03 16:42:12 +00:00
|
|
|
switch (mdTree.type) {
|
2022-11-18 15:04:37 +00:00
|
|
|
case "WikiLink": {
|
2024-01-24 10:58:33 +00:00
|
|
|
const pageLink = mdTree.children![1]!.children![0].text!;
|
|
|
|
const pageRef = parsePageRef(pageLink);
|
|
|
|
pageRef.page = resolvePath(currentPage, pageRef.page);
|
|
|
|
if (!pageRef.page) {
|
|
|
|
pageRef.page = currentPage;
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|
2024-01-24 10:58:33 +00:00
|
|
|
// This is an explicit navigate, move to the top
|
|
|
|
if (pageRef.pos === undefined) {
|
|
|
|
pageRef.pos = 0;
|
2022-08-30 08:44:20 +00:00
|
|
|
}
|
2024-01-24 10:58:33 +00:00
|
|
|
await editor.navigate(pageRef, false, inNewWindow);
|
2022-02-28 13:58:18 +00:00
|
|
|
break;
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
2022-12-19 12:42:20 +00:00
|
|
|
case "PageRef": {
|
2024-01-24 10:58:33 +00:00
|
|
|
const pageName = parsePageRef(mdTree.children![0].text!).page;
|
|
|
|
await editor.navigate({ page: pageName, pos: 0 }, false, inNewWindow);
|
2022-12-19 12:42:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-04-12 11:33:07 +00:00
|
|
|
case "NakedURL":
|
2023-01-08 14:29:34 +00:00
|
|
|
await editor.openUrl(mdTree.children![0].text!);
|
2022-02-28 13:58:18 +00:00
|
|
|
break;
|
2022-12-19 12:16:22 +00:00
|
|
|
case "Image":
|
2022-10-10 12:50:21 +00:00
|
|
|
case "Link": {
|
2022-12-29 11:53:42 +00:00
|
|
|
const urlNode = findNodeOfType(mdTree, "URL");
|
|
|
|
if (!urlNode) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-02 09:25:32 +00:00
|
|
|
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-12-19 16:55:11 +00:00
|
|
|
return editor.openUrl(
|
|
|
|
resolveAttachmentPath(currentPage, decodeURI(url)),
|
|
|
|
);
|
2023-01-08 14:29:34 +00:00
|
|
|
} else {
|
|
|
|
await editor.openUrl(url);
|
|
|
|
}
|
2022-03-07 09:21:02 +00:00
|
|
|
break;
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
|
|
|
case "CommandLink": {
|
2022-11-18 15:04:37 +00:00
|
|
|
const commandName = mdTree.children![1]!.children![0].text!;
|
2023-11-25 17:57:00 +00:00
|
|
|
const argsNode = findNodeOfType(mdTree, "CommandLinkArgs");
|
|
|
|
const argsText = argsNode?.children![0]?.text;
|
|
|
|
// Assume the arguments are can be parsed as the innards of a valid JSON list
|
|
|
|
try {
|
|
|
|
const args = argsText ? JSON.parse(`[${argsText}]`) : [];
|
|
|
|
await system.invokeCommand(commandName, args);
|
2023-12-19 16:55:11 +00:00
|
|
|
} catch (e: any) {
|
|
|
|
await editor.flashNotification(
|
|
|
|
`Error parsing command link arguments: ${e.message}`,
|
|
|
|
"error",
|
|
|
|
);
|
2023-11-25 17:57:00 +00:00
|
|
|
}
|
2022-07-18 14:44:43 +00:00
|
|
|
break;
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
2022-02-26 16:50:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:58:18 +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());
|
2022-11-18 15:04:37 +00:00
|
|
|
addParentPointers(mdTree);
|
2022-04-03 16:42:12 +00:00
|
|
|
await actionClickOrActionEnter(newNode);
|
2022-02-28 13:58:18 +00:00
|
|
|
}
|
2022-02-26 16:50:50 +00:00
|
|
|
|
2022-02-28 13:58:18 +00:00
|
|
|
export async function clickNavigate(event: ClickEvent) {
|
2022-10-29 07:23:12 +00:00
|
|
|
// 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());
|
2022-11-18 15:04:37 +00:00
|
|
|
addParentPointers(mdTree);
|
2022-10-10 12:50:21 +00:00
|
|
|
const newNode = nodeAtPos(mdTree, event.pos);
|
2022-10-29 07:23:12 +00:00
|
|
|
await actionClickOrActionEnter(newNode, event.ctrlKey || event.metaKey);
|
2022-02-26 16:50:50 +00:00
|
|
|
}
|
2022-07-04 09:52:09 +00:00
|
|
|
|
|
|
|
export async function navigateCommand(cmdDef: any) {
|
2024-01-24 10:58:33 +00:00
|
|
|
await editor.navigate({ page: cmdDef.page, pos: 0 });
|
2022-07-04 09:52:09 +00:00
|
|
|
}
|