diff --git a/common/space_lua/runtime.ts b/common/space_lua/runtime.ts index ec0261fe..4251074b 100644 --- a/common/space_lua/runtime.ts +++ b/common/space_lua/runtime.ts @@ -376,7 +376,14 @@ export function luaGet(obj: any, key: any, ctx: ASTCtx): any { } else if (typeof key === "number") { return obj[key - 1]; } else { - return obj[key]; + // Native JS object + const val = obj[key]; + if (typeof val === "function") { + // Automatically bind the function to the object + return val.bind(obj); + } else { + return val; + } } } diff --git a/common/space_lua/stdlib/js.ts b/common/space_lua/stdlib/js.ts index 748fe4f3..b26d2109 100644 --- a/common/space_lua/stdlib/js.ts +++ b/common/space_lua/stdlib/js.ts @@ -16,12 +16,7 @@ export const jsApi = new LuaTable({ importModule: new LuaBuiltinFunction((url) => { return import(url); }), - /** - * Binds a function to an object, so that the function can be called with the object as `this`. Some JS APIs require this. - */ - bind: new LuaBuiltinFunction((fn: any, obj: any, ...args: any[]) => { - return fn.bind(obj, ...args); - }), + tolua: new LuaBuiltinFunction(jsToLuaValue), tojs: new LuaBuiltinFunction(luaValueToJS), log: new LuaBuiltinFunction((...args) => { diff --git a/common/space_lua_api.ts b/common/space_lua_api.ts index 14cae352..8af8e986 100644 --- a/common/space_lua_api.ts +++ b/common/space_lua_api.ts @@ -13,6 +13,15 @@ import type { ScriptEnvironment } from "$common/space_script.ts"; export function buildLuaEnv(system: System, scriptEnv: ScriptEnvironment) { const env = new LuaEnv(luaBuildStandardEnv()); + // Expose all syscalls to Lua + exposeSyscalls(env, system); + // Support defining commands and subscriptions from Lua + exposeDefinitions(env, system, scriptEnv); + + return env; +} + +function exposeSyscalls(env: LuaEnv, system: System) { // Expose all syscalls to Lua for (const syscallName of system.registeredSyscalls.keys()) { const [ns, fn] = syscallName.split("."); @@ -26,9 +35,17 @@ export function buildLuaEnv(system: System, scriptEnv: ScriptEnvironment) { env.get(ns).set(fn, luaFn); env.get(ns).set(snakeCase(fn), luaFn); } +} - // Expose the command registration function to Lua - env.set( +function exposeDefinitions( + env: LuaEnv, + system: System, + scriptEnv: ScriptEnvironment, +) { + const defApi = new LuaTable(); + env.set("def", defApi); + // Expose the command registration function to Lua via def.command({name="foo", function() ... end}) + defApi.set( "command", new LuaBuiltinFunction( (def: LuaTable) => { @@ -47,43 +64,70 @@ export function buildLuaEnv(system: System, scriptEnv: ScriptEnvironment) { ...args.map(jsToLuaValue), ); } catch (e: any) { - console.error( - "Lua eval exception", - e.message, - e.context, - ); - if (e.context && e.context.ref) { - // We got an error and actually know where it came from, let's navigate there to help debugging - const pageRef = parsePageRef(e.context.ref); - await system.localSyscall( - "editor.flashNotification", - [ - `Lua error: ${e.message}`, - "error", - ], - ); - await system.localSyscall( - "editor.flashNotification", - [ - `Navigating to the place in the code where this error occurred in ${pageRef.page}`, - "info", - ], - ); - await system.localSyscall("editor.navigate", [ - { - page: pageRef.page, - pos: pageRef.pos + e.context.from + - "```space-lua\n".length, - }, - ]); - } + await handleLuaError(e, system); } }, ); }, ), ); - return env; + defApi.set( + "event_listener", + new LuaBuiltinFunction((def: LuaTable) => { + if (def.get(1) === undefined) { + throw new Error("Callback is required"); + } + if (!def.get("event")) { + throw new Error("Event is required"); + } + console.log("Subscribing to Lua event", def.get("event")); + scriptEnv.registerEventListener( + { name: def.get("event") }, + async (...args: any[]) => { + try { + return await def.get(1).call( + ...args.map(jsToLuaValue), + ); + } catch (e: any) { + await handleLuaError(e, system); + } + }, + ); + }), + ); +} + +async function handleLuaError(e: any, system: System) { + console.error( + "Lua eval exception", + e.message, + e.context, + ); + if (e.context && e.context.ref) { + // We got an error and actually know where it came from, let's navigate there to help debugging + const pageRef = parsePageRef(e.context.ref); + await system.localSyscall( + "editor.flashNotification", + [ + `Lua error: ${e.message}`, + "error", + ], + ); + await system.localSyscall( + "editor.flashNotification", + [ + `Navigating to the place in the code where this error occurred in ${pageRef.page}`, + "info", + ], + ); + await system.localSyscall("editor.navigate", [ + { + page: pageRef.page, + pos: pageRef.pos + e.context.from + + "```space-lua\n".length, + }, + ]); + } } function snakeCase(s: string) { diff --git a/web/cm_plugins/lua_directive.ts b/web/cm_plugins/lua_directive.ts index 0521f3d9..e41b499c 100644 --- a/web/cm_plugins/lua_directive.ts +++ b/web/cm_plugins/lua_directive.ts @@ -14,10 +14,11 @@ import type { LuaFunctionCallStatement, } from "$common/space_lua/ast.ts"; import { evalExpression } from "$common/space_lua/eval.ts"; -import { MarkdownWidget } from "./markdown_widget.ts"; +import { luaValueToJS } from "$common/space_lua/runtime.ts"; import { LuaRuntimeError } from "$common/space_lua/runtime.ts"; import { encodePageRef } from "@silverbulletmd/silverbullet/lib/page_ref"; import { resolveASTReference } from "$common/space_lua.ts"; +import { LuaWidget } from "./lua_widget.ts"; export function luaDirectivePlugin(client: Client) { return decoratorStateField((state: EditorState) => { @@ -41,7 +42,7 @@ export function luaDirectivePlugin(client: Client) { widgets.push( Decoration.widget({ - widget: new MarkdownWidget( + widget: new LuaWidget( node.from, client, `lua:${text}`, @@ -53,13 +54,12 @@ export function luaDirectivePlugin(client: Client) { (parsedLua.statements[0] as LuaFunctionCallStatement).call .args[0]; - const result = await evalExpression( - expr, - client.clientSystem.spaceLuaEnv.env, + return luaValueToJS( + await evalExpression( + expr, + client.clientSystem.spaceLuaEnv.env, + ), ); - return { - markdown: "" + result, - }; } catch (e: any) { if (e instanceof LuaRuntimeError) { if (e.context.ref) { @@ -79,8 +79,6 @@ export function luaDirectivePlugin(client: Client) { }; } }, - "sb-lua-directive", - true, ), }).range(node.to), ); diff --git a/web/cm_plugins/lua_widget.ts b/web/cm_plugins/lua_widget.ts new file mode 100644 index 00000000..629e8640 --- /dev/null +++ b/web/cm_plugins/lua_widget.ts @@ -0,0 +1,188 @@ +import { WidgetType } from "@codemirror/view"; +import type { Client } from "../client.ts"; +import type { ObjectValue } from "../../plug-api/types.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 { activeWidgets } from "./markdown_widget.ts"; +import { attachWidgetEventHandlers } from "./widget_util.ts"; + +export type LuaWidgetCallback = ( + bodyText: string, + pageName: string, +) => Promise; + +export type LuaWidgetContent = { + // Render as HTML + html?: string; + // Render as markdown + markdown?: string; + // CSS classes for wrapper + cssClasses?: string[]; + // Index objects + objects?: ObjectValue[]; + display?: "block" | "inline"; +} | string; + +export class LuaWidget extends WidgetType { + public dom?: HTMLElement; + + constructor( + readonly from: number | undefined, + readonly client: Client, + readonly cacheKey: string, + readonly bodyText: string, + readonly callback: LuaWidgetCallback, + ) { + super(); + } + + toDOM(): HTMLElement { + const div = document.createElement("div"); + div.className = "sb-lua-directive"; + const cacheItem = this.client.getWidgetCache(this.cacheKey); + if (cacheItem) { + div.innerHTML = cacheItem.html; + if (cacheItem.html) { + attachWidgetEventHandlers(div, this.client); + } + } + + // Async kick-off of content renderer + this.renderContent(div, cacheItem?.html).catch(console.error); + this.dom = div; + return div; + } + + async renderContent( + div: HTMLElement, + cachedHtml: string | undefined, + ) { + let widgetContent = await this.callback( + this.bodyText, + this.client.currentPage, + ); + activeWidgets.add(this); + if (!widgetContent) { + div.innerHTML = ""; + this.client.setWidgetCache( + this.cacheKey, + { height: div.clientHeight, html: "" }, + ); + return; + } + let html = ""; + if (typeof widgetContent !== "object") { + // Return as markdown string or number + widgetContent = { markdown: "" + widgetContent }; + } + if (widgetContent.cssClasses) { + div.className = widgetContent.cssClasses.join(" "); + } + if (widgetContent.html) { + html = widgetContent.html; + div.innerHTML = html; + attachWidgetEventHandlers(div, this.client); + this.client.setWidgetCache( + this.cacheKey, + { height: div.clientHeight, html }, + ); + } else if (widgetContent.markdown) { + let mdTree = parse( + extendedMarkdownLanguage, + widgetContent.markdown!, + ); + mdTree = await this.client.clientSystem.localSyscall( + "system.invokeFunction", + [ + "markdown.expandCodeWidgets", + mdTree, + this.client.currentPage, + ], + ); + const trimmedMarkdown = renderToText(mdTree).trim(); + + if (!trimmedMarkdown) { + // Net empty result after expansion + div.innerHTML = ""; + this.client.setWidgetCache( + this.cacheKey, + { height: div.clientHeight, html: "" }, + ); + return; + } + + if ((widgetContent as any)?.display === "inline") { + div.style.display = "inline"; + } else { + div.style.display = "block"; + } + + // 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) { + // HTML still same as in cache, no need to re-render + return; + } + div.innerHTML = html; + if (html) { + attachWidgetEventHandlers(div, this.client); + } + } + + // Let's give it a tick, then measure and cache + setTimeout(() => { + this.client.setWidgetCache( + this.cacheKey, + { + height: div.offsetHeight, + html, + }, + ); + // 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, + }, + }); + }); + } + + 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 + ); + } +} diff --git a/web/cm_plugins/markdown_widget.ts b/web/cm_plugins/markdown_widget.ts index fa115b55..52cdb351 100644 --- a/web/cm_plugins/markdown_widget.ts +++ b/web/cm_plugins/markdown_widget.ts @@ -10,12 +10,19 @@ import { resolvePath, } from "@silverbulletmd/silverbullet/lib/resolve"; import { parse } from "$common/markdown_parser/parse_tree.ts"; -import { parsePageRef } from "../../plug-api/lib/page_ref.ts"; import { extendedMarkdownLanguage } from "$common/markdown_parser/parser.ts"; -import { tagPrefix } from "../../plugs/index/constants.ts"; import { renderToText } from "@silverbulletmd/silverbullet/lib/tree"; +import { attachWidgetEventHandlers } from "./widget_util.ts"; -const activeWidgets = new Set(); +export const activeWidgets = new Set(); + +export interface DomWidget { + dom?: HTMLElement; + renderContent( + div: HTMLElement, + cachedHtml: string | undefined, + ): Promise; +} export class MarkdownWidget extends WidgetType { public dom?: HTMLElement; @@ -174,92 +181,7 @@ export class MarkdownWidget extends WidgetType { } private attachListeners(div: HTMLElement, buttons?: CodeWidgetButton[]) { - div.addEventListener("mousedown", (e) => { - // CodeMirror overrides mousedown on parent elements to implement its own selection highlighting. - // That's nice, but not for markdown widgets, so let's not propagate the event to CodeMirror here. - e.stopPropagation(); - }); - // 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) - el.addEventListener("click", (e) => { - if (e.ctrlKey || e.metaKey) { - // Don't do anything special for ctrl/meta clicks - return; - } - e.preventDefault(); - e.stopPropagation(); - const pageRef = parsePageRef(el.dataset.ref!); - this.client.navigate(pageRef, false, e.ctrlKey || e.metaKey); - }); - }); - - // Attach click handlers to hash tags - div.querySelectorAll("span.hashtag").forEach((el_) => { - const el = el_ as HTMLElement; - // Override default click behavior with a local navigate (faster) - el.addEventListener("click", (e) => { - if (e.ctrlKey || e.metaKey) { - // Don't do anything special for ctrl/meta clicks - return; - } - this.client.navigate({ - page: `${tagPrefix}${el.innerText.slice(1)}`, - pos: 0, - }); - }); - }); - - 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", - parsedOnclick, - ); - this.client.runCommandByName(command, parsedOnclick[2]).catch( - console.error, - ); - }); - } - }); - - // Implement task toggling - div.querySelectorAll("span[data-external-task-ref]").forEach((el: any) => { - const taskRef = el.dataset.externalTaskRef; - const input = el.querySelector("input[type=checkbox]")!; - input.addEventListener( - "click", - (e: any) => { - // Avoid triggering the click on the parent - e.stopPropagation(); - }, - ); - input.addEventListener( - "change", - (e: any) => { - e.stopPropagation(); - 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.clientSystem.localSyscall( - "system.invokeFunction", - ["tasks.updateTaskState", taskRef, oldState, newState], - ).catch( - console.error, - ); - }, - ); - }); + attachWidgetEventHandlers(div, this.client); if (!buttons) { buttons = []; @@ -308,7 +230,7 @@ export class MarkdownWidget extends WidgetType { } } -export function reloadAllMarkdownWidgets() { +export function reloadAllWidgets() { for (const widget of activeWidgets) { // Garbage collect as we go if (!widget.dom || !widget.dom.parentNode) { diff --git a/web/cm_plugins/widget_util.ts b/web/cm_plugins/widget_util.ts new file mode 100644 index 00000000..1272c324 --- /dev/null +++ b/web/cm_plugins/widget_util.ts @@ -0,0 +1,92 @@ +import { parsePageRef } from "@silverbulletmd/silverbullet/lib/page_ref"; +import type { Client } from "../client.ts"; +import { tagPrefix } from "../../plugs/index/constants.ts"; + +export function attachWidgetEventHandlers(div: HTMLElement, client: Client) { + div.addEventListener("mousedown", (e) => { + // CodeMirror overrides mousedown on parent elements to implement its own selection highlighting. + // That's nice, but not for markdown widgets, so let's not propagate the event to CodeMirror here. + e.stopPropagation(); + }); + // 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) + el.addEventListener("click", (e) => { + if (e.ctrlKey || e.metaKey) { + // Don't do anything special for ctrl/meta clicks + return; + } + e.preventDefault(); + e.stopPropagation(); + const pageRef = parsePageRef(el.dataset.ref!); + client.navigate(pageRef, false, e.ctrlKey || e.metaKey); + }); + }); + + // Attach click handlers to hash tags + div.querySelectorAll("span.hashtag").forEach((el_) => { + const el = el_ as HTMLElement; + // Override default click behavior with a local navigate (faster) + el.addEventListener("click", (e) => { + if (e.ctrlKey || e.metaKey) { + // Don't do anything special for ctrl/meta clicks + return; + } + client.navigate({ + page: `${tagPrefix}${el.innerText.slice(1)}`, + pos: 0, + }); + }); + }); + + 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", + parsedOnclick, + ); + client.runCommandByName(command, parsedOnclick[2]).catch( + console.error, + ); + }); + } + }); + + // Implement task toggling + div.querySelectorAll("span[data-external-task-ref]").forEach((el: any) => { + const taskRef = el.dataset.externalTaskRef; + const input = el.querySelector("input[type=checkbox]")!; + input.addEventListener( + "click", + (e: any) => { + // Avoid triggering the click on the parent + e.stopPropagation(); + }, + ); + input.addEventListener( + "change", + (e: any) => { + e.stopPropagation(); + 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); + client.clientSystem.localSyscall( + "system.invokeFunction", + ["tasks.updateTaskState", taskRef, oldState, newState], + ).catch( + console.error, + ); + }, + ); + }); +} diff --git a/web/syscalls/client_code_widget.ts b/web/syscalls/client_code_widget.ts index fd3e42d3..d3f95ded 100644 --- a/web/syscalls/client_code_widget.ts +++ b/web/syscalls/client_code_widget.ts @@ -1,12 +1,12 @@ import type { SysCallMapping } from "../../lib/plugos/system.ts"; -import { reloadAllMarkdownWidgets } from "../cm_plugins/markdown_widget.ts"; +import { reloadAllWidgets } from "../cm_plugins/markdown_widget.ts"; import { broadcastReload } from "../components/widget_sandbox_iframe.ts"; export function clientCodeWidgetSyscalls(): SysCallMapping { return { "codeWidget.refreshAll": () => { broadcastReload(); - reloadAllMarkdownWidgets(); + reloadAllWidgets(); }, }; }