2022-10-10 12:50:21 +00:00
|
|
|
import type { ClickEvent } from "../../web/app_event.ts";
|
2022-04-21 11:57:45 +00:00
|
|
|
import {
|
2022-08-29 13:40:38 +00:00
|
|
|
flashNotification,
|
2022-08-30 08:44:20 +00:00
|
|
|
getCurrentPage,
|
2022-04-21 11:57:45 +00:00
|
|
|
getCursor,
|
|
|
|
getText,
|
|
|
|
navigate as navigateTo,
|
2022-04-25 08:33:38 +00:00
|
|
|
openUrl,
|
2022-10-10 12:50:21 +00:00
|
|
|
} from "../../syscall/silverbullet-syscall/editor.ts";
|
|
|
|
import { parseMarkdown } from "../../syscall/silverbullet-syscall/markdown.ts";
|
|
|
|
import { nodeAtPos, ParseTree } from "../../common/tree.ts";
|
|
|
|
import { invokeCommand } from "../../syscall/silverbullet-syscall/system.ts";
|
2022-03-29 15:02:28 +00:00
|
|
|
|
2022-09-06 12:36:06 +00:00
|
|
|
// Checks if the URL contains a protocol, if so keeps it, otherwise assumes an attachment
|
|
|
|
function patchUrl(url: string): string {
|
|
|
|
if (url.indexOf("://") === -1) {
|
2022-09-12 12:50:37 +00:00
|
|
|
return `fs/${url}`;
|
2022-09-06 12:36:06 +00:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2022-04-11 18:34:09 +00:00
|
|
|
async function actionClickOrActionEnter(mdTree: ParseTree | null) {
|
2022-04-03 16:42:12 +00:00
|
|
|
if (!mdTree) {
|
2022-02-28 13:58:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-05-13 12:36:26 +00:00
|
|
|
// console.log("Attempting to navigate based on syntax node", mdTree);
|
2022-04-03 16:42:12 +00:00
|
|
|
switch (mdTree.type) {
|
2022-10-10 12:50:21 +00:00
|
|
|
case "WikiLinkPage": {
|
2022-04-03 16:42:12 +00:00
|
|
|
let pageLink = mdTree.children![0].text!;
|
2022-08-30 08:44:20 +00:00
|
|
|
let pos;
|
2022-03-28 13:25:05 +00:00
|
|
|
if (pageLink.includes("@")) {
|
2022-04-03 16:42:12 +00:00
|
|
|
[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
|
|
|
}
|
2022-08-30 08:44:20 +00:00
|
|
|
if (!pageLink) {
|
|
|
|
pageLink = await getCurrentPage();
|
|
|
|
}
|
|
|
|
await navigateTo(pageLink, pos);
|
2022-02-28 13:58:18 +00:00
|
|
|
break;
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
2022-02-28 13:58:18 +00:00
|
|
|
case "URL":
|
2022-04-12 11:33:07 +00:00
|
|
|
case "NakedURL":
|
2022-09-06 12:36:06 +00:00
|
|
|
await openUrl(patchUrl(mdTree.children![0].text!));
|
2022-02-28 13:58:18 +00:00
|
|
|
break;
|
2022-10-10 12:50:21 +00:00
|
|
|
case "Link": {
|
2022-09-06 12:36:06 +00:00
|
|
|
const url = patchUrl(mdTree.children![4].children![0].text!);
|
2022-08-29 13:40:38 +00:00
|
|
|
if (url.length <= 1) {
|
|
|
|
return flashNotification("Empty link, ignoring", "error");
|
|
|
|
}
|
|
|
|
await openUrl(url);
|
2022-03-07 09:21:02 +00:00
|
|
|
break;
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
|
|
|
case "CommandLink": {
|
|
|
|
const command = mdTree
|
2022-07-18 14:44:43 +00:00
|
|
|
.children![0].text!.substring(2, mdTree.children![0].text!.length - 2)
|
|
|
|
.trim();
|
|
|
|
console.log("Got command link", command);
|
|
|
|
await invokeCommand(command);
|
|
|
|
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-10 12:50:21 +00:00
|
|
|
const mdTree = await parseMarkdown(await getText());
|
|
|
|
const newNode = nodeAtPos(mdTree, await getCursor());
|
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-04-11 18:34:09 +00:00
|
|
|
// Navigate by default, don't navigate when Ctrl or Cmd is held
|
2022-02-26 16:50:50 +00:00
|
|
|
if (event.ctrlKey || event.metaKey) {
|
2022-04-11 18:34:09 +00:00
|
|
|
return;
|
2022-02-26 16:50:50 +00:00
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
const mdTree = await parseMarkdown(await getText());
|
|
|
|
const newNode = nodeAtPos(mdTree, event.pos);
|
2022-04-11 18:34:09 +00:00
|
|
|
await actionClickOrActionEnter(newNode);
|
2022-02-26 16:50:50 +00:00
|
|
|
}
|
2022-07-04 09:52:09 +00:00
|
|
|
|
|
|
|
export async function navigateCommand(cmdDef: any) {
|
|
|
|
await navigateTo(cmdDef.page);
|
|
|
|
}
|