d53f49a345ec2a1670fe8c675b0552863a954947
[yaffs-website] / web / core / modules / ckeditor / js / plugins / drupallink / plugin.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function ($, Drupal, drupalSettings, CKEDITOR) {
9   function parseAttributes(editor, element) {
10     var parsedAttributes = {};
11
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();
18
19       if (attributeName.indexOf('data-cke-') === 0) {
20         continue;
21       }
22
23       parsedAttributes[attributeName] = element.data('cke-saved-' + attributeName) || attribute.nodeValue;
24     }
25
26     if (parsedAttributes.class) {
27       parsedAttributes.class = CKEDITOR.tools.trim(parsedAttributes.class.replace(/cke_\S+/, ''));
28     }
29
30     return parsedAttributes;
31   }
32
33   function getAttributes(editor, data) {
34     var set = {};
35     Object.keys(data || {}).forEach(function (attributeName) {
36       set[attributeName] = data[attributeName];
37     });
38
39     set['data-cke-saved-href'] = set.href;
40
41     var removed = {};
42     Object.keys(set).forEach(function (s) {
43       delete removed[s];
44     });
45
46     return {
47       set: set,
48       removed: CKEDITOR.tools.objectKeys(removed)
49     };
50   }
51
52   CKEDITOR.plugins.add('drupallink', {
53     icons: 'drupallink,drupalunlink',
54     hidpi: true,
55
56     init: function init(editor) {
57       editor.addCommand('drupallink', {
58         allowedContent: {
59           a: {
60             attributes: {
61               '!href': true
62             },
63             classes: {}
64           }
65         },
66         requiredContent: new CKEDITOR.style({
67           element: 'a',
68           attributes: {
69             href: ''
70           }
71         }),
72         modes: { wysiwyg: 1 },
73         canUndo: true,
74         exec: function exec(editor) {
75           var drupalImageUtils = CKEDITOR.plugins.drupalimage;
76           var focusedImageWidget = drupalImageUtils && drupalImageUtils.getFocusedWidget(editor);
77           var linkElement = getSelectedLink(editor);
78
79           var existingValues = {};
80           if (linkElement && linkElement.$) {
81             existingValues = parseAttributes(editor, linkElement);
82           } else if (focusedImageWidget && focusedImageWidget.data.link) {
83               existingValues = CKEDITOR.tools.clone(focusedImageWidget.data.link);
84             }
85
86           var saveCallback = function saveCallback(returnValues) {
87             if (focusedImageWidget) {
88               focusedImageWidget.setData('link', CKEDITOR.tools.extend(returnValues.attributes, focusedImageWidget.data.link));
89               editor.fire('saveSnapshot');
90               return;
91             }
92
93             editor.fire('saveSnapshot');
94
95             if (!linkElement && returnValues.attributes.href) {
96               var selection = editor.getSelection();
97               var range = selection.getRanges(1)[0];
98
99               if (range.collapsed) {
100                 var text = new CKEDITOR.dom.text(returnValues.attributes.href.replace(/^mailto:/, ''), editor.document);
101                 range.insertNode(text);
102                 range.selectNodeContents(text);
103               }
104
105               var style = new CKEDITOR.style({ element: 'a', attributes: returnValues.attributes });
106               style.type = CKEDITOR.STYLE_INLINE;
107               style.applyToRange(range);
108               range.select();
109
110               linkElement = getSelectedLink(editor);
111             } else if (linkElement) {
112                 Object.keys(returnValues.attributes || {}).forEach(function (attrName) {
113                   if (returnValues.attributes[attrName].length > 0) {
114                     var value = returnValues.attributes[attrName];
115                     linkElement.data('cke-saved-' + attrName, value);
116                     linkElement.setAttribute(attrName, value);
117                   } else {
118                       linkElement.removeAttribute(attrName);
119                     }
120                 });
121               }
122
123             editor.fire('saveSnapshot');
124           };
125
126           var dialogSettings = {
127             title: linkElement ? editor.config.drupalLink_dialogTitleEdit : editor.config.drupalLink_dialogTitleAdd,
128             dialogClass: 'editor-link-dialog'
129           };
130
131           Drupal.ckeditor.openDialog(editor, Drupal.url('editor/dialog/link/' + editor.config.drupal.format), existingValues, saveCallback, dialogSettings);
132         }
133       });
134       editor.addCommand('drupalunlink', {
135         contextSensitive: 1,
136         startDisabled: 1,
137         requiredContent: new CKEDITOR.style({
138           element: 'a',
139           attributes: {
140             href: ''
141           }
142         }),
143         exec: function exec(editor) {
144           var style = new CKEDITOR.style({ element: 'a', type: CKEDITOR.STYLE_INLINE, alwaysRemoveElement: 1 });
145           editor.removeStyle(style);
146         },
147         refresh: function refresh(editor, path) {
148           var element = path.lastElement && path.lastElement.getAscendant('a', true);
149           if (element && element.getName() === 'a' && element.getAttribute('href') && element.getChildCount()) {
150             this.setState(CKEDITOR.TRISTATE_OFF);
151           } else {
152             this.setState(CKEDITOR.TRISTATE_DISABLED);
153           }
154         }
155       });
156
157       editor.setKeystroke(CKEDITOR.CTRL + 75, 'drupallink');
158
159       if (editor.ui.addButton) {
160         editor.ui.addButton('DrupalLink', {
161           label: Drupal.t('Link'),
162           command: 'drupallink'
163         });
164         editor.ui.addButton('DrupalUnlink', {
165           label: Drupal.t('Unlink'),
166           command: 'drupalunlink'
167         });
168       }
169
170       editor.on('doubleclick', function (evt) {
171         var element = getSelectedLink(editor) || evt.data.element;
172
173         if (!element.isReadOnly()) {
174           if (element.is('a')) {
175             editor.getSelection().selectElement(element);
176             editor.getCommand('drupallink').exec();
177           }
178         }
179       });
180
181       if (editor.addMenuItems) {
182         editor.addMenuItems({
183           link: {
184             label: Drupal.t('Edit Link'),
185             command: 'drupallink',
186             group: 'link',
187             order: 1
188           },
189
190           unlink: {
191             label: Drupal.t('Unlink'),
192             command: 'drupalunlink',
193             group: 'link',
194             order: 5
195           }
196         });
197       }
198
199       if (editor.contextMenu) {
200         editor.contextMenu.addListener(function (element, selection) {
201           if (!element || element.isReadOnly()) {
202             return null;
203           }
204           var anchor = getSelectedLink(editor);
205           if (!anchor) {
206             return null;
207           }
208
209           var menu = {};
210           if (anchor.getAttribute('href') && anchor.getChildCount()) {
211             menu = { link: CKEDITOR.TRISTATE_OFF, unlink: CKEDITOR.TRISTATE_OFF };
212           }
213           return menu;
214         });
215       }
216     }
217   });
218
219   function getSelectedLink(editor) {
220     var selection = editor.getSelection();
221     var selectedElement = selection.getSelectedElement();
222     if (selectedElement && selectedElement.is('a')) {
223       return selectedElement;
224     }
225
226     var range = selection.getRanges(true)[0];
227
228     if (range) {
229       range.shrink(CKEDITOR.SHRINK_TEXT);
230       return editor.elementPath(range.getCommonAncestor()).contains('a', 1);
231     }
232     return null;
233   }
234
235   CKEDITOR.plugins.drupallink = {
236     parseLinkAttributes: parseAttributes,
237     getLinkAttributes: getAttributes
238   };
239 })(jQuery, Drupal, drupalSettings, CKEDITOR);