From 7ad9094bd4bcf06a25a54b11793adbb3adf19da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=C3=A9na=20Proxima?= Date: Wed, 11 Nov 2015 13:59:13 -0500 Subject: [PATCH 1/3] Separated the CKEditor plugins into an engine-only plugin which defines the widget, and a plugin which creates the command and button(s). --- js/plugins/drupalentity/engine.js | 91 +++++++++++++++++++++++++++++++ js/plugins/drupalentity/plugin.js | 79 +-------------------------- 2 files changed, 94 insertions(+), 76 deletions(-) create mode 100644 js/plugins/drupalentity/engine.js diff --git a/js/plugins/drupalentity/engine.js b/js/plugins/drupalentity/engine.js new file mode 100644 index 00000000..f4f495d0 --- /dev/null +++ b/js/plugins/drupalentity/engine.js @@ -0,0 +1,91 @@ +/** + * @file + * Drupal Entity embed plugin. + */ + +(function ($, Drupal, CKEDITOR) { + + "use strict"; + + CKEDITOR.plugins.add('drupalentity', { + // This plugin requires the Widgets System defined in the 'widget' plugin. + requires: 'widget', + + // The plugin initialization logic goes inside this method. + beforeInit: function (editor) { + // Configure CKEditor DTD for custom drupal-entity element. + // @see https://www.drupal.org/node/2448449#comment-9717735 + var dtd = CKEDITOR.dtd, tagName; + dtd['drupal-entity'] = {'#': 1}; + // Register drupal-entity element as allowed child, in each tag that can + // contain a div element. + for (tagName in dtd) { + if (dtd[tagName].div) { + dtd[tagName]['drupal-entity'] = 1; + } + } + + // Register the entity embed widget. + editor.widgets.add('drupalentity', { + // Minimum HTML which is required by this widget to work. + allowedContent: 'drupal-entity[*]', + requiredContent: 'drupal-entity[*]', + + // Simply recognize the element as our own. The inner markup if fetched + // and inserted the init() callback, since it requires the actual DOM + // element. + upcast: function (element) { + var attributes = element.attributes; + if (attributes['data-entity-type'] === undefined || (attributes['data-entity-id'] === undefined && attributes['data-entity-uuid'] === undefined) || (attributes['data-view-mode'] === undefined && attributes['data-entity-embed-display'] === undefined)) { + return; + } + // Generate an ID for the element, so that we can use the Ajax + // framework. + element.attributes.id = generateEmbedId(); + return element; + }, + + // Fetch the rendered entity. + init: function () { + var element = this.element; + var $element = $(element.$); + // Use the Ajax framework to fetch the HTML, so that we can retrieve + // out-of-band assets (JS, CSS...). + var entityEmbedPreview = new Drupal.ajax({ + base: $element.attr('id'), + element: $element, + url: Drupal.url('embed/preview/' + editor.config.drupal.format + '?' + $.param({ + value: element.getOuterHtml() + })), + progress: {type: 'none'}, + // Use a custom event to trigger the call. + event: 'entity_embed_dummy_event' + }); + entityEmbedPreview.execute(); + }, + + // Downcast the element. + downcast: function (element) { + // Only keep the wrapping element. + element.setHtml(''); + // Remove the auto-generated ID. + delete element.attributes.id; + return element; + } + }); + } + }); + + /** + * Generates unique HTML IDs for the widgets. + * + * @returns {string} + */ + function generateEmbedId() { + if (typeof generateEmbedId.counter == 'undefined') { + generateEmbedId.counter = 0; + } + return 'entity-embed-' + generateEmbedId.counter++; + } + +})(jQuery, Drupal, CKEDITOR); diff --git a/js/plugins/drupalentity/plugin.js b/js/plugins/drupalentity/plugin.js index b47d3e93..1a1bcf9c 100644 --- a/js/plugins/drupalentity/plugin.js +++ b/js/plugins/drupalentity/plugin.js @@ -1,30 +1,18 @@ /** * @file - * Drupal Entity embed plugin. + * Drupal Entity embed button plugin. */ (function ($, Drupal, CKEDITOR) { "use strict"; - CKEDITOR.plugins.add('drupalentity', { + CKEDITOR.plugins.add('drupalentity_button', { // This plugin requires the Widgets System defined in the 'widget' plugin. - requires: 'widget', + requires: 'drupalentity', // The plugin initialization logic goes inside this method. beforeInit: function (editor) { - // Configure CKEditor DTD for custom drupal-entity element. - // @see https://www.drupal.org/node/2448449#comment-9717735 - var dtd = CKEDITOR.dtd, tagName; - dtd['drupal-entity'] = {'#': 1}; - // Register drupal-entity element as allowed child, in each tag that can - // contain a div element. - for (tagName in dtd) { - if (dtd[tagName].div) { - dtd[tagName]['drupal-entity'] = 1; - } - } - // Generic command for adding/editing entities of all types. editor.addCommand('editdrupalentity', { allowedContent: 'drupal-entity[*]', @@ -81,55 +69,6 @@ } }); - // Register the entity embed widget. - editor.widgets.add('drupalentity', { - // Minimum HTML which is required by this widget to work. - allowedContent: 'drupal-entity[*]', - requiredContent: 'drupal-entity[*]', - - // Simply recognize the element as our own. The inner markup if fetched - // and inserted the init() callback, since it requires the actual DOM - // element. - upcast: function (element) { - var attributes = element.attributes; - if (attributes['data-entity-type'] === undefined || (attributes['data-entity-id'] === undefined && attributes['data-entity-uuid'] === undefined) || (attributes['data-view-mode'] === undefined && attributes['data-entity-embed-display'] === undefined)) { - return; - } - // Generate an ID for the element, so that we can use the Ajax - // framework. - element.attributes.id = generateEmbedId(); - return element; - }, - - // Fetch the rendered entity. - init: function () { - var element = this.element; - var $element = $(element.$); - // Use the Ajax framework to fetch the HTML, so that we can retrieve - // out-of-band assets (JS, CSS...). - var entityEmbedPreview = new Drupal.ajax({ - base: $element.attr('id'), - element: $element, - url: Drupal.url('embed/preview/' + editor.config.drupal.format + '?' + $.param({ - value: element.getOuterHtml() - })), - progress: {type: 'none'}, - // Use a custom event to trigger the call. - event: 'entity_embed_dummy_event' - }); - entityEmbedPreview.execute(); - }, - - // Downcast the element. - downcast: function (element) { - // Only keep the wrapping element. - element.setHtml(''); - // Remove the auto-generated ID. - delete element.attributes.id; - return element; - } - }); - // Register the toolbar buttons. if (editor.ui.addButton) { for (var key in editor.config.DrupalEntity_buttons) { @@ -199,16 +138,4 @@ return widget && widget.name === 'drupalentity'; } - /** - * Generates unique HTML IDs for the widgets. - * - * @returns {string} - */ - function generateEmbedId() { - if (typeof generateEmbedId.counter == 'undefined') { - generateEmbedId.counter = 0; - } - return 'entity-embed-' + generateEmbedId.counter++; - } - })(jQuery, Drupal, CKEDITOR); From 0920a1612cd06bd66f13ca6596a37e3353d85ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=C3=A9na=20Proxima?= Date: Wed, 11 Nov 2015 14:06:49 -0500 Subject: [PATCH 2/3] Spearated the CKEditor plugins (on the Drupal side) into two classes. --- src/Plugin/CKEditorPlugin/DrupalEntity.php | 14 ++--- .../CKEditorPlugin/DrupalEntityButton.php | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 src/Plugin/CKEditorPlugin/DrupalEntityButton.php diff --git a/src/Plugin/CKEditorPlugin/DrupalEntity.php b/src/Plugin/CKEditorPlugin/DrupalEntity.php index 6de18483..81fb30a8 100644 --- a/src/Plugin/CKEditorPlugin/DrupalEntity.php +++ b/src/Plugin/CKEditorPlugin/DrupalEntity.php @@ -25,28 +25,22 @@ class DrupalEntity extends EmbedCKEditorPluginBase { /** * {@inheritdoc} */ - protected function getButton(EmbedButtonInterface $embed_button) { - $button = parent::getButton($embed_button); - $button['entity_type'] = $embed_button->getTypeSetting('entity_type'); - return $button; + public function isInternal() { + return TRUE; } /** * {@inheritdoc} */ public function getFile() { - return drupal_get_path('module', 'entity_embed') . '/js/plugins/drupalentity/plugin.js'; + return drupal_get_path('module', 'entity_embed') . '/js/plugins/drupalentity/engine.js'; } /** * {@inheritdoc} */ public function getConfig(Editor $editor) { - return array( - 'DrupalEntity_dialogTitleAdd' => t('Insert entity'), - 'DrupalEntity_dialogTitleEdit' => t('Edit entity'), - 'DrupalEntity_buttons' => $this->getButtons(), - ); + return array(); } } diff --git a/src/Plugin/CKEditorPlugin/DrupalEntityButton.php b/src/Plugin/CKEditorPlugin/DrupalEntityButton.php new file mode 100644 index 00000000..8b59a6df --- /dev/null +++ b/src/Plugin/CKEditorPlugin/DrupalEntityButton.php @@ -0,0 +1,52 @@ +getTypeSetting('entity_type'); + return $button; + } + + /** + * {@inheritdoc} + */ + public function getFile() { + return drupal_get_path('module', 'entity_embed') . '/js/plugins/drupalentity/plugin.js'; + } + + /** + * {@inheritdoc} + */ + public function getConfig(Editor $editor) { + return array( + 'DrupalEntity_dialogTitleAdd' => t('Insert entity'), + 'DrupalEntity_dialogTitleEdit' => t('Edit entity'), + 'DrupalEntity_buttons' => $this->getButtons(), + ); + } + +} From d89fe8cfa28f42ec5cece30182da14c12ece70c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=C3=A9na=20Proxima?= Date: Wed, 11 Nov 2015 14:28:11 -0500 Subject: [PATCH 3/3] Fixed plugin dependencies. --- src/Plugin/CKEditorPlugin/DrupalEntity.php | 8 -------- src/Plugin/CKEditorPlugin/DrupalEntityButton.php | 7 +++++++ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Plugin/CKEditorPlugin/DrupalEntity.php b/src/Plugin/CKEditorPlugin/DrupalEntity.php index 81fb30a8..7759827a 100644 --- a/src/Plugin/CKEditorPlugin/DrupalEntity.php +++ b/src/Plugin/CKEditorPlugin/DrupalEntity.php @@ -8,7 +8,6 @@ namespace Drupal\entity_embed\Plugin\CKEditorPlugin; use Drupal\editor\Entity\Editor; -use Drupal\embed\EmbedButtonInterface; use Drupal\embed\EmbedCKEditorPluginBase; /** @@ -22,13 +21,6 @@ */ class DrupalEntity extends EmbedCKEditorPluginBase { - /** - * {@inheritdoc} - */ - public function isInternal() { - return TRUE; - } - /** * {@inheritdoc} */ diff --git a/src/Plugin/CKEditorPlugin/DrupalEntityButton.php b/src/Plugin/CKEditorPlugin/DrupalEntityButton.php index 8b59a6df..df6b8452 100644 --- a/src/Plugin/CKEditorPlugin/DrupalEntityButton.php +++ b/src/Plugin/CKEditorPlugin/DrupalEntityButton.php @@ -31,6 +31,13 @@ protected function getButton(EmbedButtonInterface $embed_button) { return $button; } + /** + * {@inheritdoc} + */ + public function getDependencies(Editor $editor) { + return ['drupalentity']; + } + /** * {@inheritdoc} */