plainTextEditor.es6.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @file
  3. * ContentEditable-based in-place editor for plain text content.
  4. */
  5. (function ($, _, Drupal) {
  6. Drupal.quickedit.editors.plain_text = Drupal.quickedit.EditorView.extend(/** @lends Drupal.quickedit.editors.plain_text# */{
  7. /**
  8. * Stores the textual DOM element that is being in-place edited.
  9. */
  10. $textElement: null,
  11. /**
  12. * @constructs
  13. *
  14. * @augments Drupal.quickedit.EditorView
  15. *
  16. * @param {object} options
  17. * Options for the plain text editor.
  18. */
  19. initialize(options) {
  20. Drupal.quickedit.EditorView.prototype.initialize.call(this, options);
  21. const editorModel = this.model;
  22. const fieldModel = this.fieldModel;
  23. // Store the original value of this field. Necessary for reverting
  24. // changes.
  25. const $fieldItems = this.$el.find('.quickedit-field');
  26. const $textElement = $fieldItems.length ? $fieldItems.eq(0) : this.$el;
  27. this.$textElement = $textElement;
  28. editorModel.set('originalValue', $.trim(this.$textElement.text()));
  29. // Sets the state to 'changed' whenever the value changes.
  30. let previousText = editorModel.get('originalValue');
  31. $textElement.on('keyup paste', (event) => {
  32. const currentText = $.trim($textElement.text());
  33. if (previousText !== currentText) {
  34. previousText = currentText;
  35. editorModel.set('currentValue', currentText);
  36. fieldModel.set('state', 'changed');
  37. }
  38. });
  39. },
  40. /**
  41. * @inheritdoc
  42. *
  43. * @return {jQuery}
  44. * The text element for the plain text editor.
  45. */
  46. getEditedElement() {
  47. return this.$textElement;
  48. },
  49. /**
  50. * @inheritdoc
  51. *
  52. * @param {object} fieldModel
  53. * The field model that holds the state.
  54. * @param {string} state
  55. * The state to change to.
  56. * @param {object} options
  57. * State options, if needed by the state change.
  58. */
  59. stateChange(fieldModel, state, options) {
  60. const from = fieldModel.previous('state');
  61. const to = state;
  62. switch (to) {
  63. case 'inactive':
  64. break;
  65. case 'candidate':
  66. if (from !== 'inactive') {
  67. this.$textElement.removeAttr('contenteditable');
  68. }
  69. if (from === 'invalid') {
  70. this.removeValidationErrors();
  71. }
  72. break;
  73. case 'highlighted':
  74. break;
  75. case 'activating':
  76. // Defer updating the field model until the current state change has
  77. // propagated, to not trigger a nested state change event.
  78. _.defer(() => {
  79. fieldModel.set('state', 'active');
  80. });
  81. break;
  82. case 'active':
  83. this.$textElement.attr('contenteditable', 'true');
  84. break;
  85. case 'changed':
  86. break;
  87. case 'saving':
  88. if (from === 'invalid') {
  89. this.removeValidationErrors();
  90. }
  91. this.save(options);
  92. break;
  93. case 'saved':
  94. break;
  95. case 'invalid':
  96. this.showValidationErrors();
  97. break;
  98. }
  99. },
  100. /**
  101. * @inheritdoc
  102. *
  103. * @return {object}
  104. * A settings object for the quick edit UI.
  105. */
  106. getQuickEditUISettings() {
  107. return { padding: true, unifiedToolbar: false, fullWidthToolbar: false, popup: false };
  108. },
  109. /**
  110. * @inheritdoc
  111. */
  112. revert() {
  113. this.$textElement.html(this.model.get('originalValue'));
  114. },
  115. });
  116. }(jQuery, _, Drupal));