UNPKG

@atlaskit/editor-plugin-layout

Version:

Layout plugin for @atlaskit/editor-core

179 lines (169 loc) • 8.9 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.LayoutSectionView = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireDefault(require("react")); var _isSsr = require("@atlaskit/editor-common/is-ssr"); var _reactNodeView = _interopRequireWildcard(require("@atlaskit/editor-common/react-node-view")); var _BreakoutResizer = require("@atlaskit/editor-common/resizer/BreakoutResizer"); var _model = require("@atlaskit/editor-prosemirror/model"); var _fg = require("@atlaskit/platform-feature-flags/fg"); var _LayoutSSRReactContextsProvider = require("../ui/LayoutSSRReactContextsProvider"); var _utils = require("./utils"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(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) { (0, _defineProperty2.default)(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; } var toDOM = function toDOM(node) { return ['div', { class: 'layout-section-container' }, ['div', _objectSpread({ 'data-layout-section': true }, (0, _fg.fg)('platform_editor_adf_with_localid') && { 'data-local-id': node.attrs.localId }), 0]]; }; /** * */ var LayoutSectionView = exports.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; (0, _classCallCheck2.default)(this, LayoutSectionView); _this = _callSuper(this, LayoutSectionView, [props.node, props.view, props.getPos, props.portalProviderAPI, props.eventDispatcher, props]); _this.isEmpty = (0, _utils.isEmptyLayout)(_this.node); _this.options = props.options; _this.intl = props.intl; return _this; } /** * getContentDOM * @example * @returns */ (0, _inherits2.default)(LayoutSectionView, _ReactNodeView); return (0, _createClass2.default)(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 = _model.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 ((0, _fg.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 = (0, _utils.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 ((0, _isSsr.isSSR)()) { return /*#__PURE__*/_react.default.createElement(_LayoutSSRReactContextsProvider.LayoutSSRReactContextsProvider, { intl: this.intl }, /*#__PURE__*/_react.default.createElement(_reactNodeView.NodeViewContentHole, { ref: forwardRef })); } return null; } /** * ignoreMutation * @param mutation * @example * @returns */ }, { key: "ignoreMutation", value: function ignoreMutation(mutation) { return (0, _BreakoutResizer.ignoreResizerMutations)(mutation); } }]); }(_reactNodeView.default);