Rewrote some navigation stuff based on new parser

This commit is contained in:
Zef Hemel
2022-04-03 18:42:12 +02:00
parent 16bf0d866d
commit 07453d638b
21 changed files with 206 additions and 235 deletions
+17 -25
View File
@@ -1,62 +1,54 @@
import {ClickEvent} from "../../webapp/app_event";
import {updateMaterializedQueriesCommand} from "./materialized_queries";
import {
getSyntaxNodeAtPos,
getSyntaxNodeUnderCursor,
getText,
navigate as navigateTo,
openUrl,
} from "plugos-silverbullet-syscall/editor";
import {getCursor, getText, navigate as navigateTo, openUrl,} from "plugos-silverbullet-syscall/editor";
import {taskToggleAtPos} from "../tasks/task";
import {nodeAtPos, parse} from "plugos-silverbullet-syscall/markdown";
import type {MarkdownTree} from "../../common/tree";
const materializedQueryPrefix = /<!--\s*#query\s+/;
async function actionClickOrActionEnter(syntaxNode: any) {
if (!syntaxNode) {
async function actionClickOrActionEnter(mdTree: MarkdownTree | null) {
if (!mdTree) {
return;
}
console.log("Attempting to navigate based on syntax node", syntaxNode);
switch (syntaxNode.name) {
console.log("Attempting to navigate based on syntax node", mdTree);
switch (mdTree.type) {
case "WikiLinkPage":
let pageLink = syntaxNode.text;
let pos = 0;
let pageLink = mdTree.children![0].text!;
let pos = "0";
if (pageLink.includes("@")) {
[pageLink, pos] = syntaxNode.text.split("@");
[pageLink, pos] = pageLink.split("@");
}
await navigateTo(pageLink, +pos);
break;
case "URL":
await openUrl(syntaxNode.text);
await openUrl(mdTree.children![0].text!);
break;
case "CommentBlock":
if (syntaxNode.text.match(materializedQueryPrefix)) {
if (mdTree.children![0].text!.match(materializedQueryPrefix)) {
await updateMaterializedQueriesCommand();
}
break;
case "Link":
// Markdown link: [bla](URLHERE) needs extraction
let match = /\[[^\\]+\]\(([^\)]+)\)/.exec(syntaxNode.text);
if (match) {
await openUrl(match[1]);
}
await openUrl(mdTree.children![4].children![0].text!);
break;
case "TaskMarker":
await taskToggleAtPos(syntaxNode.from + 1);
await taskToggleAtPos(mdTree.from + 1);
break;
}
}
export async function linkNavigate() {
await actionClickOrActionEnter(await getSyntaxNodeUnderCursor());
let mdTree = await parse(await getText());
let newNode = await nodeAtPos(mdTree, await getCursor());
await actionClickOrActionEnter(newNode);
}
export async function clickNavigate(event: ClickEvent) {
if (event.ctrlKey || event.metaKey) {
let syntaxNode = await getSyntaxNodeAtPos(event.pos);
let mdTree = await parse(await getText());
let newNode = await nodeAtPos(mdTree, event.pos);
console.log("New node", newNode);
await actionClickOrActionEnter(syntaxNode);
await actionClickOrActionEnter(newNode);
}
}