UNPKG

@atlaskit/editor-plugin-toolbar

Version:

Toolbar plugin for @atlaskit/editor-core

124 lines (121 loc) • 6.35 kB
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'; const ELEMENT_TOOLBAR_SELECTOR = `[data-testid='${ELEMENT_TOOLBAR_TEST_ID}']`; const FALLBACKS = { section: ToolbarSection, 'menu-section': ToolbarDropdownItemSection, group: ToolbarButtonGroup }; /** * User intents that keep element toolbars visible. Any other intent, including a new one, hides them. */ const 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. const ElementToolbarContent = /*#__PURE__*/memo(({ components, surfaceKey }) => { const surface = useMemo(() => ({ 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 const ElementToolbar = ({ api, editorView, mountPoint, boundariesElement, scrollableElement }) => { const intl = useIntl(); const editorViewRef = useRef(editorView); editorViewRef.current = editorView; const registry = useMemo(() => ({ getComponent: component => { var _api$uiControlRegistr; return api === null || api === void 0 ? void 0 : (_api$uiControlRegistr = api.uiControlRegistry) === null || _api$uiControlRegistr === void 0 ? void 0 : _api$uiControlRegistr.actions.getComponent(component); }, getComponents: key => { var _api$uiControlRegistr2, _api$uiControlRegistr3; return (_api$uiControlRegistr2 = api === null || api === void 0 ? void 0 : (_api$uiControlRegistr3 = api.uiControlRegistry) === null || _api$uiControlRegistr3 === void 0 ? void 0 : _api$uiControlRegistr3.actions.getComponents({ type: 'toolbar', 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. const { surfaceKey, components, nodePos, doc, isOffline } = useSharedPluginStateWithSelector(api, ['toolbar', 'selection', 'userIntent', 'editorViewMode', 'editorDisabled', 'connectivity'], states => { var _states$userIntentSta, _states$editorViewMod, _states$editorDisable, _states$connectivityS; const view = editorViewRef.current; const userIntent = (_states$userIntentSta = states.userIntentState) === null || _states$userIntentSta === void 0 ? void 0 : _states$userIntentSta.currentUserIntent; const 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)); const 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) }; }); const keyboardNavigation = useMemo(() => getKeyboardNavigationConfig(editorView, intl, api, { toolbarSelector: ELEMENT_TOOLBAR_SELECTOR, ariaLabel: intl.formatMessage(messages.floatingToolbarAriaLabel) }), [editorView, intl, api]); const node = nodePos !== undefined && doc ? doc.nodeAt(nodePos) : undefined; const elementToolbar = useMemo(() => node && nodePos !== undefined ? { node, 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 }))); };