UNPKG

@atlaskit/editor-plugin-panel

Version:

Panel plugin for @atlaskit/editor-core.

256 lines 11.2 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React from 'react'; import { extendedPanel, extendedPanelC1, extendedPanelC1WithLocalId, extendedPanelWithLocalId, PanelType } from '@atlaskit/adf-schema'; import { TRANSFORM_STRUCTURE_PANEL_MENU_ITEM, TRANSFORM_STRUCTURE_MENU_SECTION, TRANSFORM_STRUCTURE_MENU_SECTION_RANK } from '@atlaskit/editor-common/block-menu'; import { blockTypeMessages } from '@atlaskit/editor-common/messages'; import { IconCustomPanel, IconPanel, IconPanelError, IconPanelNote, IconPanelSuccess, IconPanelWarning } from '@atlaskit/editor-common/quick-insert'; import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled'; import { fg } from '@atlaskit/platform-feature-flags'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments'; import { createPanelAction } from './pm-plugins/commands/create-panel-action'; import keymap from './pm-plugins/keymaps'; import { createPlugin } from './pm-plugins/main'; import { createPanelBlockMenuItem } from './ui/panelBlockMenuItem'; import { getPanelQuickInsertComponents } from './ui/quick-insert/getPanelQuickInsertComponents'; import { getToolbarConfig } from './ui/toolbar'; var PANEL_NODE_NAME = 'panel'; var panelPlugin = function panelPlugin(_ref) { var _ref$config = _ref.config, _ref$config2 = _ref$config === void 0 ? {} : _ref$config, _ref$config2$allowCus = _ref$config2.allowCustomPanel, allowCustomPanel = _ref$config2$allowCus === void 0 ? false : _ref$config2$allowCus, _ref$config2$allowCus2 = _ref$config2.allowCustomPanelEdit, allowCustomPanelEdit = _ref$config2$allowCus2 === void 0 ? false : _ref$config2$allowCus2, api = _ref.api; if (editorExperiment('platform_editor_block_menu', true)) { var _api$blockMenu; api === null || api === void 0 || (_api$blockMenu = api.blockMenu) === null || _api$blockMenu === void 0 || _api$blockMenu.actions.registerBlockMenuComponents([{ type: 'block-menu-item', key: TRANSFORM_STRUCTURE_PANEL_MENU_ITEM.key, parent: { type: 'block-menu-section', key: TRANSFORM_STRUCTURE_MENU_SECTION.key, rank: TRANSFORM_STRUCTURE_MENU_SECTION_RANK[TRANSFORM_STRUCTURE_PANEL_MENU_ITEM.key] }, component: createPanelBlockMenuItem(api), isHidden: function isHidden() { var _api$blockMenu2; return Boolean(api === null || api === void 0 || (_api$blockMenu2 = api.blockMenu) === null || _api$blockMenu2 === void 0 ? void 0 : _api$blockMenu2.actions.isTransformOptionDisabled(PANEL_NODE_NAME)); } }]); } var isRegisteredSlashCommandEnabled = isExperimentEnabled('platform_editor_slash_command'); if (isRegisteredSlashCommandEnabled) { var _api$uiControlRegistr; api === null || api === void 0 || (_api$uiControlRegistr = api.uiControlRegistry) === null || _api$uiControlRegistr === void 0 || _api$uiControlRegistr.actions.register(getPanelQuickInsertComponents({ allowCustomPanel: allowCustomPanel, allowCustomPanelEdit: allowCustomPanelEdit, api: api })); } return { name: 'panel', nodes: function nodes() { if (fg('platform_editor_adf_with_localid')) { return [{ name: 'panel', node: _objectSpread(_objectSpread({}, extendedPanelWithLocalId(!!allowCustomPanel)), {}, { definingAsContext: true }) }].concat(_toConsumableArray(expValEquals('platform_editor_nest_table_in_panel', 'isEnabled', true) ? [{ name: 'panel_c1', node: _objectSpread(_objectSpread({}, extendedPanelC1WithLocalId(!!allowCustomPanel)), {}, { definingAsContext: true }) }] : [])); } return [{ name: 'panel', node: extendedPanel(!!allowCustomPanel) }].concat(_toConsumableArray(expValEquals('platform_editor_nest_table_in_panel', 'isEnabled', true) ? [{ name: 'panel_c1', node: extendedPanelC1(!!allowCustomPanel) }] : [])); }, pmPlugins: function pmPlugins() { return [{ name: 'panel', plugin: function plugin(_ref2) { var providerFactory = _ref2.providerFactory, dispatch = _ref2.dispatch, nodeViewPortalProviderAPI = _ref2.nodeViewPortalProviderAPI; return createPlugin(dispatch, providerFactory, { allowCustomPanel: allowCustomPanel, allowCustomPanelEdit: allowCustomPanelEdit }, api, nodeViewPortalProviderAPI); } }, { name: 'panelKeyMap', plugin: function plugin() { return keymap(); } }]; }, actions: { insertPanel: function insertPanel(inputMethod) { return function (state, dispatch) { var tr = createPanelAction({ state: state, attributes: { panelType: PanelType.INFO }, api: api, inputMethod: inputMethod }); if (!tr) { return false; } if (dispatch) { dispatch(tr); } return true; }; } }, pluginsOptions: _objectSpread(_objectSpread({}, isRegisteredSlashCommandEnabled ? {} : { quickInsert: function quickInsert(_ref3) { var formatMessage = _ref3.formatMessage; var quickInsertOptions = [{ id: 'infopanel', title: formatMessage(blockTypeMessages.infoPanel), keywords: ['panel'], description: formatMessage(blockTypeMessages.infoPanelDescription), priority: 800, icon: function icon() { return /*#__PURE__*/React.createElement(IconPanel, null); }, action: function action(typeAheadInsert, state) { return createPanelAction({ state: state, attributes: { panelType: PanelType.INFO }, api: api, typeAheadInsert: typeAheadInsert }); } }, { id: 'notepanel', title: formatMessage(blockTypeMessages.notePanel), description: formatMessage(blockTypeMessages.notePanelDescription), priority: 1000, icon: function icon() { return /*#__PURE__*/React.createElement(IconPanelNote, null); }, action: function action(typeAheadInsert, state) { return createPanelAction({ state: state, attributes: { panelType: PanelType.NOTE }, api: api, typeAheadInsert: typeAheadInsert }); } }, { id: 'successpanel', title: formatMessage(blockTypeMessages.successPanel), description: formatMessage(blockTypeMessages.successPanelDescription), keywords: ['tip'], priority: 1000, icon: function icon() { return /*#__PURE__*/React.createElement(IconPanelSuccess, null); }, action: function action(typeAheadInsert, state) { return createPanelAction({ state: state, attributes: { panelType: PanelType.SUCCESS }, api: api, typeAheadInsert: typeAheadInsert }); } }, { id: 'warningpanel', title: formatMessage(blockTypeMessages.warningPanel), description: formatMessage(blockTypeMessages.warningPanelDescription), priority: 1000, icon: function icon() { return /*#__PURE__*/React.createElement(IconPanelWarning, null); }, action: function action(typeAheadInsert, state) { return createPanelAction({ state: state, attributes: { panelType: PanelType.WARNING }, api: api, typeAheadInsert: typeAheadInsert }); } }, { id: 'errorpanel', title: formatMessage(blockTypeMessages.errorPanel), description: formatMessage(blockTypeMessages.errorPanelDescription), priority: 1000, icon: function icon() { return /*#__PURE__*/React.createElement(IconPanelError, null); }, action: function action(typeAheadInsert, state) { return createPanelAction({ state: state, attributes: { panelType: PanelType.ERROR }, api: api, typeAheadInsert: typeAheadInsert }); } }]; if (allowCustomPanel && allowCustomPanelEdit) { quickInsertOptions.push({ id: 'custompanel', title: formatMessage(blockTypeMessages.customPanel), description: formatMessage(blockTypeMessages.customPanelDescription), priority: 1000, icon: function icon() { return /*#__PURE__*/React.createElement(IconCustomPanel, null); }, action: function action(typeAheadInsert, state) { return createPanelAction({ state: state, attributes: { panelType: PanelType.CUSTOM, panelIcon: ':rainbow:', panelIconId: '1f308', panelIconText: '🌈', // Ignored via go/ees007 // eslint-disable-next-line @atlaskit/editor/enforce-todo-comment-format // TODO: https://product-fabric.atlassian.net/browse/DSP-7268 // eslint-disable-next-line @atlaskit/design-system/ensure-design-token-usage panelColor: '#E6FCFF' }, api: api, typeAheadInsert: typeAheadInsert }); } }); } return quickInsertOptions; } }), {}, { floatingToolbar: function floatingToolbar(state, intl, providerFactory) { return getToolbarConfig(state, intl, { allowCustomPanel: allowCustomPanel, allowCustomPanelEdit: allowCustomPanelEdit }, providerFactory, api); } }) }; }; export default panelPlugin;