UNPKG

@atlaskit/editor-plugin-block-controls

Version:

Block controls plugin for @atlaskit/editor-core

199 lines (195 loc) • 9.59 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; 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; } 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. */ export var SurfaceLifecycle = /*#__PURE__*/function () { function SurfaceLifecycle() { var _this = this; _classCallCheck(this, SurfaceLifecycle); _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", function () { return _this.snapshot; }); _defineProperty(this, "subscribe", function (listener) { _this.listeners.add(listener); return function () { return _this.listeners.delete(listener); }; }); _defineProperty(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 _createClass(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 && _toConsumableArray(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(); export var getSurfaceLifecycle = function getSurfaceLifecycle(view) { var lifecycle = lifecycles.get(view); if (!lifecycle) { lifecycle = new SurfaceLifecycle(); lifecycles.set(view, lifecycle); } return lifecycle; };