UNPKG

@atlaskit/editor-plugin-breakout

Version:

Breakout plugin for @atlaskit/editor-core

292 lines (289 loc) 15.5 kB
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 { 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'; var addBreakoutToResizableNode = function addBreakoutToResizableNode(_ref) { var node = _ref.node, pos = _ref.pos, newState = _ref.newState, newTr = _ref.newTr, breakoutResizableNodes = _ref.breakoutResizableNodes, isFullWidthEnabled = _ref.isFullWidthEnabled, isMaxWidthEnabled = _ref.isMaxWidthEnabled; var updatedDocChanged = false; var updatedTr = newTr; var $pos = newState.doc.resolve(pos); var isTopLevelNode = ($pos === null || $pos === void 0 ? void 0 : $pos.depth) === 0; if (breakoutResizableNodes.has(node.type) && isTopLevelNode) { var breakout = newState.schema.marks.breakout; var expand = newState.schema.nodes.expand; var breakoutMark = node.marks.find(function (mark) { return mark.type === breakout; }); var isExpand = node.type === expand; if (!breakoutMark) { var width = isMaxWidthEnabled ? akEditorMaxWidthLayoutWidth : isFullWidthEnabled ? akEditorFullWidthLayoutWidth : akEditorDefaultLayoutWidth; if (isExperimentEnabled('platform_editor_lovability_resize_extensions')) { var 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: node, pos: 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) { var 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 var _width = isMaxWidthEnabled ? akEditorMaxWidthLayoutWidth : isFullWidthEnabled || mode === 'full-width' ? akEditorFullWidthLayoutWidth : akEditorCalculatedWideLayoutWidth; if (isExperimentEnabled('platform_editor_lovability_resize_extensions')) { var _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: node, pos: pos, isLivePage: true }); } updatedDocChanged = true; } } return { updatedTr: updatedTr, updatedDocChanged: updatedDocChanged }; }; export var resizingPluginKey = new PluginKey('breakout-resizing'); var pluginState = { init: function init() { return { breakoutNode: undefined, activeGuidelineLabel: undefined }; }, apply: function apply(tr, pluginState) { var meta = tr.getMeta(resizingPluginKey); if (meta) { switch (meta.type) { case 'UPDATE_BREAKOUT_NODE': return _objectSpread(_objectSpread({}, pluginState), {}, { breakoutNode: meta.data }); case 'UPDATE_ACTIVE_GUIDELINE_KEY': return _objectSpread(_objectSpread({}, pluginState), {}, { activeGuidelineKey: meta.data.activeGuidelineKey }); case 'CLEAR_ACTIVE_GUIDELINE_KEY': return _objectSpread(_objectSpread({}, pluginState), {}, { activeGuidelineKey: undefined }); case 'RESET_STATE': return { activeGuidelineLabel: undefined, breakoutNode: undefined }; } } return pluginState; } }; var requestIdleCallbackWithFallback = function requestIdleCallbackWithFallback(callback) { if (typeof requestIdleCallback !== 'undefined') { requestIdleCallback(callback); } else { // fallback to requestAnimationFrame for Safari requestAnimationFrame(callback); } }; export var createResizingPlugin = function createResizingPlugin(api, getIntl, nodeViewPortalProviderAPI, options) { return new SafePlugin({ key: resizingPluginKey, state: pluginState, props: { markViews: { breakout: function breakout(mark, view) { return new ResizingMarkView(mark, view, api, getIntl, nodeViewPortalProviderAPI); } }, handleKeyDown: handleKeyDown(api) }, view: function view(editorView) { var _api$editorViewMode2; var isPageLoadNormalizationEnabled = expValEquals('platform_editor_add_breakout_marks_on_page_load', 'isEnabled', true) || isExperimentEnabled('platform_editor_lovability_resize_extensions'); var isRuleAndPanelResizingEnabled = expValEquals('platform_editor_lovability_resize_dividers_panels', 'isEnabled', true); var 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. */ var normalizeBreakoutResizableNodes = function normalizeBreakoutResizableNodes() { var _api$editorViewMode; if ((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') { return; } var isFullWidthEnabled = !((options === null || options === void 0 ? void 0 : options.allowBreakoutButton) === true); var 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); var state = editorView.state; var _state$schema$nodes = state.schema.nodes, expand = _state$schema$nodes.expand, codeBlock = _state$schema$nodes.codeBlock, layoutSection = _state$schema$nodes.layoutSection, rule = _state$schema$nodes.rule, panel = _state$schema$nodes.panel, panel_c1 = _state$schema$nodes.panel_c1; var 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]); } var newTr = state.tr; var hasDocChanged = false; // scan the document for top-level resizable nodes state.doc.forEach(function (node, pos) { if (!breakoutResizableNodes.has(node.type)) { return; } var _addBreakoutToResizab = addBreakoutToResizableNode({ node: node, pos: pos, newState: state, newTr: newTr, breakoutResizableNodes: breakoutResizableNodes, isFullWidthEnabled: isFullWidthEnabled, isMaxWidthEnabled: isMaxWidthEnabled }), updatedTr = _addBreakoutToResizab.updatedTr, updatedDocChanged = _addBreakoutToResizab.updatedDocChanged; newTr = updatedTr; hasDocChanged = hasDocChanged || updatedDocChanged; }); if (hasDocChanged) { editorView.dispatch(newTr.setMeta('addToHistory', false)); } }; requestIdleCallbackWithFallback(normalizeBreakoutResizableNodes); var unsubscribeFromViewMode = api === null || api === void 0 || (_api$editorViewMode2 = api.editorViewMode) === null || _api$editorViewMode2 === void 0 ? void 0 : _api$editorViewMode2.sharedState.onChange(function (_ref2) { var nextSharedState = _ref2.nextSharedState; if ((nextSharedState === null || nextSharedState === void 0 ? void 0 : nextSharedState.mode) === 'edit') { requestIdleCallbackWithFallback(normalizeBreakoutResizableNodes); } }); return { destroy: function destroy() { unsubscribeFromViewMode === null || unsubscribeFromViewMode === void 0 || unsubscribeFromViewMode(); } }; }, appendTransaction: function appendTransaction(transactions, oldState, newState) { var _api$editorViewMode3; if ((api === null || api === void 0 || (_api$editorViewMode3 = api.editorViewMode) === null || _api$editorViewMode3 === void 0 || (_api$editorViewMode3 = _api$editorViewMode3.sharedState.currentState()) === null || _api$editorViewMode3 === void 0 ? void 0 : _api$editorViewMode3.mode) === 'view') { return; } var newTr = newState.tr; var hasDocChanged = false; var isRuleAndPanelResizingEnabled = expValEquals('platform_editor_lovability_resize_dividers_panels', 'isEnabled', true); var _newState$schema$node = newState.schema.nodes, expand = _newState$schema$node.expand, codeBlock = _newState$schema$node.codeBlock, layoutSection = _newState$schema$node.layoutSection, rule = _newState$schema$node.rule, panel = _newState$schema$node.panel, panel_c1 = _newState$schema$node.panel_c1; var 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]); } var isFullWidthEnabled = !((options === null || options === void 0 ? void 0 : options.allowBreakoutButton) === true); var 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); var isPageLoadNormalizationEnabled = expValEquals('platform_editor_add_breakout_marks_on_page_load', 'isEnabled', true) || isExperimentEnabled('platform_editor_lovability_resize_extensions'); var isPanelAndDividerNormalizationEnabled = isRuleAndPanelResizingEnabled && !fg('platform_editor_lovability_resize_patch_1'); if (!isPageLoadNormalizationEnabled && !isPanelAndDividerNormalizationEnabled && isReplaceDocOperation(transactions, oldState)) { newState.doc.forEach(function (node, pos) { var _addBreakoutToResizab2 = addBreakoutToResizableNode({ node: node, pos: pos, newState: newState, newTr: newTr, breakoutResizableNodes: breakoutResizableNodes, isFullWidthEnabled: isFullWidthEnabled, isMaxWidthEnabled: isMaxWidthEnabled }), updatedTr = _addBreakoutToResizab2.updatedTr, updatedDocChanged = _addBreakoutToResizab2.updatedDocChanged; newTr = updatedTr; hasDocChanged = hasDocChanged || updatedDocChanged; }); } else { transactions.forEach(function (tr) { var isAddingResizableNodes = tr.steps.some(function (step) { return stepAddsOneOf(step, breakoutResizableNodes); }); if (isAddingResizableNodes) { var changedNodes = getChangedNodes(tr); changedNodes.forEach(function (_ref3) { var node = _ref3.node, pos = _ref3.pos; var _addBreakoutToResizab3 = addBreakoutToResizableNode({ node: node, pos: pos, newState: newState, newTr: newTr, breakoutResizableNodes: breakoutResizableNodes, isFullWidthEnabled: isFullWidthEnabled, isMaxWidthEnabled: isMaxWidthEnabled }), updatedTr = _addBreakoutToResizab3.updatedTr, updatedDocChanged = _addBreakoutToResizab3.updatedDocChanged; 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; } } }); };