@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
206 lines (201 loc) • 10.2 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getSurfaceLifecycle = exports.SurfaceLifecycle = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@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) { (0, _defineProperty2.default)(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; }
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var filterAvailable = function filterAvailable(descriptors, available) {
return descriptors.every(function (descriptor) {
return available.has(descriptor.anchorName);
}) ? descriptors : descriptors.filter(function (descriptor) {
return available.has(descriptor.anchorName);
});
};
var areSameDescriptors = function areSameDescriptors(a, b) {
return a.length === b.length && a.every(function (descriptor, index) {
return 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.
*/
var SurfaceLifecycle = exports.SurfaceLifecycle = /*#__PURE__*/function () {
function SurfaceLifecycle() {
var _this = this;
(0, _classCallCheck2.default)(this, SurfaceLifecycle);
(0, _defineProperty2.default)(this, "committedRevision", 0);
(0, _defineProperty2.default)(this, "revision", 0);
(0, _defineProperty2.default)(this, "lastUsedRevision", new Map());
(0, _defineProperty2.default)(this, "listeners", new Set());
(0, _defineProperty2.default)(this, "availableAnchors", new Set());
(0, _defineProperty2.default)(this, "publishedAnchors", new Set());
(0, _defineProperty2.default)(this, "suppressed", false);
(0, _defineProperty2.default)(this, "getSnapshot", function () {
return _this.snapshot;
});
(0, _defineProperty2.default)(this, "subscribe", function (listener) {
_this.listeners.add(listener);
return function () {
return _this.listeners.delete(listener);
};
});
(0, _defineProperty2.default)(this, "acknowledge", function (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 || _this$onCommit.call(_this);
});
}
return (0, _createClass2.default)(SurfaceLifecycle, [{
key: "setOnCommit",
value: function setOnCommit(onCommit) {
this.onCommit = onCommit;
}
}, {
key: "publish",
value: function publish(snapshot, anchors) {
var _this$published2,
_this2 = this;
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 && (0, _toConsumableArray2.default)(anchors.values()).every(function (name) {
return _this2.publishedAnchors.has(name);
})) {
return;
}
this.publishedAnchors = new Set(anchors.values());
var revision = ++this.revision;
var _iterator = _createForOfIteratorHelper(anchors.values()),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var name = _step.value;
this.lastUsedRevision.set(name, revision);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
this.published = _objectSpread(_objectSpread({}, snapshot), {}, {
revision: revision
});
this.updateSnapshot(true);
}
}, {
key: "retainAnchors",
value: function retainAnchors(doc, desired, previous) {
var _this3 = this;
var positions = new Set(desired.find().map(function (decoration) {
return decoration.from;
}));
var retained = previous.find().filter(function (decoration) {
return !positions.has(decoration.from) && _this3.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. */
}, {
key: "hasRetirableAnchors",
value: function hasRetirableAnchors(decorations) {
var _this4 = this;
return decorations.find().some(function (decoration) {
return !_this4.isRetained(decoration.spec.anchorName);
});
}
}, {
key: "isRetained",
value: function isRetained(name) {
var 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.
*/
}, {
key: "updateAnchors",
value: function updateAnchors(decorations, suppressed) {
if (decorations === this.decorations && suppressed === this.suppressed) {
return;
}
this.decorations = decorations;
this.suppressed = suppressed;
var names = new Set(decorations.find().map(function (decoration) {
return decoration.spec.anchorName;
}));
this.availableAnchors = suppressed ? new Set() : names;
var _iterator2 = _createForOfIteratorHelper(this.lastUsedRevision.keys()),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var name = _step2.value;
if (!names.has(name)) {
this.lastUsedRevision.delete(name);
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
this.updateSnapshot(false);
}
/** Derives the snapshot React reads, replacing it only when the rendered surfaces change. */
}, {
key: "updateSnapshot",
value: function updateSnapshot(published) {
if (!this.published) {
return;
}
var left = filterAvailable(this.published.left, this.availableAnchors);
var right = filterAvailable(this.published.right, this.availableAnchors);
if (!published && this.snapshot && areSameDescriptors(left, this.snapshot.left) && areSameDescriptors(right, this.snapshot.right)) {
return;
}
this.snapshot = _objectSpread(_objectSpread({}, this.published), {}, {
left: left,
right: right
});
this.listeners.forEach(function (listener) {
return listener();
});
}
}, {
key: "destroy",
value: function 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(function (listener) {
return listener();
});
}
}]);
}();
var lifecycles = new WeakMap();
var getSurfaceLifecycle = exports.getSurfaceLifecycle = function getSurfaceLifecycle(view) {
var lifecycle = lifecycles.get(view);
if (!lifecycle) {
lifecycle = new SurfaceLifecycle();
lifecycles.set(view, lifecycle);
}
return lifecycle;
};