UNPKG

@atlaskit/editor-plugin-toolbar

Version:

Toolbar plugin for @atlaskit/editor-core

58 lines (57 loc) • 2.44 kB
import React from 'react'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/useSharedPluginStateWithSelector'; import { AllSelection, TextSelection } from '@atlaskit/editor-prosemirror/state'; import { ElementToolbar } from './ElementToolbar'; import { SelectionToolbarWithErrorBoundary } from './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. */ export const getActiveFloatingToolbar = (selection, userIntent) => { if (selection.empty) { return 'element'; } const isCellSelection = ('$anchorCell' in selection); if (selection instanceof TextSelection || selection instanceof AllSelection || isCellSelection && userIntent === 'default') { return 'selection'; } return 'element'; }; /** * Renders either the selection toolbar or the element toolbar, never both. */ export const FloatingToolbar = ({ api, boundariesElement, disableSelectionToolbar, disableSelectionToolbarWhenPinned, editorView, mountPoint, onToolbarMouseUp, scrollableElement }) => { const { activeToolbar } = useSharedPluginStateWithSelector(api, ['selection', 'userIntent'], 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) }; }); if (activeToolbar === 'selection') { return disableSelectionToolbar ? null : /*#__PURE__*/React.createElement(SelectionToolbarWithErrorBoundary, { api: api, editorView: editorView, mountPoint: mountPoint, disableSelectionToolbarWhenPinned: disableSelectionToolbarWhenPinned, onToolbarMouseUp: onToolbarMouseUp }); } return /*#__PURE__*/React.createElement(ElementToolbar, { api: api, editorView: editorView, mountPoint: mountPoint, boundariesElement: boundariesElement, scrollableElement: scrollableElement }); };