UNPKG

@atlaskit/editor-plugin-help-dialog

Version:

Help Dialog plugin for @atlaskit/editor-core

124 lines (122 loc) 4.05 kB
import React from 'react'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics'; import { isSSR } from '@atlaskit/editor-common/core-utils'; import { openHelp, tooltip } from '@atlaskit/editor-common/keymaps'; import { toolbarInsertBlockMessages as messages } from '@atlaskit/editor-common/messages'; import QuestionCircleIcon from '@atlaskit/icon/core/question-circle'; import { closeHelpAction, openHelpAction } from './pm-plugins/actions'; import { openHelpCommand } from './pm-plugins/commands'; import { keymapPlugin } from './pm-plugins/keymap'; import { createPlugin } from './pm-plugins/main'; import { pluginKey } from './pm-plugins/plugin-key'; import { HelpDialogLoader } from './ui/HelpDialogLoader'; /** * Normalise the caller-supplied config into a single, predictable shape. * – Boolean → "image uploads on/off", AI always off * – Object → pick the two feature flags with sane defaults (both false) */ function parseFeatureConfig(config) { if (typeof config === 'boolean') { return { imageUploadProviderExists: config, aiEnabled: false }; } // Object path – ensure we never return undefined const { imageUploadProviderExists = false, aiEnabled = false } = config !== null && config !== void 0 ? config : {}; return { imageUploadProviderExists, aiEnabled }; } export const helpDialogPlugin = ({ config, api }) => { const { imageUploadProviderExists, aiEnabled } = parseFeatureConfig(config); return { name: 'helpDialog', pmPlugins() { return [{ name: 'helpDialog', plugin: ({ dispatch }) => createPlugin(dispatch, imageUploadProviderExists, aiEnabled) }, { name: 'helpDialogKeymap', plugin: () => { var _api$analytics; return keymapPlugin(api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions); } }]; }, pluginsOptions: { quickInsert: ({ formatMessage }) => [{ id: 'helpdialog', title: formatMessage(messages.help), description: formatMessage(messages.helpDescription), keywords: ['?'], priority: 4000, keyshortcut: tooltip(openHelp), icon: () => /*#__PURE__*/React.createElement(QuestionCircleIcon, { label: "", color: "currentColor", spacing: "spacious" }), action(insert) { var _api$analytics2; const tr = insert(''); openHelpCommand(tr); api === null || api === void 0 ? void 0 : (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions.attachAnalyticsEvent({ action: ACTION.HELP_OPENED, actionSubject: ACTION_SUBJECT.HELP, actionSubjectId: ACTION_SUBJECT_ID.HELP_QUICK_INSERT, attributes: { inputMethod: INPUT_METHOD.QUICK_INSERT }, eventType: EVENT_TYPE.UI })(tr); return tr; } }] }, contentComponent({ editorView }) { if (!editorView || isSSR()) { return null; } return /*#__PURE__*/React.createElement(HelpDialogLoader, { pluginInjectionApi: api, editorView: editorView, quickInsertEnabled: !!(api !== null && api !== void 0 && api.quickInsert) }); }, getSharedState(editorState) { if (!editorState) { return null; } return pluginKey.getState(editorState) || null; }, actions: { openHelp: () => { return api === null || api === void 0 ? void 0 : api.core.actions.execute(({ tr }) => openHelpAction(tr)); }, closeHelp: () => { return api === null || api === void 0 ? void 0 : api.core.actions.execute(({ tr }) => closeHelpAction(tr)); } } }; };