js/plugins/linkit/linkit.png | Bin 539 -> 0 bytes js/plugins/linkit/plugin.js | 223 ----------------------------------- src/Form/LinkitEditorDialog.php | 207 -------------------------------- src/Plugin/CKEditorPlugin/Linkit.php | 123 ------------------- 4 files changed, 553 deletions(-) diff --git a/js/plugins/linkit/linkit.png b/js/plugins/linkit/linkit.png deleted file mode 100644 index 90fc72e..0000000 Binary files a/js/plugins/linkit/linkit.png and /dev/null differ diff --git a/js/plugins/linkit/plugin.js b/js/plugins/linkit/plugin.js deleted file mode 100644 index e3fd0ce..0000000 --- a/js/plugins/linkit/plugin.js +++ /dev/null @@ -1,223 +0,0 @@ -/** - * @file - * Linkit plugin. - * - * @ignore - */ - -(function ($, Drupal, drupalSettings, CKEDITOR) { - - 'use strict'; - - // Alter the dialog settings to make a bigger dialog. - // $(window).on('dialog:beforecreate', function (event, dialog, $element, settings) { - // settings.dialogClass = settings.dialogClass.replace('ui-dialog--narrow', ''); - // settings.width = 700; - // }); - - CKEDITOR.plugins.add('linkit', { - init: function (editor) { - // Add the commands for link and unlink. - editor.addCommand('linkit', { - allowedContent: new CKEDITOR.style({ - element: 'a', - attributes: { - '!href': '', - // @TODO: Read these dynamically from the profile. - 'accesskey': '', - 'id': '', - 'rel': '', - 'target': '', - 'title': '' - } - }), - requiredContent: new CKEDITOR.style({ - element: 'a', - attributes: { - href: '' - } - }), - modes: {wysiwyg: 1}, - canUndo: true, - exec: function (editor) { - var linkElement = getSelectedLink(editor); - var linkDOMElement = null; - - // Set existing values based on selected element. - var existingValues = {}; - if (linkElement && linkElement.$) { - linkDOMElement = linkElement.$; - - // Populate an array with the link's current attributes. - var attribute = null; - var attributeName; - for (var attrIndex = 0; attrIndex < linkDOMElement.attributes.length; attrIndex++) { - attribute = linkDOMElement.attributes.item(attrIndex); - attributeName = attribute.nodeName.toLowerCase(); - // Don't consider data-cke-saved- attributes; they're just there - // to work around browser quirks. - if (attributeName.substring(0, 15) === 'data-cke-saved-') { - continue; - } - // Store the value for this attribute, unless there's a - // data-cke-saved- alternative for it, which will contain the - // quirk-free, original value. - existingValues[attributeName] = linkElement.data('cke-saved-' + attributeName) || attribute.nodeValue; - } - } - - // Prepare a save callback to be used upon saving the dialog. - var saveCallback = function (returnValues) { - editor.fire('saveSnapshot'); - - // Create a new link element if needed. - if (!linkElement && returnValues.attributes.href) { - var selection = editor.getSelection(); - var range = selection.getRanges(1)[0]; - - // Use link URL as text with a collapsed cursor. - if (range.collapsed) { - // Shorten mailto URLs to just the email address. - var text = new CKEDITOR.dom.text(returnValues.attributes.href.replace(/^mailto:/, ''), editor.document); - range.insertNode(text); - range.selectNodeContents(text); - } - - // Create the new link by applying a style to the new text. - var style = new CKEDITOR.style({element: 'a', attributes: returnValues.attributes}); - style.type = CKEDITOR.STYLE_INLINE; - style.applyToRange(range); - range.select(); - - // Set the link so individual properties may be set below. - linkElement = getSelectedLink(editor); - } - // Update the link properties. - else if (linkElement) { - for (var attrName in returnValues.attributes) { - if (returnValues.attributes.hasOwnProperty(attrName)) { - // Update the property if a value is specified. - if (returnValues.attributes[attrName].length > 0) { - var value = returnValues.attributes[attrName]; - linkElement.data('cke-saved-' + attrName, value); - linkElement.setAttribute(attrName, value); - } - // Delete the property if set to an empty string. - else { - linkElement.removeAttribute(attrName); - } - } - } - } - - // Save snapshot for undo support. - editor.fire('saveSnapshot'); - }; - // Drupal.t() will not work inside CKEditor plugins because CKEditor - // loads the JavaScript file instead of Drupal. Pull translated - // strings from the plugin settings that are translated server-side. - var dialogSettings = { - title: linkElement ? editor.config.linkit_dialogTitleAdd : editor.config.linkit_dialogTitleEdit, - dialogClass: 'editor-linkit-dialog' - }; - - // Open the dialog for the edit form. - Drupal.ckeditor.openDialog(editor, Drupal.url('linkit/dialog/linkit/' + editor.config.drupal.format), existingValues, saveCallback, dialogSettings); - } - }); - - // CTRL + L. - editor.setKeystroke(CKEDITOR.CTRL + 76, 'linkit'); - - // Add buttons. - if (editor.ui.addButton) { - editor.ui.addButton('Linkit', { - label: Drupal.t('Link'), - command: 'linkit', - icon: this.path + '/linkit.png' - }); - } - - editor.on('doubleclick', function (evt) { - var element = getSelectedLink(editor) || evt.data.element; - - if (!element.isReadOnly()) { - if (element.is('a')) { - editor.getSelection().selectElement(element); - editor.getCommand('linkit').exec(); - } - } - }); - - // If the "menu" plugin is loaded, register the menu items. - if (editor.addMenuItems) { - editor.addMenuItems({ - linkit: { - label: Drupal.t('Edit Link'), - command: 'linkit', - group: 'link', - order: 1 - } - }); - } - - // If the "contextmenu" plugin is loaded, register the listeners. - if (editor.contextMenu) { - editor.contextMenu.addListener(function (element, selection) { - if (!element || element.isReadOnly()) { - return null; - } - var anchor = getSelectedLink(editor); - if (!anchor) { - return null; - } - - var menu = {}; - if (anchor.getAttribute('href') && anchor.getChildCount()) { - menu = { - linkit: CKEDITOR.TRISTATE_OFF - }; - } - return menu; - }); - } - } - }); - - /** - * Get the surrounding link element of current selection. - * - * The following selection will all return the link element. - * - * @example - * li^nk - * [link] - * text[link] - * li[nk] - * [li]nk] - * [li]nk - * - * @param {CKEDITOR.editor} editor - * The CKEditor editor object - * - * @return {?HTMLElement} - * The selected link element, or null. - * - */ - function getSelectedLink(editor) { - var selection = editor.getSelection(); - var selectedElement = selection.getSelectedElement(); - if (selectedElement && selectedElement.is('a')) { - return selectedElement; - } - - var range = selection.getRanges(true)[0]; - - if (range) { - range.shrink(CKEDITOR.SHRINK_TEXT); - return editor.elementPath(range.getCommonAncestor()).contains('a', 1); - } - return null; - } - -})(jQuery, Drupal, drupalSettings, CKEDITOR); diff --git a/src/Form/LinkitEditorDialog.php b/src/Form/LinkitEditorDialog.php deleted file mode 100644 index 8f3b8f5..0000000 --- a/src/Form/LinkitEditorDialog.php +++ /dev/null @@ -1,207 +0,0 @@ -editorStorage = $editor_storage; - $this->linkitProfileStorage = $linkit_profile_storage; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('entity.manager')->getStorage('editor'), - $container->get('entity.manager')->getStorage('linkit_profile') - ); - } - - /** - * {@inheritdoc} - */ - public function getFormId() { - return 'linkit_editor_dialog_form'; - } - - /** - * {@inheritdoc} - * - * @param \Drupal\filter\Entity\FilterFormat $filter_format - * The filter format for which this dialog corresponds. - */ - public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) { - // The default values are set directly from \Drupal::request()->request, - // provided by the editor plugin opening the dialog. - $user_input = $form_state->getUserInput(); - $input = isset($user_input['editor_object']) ? $user_input['editor_object'] : []; - - /** @var \Drupal\editor\EditorInterface $editor */ - $editor = $this->editorStorage->load($filter_format->id()); - $linkit_profile_id = $editor->getSettings()['plugins']['linkit']['linkit_profile']; - $this->linkitProfile = $this->linkitProfileStorage->load($linkit_profile_id); - - $form['#tree'] = TRUE; - $form['#attached']['library'][] = 'editor/drupal.editor.dialog'; - $form['#prefix'] = '
'; - $form['#suffix'] = '
'; - - // Everything under the "attributes" key is merged directly into the - // generated link tag's attributes. - $form['attributes']['href'] = [ - '#title' => $this->t('Link'), - '#type' => 'linkit', - '#default_value' => isset($input['href']) ? $input['href'] : '', - '#description' => $this->t('Start typing to find content or paste a URL.'), - '#autocomplete_route_name' => 'linkit.autocomplete', - '#autocomplete_route_parameters' => [ - 'linkit_profile_id' => $linkit_profile_id - ], - '#weight' => 0, - ]; - - $this->addAttributes($form, $form_state, $this->linkitProfile->getAttributes(), $input); - - $form['actions'] = [ - '#type' => 'actions', - ]; - - $form['actions']['save_modal'] = [ - '#type' => 'submit', - '#value' => $this->t('Save'), - '#submit' => [], - '#ajax' => [ - 'callback' => '::submitForm', - 'event' => 'click', - ], - ]; - - return $form; - } - - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, FormStateInterface $form_state) { - $attributes = array_filter($form_state->getValue('attributes')); - $form_state->setValue('attributes', $attributes); - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - $response = new AjaxResponse(); - - if ($form_state->getErrors()) { - unset($form['#prefix'], $form['#suffix']); - $form['status_messages'] = [ - '#type' => 'status_messages', - '#weight' => -10, - ]; - $response->addCommand(new HtmlCommand('#linkit-editor-dialog-form', $form)); - } - else { - $response->addCommand(new EditorDialogSave($form_state->getValues())); - $response->addCommand(new CloseModalDialogCommand()); - } - - return $response; - } - - /** - * Adds the attributes enabled on the current profile. - * - * @param array $form - * An associative array containing the structure of the form. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * @param AttributeCollection $attributes - * A collection of attributes for the current profile. - * @param array $input - * An array with the attribute values from the editor. - */ - private function addAttributes(array &$form, FormStateInterface &$form_state, AttributeCollection $attributes, array $input) { - if ($attributes->count()) { - $form['linkit_attributes'] = [ - '#type' => 'container', - '#title' => $this->t('Attributes'), - '#weight' => '10', - ]; - - /** @var \Drupal\linkit\AttributeInterface $plugin */ - foreach ($attributes as $plugin) { - $plugin_name = $plugin->getHtmlName(); - - $default_value = isset($input[$plugin_name]) ? $input[$plugin_name] : ''; - $form['linkit_attributes'][$plugin_name] = $plugin->buildFormElement($default_value); - $form['linkit_attributes'][$plugin_name] += [ - '#parents' => [ - 'attributes', $plugin_name, - ], - ]; - } - } - } - - /** - * Gets the linkit profile entity. - * - * @return \Drupal\linkit\ProfileInterface - * The current linkit profile used by this form. - */ - public function getLinkitProfile() { - return $this->linkitProfile; - } - -} diff --git a/src/Plugin/CKEditorPlugin/Linkit.php b/src/Plugin/CKEditorPlugin/Linkit.php deleted file mode 100644 index 54056a5..0000000 --- a/src/Plugin/CKEditorPlugin/Linkit.php +++ /dev/null @@ -1,123 +0,0 @@ -linkitProfileStorage = $linkit_profile_storage; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('entity.manager')->getStorage('linkit_profile') - ); - } - - /** - * {@inheritdoc} - */ - public function getFile() { - return drupal_get_path('module', 'linkit') . '/js/plugins/linkit/plugin.js'; - } - - /** - * {@inheritdoc} - */ - public function getConfig(Editor $editor) { - return array( - 'linkit_dialogTitleAdd' => t('Add link'), - 'linkit_dialogTitleEdit' => t('Edit link'), - ); - } - - /** - * {@inheritdoc} - */ - public function getButtons() { - return array( - 'Linkit' => array( - 'label' => t('Linkit'), - 'image' => drupal_get_path('module', 'linkit') . '/js/plugins/linkit/linkit.png', - ), - ); - } - - /** - * {@inheritdoc} - */ - public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) { - $settings = $editor->getSettings(); - - $all_profiles = $this->linkitProfileStorage->loadMultiple(); - - $options = array(); - foreach ($all_profiles as $profile) { - $options[$profile->id()] = $profile->label(); - } - - $form['linkit_profile'] = array( - '#type' => 'select', - '#title' => t('Select a linkit profile'), - '#options' => $options, - '#default_value' => isset($settings['plugins']['linkit']) ? $settings['plugins']['linkit'] : '', - '#empty_option' => $this->t('- Select profile -'), - '#description' => $this->t('Select the linkit profile you wish to use with this text format.'), - '#element_validate' => array( - array($this, 'validateLinkitProfileSelection'), - ), - ); - - return $form; - } - - /** - * #element_validate handler for the "linkit_profile" element in settingsForm(). - */ - public function validateLinkitProfileSelection(array $element, FormStateInterface $form_state) { - $toolbar_buttons = $form_state->getValue(array('editor', 'settings', 'toolbar', 'button_groups')); - if (strpos($toolbar_buttons, '"Linkit"') !== FALSE && empty($element['#value'])) { - $form_state->setError($element, t('Please select the linkit profile you wish to use.')); - } - } - -}