UNPKG

@atlaskit/editor-plugin-expand

Version:

Expand plugin for @atlaskit/editor-core

129 lines (128 loc) 6.04 kB
import React from 'react'; import { expandWithNestedExpand, nestedExpand } 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 { 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'; var EXPAND_NODE_NAME = 'expand'; import { createExpandNode, insertExpand, insertExpandWithInputMethod, toggleExpandWithMatch as _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 var expandPlugin = function expandPlugin(_ref) { var _api$analytics, _api$analytics2; var _ref$config = _ref.config, options = _ref$config === void 0 ? {} : _ref$config, 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_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: 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(EXPAND_NODE_NAME)); } }]); } return { name: 'expand', nodes: function nodes() { return [{ name: 'expand', node: expandWithNestedExpand }, { name: 'nestedExpand', node: nestedExpand }]; }, actions: { insertExpand: insertExpand(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions), insertExpandWithInputMethod: insertExpandWithInputMethod(api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions) }, commands: { toggleExpandWithMatch: function toggleExpandWithMatch(selection) { return _toggleExpandWithMatch(selection); }, toggleExpandRange: toggleExpandRange }, getSharedState: function 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: function pmPlugins() { return [{ name: 'expand', plugin: function plugin(_ref2) { var _options$allowInterac; var dispatch = _ref2.dispatch, getIntl = _ref2.getIntl, nodeViewPortalProviderAPI = _ref2.nodeViewPortalProviderAPI; 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: function plugin() { return expandKeymap(api, { __livePage: options.__livePage }); } }]; }, pluginsOptions: { floatingToolbar: getToolbarConfig(api), quickInsert: function quickInsert(_ref3) { var formatMessage = _ref3.formatMessage; if (options && options.allowInsertion !== true) { return []; } return [{ id: 'expand', title: formatMessage(messages.expand), description: formatMessage(messages.expandDescription), keywords: ['accordion', 'collapse'], priority: 600, icon: function icon() { return /*#__PURE__*/React.createElement(IconExpand, null); }, action: function action(insert, state) { var _api$analytics3; var node = createExpandNode(state); if (!node) { return false; } var tr = state.selection.empty ? insert(node) : createWrapSelectionTransaction({ state: state, type: node.type }); api === null || api === void 0 || (_api$analytics3 = api.analytics) === null || _api$analytics3 === 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; } }]; } } }; };