@atlaskit/editor-plugin-block-controls 20.0.3 → 20.1.1

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 (107) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/dist/cjs/blockControlsPlugin.js +53 -17
  3. package/dist/cjs/editor-commands/show-drag-handle.js +17 -5
  4. package/dist/cjs/pm-plugins/decorations-anchor.js +2 -1
  5. package/dist/cjs/pm-plugins/decorations-drop-target-active.js +2 -1
  6. package/dist/cjs/pm-plugins/decorations-drop-target.js +2 -1
  7. package/dist/cjs/pm-plugins/decorations-surface-anchor.js +72 -0
  8. package/dist/cjs/pm-plugins/handle-mouse-down.js +2 -1
  9. package/dist/cjs/pm-plugins/handle-mouse-over.js +3 -2
  10. package/dist/cjs/pm-plugins/handle-sparse-mouse-over.js +125 -0
  11. package/dist/cjs/pm-plugins/main.js +66 -54
  12. package/dist/cjs/pm-plugins/sparse-surfaces.js +49 -0
  13. package/dist/cjs/pm-plugins/utils/sparse-surface-candidates.js +419 -0
  14. package/dist/cjs/pm-plugins/utils/sparse-surfaces-mapping.js +108 -0
  15. package/dist/cjs/pm-plugins/utils/sparse-surfaces-state.js +53 -0
  16. package/dist/cjs/pm-plugins/utils/sparse-surfaces-view.js +134 -0
  17. package/dist/cjs/pm-plugins/utils/surface-viewport.js +223 -0
  18. package/dist/cjs/pm-plugins/utils/transactions.js +5 -0
  19. package/dist/cjs/ui/block-controls-left-surfaces.js +31 -25
  20. package/dist/cjs/ui/block-controls-right-surfaces.js +31 -25
  21. package/dist/cjs/ui/block-controls-surface-context.js +1 -0
  22. package/dist/cjs/ui/block-controls-surface-drag-handle-registration.js +6 -7
  23. package/dist/cjs/ui/block-controls-surface-drag-handle.js +6 -2
  24. package/dist/cjs/ui/drag-handle.js +5 -3
  25. package/dist/cjs/ui/drop-target-layout.js +3 -2
  26. package/dist/cjs/ui/drop-target.js +3 -2
  27. package/dist/cjs/ui/global-styles.js +69 -27
  28. package/dist/cjs/ui/inline-drop-target.js +6 -5
  29. package/dist/cjs/ui/utils/anchor-name.js +2 -1
  30. package/dist/cjs/ui/utils/dom-attr-name.js +3 -2
  31. package/dist/cjs/ui/utils/get-surface-placement.js +2 -1
  32. package/dist/es2019/blockControlsPlugin.js +42 -8
  33. package/dist/es2019/editor-commands/show-drag-handle.js +14 -2
  34. package/dist/es2019/pm-plugins/decorations-anchor.js +2 -1
  35. package/dist/es2019/pm-plugins/decorations-drop-target-active.js +2 -1
  36. package/dist/es2019/pm-plugins/decorations-drop-target.js +2 -1
  37. package/dist/es2019/pm-plugins/decorations-surface-anchor.js +56 -0
  38. package/dist/es2019/pm-plugins/handle-mouse-down.js +2 -1
  39. package/dist/es2019/pm-plugins/handle-mouse-over.js +3 -2
  40. package/dist/es2019/pm-plugins/handle-sparse-mouse-over.js +116 -0
  41. package/dist/es2019/pm-plugins/main.js +70 -54
  42. package/dist/es2019/pm-plugins/sparse-surfaces.js +37 -0
  43. package/dist/es2019/pm-plugins/utils/sparse-surface-candidates.js +305 -0
  44. package/dist/es2019/pm-plugins/utils/sparse-surfaces-mapping.js +78 -0
  45. package/dist/es2019/pm-plugins/utils/sparse-surfaces-state.js +39 -0
  46. package/dist/es2019/pm-plugins/utils/sparse-surfaces-view.js +121 -0
  47. package/dist/es2019/pm-plugins/utils/surface-viewport.js +192 -0
  48. package/dist/es2019/pm-plugins/utils/transactions.js +5 -0
  49. package/dist/es2019/ui/block-controls-left-surfaces.js +21 -14
  50. package/dist/es2019/ui/block-controls-right-surfaces.js +21 -14
  51. package/dist/es2019/ui/block-controls-surface-context.js +1 -0
  52. package/dist/es2019/ui/block-controls-surface-drag-handle-registration.js +4 -5
  53. package/dist/es2019/ui/block-controls-surface-drag-handle.js +6 -2
  54. package/dist/es2019/ui/drag-handle.js +5 -3
  55. package/dist/es2019/ui/drop-target-layout.js +3 -2
  56. package/dist/es2019/ui/drop-target.js +3 -2
  57. package/dist/es2019/ui/global-styles.js +67 -28
  58. package/dist/es2019/ui/inline-drop-target.js +6 -5
  59. package/dist/es2019/ui/utils/anchor-name.js +2 -1
  60. package/dist/es2019/ui/utils/dom-attr-name.js +3 -2
  61. package/dist/es2019/ui/utils/get-surface-placement.js +2 -1
  62. package/dist/esm/blockControlsPlugin.js +53 -17
  63. package/dist/esm/editor-commands/show-drag-handle.js +17 -5
  64. package/dist/esm/pm-plugins/decorations-anchor.js +2 -1
  65. package/dist/esm/pm-plugins/decorations-drop-target-active.js +2 -1
  66. package/dist/esm/pm-plugins/decorations-drop-target.js +2 -1
  67. package/dist/esm/pm-plugins/decorations-surface-anchor.js +65 -0
  68. package/dist/esm/pm-plugins/handle-mouse-down.js +2 -1
  69. package/dist/esm/pm-plugins/handle-mouse-over.js +3 -2
  70. package/dist/esm/pm-plugins/handle-sparse-mouse-over.js +119 -0
  71. package/dist/esm/pm-plugins/main.js +66 -54
  72. package/dist/esm/pm-plugins/sparse-surfaces.js +43 -0
  73. package/dist/esm/pm-plugins/utils/sparse-surface-candidates.js +413 -0
  74. package/dist/esm/pm-plugins/utils/sparse-surfaces-mapping.js +101 -0
  75. package/dist/esm/pm-plugins/utils/sparse-surfaces-state.js +46 -0
  76. package/dist/esm/pm-plugins/utils/sparse-surfaces-view.js +127 -0
  77. package/dist/esm/pm-plugins/utils/surface-viewport.js +217 -0
  78. package/dist/esm/pm-plugins/utils/transactions.js +5 -0
  79. package/dist/esm/ui/block-controls-left-surfaces.js +33 -27
  80. package/dist/esm/ui/block-controls-right-surfaces.js +33 -27
  81. package/dist/esm/ui/block-controls-surface-context.js +1 -0
  82. package/dist/esm/ui/block-controls-surface-drag-handle-registration.js +7 -8
  83. package/dist/esm/ui/block-controls-surface-drag-handle.js +6 -2
  84. package/dist/esm/ui/drag-handle.js +5 -3
  85. package/dist/esm/ui/drop-target-layout.js +3 -2
  86. package/dist/esm/ui/drop-target.js +3 -2
  87. package/dist/esm/ui/global-styles.js +70 -28
  88. package/dist/esm/ui/inline-drop-target.js +6 -5
  89. package/dist/esm/ui/utils/anchor-name.js +2 -1
  90. package/dist/esm/ui/utils/dom-attr-name.js +3 -2
  91. package/dist/esm/ui/utils/get-surface-placement.js +2 -1
  92. package/dist/types/blockControlsPluginType.d.ts +10 -3
  93. package/dist/types/pm-plugins/decorations-surface-anchor.d.ts +22 -0
  94. package/dist/types/pm-plugins/handle-sparse-mouse-over.d.ts +5 -0
  95. package/dist/types/pm-plugins/main.d.ts +2 -9
  96. package/dist/types/pm-plugins/sparse-surfaces.d.ts +8 -0
  97. package/dist/types/pm-plugins/utils/sparse-surface-candidates.d.ts +89 -0
  98. package/dist/types/pm-plugins/utils/sparse-surfaces-mapping.d.ts +20 -0
  99. package/dist/types/pm-plugins/utils/sparse-surfaces-state.d.ts +27 -0
  100. package/dist/types/pm-plugins/utils/sparse-surfaces-view.d.ts +15 -0
  101. package/dist/types/pm-plugins/utils/surface-viewport.d.ts +10 -0
  102. package/dist/types/ui/utils/get-surface-placement.d.ts +3 -2
  103. package/package.json +6 -6
  104. package/dist/cjs/pm-plugins/utils/surface-node-positions.js +0 -100
  105. package/dist/es2019/pm-plugins/utils/surface-node-positions.js +0 -78
  106. package/dist/esm/pm-plugins/utils/surface-node-positions.js +0 -93
  107. package/dist/types/pm-plugins/utils/surface-node-positions.d.ts +0 -14
