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