The example contains a CKEditor 4 widget with a dialog that allows users to edit widget properties. The dialog loads existing widget data into form fields. When the dialog is submitted, the form data is saved back into the widget element's data attribute, ensuring the widget state persists and can be reused or modified later.
Source code viewer
CKEDITOR.plugins.add('myPlugin', { icons: 'myPlugin', init: function(editor) { editor.widgets.add('myWidget', { // Button and initial template button: 'Add My Widget', template: '<div class="my-widget">Content here</div>', // Define editable areas within the widget editables: { content: { selector: '.my-widget' } }, // Allowed and required content allowedContent: 'div[!data-widget-state]', requiredContent: 'div[data-widget-state]', // Initialization function init: function() { // Load data from `data-widget-state` into `data-cke-widget-data` this.element.setAttribute('data-cke-widget-data', this.element.getAttribute('data-widget-state')); }, // Data function to handle saving widget data data: function() { // Save data from `data-cke-widget-data` into `data-widget-state` this.element.setAttribute('data-widget-state', this.element.getAttribute('data-cke-widget-data')); } }); } }); CKEDITOR.dialog.add('myDialog', function(editor) { return { title: 'Edit Widget', minWidth: 400, minHeight: 200, contents: [ { id: 'tab1', label: 'Tab 1', elements: [ { type: 'text', id: 'myField', label: 'My Field', setup: function(widget) { // Load saved data from widget.data, or use 'DEFAULT' as the default value. this.setValue(widget.data['FORM_ITEM_ID'] || 'DEFAULT'); }, commit: function(widget) { // Save form data to widget.data. widget.setData('FORM_ITEM_ID', this.getValue()); } } ] } ] }; });Programming Language: Javascript