@atlaskit/editor-plugin-selection-extension
Version:
editor-plugin-selection-extension plugin for @atlaskit/editor-core
91 lines (90 loc) • 3.86 kB
JavaScript
import React from 'react';
import { BLOCK_ACTIONS_MENU_SECTION, BLOCK_ACTIONS_MENU_SECTION_RANK, TRANSFORM_MENU_SECTION, BLOCK_ACTIONS_FEATURED_EXTENSION_SLOT_MENU_ITEM, TRANSFORM_MENU_SECTION_RANK, TRANSFORM_CREATE_MENU_SECTION, TRANSFORM_CREATE_MENU_SECTION_RANK, TRANSFORM_DEFAULT_EXTENSION_SLOT_MENU_ITEM } from '@atlaskit/editor-common/block-menu';
import { fg } from '@atlaskit/platform-feature-flags';
import { SelectionExtensionMenuItems } from '../menu/SelectionExtensionMenuItems';
import { SelectionExtensionComponentContextProvider } from '../SelectionExtensionComponentContext';
export function registerBlockMenuItems({
extensionList,
api,
editorViewRef
}) {
extensionList.forEach(({
source,
key,
blockMenu
}) => {
if (source !== 'first-party' || !blockMenu) {
return;
}
if (!(api !== null && api !== void 0 && api.blockMenu)) {
return;
}
const componentsToRegister = [];
// Use placement from BlockMenuExtensionConfiguration
// Featured placement: register under TRANSFORM_MENU_SECTION
// Default placement: register under TRANSFORM_CREATE_MENU_SECTION
if (blockMenu.placement === 'featured') {
componentsToRegister.push({
type: 'block-menu-item',
key: `selection-extension-${key}`,
parent: {
type: 'block-menu-section',
key: fg('platform_editor_block_menu_divider_patch') ? TRANSFORM_MENU_SECTION.key : BLOCK_ACTIONS_MENU_SECTION.key,
rank: fg('platform_editor_block_menu_divider_patch') ? TRANSFORM_MENU_SECTION_RANK[BLOCK_ACTIONS_FEATURED_EXTENSION_SLOT_MENU_ITEM.key] : BLOCK_ACTIONS_MENU_SECTION_RANK[BLOCK_ACTIONS_FEATURED_EXTENSION_SLOT_MENU_ITEM.key]
},
component: () => {
const editorView = editorViewRef === null || editorViewRef === void 0 ? void 0 : editorViewRef.current;
if (!editorView) {
return null;
}
return /*#__PURE__*/React.createElement(SelectionExtensionComponentContextProvider
// eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
, {
value: {
api,
editorView,
extensionKey: key,
extensionSource: source,
extensionLocation: 'block-menu'
}
}, /*#__PURE__*/React.createElement(SelectionExtensionMenuItems, {
getMenuItems: blockMenu.getMenuItems
}));
}
});
} else {
componentsToRegister.push({
type: 'block-menu-item',
key: `selection-extension-${key}`,
isHidden: () => blockMenu.getMenuItems().length === 0,
parent: {
type: 'block-menu-section',
key: TRANSFORM_CREATE_MENU_SECTION.key,
rank: TRANSFORM_CREATE_MENU_SECTION_RANK[TRANSFORM_DEFAULT_EXTENSION_SLOT_MENU_ITEM.key]
},
component: () => {
const editorView = editorViewRef === null || editorViewRef === void 0 ? void 0 : editorViewRef.current;
if (!editorView) {
return null;
}
return /*#__PURE__*/React.createElement(SelectionExtensionComponentContextProvider
// eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
, {
value: {
api,
editorView,
extensionKey: key,
extensionSource: source,
extensionLocation: 'block-menu'
}
}, /*#__PURE__*/React.createElement(SelectionExtensionMenuItems, {
getMenuItems: blockMenu.getMenuItems
}));
}
});
}
if (componentsToRegister.length > 0) {
api.blockMenu.actions.registerBlockMenuComponents(componentsToRegister);
}
});
}