UNPKG

@atlaskit/editor-plugin-breakout

Version:

Breakout plugin for @atlaskit/editor-core

114 lines (113 loc) 6.68 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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 { BreakoutCssClassName } from '@atlaskit/editor-common/styles'; import { createPragmaticResizer } from './pragmatic-resizer'; import { createResizerCallbacks } from './resizer-callbacks'; export var LOCAL_RESIZE_PROPERTY = '--local-resizing-width'; export var ResizingMarkView = /*#__PURE__*/function () { /** * Wrap node view in a resizing mark view * @param {Mark} mark - The breakout mark to resize * @param {EditorView} view - The editor view * @param {ExtractInjectionAPI<BreakoutPlugin> | undefined} api - The pluginInjectionAPI * @param {Function} getIntl - () => IntlShape * @param {PortalProviderAPI} - The nodeViewPortalProviderAPI * @example * ```ts * ``` */ function ResizingMarkView(mark, view, api, getIntl, nodeViewPortalProviderAPI) { var _api$editorViewMode, _api$editorViewMode2, _this = this; _classCallCheck(this, ResizingMarkView); _defineProperty(this, "isResizingInitialised", false); var dom = document.createElement('div'); var contentDOM = document.createElement('div'); contentDOM.className = BreakoutCssClassName.BREAKOUT_MARK_DOM; contentDOM.setAttribute('data-testid', 'ak-editor-breakout-mark-dom'); dom.className = BreakoutCssClassName.BREAKOUT_MARK; dom.setAttribute('data-layout', mark.attrs.mode); dom.setAttribute('data-testid', 'ak-editor-breakout-mark'); // dom - styles dom.style.transform = 'none'; dom.style.display = 'grid'; dom.style.justifyContent = 'center'; // contentDOM - styles contentDOM.style.gridColumn = '2'; contentDOM.style.zIndex = '1'; if (mark.attrs.width) { dom.style.gridTemplateColumns = "auto max(var(--ak-editor--breakout-min-width), min(var(".concat(LOCAL_RESIZE_PROPERTY, ", ").concat(mark.attrs.width, "px), var(--ak-editor--breakout-fallback-width))) auto"); } else { if (mark.attrs.mode === 'wide') { contentDOM.style.width = "max(var(--ak-editor--line-length), min(var(".concat(LOCAL_RESIZE_PROPERTY, ", var(--ak-editor--breakout-wide-layout-width)), calc(100cqw - var(--ak-editor--breakout-full-page-guttering-padding))))"); } if (mark.attrs.mode === 'full-width') { contentDOM.style.width = "max(var(--ak-editor--line-length), min(var(".concat(LOCAL_RESIZE_PROPERTY, ", var(--ak-editor--full-width-layout-width)), calc(100cqw - var(--ak-editor--breakout-full-page-guttering-padding))))"); } } dom.appendChild(contentDOM); this.dom = dom; this.contentDOM = contentDOM; this.view = view; this.mark = mark; this.intl = getIntl(); this.nodeViewPortalProviderAPI = nodeViewPortalProviderAPI; var isLiveViewMode = (api === null || api === void 0 || (_api$editorViewMode = api.editorViewMode) === null || _api$editorViewMode === void 0 || (_api$editorViewMode = _api$editorViewMode.sharedState.currentState()) === null || _api$editorViewMode === void 0 ? void 0 : _api$editorViewMode.mode) === 'view'; if (!isLiveViewMode) { this.setupResizerCallbacks(dom, contentDOM, view, mark, api); } this.unsubscribeToViewModeChange = api === null || api === void 0 || (_api$editorViewMode2 = api.editorViewMode) === null || _api$editorViewMode2 === void 0 ? void 0 : _api$editorViewMode2.sharedState.onChange(function (sharedState) { var _sharedState$nextShar, _sharedState$prevShar; if (((_sharedState$nextShar = sharedState.nextSharedState) === null || _sharedState$nextShar === void 0 ? void 0 : _sharedState$nextShar.mode) !== ((_sharedState$prevShar = sharedState.prevSharedState) === null || _sharedState$prevShar === void 0 ? void 0 : _sharedState$prevShar.mode)) { var _sharedState$nextShar2, _sharedState$nextShar3; if (((_sharedState$nextShar2 = sharedState.nextSharedState) === null || _sharedState$nextShar2 === void 0 ? void 0 : _sharedState$nextShar2.mode) === 'view' && _this.isResizingInitialised) { var _this$destroyFn; (_this$destroyFn = _this.destroyFn) === null || _this$destroyFn === void 0 || _this$destroyFn.call(_this, true); _this.isResizingInitialised = false; } else if (((_sharedState$nextShar3 = sharedState.nextSharedState) === null || _sharedState$nextShar3 === void 0 ? void 0 : _sharedState$nextShar3.mode) === 'edit' && !_this.isResizingInitialised) { _this.setupResizerCallbacks(dom, contentDOM, view, mark, api); } } }); } return _createClass(ResizingMarkView, [{ key: "setupResizerCallbacks", value: function setupResizerCallbacks(dom, contentDOM, view, mark, api) { var callbacks = createResizerCallbacks({ dom: dom, view: view, mark: mark, api: api }); var _createPragmaticResiz = createPragmaticResizer(_objectSpread(_objectSpread({ target: contentDOM }, callbacks), {}, { intl: this.intl, nodeViewPortalProviderAPI: this.nodeViewPortalProviderAPI })), leftHandle = _createPragmaticResiz.leftHandle, rightHandle = _createPragmaticResiz.rightHandle, destroy = _createPragmaticResiz.destroy; this.dom.prepend(leftHandle); this.dom.appendChild(rightHandle); this.destroyFn = destroy; this.isResizingInitialised = true; } }, { key: "ignoreMutation", value: function ignoreMutation() { return true; } }, { key: "destroy", value: function destroy() { var _this$destroyFn2, _this$unsubscribeToVi; (_this$destroyFn2 = this.destroyFn) === null || _this$destroyFn2 === void 0 || _this$destroyFn2.call(this); (_this$unsubscribeToVi = this.unsubscribeToViewModeChange) === null || _this$unsubscribeToVi === void 0 || _this$unsubscribeToVi.call(this); } }]); }();