UNPKG

@atlaskit/editor-plugin-layout

Version:

Layout plugin for @atlaskit/editor-core

170 lines (161 loc) • 7.54 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React from 'react'; import { isSSR } from '@atlaskit/editor-common/is-ssr'; import ReactNodeView, { NodeViewContentHole } from '@atlaskit/editor-common/react-node-view'; import { ignoreResizerMutations } from '@atlaskit/editor-common/resizer/BreakoutResizer'; import { DOMSerializer } from '@atlaskit/editor-prosemirror/model'; import { fg } from '@atlaskit/platform-feature-flags/fg'; import { LayoutSSRReactContextsProvider } from '../ui/LayoutSSRReactContextsProvider'; import { isEmptyLayout } from './utils'; var toDOM = function toDOM(node) { return ['div', { class: 'layout-section-container' }, ['div', _objectSpread({ 'data-layout-section': true }, fg('platform_editor_adf_with_localid') && { 'data-local-id': node.attrs.localId }), 0]]; }; /** * */ export var LayoutSectionView = /*#__PURE__*/function (_ReactNodeView) { /** * constructor * @param props * @param props.node * @param props.view * @param props.getPos * @param props.portalProviderAPI * @param props.eventDispatcher * @param props.pluginInjectionApi * @param props.options * @param props.intl * @example */ function LayoutSectionView(props) { var _this; _classCallCheck(this, LayoutSectionView); _this = _callSuper(this, LayoutSectionView, [props.node, props.view, props.getPos, props.portalProviderAPI, props.eventDispatcher, props]); _this.isEmpty = isEmptyLayout(_this.node); _this.options = props.options; _this.intl = props.intl; return _this; } /** * getContentDOM * @example * @returns */ _inherits(LayoutSectionView, _ReactNodeView); return _createClass(LayoutSectionView, [{ key: "getContentDOM", value: function getContentDOM() { // Build the layout DOM via the schema's toDOM spec. This is the same // path used in both CSR and SSR — the only SSR-specific concern is // re-attaching `contentDOM` (= the `[data-layout-section]` element) // after the portal's renderToStaticMarkup + innerHTML write detaches // it. We handle that by stamping `data-ssr-content-dom-ref` on the // outer container so `ReactNodeView.init()` can find a re-attach // target inside `domRef` after the portal write. var _ref = DOMSerializer.renderSpec(document, toDOM(this.node)), container = _ref.dom, contentDOM = _ref.contentDOM; // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting this.layoutDOM = container.querySelector('[data-layout-section]'); this.layoutDOM.setAttribute('data-column-rule-style', this.node.attrs.columnRuleStyle); this.layoutDOM.setAttribute('data-empty-layout', Boolean(this.isEmpty).toString()); if (fg('platform_editor_adf_with_localid')) { this.layoutDOM.setAttribute('data-local-id', this.node.attrs.localId); } // SSR streaming re-attach note: // In SSR, `init()` appends `container` into `domRef`; the portal's // renderToStaticMarkup + innerHTML write then wipes `domRef`, // detaching the entire subtree (with PM-serialized children inside // `[data-layout-section]`). React's `render()` emits a // `<NodeViewContentHole/>` placeholder inside `domRef`; the SSR // re-attach logic in `init()` finds it via `[data-ssr-content-dom-ref]` // and calls `_handleRef`, which appends `contentDOMWrapper` (the // detached `container`) back inside the placeholder. The end result // is `domRef > NodeViewContentHole > layout-section-container > // [data-layout-section] > [data-layout-column] children` — the // layout DOM contract is preserved. return { dom: container, contentDOM: contentDOM }; } /** * setDomAttrs * @param node * @param element * @example */ }, { key: "setDomAttrs", value: function setDomAttrs(node, _element) { if (this.layoutDOM) { this.layoutDOM.setAttribute('data-column-rule-style', node.attrs.columnRuleStyle); } } /** * render * @param props * @param forwardRef * @example * @returns */ }, { key: "render", value: function render(props, forwardRef) { this.isEmpty = isEmptyLayout(this.node); if (this.layoutDOM) { this.layoutDOM.setAttribute('data-empty-layout', Boolean(this.isEmpty).toString()); } // SSR streaming path: render only a `<NodeViewContentHole/>` placeholder // so ReactNodeView.init()'s SSR re-attach logic can find the marker // (`data-ssr-content-dom-ref`) and re-append the detached // contentDOMWrapper — which is the FULL layout structure // (`layout-section-container > [data-layout-section] > children`) built // in `getContentDOM` via DOMSerializer.renderSpec. This avoids // duplicating layout structure between getContentDOM and render(), which // previously caused an extra wrapping div between `[data-layout-section]` // and the `[data-layout-column]` children and broke the flex layout. // // The BreakoutResizer is intentionally omitted in SSR — it relies on // browser-only APIs and contributes no useful static markup. The // LayoutSSRReactContextsProvider wraps the placeholder to inject the // editor's IntlShape, defending against any descendants that call // `useIntl()` during renderToStaticMarkup. if (isSSR()) { return /*#__PURE__*/React.createElement(LayoutSSRReactContextsProvider, { intl: this.intl }, /*#__PURE__*/React.createElement(NodeViewContentHole, { ref: forwardRef })); } return null; } /** * ignoreMutation * @param mutation * @example * @returns */ }, { key: "ignoreMutation", value: function ignoreMutation(mutation) { return ignoreResizerMutations(mutation); } }]); }(ReactNodeView);