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 Object.keys(data || {}).forEach(function (attributeName) {
36 set[attributeName] = data[attributeName];
39 set['data-cke-saved-href'] = set.href;
42 Object.keys(set).forEach(function (s) {
48 removed: CKEDITOR.tools.objectKeys(removed)
52 CKEDITOR.plugins.add('drupallink', {
53 icons: 'drupallink,drupalunlink',
56 init: function init(editor) {
57 editor.addCommand('drupallink', {
66 requiredContent: new CKEDITOR.style({
72 modes: { wysiwyg: 1 },
74 exec: function exec(editor) {
75 var drupalImageUtils = CKEDITOR.plugins.drupalimage;
76 var focusedImageWidget = drupalImageUtils && drupalImageUtils.getFocusedWidget(editor);
77 var linkElement = getSelectedLink(editor);
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);
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');
93 editor.fire('saveSnapshot');
95 if (!linkElement && returnValues.attributes.href) {
96 var selection = editor.getSelection();
97 var range = selection.getRanges(1)[0];
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);
105 var style = new CKEDITOR.style({ element: 'a', attributes: returnValues.attributes });
106 style.type = CKEDITOR.STYLE_INLINE;
107 style.applyToRange(range);
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);
118 linkElement.removeAttribute(attrName);
123 editor.fire('saveSnapshot');
126 var dialogSettings = {
127 title: linkElement ? editor.config.drupalLink_dialogTitleEdit : editor.config.drupalLink_dialogTitleAdd,
128 dialogClass: 'editor-link-dialog'
131 Drupal.ckeditor.openDialog(editor, Drupal.url('editor/dialog/link/' + editor.config.drupal.format), existingValues, saveCallback, dialogSettings);
134 editor.addCommand('drupalunlink', {
137 requiredContent: new CKEDITOR.style({
143 exec: function exec(editor) {
144 var style = new CKEDITOR.style({ element: 'a', type: CKEDITOR.STYLE_INLINE, alwaysRemoveElement: 1 });
145 editor.removeStyle(style);
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);
152 this.setState(CKEDITOR.TRISTATE_DISABLED);
157 editor.setKeystroke(CKEDITOR.CTRL + 75, 'drupallink');
159 if (editor.ui.addButton) {
160 editor.ui.addButton('DrupalLink', {
161 label: Drupal.t('Link'),
162 command: 'drupallink'
164 editor.ui.addButton('DrupalUnlink', {
165 label: Drupal.t('Unlink'),
166 command: 'drupalunlink'
170 editor.on('doubleclick', function (evt) {
171 var element = getSelectedLink(editor) || evt.data.element;
173 if (!element.isReadOnly()) {
174 if (element.is('a')) {
175 editor.getSelection().selectElement(element);
176 editor.getCommand('drupallink').exec();
181 if (editor.addMenuItems) {
182 editor.addMenuItems({
184 label: Drupal.t('Edit Link'),
185 command: 'drupallink',
191 label: Drupal.t('Unlink'),
192 command: 'drupalunlink',
199 if (editor.contextMenu) {
200 editor.contextMenu.addListener(function (element, selection) {
201 if (!element || element.isReadOnly()) {
204 var anchor = getSelectedLink(editor);
210 if (anchor.getAttribute('href') && anchor.getChildCount()) {
211 menu = { link: CKEDITOR.TRISTATE_OFF, unlink: CKEDITOR.TRISTATE_OFF };
219 function getSelectedLink(editor) {
220 var selection = editor.getSelection();
221 var selectedElement = selection.getSelectedElement();
222 if (selectedElement && selectedElement.is('a')) {
223 return selectedElement;
226 var range = selection.getRanges(true)[0];
229 range.shrink(CKEDITOR.SHRINK_TEXT);
230 return editor.elementPath(range.getCommonAncestor()).contains('a', 1);
235 CKEDITOR.plugins.drupallink = {
236 parseLinkAttributes: parseAttributes,
237 getLinkAttributes: getAttributes
239 })(jQuery, Drupal, drupalSettings, CKEDITOR);