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 function getSelectedLink(editor) {
53 var selection = editor.getSelection();
54 var selectedElement = selection.getSelectedElement();
55 if (selectedElement && selectedElement.is('a')) {
56 return selectedElement;
59 var range = selection.getRanges(true)[0];
62 range.shrink(CKEDITOR.SHRINK_TEXT);
63 return editor.elementPath(range.getCommonAncestor()).contains('a', 1);
68 CKEDITOR.plugins.add('drupallink', {
69 icons: 'drupallink,drupalunlink',
72 init: function init(editor) {
73 editor.addCommand('drupallink', {
82 requiredContent: new CKEDITOR.style({
88 modes: { wysiwyg: 1 },
90 exec: function exec(editor) {
91 var drupalImageUtils = CKEDITOR.plugins.drupalimage;
92 var focusedImageWidget = drupalImageUtils && drupalImageUtils.getFocusedWidget(editor);
93 var linkElement = getSelectedLink(editor);
95 var existingValues = {};
96 if (linkElement && linkElement.$) {
97 existingValues = parseAttributes(editor, linkElement);
98 } else if (focusedImageWidget && focusedImageWidget.data.link) {
99 existingValues = CKEDITOR.tools.clone(focusedImageWidget.data.link);
102 var saveCallback = function saveCallback(returnValues) {
103 if (focusedImageWidget) {
104 focusedImageWidget.setData('link', CKEDITOR.tools.extend(returnValues.attributes, focusedImageWidget.data.link));
105 editor.fire('saveSnapshot');
109 editor.fire('saveSnapshot');
111 if (!linkElement && returnValues.attributes.href) {
112 var selection = editor.getSelection();
113 var range = selection.getRanges(1)[0];
115 if (range.collapsed) {
116 var text = new CKEDITOR.dom.text(returnValues.attributes.href.replace(/^mailto:/, ''), editor.document);
117 range.insertNode(text);
118 range.selectNodeContents(text);
121 var style = new CKEDITOR.style({
123 attributes: returnValues.attributes
125 style.type = CKEDITOR.STYLE_INLINE;
126 style.applyToRange(range);
129 linkElement = getSelectedLink(editor);
130 } else if (linkElement) {
131 Object.keys(returnValues.attributes || {}).forEach(function (attrName) {
132 if (returnValues.attributes[attrName].length > 0) {
133 var value = returnValues.attributes[attrName];
134 linkElement.data('cke-saved-' + attrName, value);
135 linkElement.setAttribute(attrName, value);
137 linkElement.removeAttribute(attrName);
142 editor.fire('saveSnapshot');
145 var dialogSettings = {
146 title: linkElement ? editor.config.drupalLink_dialogTitleEdit : editor.config.drupalLink_dialogTitleAdd,
147 dialogClass: 'editor-link-dialog'
150 Drupal.ckeditor.openDialog(editor, Drupal.url('editor/dialog/link/' + editor.config.drupal.format), existingValues, saveCallback, dialogSettings);
153 editor.addCommand('drupalunlink', {
156 requiredContent: new CKEDITOR.style({
162 exec: function exec(editor) {
163 var style = new CKEDITOR.style({
165 type: CKEDITOR.STYLE_INLINE,
166 alwaysRemoveElement: 1
168 editor.removeStyle(style);
170 refresh: function refresh(editor, path) {
171 var element = path.lastElement && path.lastElement.getAscendant('a', true);
172 if (element && element.getName() === 'a' && element.getAttribute('href') && element.getChildCount()) {
173 this.setState(CKEDITOR.TRISTATE_OFF);
175 this.setState(CKEDITOR.TRISTATE_DISABLED);
180 editor.setKeystroke(CKEDITOR.CTRL + 75, 'drupallink');
182 if (editor.ui.addButton) {
183 editor.ui.addButton('DrupalLink', {
184 label: Drupal.t('Link'),
185 command: 'drupallink'
187 editor.ui.addButton('DrupalUnlink', {
188 label: Drupal.t('Unlink'),
189 command: 'drupalunlink'
193 editor.on('doubleclick', function (evt) {
194 var element = getSelectedLink(editor) || evt.data.element;
196 if (!element.isReadOnly()) {
197 if (element.is('a')) {
198 editor.getSelection().selectElement(element);
199 editor.getCommand('drupallink').exec();
204 if (editor.addMenuItems) {
205 editor.addMenuItems({
207 label: Drupal.t('Edit Link'),
208 command: 'drupallink',
214 label: Drupal.t('Unlink'),
215 command: 'drupalunlink',
222 if (editor.contextMenu) {
223 editor.contextMenu.addListener(function (element, selection) {
224 if (!element || element.isReadOnly()) {
227 var anchor = getSelectedLink(editor);
233 if (anchor.getAttribute('href') && anchor.getChildCount()) {
235 link: CKEDITOR.TRISTATE_OFF,
236 unlink: CKEDITOR.TRISTATE_OFF
245 CKEDITOR.plugins.drupallink = {
246 parseLinkAttributes: parseAttributes,
247 getLinkAttributes: getAttributes
249 })(jQuery, Drupal, drupalSettings, CKEDITOR);