UNPKG

@atlaskit/editor-plugin-selection

Version:

Selection plugin for @atlaskit/editor-core

115 lines (112 loc) 5.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getInitialState = exports.createPlugin = void 0; var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _state = require("@atlaskit/editor-prosemirror/state"); var _view = require("@atlaskit/editor-prosemirror/view"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var _experiments = require("@atlaskit/tmp-editor-statsig/experiments"); var _types = require("../types"); var _actions = require("./actions"); var _createSelectionBetween = require("./events/create-selection-between"); var _keydown = require("./events/keydown"); var _mouseout = require("./events/mouseout"); var _pluginFactory = require("./plugin-factory"); var _utils = require("./utils"); var getInitialState = exports.getInitialState = function getInitialState(state) { return { decorationSet: (0, _utils.getDecorations)(state.tr), selection: state.selection }; }; var createPlugin = exports.createPlugin = function createPlugin(api, dispatch, dispatchAnalyticsEvent) { var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; var cursorHidden = false; var blockSelection; return new _safePlugin.SafePlugin({ key: _types.selectionPluginKey, state: (0, _pluginFactory.createPluginState)(dispatch, getInitialState), appendTransaction: function appendTransaction(transactions, oldEditorState, newEditorState) { var tr = newEditorState.tr; var manualSelection; var hideCursorChanged = false; var blockSelectionChanged = false; var needsManualSelection = (0, _experiments.editorExperiment)('platform_editor_element_drag_and_drop_multiselect', true); var needsBlockSelection = (0, _experiments.editorExperiment)('platform_editor_block_menu', true, { exposure: true }); for (var i = transactions.length - 1; i >= 0; i--) { var meta = transactions[i].getMeta(_types.selectionPluginKey); if ((meta === null || meta === void 0 ? void 0 : meta.hideCursor) !== undefined) { var newHideCursorValue = meta.hideCursor; if (cursorHidden !== newHideCursorValue) { cursorHidden = newHideCursorValue; hideCursorChanged = true; } } if (needsManualSelection && meta !== null && meta !== void 0 && meta.manualSelection && !manualSelection) { manualSelection = meta.manualSelection; } if (needsBlockSelection) { if (meta !== null && meta !== void 0 && meta.setBlockSelection) { blockSelection = meta.setBlockSelection; blockSelectionChanged = true; } if (meta !== null && meta !== void 0 && meta.clearBlockSelection) { blockSelection = undefined; blockSelectionChanged = true; } } } if (!(0, _utils.shouldRecalcDecorations)({ oldEditorState: oldEditorState, newEditorState: newEditorState }) && !manualSelection && !hideCursorChanged && !blockSelectionChanged) { return; } tr.setMeta(_types.selectionPluginKey, { type: _actions.SelectionActionTypes.SET_DECORATIONS, selection: tr.selection, decorationSet: (0, _utils.getDecorations)(tr, manualSelection, cursorHidden, blockSelection) }); return tr; }, filterTransaction: function filterTransaction(tr, state) { // Prevent single click selecting atom nodes on mobile (we want to select with long press gesture instead) if (options.useLongPressSelection && tr.selectionSet && tr.selection instanceof _state.NodeSelection && !tr.getMeta(_types.selectionPluginKey)) { return false; } // Prevent prosemirror's mutation observer overriding a node selection with a text selection // for exact same range - this was cause of being unable to change dates in collab: // https://product-fabric.atlassian.net/browse/ED-10645 if (state.selection instanceof _state.NodeSelection && tr.selection instanceof _state.TextSelection && state.selection.from === tr.selection.from && state.selection.to === tr.selection.to) { return false; } return true; }, props: { createSelectionBetween: _createSelectionBetween.onCreateSelectionBetween, decorations: function decorations(state) { var _api$interaction; var interactionState = api === null || api === void 0 || (_api$interaction = api.interaction) === null || _api$interaction === void 0 || (_api$interaction = _api$interaction.sharedState.currentState()) === null || _api$interaction === void 0 ? void 0 : _api$interaction.interactionState; // Do not show selection decorations for live pages where the user has not // interacted with the page. We do not show cursor until interaction and we do not // want to show selections either. if ((options.__livePage || (0, _expValEquals.expValEquals)('platform_editor_no_cursor_on_edit_page_init', 'isEnabled', true)) && interactionState === 'hasNotHadInteraction') { return _view.DecorationSet.empty; } return (0, _pluginFactory.getPluginState)(state).decorationSet; }, handleDOMEvents: { keydown: (0, _keydown.createOnKeydown)({ __livePage: options.__livePage }), // Without this event, it is not possible to click and drag to select the first node in the // document if the node is a block with content (e.g. a panel with a paragraph inside). mouseout: _mouseout.onMouseOut } } }); };