@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
125 lines (121 loc) • 5.47 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
const filterAvailable = (descriptors, available) => descriptors.every(descriptor => available.has(descriptor.anchorName)) ? descriptors : descriptors.filter(descriptor => available.has(descriptor.anchorName));
const areSameDescriptors = (a, b) => a.length === b.length && a.every((descriptor, index) => descriptor === b[index]);
/**
* Keeps anchor decorations in step with the block-control surfaces React renders.
* - An anchor stays until React has committed a tree that no longer uses it.
* - Surfaces whose anchor is gone (deleted block, drag, limited mode) are left out of the snapshot,
* so React removes them before the browser paints.
*/
export class SurfaceLifecycle {
constructor() {
_defineProperty(this, "committedRevision", 0);
_defineProperty(this, "revision", 0);
_defineProperty(this, "lastUsedRevision", new Map());
_defineProperty(this, "listeners", new Set());
_defineProperty(this, "availableAnchors", new Set());
_defineProperty(this, "publishedAnchors", new Set());
_defineProperty(this, "suppressed", false);
_defineProperty(this, "getSnapshot", () => this.snapshot);
_defineProperty(this, "subscribe", listener => {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
});
_defineProperty(this, "acknowledge", revision => {
var _this$published$revis, _this$published, _this$onCommit;
if (revision <= this.committedRevision || revision > ((_this$published$revis = (_this$published = this.published) === null || _this$published === void 0 ? void 0 : _this$published.revision) !== null && _this$published$revis !== void 0 ? _this$published$revis : 0)) {
return;
}
this.committedRevision = revision;
(_this$onCommit = this.onCommit) === null || _this$onCommit === void 0 ? void 0 : _this$onCommit.call(this);
});
}
setOnCommit(onCommit) {
this.onCommit = onCommit;
}
publish(snapshot, anchors) {
var _this$published2;
if (((_this$published2 = this.published) === null || _this$published2 === void 0 ? void 0 : _this$published2.left) === snapshot.left && this.published.right === snapshot.right && this.published.forceVisibleOnMouseOut === snapshot.forceVisibleOnMouseOut && this.publishedAnchors.size === anchors.size && [...anchors.values()].every(name => this.publishedAnchors.has(name))) {
return;
}
this.publishedAnchors = new Set(anchors.values());
const revision = ++this.revision;
for (const name of anchors.values()) {
this.lastUsedRevision.set(name, revision);
}
this.published = {
...snapshot,
revision
};
this.updateSnapshot(true);
}
retainAnchors(doc, desired, previous) {
const positions = new Set(desired.find().map(decoration => decoration.from));
const retained = previous.find().filter(decoration => !positions.has(decoration.from) && this.isRetained(decoration.spec.anchorName));
return retained.length ? desired.add(doc, retained) : desired;
}
/** Whether a reconcile would drop anchors that no committed or pending tree still uses. */
hasRetirableAnchors(decorations) {
return decorations.find().some(decoration => !this.isRetained(decoration.spec.anchorName));
}
isRetained(name) {
const revision = this.lastUsedRevision.get(name);
// Keep the committed tree and every newer published tree until a later commit replaces them.
return revision !== undefined && revision >= this.committedRevision;
}
/**
* Records which anchors the browser can currently resolve. Removes surfaces for missing or
* suppressed anchors from the snapshot synchronously, ahead of the next reconcile.
*/
updateAnchors(decorations, suppressed) {
if (decorations === this.decorations && suppressed === this.suppressed) {
return;
}
this.decorations = decorations;
this.suppressed = suppressed;
const names = new Set(decorations.find().map(decoration => decoration.spec.anchorName));
this.availableAnchors = suppressed ? new Set() : names;
for (const name of this.lastUsedRevision.keys()) {
if (!names.has(name)) {
this.lastUsedRevision.delete(name);
}
}
this.updateSnapshot(false);
}
/** Derives the snapshot React reads, replacing it only when the rendered surfaces change. */
updateSnapshot(published) {
if (!this.published) {
return;
}
const left = filterAvailable(this.published.left, this.availableAnchors);
const right = filterAvailable(this.published.right, this.availableAnchors);
if (!published && this.snapshot && areSameDescriptors(left, this.snapshot.left) && areSameDescriptors(right, this.snapshot.right)) {
return;
}
this.snapshot = {
...this.published,
left,
right
};
this.listeners.forEach(listener => listener());
}
destroy() {
this.availableAnchors = new Set();
this.publishedAnchors.clear();
this.lastUsedRevision.clear();
this.onCommit = undefined;
this.decorations = undefined;
this.published = undefined;
this.snapshot = undefined;
this.listeners.forEach(listener => listener());
}
}
const lifecycles = new WeakMap();
export const getSurfaceLifecycle = view => {
let lifecycle = lifecycles.get(view);
if (!lifecycle) {
lifecycle = new SurfaceLifecycle();
lifecycles.set(view, lifecycle);
}
return lifecycle;
};