UNPKG

@atlaskit/editor-plugin-breakout

Version:

Breakout plugin for @atlaskit/editor-core

101 lines (100 loc) 5.34 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import { BreakoutCssClassName } from '@atlaskit/editor-common/styles'; import { createPragmaticResizer } from './pragmatic-resizer'; import { createResizerCallbacks } from './resizer-callbacks'; export const LOCAL_RESIZE_PROPERTY = '--local-resizing-width'; export class ResizingMarkView { /** * 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 * ``` */ constructor(mark, view, api, getIntl, nodeViewPortalProviderAPI) { var _api$editorViewMode, _api$editorViewMode$s, _api$editorViewMode2; _defineProperty(this, "isResizingInitialised", false); const dom = document.createElement('div'); const 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(${LOCAL_RESIZE_PROPERTY}, ${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(${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(${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; const isLiveViewMode = (api === null || api === void 0 ? void 0 : (_api$editorViewMode = api.editorViewMode) === null || _api$editorViewMode === void 0 ? void 0 : (_api$editorViewMode$s = _api$editorViewMode.sharedState.currentState()) === null || _api$editorViewMode$s === void 0 ? void 0 : _api$editorViewMode$s.mode) === 'view'; if (!isLiveViewMode) { this.setupResizerCallbacks(dom, contentDOM, view, mark, api); } this.unsubscribeToViewModeChange = api === null || api === void 0 ? void 0 : (_api$editorViewMode2 = api.editorViewMode) === null || _api$editorViewMode2 === void 0 ? void 0 : _api$editorViewMode2.sharedState.onChange(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 ? 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); } } }); } setupResizerCallbacks(dom, contentDOM, view, mark, api) { const callbacks = createResizerCallbacks({ dom, view, mark, api }); const { leftHandle, rightHandle, destroy } = createPragmaticResizer({ target: contentDOM, ...callbacks, intl: this.intl, nodeViewPortalProviderAPI: this.nodeViewPortalProviderAPI }); this.dom.prepend(leftHandle); this.dom.appendChild(rightHandle); this.destroyFn = destroy; this.isResizingInitialised = true; } ignoreMutation() { return true; } destroy() { var _this$destroyFn2, _this$unsubscribeToVi; (_this$destroyFn2 = this.destroyFn) === null || _this$destroyFn2 === void 0 ? void 0 : _this$destroyFn2.call(this); (_this$unsubscribeToVi = this.unsubscribeToViewModeChange) === null || _this$unsubscribeToVi === void 0 ? void 0 : _this$unsubscribeToVi.call(this); } }