UNPKG

@atlaskit/editor-plugin-insert-block

Version:

Insert block plugin for @atlaskit/editor-core

60 lines (59 loc) • 3.44 kB
import React from 'react'; import { useIntl } from 'react-intl'; import { INPUT_METHOD } from '@atlaskit/editor-common/analytics/types/enums'; import { ToolTipContent, insertMention } from '@atlaskit/editor-common/keymaps'; import { toolbarInsertBlockMessages as messages } from '@atlaskit/editor-common/messages/insert-block'; import { TOOLBAR_BUTTON_TEST_ID } from '@atlaskit/editor-common/toolbar/keys'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/useSharedPluginStateWithSelector'; import { MentionIcon } from '@atlaskit/editor-toolbar/mention-icon'; import { ToolbarButton } from '@atlaskit/editor-toolbar/toolbar-button'; import { ToolbarTooltip } from '@atlaskit/editor-toolbar/toolbar-tooltip'; import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled'; export const MentionButton = ({ api }) => { const { formatMessage } = useIntl(); const { canInsertMention, mentionProvider, mentionProviderStatus, hasMentionState, isTypeAheadAllowed } = useSharedPluginStateWithSelector(api, ['mention', 'typeAhead'], states => { var _states$mentionState, _states$mentionState2, _states$mentionState3, _states$typeAheadStat; return { canInsertMention: (_states$mentionState = states.mentionState) === null || _states$mentionState === void 0 ? void 0 : _states$mentionState.canInsertMention, mentionProvider: (_states$mentionState2 = states.mentionState) === null || _states$mentionState2 === void 0 ? void 0 : _states$mentionState2.mentionProvider, mentionProviderStatus: (_states$mentionState3 = states.mentionState) === null || _states$mentionState3 === void 0 ? void 0 : _states$mentionState3.mentionProviderStatus, hasMentionState: states.mentionState !== undefined, isTypeAheadAllowed: (_states$typeAheadStat = states.typeAheadState) === null || _states$typeAheadStat === void 0 ? void 0 : _states$typeAheadStat.isAllowed }; }); if (!(api !== null && api !== void 0 && api.mention)) { return null; } const onClick = () => { var _api$mention, _api$mention$actions; api === null || api === void 0 ? void 0 : (_api$mention = api.mention) === null || _api$mention === void 0 ? void 0 : (_api$mention$actions = _api$mention.actions) === null || _api$mention$actions === void 0 ? void 0 : _api$mention$actions.openTypeAhead(INPUT_METHOD.TOOLBAR); }; // The SSR state cannot resolve providers. Keep pending and available visually identical, // while preserving known selection restrictions and settled provider failures. const isDisabled = isExperimentEnabled('platform_editor_ssr_toolbar_optimistic') ? canInsertMention === false || hasMentionState && isTypeAheadAllowed === false || mentionProviderStatus !== 'pending' && hasMentionState && !mentionProvider : !canInsertMention || !mentionProvider || !isTypeAheadAllowed; return /*#__PURE__*/React.createElement(ToolbarTooltip, { content: /*#__PURE__*/React.createElement(ToolTipContent, { description: formatMessage(messages.mention), keymap: insertMention }) }, /*#__PURE__*/React.createElement(ToolbarButton, { iconBefore: /*#__PURE__*/React.createElement(MentionIcon, { label: formatMessage(messages.mention), size: "small" }), onClick: onClick, ariaKeyshortcuts: "Shift+2 Space", isDisabled: isDisabled, testId: TOOLBAR_BUTTON_TEST_ID.MENTION })); };