2023-12-27 12:38:38 +00:00
|
|
|
import { WidgetType } from "../deps.ts";
|
|
|
|
import type { Client } from "../client.ts";
|
2023-12-28 15:14:30 +00:00
|
|
|
import type { CodeWidgetButton, CodeWidgetCallback } from "$sb/types.ts";
|
2023-12-27 12:38:38 +00:00
|
|
|
import { renderMarkdownToHtml } from "../../plugs/markdown/markdown_render.ts";
|
|
|
|
import { resolveAttachmentPath } from "$sb/lib/resolve.ts";
|
|
|
|
import { parse } from "../../common/markdown_parser/parse_tree.ts";
|
|
|
|
import buildMarkdown from "../../common/markdown_parser/parser.ts";
|
|
|
|
import { renderToText } from "$sb/lib/tree.ts";
|
|
|
|
|
2023-12-28 15:14:30 +00:00
|
|
|
const activeWidgets = new Set<MarkdownWidget>();
|
|
|
|
|
2023-12-27 12:38:38 +00:00
|
|
|
export class MarkdownWidget extends WidgetType {
|
|
|
|
renderedMarkdown?: string;
|
2023-12-28 15:14:30 +00:00
|
|
|
public dom?: HTMLElement;
|
2023-12-27 12:38:38 +00:00
|
|
|
|
|
|
|
constructor(
|
2023-12-27 17:05:47 +00:00
|
|
|
readonly from: number | undefined,
|
2023-12-27 12:38:38 +00:00
|
|
|
readonly client: Client,
|
|
|
|
readonly bodyText: string,
|
|
|
|
readonly codeWidgetCallback: CodeWidgetCallback,
|
2023-12-27 17:05:47 +00:00
|
|
|
readonly className: string,
|
2023-12-27 12:38:38 +00:00
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
toDOM(): HTMLElement {
|
|
|
|
const div = document.createElement("div");
|
2023-12-27 17:05:47 +00:00
|
|
|
div.className = this.className;
|
2023-12-27 12:38:38 +00:00
|
|
|
const cacheItem = this.client.getWidgetCache(this.bodyText);
|
|
|
|
if (cacheItem) {
|
2023-12-27 17:05:47 +00:00
|
|
|
div.innerHTML = this.wrapHtml(
|
|
|
|
cacheItem.html,
|
2023-12-28 15:14:30 +00:00
|
|
|
cacheItem.buttons,
|
2023-12-27 17:05:47 +00:00
|
|
|
);
|
2023-12-28 15:14:30 +00:00
|
|
|
this.attachListeners(div, cacheItem.buttons);
|
2023-12-27 12:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Async kick-off of content renderer
|
|
|
|
this.renderContent(div, cacheItem?.html).catch(console.error);
|
|
|
|
|
2023-12-28 15:14:30 +00:00
|
|
|
this.dom = div;
|
|
|
|
|
2023-12-27 12:38:38 +00:00
|
|
|
return div;
|
|
|
|
}
|
|
|
|
|
2023-12-28 15:14:30 +00:00
|
|
|
async renderContent(
|
2023-12-27 12:38:38 +00:00
|
|
|
div: HTMLElement,
|
|
|
|
cachedHtml: string | undefined,
|
|
|
|
) {
|
|
|
|
const widgetContent = await this.codeWidgetCallback(
|
|
|
|
this.bodyText,
|
|
|
|
this.client.currentPage!,
|
|
|
|
);
|
2023-12-28 15:14:30 +00:00
|
|
|
activeWidgets.add(this);
|
2023-12-27 17:05:47 +00:00
|
|
|
if (!widgetContent) {
|
|
|
|
div.innerHTML = "";
|
2023-12-29 19:03:54 +00:00
|
|
|
this.client.setWidgetCache(
|
|
|
|
this.bodyText,
|
|
|
|
{ height: div.clientHeight, html: "" },
|
|
|
|
);
|
2023-12-27 17:05:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-12-27 12:38:38 +00:00
|
|
|
const lang = buildMarkdown(this.client.system.mdExtensions);
|
|
|
|
let mdTree = parse(
|
|
|
|
lang,
|
|
|
|
widgetContent.markdown!,
|
|
|
|
);
|
|
|
|
mdTree = await this.client.system.localSyscall(
|
|
|
|
"system.invokeFunction",
|
|
|
|
[
|
|
|
|
"markdown.expandCodeWidgets",
|
|
|
|
mdTree,
|
|
|
|
this.client.currentPage,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
// Used for the source button
|
|
|
|
this.renderedMarkdown = renderToText(mdTree);
|
|
|
|
|
|
|
|
const html = renderMarkdownToHtml(mdTree, {
|
|
|
|
// Annotate every element with its position so we can use it to put
|
|
|
|
// the cursor there when the user clicks on the table.
|
|
|
|
annotationPositions: true,
|
|
|
|
translateUrls: (url) => {
|
|
|
|
if (!url.includes("://")) {
|
|
|
|
url = resolveAttachmentPath(
|
|
|
|
this.client.currentPage!,
|
|
|
|
decodeURI(url),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
preserveAttributes: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (cachedHtml === html) {
|
|
|
|
// HTML still same as in cache, no need to re-render
|
|
|
|
return;
|
|
|
|
}
|
2023-12-28 15:14:30 +00:00
|
|
|
div.innerHTML = this.wrapHtml(html, widgetContent.buttons);
|
|
|
|
this.attachListeners(div, widgetContent.buttons);
|
2023-12-27 12:38:38 +00:00
|
|
|
|
|
|
|
// Let's give it a tick, then measure and cache
|
|
|
|
setTimeout(() => {
|
|
|
|
this.client.setWidgetCache(
|
|
|
|
this.bodyText,
|
2024-01-02 11:43:10 +00:00
|
|
|
{ height: div.offsetHeight, html, buttons: widgetContent.buttons },
|
2023-12-27 12:38:38 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-28 15:14:30 +00:00
|
|
|
private wrapHtml(html: string, buttons?: CodeWidgetButton[]) {
|
|
|
|
if (!buttons) {
|
|
|
|
return html;
|
2023-12-27 17:05:47 +00:00
|
|
|
}
|
2023-12-28 15:14:30 +00:00
|
|
|
return `<div class="button-bar">${
|
|
|
|
buttons.map((button, idx) =>
|
|
|
|
`<button data-button="${idx}" title="${button.description}">${button.svg}</button> `
|
|
|
|
).join("")
|
|
|
|
}</div>${html}`;
|
2023-12-27 12:38:38 +00:00
|
|
|
}
|
|
|
|
|
2023-12-28 15:14:30 +00:00
|
|
|
private attachListeners(div: HTMLElement, buttons?: CodeWidgetButton[]) {
|
2023-12-27 12:38:38 +00:00
|
|
|
div.querySelectorAll("a[data-ref]").forEach((el_) => {
|
|
|
|
const el = el_ as HTMLElement;
|
|
|
|
// Override default click behavior with a local navigate (faster)
|
|
|
|
el.addEventListener("click", (e) => {
|
|
|
|
e.preventDefault();
|
2023-12-27 17:05:47 +00:00
|
|
|
const [pageName, pos] = el.dataset.ref!.split(/[$@]/);
|
|
|
|
if (pos && pos.match(/^\d+$/)) {
|
|
|
|
this.client.navigate(pageName, +pos);
|
|
|
|
} else {
|
|
|
|
this.client.navigate(pageName, pos);
|
|
|
|
}
|
2023-12-27 12:38:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Implement task toggling
|
|
|
|
div.querySelectorAll("span[data-external-task-ref]").forEach((el: any) => {
|
|
|
|
const taskRef = el.dataset.externalTaskRef;
|
|
|
|
el.querySelector("input[type=checkbox]").addEventListener(
|
|
|
|
"change",
|
|
|
|
(e: any) => {
|
|
|
|
const oldState = e.target.dataset.state;
|
|
|
|
const newState = oldState === " " ? "x" : " ";
|
|
|
|
// Update state in DOM as well for future toggles
|
|
|
|
e.target.dataset.state = newState;
|
|
|
|
console.log("Toggling task", taskRef);
|
|
|
|
this.client.system.localSyscall(
|
|
|
|
"system.invokeFunction",
|
|
|
|
["tasks.updateTaskState", taskRef, oldState, newState],
|
|
|
|
).catch(
|
|
|
|
console.error,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-12-28 15:14:30 +00:00
|
|
|
if (!buttons) {
|
|
|
|
buttons = [];
|
2023-12-27 17:05:47 +00:00
|
|
|
}
|
2023-12-28 15:14:30 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < buttons.length; i++) {
|
|
|
|
const button = buttons[i];
|
|
|
|
div.querySelector(`button[data-button="${i}"]`)!.addEventListener(
|
|
|
|
"click",
|
|
|
|
() => {
|
2023-12-29 17:59:42 +00:00
|
|
|
console.log("Button clicked:", button.description);
|
2023-12-28 15:14:30 +00:00
|
|
|
this.client.system.localSyscall("system.invokeFunction", [
|
|
|
|
button.invokeFunction,
|
|
|
|
this.from,
|
|
|
|
]).then((newContent: string | undefined) => {
|
|
|
|
if (newContent) {
|
|
|
|
div.innerText = newContent;
|
|
|
|
}
|
|
|
|
this.client.focus();
|
|
|
|
}).catch(console.error);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// div.querySelectorAll("ul > li").forEach((el) => {
|
|
|
|
// el.classList.add("sb-line-li-1", "sb-line-ul");
|
|
|
|
// });
|
2023-12-27 12:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get estimatedHeight(): number {
|
|
|
|
const cacheItem = this.client.getWidgetCache(this.bodyText);
|
2024-01-02 11:48:18 +00:00
|
|
|
// console.log("Calling estimated height", this.bodyText, cacheItem);
|
2023-12-27 12:38:38 +00:00
|
|
|
return cacheItem ? cacheItem.height : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
eq(other: WidgetType): boolean {
|
|
|
|
return (
|
|
|
|
other instanceof MarkdownWidget &&
|
|
|
|
other.bodyText === this.bodyText
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-12-28 15:14:30 +00:00
|
|
|
|
|
|
|
export function reloadAllMarkdownWidgets() {
|
|
|
|
for (const widget of activeWidgets) {
|
|
|
|
// Garbage collect as we go
|
|
|
|
if (!widget.dom || !widget.dom.parentNode) {
|
|
|
|
activeWidgets.delete(widget);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
widget.renderContent(widget.dom!, undefined).catch(console.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function garbageCollectWidgets() {
|
|
|
|
for (const widget of activeWidgets) {
|
|
|
|
if (!widget.dom || !widget.dom.parentNode) {
|
|
|
|
// console.log("Garbage collecting widget", widget.bodyText);
|
|
|
|
activeWidgets.delete(widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setInterval(garbageCollectWidgets, 5000);
|