2022-04-05 15:02:17 +00:00
|
|
|
import { ClickEvent } from "../../webapp/app_event";
|
|
|
|
import { updateMaterializedQueriesCommand } from "./materialized_queries";
|
|
|
|
import { getCursor, getText, navigate as navigateTo, openUrl } from "plugos-silverbullet-syscall/editor";
|
|
|
|
import { taskToggleAtPos } from "../tasks/task";
|
|
|
|
import { parseMarkdown } from "plugos-silverbullet-syscall/markdown";
|
|
|
|
import { MarkdownTree, nodeAtPos } from "../lib/tree";
|
2022-03-29 15:02:28 +00:00
|
|
|
|
|
|
|
const materializedQueryPrefix = /<!--\s*#query\s+/;
|
2022-02-26 16:50:50 +00:00
|
|
|
|
2022-04-03 16:42:12 +00:00
|
|
|
async function actionClickOrActionEnter(mdTree: MarkdownTree | null) {
|
|
|
|
if (!mdTree) {
|
2022-02-28 13:58:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-03 16:42:12 +00:00
|
|
|
console.log("Attempting to navigate based on syntax node", mdTree);
|
|
|
|
switch (mdTree.type) {
|
2022-02-28 13:58:18 +00:00
|
|
|
case "WikiLinkPage":
|
2022-04-03 16:42:12 +00:00
|
|
|
let pageLink = mdTree.children![0].text!;
|
|
|
|
let pos = "0";
|
2022-03-28 13:25:05 +00:00
|
|
|
if (pageLink.includes("@")) {
|
2022-04-03 16:42:12 +00:00
|
|
|
[pageLink, pos] = pageLink.split("@");
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|
2022-04-01 15:07:08 +00:00
|
|
|
await navigateTo(pageLink, +pos);
|
2022-02-28 13:58:18 +00:00
|
|
|
break;
|
|
|
|
case "URL":
|
2022-04-03 16:42:12 +00:00
|
|
|
await openUrl(mdTree.children![0].text!);
|
2022-02-28 13:58:18 +00:00
|
|
|
break;
|
2022-03-29 15:02:28 +00:00
|
|
|
case "CommentBlock":
|
2022-04-03 16:42:12 +00:00
|
|
|
if (mdTree.children![0].text!.match(materializedQueryPrefix)) {
|
2022-03-29 15:02:28 +00:00
|
|
|
await updateMaterializedQueriesCommand();
|
|
|
|
}
|
|
|
|
break;
|
2022-02-28 13:58:18 +00:00
|
|
|
case "Link":
|
2022-04-03 16:42:12 +00:00
|
|
|
await openUrl(mdTree.children![4].children![0].text!);
|
2022-03-07 09:21:02 +00:00
|
|
|
break;
|
2022-04-01 15:32:03 +00:00
|
|
|
case "TaskMarker":
|
2022-04-04 13:25:07 +00:00
|
|
|
await taskToggleAtPos(mdTree.from! + 1);
|
2022-04-01 15:32:03 +00:00
|
|
|
break;
|
2022-02-26 16:50:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:58:18 +00:00
|
|
|
export async function linkNavigate() {
|
2022-04-04 09:51:41 +00:00
|
|
|
let mdTree = await parseMarkdown(await getText());
|
2022-04-04 13:25:07 +00:00
|
|
|
let 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-02-26 16:50:50 +00:00
|
|
|
if (event.ctrlKey || event.metaKey) {
|
2022-04-04 09:51:41 +00:00
|
|
|
let mdTree = await parseMarkdown(await getText());
|
|
|
|
let newNode = nodeAtPos(mdTree, event.pos);
|
2022-04-03 16:42:12 +00:00
|
|
|
await actionClickOrActionEnter(newNode);
|
2022-02-26 16:50:50 +00:00
|
|
|
}
|
|
|
|
}
|