UNPKG

@atlaskit/editor-plugin-expand

Version:

Expand plugin for @atlaskit/editor-core

139 lines (138 loc) • 7.22 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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 { 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'; 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$blockMenu, _api$analytics, _api$analytics2; var _ref$config = _ref.config, options = _ref$config === void 0 ? {} : _ref$config, api = _ref.api; var isRegisteredSlashCommandEnabled = isExperimentEnabled('platform_editor_slash_command'); 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)); } }]); if (isRegisteredSlashCommandEnabled && options.allowInsertion === true) { var _api$uiControlRegistr; api === null || api === void 0 || (_api$uiControlRegistr = api.uiControlRegistry) === null || _api$uiControlRegistr === void 0 || _api$uiControlRegistr.actions.register(getExpandQuickInsertComponents({ api: api, isLegacy: true })); } 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 { allowInsertion: (_options$allowInserti = options === null || options === void 0 ? void 0 : options.allowInsertion) !== null && _options$allowInserti !== void 0 ? _options$allowInserti : true }; }, 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: _objectSpread({ floatingToolbar: getToolbarConfig(api) }, !isRegisteredSlashCommandEnabled && { 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; } }]; } }) }; };