UNPKG

@atlaskit/editor-plugin-layout

Version:

Layout plugin for @atlaskit/editor-core

122 lines (119 loc) 5.99 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _typeof from "@babel/runtime/helpers/typeof"; import { isSSR } from '@atlaskit/editor-common/core-utils'; import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { DOMSerializer } from '@atlaskit/editor-prosemirror/model'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { LayoutSectionView } from '../nodeviews'; import { LayoutSectionView as LayoutSectionViewVanilla } from '../nodeviews/layoutSectionView'; export var pluginKey = new PluginKey('layoutResizingPlugin'); /** * Minimal node view for layoutColumn that delegates all DOM serialization to the * NodeSpec's own toDOM, but overrides ignoreMutation to suppress style attribute * mutations from ProseMirror's MutationObserver. * * This is necessary so that direct inline style mutations during column drag * (e.g. setting flex-basis to give real-time visual feedback without dispatching * PM transactions) are not "corrected" back by ProseMirror's DOM reconciliation. */ var isLayoutElementLike = function isLayoutElementLike(element) { if (isSSR()) { // In SSR environments, `HTMLElement` is undefined globally so a plain // `instanceof HTMLElement` check is always `false`. That makes the // `DOMSerializer.renderSpec(...)` result get rejected by the guard below and // the NodeView falls back to a bare `<div>`, losing every schema-defined // attribute (`data-layout-column`, `style="flex-basis:..."`, // `data-column-width`, plus the inner `<div data-layout-content="true">` // wrapper) and breaking the layout's flex sizing in SSR output. // // To unblock SSR without changing CSR semantics, we gate the check: // - In SSR, use a duck-typed check that mirrors `safe-plugin`'s `isHTMLElement`. // - Everywhere else, keep the original `instanceof HTMLElement` check exactly // as it was so we don't accidentally widen acceptance in CSR. if (element === null || element === undefined) { return false; } return _typeof(element) === 'object' && 'innerHTML' in element && 'style' in element && 'classList' in element; } return element instanceof HTMLElement; }; var LayoutColumnView = /*#__PURE__*/function () { function LayoutColumnView(node, view, getPos) { _classCallCheck(this, LayoutColumnView); // Use the NodeSpec's own toDOM to produce the correct DOM structure and attributes. var nodeType = view.state.schema.nodes[node.type.name]; // Fallback: create a plain div so PM always has a valid DOM node to work with. // This path should never be reached in practice — layoutColumn always has a toDOM. if (!nodeType.spec.toDOM) { var fallbackDiv = document.createElement('div'); this.dom = fallbackDiv; this.contentDOM = fallbackDiv; return; } var _DOMSerializer$render = DOMSerializer.renderSpec(document, nodeType.spec.toDOM(node)), dom = _DOMSerializer$render.dom, contentDOM = _DOMSerializer$render.contentDOM; if (!isLayoutElementLike(dom) || !isLayoutElementLike(contentDOM)) { var _fallbackDiv = document.createElement('div'); this.dom = _fallbackDiv; this.contentDOM = _fallbackDiv; return; } this.dom = dom; this.contentDOM = contentDOM; // Stamp the column's index within its parent section onto the DOM element so that // column-resize-divider can query columns by index rather than relying on positional // order of [data-layout-column] elements (which could break if the DOM structure changes). var pos = getPos(); if (pos !== undefined) { var $pos = view.state.doc.resolve(pos); this.dom.setAttribute('data-layout-column-index', String($pos.index())); } } return _createClass(LayoutColumnView, [{ key: "ignoreMutation", value: function ignoreMutation(mutation) { // Ignore style attribute mutations — these are direct DOM writes during column drag // (setting flex-basis for real-time resize feedback). Without this, PM's // MutationObserver would immediately revert our style changes. return mutation.type === 'attributes' && mutation.attributeName === 'style'; } }]); }(); export default (function (options, pluginInjectionApi, portalProviderAPI, eventDispatcher, intl) { return new SafePlugin({ key: pluginKey, props: { nodeViews: { layoutSection: function layoutSection(node, view, getPos) { // Both experiments are required: the vanilla view renders no React, so it // cannot mount the <LayoutBreakoutResizer> that LayoutSectionView.render() // returns. That resizer is only redundant once // `platform_editor_breakout_resizing` is on (render() returns null and // breakout resizing is handled by the pragmatic resizer in // editor-plugin-breakout). Taking the vanilla path while that experiment is // off would silently remove the breakout drag handles. if (expValEquals('platform_editor_breakout_resizing', 'isEnabled', true) && isExperimentEnabled('platform_editor_vanilla_node_views_phase1')) { return new LayoutSectionViewVanilla(node); } return new LayoutSectionView({ node: node, view: view, getPos: getPos, portalProviderAPI: portalProviderAPI, eventDispatcher: eventDispatcher, pluginInjectionApi: pluginInjectionApi, options: options, intl: intl }).init(); }, layoutColumn: function layoutColumn(node, view, getPos) { return new LayoutColumnView(node, view, getPos); } } } }); });