@atlaskit/editor-plugin-block-controls 31.0.5 → 31.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/cjs/blockControlsPlugin.js +6 -2
  3. package/dist/cjs/pm-plugins/sparse-gutter-hover.js +23 -2
  4. package/dist/cjs/pm-plugins/sparse-surfaces-view.js +10 -1
  5. package/dist/cjs/surface-lifecycle/are-block-controls-surface-contexts-equal.js +39 -0
  6. package/dist/cjs/surface-lifecycle/block-controls-surface-descriptors.js +83 -0
  7. package/dist/cjs/surface-lifecycle/block-controls-surface-with-cls-fix.js +46 -0
  8. package/dist/cjs/surface-lifecycle/block-controls-surfaces.js +80 -0
  9. package/dist/cjs/surface-lifecycle/sparse-surfaces-view-with-lifecycle.js +287 -0
  10. package/dist/cjs/surface-lifecycle/surface-lifecycle.js +206 -0
  11. package/dist/cjs/surface-lifecycle/use-measured-surface-placements.js +199 -0
  12. package/dist/cjs/ui/block-controls-left-surface.compiled.css +1 -0
  13. package/dist/cjs/ui/block-controls-left-surface.js +61 -16
  14. package/dist/cjs/ui/block-controls-right-surface.compiled.css +1 -0
  15. package/dist/cjs/ui/block-controls-right-surface.js +48 -14
  16. package/dist/es2019/blockControlsPlugin.js +6 -2
  17. package/dist/es2019/pm-plugins/sparse-gutter-hover.js +23 -2
  18. package/dist/es2019/pm-plugins/sparse-surfaces-view.js +10 -1
  19. package/dist/es2019/surface-lifecycle/are-block-controls-surface-contexts-equal.js +31 -0
  20. package/dist/es2019/surface-lifecycle/block-controls-surface-descriptors.js +67 -0
  21. package/dist/es2019/surface-lifecycle/block-controls-surface-with-cls-fix.js +41 -0
  22. package/dist/es2019/surface-lifecycle/block-controls-surfaces.js +69 -0
  23. package/dist/es2019/surface-lifecycle/sparse-surfaces-view-with-lifecycle.js +256 -0
  24. package/dist/es2019/surface-lifecycle/surface-lifecycle.js +125 -0
  25. package/dist/es2019/surface-lifecycle/use-measured-surface-placements.js +163 -0
  26. package/dist/es2019/ui/block-controls-left-surface.compiled.css +1 -0
  27. package/dist/es2019/ui/block-controls-left-surface.js +56 -10
  28. package/dist/es2019/ui/block-controls-right-surface.compiled.css +1 -0
  29. package/dist/es2019/ui/block-controls-right-surface.js +43 -8
  30. package/dist/esm/blockControlsPlugin.js +6 -2
  31. package/dist/esm/pm-plugins/sparse-gutter-hover.js +23 -2
  32. package/dist/esm/pm-plugins/sparse-surfaces-view.js +10 -1
  33. package/dist/esm/surface-lifecycle/are-block-controls-surface-contexts-equal.js +33 -0
  34. package/dist/esm/surface-lifecycle/block-controls-surface-descriptors.js +76 -0
  35. package/dist/esm/surface-lifecycle/block-controls-surface-with-cls-fix.js +39 -0
  36. package/dist/esm/surface-lifecycle/block-controls-surfaces.js +72 -0
  37. package/dist/esm/surface-lifecycle/sparse-surfaces-view-with-lifecycle.js +280 -0
  38. package/dist/esm/surface-lifecycle/surface-lifecycle.js +199 -0
  39. package/dist/esm/surface-lifecycle/use-measured-surface-placements.js +192 -0
  40. package/dist/esm/ui/block-controls-left-surface.compiled.css +1 -0
  41. package/dist/esm/ui/block-controls-left-surface.js +61 -16
  42. package/dist/esm/ui/block-controls-right-surface.compiled.css +1 -0
  43. package/dist/esm/ui/block-controls-right-surface.js +48 -14
  44. package/dist/types/pm-plugins/sparse-surfaces-view.d.ts +2 -1
  45. package/dist/types/surface-lifecycle/are-block-controls-surface-contexts-equal.d.ts +19 -0
  46. package/dist/types/surface-lifecycle/block-controls-surface-descriptors.d.ts +28 -0
  47. package/dist/types/surface-lifecycle/block-controls-surface-with-cls-fix.d.ts +26 -0
  48. package/dist/types/surface-lifecycle/block-controls-surfaces.d.ts +10 -0
  49. package/dist/types/surface-lifecycle/sparse-surfaces-view-with-lifecycle.d.ts +21 -0
  50. package/dist/types/surface-lifecycle/surface-lifecycle.d.ts +46 -0
  51. package/dist/types/surface-lifecycle/use-measured-surface-placements.d.ts +12 -0
  52. package/package.json +2 -2
