Skip to content

Custom Bard buttons can be added conditionally#4106

Merged
jasonvarga merged 7 commits intostatamic:3.2from
morhi:enhancement/bard-user-buttons
Nov 24, 2021
Merged

Custom Bard buttons can be added conditionally#4106
jasonvarga merged 7 commits intostatamic:3.2from
morhi:enhancement/bard-user-buttons

Conversation

@morhi
Copy link
Copy Markdown
Contributor

@morhi morhi commented Aug 10, 2021

When adding custom buttons to bard using

Statamic.$bard.buttons( (button) => { ... } );

the buttons will be available in any bard instance, without the possibility to disable them in the blueprint editor. This PR adds the option to enable or disable custom buttons from addons or the project for each fieldtype config.

Blueprint Editor:
Bildschirmfoto 2021-08-10 um 15 16 17

Bard Instance:
Bildschirmfoto 2021-08-10 um 15 16 34

Javascript Usage:

Statamic.$bard.buttons((buttons, selectedButtons) => {
    return [
        {
            name: 'world',
            text: 'Beatiful World',
            command: 'beautifulWorld
            args: { world: 'beautiful' },
            icon: 'check',
            component: BeautifulWorldButton,
        },
    ];
});

// or

Statamic.$bard.buttons((buttons, selectedButtons) => {
    if (!selectedButtons || selectedButtons.include('world')) {
        buttons.push({
            name: 'world',
            text: 'Beatiful World',
            command: 'beautifulWorld
            args: { world: 'beautiful' },
            icon: 'check',
            component: BeautifulWorldButton,
        });
    }
});

Closes #2340

morhi added 3 commits August 10, 2021 14:33
Custom buttons, that have been added to bard using

  Statamic.$bard.buttons( ... )

will now be shown in the Blueprint editor, to enable or
disable them for a specific field.
Custom buttons that have not been selected in the blueprint editor
to be available for that field will now get hidden. However, this will
only happen if you return an array of button in the $bard.buttons callback
and not if you manipulate the buttons array directly.

The Statamic.$bard.buttons callback handler receives a new property `selectedButtons`.
This array of strings contains the selected buttons of the current field. The argument
will be undefined when being called from the blueprint editor, so that you can
distinguish whether you should add a button or not.

Example:

Statamic.$bard.buttons((buttons, selectedButtons) => {
    if (!selectedButtons || selectedButtons.includes('lead')) {
        buttons.push({
            name: 'lead',
            text: 'Lead Text',
            command: 'lead',
            icon: 'lead',
            component: LeadButton,
        });
    }
});

However, if you simply return an array of buttons bard will take care of the visible
state itself. This is meant for backwards compatibility reasons.
@jasonvarga
Copy link
Copy Markdown
Member

What's the purpose of the new selectedButtons argument being passed into the callback? It's being filtered out immediately anyway.

@morhi
Copy link
Copy Markdown
Contributor Author

morhi commented Aug 25, 2021

What's the purpose of the new selectedButtons argument being passed into the callback? It's being filtered out immediately anyway.

This is more like backwards-compatibility issue. The Statamic.$bard.buttons is called from two places: 1) In the blueprint editor, where you can define which buttons should be available for this bard field 2) In the entry publish form, where the field is actually used.

So in case 1 all buttons should be shown. In case 2 only the selected buttons from 1) should be shown. However, this is not a problem, as long as you return an array of buttons that should be added, because they will be filtered out automatically according to their availability state. But there is the possibility to directly manipulate the buttons reference without returning a value, which makes the button always appear in case 2), if you don't manipulate the buttons conditionally.

When you look at https://statamic.dev/extending/bard#buttons the reference manipulation is one of the examples.

Maybe I can fix this, so it is not necessary to do this extra check.

@jasonvarga jasonvarga changed the base branch from 3.1 to 3.2 November 24, 2021 15:26
@jasonvarga
Copy link
Copy Markdown
Member

jasonvarga commented Nov 24, 2021

Thanks for this PR, and sorry for the delay on it.

The way you had it was a breaking change. If you didn't have the button configured in the buttons array in the blueprint, it would no longer be visible in the toolbar.

What I've done is introduce a button function that developers can use to wrap their buttons in.

When we use the callback in the fieldtype, it'll filter itself out if it's not configured in the buttons array.
When we use the callback in the blueprint builder, it'll always show it.

Statamic.$bard.buttons((buttons, button) => {
    return button({ name: 'bold' });
    // or multiple...
    return [
      button({ name: 'bold' }),
      button({ name: 'italic' }),
    ];
});

People using the existing way, where you return an object or an array of objects, it'll continue to add the button to all fields. If they want to start making it conditional, they can just wrap their object in button().

I also removed the css change, it was a bit buggy in the regular fieldtype. Feel free to open a separate issue or PR for that.

…ly and they return null, they'll get filtered out too
@jasonvarga jasonvarga changed the title Make bard only show custom buttons being selected in the field config Custom Bard buttons can be added conditionally Nov 24, 2021
@jasonvarga jasonvarga merged commit da4936d into statamic:3.2 Nov 24, 2021
@morhi morhi deleted the enhancement/bard-user-buttons branch May 3, 2023 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom Bard Extensions can not be shown or hidden via the button config

2 participants