UNPKG

@atlaskit/editor-plugin-expand

Version:

Expand plugin for @atlaskit/editor-core

133 lines (132 loc) • 6.09 kB
import React from 'react'; import { expandWithNestedExpand } from '@atlaskit/adf-schema/expand'; import { nestedExpand } from '@atlaskit/adf-schema/nested-expand'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics'; import { IconExpand } from '@atlaskit/editor-common/assets'; import { TRANSFORM_STRUCTURE_EXPAND_MENU_ITEM, TRANSFORM_STRUCTURE_MENU_SECTION, TRANSFORM_STRUCTURE_MENU_SECTION_RANK } from '@atlaskit/editor-common/block-menu'; import { toolbarInsertBlockMessages as messages } from '@atlaskit/editor-common/messages'; import { createWrapSelectionTransaction } from '@atlaskit/editor-common/utils'; import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled'; import { toggleExpandRange } from '../editor-commands/toggleExpandRange'; import { createExpandBlockMenuItem } from '../ui/ExpandBlockMenuItem'; import { getExpandQuickInsertComponents } from '../ui/quick-insert/getExpandQuickInsertComponents'; const EXPAND_NODE_NAME = 'expand'; import { createExpandNode, insertExpand, insertExpandWithInputMethod, toggleExpandWithMatch } from './commands'; import { expandKeymap } from './pm-plugins/keymap'; import { createPlugin } from './pm-plugins/main'; import { getToolbarConfig } from './toolbar'; // Ignored via go/ees005 // eslint-disable-next-line prefer-const export let expandPlugin = ({ config: options = {}, api }) => { var _api$blockMenu, _api$analytics, _api$analytics2; const isRegisteredSlashCommandEnabled = isExperimentEnabled('platform_editor_slash_command'); api === null || api === void 0 ? void 0 : (_api$blockMenu = api.blockMenu) === null || _api$blockMenu === void 0 ? void 0 : _api$blockMenu.actions.registerBlockMenuComponents([{ type: 'block-menu-item', key: TRANSFORM_STRUCTURE_EXPAND_MENU_ITEM.key, parent: { type: 'block-menu-section', key: TRANSFORM_STRUCTURE_MENU_SECTION.key, rank: TRANSFORM_STRUCTURE_MENU_SECTION_RANK[TRANSFORM_STRUCTURE_EXPAND_MENU_ITEM.key] }, component: createExpandBlockMenuItem(api), isHidden: () => { var _api$blockMenu2; return Boolean(api === null || api === void 0 ? void 0 : (_api$blockMenu2 = api.blockMenu) === null || _api$blockMenu2 === void 0 ? void 0 : _api$blockMenu2.actions.isTransformOptionDisabled(EXPAND_NODE_NAME)); } }]); if (isRegisteredSlashCommandEnabled && options.allowInsertion === true) { var _api$uiControlRegistr; api === null || api === void 0 ? void 0 : (_api$uiControlRegistr = api.uiControlRegistry) === null || _api$uiControlRegistr === void 0 ? void 0 : _api$uiControlRegistr.actions.register(getExpandQuickInsertComponents({ api, isLegacy: true })); } return { name: 'expand', nodes() { return [{ name: 'expand', node: expandWithNestedExpand }, { name: 'nestedExpand', node: nestedExpand }]; }, actions: { insertExpand: insertExpand(api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions), insertExpandWithInputMethod: insertExpandWithInputMethod(api === null || api === void 0 ? void 0 : (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions) }, commands: { toggleExpandWithMatch: selection => toggleExpandWithMatch(selection), toggleExpandRange }, getSharedState() { var _options$allowInserti; return { allowInsertion: (_options$allowInserti = options === null || options === void 0 ? void 0 : options.allowInsertion) !== null && _options$allowInserti !== void 0 ? _options$allowInserti : true }; }, pmPlugins() { return [{ name: 'expand', plugin: ({ dispatch, getIntl, nodeViewPortalProviderAPI }) => { var _options$allowInterac; return createPlugin(dispatch, getIntl, options.appearance, options.useLongPressSelection, api, nodeViewPortalProviderAPI, (_options$allowInterac = options.allowInteractiveExpand) !== null && _options$allowInterac !== void 0 ? _options$allowInterac : true, options.__livePage); } }, { name: 'expandKeymap', plugin: () => expandKeymap(api, { __livePage: options.__livePage }) }]; }, pluginsOptions: { floatingToolbar: getToolbarConfig(api), ...(!isRegisteredSlashCommandEnabled && { quickInsert: ({ formatMessage }) => { if (options && options.allowInsertion !== true) { return []; } return [{ id: 'expand', title: formatMessage(messages.expand), description: formatMessage(messages.expandDescription), keywords: ['accordion', 'collapse'], priority: 600, icon: () => /*#__PURE__*/React.createElement(IconExpand, null), action(insert, state) { var _api$analytics3; const node = createExpandNode(state); if (!node) { return false; } const tr = state.selection.empty ? insert(node) : createWrapSelectionTransaction({ state, type: node.type }); api === null || api === void 0 ? void 0 : (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 ? void 0 : _api$analytics3.actions.attachAnalyticsEvent({ action: ACTION.INSERTED, actionSubject: ACTION_SUBJECT.DOCUMENT, actionSubjectId: node.type === state.schema.nodes.nestedExpand ? ACTION_SUBJECT_ID.NESTED_EXPAND : ACTION_SUBJECT_ID.EXPAND, attributes: { inputMethod: INPUT_METHOD.QUICK_INSERT }, eventType: EVENT_TYPE.TRACK })(tr); return tr; } }]; } }) } }; };