Fix Edit button on transclusion widgets
This commit is contained in:
@@ -57,6 +57,7 @@ export function inlineContentPlugin(client: Client) {
|
||||
client,
|
||||
`widget:${client.currentPath()}:${text}`,
|
||||
text,
|
||||
text,
|
||||
async () => {
|
||||
const result = await inlineContentFromURL(
|
||||
client.space,
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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 {
|
||||
'<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-edit"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"></path><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"></path></svg>',
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,7 @@ class ArrayWidget extends WidgetType {
|
||||
this.client,
|
||||
`${this.cacheKey}:${i}`,
|
||||
"",
|
||||
"",
|
||||
() => Promise.resolve(widgetContent),
|
||||
false,
|
||||
false,
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user