This example shows how to build a custom plugin that opens a dropdown with a list of actions.
<div id="entry-dropdown-with-list"></div>
const app = Redactor('#entry-dropdown-with-list', {
plugins: ['dropdownList']
});
// Plugin example
class DropdownListPlugin {
static defaultLang = {
en: {
dropdownList: {
title: 'Insert',
greeting: 'Greeting',
quote: 'Quote',
divider: 'Divider'
}
}
};
constructor(app) {
this.app = app;
}
start() {
this.app.toolbar.add('dropdownList', {
title: '## dropdownList.title ##',
icon: '<svg ...>',
command: 'dropdownList.open',
position: { after: 'add' }
});
}
open(params = {}, { button, el } = {}) {
const items = {
greeting: {
title: '## dropdownList.greeting ##',
command: 'dropdownList.insert',
params: { html: '<p>Hello from the dropdown!</p>' }
},
quote: {
icon: false,
title: '## dropdownList.quote ##',
command: 'dropdownList.insert',
params: { html: '<blockquote>A short quote block.</blockquote>' }
},
divider: {
title: '## dropdownList.divider ##',
command: 'dropdownList.insert',
params: { html: '<hr>' }
}
};
this.app.dropdown.create('dropdownList', { items });
this.app.dropdown.open(button || el);
}
insert(params = {}) {
this.app.dropdown.close();
const html = params.html;
if (!html) return;
const inserter = this.app.getService('inserter');
inserter.insertHtml(html, { caret: 'end' });
}
}
Redactor.addPlugin('dropdownList', DropdownListPlugin);