This code snippet adds a custom "Edit" button to the CKEditor context menu, specifically for a widget with the class "my-widget." It first creates a new menu group called myWidgetGroup and then registers a menu item labeled "Edit My Widget," which is associated with the myWidgetEdit command and includes an optional icon. The context menu listener ensures that this option only appears when right-clicking a div element with the my-widget class. When the "Edit My Widget" option is clicked, the myWidgetEdit command is executed, triggering the edit functionality for the selected widget. The key is to have the correct command, so the widget dialog is correctly opened for the pre-existing widget.
Source code viewer
if (editor.contextMenu) { editor.addMenuGroup('myWidgetGroup'); editor.addMenuItem('myWidgetEdit', { label: 'Edit My Widget', // Provide an icon if needed. icon: this.path + 'icons/edit.png', command: 'myWidgetEdit', group: 'myWidgetGroup' }); editor.contextMenu.addListener(function(element) { if (element.getAscendant('div', true) && element.hasClass('my-widget')) { return { myWidgetEdit: CKEDITOR.TRISTATE_OFF }; } }); editor.addCommand('myWidgetEdit', { exec: function(editor) { var widget = editor.widgets.focused; if (widget && widget.name === 'myWidget') { widget.edit(); } } }); } } });Programming Language: Javascript