@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
48 lines • 1.83 kB
JavaScript
import { BLOCK_CONTROL_UI_CONTEXT } from '@atlaskit/editor-common/block-controls/block-control-ui-context';
import { createSurfaceContext } from '@atlaskit/editor-ui-control-model/create-surface-context';
import { getComponentIdentity, resolveSurface, willComponentRender } from '@atlaskit/editor-ui-control-model/surface-renderer';
/**
* Split registered components into two groups:
* those that should be rendered persistently and those that should only be rendered on hover.
*/
export const partitionComponentsByPersistence = (components, surface, surfaceContext) => {
const {
childrenMap,
root,
topLevelChildren
} = resolveSurface(components, surface);
if (!root || !topLevelChildren || topLevelChildren.length === 0) {
return {
hoverOnlyComponents: [],
persistentComponents: components
};
}
const context = surfaceContext === null || surfaceContext === void 0 ? void 0 : surfaceContext.get(BLOCK_CONTROL_UI_CONTEXT);
const storedSurfaceContext = context ? createSurfaceContext(BLOCK_CONTROL_UI_CONTEXT, {
activeNode: undefined,
rootNode: context.targetNode,
targetNode: context.targetNode
}) : undefined;
const isLeaf = component => {
const children = childrenMap.get(getComponentIdentity(component));
return !children || children.length === 0;
};
const persistentComponents = [];
const hoverOnlyComponents = [];
for (const component of components) {
if (!isLeaf(component)) {
persistentComponents.push(component);
hoverOnlyComponents.push(component);
continue;
}
if (willComponentRender(component, childrenMap, storedSurfaceContext)) {
persistentComponents.push(component);
} else {
hoverOnlyComponents.push(component);
}
}
return {
hoverOnlyComponents,
persistentComponents
};
};