@@ -0,0 +1,419 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.reduceSparseSurfaceCandidates = exports.reconcileSparseSurfaceCandidates = exports.mapSparseSurfaceCandidates = exports.emptySparseSurfaceCandidateState = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11
+ var _blockControlUiContext = require("@atlaskit/editor-common/block-controls/block-control-ui-context");
12
+ var _createSurfaceContext = require("@atlaskit/editor-ui-control-model/create-surface-context");
13
+ var _surfaceRenderer = require("@atlaskit/editor-ui-control-model/surface-renderer");
14
+ var _blockControlsSurfaceContext = require("../../ui/block-controls-surface-context");
15
+ var _hasSurfaceControls = require("../../ui/utils/has-surface-controls");
16
+ 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; }
17
+ 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; }
18
+ 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; } } }; }
19
+ 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; } }
20
+ 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; }
21
+ /**
22
+ * Viewport positions are post-transaction positions supplied by the DOM observer. The reducer
23
+ * never needs to inspect DOM or search document IDs for viewport discovery.
24
+ */
25
+
26
+ var NO_ACTIVE = 'none';
27
+ var EMPTY_CANDIDATES = new Map();
28
+ var EMPTY_POSITIONS = [];
29
+ var EMPTY_PROTECTED_POSITIONS = [];
30
+ var emptySparseSurfaceCandidateState = exports.emptySparseSurfaceCandidateState = {
31
+ activeRevision: NO_ACTIVE,
32
+ candidates: EMPTY_CANDIDATES,
33
+ positions: EMPTY_POSITIONS,
34
+ protectedPositions: EMPTY_PROTECTED_POSITIONS,
35
+ visiblePositions: EMPTY_POSITIONS
36
+ };
37
+ var getActiveRevision = function getActiveRevision(activeNode) {
38
+ var _activeNode$rootPos, _activeNode$rootNodeT;
39
+ return activeNode ? [activeNode.pos, activeNode.nodeType, (_activeNode$rootPos = activeNode.rootPos) !== null && _activeNode$rootPos !== void 0 ? _activeNode$rootPos : activeNode.pos, (_activeNode$rootNodeT = activeNode.rootNodeType) !== null && _activeNode$rootNodeT !== void 0 ? _activeNode$rootNodeT : ''].join(':') : NO_ACTIVE;
40
+ };
41
+ var isInRange = function isInRange(position, range) {
42
+ return position >= range.from && position <= range.to;
43
+ };
44
+ var normalizeRange = function normalizeRange(range, contentSize) {
45
+ return {
46
+ from: Math.max(0, Math.min(contentSize, Math.min(range.from, range.to))),
47
+ to: Math.max(0, Math.min(contentSize, Math.max(range.from, range.to)))
48
+ };
49
+ };
50
+ var getSurfaces = function getSurfaces(resolvedSurfaces) {
51
+ return resolvedSurfaces.flatMap(function (_ref) {
52
+ var childrenMap = _ref.childrenMap,
53
+ components = _ref.components,
54
+ root = _ref.root;
55
+ return root && (0, _hasSurfaceControls.hasSurfaceControls)(components) ? [{
56
+ childrenMap: childrenMap,
57
+ root: root
58
+ }] : [];
59
+ });
60
+ };
61
+ var getNodeId = function getNodeId(node) {
62
+ var _node$attrs;
63
+ var localId = (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.localId;
64
+ return typeof localId === 'string' ? localId : undefined;
65
+ };
66
+ var sameCandidate = function sameCandidate(first, second) {
67
+ return (first === null || first === void 0 ? void 0 : first.id) === (second === null || second === void 0 ? void 0 : second.id) && (first === null || first === void 0 ? void 0 : first.contextRevision) === (second === null || second === void 0 ? void 0 : second.contextRevision) && (first === null || first === void 0 ? void 0 : first.isPersistent) === (second === null || second === void 0 ? void 0 : second.isPersistent) && (first === null || first === void 0 ? void 0 : first.nodeRef) === (second === null || second === void 0 ? void 0 : second.nodeRef) && (first === null || first === void 0 ? void 0 : first.nodeType) === (second === null || second === void 0 ? void 0 : second.nodeType) && (first === null || first === void 0 ? void 0 : first.nodeSize) === (second === null || second === void 0 ? void 0 : second.nodeSize) && (first === null || first === void 0 ? void 0 : first.pos) === (second === null || second === void 0 ? void 0 : second.pos) && (first === null || first === void 0 ? void 0 : first.renders) === (second === null || second === void 0 ? void 0 : second.renders);
68
+ };
69
+ var sameCandidateMap = function sameCandidateMap(first, second) {
70
+ if (first.size !== second.size) {
71
+ return false;
72
+ }
73
+ var _iterator = _createForOfIteratorHelper(first),
74
+ _step;
75
+ try {
76
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
77
+ var _step$value = (0, _slicedToArray2.default)(_step.value, 2),
78
+ position = _step$value[0],
79
+ candidate = _step$value[1];
80
+ if (!sameCandidate(candidate, second.get(position))) {
81
+ return false;
82
+ }
83
+ }
84
+ } catch (err) {
85
+ _iterator.e(err);
86
+ } finally {
87
+ _iterator.f();
88
+ }
89
+ return true;
90
+ };
91
+ var samePositions = function samePositions(first, second) {
92
+ return first.length === second.length && first.every(function (position, index) {
93
+ return position === second[index];
94
+ });
95
+ };
96
+ var normalizePositions = function normalizePositions(positions) {
97
+ return Array.from(new Set(positions)).sort(function (first, second) {
98
+ return first - second;
99
+ });
100
+ };
101
+ var mapPositions = function mapPositions(positions, tr) {
102
+ return normalizePositions(positions.map(function (position) {
103
+ return tr.docChanged ? tr.mapping.mapResult(position, 1) : {
104
+ deleted: false,
105
+ pos: position
106
+ };
107
+ }).filter(function (result) {
108
+ return !result.deleted;
109
+ }).map(function (result) {
110
+ return result.pos;
111
+ }));
112
+ };
113
+ var createSurfaceContextAtPosition = function createSurfaceContextAtPosition(state, position, activeNode) {
114
+ var blockControlsContext = (0, _blockControlsSurfaceContext.createBlockControlsSurfaceContextForPosition)(state, position, activeNode);
115
+ return blockControlsContext ? (0, _createSurfaceContext.createSurfaceContext)(_blockControlUiContext.BLOCK_CONTROL_UI_CONTEXT, blockControlsContext) : undefined;
116
+ };
117
+ var willAnySurfaceRenderAt = function willAnySurfaceRenderAt(surfaces, state, position, activeNode) {
118
+ var surfaceContext = createSurfaceContextAtPosition(state, position, activeNode);
119
+ return surfaces.some(function (_ref2) {
120
+ var childrenMap = _ref2.childrenMap,
121
+ root = _ref2.root;
122
+ return (0, _surfaceRenderer.willComponentRender)(root, childrenMap, surfaceContext);
123
+ });
124
+ };
125
+ var addCandidateAt = function addCandidateAt(_ref3) {
126
+ var candidates = _ref3.candidates,
127
+ position = _ref3.position,
128
+ state = _ref3.state,
129
+ surfaces = _ref3.surfaces,
130
+ activeNode = _ref3.activeNode;
131
+ var node = state.doc.nodeAt(position);
132
+ if (!(node !== null && node !== void 0 && node.isBlock)) {
133
+ candidates.delete(position);
134
+ return;
135
+ }
136
+ var isPersistent = willAnySurfaceRenderAt(surfaces, state, position, undefined);
137
+ var renders = isPersistent || activeNode !== undefined && willAnySurfaceRenderAt(surfaces, state, position, activeNode);
138
+ candidates.set(position, {
139
+ contextRevision: getActiveRevision(activeNode),
140
+ id: getNodeId(node),
141
+ isPersistent: isPersistent,
142
+ nodeRef: node,
143
+ nodeType: node.type.name,
144
+ nodeSize: node.nodeSize,
145
+ renders: renders,
146
+ pos: position
147
+ });
148
+ };
149
+ var isInvalidated = function isInvalidated(candidate, position, invalidation, changedRange) {
150
+ var _invalidation$ids, _invalidation$positio, _invalidation$ranges;
151
+ if (invalidation !== null && invalidation !== void 0 && invalidation.all) {
152
+ return true;
153
+ }
154
+ if (invalidation !== null && invalidation !== void 0 && (_invalidation$ids = invalidation.ids) !== null && _invalidation$ids !== void 0 && _invalidation$ids.some(function (id) {
155
+ return id === candidate.id;
156
+ })) {
157
+ return true;
158
+ }
159
+ if (invalidation !== null && invalidation !== void 0 && (_invalidation$positio = invalidation.positions) !== null && _invalidation$positio !== void 0 && _invalidation$positio.includes(position)) {
160
+ return true;
161
+ }
162
+ return Boolean(changedRange && isInRange(position, changedRange) || (invalidation === null || invalidation === void 0 || (_invalidation$ranges = invalidation.ranges) === null || _invalidation$ranges === void 0 ? void 0 : _invalidation$ranges.some(function (range) {
163
+ return isInRange(position, range);
164
+ })));
165
+ };
166
+ var getPositions = function getPositions(candidates) {
167
+ return Array.from(candidates.values()).filter(function (candidate) {
168
+ return candidate.renders;
169
+ }).map(function (candidate) {
170
+ return candidate.pos;
171
+ }).sort(function (first, second) {
172
+ return first - second;
173
+ });
174
+ };
175
+ var mapCandidates = function mapCandidates(state, tr, retain) {
176
+ var mapped = new Map();
177
+ var _iterator2 = _createForOfIteratorHelper(state.candidates.values()),
178
+ _step2;
179
+ try {
180
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
181
+ var candidate = _step2.value;
182
+ var result = tr.docChanged ? tr.mapping.mapResult(candidate.pos, 1) : {
183
+ deleted: false,
184
+ pos: candidate.pos
185
+ };
186
+ if (!result.deleted && retain(result.pos, candidate)) {
187
+ mapped.set(result.pos, result.pos === candidate.pos ? candidate : _objectSpread(_objectSpread({}, candidate), {}, {
188
+ pos: result.pos
189
+ }));
190
+ }
191
+ }
192
+ } catch (err) {
193
+ _iterator2.e(err);
194
+ } finally {
195
+ _iterator2.f();
196
+ }
197
+ return mapped;
198
+ };
199
+ /** Map cache state during PM apply. This phase never calls isHidden or scans doc. */
200
+ var mapSparseSurfaceCandidates = exports.mapSparseSurfaceCandidates = function mapSparseSurfaceCandidates(_ref4) {
201
+ var newState = _ref4.newState,
202
+ previousState = _ref4.previousState,
203
+ tr = _ref4.tr;
204
+ if (!tr.docChanged) {
205
+ return previousState;
206
+ }
207
+ var candidates = new Map();
208
+ var _iterator3 = _createForOfIteratorHelper(previousState.candidates.values()),
209
+ _step3;
210
+ try {
211
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
212
+ var candidate = _step3.value;
213
+ var result = tr.mapping.mapResult(candidate.pos, 1);
214
+ if (!result.deleted) {
215
+ candidates.set(result.pos, result.pos === candidate.pos ? candidate : _objectSpread(_objectSpread({}, candidate), {}, {
216
+ pos: result.pos
217
+ }));
218
+ }
219
+ }
220
+ } catch (err) {
221
+ _iterator3.e(err);
222
+ } finally {
223
+ _iterator3.f();
224
+ }
225
+ var protectedPositions = normalizePositions(previousState.protectedPositions.map(function (position) {
226
+ return tr.mapping.mapResult(position, 1);
227
+ }).filter(function (result) {
228
+ return !result.deleted;
229
+ }).map(function (result) {
230
+ return result.pos;
231
+ }));
232
+ var visiblePositions = mapPositions(previousState.visiblePositions, tr);
233
+ var positions = getPositions(candidates);
234
+ var candidatesUnchanged = sameCandidateMap(previousState.candidates, candidates);
235
+ var positionsUnchanged = samePositions(previousState.positions, positions);
236
+ var protectedUnchanged = samePositions(previousState.protectedPositions, protectedPositions);
237
+ if (candidatesUnchanged && positionsUnchanged && protectedUnchanged && samePositions(previousState.visiblePositions, visiblePositions)) {
238
+ return previousState;
239
+ }
240
+ return _objectSpread(_objectSpread({}, previousState), {}, {
241
+ candidates: candidatesUnchanged ? previousState.candidates : candidates,
242
+ positions: positionsUnchanged ? previousState.positions : positions,
243
+ protectedPositions: protectedUnchanged ? previousState.protectedPositions : protectedPositions,
244
+ visiblePositions: visiblePositions
245
+ });
246
+ };
247
+ /**
248
+ * Reconcile after view state is current. Predicate work stays bounded to cache,
249
+ * exact visible positions, explicit ranges, and protected active/menu positions.
250
+ */
251
+ var reconcileSparseSurfaceCandidates = exports.reconcileSparseSurfaceCandidates = function reconcileSparseSurfaceCandidates(_ref5) {
252
+ var activeNodesByPosition = _ref5.activeNodesByPosition,
253
+ activeNode = _ref5.activeNode,
254
+ changedRange = _ref5.changedRange,
255
+ _ref5$documentChanged = _ref5.documentChanged,
256
+ documentChanged = _ref5$documentChanged === void 0 ? false : _ref5$documentChanged,
257
+ invalidation = _ref5.invalidation,
258
+ newState = _ref5.newState,
259
+ protectedPositions = _ref5.protectedPositions,
260
+ resolvedSurfaces = _ref5.resolvedSurfaces,
261
+ state = _ref5.state,
262
+ visiblePositions = _ref5.visiblePositions;
263
+ return reduceSparseSurfaceCandidates({
264
+ activeNode: activeNode,
265
+ meta: {
266
+ activeNodesByPosition: activeNodesByPosition,
267
+ changedRange: changedRange,
268
+ documentChanged: documentChanged,
269
+ invalidation: invalidation,
270
+ protectedPositions: protectedPositions,
271
+ visiblePositions: visiblePositions
272
+ },
273
+ newState: newState,
274
+ previousState: state,
275
+ resolvedSurfaces: resolvedSurfaces,
276
+ tr: newState.tr
277
+ });
278
+ };
279
+
280
+ /**
281
+ * Update sparse candidates. Viewport and invalidation metadata are the only
282
+ * discovery inputs; active changes re-evaluate retained cache, never document.
283
+ */
284
+ var reduceSparseSurfaceCandidates = exports.reduceSparseSurfaceCandidates = function reduceSparseSurfaceCandidates(_ref6) {
285
+ var _meta$visiblePosition, _meta$protectedPositi, _meta$documentChanged, _invalidation$positio2;
286
+ var activeNode = _ref6.activeNode,
287
+ meta = _ref6.meta,
288
+ newState = _ref6.newState,
289
+ previousState = _ref6.previousState,
290
+ resolvedSurfaces = _ref6.resolvedSurfaces,
291
+ tr = _ref6.tr;
292
+ var activeRevision = getActiveRevision(activeNode);
293
+ var activeChanged = activeRevision !== previousState.activeRevision;
294
+ var hasVisiblePositionsUpdate = Boolean(meta && Object.prototype.hasOwnProperty.call(meta, 'visiblePositions'));
295
+ var visiblePositions = hasVisiblePositionsUpdate ? normalizePositions((_meta$visiblePosition = meta === null || meta === void 0 ? void 0 : meta.visiblePositions) !== null && _meta$visiblePosition !== void 0 ? _meta$visiblePosition : EMPTY_POSITIONS) : tr.docChanged ? mapPositions(previousState.visiblePositions, tr) : previousState.visiblePositions;
296
+ var visiblePositionsChanged = !samePositions(visiblePositions, previousState.visiblePositions);
297
+ var requestedProtectedPositions = normalizePositions((_meta$protectedPositi = meta === null || meta === void 0 ? void 0 : meta.protectedPositions) !== null && _meta$protectedPositi !== void 0 ? _meta$protectedPositi : previousState.protectedPositions);
298
+ var protectedPositions = samePositions(previousState.protectedPositions, requestedProtectedPositions) ? previousState.protectedPositions : requestedProtectedPositions;
299
+ var protectedSet = new Set(protectedPositions);
300
+ if (activeNode) {
301
+ var _activeNode$rootPos2;
302
+ protectedSet.add(activeNode.pos);
303
+ protectedSet.add((_activeNode$rootPos2 = activeNode.rootPos) !== null && _activeNode$rootPos2 !== void 0 ? _activeNode$rootPos2 : activeNode.pos);
304
+ }
305
+ var visibleSet = new Set(visiblePositions);
306
+ var retain = function retain(position, candidate) {
307
+ return ((candidate === null || candidate === void 0 ? void 0 : candidate.isPersistent) || protectedSet.has(position) || visibleSet.has(position)) && position >= 0 && position <= newState.doc.content.size;
308
+ };
309
+ var surfaces = getSurfaces(resolvedSurfaces);
310
+ var activeNodeAt = function activeNodeAt(position) {
311
+ var _ref7, _meta$activeNodesByPo, _meta$activeNodesByPo2;
312
+ return (_ref7 = (_meta$activeNodesByPo = meta === null || meta === void 0 || (_meta$activeNodesByPo2 = meta.activeNodesByPosition) === null || _meta$activeNodesByPo2 === void 0 ? void 0 : _meta$activeNodesByPo2.get(position)) !== null && _meta$activeNodesByPo !== void 0 ? _meta$activeNodesByPo : activeNode) !== null && _ref7 !== void 0 ? _ref7 : undefined;
313
+ };
314
+ var changedRange = meta !== null && meta !== void 0 && meta.changedRange ? normalizeRange(meta.changedRange, newState.doc.content.size) : undefined;
315
+ var invalidation = meta === null || meta === void 0 ? void 0 : meta.invalidation;
316
+ var documentChanged = (_meta$documentChanged = meta === null || meta === void 0 ? void 0 : meta.documentChanged) !== null && _meta$documentChanged !== void 0 ? _meta$documentChanged : tr.docChanged;
317
+ var previousCandidates = mapCandidates(previousState, tr, retain);
318
+ var dirty = function dirty(candidate, position) {
319
+ var currentNode = newState.doc.nodeAt(position);
320
+ var currentContextRevision = getActiveRevision(activeNodeAt(position));
321
+ return activeChanged || candidate.contextRevision !== currentContextRevision || candidate.nodeRef !== currentNode || isInvalidated(candidate, position, invalidation, changedRange);
322
+ };
323
+ if (surfaces.length === 0) {
324
+ var noCandidates = previousCandidates.size === 0;
325
+ if (noCandidates && previousState.positions.length === 0 && samePositions(visiblePositions, previousState.visiblePositions) && activeRevision === previousState.activeRevision && protectedPositions === previousState.protectedPositions) {
326
+ return previousState;
327
+ }
328
+ return {
329
+ activeRevision: activeRevision,
330
+ candidates: noCandidates ? previousState.candidates : EMPTY_CANDIDATES,
331
+ positions: EMPTY_POSITIONS,
332
+ protectedPositions: protectedPositions,
333
+ visiblePositions: visiblePositions
334
+ };
335
+ }
336
+ var _iterator4 = _createForOfIteratorHelper(previousCandidates),
337
+ _step4;
338
+ try {
339
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
340
+ var _step4$value = (0, _slicedToArray2.default)(_step4.value, 2),
341
+ _position = _step4$value[0],
342
+ candidate = _step4$value[1];
343
+ if (dirty(candidate, _position)) {
344
+ addCandidateAt({
345
+ candidates: previousCandidates,
346
+ position: _position,
347
+ state: newState,
348
+ surfaces: surfaces,
349
+ activeNode: activeNodeAt(_position)
350
+ });
351
+ var updatedCandidate = previousCandidates.get(_position);
352
+ if (updatedCandidate && !retain(_position, updatedCandidate)) {
353
+ previousCandidates.delete(_position);
354
+ }
355
+ }
356
+ }
357
+
358
+ // IO supplies exact visible positions. Iterate those positions directly so a long visible
359
+ // container cannot cause a traversal of its entire nested document.
360
+ } catch (err) {
361
+ _iterator4.e(err);
362
+ } finally {
363
+ _iterator4.f();
364
+ }
365
+ if (visiblePositionsChanged || documentChanged || invalidation !== null && invalidation !== void 0 && invalidation.all || changedRange) {
366
+ var _iterator5 = _createForOfIteratorHelper(visiblePositions),
367
+ _step5;
368
+ try {
369
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
370
+ var position = _step5.value;
371
+ if (!previousCandidates.has(position)) {
372
+ addCandidateAt({
373
+ candidates: previousCandidates,
374
+ position: position,
375
+ state: newState,
376
+ surfaces: surfaces,
377
+ activeNode: activeNodeAt(position)
378
+ });
379
+ }
380
+ }
381
+ } catch (err) {
382
+ _iterator5.e(err);
383
+ } finally {
384
+ _iterator5.f();
385
+ }
386
+ }
387
+ var directPositions = new Set([].concat((0, _toConsumableArray2.default)(protectedSet), (0, _toConsumableArray2.default)((_invalidation$positio2 = invalidation === null || invalidation === void 0 ? void 0 : invalidation.positions) !== null && _invalidation$positio2 !== void 0 ? _invalidation$positio2 : [])));
388
+ var _iterator6 = _createForOfIteratorHelper(directPositions),
389
+ _step6;
390
+ try {
391
+ for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
392
+ var _position2 = _step6.value;
393
+ if (retain(_position2) && !previousCandidates.has(_position2)) {
394
+ addCandidateAt({
395
+ candidates: previousCandidates,
396
+ position: _position2,
397
+ state: newState,
398
+ surfaces: surfaces,
399
+ activeNode: activeNodeAt(_position2)
400
+ });
401
+ }
402
+ }
403
+ } catch (err) {
404
+ _iterator6.e(err);
405
+ } finally {
406
+ _iterator6.f();
407
+ }
408
+ var positions = getPositions(previousCandidates);
409
+ if (sameCandidateMap(previousState.candidates, previousCandidates) && samePositions(previousState.positions, positions) && samePositions(previousState.visiblePositions, visiblePositions) && activeRevision === previousState.activeRevision && protectedPositions === previousState.protectedPositions) {
410
+ return previousState;
411
+ }
412
+ return {
413
+ activeRevision: activeRevision,
414
+ candidates: sameCandidateMap(previousState.candidates, previousCandidates) ? previousState.candidates : previousCandidates,
415
+ positions: samePositions(previousState.positions, positions) ? previousState.positions : positions,
416
+ protectedPositions: protectedPositions,
417
+ visiblePositions: visiblePositions
418
+ };
419
+ };
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.reducePendingState = exports.mapTransactionState = exports.isUnchangedTransaction = 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 _sparseSurfaceCandidates = require("./sparse-surface-candidates");
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 mergeInvalidation = function mergeInvalidation(_ref) {
18
+ var _current$ids, _incoming$ids, _current$positions, _incoming$positions, _current$ranges, _incoming$ranges;
19
+ var current = _ref.current,
20
+ incoming = _ref.incoming,
21
+ tr = _ref.tr;
22
+ if (!current && !incoming) {
23
+ return undefined;
24
+ }
25
+ if (!incoming && !tr.docChanged) {
26
+ return current;
27
+ }
28
+ var ids = [].concat((0, _toConsumableArray2.default)((_current$ids = current === null || current === void 0 ? void 0 : current.ids) !== null && _current$ids !== void 0 ? _current$ids : []), (0, _toConsumableArray2.default)((_incoming$ids = incoming === null || incoming === void 0 ? void 0 : incoming.ids) !== null && _incoming$ids !== void 0 ? _incoming$ids : []));
29
+ var positions = [].concat((0, _toConsumableArray2.default)(((_current$positions = current === null || current === void 0 ? void 0 : current.positions) !== null && _current$positions !== void 0 ? _current$positions : []).map(function (position) {
30
+ return tr.mapping.map(position, 1);
31
+ })), (0, _toConsumableArray2.default)((_incoming$positions = incoming === null || incoming === void 0 ? void 0 : incoming.positions) !== null && _incoming$positions !== void 0 ? _incoming$positions : []));
32
+ var ranges = [].concat((0, _toConsumableArray2.default)(((_current$ranges = current === null || current === void 0 ? void 0 : current.ranges) !== null && _current$ranges !== void 0 ? _current$ranges : []).map(function (_ref2) {
33
+ var from = _ref2.from,
34
+ to = _ref2.to;
35
+ return {
36
+ from: tr.mapping.map(from, -1),
37
+ to: tr.mapping.map(to, 1)
38
+ };
39
+ })), (0, _toConsumableArray2.default)((_incoming$ranges = incoming === null || incoming === void 0 ? void 0 : incoming.ranges) !== null && _incoming$ranges !== void 0 ? _incoming$ranges : []));
40
+ return ids.length || positions.length || ranges.length ? {
41
+ ids: ids,
42
+ positions: positions,
43
+ ranges: ranges
44
+ } : undefined;
45
+ };
46
+ var mapAnchors = function mapAnchors(anchors, tr) {
47
+ if (!tr.docChanged || anchors.size === 0) {
48
+ return anchors;
49
+ }
50
+ var changed = false;
51
+ var mappedAnchors = new Map();
52
+ var _iterator = _createForOfIteratorHelper(anchors),
53
+ _step;
54
+ try {
55
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
56
+ var _step$value = (0, _slicedToArray2.default)(_step.value, 2),
57
+ position = _step$value[0],
58
+ name = _step$value[1];
59
+ var mapped = tr.mapping.mapResult(position, 1);
60
+ if (!mapped.deleted) {
61
+ mappedAnchors.set(mapped.pos, name);
62
+ changed || (changed = mapped.pos !== position);
63
+ } else {
64
+ changed = true;
65
+ }
66
+ }
67
+ } catch (err) {
68
+ _iterator.e(err);
69
+ } finally {
70
+ _iterator.f();
71
+ }
72
+ return changed ? mappedAnchors : anchors;
73
+ };
74
+ var reducePendingState = exports.reducePendingState = function reducePendingState(_ref3) {
75
+ var invalidation = _ref3.invalidation,
76
+ previous = _ref3.previous,
77
+ tr = _ref3.tr;
78
+ return {
79
+ invalidation: mergeInvalidation({
80
+ current: previous.invalidation,
81
+ incoming: invalidation,
82
+ tr: tr
83
+ })
84
+ };
85
+ };
86
+ var isUnchangedTransaction = exports.isUnchangedTransaction = function isUnchangedTransaction(_ref4) {
87
+ var pending = _ref4.pending,
88
+ previous = _ref4.previous,
89
+ tr = _ref4.tr;
90
+ return !tr.docChanged && pending.invalidation === previous.invalidation;
91
+ };
92
+ var mapTransactionState = exports.mapTransactionState = function mapTransactionState(_ref5) {
93
+ var newState = _ref5.newState,
94
+ pending = _ref5.pending,
95
+ previous = _ref5.previous,
96
+ tr = _ref5.tr;
97
+ return _objectSpread(_objectSpread({}, previous), {}, {
98
+ anchors: mapAnchors(previous.anchors, tr),
99
+ candidates: tr.docChanged ? (0, _sparseSurfaceCandidates.mapSparseSurfaceCandidates)({
100
+ newState: newState,
101
+ previousState: previous.candidates,
102
+ tr: tr
103
+ }) : previous.candidates,
104
+ decorations: tr.docChanged ? previous.decorations.map(tr.mapping, tr.doc) : previous.decorations,
105
+ documentChanged: previous.documentChanged || tr.docChanged,
106
+ invalidation: pending.invalidation
107
+ });
108
+ };
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.createInitialSparseSurfacesState = exports.applySparseSurfacesTransaction = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _view = require("@atlaskit/editor-prosemirror/view");
10
+ var _sparseSurfacesMapping = require("./sparse-surfaces-mapping");
11
+ 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; }
12
+ 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; }
13
+ var createInitialSparseSurfacesState = exports.createInitialSparseSurfacesState = function createInitialSparseSurfacesState(candidates) {
14
+ return {
15
+ activeNodesByPosition: new Map(),
16
+ anchors: new Map(),
17
+ candidates: candidates,
18
+ decorations: _view.DecorationSet.empty,
19
+ documentChanged: false
20
+ };
21
+ };
22
+ var isReconciliationCommit = function isReconciliationCommit(meta) {
23
+ return (meta === null || meta === void 0 ? void 0 : meta.type) === 'commitReconciliation';
24
+ };
25
+ var commitReconciliation = function commitReconciliation(previous, meta) {
26
+ return _objectSpread(_objectSpread(_objectSpread({}, previous), meta.reconciliation), {}, {
27
+ documentChanged: false,
28
+ invalidation: undefined
29
+ });
30
+ };
31
+ var applySparseSurfacesTransaction = exports.applySparseSurfacesTransaction = function applySparseSurfacesTransaction(input) {
32
+ if (isReconciliationCommit(input.meta)) {
33
+ return commitReconciliation(input.previous, input.meta);
34
+ }
35
+ var pending = (0, _sparseSurfacesMapping.reducePendingState)({
36
+ invalidation: input.invalidation,
37
+ previous: input.previous,
38
+ tr: input.tr
39
+ });
40
+ if ((0, _sparseSurfacesMapping.isUnchangedTransaction)({
41
+ pending: pending,
42
+ previous: input.previous,
43
+ tr: input.tr
44
+ })) {
45
+ return input.previous;
46
+ }
47
+ return (0, _sparseSurfacesMapping.mapTransactionState)({
48
+ newState: input.newState,
49
+ pending: pending,
50
+ previous: input.previous,
51
+ tr: input.tr
52
+ });
53
+ };