import { WidgetType } from "@codemirror/view"; import type { Client } from "../client.ts"; import { renderMarkdownToHtml } from "../../plugs/markdown/markdown_render.ts"; import { isLocalPath, resolvePath, } from "@silverbulletmd/silverbullet/lib/resolve"; import { parse } from "$common/markdown_parser/parse_tree.ts"; import { extendedMarkdownLanguage } from "$common/markdown_parser/parser.ts"; import { renderToText } from "@silverbulletmd/silverbullet/lib/tree"; import { attachWidgetEventHandlers, moveCursorIntoText, } from "./widget_util.ts"; import { expandMarkdown } from "$common/markdown.ts"; import { LuaStackFrame, LuaTable } from "$common/space_lua/runtime.ts"; import { jsonToMDTable } from "$common/markdown_util.ts"; import { activeWidgets } from "./code_widget.ts"; export type LuaWidgetCallback = ( bodyText: string, pageName: string, ) => Promise; export type EventPayLoad = { name: string; data: any; }; export type LuaWidgetContent = { // Magic marker _isWidget?: true; // Render as HTML html?: string; // Render as markdown markdown?: string; // CSS classes for wrapper cssClasses?: string[]; display?: "block" | "inline"; // Event handlers events?: Record void>; } | string; export class LuaWidget extends WidgetType { public dom?: HTMLElement; constructor( readonly client: Client, readonly cacheKey: string, readonly bodyText: string, readonly callback: LuaWidgetCallback, private renderEmpty: boolean, readonly inPage: boolean, ) { super(); } toDOM(): HTMLElement { const wrapperSpan = document.createElement("span"); wrapperSpan.className = "sb-lua-wrapper"; const innerDiv = document.createElement("div"); wrapperSpan.appendChild(innerDiv); const cacheItem = this.client.getWidgetCache(this.cacheKey); if (cacheItem) { innerDiv.innerHTML = this.wrapHtml(!!cacheItem.block, cacheItem.html); } // Async kick-off of content renderer this.renderContent(innerDiv, cacheItem?.html).catch(console.error); this.dom = wrapperSpan; return wrapperSpan; } async renderContent( div: HTMLElement, cachedHtml: string | undefined, ) { let widgetContent = await this.callback( this.bodyText, this.client.currentPage, ); activeWidgets.add(this); if (widgetContent === null || widgetContent === undefined) { if (!this.renderEmpty) { div.innerHTML = ""; this.client.setWidgetCache( this.cacheKey, { height: div.clientHeight, html: "", block: false }, ); return; } widgetContent = { markdown: "nil", _isWidget: true }; } let html = ""; let block = false; // Normalization if (typeof widgetContent === "string" || !widgetContent._isWidget) { // Apply heuristic to render the object as a markdown table widgetContent = { _isWidget: true, markdown: await renderExpressionResult(widgetContent), }; } if (widgetContent.cssClasses) { div.className = widgetContent.cssClasses.join(" "); } if (widgetContent.html) { html = widgetContent.html; block = widgetContent.display === "block"; if (block) { div.className += " sb-lua-directive-block"; } else { div.className += " sb-lua-directive-inline"; } } if (widgetContent.markdown) { let mdTree = parse( extendedMarkdownLanguage, widgetContent.markdown || "", ); const sf = LuaStackFrame.createWithGlobalEnv( client.clientSystem.spaceLuaEnv.env, ); mdTree = await expandMarkdown( client, mdTree, client.clientSystem.spaceLuaEnv.env, sf, ); const trimmedMarkdown = renderToText(mdTree).trim(); if (!trimmedMarkdown) { // Net empty result after expansion div.innerHTML = ""; this.client.setWidgetCache( this.cacheKey, { height: div.clientHeight, html: "", block: false }, ); return; } block = widgetContent._isWidget && widgetContent.display === "block" || trimmedMarkdown.includes("\n"); if (block) { div.className += " sb-lua-directive-block"; } else { div.className += " sb-lua-directive-inline"; } // Parse the markdown again after trimming mdTree = parse( extendedMarkdownLanguage, trimmedMarkdown, ); 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 (isLocalPath(url)) { url = resolvePath( this.client.currentPage, decodeURI(url), ); } return url; }, preserveAttributes: true, }, this.client.ui.viewState.allPages); } if (cachedHtml !== html) { // If the content has changed, update the DOM div.innerHTML = this.wrapHtml(block, html); } if (html) { attachWidgetEventHandlers( div, this.client, this.inPage ? "${" + this.bodyText + "}" : undefined, widgetContent._isWidget && widgetContent.events, ); this.attachHandlers(div); } // Let's give it a tick, then measure and cache setTimeout(() => { this.client.setWidgetCache( this.cacheKey, { height: div.offsetHeight, html, block, }, ); // Because of the rejiggering of the DOM, we need to do a no-op cursor move to make sure it's positioned correctly this.client.editorView.dispatch({ selection: { anchor: this.client.editorView.state.selection.main.anchor, }, }); }); } wrapHtml(isBlock: boolean, html: string): string { if (!isBlock) { return html; } return `
${ this.inPage ? ` ` : "" }
${html}
`; } attachHandlers(div: HTMLElement) { div.querySelector(`button[data-button="reload"]`)?.addEventListener( "click", (e) => { e.stopPropagation(); this.client.clientSystem.localSyscall( "system.invokeFunction", ["index.refreshWidgets"], ).catch(console.error); }, ); div.querySelector(`button[data-button="edit"]`)?.addEventListener( "click", (e) => { e.stopPropagation(); moveCursorIntoText(this.client, "${" + this.bodyText + "}"); }, ); } override get estimatedHeight(): number { const cacheItem = this.client.getWidgetCache(this.cacheKey); return cacheItem ? cacheItem.height : -1; } override eq(other: WidgetType): boolean { return ( other instanceof LuaWidget && other.bodyText === this.bodyText && other.cacheKey === this.cacheKey // && this.from === other.from ); } } export function renderExpressionResult(result: any): Promise { if (result instanceof LuaTable) { result = result.toJS(); } if ( Array.isArray(result) && result.length > 0 && typeof result[0] === "object" ) { // If result is an array of objects, render as a markdown table try { return jsonToMDTable(result); } catch (e: any) { console.error( `Error rendering expression directive: ${e.message} for value ${ JSON.stringify(result) }`, ); return Promise.resolve(JSON.stringify(result)); } } else if (typeof result === "object" && result.constructor === Object) { // if result is a plain object, render as a markdown table return jsonToMDTable([result]); } else if (Array.isArray(result)) { // Not-object array, let's render it as a markdown list return Promise.resolve(result.map((item) => `- ${item}`).join("\n")); } else { return Promise.resolve("" + result); } }