3 * Text editor-based in-place editor for formatted text content in Drupal.
5 * Depends on editor.module. Works with any (WYSIWYG) editor that implements the
6 * editor.js API, including the optional attachInlineEditor() and onChange()
8 * For example, assuming that a hypothetical editor's name was "Magical Editor"
9 * and its editor.js API implementation lived at Drupal.editors.magical, this
10 * JavaScript would use:
11 * - Drupal.editors.magical.attachInlineEditor()
14 (function($, Drupal, drupalSettings, _) {
15 Drupal.quickedit.editors.editor = Drupal.quickedit.EditorView.extend(
16 /** @lends Drupal.quickedit.editors.editor# */ {
18 * The text format for this field.
25 * Indicates whether this text format has transformations.
29 textFormatHasTransformations: null,
32 * Stores a reference to the text editor object for this field.
34 * @type {Drupal.quickedit.EditorModel}
39 * Stores the textual DOM element that is being in-place edited.
48 * @augments Drupal.quickedit.EditorView
50 * @param {object} options
51 * Options for the editor view.
54 Drupal.quickedit.EditorView.prototype.initialize.call(this, options);
56 const metadata = Drupal.quickedit.metadata.get(
57 this.fieldModel.get('fieldID'),
60 this.textFormat = drupalSettings.editor.formats[metadata.format];
61 this.textFormatHasTransformations = metadata.formatHasTransformations;
62 this.textEditor = Drupal.editors[this.textFormat.editor];
64 // Store the actual value of this field. We'll need this to restore the
65 // original value when the user discards his modifications.
66 const $fieldItems = this.$el.find('.quickedit-field');
67 if ($fieldItems.length) {
68 this.$textElement = $fieldItems.eq(0);
70 this.$textElement = this.$el;
72 this.model.set('originalValue', this.$textElement.html());
79 * The text element edited.
82 return this.$textElement;
88 * @param {object} fieldModel
90 * @param {string} state
93 stateChange(fieldModel, state) {
94 const editorModel = this.model;
95 const from = fieldModel.previous('state');
102 // Detach the text editor when entering the 'candidate' state from one
103 // of the states where it could have been attached.
104 if (from !== 'inactive' && from !== 'highlighted') {
105 this.textEditor.detach(this.$textElement.get(0), this.textFormat);
107 // A field model's editor view revert() method is invoked when an
108 // 'active' field becomes a 'candidate' field. But, in the case of
109 // this in-place editor, the content will have been *replaced* if the
110 // text format has transformation filters. Therefore, if we stop
111 // in-place editing this entity, revert explicitly.
112 if (from === 'active' && this.textFormatHasTransformations) {
115 if (from === 'invalid') {
116 this.removeValidationErrors();
124 // When transformation filters have been applied to the formatted text
125 // of this field, then we'll need to load a re-formatted version of it
126 // without the transformation filters.
127 if (this.textFormatHasTransformations) {
128 const $textElement = this.$textElement;
129 this._getUntransformedText(untransformedText => {
130 $textElement.html(untransformedText);
131 fieldModel.set('state', 'active');
134 // When no transformation filters have been applied: start WYSIWYG
135 // editing immediately!
137 // Defer updating the model until the current state change has
138 // propagated, to not trigger a nested state change event.
140 fieldModel.set('state', 'active');
146 const textElement = this.$textElement.get(0);
147 const toolbarView = fieldModel.toolbarView;
148 this.textEditor.attachInlineEditor(
151 toolbarView.getMainWysiwygToolgroupId(),
152 toolbarView.getFloatedWysiwygToolgroupId(),
154 // Set the state to 'changed' whenever the content has changed.
155 this.textEditor.onChange(textElement, htmlText => {
156 editorModel.set('currentValue', htmlText);
157 fieldModel.set('state', 'changed');
166 if (from === 'invalid') {
167 this.removeValidationErrors();
176 this.showValidationErrors();
185 * The settings for the quick edit UI.
187 getQuickEditUISettings() {
190 unifiedToolbar: true,
191 fullWidthToolbar: true,
200 this.$textElement.html(this.model.get('originalValue'));
204 * Loads untransformed text for this field.
206 * More accurately: it re-filters formatted text to exclude transformation
207 * filters used by the text format.
209 * @param {function} callback
210 * A callback function that will receive the untransformed text.
212 * @see \Drupal\editor\Ajax\GetUntransformedTextCommand
214 _getUntransformedText(callback) {
215 const fieldID = this.fieldModel.get('fieldID');
217 // Create a Drupal.ajax instance to load the form.
218 const textLoaderAjax = Drupal.ajax({
219 url: Drupal.quickedit.util.buildUrl(
222 'editor/!entity_type/!id/!field_name/!langcode/!view_mode',
225 submit: { nocssjs: true },
228 // Implement a scoped editorGetUntransformedText AJAX command: calls the
230 textLoaderAjax.commands.editorGetUntransformedText = function(
235 callback(response.data);
238 // This will ensure our scoped editorGetUntransformedText AJAX command
240 textLoaderAjax.execute();
244 })(jQuery, Drupal, drupalSettings, _);