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(/** @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(this.fieldModel.get('fieldID'), 'custom');
57 this.textFormat = drupalSettings.editor.formats[metadata.format];
58 this.textFormatHasTransformations = metadata.formatHasTransformations;
59 this.textEditor = Drupal.editors[this.textFormat.editor];
61 // Store the actual value of this field. We'll need this to restore the
62 // original value when the user discards his modifications.
63 const $fieldItems = this.$el.find('.quickedit-field');
64 if ($fieldItems.length) {
65 this.$textElement = $fieldItems.eq(0);
68 this.$textElement = this.$el;
70 this.model.set('originalValue', this.$textElement.html());
77 * The text element edited.
80 return this.$textElement;
86 * @param {object} fieldModel
88 * @param {string} state
91 stateChange(fieldModel, state) {
92 const editorModel = this.model;
93 const from = fieldModel.previous('state');
100 // Detach the text editor when entering the 'candidate' state from one
101 // of the states where it could have been attached.
102 if (from !== 'inactive' && from !== 'highlighted') {
103 this.textEditor.detach(this.$textElement.get(0), this.textFormat);
105 // A field model's editor view revert() method is invoked when an
106 // 'active' field becomes a 'candidate' field. But, in the case of
107 // this in-place editor, the content will have been *replaced* if the
108 // text format has transformation filters. Therefore, if we stop
109 // in-place editing this entity, revert explicitly.
110 if (from === 'active' && this.textFormatHasTransformations) {
113 if (from === 'invalid') {
114 this.removeValidationErrors();
122 // When transformation filters have been applied to the formatted text
123 // of this field, then we'll need to load a re-formatted version of it
124 // without the transformation filters.
125 if (this.textFormatHasTransformations) {
126 const $textElement = this.$textElement;
127 this._getUntransformedText((untransformedText) => {
128 $textElement.html(untransformedText);
129 fieldModel.set('state', 'active');
132 // When no transformation filters have been applied: start WYSIWYG
133 // editing immediately!
135 // Defer updating the model until the current state change has
136 // propagated, to not trigger a nested state change event.
138 fieldModel.set('state', 'active');
144 var textElement = this.$textElement.get(0);
145 var toolbarView = fieldModel.toolbarView;
146 this.textEditor.attachInlineEditor(
149 toolbarView.getMainWysiwygToolgroupId(),
150 toolbarView.getFloatedWysiwygToolgroupId(),
152 // Set the state to 'changed' whenever the content has changed.
153 this.textEditor.onChange(textElement, (htmlText) => {
154 editorModel.set('currentValue', htmlText);
155 fieldModel.set('state', 'changed');
163 if (from === 'invalid') {
164 this.removeValidationErrors();
173 this.showValidationErrors();
182 * The sttings for the quick edit UI.
184 getQuickEditUISettings() {
185 return { padding: true, unifiedToolbar: true, fullWidthToolbar: true, popup: false };
192 this.$textElement.html(this.model.get('originalValue'));
196 * Loads untransformed text for this field.
198 * More accurately: it re-filters formatted text to exclude transformation
199 * filters used by the text format.
201 * @param {function} callback
202 * A callback function that will receive the untransformed text.
204 * @see \Drupal\editor\Ajax\GetUntransformedTextCommand
206 _getUntransformedText(callback) {
207 const fieldID = this.fieldModel.get('fieldID');
209 // Create a Drupal.ajax instance to load the form.
210 const textLoaderAjax = Drupal.ajax({
211 url: Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('editor/!entity_type/!id/!field_name/!langcode/!view_mode')),
212 submit: { nocssjs: true },
215 // Implement a scoped editorGetUntransformedText AJAX command: calls the
217 textLoaderAjax.commands.editorGetUntransformedText = function (ajax, response, status) {
218 callback(response.data);
221 // This will ensure our scoped editorGetUntransformedText AJAX command
223 textLoaderAjax.execute();
227 }(jQuery, Drupal, drupalSettings, _));