Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster remove #118

Merged
merged 10 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 20 additions & 26 deletions src/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
95 changes: 37 additions & 58 deletions src/buttonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
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<void> => {
await removeSection(app, replace);
};
export const text = async (
app: App,
Expand All @@ -72,17 +75,17 @@ export const text = async (
): Promise<void> => {
// 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);
}
};

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
});
Expand Down