editor.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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, drupalSettings) {
  8. function findFieldForFormatSelector($formatSelector) {
  9. var fieldId = $formatSelector.attr('data-editor-for');
  10. return $('#' + fieldId).get(0);
  11. }
  12. function filterXssWhenSwitching(field, format, originalFormatID, callback) {
  13. if (format.editor.isXssSafe) {
  14. callback(field, format);
  15. } else {
  16. $.ajax({
  17. url: Drupal.url('editor/filter_xss/' + format.format),
  18. type: 'POST',
  19. data: {
  20. value: field.value,
  21. original_format_id: originalFormatID
  22. },
  23. dataType: 'json',
  24. success: function success(xssFilteredValue) {
  25. if (xssFilteredValue !== false) {
  26. field.value = xssFilteredValue;
  27. }
  28. callback(field, format);
  29. }
  30. });
  31. }
  32. }
  33. function changeTextEditor(field, newFormatID) {
  34. var previousFormatID = field.getAttribute('data-editor-active-text-format');
  35. if (drupalSettings.editor.formats[previousFormatID]) {
  36. Drupal.editorDetach(field, drupalSettings.editor.formats[previousFormatID]);
  37. } else {
  38. $(field).off('.editor');
  39. }
  40. if (drupalSettings.editor.formats[newFormatID]) {
  41. var format = drupalSettings.editor.formats[newFormatID];
  42. filterXssWhenSwitching(field, format, previousFormatID, Drupal.editorAttach);
  43. }
  44. field.setAttribute('data-editor-active-text-format', newFormatID);
  45. }
  46. function onTextFormatChange(event) {
  47. var $select = $(event.target);
  48. var field = event.data.field;
  49. var activeFormatID = field.getAttribute('data-editor-active-text-format');
  50. var newFormatID = $select.val();
  51. if (newFormatID === activeFormatID) {
  52. return;
  53. }
  54. var supportContentFiltering = drupalSettings.editor.formats[newFormatID] && drupalSettings.editor.formats[newFormatID].editorSupportsContentFiltering;
  55. var hasContent = field.value !== '';
  56. if (hasContent && supportContentFiltering) {
  57. var message = Drupal.t('Changing the text format to %text_format will permanently remove content that is not allowed in that text format.<br><br>Save your changes before switching the text format to avoid losing data.', {
  58. '%text_format': $select.find('option:selected').text()
  59. });
  60. var confirmationDialog = Drupal.dialog('<div>' + message + '</div>', {
  61. title: Drupal.t('Change text format?'),
  62. dialogClass: 'editor-change-text-format-modal',
  63. resizable: false,
  64. buttons: [{
  65. text: Drupal.t('Continue'),
  66. class: 'button button--primary',
  67. click: function click() {
  68. changeTextEditor(field, newFormatID);
  69. confirmationDialog.close();
  70. }
  71. }, {
  72. text: Drupal.t('Cancel'),
  73. class: 'button',
  74. click: function click() {
  75. $select.val(activeFormatID);
  76. confirmationDialog.close();
  77. }
  78. }],
  79. closeOnEscape: false,
  80. create: function create() {
  81. $(this).parent().find('.ui-dialog-titlebar-close').remove();
  82. },
  83. beforeClose: false,
  84. close: function close(event) {
  85. $(event.target).remove();
  86. }
  87. });
  88. confirmationDialog.showModal();
  89. } else {
  90. changeTextEditor(field, newFormatID);
  91. }
  92. }
  93. Drupal.editors = {};
  94. Drupal.behaviors.editor = {
  95. attach: function attach(context, settings) {
  96. if (!settings.editor) {
  97. return;
  98. }
  99. $(context).find('[data-editor-for]').once('editor').each(function () {
  100. var $this = $(this);
  101. var field = findFieldForFormatSelector($this);
  102. if (!field) {
  103. return;
  104. }
  105. var activeFormatID = $this.val();
  106. field.setAttribute('data-editor-active-text-format', activeFormatID);
  107. if (settings.editor.formats[activeFormatID]) {
  108. Drupal.editorAttach(field, settings.editor.formats[activeFormatID]);
  109. }
  110. $(field).on('change.editor keypress.editor', function () {
  111. field.setAttribute('data-editor-value-is-changed', 'true');
  112. $(field).off('.editor');
  113. });
  114. if ($this.is('select')) {
  115. $this.on('change.editorAttach', { field: field }, onTextFormatChange);
  116. }
  117. $this.parents('form').on('submit', function (event) {
  118. if (event.isDefaultPrevented()) {
  119. return;
  120. }
  121. if (settings.editor.formats[activeFormatID]) {
  122. Drupal.editorDetach(field, settings.editor.formats[activeFormatID], 'serialize');
  123. }
  124. });
  125. });
  126. },
  127. detach: function detach(context, settings, trigger) {
  128. var editors = void 0;
  129. if (trigger === 'serialize') {
  130. editors = $(context).find('[data-editor-for]').findOnce('editor');
  131. } else {
  132. editors = $(context).find('[data-editor-for]').removeOnce('editor');
  133. }
  134. editors.each(function () {
  135. var $this = $(this);
  136. var activeFormatID = $this.val();
  137. var field = findFieldForFormatSelector($this);
  138. if (field && activeFormatID in settings.editor.formats) {
  139. Drupal.editorDetach(field, settings.editor.formats[activeFormatID], trigger);
  140. }
  141. });
  142. }
  143. };
  144. Drupal.editorAttach = function (field, format) {
  145. if (format.editor) {
  146. Drupal.editors[format.editor].attach(field, format);
  147. Drupal.editors[format.editor].onChange(field, function () {
  148. $(field).trigger('formUpdated');
  149. field.setAttribute('data-editor-value-is-changed', 'true');
  150. });
  151. }
  152. };
  153. Drupal.editorDetach = function (field, format, trigger) {
  154. if (format.editor) {
  155. Drupal.editors[format.editor].detach(field, format, trigger);
  156. if (field.getAttribute('data-editor-value-is-changed') === 'false') {
  157. field.value = field.getAttribute('data-editor-value-original');
  158. }
  159. }
  160. };
  161. })(jQuery, Drupal, drupalSettings);