3 * ContentEditable-based in-place editor for plain text content.
6 (function($, _, Drupal) {
7 Drupal.quickedit.editors.plain_text = Drupal.quickedit.EditorView.extend(
8 /** @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
30 const $fieldItems = this.$el.find('.quickedit-field');
31 const $textElement = $fieldItems.length ? $fieldItems.eq(0) : this.$el;
32 this.$textElement = $textElement;
33 editorModel.set('originalValue', $.trim(this.$textElement.text()));
35 // Sets the state to 'changed' whenever the value changes.
36 let previousText = editorModel.get('originalValue');
37 $textElement.on('keyup paste', event => {
38 const currentText = $.trim($textElement.text());
39 if (previousText !== currentText) {
40 previousText = currentText;
41 editorModel.set('currentValue', currentText);
42 fieldModel.set('state', 'changed');
51 * The text element for the plain text editor.
54 return this.$textElement;
60 * @param {object} fieldModel
61 * The field model that holds the state.
62 * @param {string} state
63 * The state to change to.
64 * @param {object} options
65 * State options, if needed by the state change.
67 stateChange(fieldModel, state, options) {
68 const from = fieldModel.previous('state');
75 if (from !== 'inactive') {
76 this.$textElement.removeAttr('contenteditable');
78 if (from === 'invalid') {
79 this.removeValidationErrors();
87 // Defer updating the field model until the current state change has
88 // propagated, to not trigger a nested state change event.
90 fieldModel.set('state', 'active');
95 this.$textElement.attr('contenteditable', 'true');
102 if (from === 'invalid') {
103 this.removeValidationErrors();
112 this.showValidationErrors();
121 * A settings object for the quick edit UI.
123 getQuickEditUISettings() {
126 unifiedToolbar: false,
127 fullWidthToolbar: false,
136 this.$textElement.html(this.model.get('originalValue'));
140 })(jQuery, _, Drupal);