created: 20201216182347597 modified: 20211018102328148 tags: title: How to create dynamic editor toolbar buttons type: text/vnd.tiddlywiki \define disabledFilterExample()`[[$(tempBoldDisabled)$]get[state-disabled]else[no]]` \define remove-shortcuts() <$action-deletetiddler $tiddler=<>/> <$action-deletetiddler $tiddler=<>/> <$action-deletetiddler $tiddler=<>/> <$action-deletetiddler $tiddler=<>/> <$action-deletetiddler $tiddler=<>/> \end \define create-shortcut-tiddlers() <$action-createtiddler $basetitle=<> $template="$:/config/ShortcutInfo/bold" $overwrite="yes" /> <$action-navigate $to=<>/> <$action-createtiddler $basetitle=<> $template="$:/config/shortcuts-mac/bold" $overwrite="yes" text="meta-shift-X" /> <$action-navigate $to=<>/> <$action-createtiddler $basetitle=<> $template="$:/config/shortcuts-not-mac/bold" $overwrite="yes" text="ctrl-shift-X" /> <$action-navigate $to=<>/> \end \define clone-bold() <$action-createtiddler $basetitle=<> $template="$:/core/ui/EditorToolbar/bold" $overwrite="yes" icon="$:/core/images/spiral" shortcuts="((temp-bold))" condition-disabled="[[$(tempBoldDisabled)$]get[state-disabled]else[no]]" /> <$action-sendmessage $message="tm-edit-tiddler" $param=<>/> \end \define clone-button-bold() <$button actions=<> >Create a Temporary Bold Button \end \define toggle-bold() <$action-listops $tiddler=<> $field="state-disabled" $subfilter="+[toggle[yes],[no]]" /> \end <$vars tempBold="$:/temp/bold" tempBoldDisabled="$:/temp/bold/disabled" shortcutInfo="$:/config/ShortcutInfo/temp-bold" shortcutConfigMac="$:/config/shortcuts-mac/temp-bold" shortcutConfigNotMac="$:/config/shortcuts-not-mac/temp-bold"> !! Create a New Toolbar Button The easiest way to create new editor toolbar button is to clone and open one. ><> This tiddler contains all the necessary elements that are important for toolbar buttons. <<< ; text : We don't discuss the text field details in this howto ; caption : The caption field is used to display the shortcut name in the $:/ControlPanel : Keyboard Shortcuts tab ; condition : A filter, that defines the button visibility state ; condition-disabled <<.from-version "5.1.23">> : A ''filter'', that allows us to define the "disabled" attribute for buttons. eg: <> : This condition ''must'' return "no", if the "state tiddler" or "state field" doesn't exist! So there has to be a `else[no]` element in the filter. ; description : Is used as the button tooltip ; icon : Assigns the button icon. We use `$:/core/images/spiral` {{$:/core/images/spiral}} here. More button icons can be found in the [[Icon Gallery]] ; shortcuts : This is the [[Keyboard Shortcut Descriptor]] eg: `((temp-bold))` <<< !! Disabled State You can use any "state tiddler" to define the button disabled state. It's important, that the ''condition-disabled'' field is a ''filter''. For our example we use the `state-disabled` field from tiddler: <> The easiest way to test filters is with the $:/AdvancedSearch : Filter tab > The temporary "bold button" is disabled: ''"{{{ [get[state-disabled]else[no]] }}}"'' ... You can see the button in the editor toolbar if you edit any tiddler! > <$button actions=<>>Toggle Temporary Bold Button Visisbility !! Create Keyboard Shortcuts for New Button For our ~HowTo we use the ''Keyboard Shortcut Descriptor'': `((temp-bold))` To create a valid shortcut configuration we need 2 tiddlers: >[[$:/config/ShortcutInfo/|$:/config/ShortcutInfo/temp-bold]]''temp-bold'' .. and >[[$:/config/shortcuts/|$:/config/shortcuts/temp-bold]]''temp-bold'' > <$button actions=<>>Create Shortcut Tiddlers !! !! Clean up the Configuration Tiddlers If you don't need the config tiddlers anymore you can ><$button actions=<>>Remove the Shortcut Configuration