From e07001dae916eb195eff81c7bffef7c9fa96755a Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Tue, 9 Jan 2024 09:37:06 +0100 Subject: [PATCH] Fix command links in widgets and multi widget suport --- plugs/index/template_widget.ts | 13 ++++++++++--- web/cm_plugins/markdown_widget.ts | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/plugs/index/template_widget.ts b/plugs/index/template_widget.ts index afffcf6a..b8388774 100644 --- a/plugs/index/template_widget.ts +++ b/plugs/index/template_widget.ts @@ -38,6 +38,14 @@ export async function renderTemplateWidgets(side: "top" | "bottom"): Promise< const templateBits: string[] = []; // Strategy: walk through all matching templates, evaluate the 'where' expression, and pick the first one that matches for (const template of allFrontMatterTemplates) { + if (!template.where) { + console.warn( + "Skipping template", + template.ref, + "because it has no 'where' expression", + ); + continue; + } const exprAST = parseTreeToAST( await language.parseLanguage("expression", template.where!), ); @@ -45,7 +53,6 @@ export async function renderTemplateWidgets(side: "top" | "bottom"): Promise< if (evalQueryExpression(parsedExpression, pageMeta)) { // Match! We're happy const templateText = await space.readPage(template.ref); - // templateBits.push(await space.readPage(template.ref)); let renderedTemplate = (await renderTemplate( templateText, pageMeta, @@ -56,10 +63,10 @@ export async function renderTemplateWidgets(side: "top" | "bottom"): Promise< rewritePageRefs(parsedMarkdown, template.ref); renderedTemplate = renderToText(parsedMarkdown); - templateBits.push(renderedTemplate); + templateBits.push(renderedTemplate.trim()); } } - const summaryText = templateBits.join(""); + const summaryText = templateBits.join("\n"); // console.log("Rendered", summaryText); return { markdown: summaryText, diff --git a/web/cm_plugins/markdown_widget.ts b/web/cm_plugins/markdown_widget.ts index 762ace94..2521640d 100644 --- a/web/cm_plugins/markdown_widget.ts +++ b/web/cm_plugins/markdown_widget.ts @@ -136,6 +136,7 @@ export class MarkdownWidget extends WidgetType { } private attachListeners(div: HTMLElement, buttons?: CodeWidgetButton[]) { + // Override wiki links with local navigate (faster) div.querySelectorAll("a[data-ref]").forEach((el_) => { const el = el_ as HTMLElement; // Override default click behavior with a local navigate (faster) @@ -151,6 +152,21 @@ export class MarkdownWidget extends WidgetType { }); }); + div.querySelectorAll("button[data-onclick]").forEach((el_) => { + const el = el_ as HTMLElement; + const onclick = el.dataset.onclick!; + const parsedOnclick = JSON.parse(onclick); + if (parsedOnclick[0] === "command") { + const command = parsedOnclick[1]; + el.addEventListener("click", (e) => { + e.preventDefault(); + e.stopPropagation(); + console.info("Command link clicked in widget, running", command); + this.client.runCommandByName(command).catch(console.error); + }); + } + }); + // Implement task toggling div.querySelectorAll("span[data-external-task-ref]").forEach((el: any) => { const taskRef = el.dataset.externalTaskRef;