@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
33 lines (30 loc) • 2.12 kB
JavaScript
/**
* Resolves to `T` only when `Compared` lists every key of `T`. Adding a field to the context types
* turns the comparators' parameters into `never`, so callers stop compiling until the comparison
* handles the new field.
*/
/**
* `controlVisibility` is derived from component predicates after the context is created, never by
* `createBlockControlsSurfaceContextForPosition`, so it is not compared.
*/
/**
* ProseMirror preserves immutable node objects for unchanged content, so node identity detects
* content changes without a deep comparison. Position and ancestry must also match: an unchanged
* node can move or gain a different parent, which changes the context supplied to its controls.
* The ancestor comparison is O(depth); it never walks document children or reads DOM geometry.
*/
var areNodeContextsEqual = function areNodeContextsEqual(a, b) {
var _a$rootType, _b$rootType;
return a === b || Boolean(a && b && a.node === b.node && a.pos === b.pos && a.depth === b.depth && a.parentType === b.parentType && a.type.name === b.type.name && a.rootPos === b.rootPos && ((_a$rootType = a.rootType) === null || _a$rootType === void 0 ? void 0 : _a$rootType.name) === ((_b$rootType = b.rootType) === null || _b$rootType === void 0 ? void 0 : _b$rootType.name) && a.ancestors.length === b.ancestors.length && a.ancestors.every(function (ancestor, index) {
var other = b.ancestors[index];
return ancestor.pos === other.pos && ancestor.depth === other.depth && ancestor.type.name === other.type.name;
}));
};
/**
* Compares contexts produced by `createBlockControlsSurfaceContextForPosition`. Includes root and
* active-node context because controls can depend on more than the target itself, for example a
* changed containing block or a different active control.
*/
export var areBlockControlsSurfaceContextsEqual = function areBlockControlsSurfaceContextsEqual(a, b) {
return a.activeControlKey === b.activeControlKey && areNodeContextsEqual(a.targetNode, b.targetNode) && areNodeContextsEqual(a.rootNode, b.rootNode) && areNodeContextsEqual(a.activeNode, b.activeNode);
};