UNPKG

@atlaskit/editor-plugin-breakout

Version:

Breakout plugin for @atlaskit/editor-core

271 lines (267 loc) • 14 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.resizingPluginKey = exports.createResizingPlugin = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _utils = require("@atlaskit/editor-common/utils"); var _document = require("@atlaskit/editor-common/utils/document"); var _state = require("@atlaskit/editor-prosemirror/state"); var _editorSharedStyles = require("@atlaskit/editor-shared-styles"); var _isExperimentEnabled = require("@atlaskit/platform-feature-experiments/is-experiment-enabled"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var _handleKeyDown = require("./handle-key-down"); var _resizingMarkView = require("./resizing-mark-view"); var _singlePlayerExpand = require("./utils/single-player-expand"); 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) { (0, _defineProperty2.default)(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; } 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 ? _editorSharedStyles.akEditorMaxWidthLayoutWidth : isFullWidthEnabled ? _editorSharedStyles.akEditorFullWidthLayoutWidth : _editorSharedStyles.akEditorDefaultLayoutWidth; if ((0, _isExperimentEnabled.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) { (0, _singlePlayerExpand.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 ? _editorSharedStyles.akEditorMaxWidthLayoutWidth : isFullWidthEnabled || mode === 'full-width' ? _editorSharedStyles.akEditorFullWidthLayoutWidth : _editorSharedStyles.akEditorCalculatedWideLayoutWidth; if ((0, _isExperimentEnabled.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) { (0, _singlePlayerExpand.updateExpandedStateNew)({ tr: updatedTr, node: node, pos: pos, isLivePage: true }); } updatedDocChanged = true; } } return { updatedTr: updatedTr, updatedDocChanged: updatedDocChanged }; }; var resizingPluginKey = exports.resizingPluginKey = new _state.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); } }; var createResizingPlugin = exports.createResizingPlugin = function createResizingPlugin(api, getIntl, nodeViewPortalProviderAPI, options) { return new _safePlugin.SafePlugin({ key: resizingPluginKey, state: pluginState, props: { markViews: { breakout: function breakout(mark, view) { return new _resizingMarkView.ResizingMarkView(mark, view, api, getIntl, nodeViewPortalProviderAPI); } }, handleKeyDown: (0, _handleKeyDown.handleKeyDown)(api) }, view: function view(editorView) { var _api$editorViewMode2; var isPageLoadNormalizationEnabled = (0, _expValEquals.expValEquals)('platform_editor_add_breakout_marks_on_page_load', 'isEnabled', true) || (0, _isExperimentEnabled.isExperimentEnabled)('platform_editor_lovability_resize_extensions'); var isRuleAndPanelResizingEnabled = (0, _expValEquals.expValEquals)('platform_editor_lovability_resize_dividers_panels', 'isEnabled', true); if (!isPageLoadNormalizationEnabled && !isRuleAndPanelResizingEnabled) { 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'; var state = editorView.state; var breakoutResizableNodes = (0, _isExperimentEnabled.isExperimentEnabled)('platform_editor_lovability_resize_extensions') ? (0, _utils.getBreakoutResizableNodeTypesNew)(state.schema) : (0, _utils.getBreakoutResizableNodeTypes)(state.schema, isRuleAndPanelResizingEnabled); 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 = (0, _expValEquals.expValEquals)('platform_editor_lovability_resize_dividers_panels', 'isEnabled', true); var breakoutResizableNodes = (0, _isExperimentEnabled.isExperimentEnabled)('platform_editor_lovability_resize_extensions') ? (0, _utils.getBreakoutResizableNodeTypesNew)(newState.schema) : (0, _utils.getBreakoutResizableNodeTypes)(newState.schema, isRuleAndPanelResizingEnabled); var isFullWidthEnabled = !((options === null || options === void 0 ? void 0 : options.allowBreakoutButton) === true); var isMaxWidthEnabled = (options === null || options === void 0 ? void 0 : options.appearance) === 'max'; var isPageLoadNormalizationEnabled = (0, _expValEquals.expValEquals)('platform_editor_add_breakout_marks_on_page_load', 'isEnabled', true) || (0, _isExperimentEnabled.isExperimentEnabled)('platform_editor_lovability_resize_extensions'); if (!isPageLoadNormalizationEnabled && !isRuleAndPanelResizingEnabled && (0, _document.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 (0, _utils.stepAddsOneOf)(step, breakoutResizableNodes); }); if (isAddingResizableNodes) { var changedNodes = (0, _document.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 newTr.setMeta('addToHistory', false); } } }); };