UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

119 lines (117 loc) 5.96 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import React from 'react'; import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure'; import { createBlockMenuRegistry } from './editor-actions'; import { isTransformToTargetDisabled } from './editor-actions/isTransformToTargetDisabled'; import { transformNode as _transformNode } from './editor-commands/transformNode'; import { getBlockMenuExperiencesPlugin } from './pm-plugins/experiences/block-menu-experiences'; import { keymapPlugin } from './pm-plugins/keymap'; import { blockMenuPluginKey, createPlugin } from './pm-plugins/main'; import BlockMenu from './ui/block-menu'; import { getBlockMenuComponents } from './ui/block-menu-components'; import { BlockMenuProvider } from './ui/block-menu-provider'; import { Flag } from './ui/flag'; export var blockMenuPlugin = function blockMenuPlugin(_ref) { var api = _ref.api, config = _ref.config; var registry = createBlockMenuRegistry(); registry.register(getBlockMenuComponents({ api: api, config: config })); var refs = {}; return { name: 'blockMenu', pmPlugins: function pmPlugins() { return [{ name: 'blockMenuPlugin', plugin: function plugin() { return createPlugin(api); } }, { name: 'blockMenuKeymap', plugin: function plugin() { return keymapPlugin(api, config); } }].concat(_toConsumableArray(expValEqualsNoExposure('platform_editor_experience_tracking_observer', 'isEnabled', true) ? [{ name: 'blockMenuExperiences', plugin: function plugin() { return getBlockMenuExperiencesPlugin({ refs: refs, dispatchAnalyticsEvent: function dispatchAnalyticsEvent(payload) { var _api$analytics; return api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || (_api$analytics = _api$analytics.actions) === null || _api$analytics === void 0 ? void 0 : _api$analytics.fireAnalyticsEvent(payload); } }); } }] : [])); }, actions: { registerBlockMenuComponents: function registerBlockMenuComponents(blockMenuComponents) { registry.register(blockMenuComponents); }, getBlockMenuComponents: function getBlockMenuComponents() { return registry.components; }, isTransformOptionDisabled: function isTransformOptionDisabled(optionNodeTypeName, optionNodeTypeAttrs) { var _api$blockControls, _api$selection; var preservedSelection = api === null || api === void 0 || (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 || (_api$blockControls = _api$blockControls.sharedState.currentState()) === null || _api$blockControls === void 0 ? void 0 : _api$blockControls.preservedSelection; var selection = api === null || api === void 0 || (_api$selection = api.selection) === null || _api$selection === void 0 || (_api$selection = _api$selection.sharedState) === null || _api$selection === void 0 || (_api$selection = _api$selection.currentState()) === null || _api$selection === void 0 ? void 0 : _api$selection.selection; var currentSelection = preservedSelection || selection; if (!currentSelection) { return true; } return isTransformToTargetDisabled({ selection: currentSelection, targetNodeTypeName: optionNodeTypeName, targetNodeTypeAttrs: optionNodeTypeAttrs }); } }, commands: { transformNode: function transformNode(targetType, metadata) { return _transformNode(api)(targetType, metadata); } }, getSharedState: function getSharedState(editorState) { var _config$useStandardNo, _api$blockControls2, _pluginState$showFlag; var useStandardNodeWidth = (_config$useStandardNo = config === null || config === void 0 ? void 0 : config.useStandardNodeWidth) !== null && _config$useStandardNo !== void 0 ? _config$useStandardNo : false; if (!editorState) { return { currentSelectedNodeName: undefined, showFlag: false, useStandardNodeWidth: useStandardNodeWidth }; } // Get the menuTriggerBy from blockControls plugin if available var currentSelectedNodeName = api === null || api === void 0 || (_api$blockControls2 = api.blockControls) === null || _api$blockControls2 === void 0 || (_api$blockControls2 = _api$blockControls2.sharedState.currentState()) === null || _api$blockControls2 === void 0 ? void 0 : _api$blockControls2.menuTriggerBy; // Get the showFlag from plugin state var pluginState = blockMenuPluginKey.getState(editorState); var showFlag = (_pluginState$showFlag = pluginState === null || pluginState === void 0 ? void 0 : pluginState.showFlag) !== null && _pluginState$showFlag !== void 0 ? _pluginState$showFlag : false; return { currentSelectedNodeName: currentSelectedNodeName, showFlag: showFlag, useStandardNodeWidth: useStandardNodeWidth }; }, contentComponent: function contentComponent(_ref2) { var editorView = _ref2.editorView, popupsMountPoint = _ref2.popupsMountPoint, popupsBoundariesElement = _ref2.popupsBoundariesElement, popupsScrollableElement = _ref2.popupsScrollableElement; refs.popupsMountPoint = popupsMountPoint || undefined; return /*#__PURE__*/React.createElement(BlockMenuProvider, { api: api, editorView: editorView }, /*#__PURE__*/React.createElement(BlockMenu, { editorView: editorView, api: api, mountTo: popupsMountPoint, boundariesElement: popupsBoundariesElement, scrollableElement: popupsScrollableElement }), /*#__PURE__*/React.createElement(Flag, { api: api })); } }; };