editor.formattedTextEditor.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. Drupal.quickedit.editors.editor = Drupal.quickedit.EditorView.extend({
  9. textFormat: null,
  10. textFormatHasTransformations: null,
  11. textEditor: null,
  12. $textElement: null,
  13. initialize: function initialize(options) {
  14. Drupal.quickedit.EditorView.prototype.initialize.call(this, options);
  15. var metadata = Drupal.quickedit.metadata.get(this.fieldModel.get('fieldID'), 'custom');
  16. this.textFormat = drupalSettings.editor.formats[metadata.format];
  17. this.textFormatHasTransformations = metadata.formatHasTransformations;
  18. this.textEditor = Drupal.editors[this.textFormat.editor];
  19. var $fieldItems = this.$el.find('.quickedit-field');
  20. if ($fieldItems.length) {
  21. this.$textElement = $fieldItems.eq(0);
  22. } else {
  23. this.$textElement = this.$el;
  24. }
  25. this.model.set('originalValue', this.$textElement.html());
  26. },
  27. getEditedElement: function getEditedElement() {
  28. return this.$textElement;
  29. },
  30. stateChange: function stateChange(fieldModel, state) {
  31. var editorModel = this.model;
  32. var from = fieldModel.previous('state');
  33. var to = state;
  34. switch (to) {
  35. case 'inactive':
  36. break;
  37. case 'candidate':
  38. if (from !== 'inactive' && from !== 'highlighted') {
  39. this.textEditor.detach(this.$textElement.get(0), this.textFormat);
  40. }
  41. if (from === 'active' && this.textFormatHasTransformations) {
  42. this.revert();
  43. }
  44. if (from === 'invalid') {
  45. this.removeValidationErrors();
  46. }
  47. break;
  48. case 'highlighted':
  49. break;
  50. case 'activating':
  51. if (this.textFormatHasTransformations) {
  52. var $textElement = this.$textElement;
  53. this._getUntransformedText(function (untransformedText) {
  54. $textElement.html(untransformedText);
  55. fieldModel.set('state', 'active');
  56. });
  57. } else {
  58. _.defer(function () {
  59. fieldModel.set('state', 'active');
  60. });
  61. }
  62. break;
  63. case 'active':
  64. {
  65. var textElement = this.$textElement.get(0);
  66. var toolbarView = fieldModel.toolbarView;
  67. this.textEditor.attachInlineEditor(textElement, this.textFormat, toolbarView.getMainWysiwygToolgroupId(), toolbarView.getFloatedWysiwygToolgroupId());
  68. this.textEditor.onChange(textElement, function (htmlText) {
  69. editorModel.set('currentValue', htmlText);
  70. fieldModel.set('state', 'changed');
  71. });
  72. break;
  73. }
  74. case 'changed':
  75. break;
  76. case 'saving':
  77. if (from === 'invalid') {
  78. this.removeValidationErrors();
  79. }
  80. this.save();
  81. break;
  82. case 'saved':
  83. break;
  84. case 'invalid':
  85. this.showValidationErrors();
  86. break;
  87. }
  88. },
  89. getQuickEditUISettings: function getQuickEditUISettings() {
  90. return {
  91. padding: true,
  92. unifiedToolbar: true,
  93. fullWidthToolbar: true,
  94. popup: false
  95. };
  96. },
  97. revert: function revert() {
  98. this.$textElement.html(this.model.get('originalValue'));
  99. },
  100. _getUntransformedText: function _getUntransformedText(callback) {
  101. var fieldID = this.fieldModel.get('fieldID');
  102. var textLoaderAjax = Drupal.ajax({
  103. url: Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('editor/!entity_type/!id/!field_name/!langcode/!view_mode')),
  104. submit: { nocssjs: true }
  105. });
  106. textLoaderAjax.commands.editorGetUntransformedText = function (ajax, response, status) {
  107. callback(response.data);
  108. };
  109. textLoaderAjax.execute();
  110. }
  111. });
  112. })(jQuery, Drupal, drupalSettings, _);