Skip to content

Commit

Permalink
Add Prepend and Append logic to Command buttons (#169)
Browse files Browse the repository at this point in the history
* Add buttonPosition to permit prepend and append

Add the buttonStart variable and pass it to the command.

* Add logic to command buttons to permit prepend and append

Added buttonStart to the command

Added prepend logic to set cursor right before the button. This permits any output from the command to be prepended to the button.

Added append logic to set cursor right after the button (and after the button block id). This permits any output from the command to be appended to the button.Added buttonStart to the command

Added prepend logic to set cursor right before the button. This permits any output from the command to be prepended to the button.

Added append logic to set cursor right after the button (and after the button block id). This permits any output from the command to be appended to the button.

* Update modal to accept options for command

Adding a Default, a Prepend, and an Append option when creating a command button. Also storing that value.

---------

Co-authored-by: Sam <sam@sbgood.ca>
  • Loading branch information
pangolinsec and shabegom authored Jan 31, 2024
1 parent 9f607c0 commit e439bb9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const clickHandler = async (
let position = inline
? await getInlineButtonPosition(app, id)
: getButtonPosition(content, args);
const buttonStart = getButtonPosition(content,args);
// handle command buttons
if (args.templater) {
args = await templater(app, position);
Expand All @@ -81,6 +82,13 @@ const clickHandler = async (
replace(app, args);
}

if (args.type && args.type.includes("command")) {
command(app, args, buttonStart);
}
// handle link buttons
if (args.type === "link") {
link(args);
}
// handle template buttons
if (args.type && args.type.includes("template")) {
content = await app.vault.read(activeView.file);
Expand Down
15 changes: 13 additions & 2 deletions src/buttonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,23 @@ export const link = ({ action }: Arguments): void => {
window.open(link);
};

export const command = (app: App, { action }: Arguments): void => {
export const command = (app: App, args: Arguments, buttonStart): void => {
const allCommands = app.commands.listCommands();
const action = args.action;
const command = allCommands.filter(
(command) => command.name.toUpperCase() === action.toUpperCase().trim()
)[0];
app.commands.executeCommandById(command.id);
if (args.type.includes("prepend")) {
app.workspace.getActiveViewOfType(MarkdownView).editor.setCursor(buttonStart.lineStart,0);
app.commands.executeCommandById(command.id);
}
if (args.type.includes("append")) {
app.workspace.getActiveViewOfType(MarkdownView).editor.setCursor(buttonStart.lineEnd+2,0);
app.commands.executeCommandById(command.id);
}
if (args.type === "command") {
app.commands.executeCommandById(command.id);
}
};

export const swap = async (
Expand Down
8 changes: 8 additions & 0 deletions src/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ export class ButtonModal extends Modal {
new Setting(action)
.setName("Command")
.setDesc("Enter a command to run")
.addDropdown((drop) => {
drop.addOption("command", "Default");
drop.addOption("prepend command", "Prepend");
drop.addOption("apped command", "Append");
drop.onChange((value) => {
this.outputObject.type = value;
})
})
.addText((textEl) => {
textEl.inputEl.replaceWith(this.commandSuggestEl);
});
Expand Down

0 comments on commit e439bb9

Please sign in to comment.