import { JoomlaEditor, JoomlaEditorButton } from 'editor-api'; /** * @copyright (C) 2023 Open Source Matters, Inc. * @license GNU General Public License version 2 or later; see LICENSE.txt */ /** * @param {Editor} editor */ const pluginSetUp = editor => { editor.options.register('joomlaExtButtons', { processor: 'object', default: { names: [] } }); // Get buttons list const buttons = editor.options.get('joomlaExtButtons').names || []; if (!buttons.length) { return; } // Build menu const subMenu = []; const icons = { joomla: '' }; buttons.forEach(xtdButton => { const button = {}; button.text = xtdButton.name; button.icon = xtdButton.icon; button.type = 'menuitem'; if (xtdButton.iconSVG) { icons[button.icon] = xtdButton.iconSVG; } button.onAction = () => { JoomlaEditor.setActive(editor.id); if (xtdButton.action) { JoomlaEditorButton.runAction(xtdButton.action, xtdButton.options || {}); } else if (xtdButton.bsModal) { document.getElementById(`${xtdButton.id}_modal`).open(); } else if (xtdButton.click) { // eslint-disable-next-line no-new-func new Function(xtdButton.click)(); } }; subMenu.push(button); }); // Register icons Object.keys(icons).forEach(icon => { editor.ui.registry.addIcon(icon, icons[icon]); }); // Register main button editor.ui.registry.addMenuButton('jxtdbuttons', { text: Joomla.Text._('PLG_TINY_CORE_BUTTONS'), icon: 'joomla', fetch: callback => callback(subMenu) }); }; window.tinymce.PluginManager.add('jxtdbuttons', editor => { pluginSetUp(editor); return { getMetadata: () => ({ name: 'Editor XTD buttons (Joomla)', url: 'https://www.joomla.org/' }) }; });