UNPKG

@atlaskit/editor-plugin-block-controls

Version:

Block controls plugin for @atlaskit/editor-core

67 lines (66 loc) • 3.47 kB
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 { getBlockControlsLeftSurfaceComponents, getBlockControlsRightSurfaceComponents } from '../ui/block-controls-surface-components'; import { createBlockControlsSurfaceContextForPosition } from '../ui/block-controls-surface-context'; import { areBlockControlsSurfaceContextsEqual } from './are-block-controls-surface-contexts-equal'; /** * Resolves each target into the controls, context and anchor information needed to render a surface. * When a previous descriptor still matches, reuse it without rerunning component visibility * predicates. The caller must omit `previous` when registry or external state changes invalidate * those results. Targets without an anchor, a valid context or renderable components are omitted. */ const createDescriptors = ({ activeNode, components, state, surfaceActiveNodes, surfaceAnchors, targets }, resolveComponents, previous) => targets.flatMap(target => { var _surfaceActiveNodes$g; const anchorName = surfaceAnchors.get(target.position); if (!anchorName) { return []; } const context = createBlockControlsSurfaceContextForPosition(state, target.position, (_surfaceActiveNodes$g = surfaceActiveNodes === null || surfaceActiveNodes === void 0 ? void 0 : surfaceActiveNodes.get(target.position)) !== null && _surfaceActiveNodes$g !== void 0 ? _surfaceActiveNodes$g : activeNode); if (!context) { return []; } const cached = previous === null || previous === void 0 ? void 0 : previous.get(anchorName); if (cached && cached.position === target.position && cached.source === target.source && areBlockControlsSurfaceContextsEqual(cached.blockControlsContext, context)) { return [cached]; } const surfaceContext = createSurfaceContext(BLOCK_CONTROL_UI_CONTEXT, context); const surfaceComponents = resolveComponents(components, surfaceContext); if (!surfaceComponents) { return []; } return [{ ...target, anchorName, blockControlsContext: context, isTopLevel: context.targetNode.parentType === 'doc', surfaceComponents, surfaceContext }]; }); /** * Creates a cache owned by one editor view and surface side on the gated CLS fix path. * Unchanged descriptors let memoized surfaces skip rendering; preserving the array reference also * lets SurfaceLifecycle skip publishing an otherwise unchanged snapshot. Registry changes or * explicit invalidation force predicates to run again, even if the document context is unchanged. */ export const createSurfaceDescriptorResolver = side => { let descriptors = []; let components = []; return (input, invalidated) => { const registryUnchanged = components.length === input.components.length && components.every((component, index) => component === input.components[index]); const previous = !invalidated && registryUnchanged ? new Map(descriptors.map(descriptor => [descriptor.anchorName, descriptor])) : undefined; const next = createDescriptors(input, side === 'left' ? getBlockControlsLeftSurfaceComponents : getBlockControlsRightSurfaceComponents, previous); components = input.components; if (next.length !== descriptors.length || next.some((descriptor, index) => descriptor !== descriptors[index])) { descriptors = next; } return descriptors; }; };