ckeditor.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. (function (Drupal, debounce, CKEDITOR, $, displace, AjaxCommands) {
  8. Drupal.editors.ckeditor = {
  9. attach: function attach(element, format) {
  10. this._loadExternalPlugins(format);
  11. format.editorSettings.drupal = {
  12. format: format.format
  13. };
  14. var label = $('label[for=' + element.getAttribute('id') + ']').html();
  15. format.editorSettings.title = Drupal.t('Rich Text Editor, !label field', {
  16. '!label': label
  17. });
  18. return !!CKEDITOR.replace(element, format.editorSettings);
  19. },
  20. detach: function detach(element, format, trigger) {
  21. var editor = CKEDITOR.dom.element.get(element).getEditor();
  22. if (editor) {
  23. if (trigger === 'serialize') {
  24. editor.updateElement();
  25. } else {
  26. editor.destroy();
  27. element.removeAttribute('contentEditable');
  28. }
  29. }
  30. return !!editor;
  31. },
  32. onChange: function onChange(element, callback) {
  33. var editor = CKEDITOR.dom.element.get(element).getEditor();
  34. if (editor) {
  35. editor.on('change', debounce(function () {
  36. callback(editor.getData());
  37. }, 400));
  38. editor.on('mode', function () {
  39. var editable = editor.editable();
  40. if (!editable.isInline()) {
  41. editor.on('autoGrow', function (evt) {
  42. var doc = evt.editor.document;
  43. var scrollable = CKEDITOR.env.quirks ? doc.getBody() : doc.getDocumentElement();
  44. if (scrollable.$.scrollHeight < scrollable.$.clientHeight) {
  45. scrollable.setStyle('overflow-y', 'hidden');
  46. } else {
  47. scrollable.removeStyle('overflow-y');
  48. }
  49. }, null, null, 10000);
  50. }
  51. });
  52. }
  53. return !!editor;
  54. },
  55. attachInlineEditor: function attachInlineEditor(element, format, mainToolbarId, floatedToolbarId) {
  56. this._loadExternalPlugins(format);
  57. format.editorSettings.drupal = {
  58. format: format.format
  59. };
  60. var settings = $.extend(true, {}, format.editorSettings);
  61. if (mainToolbarId) {
  62. var settingsOverride = {
  63. extraPlugins: 'sharedspace',
  64. removePlugins: 'floatingspace,elementspath',
  65. sharedSpaces: {
  66. top: mainToolbarId
  67. }
  68. };
  69. var sourceButtonFound = false;
  70. for (var i = 0; !sourceButtonFound && i < settings.toolbar.length; i++) {
  71. if (settings.toolbar[i] !== '/') {
  72. for (var j = 0; !sourceButtonFound && j < settings.toolbar[i].items.length; j++) {
  73. if (settings.toolbar[i].items[j] === 'Source') {
  74. sourceButtonFound = true;
  75. settings.toolbar[i].items[j] = 'Sourcedialog';
  76. settingsOverride.extraPlugins += ',sourcedialog';
  77. settingsOverride.removePlugins += ',sourcearea';
  78. }
  79. }
  80. }
  81. }
  82. settings.extraPlugins += ',' + settingsOverride.extraPlugins;
  83. settings.removePlugins += ',' + settingsOverride.removePlugins;
  84. settings.sharedSpaces = settingsOverride.sharedSpaces;
  85. }
  86. element.setAttribute('contentEditable', 'true');
  87. return !!CKEDITOR.inline(element, settings);
  88. },
  89. _loadExternalPlugins: function _loadExternalPlugins(format) {
  90. var externalPlugins = format.editorSettings.drupalExternalPlugins;
  91. if (externalPlugins) {
  92. Object.keys(externalPlugins || {}).forEach(function (pluginName) {
  93. CKEDITOR.plugins.addExternal(pluginName, externalPlugins[pluginName], '');
  94. });
  95. delete format.editorSettings.drupalExternalPlugins;
  96. }
  97. }
  98. };
  99. Drupal.ckeditor = {
  100. saveCallback: null,
  101. openDialog: function openDialog(editor, url, existingValues, saveCallback, dialogSettings) {
  102. var $target = $(editor.container.$);
  103. if (editor.elementMode === CKEDITOR.ELEMENT_MODE_REPLACE) {
  104. $target = $target.find('.cke_contents');
  105. }
  106. $target.css('position', 'relative').find('.ckeditor-dialog-loading').remove();
  107. var classes = dialogSettings.dialogClass ? dialogSettings.dialogClass.split(' ') : [];
  108. classes.push('ui-dialog--narrow');
  109. dialogSettings.dialogClass = classes.join(' ');
  110. dialogSettings.autoResize = window.matchMedia('(min-width: 600px)').matches;
  111. dialogSettings.width = 'auto';
  112. var $content = $('<div class="ckeditor-dialog-loading"><span style="top: -40px;" class="ckeditor-dialog-loading-link">' + Drupal.t('Loading...') + '</span></div>');
  113. $content.appendTo($target);
  114. var ckeditorAjaxDialog = Drupal.ajax({
  115. dialog: dialogSettings,
  116. dialogType: 'modal',
  117. selector: '.ckeditor-dialog-loading-link',
  118. url: url,
  119. progress: { type: 'throbber' },
  120. submit: {
  121. editor_object: existingValues
  122. }
  123. });
  124. ckeditorAjaxDialog.execute();
  125. window.setTimeout(function () {
  126. $content.find('span').animate({ top: '0px' });
  127. }, 1000);
  128. Drupal.ckeditor.saveCallback = saveCallback;
  129. }
  130. };
  131. $(window).on('dialogcreate', function (e, dialog, $element, settings) {
  132. $('.ui-dialog--narrow').css('zIndex', CKEDITOR.config.baseFloatZIndex + 1);
  133. });
  134. $(window).on('dialog:beforecreate', function (e, dialog, $element, settings) {
  135. $('.ckeditor-dialog-loading').animate({ top: '-40px' }, function () {
  136. $(this).remove();
  137. });
  138. });
  139. $(window).on('editor:dialogsave', function (e, values) {
  140. if (Drupal.ckeditor.saveCallback) {
  141. Drupal.ckeditor.saveCallback(values);
  142. }
  143. });
  144. $(window).on('dialog:afterclose', function (e, dialog, $element) {
  145. if (Drupal.ckeditor.saveCallback) {
  146. Drupal.ckeditor.saveCallback = null;
  147. }
  148. });
  149. $(document).on('drupalViewportOffsetChange', function () {
  150. CKEDITOR.config.autoGrow_maxHeight = 0.7 * (window.innerHeight - displace.offsets.top - displace.offsets.bottom);
  151. });
  152. function redirectTextareaFragmentToCKEditorInstance() {
  153. var hash = window.location.hash.substr(1);
  154. var element = document.getElementById(hash);
  155. if (element) {
  156. var editor = CKEDITOR.dom.element.get(element).getEditor();
  157. if (editor) {
  158. var id = editor.container.getAttribute('id');
  159. window.location.replace('#' + id);
  160. }
  161. }
  162. }
  163. $(window).on('hashchange.ckeditor', redirectTextareaFragmentToCKEditorInstance);
  164. CKEDITOR.config.autoGrow_onStartup = true;
  165. CKEDITOR.config.autoGrow_maxHeight = 0.7 * window.innerHeight;
  166. CKEDITOR.timestamp = drupalSettings.ckeditor.timestamp;
  167. if (AjaxCommands) {
  168. AjaxCommands.prototype.ckeditor_add_stylesheet = function (ajax, response, status) {
  169. var editor = CKEDITOR.instances[response.editor_id];
  170. if (editor) {
  171. response.stylesheets.forEach(function (url) {
  172. editor.document.appendStyleSheet(url);
  173. });
  174. }
  175. };
  176. }
  177. })(Drupal, Drupal.debounce, CKEDITOR, jQuery, Drupal.displace, Drupal.AjaxCommands);