@atlaskit/editor-plugin-selection-extension
Version:
editor-plugin-selection-extension plugin for @atlaskit/editor-core
84 lines (83 loc) • 4.28 kB
JavaScript
import React from 'react';
import { APPS_SECTION, EXTERNAL_EXTENSIONS_MENU_ITEM, FIRST_PARTY_EXTENSIONS_MENU_ITEM, OVERFLOW_EXTENSIONS_MENU_SECTION, OVERFLOW_EXTENSIONS_MENU_SECTION_RANK, TOOLBAR_RANK, TOOLBARS } from '@atlaskit/editor-common/toolbar';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { MenuItem } from './toolbar-components/MenuItem';
import { registerInlineToolbar } from './toolbar-components/register-inline-toolbar';
import { migrateSelectionExtensionToMenuItem } from './utils/migrate-selection-extention';
export const getToolbarComponents = ({
api,
config
}) => {
var _config$extensionList, _config$extensions, _config$extensions$fi, _config$extensions2, _config$extensions2$e;
const extensionToolbarComponents = (config === null || config === void 0 ? void 0 : (_config$extensionList = config.extensionList) === null || _config$extensionList === void 0 ? void 0 : _config$extensionList.flatMap((extension, index) => registerInlineToolbar({
api,
extension,
index
}))) || [];
/**
* Continue to support firstParty apps for now as some apps haven't migrated yet, this simply converts apps to new ExtensionMenuItemConfiguration type
* to process easier. extensionList and extensions?.firstParty will most likely be mutually exclusive.
*
* **Warning:** This is called on every selection change to ensure calls to api?.selectionExtension.actions.getSelectionAdf() are
* fresh and up to date.
*/
const firstPartyExtensions = (config === null || config === void 0 ? void 0 : (_config$extensions = config.extensions) === null || _config$extensions === void 0 ? void 0 : (_config$extensions$fi = _config$extensions.firstParty) === null || _config$extensions$fi === void 0 ? void 0 : _config$extensions$fi.map(extension => migrateSelectionExtensionToMenuItem(extension, api)).filter(extension => extension !== undefined)) || [];
const externalExtensions = (config === null || config === void 0 ? void 0 : (_config$extensions2 = config.extensions) === null || _config$extensions2 === void 0 ? void 0 : (_config$extensions2$e = _config$extensions2.external) === null || _config$extensions2$e === void 0 ? void 0 : _config$extensions2$e.map(extension => migrateSelectionExtensionToMenuItem(extension, api)).filter(extension => extension !== undefined)) || [];
return [{
type: APPS_SECTION.type,
key: APPS_SECTION.key,
parents: [{
type: 'toolbar',
key: TOOLBARS.INLINE_TEXT_TOOLBAR,
rank: TOOLBAR_RANK[APPS_SECTION.key]
}, {
type: 'toolbar',
key: TOOLBARS.PRIMARY_TOOLBAR,
rank: TOOLBAR_RANK[APPS_SECTION.key]
}]
}, ...extensionToolbarComponents, ...registerFirstPartyExtensions(api, firstPartyExtensions), ...registerExternalExtensions(api, externalExtensions)];
};
const registerFirstPartyExtensions = (api, extensions) => {
const components = [];
if (extensions.length === 0 && expValEquals('platform_editor_toolbar_hide_overflow_menu', 'isEnabled', true)) {
return components;
}
components.push({
type: FIRST_PARTY_EXTENSIONS_MENU_ITEM.type,
key: FIRST_PARTY_EXTENSIONS_MENU_ITEM.key,
parents: [{
type: OVERFLOW_EXTENSIONS_MENU_SECTION.type,
key: OVERFLOW_EXTENSIONS_MENU_SECTION.key,
rank: OVERFLOW_EXTENSIONS_MENU_SECTION_RANK[FIRST_PARTY_EXTENSIONS_MENU_ITEM.key]
}],
component: () => {
return /*#__PURE__*/React.createElement(MenuItem, {
api: api,
extensionMenuItems: extensions
});
}
});
return components;
};
const registerExternalExtensions = (api, extensions) => {
const components = [];
if (extensions.length === 0 && expValEquals('platform_editor_toolbar_hide_overflow_menu', 'isEnabled', true)) {
return components;
}
components.push({
type: EXTERNAL_EXTENSIONS_MENU_ITEM.type,
key: EXTERNAL_EXTENSIONS_MENU_ITEM.key,
parents: [{
type: OVERFLOW_EXTENSIONS_MENU_SECTION.type,
key: OVERFLOW_EXTENSIONS_MENU_SECTION.key,
rank: OVERFLOW_EXTENSIONS_MENU_SECTION_RANK[EXTERNAL_EXTENSIONS_MENU_ITEM.key]
}],
component: () => {
return /*#__PURE__*/React.createElement(MenuItem, {
api: api,
extensionMenuItems: extensions
});
}
});
return components;
};