shabegom / buttons

Buttons in Obsidian
The Unlicense
455 stars 47 forks source link

Multiple buttons on a page run all buttons actions #167

Closed ZobaJakColbert closed 5 months ago

ZobaJakColbert commented 1 year ago

Buttons v0.4.19 Obsidian v1.1.16

I put two buttons in one note. Both fire the templater user script, but with different strings. When I run either of them I see in the console that they both run.

First button

```button
name Dodaj akumulator AA
type note(Akumulatory/AA/<% tp.user.getThisAcumulatorNum(tp, "Button 1") %>, split) template
action Szablon - Akumulator
templater true

^button-NowyAkumulatorPaluszek


## Second button
name Dodaj akumulator AAA
type note(Akumulatory/AAA/<% tp.user.getThisAcumulatorNum(tp, "Button 2") %>, split) template
action Szablon - Akumulator
templater true

^button-NowyAAAAkumulator



In console I see that pressing only one button run both of them.

![2023 03 08 12_46_36-Stan_akumulatorów_Eneloop_AA_i_AAA_-_Moje_Dokument — Obsidian](https://user-images.githubusercontent.com/57141103/223706478-a42799bc-aef6-4807-adf1-8e8f14f1f14b.png)
blbaker commented 1 year ago

I had this issue too so I dived into the source code. The reason this happens is when you have templater true, it runs templater-obsidian:replace-in-file-templater on the entire file. This executes all of your templater commands. I found no way to make it work and ended up refactoring how I was creating templates and notes.

brennanmuir commented 1 year ago

Happening to me too

blbaker commented 1 year ago

I recently discovered another work around for this issue. You can programmatically create Buttons in Dataview blocks. By doing this, you bypass using Templater markup, and instead write Javascript. When you generate a note from a template, button code isn't execute so you don't have this issue happen.

Example. Instead of this:

    ```button
    name Dodaj akumulator AA
    type note(Akumulatory/AA/<% tp.user.getThisAcumulatorNum(tp, "Button 1") %>, split) template
    action Szablon - Akumulator
    templater true
```button
name Dodaj akumulator AA
type note(Akumulatory/AA/<% tp.user.getThisAcumulatorNum(tp, "Button 2") %>, split) template
action Szablon - Akumulator
templater true
```

do this:
```dataviewjs
const {createButton} = app.plugins.plugins["buttons"];
const { user, file } = app.plugins.plugins["templater-obsidian"].templater.current_functions_object;
const createNote = (buttonString) => {
  const number = user.getThisAcumulatorNum(tp, buttonString);
  file.create_new("Szablon - Akumulator", `Akumulatory/AA/${number}`, true);
};

const buttonName1 = 'Button 1';
this.container.appendChild(createButton({
  app,
  el: this.container,
  args: { name: buttonName1 },
  clickOverride: { click: createNote, params: [buttonName1] }
}));

const buttonName2 = 'Button 2';
this.container.appendChild(createButton({
  app,
  el: this.container,
  args: { name: buttonName2 },
  clickOverride: { click: createNote, params: [buttonName2] }
}));
```
ZobaJakColbert commented 12 months ago

Unfortunately this doesn't work on Obsidian Mobile.

CaptSolo commented 8 months ago

I noticed a similar issue with the following button definitions: https://gist.github.com/CaptSolo/b1dec50dd3f8b48925ab80ba3c74009a

When the 1st of the two buttons is pressed, for some reason the command / template for the second button gets executed. That is, the command / template of the 1st button does not get executed at all.

Thanks to @blbaker for a workaround – if the line "templater true" is removed from button definitions, the problem disappears.

shabegom commented 5 months ago

this should be fixed now, reopen if still a problem