diff --git a/client/codemirror/inline_content.ts b/client/codemirror/inline_content.ts index 5a491877..2b70d79e 100644 --- a/client/codemirror/inline_content.ts +++ b/client/codemirror/inline_content.ts @@ -57,6 +57,7 @@ export function inlineContentPlugin(client: Client) { client, `widget:${client.currentPath()}:${text}`, text, + text, async () => { const result = await inlineContentFromURL( client.space, diff --git a/client/codemirror/lua_directive.ts b/client/codemirror/lua_directive.ts index 825045b5..761a8f82 100644 --- a/client/codemirror/lua_directive.ts +++ b/client/codemirror/lua_directive.ts @@ -71,14 +71,16 @@ export function luaDirectivePlugin(client: Client) { return; } - const text = state.sliceDoc(node.from + 2, node.to - 1); + const codeText = state.sliceDoc(node.from, node.to); + const expressionText = codeText.slice(2, -1); const currentPageMeta = client.ui.viewState.current?.meta as PageMeta; widgets.push( Decoration.widget({ widget: new LuaWidget( client, - `lua:${text}:${currentPageMeta?.name}`, - text, + `lua:${expressionText}:${currentPageMeta?.name}`, + expressionText, + codeText, async (bodyText) => { if (bodyText.trim().length === 0) { return "**Error:** Empty Lua expression"; diff --git a/client/codemirror/lua_widget.ts b/client/codemirror/lua_widget.ts index 83738f54..50e5eb68 100644 --- a/client/codemirror/lua_widget.ts +++ b/client/codemirror/lua_widget.ts @@ -50,8 +50,12 @@ export class LuaWidget extends WidgetType { constructor( readonly client: Client, + // key to use for caching readonly cacheKey: string, - readonly bodyText: string, + // body text to send to widget renderer + readonly expressionText: string, + // code as it appears in the page (used to find when hitting the "edit" button) + readonly codeText: string, readonly callback: LuaWidgetCallback, private renderEmpty: boolean, readonly inPage: boolean, @@ -92,7 +96,7 @@ export class LuaWidget extends WidgetType { ) { const currentName = this.client.currentName(); let widgetContent = await this.callback( - this.bodyText, + this.expressionText, currentName, ); activeWidgets.add(this); @@ -201,7 +205,7 @@ export class LuaWidget extends WidgetType { attachWidgetEventHandlers( div, this.client, - this.inPage ? "${" + this.bodyText + "}" : undefined, + this.inPage ? this.codeText : undefined, widgetContent._isWidget && widgetContent.events, ); } @@ -295,7 +299,7 @@ export class LuaWidget extends WidgetType { '', listener: (e) => { e.stopPropagation(); - moveCursorIntoText(this.client, "${" + this.bodyText + "}"); + moveCursorIntoText(this.client, this.codeText); }, }, )); @@ -314,7 +318,8 @@ export class LuaWidget extends WidgetType { override eq(other: WidgetType): boolean { return ( other instanceof LuaWidget && - other.bodyText === this.bodyText && other.cacheKey === this.cacheKey + other.expressionText === this.expressionText && + other.cacheKey === this.cacheKey ); } } diff --git a/client/codemirror/top_bottom_panels.ts b/client/codemirror/top_bottom_panels.ts index 2a611e2f..e7658532 100644 --- a/client/codemirror/top_bottom_panels.ts +++ b/client/codemirror/top_bottom_panels.ts @@ -64,6 +64,7 @@ class ArrayWidget extends WidgetType { this.client, `${this.cacheKey}:${i}`, "", + "", () => Promise.resolve(widgetContent), false, false, diff --git a/client/codemirror/widget_util.ts b/client/codemirror/widget_util.ts index f61ac40c..d8358ea0 100644 --- a/client/codemirror/widget_util.ts +++ b/client/codemirror/widget_util.ts @@ -6,7 +6,7 @@ export function moveCursorIntoText(client: Client, textToFind: string) { const allText = client.editorView.state.sliceDoc(); const pos = allText.indexOf(textToFind); if (pos === -1) { - console.error("Could not find position of widget in text"); + console.error("Could not find position of widget in text", textToFind); return; } client.editorView.dispatch({