@atlaskit/editor-plugin-help-dialog
Version:
Help Dialog plugin for @atlaskit/editor-core
128 lines (126 loc) • 4.89 kB
JavaScript
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
var _ref = config !== null && config !== void 0 ? config : {},
_ref$imageUploadProvi = _ref.imageUploadProviderExists,
imageUploadProviderExists = _ref$imageUploadProvi === void 0 ? false : _ref$imageUploadProvi,
_ref$aiEnabled = _ref.aiEnabled,
aiEnabled = _ref$aiEnabled === void 0 ? false : _ref$aiEnabled;
return {
imageUploadProviderExists: imageUploadProviderExists,
aiEnabled: aiEnabled
};
}
export var helpDialogPlugin = function helpDialogPlugin(_ref2) {
var config = _ref2.config,
api = _ref2.api;
var _parseFeatureConfig = parseFeatureConfig(config),
imageUploadProviderExists = _parseFeatureConfig.imageUploadProviderExists,
aiEnabled = _parseFeatureConfig.aiEnabled;
return {
name: 'helpDialog',
pmPlugins: function pmPlugins() {
return [{
name: 'helpDialog',
plugin: function plugin(_ref3) {
var dispatch = _ref3.dispatch;
return createPlugin(dispatch, imageUploadProviderExists, aiEnabled);
}
}, {
name: 'helpDialogKeymap',
plugin: function plugin() {
var _api$analytics;
return keymapPlugin(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions);
}
}];
},
pluginsOptions: {
quickInsert: function quickInsert(_ref4) {
var formatMessage = _ref4.formatMessage;
return [{
id: 'helpdialog',
title: formatMessage(messages.help),
description: formatMessage(messages.helpDescription),
keywords: ['?'],
priority: 4000,
keyshortcut: tooltip(openHelp),
icon: function icon() {
return /*#__PURE__*/React.createElement(QuestionCircleIcon, {
label: "",
color: "currentColor",
spacing: "spacious"
});
},
action: function action(insert) {
var _api$analytics2;
var tr = insert('');
openHelpCommand(tr);
api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === 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: function contentComponent(_ref5) {
var editorView = _ref5.editorView;
if (!editorView || isSSR()) {
return null;
}
return /*#__PURE__*/React.createElement(HelpDialogLoader, {
pluginInjectionApi: api,
editorView: editorView,
quickInsertEnabled: !!(api !== null && api !== void 0 && api.quickInsert)
});
},
getSharedState: function getSharedState(editorState) {
if (!editorState) {
return null;
}
return pluginKey.getState(editorState) || null;
},
actions: {
openHelp: function openHelp() {
return api === null || api === void 0 ? void 0 : api.core.actions.execute(function (_ref6) {
var tr = _ref6.tr;
return openHelpAction(tr);
});
},
closeHelp: function closeHelp() {
return api === null || api === void 0 ? void 0 : api.core.actions.execute(function (_ref7) {
var tr = _ref7.tr;
return closeHelpAction(tr);
});
}
}
};
};