UNPKG

@atlaskit/editor-plugin-toolbar

Version:

Toolbar plugin for @atlaskit/editor-core

62 lines (61 loc) • 3.15 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getActiveFloatingToolbar = exports.FloatingToolbar = void 0; var _react = _interopRequireDefault(require("react")); var _useSharedPluginStateWithSelector = require("@atlaskit/editor-common/useSharedPluginStateWithSelector"); var _state = require("@atlaskit/editor-prosemirror/state"); var _ElementToolbar = require("./ElementToolbar"); var _SelectionToolbar = require("./SelectionToolbar"); /** * The selection toolbar owns non-empty text and all selections, and plain cell selections. A cell * selection made by clicking a drag handle has its own user intent, so the element toolbar stays. */ var getActiveFloatingToolbar = exports.getActiveFloatingToolbar = function getActiveFloatingToolbar(selection, userIntent) { if (selection.empty) { return 'element'; } var isCellSelection = ('$anchorCell' in selection); if (selection instanceof _state.TextSelection || selection instanceof _state.AllSelection || isCellSelection && userIntent === 'default') { return 'selection'; } return 'element'; }; /** * Renders either the selection toolbar or the element toolbar, never both. */ var FloatingToolbar = exports.FloatingToolbar = function FloatingToolbar(_ref) { var api = _ref.api, boundariesElement = _ref.boundariesElement, disableSelectionToolbar = _ref.disableSelectionToolbar, disableSelectionToolbarWhenPinned = _ref.disableSelectionToolbarWhenPinned, editorView = _ref.editorView, mountPoint = _ref.mountPoint, onToolbarMouseUp = _ref.onToolbarMouseUp, scrollableElement = _ref.scrollableElement; var _useSharedPluginState = (0, _useSharedPluginStateWithSelector.useSharedPluginStateWithSelector)(api, ['selection', 'userIntent'], function (states) { var _states$selectionStat, _states$selectionStat2, _states$userIntentSta; return { activeToolbar: getActiveFloatingToolbar((_states$selectionStat = (_states$selectionStat2 = states.selectionState) === null || _states$selectionStat2 === void 0 ? void 0 : _states$selectionStat2.selection) !== null && _states$selectionStat !== void 0 ? _states$selectionStat : editorView.state.selection, (_states$userIntentSta = states.userIntentState) === null || _states$userIntentSta === void 0 ? void 0 : _states$userIntentSta.currentUserIntent) }; }), activeToolbar = _useSharedPluginState.activeToolbar; if (activeToolbar === 'selection') { return disableSelectionToolbar ? null : /*#__PURE__*/_react.default.createElement(_SelectionToolbar.SelectionToolbarWithErrorBoundary, { api: api, editorView: editorView, mountPoint: mountPoint, disableSelectionToolbarWhenPinned: disableSelectionToolbarWhenPinned, onToolbarMouseUp: onToolbarMouseUp }); } return /*#__PURE__*/_react.default.createElement(_ElementToolbar.ElementToolbar, { api: api, editorView: editorView, mountPoint: mountPoint, boundariesElement: boundariesElement, scrollableElement: scrollableElement }); };