This example shows how to build a custom plugin that opens a dropdown with custom HTML content.
<div id="entry-dropdown-with-html"></div>
const app = Redactor('#entry-dropdown-with-html', {
plugins: ['dropdownHtml']
});
// Plugin example
class DropdownHtmlPlugin {
static defaultLang = {
en: {
dropdownHtml: {
title: 'Selection',
section: 'Selection',
characters: 'Characters',
words: 'Words',
preview: 'Preview',
previewEmpty: 'Select text in the editor…',
transform: 'Transform',
uppercase: 'UPPERCASE',
lowercase: 'lowercase',
titleCase: 'Title Case'
}
}
};
constructor(app) {
this.app = app;
this.panel = null;
this._onSelectionChange = () => this._updatePanel();
this.app.on('dropdown:close', () => this._onDropdownClose());
}
start() {
this.app.toolbar.add('dropdownHtml', {
title: '## dropdownHtml.title ##',
icon: '<svg ...>',
command: 'dropdownHtml.open',
position: { after: 'add' }
});
}
open(params = {}, { button, el } = {}) {
this.panel = this._createPanel();
this.app.dropdown.create('dropdownHtml', { nodes: this.panel, width: '280px' });
this.app.dropdown.open(button || el);
this.app.on('selection:change', this._onSelectionChange);
this._updatePanel();
}
transform(mode) {
this.app.dropdown.close();
const selection = this.app.getService('selection');
const text = selection.getText() || '';
if (!text) return;
let result = text;
selection.saveSelection();
if (mode === 'upper') {
result = text.toUpperCase();
} else if (mode === 'lower') {
result = text.toLowerCase();
} else if (mode === 'title') {
result = text.replace(/\b\w/g, (char) => char.toUpperCase());
}
const inserter = this.app.getService('inserter');
inserter.insertText(result, { caret: 'end' });
selection.restoreSelection();
this._updatePanel();
}
_onDropdownClose() {
if (this.app.dropdown?.name !== 'dropdownHtml') return;
this.app.off('selection:change', this._onSelectionChange);
this.panel = null;
}
_updatePanel() {
if (!this.panel) return;
const selection = this.app.getService('selection');
const text = selection.getText() || '';
const hasText = text.length > 0;
const words = hasText ? text.trim().split(/\s+/).filter(Boolean).length : 0;
this.panel.querySelector('[data-stat="chars"]').textContent = String(text.length);
this.panel.querySelector('[data-stat="words"]').textContent = String(words);
const preview = this.panel.querySelector('[data-role="preview"]');
preview.value = hasText ? text : '';
preview.placeholder = this.app.getLang('dropdownHtml.previewEmpty');
this.panel.querySelectorAll('[data-transform]').forEach((button) => {
button.disabled = !hasText;
});
}
_createPanel() {
const doc = this.app.host.getDocument() || document;
const lang = (key) => this.app.getLang(`dropdownHtml.${key}`);
const form = doc.createElement('div');
form.className = 'rx-form rx-form-box';
const section = doc.createElement('div');
section.className = 'rx-form-section';
section.textContent = lang('section');
const stats = doc.createElement('div');
stats.className = 'rx-text-stats';
stats.style.marginBottom = '12px';
const charsRow = doc.createElement('div');
charsRow.className = 'rx-stat-item';
const charsLabel = doc.createElement('span');
charsLabel.className = 'rx-stat-label';
charsLabel.textContent = lang('characters');
const charsValue = doc.createElement('span');
charsValue.className = 'rx-stat-value';
charsValue.dataset.stat = 'chars';
charsValue.textContent = '0';
charsRow.append(charsLabel, charsValue);
const wordsRow = doc.createElement('div');
wordsRow.className = 'rx-stat-item';
const wordsLabel = doc.createElement('span');
wordsLabel.className = 'rx-stat-label';
wordsLabel.textContent = lang('words');
const wordsValue = doc.createElement('span');
wordsValue.className = 'rx-stat-value';
wordsValue.dataset.stat = 'words';
wordsValue.textContent = '0';
wordsRow.append(wordsLabel, wordsValue);
stats.append(charsRow, wordsRow);
const previewItem = doc.createElement('div');
previewItem.className = 'rx-form-item';
const previewLabel = doc.createElement('label');
previewLabel.className = 'rx-form-section';
previewLabel.textContent = lang('preview');
const preview = doc.createElement('textarea');
preview.className = 'rx-form-textarea';
preview.dataset.role = 'preview';
preview.readOnly = true;
preview.rows = 3;
previewItem.append(previewLabel, preview);
const transformSection = doc.createElement('div');
transformSection.className = 'rx-form-section';
transformSection.textContent = lang('transform');
const controls = doc.createElement('div');
controls.className = 'rx-form-flex rx-form-flex-wrap';
[
['upper', 'uppercase'],
['lower', 'lowercase'],
['title', 'titleCase']
].forEach(([mode, labelKey]) => {
const button = doc.createElement('button');
button.type = 'button';
button.className = 'rx-form-button';
button.dataset.transform = mode;
button.textContent = lang(labelKey);
button.disabled = true;
button.addEventListener('click', () => this.transform(mode));
controls.appendChild(button);
});
form.append(section, stats, previewItem, transformSection, controls);
previewLabel.setAttribute('for', preview.id = `dropdown-html-preview-${Date.now()}`);
return form;
}
}
Redactor.addPlugin('dropdownHtml', DropdownHtmlPlugin);