3 * ContentEditable-based in-place editor for plain text content.
6 (function ($, _, Drupal) {
7 Drupal.quickedit.editors.plain_text = Drupal.quickedit.EditorView.extend(/** @lends Drupal.quickedit.editors.plain_text# */{
10 * Stores the textual DOM element that is being in-place edited.
17 * @augments Drupal.quickedit.EditorView
19 * @param {object} options
20 * Options for the plain text editor.
23 Drupal.quickedit.EditorView.prototype.initialize.call(this, options);
25 const editorModel = this.model;
26 const fieldModel = this.fieldModel;
28 // Store the original value of this field. Necessary for reverting
31 const $fieldItems = this.$el.find('.quickedit-field');
32 if ($fieldItems.length) {
33 $textElement = this.$textElement = $fieldItems.eq(0);
36 $textElement = this.$textElement = this.$el;
38 editorModel.set('originalValue', $.trim(this.$textElement.text()));
40 // Sets the state to 'changed' whenever the value changes.
41 let previousText = editorModel.get('originalValue');
42 $textElement.on('keyup paste', (event) => {
43 const currentText = $.trim($textElement.text());
44 if (previousText !== currentText) {
45 previousText = currentText;
46 editorModel.set('currentValue', currentText);
47 fieldModel.set('state', 'changed');
56 * The text element for the plain text editor.
59 return this.$textElement;
65 * @param {object} fieldModel
66 * The field model that holds the state.
67 * @param {string} state
68 * The state to change to.
69 * @param {object} options
70 * State options, if needed by the state change.
72 stateChange(fieldModel, state, options) {
73 const from = fieldModel.previous('state');
80 if (from !== 'inactive') {
81 this.$textElement.removeAttr('contenteditable');
83 if (from === 'invalid') {
84 this.removeValidationErrors();
92 // Defer updating the field model until the current state change has
93 // propagated, to not trigger a nested state change event.
95 fieldModel.set('state', 'active');
100 this.$textElement.attr('contenteditable', 'true');
107 if (from === 'invalid') {
108 this.removeValidationErrors();
117 this.showValidationErrors();
126 * A settings object for the quick edit UI.
128 getQuickEditUISettings() {
129 return { padding: true, unifiedToolbar: false, fullWidthToolbar: false, popup: false };
136 this.$textElement.html(this.model.get('originalValue'));
140 }(jQuery, _, Drupal));