@@ -0,0 +1,287 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.createSparseSurfacesViewWithLifecycle = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
10
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
+ var _surfaceKeys = require("@atlaskit/editor-common/block-controls/surface-keys");
12
+ var _nodeAnchorProvider = require("@atlaskit/editor-common/node-anchor-provider");
13
+ var _surfaceRenderer = require("@atlaskit/editor-ui-control-model/surface-renderer");
14
+ var _decorationsSurfaceAnchor = require("../pm-plugins/decorations-surface-anchor");
15
+ var _main = require("../pm-plugins/main");
16
+ var _sparseGutterHover = require("../pm-plugins/sparse-gutter-hover");
17
+ var _sparseSurfaceCandidates = require("../pm-plugins/utils/sparse-surface-candidates");
18
+ var _surfaceViewport = require("../pm-plugins/utils/surface-viewport");
19
+ var _blockControlsSurfaceTargets = require("../ui/block-controls-surface-targets");
20
+ var _blockControlsSurfaceDescriptors = require("./block-controls-surface-descriptors");
21
+ var _surfaceLifecycle = require("./surface-lifecycle");
22
+ 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; }
23
+ 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; }
24
+ var haveAnchorPositionsChanged = function haveAnchorPositionsChanged(positions, anchors) {
25
+ return positions.length !== anchors.size || positions.some(function (position) {
26
+ return !anchors.has(position);
27
+ });
28
+ };
29
+ var areAnchorsEqual = function areAnchorsEqual(a, b) {
30
+ return a.size === b.size && (0, _toConsumableArray2.default)(a).every(function (_ref) {
31
+ var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
32
+ position = _ref2[0],
33
+ name = _ref2[1];
34
+ return b.get(position) === name;
35
+ });
36
+ };
37
+
38
+ /** Limited mode and dragging remove every sparse anchor decoration from the document. */
39
+ var areSurfacesSuppressed = function areSurfacesSuppressed(api, state) {
40
+ var _api$limitedMode, _blockControlsKey$get;
41
+ return Boolean((api === null || api === void 0 || (_api$limitedMode = api.limitedMode) === null || _api$limitedMode === void 0 || (_api$limitedMode = _api$limitedMode.sharedState.currentState()) === null || _api$limitedMode === void 0 ? void 0 : _api$limitedMode.enabled) || ((_blockControlsKey$get = _main.key.getState(state)) === null || _blockControlsKey$get === void 0 ? void 0 : _blockControlsKey$get.isDragging));
42
+ };
43
+
44
+ /** An open block menu keeps its trigger node, and that node's root, as active surface positions. */
45
+ var getMenuActiveNodes = function getMenuActiveNodes(controls, doc) {
46
+ var activeNodesByPosition = new Map();
47
+ var menu = controls !== null && controls !== void 0 && controls.isMenuOpen ? controls.menuTriggerByNode : undefined;
48
+ if (menu && controls !== null && controls !== void 0 && controls.menuTriggerBy && doc.nodeAt(menu.pos)) {
49
+ var _menu$rootPos;
50
+ var menuNode = _objectSpread(_objectSpread({}, menu), {}, {
51
+ anchorName: controls.menuTriggerBy
52
+ });
53
+ activeNodesByPosition.set(menu.pos, menuNode);
54
+ activeNodesByPosition.set((_menu$rootPos = menu.rootPos) !== null && _menu$rootPos !== void 0 ? _menu$rootPos : menu.pos, menuNode);
55
+ }
56
+ return activeNodesByPosition;
57
+ };
58
+ var reconcileCandidates = function reconcileCandidates(_ref3) {
59
+ var activeNode = _ref3.activeNode,
60
+ activeNodesByPosition = _ref3.activeNodesByPosition,
61
+ api = _ref3.api,
62
+ previous = _ref3.previous,
63
+ registryChanged = _ref3.registryChanged,
64
+ state = _ref3.state,
65
+ visiblePositions = _ref3.visiblePositions;
66
+ return (0, _sparseSurfaceCandidates.reconcileSparseSurfaceCandidates)({
67
+ activeNode: activeNode,
68
+ activeNodesByPosition: activeNodesByPosition,
69
+ newState: state,
70
+ state: previous.candidates,
71
+ visiblePositions: visiblePositions,
72
+ protectedPositions: (0, _toConsumableArray2.default)(activeNodesByPosition.keys()),
73
+ resolvedSurfaces: [_surfaceKeys.BLOCK_CONTROLS_LEFT_SURFACE, _surfaceKeys.BLOCK_CONTROLS_RIGHT_SURFACE].map(function (surface) {
74
+ var _api$uiControlRegistr, _api$uiControlRegistr2;
75
+ return (0, _surfaceRenderer.resolveSurface)((_api$uiControlRegistr = api === null || api === void 0 || (_api$uiControlRegistr2 = api.uiControlRegistry) === null || _api$uiControlRegistr2 === void 0 ? void 0 : _api$uiControlRegistr2.actions.getComponents(surface)) !== null && _api$uiControlRegistr !== void 0 ? _api$uiControlRegistr : [], surface);
76
+ }),
77
+ documentChanged: previous.documentChanged,
78
+ invalidation: _objectSpread(_objectSpread({}, previous.invalidation), {}, {
79
+ all: registryChanged
80
+ })
81
+ });
82
+ };
83
+
84
+ /** True when the committed state already matches the candidates and no anchor awaits retirement. */
85
+ var isReconciled = function isReconciled(previous, candidates, lifecycle) {
86
+ return !haveAnchorPositionsChanged(candidates.positions, previous.anchors) && candidates === previous.candidates && !previous.documentChanged && !previous.invalidation && !lifecycle.hasRetirableAnchors(previous.decorations);
87
+ };
88
+
89
+ /**
90
+ * Builds anchor decorations for the candidate positions, keeping each node's existing anchor name
91
+ * and any anchor a committed or pending surface still uses.
92
+ */
93
+ var createReconciledDecorations = function createReconciledDecorations(view, previous, positions, lifecycle) {
94
+ var previousAnchors = new Map(previous.decorations.find(undefined, undefined, function (spec) {
95
+ return spec.type === _decorationsSurfaceAnchor.TYPE_SURFACE_ANCHOR_DEC;
96
+ }).map(function (decoration) {
97
+ return [decoration.from, String(decoration.spec.anchorName)];
98
+ }));
99
+ var nodeIdProvider = (0, _nodeAnchorProvider.getNodeIdProvider)(view);
100
+ var _createSurfaceAnchorD = (0, _decorationsSurfaceAnchor.createSurfaceAnchorDecorations)(view.state, positions, function (node, position, previousAnchorName) {
101
+ if (previousAnchorName) {
102
+ nodeIdProvider.setIdForNode(node, previousAnchorName);
103
+ return previousAnchorName;
104
+ }
105
+ return nodeIdProvider.getOrGenerateId(node, position);
106
+ }, previousAnchors),
107
+ nextAnchors = _createSurfaceAnchorD.anchors,
108
+ desiredDecorations = _createSurfaceAnchorD.decorations;
109
+ return {
110
+ // Retiring anchors after a commit leaves the anchor map unchanged, so keep its identity.
111
+ anchors: areAnchorsEqual(nextAnchors, previous.anchors) ? previous.anchors : nextAnchors,
112
+ decorations: lifecycle.retainAnchors(view.state.doc, desiredDecorations, previous.decorations)
113
+ };
114
+ };
115
+ var resolveSurfaceSnapshot = function resolveSurfaceSnapshot(_ref4) {
116
+ var _activeNode$handleOpt, _api$uiControlRegistr3, _api$uiControlRegistr4, _api$uiControlRegistr5, _api$uiControlRegistr6;
117
+ var activeNode = _ref4.activeNode,
118
+ activeNodesByPosition = _ref4.activeNodesByPosition,
119
+ anchors = _ref4.anchors,
120
+ api = _ref4.api,
121
+ invalidated = _ref4.invalidated,
122
+ positions = _ref4.positions,
123
+ resolvers = _ref4.resolvers,
124
+ state = _ref4.state;
125
+ var candidatePositions = new Set(positions);
126
+ var input = {
127
+ activeNode: activeNode,
128
+ state: state,
129
+ surfaceActiveNodes: activeNodesByPosition,
130
+ surfaceAnchors: anchors,
131
+ targets: (0, _blockControlsSurfaceTargets.getBlockControlsSurfaceTargets)(activeNode, positions).filter(function (_ref5) {
132
+ var position = _ref5.position;
133
+ return candidatePositions.has(position);
134
+ })
135
+ };
136
+ return {
137
+ forceVisibleOnMouseOut: Boolean(activeNode === null || activeNode === void 0 || (_activeNode$handleOpt = activeNode.handleOptions) === null || _activeNode$handleOpt === void 0 ? void 0 : _activeNode$handleOpt.isFocused),
138
+ left: resolvers.left(_objectSpread(_objectSpread({}, input), {}, {
139
+ components: (_api$uiControlRegistr3 = api === null || api === void 0 || (_api$uiControlRegistr4 = api.uiControlRegistry) === null || _api$uiControlRegistr4 === void 0 ? void 0 : _api$uiControlRegistr4.actions.getComponents(_surfaceKeys.BLOCK_CONTROLS_LEFT_SURFACE)) !== null && _api$uiControlRegistr3 !== void 0 ? _api$uiControlRegistr3 : []
140
+ }), invalidated),
141
+ right: resolvers.right(_objectSpread(_objectSpread({}, input), {}, {
142
+ components: (_api$uiControlRegistr5 = api === null || api === void 0 || (_api$uiControlRegistr6 = api.uiControlRegistry) === null || _api$uiControlRegistr6 === void 0 ? void 0 : _api$uiControlRegistr6.actions.getComponents(_surfaceKeys.BLOCK_CONTROLS_RIGHT_SURFACE)) !== null && _api$uiControlRegistr5 !== void 0 ? _api$uiControlRegistr5 : []
143
+ }), invalidated)
144
+ };
145
+ };
146
+
147
+ /** Changes that can add or remove anchor suppression, which also invalidates control visibility. */
148
+ var haveSuppressionInputsChanged = function haveSuppressionInputsChanged(controls, previousControls) {
149
+ return (controls === null || controls === void 0 ? void 0 : controls.isDragging) !== (previousControls === null || previousControls === void 0 ? void 0 : previousControls.isDragging) || (controls === null || controls === void 0 ? void 0 : controls.isResizerResizing) !== (previousControls === null || previousControls === void 0 ? void 0 : previousControls.isResizerResizing);
150
+ };
151
+
152
+ /** Changes that can move surfaces between blocks without invalidating control visibility. */
153
+ var haveSurfaceInputsChanged = function haveSurfaceInputsChanged(sparse, controls, previousControls) {
154
+ return Boolean((sparse === null || sparse === void 0 ? void 0 : sparse.documentChanged) || (sparse === null || sparse === void 0 ? void 0 : sparse.invalidation)) || (controls === null || controls === void 0 ? void 0 : controls.activeNode) !== (previousControls === null || previousControls === void 0 ? void 0 : previousControls.activeNode) || (controls === null || controls === void 0 ? void 0 : controls.isMenuOpen) !== (previousControls === null || previousControls === void 0 ? void 0 : previousControls.isMenuOpen) || (controls === null || controls === void 0 ? void 0 : controls.menuTriggerByNode) !== (previousControls === null || previousControls === void 0 ? void 0 : previousControls.menuTriggerByNode);
155
+ };
156
+ var subscribeToInvalidation = function subscribeToInvalidation(api, invalidate) {
157
+ var _api$uiControlRegistr7, _api$editorDisabled, _api$editorViewMode, _api$limitedMode2, _api$showDiff, _api$userIntent;
158
+ var unsubscribers = [api === null || api === void 0 || (_api$uiControlRegistr7 = api.uiControlRegistry) === null || _api$uiControlRegistr7 === void 0 ? void 0 : _api$uiControlRegistr7.actions.subscribe(invalidate), api === null || api === void 0 || (_api$editorDisabled = api.editorDisabled) === null || _api$editorDisabled === void 0 ? void 0 : _api$editorDisabled.sharedState.onChange(invalidate), api === null || api === void 0 || (_api$editorViewMode = api.editorViewMode) === null || _api$editorViewMode === void 0 ? void 0 : _api$editorViewMode.sharedState.onChange(invalidate), api === null || api === void 0 || (_api$limitedMode2 = api.limitedMode) === null || _api$limitedMode2 === void 0 ? void 0 : _api$limitedMode2.sharedState.onChange(invalidate), api === null || api === void 0 || (_api$showDiff = api.showDiff) === null || _api$showDiff === void 0 ? void 0 : _api$showDiff.sharedState.onChange(invalidate), api === null || api === void 0 || (_api$userIntent = api.userIntent) === null || _api$userIntent === void 0 ? void 0 : _api$userIntent.sharedState.onChange(invalidate)];
159
+ return function () {
160
+ return unsubscribers.forEach(function (unsubscribe) {
161
+ return unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe();
162
+ });
163
+ };
164
+ };
165
+
166
+ /**
167
+ * Sparse surfaces plugin view for the CLS fix. Each animation frame reconciles which blocks have
168
+ * surfaces, commits their anchor decorations, and publishes the surfaces React renders through
169
+ * `SurfaceLifecycle`, which retains anchors until React commits their replacement.
170
+ */
171
+ var createSparseSurfacesViewWithLifecycle = exports.createSparseSurfacesViewWithLifecycle = function createSparseSurfacesViewWithLifecycle(_ref6) {
172
+ var api = _ref6.api,
173
+ pluginKey = _ref6.pluginKey,
174
+ view = _ref6.view;
175
+ var win = view.dom.ownerDocument.defaultView;
176
+ var lifecycle = (0, _surfaceLifecycle.getSurfaceLifecycle)(view);
177
+ var resolvers = {
178
+ left: (0, _blockControlsSurfaceDescriptors.createSurfaceDescriptorResolver)('left'),
179
+ right: (0, _blockControlsSurfaceDescriptors.createSurfaceDescriptorResolver)('right')
180
+ };
181
+ var frame;
182
+ var visiblePositions = [];
183
+ var registryChanged = true;
184
+ var surfaceStateChanged = false;
185
+ var updateAvailableAnchors = function updateAvailableAnchors() {
186
+ var sparse = pluginKey.getState(view.state);
187
+ if (sparse) {
188
+ lifecycle.updateAnchors(sparse.decorations, areSurfacesSuppressed(api, view.state));
189
+ }
190
+ };
191
+ var reconcile = function reconcile() {
192
+ var _api$limitedMode3;
193
+ frame = undefined;
194
+ var previous = pluginKey.getState(view.state);
195
+ if (!previous || api !== null && api !== void 0 && (_api$limitedMode3 = api.limitedMode) !== null && _api$limitedMode3 !== void 0 && (_api$limitedMode3 = _api$limitedMode3.sharedState.currentState()) !== null && _api$limitedMode3 !== void 0 && _api$limitedMode3.enabled) {
196
+ return;
197
+ }
198
+ var controls = _main.key.getState(view.state);
199
+ var activeNode = controls === null || controls === void 0 ? void 0 : controls.activeNode;
200
+ var activeNodesByPosition = getMenuActiveNodes(controls, view.state.doc);
201
+ var candidates = reconcileCandidates({
202
+ activeNode: activeNode,
203
+ activeNodesByPosition: activeNodesByPosition,
204
+ api: api,
205
+ previous: previous,
206
+ registryChanged: registryChanged,
207
+ state: view.state,
208
+ visiblePositions: visiblePositions
209
+ });
210
+ var visibilityChanged = registryChanged;
211
+ registryChanged = false;
212
+ if (!surfaceStateChanged && !visibilityChanged && isReconciled(previous, candidates, lifecycle)) {
213
+ return;
214
+ }
215
+ surfaceStateChanged = false;
216
+ var _createReconciledDeco = createReconciledDecorations(view, previous, candidates.positions, lifecycle),
217
+ anchors = _createReconciledDeco.anchors,
218
+ decorations = _createReconciledDeco.decorations;
219
+ view.dispatch(view.state.tr.setMeta(pluginKey, {
220
+ reconciliation: {
221
+ activeNodesByPosition: activeNodesByPosition,
222
+ anchors: anchors,
223
+ candidates: candidates,
224
+ decorations: decorations
225
+ },
226
+ type: 'commitReconciliation'
227
+ }));
228
+ lifecycle.publish(resolveSurfaceSnapshot({
229
+ activeNode: activeNode,
230
+ activeNodesByPosition: activeNodesByPosition,
231
+ anchors: anchors,
232
+ api: api,
233
+ invalidated: visibilityChanged || Boolean(previous.invalidation),
234
+ positions: candidates.positions,
235
+ resolvers: resolvers,
236
+ state: view.state
237
+ }), anchors);
238
+ };
239
+ var schedule = function schedule() {
240
+ if (frame === undefined && win) {
241
+ frame = win.requestAnimationFrame(reconcile);
242
+ }
243
+ };
244
+ var invalidate = function invalidate() {
245
+ if (areSurfacesSuppressed(api, view.state)) {
246
+ updateAvailableAnchors();
247
+ }
248
+ registryChanged = true;
249
+ schedule();
250
+ };
251
+ updateAvailableAnchors();
252
+ // A committed tree may no longer need older anchors; the next reconcile retires them.
253
+ lifecycle.setOnCommit(schedule);
254
+ var observer = (0, _surfaceViewport.observeSurfaceViewport)(view, function (positions) {
255
+ visiblePositions = positions;
256
+ schedule();
257
+ });
258
+ var unsubscribe = subscribeToInvalidation(api, invalidate);
259
+ var unbindGutterHover = (0, _sparseGutterHover.bindSparseGutterHover)({
260
+ api: api,
261
+ view: view
262
+ });
263
+ return {
264
+ update: function update(currentView, previousState) {
265
+ updateAvailableAnchors();
266
+ observer.update();
267
+ var controls = _main.key.getState(currentView.state);
268
+ var previousControls = _main.key.getState(previousState);
269
+ if (haveSuppressionInputsChanged(controls, previousControls)) {
270
+ invalidate();
271
+ }
272
+ if (haveSurfaceInputsChanged(pluginKey.getState(currentView.state), controls, previousControls)) {
273
+ surfaceStateChanged = true;
274
+ schedule();
275
+ }
276
+ },
277
+ destroy: function destroy() {
278
+ if (frame !== undefined) {
279
+ win === null || win === void 0 || win.cancelAnimationFrame(frame);
280
+ }
281
+ lifecycle.destroy();
282
+ observer.destroy();
283
+ unbindGutterHover === null || unbindGutterHover === void 0 || unbindGutterHover();
284
+ unsubscribe();
285
+ }
286
+ };
287
+ };
@@ -0,0 +1,206 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.getSurfaceLifecycle = exports.SurfaceLifecycle = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
10
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
11
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
12
+ 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; }
13
+ 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; }
14
+ 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; } } }; }
15
+ 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; } }
16
+ 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; }
17
+ var filterAvailable = function filterAvailable(descriptors, available) {
18
+ return descriptors.every(function (descriptor) {
19
+ return available.has(descriptor.anchorName);
20
+ }) ? descriptors : descriptors.filter(function (descriptor) {
21
+ return available.has(descriptor.anchorName);
22
+ });
23
+ };
24
+ var areSameDescriptors = function areSameDescriptors(a, b) {
25
+ return a.length === b.length && a.every(function (descriptor, index) {
26
+ return descriptor === b[index];
27
+ });
28
+ };
29
+
30
+ /**
31
+ * Keeps anchor decorations in step with the block-control surfaces React renders.
32
+ * - An anchor stays until React has committed a tree that no longer uses it.
33
+ * - Surfaces whose anchor is gone (deleted block, drag, limited mode) are left out of the snapshot,
34
+ * so React removes them before the browser paints.
35
+ */
36
+ var SurfaceLifecycle = exports.SurfaceLifecycle = /*#__PURE__*/function () {
37
+ function SurfaceLifecycle() {
38
+ var _this = this;
39
+ (0, _classCallCheck2.default)(this, SurfaceLifecycle);
40
+ (0, _defineProperty2.default)(this, "committedRevision", 0);
41
+ (0, _defineProperty2.default)(this, "revision", 0);
42
+ (0, _defineProperty2.default)(this, "lastUsedRevision", new Map());
43
+ (0, _defineProperty2.default)(this, "listeners", new Set());
44
+ (0, _defineProperty2.default)(this, "availableAnchors", new Set());
45
+ (0, _defineProperty2.default)(this, "publishedAnchors", new Set());
46
+ (0, _defineProperty2.default)(this, "suppressed", false);
47
+ (0, _defineProperty2.default)(this, "getSnapshot", function () {
48
+ return _this.snapshot;
49
+ });
50
+ (0, _defineProperty2.default)(this, "subscribe", function (listener) {
51
+ _this.listeners.add(listener);
52
+ return function () {
53
+ return _this.listeners.delete(listener);
54
+ };
55
+ });
56
+ (0, _defineProperty2.default)(this, "acknowledge", function (revision) {
57
+ var _this$published$revis, _this$published, _this$onCommit;
58
+ 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)) {
59
+ return;
60
+ }
61
+ _this.committedRevision = revision;
62
+ (_this$onCommit = _this.onCommit) === null || _this$onCommit === void 0 || _this$onCommit.call(_this);
63
+ });
64
+ }
65
+ return (0, _createClass2.default)(SurfaceLifecycle, [{
66
+ key: "setOnCommit",
67
+ value: function setOnCommit(onCommit) {
68
+ this.onCommit = onCommit;
69
+ }
70
+ }, {
71
+ key: "publish",
72
+ value: function publish(snapshot, anchors) {
73
+ var _this$published2,
74
+ _this2 = this;
75
+ 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) {
76
+ return _this2.publishedAnchors.has(name);
77
+ })) {
78
+ return;
79
+ }
80
+ this.publishedAnchors = new Set(anchors.values());
81
+ var revision = ++this.revision;
82
+ var _iterator = _createForOfIteratorHelper(anchors.values()),
83
+ _step;
84
+ try {
85
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
86
+ var name = _step.value;
87
+ this.lastUsedRevision.set(name, revision);
88
+ }
89
+ } catch (err) {
90
+ _iterator.e(err);
91
+ } finally {
92
+ _iterator.f();
93
+ }
94
+ this.published = _objectSpread(_objectSpread({}, snapshot), {}, {
95
+ revision: revision
96
+ });
97
+ this.updateSnapshot(true);
98
+ }
99
+ }, {
100
+ key: "retainAnchors",
101
+ value: function retainAnchors(doc, desired, previous) {
102
+ var _this3 = this;
103
+ var positions = new Set(desired.find().map(function (decoration) {
104
+ return decoration.from;
105
+ }));
106
+ var retained = previous.find().filter(function (decoration) {
107
+ return !positions.has(decoration.from) && _this3.isRetained(decoration.spec.anchorName);
108
+ });
109
+ return retained.length ? desired.add(doc, retained) : desired;
110
+ }
111
+
112
+ /** Whether a reconcile would drop anchors that no committed or pending tree still uses. */
113
+ }, {
114
+ key: "hasRetirableAnchors",
115
+ value: function hasRetirableAnchors(decorations) {
116
+ var _this4 = this;
117
+ return decorations.find().some(function (decoration) {
118
+ return !_this4.isRetained(decoration.spec.anchorName);
119
+ });
120
+ }
121
+ }, {
122
+ key: "isRetained",
123
+ value: function isRetained(name) {
124
+ var revision = this.lastUsedRevision.get(name);
125
+ // Keep the committed tree and every newer published tree until a later commit replaces them.
126
+ return revision !== undefined && revision >= this.committedRevision;
127
+ }
128
+
129
+ /**
130
+ * Records which anchors the browser can currently resolve. Removes surfaces for missing or
131
+ * suppressed anchors from the snapshot synchronously, ahead of the next reconcile.
132
+ */
133
+ }, {
134
+ key: "updateAnchors",
135
+ value: function updateAnchors(decorations, suppressed) {
136
+ if (decorations === this.decorations && suppressed === this.suppressed) {
137
+ return;
138
+ }
139
+ this.decorations = decorations;
140
+ this.suppressed = suppressed;
141
+ var names = new Set(decorations.find().map(function (decoration) {
142
+ return decoration.spec.anchorName;
143
+ }));
144
+ this.availableAnchors = suppressed ? new Set() : names;
145
+ var _iterator2 = _createForOfIteratorHelper(this.lastUsedRevision.keys()),
146
+ _step2;
147
+ try {
148
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
149
+ var name = _step2.value;
150
+ if (!names.has(name)) {
151
+ this.lastUsedRevision.delete(name);
152
+ }
153
+ }
154
+ } catch (err) {
155
+ _iterator2.e(err);
156
+ } finally {
157
+ _iterator2.f();
158
+ }
159
+ this.updateSnapshot(false);
160
+ }
161
+
162
+ /** Derives the snapshot React reads, replacing it only when the rendered surfaces change. */
163
+ }, {
164
+ key: "updateSnapshot",
165
+ value: function updateSnapshot(published) {
166
+ if (!this.published) {
167
+ return;
168
+ }
169
+ var left = filterAvailable(this.published.left, this.availableAnchors);
170
+ var right = filterAvailable(this.published.right, this.availableAnchors);
171
+ if (!published && this.snapshot && areSameDescriptors(left, this.snapshot.left) && areSameDescriptors(right, this.snapshot.right)) {
172
+ return;
173
+ }
174
+ this.snapshot = _objectSpread(_objectSpread({}, this.published), {}, {
175
+ left: left,
176
+ right: right
177
+ });
178
+ this.listeners.forEach(function (listener) {
179
+ return listener();
180
+ });
181
+ }
182
+ }, {
183
+ key: "destroy",
184
+ value: function destroy() {
185
+ this.availableAnchors = new Set();
186
+ this.publishedAnchors.clear();
187
+ this.lastUsedRevision.clear();
188
+ this.onCommit = undefined;
189
+ this.decorations = undefined;
190
+ this.published = undefined;
191
+ this.snapshot = undefined;
192
+ this.listeners.forEach(function (listener) {
193
+ return listener();
194
+ });
195
+ }
196
+ }]);
197
+ }();
198
+ var lifecycles = new WeakMap();
199
+ var getSurfaceLifecycle = exports.getSurfaceLifecycle = function getSurfaceLifecycle(view) {
200
+ var lifecycle = lifecycles.get(view);
201
+ if (!lifecycle) {
202
+ lifecycle = new SurfaceLifecycle();
203
+ lifecycles.set(view, lifecycle);
204
+ }
205
+ return lifecycle;
206
+ };