UNPKG

@atlaskit/editor-plugin-block-controls

Version:

Block controls plugin for @atlaskit/editor-core

69 lines (65 loc) 3.15 kB
"use strict"; var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.unmountFromMountPoint = exports.renderToMountPoint = void 0; var _reactDom = _interopRequireWildcard(require("react-dom")); var _client = require("react-dom/client"); var _platformFeatureFlags = require("@atlaskit/platform-feature-flags"); 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); } /** * Shared registry of React roots created via `createRoot`. * * Both `decorations-drag-handle.ts` (render) and `decorations-common.ts` * (unmount) must operate on the **same** root for a given DOM element, so * the WeakMap lives here and is imported by both modules. */ var reactRoots = new WeakMap(); /** * Mounts `element` into `mountPoint`. * * When `nike_r19_render_unmount` is **on**, uses the React 18/19 `createRoot` * API wrapped in `flushSync` so the render is synchronous – matching the * legacy `ReactDOM.render` timing that ProseMirror decoration widgets depend * on (focus management, anchor positioning, block-menu visibility all read * the rendered DOM immediately after the callback returns). * * When the gate is **off**, falls back to the legacy `ReactDOM.render` path. */ var renderToMountPoint = exports.renderToMountPoint = function renderToMountPoint(element, mountPoint) { if ((0, _platformFeatureFlags.fg)('nike_r19_render_unmount')) { var root = reactRoots.get(mountPoint); if (!root) { root = (0, _client.createRoot)(mountPoint); reactRoots.set(mountPoint, root); } var activeRoot = root; // flushSync makes createRoot render synchronously, matching legacy // ReactDOM.render timing that the widget/focus/positioning code depends on. (0, _reactDom.flushSync)(function () { return activeRoot.render(element); }); } else { _reactDom.default.render(element, mountPoint); } }; /** * Unmounts the React tree at `mountPoint`. * * When `nike_r19_render_unmount` is **on**, looks up the root in the shared * registry, calls `root.unmount()`, and removes the entry. * * When the gate is **off**, falls back to `ReactDOM.unmountComponentAtNode`. */ var unmountFromMountPoint = exports.unmountFromMountPoint = function unmountFromMountPoint(mountPoint) { if ((0, _platformFeatureFlags.fg)('nike_r19_render_unmount')) { var root = reactRoots.get(mountPoint); if (root) { root.unmount(); reactRoots.delete(mountPoint); } } else { _reactDom.default.unmountComponentAtNode(mountPoint); } };