diff --git a/README.md b/README.md index 01a4814..3aa826b 100644 --- a/README.md +++ b/README.md @@ -309,7 +309,10 @@ Note: swap count is reset if you close the note. ### Next version (not released yet) + +- Bugfix: buttons now render in Live Preview mode when Obsidian starts ([Lx]) - Bugfix: improve reliability of `templater` option ([Lx]) +- improve speed of `remove` option ([Lx]) [Lx]: https://github.com/Lx diff --git a/src/button.ts b/src/button.ts index 841327c..465fde3 100644 --- a/src/button.ts +++ b/src/button.ts @@ -83,41 +83,35 @@ const clickHandler = async ( // handle template buttons if (args.type && args.type.includes("template")) { - setTimeout(async () => { - content = await app.vault.read(activeView.file); - position = inline - ? await getInlineButtonPosition(app, id) - : getButtonPosition(content, args); - template(app, args, position); - }, 50); + content = await app.vault.read(activeView.file); + position = inline + ? await getInlineButtonPosition(app, id) + : getButtonPosition(content, args); + await template(app, args, position); } if (args.type === "calculate") { - calculate(app, args, position); + await calculate(app, args, position); } if (args.type && args.type.includes("text")) { - setTimeout(async () => { - content = await app.vault.read(activeView.file); - position = inline - ? await getInlineButtonPosition(app, id) - : getButtonPosition(content, args); - text(app, args, position); - }, 50); - } - // handle removing the button - if (args.remove) { - setTimeout(async () => { - content = await app.vault.read(activeView.file); - position = inline - ? await getInlineButtonPosition(app, id) - : getButtonPosition(content, args); - remove(app, args, position); - }, 1000); + content = await app.vault.read(activeView.file); + position = inline + ? await getInlineButtonPosition(app, id) + : getButtonPosition(content, args); + await text(app, args, position); } if (args.swap) { if (!inline) { new Notice("swap args only work in inline buttons for now", 2000); } else { - swap(app, args.swap, id, inline, activeView.file); + await swap(app, args.swap, id, inline, activeView.file); } } + // handle removing the button + if (args.remove) { + content = await app.vault.read(activeView.file); + position = inline + ? await getInlineButtonPosition(app, id) + : getButtonPosition(content, args); + await remove(app, args, position); + } }; diff --git a/src/buttonTypes.ts b/src/buttonTypes.ts index 00ee90b..760b40c 100644 --- a/src/buttonTypes.ts +++ b/src/buttonTypes.ts @@ -54,16 +54,19 @@ export const calculate = async ( fun && appendContent(app, `Result: ${fun}`, position.lineEnd); }; -export const remove = ( +export const remove = async ( app: App, { remove }: Arguments, { lineStart, lineEnd }: { lineStart: number; lineEnd: number } -): void => { - setTimeout(() => removeButton(app, remove, lineStart, lineEnd), 1000); +): Promise => { + await removeButton(app, remove, lineStart, lineEnd); }; -export const replace = (app: App, { replace }: Arguments): void => { - removeSection(app, replace); +export const replace = async ( + app: App, + { replace }: Arguments +): Promise => { + await removeSection(app, replace); }; export const text = async ( app: App, @@ -72,17 +75,17 @@ export const text = async ( ): Promise => { // prepend template above the button if (args.type.includes("prepend")) { - prependContent(app, args.action, position.lineStart); + await prependContent(app, args.action, position.lineStart); } // append template below the button if (args.type.includes("append")) { - appendContent(app, args.action, position.lineEnd); + await appendContent(app, args.action, position.lineEnd); } if (args.type.includes("note")) { - createNote(app, args.action, args.type); + await createNote(app, args.action, args.type); } if (args.type.includes("line")) { - addContentAtLine(app, args.action, args.type); + await addContentAtLine(app, args.action, args.type); } }; @@ -122,38 +125,20 @@ export const template = async ( const content = await app.vault.read(file); // prepend template above the button if (args.type.includes("prepend")) { - prependContent(app, content, position.lineStart); - setTimeout( - () => - app.commands.executeCommandById( - "templater-obsidian:replace-in-file-templater" - ), - 100 - ); + await prependContent(app, content, position.lineStart); + await runTemplater(app); } // append template below the button if (args.type.includes("append")) { - appendContent(app, content, position.lineEnd); - setTimeout( - () => - app.commands.executeCommandById( - "templater-obsidian:replace-in-file-templater" - ), - 100 - ); + await appendContent(app, content, position.lineEnd); + await runTemplater(app); } if (args.type.includes("note")) { - createNote(app, content, args.type); + await createNote(app, content, args.type); } if (args.type.includes("line")) { - addContentAtLine(app, content, args.type); - setTimeout( - () => - app.commands.executeCommandById( - "templater-obsidian:replace-in-file-templater" - ), - 100 - ); + await addContentAtLine(app, content, args.type); + await runTemplater(app); } } else { new Notice( @@ -204,7 +189,7 @@ export const swap = async ( } } if (args.replace) { - replace(app, args); + await replace(app, args); } if (args.type === "command") { command(app, args); @@ -215,38 +200,32 @@ export const swap = async ( } // handle template buttons if (args.type && args.type.includes("template")) { - setTimeout(async () => { - content = await app.vault.read(file); - position = inline - ? await getInlineButtonPosition(app, id) - : getButtonPosition(content, args); - template(app, args, position); - }, 50); + content = await app.vault.read(file); + position = inline + ? await getInlineButtonPosition(app, id) + : getButtonPosition(content, args); + await template(app, args, position); } if (args.type === "calculate") { - calculate(app, args, position); + await calculate(app, args, position); } if (args.type && args.type.includes("text")) { - setTimeout(async () => { - content = await app.vault.read(file); - position = inline - ? await getInlineButtonPosition(app, id) - : getButtonPosition(content, args); - text(app, args, position); - }, 50); + content = await app.vault.read(file); + position = inline + ? await getInlineButtonPosition(app, id) + : getButtonPosition(content, args); + await text(app, args, position); } // handle removing the button if (args.remove) { - setTimeout(async () => { - content = await app.vault.read(file); - position = inline - ? await getInlineButtonPosition(app, id) - : getButtonPosition(content, args); - remove(app, args, position); - }, 75); + content = await app.vault.read(file); + position = inline + ? await getInlineButtonPosition(app, id) + : getButtonPosition(content, args); + await remove(app, args, position); } if (args.replace) { - replace(app, args); + await replace(app, args); } } });