UNPKG

@atlaskit/editor-plugin-expand

Version:

Expand plugin for @atlaskit/editor-core

123 lines (122 loc) 5.7 kB
import React from 'react'; import { expandWithNestedExpand, expandWithNestedExpandLocalId, nestedExpand, nestedExpandWithLocalId } from '@atlaskit/adf-schema'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics'; 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 { IconExpand } from '@atlaskit/editor-common/quick-insert'; import { createWrapSelectionTransaction } from '@atlaskit/editor-common/utils'; 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 { toggleExpandRange } from '../editor-commands/toggleExpandRange'; import { createExpandBlockMenuItem } from '../ui/ExpandBlockMenuItem'; const EXPAND_NODE_NAME = 'expand'; import { createExpandNode, insertExpand, insertExpandWithInputMethod, toggleExpandWithMatch, wrapSelectionAndSetExpandedState } 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 }) => { if (editorExperiment('platform_editor_block_menu', true)) { var _api$blockMenu; 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)); } }]); } return { name: 'expand', nodes() { return [{ name: 'expand', node: fg('platform_editor_adf_with_localid') ? expandWithNestedExpandLocalId : expandWithNestedExpand }, { name: 'nestedExpand', node: fg('platform_editor_adf_with_localid') ? nestedExpandWithLocalId : nestedExpand }]; }, actions: { insertExpand: insertExpand(api), insertExpandWithInputMethod: insertExpandWithInputMethod(api) }, commands: { toggleExpandWithMatch: selection => toggleExpandWithMatch(selection), toggleExpandRange }, getSharedState() { var _options$allowInserti; return expValEquals('platform_editor_expand_paste_in_comment_editor', 'isEnabled', true) ? { allowInsertion: (_options$allowInserti = options === null || options === void 0 ? void 0 : options.allowInsertion) !== null && _options$allowInserti !== void 0 ? _options$allowInserti : true } : undefined; }, 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) }]; }, pluginsOptions: { floatingToolbar: getToolbarConfig(api), 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, source) { var _api$analytics; const node = createExpandNode(state, undefined, !!(api !== null && api !== void 0 && api.localId)); if (!node) { return false; } const tr = state.selection.empty ? insert(node) : fg('platform_editor_adf_with_localid') ? wrapSelectionAndSetExpandedState(state, node) : createWrapSelectionTransaction({ state, type: node.type }); api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.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: source !== null && source !== void 0 ? source : INPUT_METHOD.QUICK_INSERT }, eventType: EVENT_TYPE.TRACK })(tr); return tr; } }]; } } }; };