@atlaskit/editor-plugin-selection-extension
Version:
editor-plugin-selection-extension plugin for @atlaskit/editor-core
94 lines (93 loc) • 4.61 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
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 var getToolbarComponents = function getToolbarComponents(_ref) {
var _config$extensionList, _config$extensions, _config$extensions2;
var api = _ref.api,
config = _ref.config;
var extensionToolbarComponents = (config === null || config === void 0 || (_config$extensionList = config.extensionList) === null || _config$extensionList === void 0 ? void 0 : _config$extensionList.flatMap(function (extension, index) {
return registerInlineToolbar({
api: api,
extension: extension,
index: 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.
*/
var firstPartyExtensions = (config === null || config === void 0 || (_config$extensions = config.extensions) === null || _config$extensions === void 0 || (_config$extensions = _config$extensions.firstParty) === null || _config$extensions === void 0 ? void 0 : _config$extensions.map(function (extension) {
return migrateSelectionExtensionToMenuItem(extension, api);
}).filter(function (extension) {
return extension !== undefined;
})) || [];
var externalExtensions = (config === null || config === void 0 || (_config$extensions2 = config.extensions) === null || _config$extensions2 === void 0 || (_config$extensions2 = _config$extensions2.external) === null || _config$extensions2 === void 0 ? void 0 : _config$extensions2.map(function (extension) {
return migrateSelectionExtensionToMenuItem(extension, api);
}).filter(function (extension) {
return 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]
}]
}].concat(_toConsumableArray(extensionToolbarComponents), _toConsumableArray(registerFirstPartyExtensions(api, firstPartyExtensions)), _toConsumableArray(registerExternalExtensions(api, externalExtensions)));
};
var registerFirstPartyExtensions = function registerFirstPartyExtensions(api, extensions) {
var 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: function component() {
return /*#__PURE__*/React.createElement(MenuItem, {
api: api,
extensionMenuItems: extensions
});
}
});
return components;
};
var registerExternalExtensions = function registerExternalExtensions(api, extensions) {
var 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: function component() {
return /*#__PURE__*/React.createElement(MenuItem, {
api: api,
extensionMenuItems: extensions
});
}
});
return components;
};