2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
8 (function ($, Drupal, drupalSettings, CKEDITOR) {
9 function parseAttributes(editor, element) {
10 var parsedAttributes = {};
12 var domElement = element.$;
13 var attribute = void 0;
14 var attributeName = void 0;
15 for (var attrIndex = 0; attrIndex < domElement.attributes.length; attrIndex++) {
16 attribute = domElement.attributes.item(attrIndex);
17 attributeName = attribute.nodeName.toLowerCase();
19 if (attributeName.indexOf('data-cke-') === 0) {
23 parsedAttributes[attributeName] = element.data('cke-saved-' + attributeName) || attribute.nodeValue;
26 if (parsedAttributes.class) {
27 parsedAttributes.class = CKEDITOR.tools.trim(parsedAttributes.class.replace(/cke_\S+/, ''));
30 return parsedAttributes;
33 function getAttributes(editor, data) {
35 for (var attributeName in data) {
36 if (data.hasOwnProperty(attributeName)) {
37 set[attributeName] = data[attributeName];
41 set['data-cke-saved-href'] = set.href;
45 if (set.hasOwnProperty(s)) {
52 removed: CKEDITOR.tools.objectKeys(removed)
56 CKEDITOR.plugins.add('drupallink', {
57 icons: 'drupallink,drupalunlink',
60 init: function init(editor) {
61 editor.addCommand('drupallink', {
70 requiredContent: new CKEDITOR.style({
76 modes: { wysiwyg: 1 },
78 exec: function exec(editor) {
79 var drupalImageUtils = CKEDITOR.plugins.drupalimage;
80 var focusedImageWidget = drupalImageUtils && drupalImageUtils.getFocusedWidget(editor);
81 var linkElement = getSelectedLink(editor);
83 var existingValues = {};
84 if (linkElement && linkElement.$) {
85 existingValues = parseAttributes(editor, linkElement);
86 } else if (focusedImageWidget && focusedImageWidget.data.link) {
87 existingValues = CKEDITOR.tools.clone(focusedImageWidget.data.link);
90 var saveCallback = function saveCallback(returnValues) {
91 if (focusedImageWidget) {
92 focusedImageWidget.setData('link', CKEDITOR.tools.extend(returnValues.attributes, focusedImageWidget.data.link));
93 editor.fire('saveSnapshot');
97 editor.fire('saveSnapshot');
99 if (!linkElement && returnValues.attributes.href) {
100 var selection = editor.getSelection();
101 var range = selection.getRanges(1)[0];
103 if (range.collapsed) {
104 var text = new CKEDITOR.dom.text(returnValues.attributes.href.replace(/^mailto:/, ''), editor.document);
105 range.insertNode(text);
106 range.selectNodeContents(text);
109 var style = new CKEDITOR.style({ element: 'a', attributes: returnValues.attributes });
110 style.type = CKEDITOR.STYLE_INLINE;
111 style.applyToRange(range);
114 linkElement = getSelectedLink(editor);
115 } else if (linkElement) {
116 for (var attrName in returnValues.attributes) {
117 if (returnValues.attributes.hasOwnProperty(attrName)) {
118 if (returnValues.attributes[attrName].length > 0) {
119 var value = returnValues.attributes[attrName];
120 linkElement.data('cke-saved-' + attrName, value);
121 linkElement.setAttribute(attrName, value);
123 linkElement.removeAttribute(attrName);
129 editor.fire('saveSnapshot');
132 var dialogSettings = {
133 title: linkElement ? editor.config.drupalLink_dialogTitleEdit : editor.config.drupalLink_dialogTitleAdd,
134 dialogClass: 'editor-link-dialog'
137 Drupal.ckeditor.openDialog(editor, Drupal.url('editor/dialog/link/' + editor.config.drupal.format), existingValues, saveCallback, dialogSettings);
140 editor.addCommand('drupalunlink', {
143 requiredContent: new CKEDITOR.style({
149 exec: function exec(editor) {
150 var style = new CKEDITOR.style({ element: 'a', type: CKEDITOR.STYLE_INLINE, alwaysRemoveElement: 1 });
151 editor.removeStyle(style);
153 refresh: function refresh(editor, path) {
154 var element = path.lastElement && path.lastElement.getAscendant('a', true);
155 if (element && element.getName() === 'a' && element.getAttribute('href') && element.getChildCount()) {
156 this.setState(CKEDITOR.TRISTATE_OFF);
158 this.setState(CKEDITOR.TRISTATE_DISABLED);
163 editor.setKeystroke(CKEDITOR.CTRL + 75, 'drupallink');
165 if (editor.ui.addButton) {
166 editor.ui.addButton('DrupalLink', {
167 label: Drupal.t('Link'),
168 command: 'drupallink'
170 editor.ui.addButton('DrupalUnlink', {
171 label: Drupal.t('Unlink'),
172 command: 'drupalunlink'
176 editor.on('doubleclick', function (evt) {
177 var element = getSelectedLink(editor) || evt.data.element;
179 if (!element.isReadOnly()) {
180 if (element.is('a')) {
181 editor.getSelection().selectElement(element);
182 editor.getCommand('drupallink').exec();
187 if (editor.addMenuItems) {
188 editor.addMenuItems({
190 label: Drupal.t('Edit Link'),
191 command: 'drupallink',
197 label: Drupal.t('Unlink'),
198 command: 'drupalunlink',
205 if (editor.contextMenu) {
206 editor.contextMenu.addListener(function (element, selection) {
207 if (!element || element.isReadOnly()) {
210 var anchor = getSelectedLink(editor);
216 if (anchor.getAttribute('href') && anchor.getChildCount()) {
217 menu = { link: CKEDITOR.TRISTATE_OFF, unlink: CKEDITOR.TRISTATE_OFF };
225 function getSelectedLink(editor) {
226 var selection = editor.getSelection();
227 var selectedElement = selection.getSelectedElement();
228 if (selectedElement && selectedElement.is('a')) {
229 return selectedElement;
232 var range = selection.getRanges(true)[0];
235 range.shrink(CKEDITOR.SHRINK_TEXT);
236 return editor.elementPath(range.getCommonAncestor()).contains('a', 1);
241 CKEDITOR.plugins.drupallink = {
242 parseLinkAttributes: parseAttributes,
243 getLinkAttributes: getAttributes
245 })(jQuery, Drupal, drupalSettings, CKEDITOR);