UNPKG

@atlaskit/editor-plugin-breakout

Version:

Breakout plugin for @atlaskit/editor-core

302 lines (299 loc) 13.3 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { getBreakoutResizableNodeTypes, getBreakoutResizableNodeTypesNew, stepAddsOneOf } from '@atlaskit/editor-common/utils'; import { getChangedNodes, isReplaceDocOperation } from '@atlaskit/editor-common/utils/document'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; import { akEditorDefaultLayoutWidth, akEditorFullWidthLayoutWidth, akEditorMaxWidthLayoutWidth, akEditorCalculatedWideLayoutWidth } from '@atlaskit/editor-shared-styles'; import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled'; import { fg } from '@atlaskit/platform-feature-flags/fg'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments'; import { handleKeyDown } from './handle-key-down'; import { ResizingMarkView } from './resizing-mark-view'; import { updateExpandedStateNew } from './utils/single-player-expand'; const addBreakoutToResizableNode = ({ node, pos, newState, newTr, breakoutResizableNodes, isFullWidthEnabled, isMaxWidthEnabled }) => { let updatedDocChanged = false; let updatedTr = newTr; const $pos = newState.doc.resolve(pos); const isTopLevelNode = ($pos === null || $pos === void 0 ? void 0 : $pos.depth) === 0; if (breakoutResizableNodes.has(node.type) && isTopLevelNode) { const { breakout } = newState.schema.marks; const { expand } = newState.schema.nodes; const breakoutMark = node.marks.find(mark => mark.type === breakout); const isExpand = node.type === expand; if (!breakoutMark) { const width = isMaxWidthEnabled ? akEditorMaxWidthLayoutWidth : isFullWidthEnabled ? akEditorFullWidthLayoutWidth : akEditorDefaultLayoutWidth; if (isExperimentEnabled('platform_editor_lovability_resize_extensions')) { const marks = breakout.create({ width: width }).addToSet(node.marks); updatedTr = newTr.setNodeMarkup(pos, node.type, node.attrs, marks); } else { updatedTr = newTr.setNodeMarkup(pos, node.type, node.attrs, [breakout.create({ width: width })]); } if (isExpand) { updateExpandedStateNew({ tr: updatedTr, node, pos, isLivePage: true }); } updatedDocChanged = true; } else if ((breakoutMark === null || breakoutMark === void 0 ? void 0 : breakoutMark.attrs.width) === null || (breakoutMark === null || breakoutMark === void 0 ? void 0 : breakoutMark.attrs.width) === undefined) { const mode = breakoutMark.attrs.mode; // if breakout is present on node, but page appearance is 'full width' force width to full width to maintain backwards compatibility const width = isMaxWidthEnabled ? akEditorMaxWidthLayoutWidth : isFullWidthEnabled || mode === 'full-width' ? akEditorFullWidthLayoutWidth : akEditorCalculatedWideLayoutWidth; if (isExperimentEnabled('platform_editor_lovability_resize_extensions')) { const marks = breakout.create({ width: width }).addToSet(node.marks); updatedTr = newTr.setNodeMarkup(pos, node.type, node.attrs, marks); } else { updatedTr = newTr.setNodeMarkup(pos, node.type, node.attrs, [breakout.create({ width: width, mode: mode })]); } if (isExpand) { updateExpandedStateNew({ tr: updatedTr, node, pos, isLivePage: true }); } updatedDocChanged = true; } } return { updatedTr, updatedDocChanged }; }; export const resizingPluginKey = new PluginKey('breakout-resizing'); const pluginState = { init() { return { breakoutNode: undefined, activeGuidelineLabel: undefined }; }, apply(tr, pluginState) { const meta = tr.getMeta(resizingPluginKey); if (meta) { switch (meta.type) { case 'UPDATE_BREAKOUT_NODE': return { ...pluginState, breakoutNode: meta.data }; case 'UPDATE_ACTIVE_GUIDELINE_KEY': return { ...pluginState, activeGuidelineKey: meta.data.activeGuidelineKey }; case 'CLEAR_ACTIVE_GUIDELINE_KEY': return { ...pluginState, activeGuidelineKey: undefined }; case 'RESET_STATE': return { activeGuidelineLabel: undefined, breakoutNode: undefined }; } } return pluginState; } }; const requestIdleCallbackWithFallback = callback => { if (typeof requestIdleCallback !== 'undefined') { requestIdleCallback(callback); } else { // fallback to requestAnimationFrame for Safari requestAnimationFrame(callback); } }; export const createResizingPlugin = (api, getIntl, nodeViewPortalProviderAPI, options) => { return new SafePlugin({ key: resizingPluginKey, state: pluginState, props: { markViews: { breakout: (mark, view) => { return new ResizingMarkView(mark, view, api, getIntl, nodeViewPortalProviderAPI); } }, handleKeyDown: handleKeyDown(api) }, view: editorView => { var _api$editorViewMode2; const isPageLoadNormalizationEnabled = expValEquals('platform_editor_add_breakout_marks_on_page_load', 'isEnabled', true) || isExperimentEnabled('platform_editor_lovability_resize_extensions'); const isRuleAndPanelResizingEnabled = expValEquals('platform_editor_lovability_resize_dividers_panels', 'isEnabled', true); const isPanelAndDividerNormalizationEnabled = isRuleAndPanelResizingEnabled && !fg('platform_editor_lovability_resize_patch_1'); if (!isPageLoadNormalizationEnabled && !isPanelAndDividerNormalizationEnabled) { return {}; } /** * This performs a one-time scan of the document to add breakout marks * to nodes that don't have them. It's designed to run only once per * editor instance to avoid performance issues. */ const normalizeBreakoutResizableNodes = () => { var _api$editorViewMode, _api$editorViewMode$s; if ((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') { return; } const isFullWidthEnabled = !((options === null || options === void 0 ? void 0 : options.allowBreakoutButton) === true); const isMaxWidthEnabled = (options === null || options === void 0 ? void 0 : options.appearance) === 'max' && expValEquals('confluence_max_width_content_appearance', 'isEnabled', true) && expValEquals('platform_editor_lovability_breakout_resizing_fixes', 'isEnabled', true); const { state } = editorView; const { expand, codeBlock, layoutSection, rule, panel, panel_c1 } = state.schema.nodes; let breakoutResizableNodes = new Set(); if (isExperimentEnabled('platform_editor_lovability_resize_extensions')) { breakoutResizableNodes = getBreakoutResizableNodeTypesNew(state.schema); } else { breakoutResizableNodes = editorExperiment('platform_synced_block', true) ? getBreakoutResizableNodeTypes(state.schema, isRuleAndPanelResizingEnabled) : isRuleAndPanelResizingEnabled ? new Set([expand, codeBlock, layoutSection, rule, panel, panel_c1]) : new Set([expand, codeBlock, layoutSection]); } let newTr = state.tr; let hasDocChanged = false; // scan the document for top-level resizable nodes state.doc.forEach((node, pos) => { if (!breakoutResizableNodes.has(node.type)) { return; } const { updatedTr, updatedDocChanged } = addBreakoutToResizableNode({ node, pos, newState: state, newTr, breakoutResizableNodes, isFullWidthEnabled, isMaxWidthEnabled }); newTr = updatedTr; hasDocChanged = hasDocChanged || updatedDocChanged; }); if (hasDocChanged) { editorView.dispatch(newTr.setMeta('addToHistory', false)); } }; requestIdleCallbackWithFallback(normalizeBreakoutResizableNodes); const unsubscribeFromViewMode = api === null || api === void 0 ? void 0 : (_api$editorViewMode2 = api.editorViewMode) === null || _api$editorViewMode2 === void 0 ? void 0 : _api$editorViewMode2.sharedState.onChange(({ nextSharedState }) => { if ((nextSharedState === null || nextSharedState === void 0 ? void 0 : nextSharedState.mode) === 'edit') { requestIdleCallbackWithFallback(normalizeBreakoutResizableNodes); } }); return { destroy: () => { unsubscribeFromViewMode === null || unsubscribeFromViewMode === void 0 ? void 0 : unsubscribeFromViewMode(); } }; }, appendTransaction(transactions, oldState, newState) { var _api$editorViewMode3, _api$editorViewMode3$; if ((api === null || api === void 0 ? void 0 : (_api$editorViewMode3 = api.editorViewMode) === null || _api$editorViewMode3 === void 0 ? void 0 : (_api$editorViewMode3$ = _api$editorViewMode3.sharedState.currentState()) === null || _api$editorViewMode3$ === void 0 ? void 0 : _api$editorViewMode3$.mode) === 'view') { return; } let newTr = newState.tr; let hasDocChanged = false; const isRuleAndPanelResizingEnabled = expValEquals('platform_editor_lovability_resize_dividers_panels', 'isEnabled', true); const { expand, codeBlock, layoutSection, rule, panel, panel_c1 } = newState.schema.nodes; let breakoutResizableNodes = new Set(); if (isExperimentEnabled('platform_editor_lovability_resize_extensions')) { breakoutResizableNodes = getBreakoutResizableNodeTypesNew(newState.schema); } else { breakoutResizableNodes = editorExperiment('platform_synced_block', true) ? getBreakoutResizableNodeTypes(newState.schema, isRuleAndPanelResizingEnabled) : isRuleAndPanelResizingEnabled ? new Set([expand, codeBlock, layoutSection, rule, panel, panel_c1]) : new Set([expand, codeBlock, layoutSection]); } const isFullWidthEnabled = !((options === null || options === void 0 ? void 0 : options.allowBreakoutButton) === true); const isMaxWidthEnabled = (options === null || options === void 0 ? void 0 : options.appearance) === 'max' && expValEquals('confluence_max_width_content_appearance', 'isEnabled', true) && expValEquals('platform_editor_lovability_breakout_resizing_fixes', 'isEnabled', true); const isPageLoadNormalizationEnabled = expValEquals('platform_editor_add_breakout_marks_on_page_load', 'isEnabled', true) || isExperimentEnabled('platform_editor_lovability_resize_extensions'); const isPanelAndDividerNormalizationEnabled = isRuleAndPanelResizingEnabled && !fg('platform_editor_lovability_resize_patch_1'); if (!isPageLoadNormalizationEnabled && !isPanelAndDividerNormalizationEnabled && isReplaceDocOperation(transactions, oldState)) { newState.doc.forEach((node, pos) => { const { updatedTr, updatedDocChanged } = addBreakoutToResizableNode({ node, pos, newState, newTr, breakoutResizableNodes, isFullWidthEnabled, isMaxWidthEnabled }); newTr = updatedTr; hasDocChanged = hasDocChanged || updatedDocChanged; }); } else { transactions.forEach(tr => { const isAddingResizableNodes = tr.steps.some(step => stepAddsOneOf(step, breakoutResizableNodes)); if (isAddingResizableNodes) { const changedNodes = getChangedNodes(tr); changedNodes.forEach(({ node, pos }) => { const { updatedTr, updatedDocChanged } = addBreakoutToResizableNode({ node, pos, newState, newTr, breakoutResizableNodes, isFullWidthEnabled, isMaxWidthEnabled }); newTr = updatedTr; hasDocChanged = hasDocChanged || updatedDocChanged; }); } }); } if (hasDocChanged) { // This transaction normalizes a newly inserted node to the editor's default breakout // width. It is not a user resize and must not become an undo step — in particular, a // source synced block can be deleted permanently before Undo reaches this normalization. return fg('platform_editor_breakout_auto_normalization_history_patch') ? newTr.setMeta('addToHistory', false) : newTr; } } }); };