UNPKG

@atlaskit/editor-plugin-selection-extension

Version:

editor-plugin-selection-extension plugin for @atlaskit/editor-core

46 lines (45 loc) 2.19 kB
/** * * TODO: ED-29157 - remove once Confluence migrates to `extensionList`API * * NOTES: * - This is temporary until we deprecate SelectionExtension and DynamicSelectionExtension. * - Only supporing SelectionExtension as DynamicSelectionExtension is not being used and will be deprecated. * * Converts a SelectionExtension -> ExtensionMenuItemConfiguration. This allows existing extensions to appear in the new toolbar. */ export const migrateSelectionExtensionToMenuItem = (extension, api) => { if (!extension.icon) { return undefined; } // Warning! These are inlined because the onClick handler must have up to date references to selection (which is awkwaradly set during extension.onClick invocation). const getSelection = () => { var _sharedState$activeEx; const sharedState = api === null || api === void 0 ? void 0 : api.selectionExtension.sharedState.currentState(); return sharedState === null || sharedState === void 0 ? void 0 : (_sharedState$activeEx = sharedState.activeExtension) === null || _sharedState$activeEx === void 0 ? void 0 : _sharedState$activeEx.selection; }; return { label: extension.name, icon: extension.icon, isDisabled: extension.isDisabled && extension.isDisabled({ selection: getSelection(), // pass undefined as no consumers use this selectedNodeAdf: undefined, selectionRanges: undefined }), /** * SelectionExtension supports passing through selection, selectionAdf, and selectionRanges. * * To support backwards compatibility wrap the onClick with these parameters here and let MenuItem invoke onClick using its expected signature. */ onClick: () => { const selectionAdf = api === null || api === void 0 ? void 0 : api.selectionExtension.actions.getSelectionAdf(); extension.onClick && extension.onClick({ selection: getSelection(), selectedNodeAdf: selectionAdf === null || selectionAdf === void 0 ? void 0 : selectionAdf.selectedNodeAdf, selectionRanges: selectionAdf === null || selectionAdf === void 0 ? void 0 : selectionAdf.selectionRanges }); }, contentComponent: extension.component }; };