2022-04-05 23:02:17 +08:00
|
|
|
import { ClickEvent } from "../../webapp/app_event";
|
|
|
|
import { getCursor, getText, navigate as navigateTo, openUrl } from "plugos-silverbullet-syscall/editor";
|
|
|
|
import { parseMarkdown } from "plugos-silverbullet-syscall/markdown";
|
2022-04-12 02:34:09 +08:00
|
|
|
import { nodeAtPos, ParseTree } from "../../common/tree";
|
2022-03-29 23:02:28 +08:00
|
|
|
|
|
|
|
const materializedQueryPrefix = /<!--\s*#query\s+/;
|
2022-02-27 00:50:50 +08:00
|
|
|
|
2022-04-12 02:34:09 +08:00
|
|
|
async function actionClickOrActionEnter(mdTree: ParseTree | null) {
|
2022-04-04 00:42:12 +08:00
|
|
|
if (!mdTree) {
|
2022-02-28 21:58:18 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-04-04 00:42:12 +08:00
|
|
|
console.log("Attempting to navigate based on syntax node", mdTree);
|
|
|
|
switch (mdTree.type) {
|
2022-02-28 21:58:18 +08:00
|
|
|
case "WikiLinkPage":
|
2022-04-04 00:42:12 +08:00
|
|
|
let pageLink = mdTree.children![0].text!;
|
|
|
|
let pos = "0";
|
2022-03-28 21:25:05 +08:00
|
|
|
if (pageLink.includes("@")) {
|
2022-04-04 00:42:12 +08:00
|
|
|
[pageLink, pos] = pageLink.split("@");
|
2022-03-28 21:25:05 +08:00
|
|
|
}
|
2022-04-01 23:07:08 +08:00
|
|
|
await navigateTo(pageLink, +pos);
|
2022-02-28 21:58:18 +08:00
|
|
|
break;
|
|
|
|
case "URL":
|
2022-04-04 00:42:12 +08:00
|
|
|
await openUrl(mdTree.children![0].text!);
|
2022-02-28 21:58:18 +08:00
|
|
|
break;
|
|
|
|
case "Link":
|
2022-04-04 00:42:12 +08:00
|
|
|
await openUrl(mdTree.children![4].children![0].text!);
|
2022-03-07 17:21:02 +08:00
|
|
|
break;
|
2022-02-27 00:50:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 21:58:18 +08:00
|
|
|
export async function linkNavigate() {
|
2022-04-04 17:51:41 +08:00
|
|
|
let mdTree = await parseMarkdown(await getText());
|
2022-04-04 21:25:07 +08:00
|
|
|
let newNode = nodeAtPos(mdTree, await getCursor());
|
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-04-12 02:34:09 +08:00
|
|
|
// Navigate by default, don't navigate when Ctrl or Cmd is held
|
2022-02-27 00:50:50 +08:00
|
|
|
if (event.ctrlKey || event.metaKey) {
|
2022-04-12 02:34:09 +08:00
|
|
|
return;
|
2022-02-27 00:50:50 +08:00
|
|
|
}
|
2022-04-12 02:34:09 +08:00
|
|
|
let mdTree = await parseMarkdown(await getText());
|
|
|
|
let newNode = nodeAtPos(mdTree, event.pos);
|
|
|
|
await actionClickOrActionEnter(newNode);
|
2022-02-27 00:50:50 +08:00
|
|
|
}
|