UNPKG

@atlaskit/editor-plugin-layout

Version:

Layout plugin for @atlaskit/editor-core

97 lines (93 loc) 5.01 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.pluginKey = exports.default = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _model = require("@atlaskit/editor-prosemirror/model"); var _state = require("@atlaskit/editor-prosemirror/state"); var _experiments = require("@atlaskit/tmp-editor-statsig/experiments"); var _nodeviews = require("../nodeviews"); 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 pluginKey = exports.pluginKey = new _state.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 LayoutColumnView = /*#__PURE__*/function () { function LayoutColumnView(node, view, getPos) { (0, _classCallCheck2.default)(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 = _model.DOMSerializer.renderSpec(document, nodeType.spec.toDOM(node)), dom = _DOMSerializer$render.dom, contentDOM = _DOMSerializer$render.contentDOM; if (!(dom instanceof HTMLElement) || !(contentDOM instanceof HTMLElement)) { 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 (0, _createClass2.default)(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'; } }]); }(); var _default = exports.default = function _default(options, pluginInjectionApi, portalProviderAPI, eventDispatcher) { return new _safePlugin.SafePlugin({ key: pluginKey, props: { nodeViews: _objectSpread({ layoutSection: function layoutSection(node, view, getPos) { return new _nodeviews.LayoutSectionView({ node: node, view: view, getPos: getPos, portalProviderAPI: portalProviderAPI, eventDispatcher: eventDispatcher, pluginInjectionApi: pluginInjectionApi, options: options }).init(); } }, (0, _experiments.editorExperiment)('platform_editor_layout_column_resize_handle', true) ? { layoutColumn: function layoutColumn(node, view, getPos) { return new LayoutColumnView(node, view, getPos); } } : {}) } }); };