@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
76 lines (75 loc) • 4.65 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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.
*/
var createDescriptors = function createDescriptors(_ref, resolveComponents, previous) {
var activeNode = _ref.activeNode,
components = _ref.components,
state = _ref.state,
surfaceActiveNodes = _ref.surfaceActiveNodes,
surfaceAnchors = _ref.surfaceAnchors,
targets = _ref.targets;
return targets.flatMap(function (target) {
var _surfaceActiveNodes$g;
var anchorName = surfaceAnchors.get(target.position);
if (!anchorName) {
return [];
}
var 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 [];
}
var 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];
}
var surfaceContext = createSurfaceContext(BLOCK_CONTROL_UI_CONTEXT, context);
var surfaceComponents = resolveComponents(components, surfaceContext);
if (!surfaceComponents) {
return [];
}
return [_objectSpread(_objectSpread({}, target), {}, {
anchorName: anchorName,
blockControlsContext: context,
isTopLevel: context.targetNode.parentType === 'doc',
surfaceComponents: surfaceComponents,
surfaceContext: 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 var createSurfaceDescriptorResolver = function createSurfaceDescriptorResolver(side) {
var descriptors = [];
var components = [];
return function (input, invalidated) {
var registryUnchanged = components.length === input.components.length && components.every(function (component, index) {
return component === input.components[index];
});
var previous = !invalidated && registryUnchanged ? new Map(descriptors.map(function (descriptor) {
return [descriptor.anchorName, descriptor];
})) : undefined;
var next = createDescriptors(input, side === 'left' ? getBlockControlsLeftSurfaceComponents : getBlockControlsRightSurfaceComponents, previous);
components = input.components;
if (next.length !== descriptors.length || next.some(function (descriptor, index) {
return descriptor !== descriptors[index];
})) {
descriptors = next;
}
return descriptors;
};
};