@atlaskit/editor-plugin-insert-block
Version:
Insert block plugin for @atlaskit/editor-core
75 lines • 3.38 kB
JavaScript
import React, { useRef } from 'react';
import { useIntl } from 'react-intl';
import { ToolTipContent, insertEmoji } 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 { EmojiIcon } from '@atlaskit/editor-toolbar/emoji-icon';
import { ToolbarButton } from '@atlaskit/editor-toolbar/toolbar-button';
import { ToolbarTooltip } from '@atlaskit/editor-toolbar/toolbar-tooltip';
import { useToolbarUI } from '@atlaskit/editor-toolbar/ui-context';
import { useEmojiPickerPopup } from './hooks/useEmojiPickerPopup';
import { EmojiPickerPopup } from './popups/EmojiPickerPopup';
export const EmojiButton = ({
api
}) => {
const {
formatMessage
} = useIntl();
const emojiButtonRef = useRef(null);
const {
popupsMountPoint,
popupsBoundariesElement,
popupsScrollableElement
} = useToolbarUI();
const {
contentId,
emojiProviderPromise,
isTypeAheadAllowed
} = useSharedPluginStateWithSelector(api, ['emoji', 'typeAhead'], states => {
var _states$emojiState, _states$emojiState2, _states$typeAheadStat;
return {
contentId: (_states$emojiState = states.emojiState) === null || _states$emojiState === void 0 ? void 0 : _states$emojiState.contentId,
emojiProviderPromise: (_states$emojiState2 = states.emojiState) === null || _states$emojiState2 === void 0 ? void 0 : _states$emojiState2.emojiProviderPromise,
isTypeAheadAllowed: (_states$typeAheadStat = states.typeAheadState) === null || _states$typeAheadStat === void 0 ? void 0 : _states$typeAheadStat.isAllowed
};
});
const emojiPickerPopup = useEmojiPickerPopup({
api,
buttonRef: emojiButtonRef
});
if (!(api !== null && api !== void 0 && api.emoji)) {
return null;
}
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(EmojiPickerPopup, {
contentId: contentId,
isOpen: emojiPickerPopup.isOpen,
targetRef: emojiButtonRef,
emojiProvider: emojiProviderPromise,
onSelection: emojiPickerPopup.handleSelectedEmoji,
onClickOutside: emojiPickerPopup.handleClickOutside,
onEscapeKeydown: emojiPickerPopup.handleEscapeKeydown,
onUnmount: emojiPickerPopup.onPopupUnmount,
popupsMountPoint: popupsMountPoint,
popupsBoundariesElement: popupsBoundariesElement,
popupsScrollableElement: popupsScrollableElement
}), /*#__PURE__*/React.createElement(ToolbarTooltip, {
content: /*#__PURE__*/React.createElement(ToolTipContent, {
description: formatMessage(messages.emoji),
keymap: insertEmoji
})
}, /*#__PURE__*/React.createElement(ToolbarButton, {
iconBefore: /*#__PURE__*/React.createElement(EmojiIcon, {
label: formatMessage(messages.emoji),
size: "small"
}),
ariaKeyshortcuts: "Shift+;",
ref: emojiButtonRef
// eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
,
onClick: () => emojiPickerPopup.toggle(),
isSelected: emojiPickerPopup.isOpen,
isDisabled: !isTypeAheadAllowed || !emojiProviderPromise,
testId: TOOLBAR_BUTTON_TEST_ID.EMOJI
})));
};