@atlaskit/editor-plugin-toolbar
Version:
Toolbar plugin for @atlaskit/editor-core
129 lines (126 loc) • 6.91 kB
JavaScript
import React, { memo, useMemo, useRef } from 'react';
import { useIntl } from 'react-intl';
import { isOfflineMode } from '@atlaskit/editor-common/connectivity/isOfflineMode';
import messages from '@atlaskit/editor-common/floating-toolbar/messages';
import { isSSR } from '@atlaskit/editor-common/is-ssr';
import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/useSharedPluginStateWithSelector';
import { ToolbarButtonGroup } from '@atlaskit/editor-toolbar/toolbar-button-group';
import { ToolbarDropdownItemSection } from '@atlaskit/editor-toolbar/toolbar-dropdown-item-section';
import { ToolbarSection } from '@atlaskit/editor-toolbar/toolbar-section';
import { SurfaceRenderer } from '@atlaskit/editor-ui-control-model/surface-renderer';
import { getKeyboardNavigationConfig } from '../SelectionToolbar/keyboard-config';
import { ToolbarProviders } from '../ToolbarProviders';
import { ElementToolbarContext } from './element-toolbar-context';
import { ELEMENT_TOOLBAR_TEST_ID } from './ElementToolbarPopup';
import { getActiveElementToolbar } from './get-active-element-toolbar';
var ELEMENT_TOOLBAR_SELECTOR = "[data-testid='".concat(ELEMENT_TOOLBAR_TEST_ID, "']");
var FALLBACKS = {
section: ToolbarSection,
'menu-section': ToolbarDropdownItemSection,
group: ToolbarButtonGroup
};
/**
* User intents that keep element toolbars visible. Any other intent, including a new one, hides them.
*/
var VISIBLE_USER_INTENTS = ['default', 'dragHandleSelected', 'popupOpen', 'layoutColumnMenuPopupOpen', 'aiStreaming', 'viewingDiff'];
// Re-renders only when the toolbar or its components change. The root component (usually
// ElementToolbarPopup) reads the node from ElementToolbarContext, so edits inside the node
// reposition the popup without re-rendering items. Each item subscribes to the state it needs.
var ElementToolbarContent = /*#__PURE__*/memo(function (_ref) {
var components = _ref.components,
surfaceKey = _ref.surfaceKey;
var surface = useMemo(function () {
return {
type: 'toolbar',
key: surfaceKey
};
}, [surfaceKey]);
return /*#__PURE__*/React.createElement(SurfaceRenderer, {
surface: surface,
components: components,
fallbacks: FALLBACKS
});
});
/**
* Renders the element toolbar for the selected node or its deepest ancestor. Element toolbars are
* `uiControlRegistry` toolbar surfaces keyed by node type name, e.g. `{ type: 'toolbar', key: 'table' }`.
*/
export var ElementToolbar = function ElementToolbar(_ref2) {
var api = _ref2.api,
editorView = _ref2.editorView,
mountPoint = _ref2.mountPoint,
boundariesElement = _ref2.boundariesElement,
scrollableElement = _ref2.scrollableElement;
var intl = useIntl();
var editorViewRef = useRef(editorView);
editorViewRef.current = editorView;
var registry = useMemo(function () {
return {
getComponent: function getComponent(component) {
var _api$uiControlRegistr;
return api === null || api === void 0 || (_api$uiControlRegistr = api.uiControlRegistry) === null || _api$uiControlRegistr === void 0 ? void 0 : _api$uiControlRegistr.actions.getComponent(component);
},
getComponents: function getComponents(key) {
var _api$uiControlRegistr2, _api$uiControlRegistr3;
return (_api$uiControlRegistr2 = api === null || api === void 0 || (_api$uiControlRegistr3 = api.uiControlRegistry) === null || _api$uiControlRegistr3 === void 0 ? void 0 : _api$uiControlRegistr3.actions.getComponents({
type: 'toolbar',
key: key
})) !== null && _api$uiControlRegistr2 !== void 0 ? _api$uiControlRegistr2 : [];
}
};
}, [api]);
// The selector returns primitives and cached surface arrays only, so the host re-renders when
// the active toolbar, its node or the document changes - not on every plugin state update. The
// document is included so the popup repositions when the element changes size. The selector is
// read once by the hook, so it must only use refs and stable values.
var _useSharedPluginState = useSharedPluginStateWithSelector(api, ['toolbar', 'selection', 'userIntent', 'editorViewMode', 'editorDisabled', 'connectivity'], function (states) {
var _states$userIntentSta, _states$editorViewMod, _states$editorDisable, _states$connectivityS;
var view = editorViewRef.current;
var userIntent = (_states$userIntentSta = states.userIntentState) === null || _states$userIntentSta === void 0 ? void 0 : _states$userIntentSta.currentUserIntent;
var isHidden = isSSR() || ((_states$editorViewMod = states.editorViewModeState) === null || _states$editorViewMod === void 0 ? void 0 : _states$editorViewMod.mode) === 'view' || Boolean((_states$editorDisable = states.editorDisabledState) === null || _states$editorDisable === void 0 ? void 0 : _states$editorDisable.editorDisabled) || Boolean(userIntent && !VISIBLE_USER_INTENTS.includes(userIntent));
var active = isHidden ? undefined : getActiveElementToolbar(view.state, registry);
return {
surfaceKey: active === null || active === void 0 ? void 0 : active.key,
components: active === null || active === void 0 ? void 0 : active.components,
nodePos: active === null || active === void 0 ? void 0 : active.pos,
doc: active ? view.state.doc : undefined,
isOffline: isOfflineMode((_states$connectivityS = states.connectivityState) === null || _states$connectivityS === void 0 ? void 0 : _states$connectivityS.mode)
};
}),
surfaceKey = _useSharedPluginState.surfaceKey,
components = _useSharedPluginState.components,
nodePos = _useSharedPluginState.nodePos,
doc = _useSharedPluginState.doc,
isOffline = _useSharedPluginState.isOffline;
var keyboardNavigation = useMemo(function () {
return getKeyboardNavigationConfig(editorView, intl, api, {
toolbarSelector: ELEMENT_TOOLBAR_SELECTOR,
ariaLabel: intl.formatMessage(messages.floatingToolbarAriaLabel)
});
}, [editorView, intl, api]);
var node = nodePos !== undefined && doc ? doc.nodeAt(nodePos) : undefined;
var elementToolbar = useMemo(function () {
return node && nodePos !== undefined ? {
node: node,
nodePos: nodePos
} : undefined;
}, [node, nodePos]);
if (!surfaceKey || !components || !elementToolbar) {
return null;
}
return /*#__PURE__*/React.createElement(ToolbarProviders, {
api: api,
editorView: editorView,
editorViewMode: "edit",
isOffline: isOffline,
keyboardNavigation: keyboardNavigation,
popupsMountPoint: mountPoint,
popupsBoundariesElement: boundariesElement,
popupsScrollableElement: scrollableElement
}, /*#__PURE__*/React.createElement(ElementToolbarContext.Provider, {
value: elementToolbar
}, /*#__PURE__*/React.createElement(ElementToolbarContent, {
surfaceKey: surfaceKey,
components: components
})));
};