From c9173a873940b9f5e5be467fbdda2751d137a48a Mon Sep 17 00:00:00 2001 From: Maurice Hildebrandt Date: Tue, 10 Aug 2021 14:33:18 +0200 Subject: [PATCH 1/6] Show custom Bard buttons in blueprint editor 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. --- .../bard/BardButtonsSettingFieldtype.vue | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/resources/js/components/fieldtypes/bard/BardButtonsSettingFieldtype.vue b/resources/js/components/fieldtypes/bard/BardButtonsSettingFieldtype.vue index 79b19e852c3..89f6bbe3893 100644 --- a/resources/js/components/fieldtypes/bard/BardButtonsSettingFieldtype.vue +++ b/resources/js/components/fieldtypes/bard/BardButtonsSettingFieldtype.vue @@ -58,7 +58,23 @@ export default { methods: { initButtons() { - let available = addButtonHtml(availableButtons()); + // Get all default buttons first + let available = availableButtons(); + + // Add custom buttons from a project or addon + this.$bard.buttonCallbacks.map(callback => { + let returned = callback(available); + + // No return value means they intend to manipulate the + // buttons object manually. Just continue on. + if (!returned) return; + + available = available.concat( + Array.isArray(returned) ? returned : [returned] + ); + }) + + available = addButtonHtml(available); let buttons = available.map(button => { button.enabled = this.data.includes(button.name); From 2c42bc52558ef3d01d798a7626a71b139e0bcfff Mon Sep 17 00:00:00 2001 From: Maurice Hildebrandt Date: Tue, 10 Aug 2021 14:50:37 +0200 Subject: [PATCH 2/6] Make bard hide custom buttons that have not been selected 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. --- .../components/fieldtypes/bard/BardFieldtype.vue | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/resources/js/components/fieldtypes/bard/BardFieldtype.vue b/resources/js/components/fieldtypes/bard/BardFieldtype.vue index cdcbd01da2d..199440c819d 100644 --- a/resources/js/components/fieldtypes/bard/BardFieldtype.vue +++ b/resources/js/components/fieldtypes/bard/BardFieldtype.vue @@ -1,6 +1,6 @@