Gutenberg 4 - Add support for detaching Gutenberg and all its side-effects from the editor

Created on 12 June 2024, 7 months ago

Problem/Motivation

As part the 4.0 roadmap, if the aim is to support "Single field editing" in the future, then I think the editor needs to support cleaning up and detaching the Gutenberg editor from the field in case it's loaded dynamically via AJAX.

Proposed resolution

Something along the lines of this could be used as a jumping off point:

Drupal.editors.gutenberg.detach = function(element, format, trigger) {
  let editor = element.nextElementSibling;
  let reactRootContainer;
  if (editor && editor.classList.contains('gutenberg__editor') && editor.hasOwnProperty('_reactRootContainer')) {
    reactRootContainer = editor._reactRootContainer;
  } else {
    console.error('Could not detach Gutenberg editor', element);
    return false;
  }
  if (reactRootContainer) {
    if (trigger === 'serialize') {
      // Update the underlying field value.
      // @todo updateSourceElementValue();
    } else {
      // @todo unmount and revert back to a normal textarea field, as well as all other side-effects.
      element.style.display = ''; // @todo this should've been hidden with a class instead.
      reactRootContainer.unmount();
    }
  }
  return true;
}

Remaining tasks

PR/issue forks.

User interface changes

N/A

API changes

TBD

📌 Task
Status

Active

Version

3.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024