UNPKG

@atlaskit/editor-plugin-help-dialog

Version:

Help Dialog plugin for @atlaskit/editor-core

68 lines • 3.3 kB
import React, { useCallback, useEffect } from 'react'; import { injectIntl } from 'react-intl'; import { helpDialogMessages as messages } from '@atlaskit/editor-common/messages/help-dialog'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/useSharedPluginStateWithSelector'; import AkModalDialog from '@atlaskit/modal-dialog/modal-dialog'; import ModalTransition from '@atlaskit/modal-dialog/modal-transition'; import { fg } from '@atlaskit/platform-feature-flags/fg'; import { closeHelpCommand } from '../pm-plugins/commands'; import { getSupportedFormatting } from './formatting'; import Modal from './Modal'; var selector = function selector(states) { var _states$helpDialogSta, _states$helpDialogSta2, _states$helpDialogSta3; return { isVisible: (_states$helpDialogSta = states.helpDialogState) === null || _states$helpDialogSta === void 0 ? void 0 : _states$helpDialogSta.isVisible, imageEnabled: (_states$helpDialogSta2 = states.helpDialogState) === null || _states$helpDialogSta2 === void 0 ? void 0 : _states$helpDialogSta2.imageEnabled, aiEnabled: (_states$helpDialogSta3 = states.helpDialogState) === null || _states$helpDialogSta3 === void 0 ? void 0 : _states$helpDialogSta3.aiEnabled }; }; var HelpDialog = function HelpDialog(_ref) { var pluginInjectionApi = _ref.pluginInjectionApi, editorView = _ref.editorView, quickInsertEnabled = _ref.quickInsertEnabled, intl = _ref.intl; var _useSharedPluginState = useSharedPluginStateWithSelector(pluginInjectionApi, ['helpDialog'], selector), isVisible = _useSharedPluginState.isVisible, imageEnabled = _useSharedPluginState.imageEnabled, aiEnabled = _useSharedPluginState.aiEnabled; var closeDialog = useCallback(function () { var tr = editorView.state.tr, dispatch = editorView.dispatch; closeHelpCommand(tr, dispatch); }, [editorView]); var handleEsc = useCallback(function (e) { if (e.key === 'Escape' && isVisible) { closeDialog(); } }, [closeDialog, isVisible]); useEffect(function () { // On the top-layer path, the modal closes on Escape through `onClose`, after any open layer // above it. if (fg('platform-dst-top-layer')) { return; } // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners document.addEventListener('keydown', handleEsc); return function () { // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners document.removeEventListener('keydown', handleEsc); }; }, [handleEsc]); var formatting = getSupportedFormatting(editorView.state.schema, intl, imageEnabled, quickInsertEnabled, aiEnabled); var label = intl.formatMessage(messages.editorHelp); return /*#__PURE__*/React.createElement(ModalTransition, null, isVisible ? /*#__PURE__*/ // eslint-disable-next-line @atlaskit/design-system/no-modal-label React.createElement(AkModalDialog, { label: fg('platform_dst-modal_title') ? undefined : label, width: "large", onClose: closeDialog, testId: "help-modal-dialog" }, /*#__PURE__*/React.createElement(Modal, { formatting: formatting })) : null); }; var _default_1 = injectIntl(HelpDialog); export default _default_1;