Fix command links in widgets and multi widget suport

pull/624/head
Zef Hemel 2024-01-09 09:37:06 +01:00
parent 59a9e161de
commit e07001dae9
2 changed files with 26 additions and 3 deletions

View File

@ -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,

View File

@ -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;