plugin.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * @file
  3. * Plugin for inserting links with Linkit.
  4. */
  5. (function ($) {
  6. CKEDITOR.plugins.add( 'linkit', {
  7. requires : [ 'link' ],
  8. hidpi: true,
  9. icons: 'linkit',
  10. init: function( editor ) {
  11. // Get the major CKeditor verison.
  12. // We do not care about minor versions.
  13. var version = parseInt(CKEDITOR.version);
  14. // Add Button.
  15. editor.ui.addButton( 'linkit', {
  16. label: Drupal.t('Link to content'),
  17. command: 'linkit'
  18. });
  19. // Ckeditor version lower then 4 needs to have a icon path.
  20. if (version < 4) {
  21. editor.ui._.items.linkit.icon = this.path + 'icons/linkit.png';
  22. editor.ui._.items.linkit.args[0].icon = this.path + 'icons/linkit.png';
  23. }
  24. // Add Command.
  25. editor.addCommand( 'linkit', {
  26. // FOR ACF in ckeditor 4.1+, allow everything.
  27. allowedContent: 'a[*]{*}(*)',
  28. exec : function () {
  29. if (typeof Drupal.settings.linkit === 'undefined') {
  30. alert(Drupal.t('Could not find the Linkit profile.'));
  31. return ;
  32. }
  33. // Set the editor object.
  34. Drupal.settings.linkit.currentInstance.editor = editor;
  35. // Set profile.
  36. Drupal.settings.linkit.currentInstance.profile = Drupal.settings.linkit.fields[editor.name].profile;
  37. // Set the name of the source field.
  38. Drupal.settings.linkit.currentInstance.source = editor.name;
  39. // Set the source type.
  40. Drupal.settings.linkit.currentInstance.helper = 'ckeditor';
  41. var selection = editor.getSelection(),
  42. element = null;
  43. // If we have selected a link element, we what to grab its attributes
  44. // so we can inserten them into the Linkit form in the dialog.
  45. if ((element = CKEDITOR.plugins.link.getSelectedLink(editor)) && element.hasAttribute('href')) {
  46. selection.selectElement(element);
  47. }
  48. else {
  49. element = null;
  50. }
  51. // Save the selection.
  52. Drupal.settings.linkit.currentInstance.selection = selection;
  53. // Lock the selecton for IE.
  54. if (CKEDITOR.env.ie && typeof selection !== 'undefined') {
  55. selection.lock();
  56. }
  57. // Save the selected element.
  58. Drupal.settings.linkit.currentInstance.selectedElement = element;
  59. // Create the modal.
  60. Drupal.linkit.createModal();
  61. }
  62. });
  63. // If the "menu" plugin is loaded, register the menu items.
  64. if (editor.addMenuItems) {
  65. // Use the default link menu group weight and subtract one.
  66. var defaultMenuGroup = editor._.menuGroups.link;
  67. editor.addMenuGroup("Linkit", defaultMenuGroup - 1);
  68. editor.addMenuItems({
  69. linkit: {
  70. label: Drupal.t('Link to content'),
  71. command: 'linkit',
  72. group: 'Linkit',
  73. order: 0
  74. }
  75. });
  76. // Remove the default link option.
  77. editor.removeMenuItem('link');
  78. }
  79. // If the "contextmenu" plugin is loaded, register the listeners.
  80. if (editor.contextMenu) {
  81. editor.contextMenu.addListener(function(element, selection) {
  82. if (!element || element.isReadOnly() || (selection.getSelectedText().length < 1 && !element.is('a'))) {
  83. return null;
  84. }
  85. return {linkit: CKEDITOR.TRISTATE_OFF};
  86. });
  87. }
  88. // Add a shortcut. Only CKeditor version 4 has this function.
  89. if (version >= 4) {
  90. editor.setKeystroke( CKEDITOR.CTRL + 76 /*L*/, 'linkit' );
  91. }
  92. // Add event listener.
  93. editor.on( 'doubleclick', function( evt ) {
  94. // Delete the default link dialog.
  95. delete evt.data.dialog;
  96. var element = CKEDITOR.plugins.link.getSelectedLink( editor ) || evt.data.element;
  97. if ( !element.isReadOnly() ) {
  98. if ( element.is( 'a' ) ) {
  99. editor.getSelection().selectElement( element );
  100. if (version >= 4) {
  101. editor.commands.linkit.exec();
  102. }
  103. else if(version == 3) {
  104. editor._.commands.linkit.exec();
  105. }
  106. }
  107. }
  108. });
  109. // Register an extra fucntion, this will be used in the modal.
  110. editor._.linkitFnNum = CKEDITOR.tools.addFunction( insertLink, editor );
  111. }
  112. });
  113. /**
  114. * Create or update a link element in the editor.
  115. */
  116. function insertLink(data, editor) {
  117. var selection = editor.getSelection();
  118. data.path = CKEDITOR.tools.trim(data.path);
  119. // Browser need the "href" for copy/paste link to work. (CKEDITOR ISSUE #6641)
  120. data.attributes['data-cke-saved-href'] = data.path;
  121. if (!Drupal.settings.linkit.currentInstance.selectedElement) {
  122. // We have not selected any link element so lets create a new one.
  123. var range = selection.getRanges(1)[0];
  124. if (range.collapsed) {
  125. var content = (Drupal.settings.linkit.currentInstance.linkContent) ? Drupal.settings.linkit.currentInstance.linkContent : data.path;
  126. var text = new CKEDITOR.dom.text(content , editor.document );
  127. range.insertNode(text);
  128. range.selectNodeContents(text);
  129. }
  130. // Delete all attributes that are empty.
  131. data.attributes.href = data.path;
  132. for (name in data.attributes) {
  133. data.attributes[name] ? null : delete data.attributes[name];
  134. }
  135. // Apply style.
  136. var style = new CKEDITOR.style({element : 'a', attributes : data.attributes});
  137. style.type = CKEDITOR.STYLE_INLINE;
  138. style.applyToRange(range);
  139. range.select();
  140. }
  141. else {
  142. var element = Drupal.settings.linkit.currentInstance.selectedElement;
  143. // We are editing an existing link, so just overwrite the attributes.
  144. element.setAttribute('href', data.path);
  145. element.setAttribute('data-cke-saved-href', data.path);
  146. for (name in data.attributes) {
  147. data.attributes[name] ?
  148. element.setAttribute(name, data.attributes[name]) :
  149. element.removeAttribute(name);
  150. }
  151. selection.selectElement( element );
  152. }
  153. // Unlock the selection.
  154. if (CKEDITOR.env.ie && typeof selection !== 'undefined') {
  155. selection.unlock();
  156. }
  157. }
  158. })(jQuery);