Froala, Inc
- Getting Started
- Browser Support
- Languages Support
- Shortcuts
- Activation
- Examples
- Customize the Editor
- Use-cases
- Plugins
- APIs
- Development Frameworks
- Server Integrations
- Server SDKs
- Migration Guides
- Changelog
- Tutorials
Custom Elements
Custom Dropdown
The following example shows how to create a custom dropdown for the editor:
- Define an icon.
FroalaEditor.DefineIcon('dropdownIcon', { NAME: 'magic'})
- Define a dropdown button.
FroalaEditor.RegisterCommand('myDropdown', {
- Set a title.
title: 'My Dropdown',
- Mark the button as a dropdown.
type: 'dropdown',
- Specify the icon for the button. If this option is not specified, the button name will be used.
icon: 'dropdownIcon',
- Options for the dropdown.
options: { 'opt1': 'Option 1', 'opt2': 'Option 2' },
- If present, the options property will be ignored.
It can be used to define a custom HTML for the dropdown.
The current context is the editor instance.
html: function () { return ''; },
- Save the dropdown action into undo stack.
undo: true,
- Focus inside the editor before callback.
focus: true,
- Refresh the button state after the callback.
refreshAfterCallback: true,
- Add a callback.
The current context is the editor instance.
callback: function (cmd, val, params) { console.log (this.html.get()); },
- Call a function when the dropdown button state might have changed.
The current context is the editor instance.
refresh: function ($btn) { console.log (this.selection.element()); },
- Call a function when the dropdown is shown.
The current context is the editor instance.
refreshOnShow: function ($btn, $dropdown) {
console.log (this.selection.element()); } })
Your final code should look like this:
FroalaEditor.DefineIcon('dropdownIcon', { NAME: 'magic'})
FroalaEditor.RegisterCommand('myDropdown', {
title: 'My Dropdown',
type: 'dropdown',
icon: 'dropdownIcon',
options: {
'opt1': 'Option 1',
'opt2': 'Option 2'
},
html: function () {
return '';
},
undo: true,
focus: true,
refreshAfterCallback: true,
callback: function (cmd, val, params) {
console.log (this.html.get());
},
refresh: function ($btn) {
console.log (this.selection.element());
},
refreshOnShow: function ($btn, $dropdown) {
console.log (this.selection.element());
}
})
For a working example please refer to the Custom Dropdown Example
Note: Once a command is defined it can be included in the buttons options: imageAltButtons
, imageEditButtons
, imageInsertButtons
, imageSizeButtons
, linkEditButtons
, linkInsertButtons
, tableColorsButtons
, tableEditButtons
, tableInsertButtons
, toolbarButtons
, toolbarButtonsMD
, toolbarButtonsSM
, toolbarButtonsXS
, videoEditButtons
, videoInsertButtons
or videoSizeButtons
.
Do you think we can improve this article? Let us know.