UNPKG

@atlaskit/editor-plugin-breakout

Version:

Breakout plugin for @atlaskit/editor-core

180 lines (173 loc) 10.1 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import { BreakoutCssClassName } from '@atlaskit/editor-common/styles'; import { fg } from '@atlaskit/platform-feature-flags'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { createPragmaticResizer } from './pragmatic-resizer'; import { createResizerCallbacks } from './resizer-callbacks'; export const LOCAL_RESIZE_PROPERTY = '--local-resizing-width'; const RESIZE_HANDLE_TRACK_WIDTH = '7px'; // this function delays the resizer callbacks until the mark view DOM is attached to the DOM const scheduleResizeHandleSetup = callback => { if (typeof window === 'undefined') { return () => {}; } if (typeof window.requestAnimationFrame === 'function') { const frame = window.requestAnimationFrame(callback); return () => window.cancelAnimationFrame(frame); } const timeout = window.setTimeout(callback, 0); return () => window.clearTimeout(timeout); }; 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'); const isResizingExperimentEnabled = expValEquals('platform_editor_lovability_resize_dividers_panels', 'isEnabled', true); // DOM styles // Keep a three-column grid even when the left resize handle is disabled. The empty // left track preserves the original content alignment without translating `contentDOM`, // so floating UI anchored to the node (for example block drag handles) keeps its position. 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 = isResizingExperimentEnabled ? // new code - phantom left track + content + right handle `${RESIZE_HANDLE_TRACK_WIDTH} max(var(--ak-editor--breakout-min-width), min(var(${LOCAL_RESIZE_PROPERTY}, ${mark.attrs.width}px), var(--ak-editor--breakout-fallback-width))) ${RESIZE_HANDLE_TRACK_WIDTH}` : // old code - left handle + content + right handle `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 (isResizingExperimentEnabled) { dom.style.gridTemplateColumns = `${RESIZE_HANDLE_TRACK_WIDTH} auto ${RESIZE_HANDLE_TRACK_WIDTH}`; } 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) { fg('platform_editor_lovability_resize_gracefully') ? this.setupResizerCallbacksIfSupported(dom, contentDOM, view, mark, isResizingExperimentEnabled, api) : 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$nextShar4, _sharedState$nextShar5; if (fg('platform_editor_lovability_resize_gracefully')) { var _sharedState$nextShar2, _sharedState$nextShar3; if (((_sharedState$nextShar2 = sharedState.nextSharedState) === null || _sharedState$nextShar2 === void 0 ? void 0 : _sharedState$nextShar2.mode) === 'view') { var _this$cancelScheduled; (_this$cancelScheduled = this.cancelScheduledResizeHandleSetup) === null || _this$cancelScheduled === void 0 ? void 0 : _this$cancelScheduled.call(this); this.cancelScheduledResizeHandleSetup = undefined; if (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.setupResizerCallbacksIfSupported(dom, contentDOM, view, mark, isResizingExperimentEnabled, api); } } else if (((_sharedState$nextShar4 = sharedState.nextSharedState) === null || _sharedState$nextShar4 === void 0 ? void 0 : _sharedState$nextShar4.mode) === 'view' && this.isResizingInitialised) { var _this$destroyFn2; (_this$destroyFn2 = this.destroyFn) === null || _this$destroyFn2 === void 0 ? void 0 : _this$destroyFn2.call(this, true); this.isResizingInitialised = false; } else if (((_sharedState$nextShar5 = sharedState.nextSharedState) === null || _sharedState$nextShar5 === void 0 ? void 0 : _sharedState$nextShar5.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 }); if (leftHandle) { this.dom.prepend(leftHandle); } this.dom.appendChild(rightHandle); this.destroyFn = destroy; this.isResizingInitialised = true; } setupResizerCallbacksIfSupported(dom, contentDOM, view, mark, isResizingExperimentEnabled, api) { var _this$cancelScheduled2; // cancel any pending setup before scheduling a new one (_this$cancelScheduled2 = this.cancelScheduledResizeHandleSetup) === null || _this$cancelScheduled2 === void 0 ? void 0 : _this$cancelScheduled2.call(this); this.cancelScheduledResizeHandleSetup = undefined; // if panels and rules support breakout resizing, // set up the resizer callbacks if (isResizingExperimentEnabled) { this.setupResizerCallbacks(dom, contentDOM, view, mark, api); return; } // else if panels and rules don't support breakout resizing, // do NOT set up the resizer callbacks (yet) // wait for the mark view DOM to be attached to the DOM first this.cancelScheduledResizeHandleSetup = scheduleResizeHandleSetup(() => { var _view$state$doc$nodeA; this.cancelScheduledResizeHandleSetup = undefined; if (view.isDestroyed || !dom.isConnected) { return; } const pos = view.posAtDOM(dom, 0); const nodeName = (_view$state$doc$nodeA = view.state.doc.nodeAt(pos)) === null || _view$state$doc$nodeA === void 0 ? void 0 : _view$state$doc$nodeA.type.name; // if the node is a panel or a rule, do NOT set up the resizer callbacks if (nodeName && ['panel', 'rule', 'panel_c1'].includes(nodeName) || this.isResizingInitialised) { return; } // else, continue to set up the resizer callbacks this.setupResizerCallbacks(dom, contentDOM, view, mark, api); }); } ignoreMutation() { return true; } destroy() { var _this$destroyFn3, _this$unsubscribeToVi, _this$cancelScheduled3; (_this$destroyFn3 = this.destroyFn) === null || _this$destroyFn3 === void 0 ? void 0 : _this$destroyFn3.call(this); (_this$unsubscribeToVi = this.unsubscribeToViewModeChange) === null || _this$unsubscribeToVi === void 0 ? void 0 : _this$unsubscribeToVi.call(this); (_this$cancelScheduled3 = this.cancelScheduledResizeHandleSetup) === null || _this$cancelScheduled3 === void 0 ? void 0 : _this$cancelScheduled3.call(this); } }