UNPKG

@atlaskit/editor-plugin-toolbar

Version:

Toolbar plugin for @atlaskit/editor-core

138 lines (134 loc) • 8.24 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.ElementToolbar = void 0; var _react = _interopRequireWildcard(require("react")); var _reactIntl = require("react-intl"); var _isOfflineMode = require("@atlaskit/editor-common/connectivity/isOfflineMode"); var _messages = _interopRequireDefault(require("@atlaskit/editor-common/floating-toolbar/messages")); var _isSsr = require("@atlaskit/editor-common/is-ssr"); var _useSharedPluginStateWithSelector = require("@atlaskit/editor-common/useSharedPluginStateWithSelector"); var _toolbarButtonGroup = require("@atlaskit/editor-toolbar/toolbar-button-group"); var _toolbarDropdownItemSection = require("@atlaskit/editor-toolbar/toolbar-dropdown-item-section"); var _toolbarSection = require("@atlaskit/editor-toolbar/toolbar-section"); var _surfaceRenderer = require("@atlaskit/editor-ui-control-model/surface-renderer"); var _keyboardConfig = require("../SelectionToolbar/keyboard-config"); var _ToolbarProviders = require("../ToolbarProviders"); var _elementToolbarContext = require("./element-toolbar-context"); var _ElementToolbarPopup = require("./ElementToolbarPopup"); var _getActiveElementToolbar = require("./get-active-element-toolbar"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } var ELEMENT_TOOLBAR_SELECTOR = "[data-testid='".concat(_ElementToolbarPopup.ELEMENT_TOOLBAR_TEST_ID, "']"); var FALLBACKS = { section: _toolbarSection.ToolbarSection, 'menu-section': _toolbarDropdownItemSection.ToolbarDropdownItemSection, group: _toolbarButtonGroup.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__*/(0, _react.memo)(function (_ref) { var components = _ref.components, surfaceKey = _ref.surfaceKey; var surface = (0, _react.useMemo)(function () { return { type: 'toolbar', key: surfaceKey }; }, [surfaceKey]); return /*#__PURE__*/_react.default.createElement(_surfaceRenderer.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' }`. */ var ElementToolbar = exports.ElementToolbar = function ElementToolbar(_ref2) { var api = _ref2.api, editorView = _ref2.editorView, mountPoint = _ref2.mountPoint, boundariesElement = _ref2.boundariesElement, scrollableElement = _ref2.scrollableElement; var intl = (0, _reactIntl.useIntl)(); var editorViewRef = (0, _react.useRef)(editorView); editorViewRef.current = editorView; var registry = (0, _react.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 = (0, _useSharedPluginStateWithSelector.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 = (0, _isSsr.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 : (0, _getActiveElementToolbar.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: (0, _isOfflineMode.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 = (0, _react.useMemo)(function () { return (0, _keyboardConfig.getKeyboardNavigationConfig)(editorView, intl, api, { toolbarSelector: ELEMENT_TOOLBAR_SELECTOR, ariaLabel: intl.formatMessage(_messages.default.floatingToolbarAriaLabel) }); }, [editorView, intl, api]); var node = nodePos !== undefined && doc ? doc.nodeAt(nodePos) : undefined; var elementToolbar = (0, _react.useMemo)(function () { return node && nodePos !== undefined ? { node: node, nodePos: nodePos } : undefined; }, [node, nodePos]); if (!surfaceKey || !components || !elementToolbar) { return null; } return /*#__PURE__*/_react.default.createElement(_ToolbarProviders.ToolbarProviders, { api: api, editorView: editorView, editorViewMode: "edit", isOffline: isOffline, keyboardNavigation: keyboardNavigation, popupsMountPoint: mountPoint, popupsBoundariesElement: boundariesElement, popupsScrollableElement: scrollableElement }, /*#__PURE__*/_react.default.createElement(_elementToolbarContext.ElementToolbarContext.Provider, { value: elementToolbar }, /*#__PURE__*/_react.default.createElement(ElementToolbarContent, { surfaceKey: surfaceKey, components: components }))); };