@atlaskit/editor-plugin-toolbar
Version:
Toolbar plugin for @atlaskit/editor-core
56 lines (55 loc) • 2.77 kB
JavaScript
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 var getActiveFloatingToolbar = function getActiveFloatingToolbar(selection, userIntent) {
if (selection.empty) {
return 'element';
}
var 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 var 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 = 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.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
});
};