@atlaskit/editor-common 130.1.0 → 130.2.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.
@@ -0,0 +1,463 @@
1
+ import isEqual from 'lodash/isEqual';
2
+ // localId is identity metadata: regenerating it alone should not create activity.
3
+ const sameAttrs = (left, right) => {
4
+ const keys = Object.keys(left).filter(key => key !== 'localId');
5
+ return keys.length === Object.keys(right).filter(key => key !== 'localId').length && keys.every(key => isEqual(left[key], right[key]));
6
+ };
7
+
8
+ // Compare the node's own type, attributes and marks, without comparing its children.
9
+ const sameMarkup = (left, right) => left.type === right.type && sameAttrs(left.attrs, right.attrs) && left.marks.length === right.marks.length && left.marks.every((mark, index) => mark.type === right.marks[index].type && sameAttrs(mark.attrs, right.marks[index].attrs));
10
+ const sameNode = (left, right) => {
11
+ if (left === right) {
12
+ return true;
13
+ }
14
+ if (!sameMarkup(left, right) || left.text !== right.text || left.childCount !== right.childCount) {
15
+ return false;
16
+ }
17
+ for (let index = 0; index < left.childCount; index++) {
18
+ if (!sameNode(left.child(index), right.child(index))) {
19
+ return false;
20
+ }
21
+ }
22
+ return true;
23
+ };
24
+
25
+ // Positions include document structure, not just character offsets. A non-leaf node
26
+ // uses one position for each opening/closing boundary, plus the size of its content.
27
+ // Example: paragraph('Hi') at pos 0 has nodeSize 4, outer range [0, 4),
28
+ // and text content [1, 3). All ranges here use an exclusive end position.
29
+
30
+ // Keep the reason internally so a panel attribute change can survive child edits.
31
+ // Only ordinary { from, to } ranges are returned to the chrome consumer.
32
+
33
+ const getSnapshotIndex = (doc, indexes) => {
34
+ const cached = indexes.get(doc);
35
+ if (cached) {
36
+ return cached;
37
+ }
38
+ const snapshot = {
39
+ byId: new Map(),
40
+ byPosition: new Map(),
41
+ nodes: [],
42
+ textblocks: []
43
+ };
44
+ const parents = [{
45
+ node: doc,
46
+ pos: -1,
47
+ index: 0
48
+ }];
49
+ doc.descendants((node, pos, _parent, index) => {
50
+ // Track ancestry by position because one immutable node can occupy several slots.
51
+ while (parents.length > 1 && pos >= parents[parents.length - 1].pos + parents[parents.length - 1].node.nodeSize - 1) {
52
+ parents.pop();
53
+ }
54
+ const positioned = {
55
+ node,
56
+ pos,
57
+ parent: parents[parents.length - 1],
58
+ index
59
+ };
60
+ snapshot.byPosition.set(pos, positioned);
61
+ snapshot.nodes.push(positioned);
62
+ if (node.isTextblock) {
63
+ snapshot.textblocks.push(positioned);
64
+ }
65
+ const id = node.attrs.localId;
66
+ if (typeof id === 'string' && id) {
67
+ snapshot.byId.set(id, snapshot.byId.has(id) ? null : positioned);
68
+ }
69
+ if (!node.isLeaf) {
70
+ parents.push(positioned);
71
+ }
72
+ });
73
+ indexes.set(doc, snapshot);
74
+ return snapshot;
75
+ };
76
+ const indexedNodeAt = (doc, pos, indexes) => {
77
+ const snapshot = getSnapshotIndex(doc, indexes);
78
+ const exact = snapshot.byPosition.get(pos);
79
+ if (exact) {
80
+ return exact.node;
81
+ }
82
+ // nodeAt also returns a text node when the position is inside its text.
83
+ let low = 0;
84
+ let high = snapshot.nodes.length;
85
+ while (low < high) {
86
+ const middle = low + high >>> 1;
87
+ if (snapshot.nodes[middle].pos <= pos) {
88
+ low = middle + 1;
89
+ } else {
90
+ high = middle;
91
+ }
92
+ }
93
+ const previous = snapshot.nodes[low - 1];
94
+ return previous !== null && previous !== void 0 && previous.node.isText && pos < previous.pos + previous.node.nodeSize ? previous.node : undefined;
95
+ };
96
+ const collectChanges = (before, after, map, ranges) => {
97
+ const {
98
+ node,
99
+ pos
100
+ } = after;
101
+ if (before && sameNode(before.node, node)) {
102
+ return;
103
+ }
104
+ // Text edits belong to their textblock. A node's own markup change instead
105
+ // owns its full content, so we stop here rather than adding child ranges too.
106
+ const markupChanged = !!before && node.isBlock && !sameMarkup(before.node, node);
107
+ if (node.isTextblock || markupChanged) {
108
+ if (node.content.size > 0) {
109
+ ranges.push({
110
+ from: pos,
111
+ to: pos + node.nodeSize,
112
+ markupChanged
113
+ });
114
+ }
115
+ return;
116
+ }
117
+ const byPosition = new Map();
118
+ const byId = new Map();
119
+ const oldChildren = [];
120
+ before === null || before === void 0 ? void 0 : before.node.forEach((child, offset) => {
121
+ // forEach offsets start inside the parent. Skip its opening boundary (+1)
122
+ // to convert this child offset into an absolute document position.
123
+ const previous = {
124
+ node: child,
125
+ pos: before.pos + 1 + offset
126
+ };
127
+ oldChildren.push(previous);
128
+ // This 1 is a mapping bias, not an added offset: follow the right side
129
+ // of an insertion at the child's start, keeping the original child after it.
130
+ const mapped = map.mapResult(previous.pos, 1);
131
+ if (!mapped.deleted) {
132
+ byPosition.set(mapped.pos, previous);
133
+ }
134
+ const id = child.attrs.localId;
135
+ if (typeof id === 'string' && id) {
136
+ // Duplicate IDs cannot identify a unique old child.
137
+ byId.set(id, byId.has(id) ? null : previous);
138
+ }
139
+ });
140
+ const nextChildren = [];
141
+ node.forEach((child, offset) => nextChildren.push({
142
+ node: child,
143
+ pos: pos + 1 + offset
144
+ }));
145
+ // Narrow edits usually preserve mapped positions. Whole-container replacements
146
+ // may erase those positions, so stable child IDs provide a second way to match.
147
+ const preferred = nextChildren.map(next => {
148
+ var _ref, _byPosition$get;
149
+ return (_ref = (_byPosition$get = byPosition.get(next.pos)) !== null && _byPosition$get !== void 0 ? _byPosition$get : byId.get(next.node.attrs.localId)) !== null && _ref !== void 0 ? _ref : undefined;
150
+ });
151
+ const claims = new Map();
152
+ for (const match of preferred) {
153
+ if (match) {
154
+ var _claims$get;
155
+ claims.set(match, ((_claims$get = claims.get(match)) !== null && _claims$get !== void 0 ? _claims$get : 0) + 1);
156
+ }
157
+ }
158
+ nextChildren.forEach((next, index) => {
159
+ const match = preferred[index];
160
+ // Same-index fallback supports replacements that regenerate IDs but keep
161
+ // the child structure. Different child counts make that correspondence unsafe.
162
+ const aligned = oldChildren.length === node.childCount ? oldChildren[index] : undefined;
163
+ // Reserve every position/ID match before fallback, including later siblings.
164
+ // Reusing one old sibling can otherwise disguise an equal-text insertion.
165
+ const previous = match && claims.get(match) === 1 ? match : aligned && !claims.has(aligned) ? aligned : undefined;
166
+ collectChanges(previous, next, map, ranges);
167
+ });
168
+ };
169
+ const findPreservedNode = (before, doc, previous, markupChanged, indexes) => {
170
+ const id = previous.attrs.localId;
171
+ if (typeof id !== 'string' || !id) {
172
+ return;
173
+ }
174
+ // When source uniqueness is required, removing a duplicate must not transfer
175
+ // its activity to the remaining equal-content sibling.
176
+ if (before && !getSnapshotIndex(before, indexes).byId.get(id)) {
177
+ return;
178
+ }
179
+ const match = getSnapshotIndex(doc, indexes).byId.get(id);
180
+ // Attribute/mark activity belongs to the node even when its children are edited.
181
+ // Content-only activity still requires the entire candidate to survive unchanged.
182
+ const isPreserved = markupChanged ? sameMarkup : sameNode;
183
+ return match && isPreserved(previous, match.node) ? match : undefined;
184
+ };
185
+ const findAlignedTextblock = (before, after, pos, offsets, indexes) => {
186
+ let original = getSnapshotIndex(before, indexes).byPosition.get(pos);
187
+ const path = [];
188
+ while ((_original = original) !== null && _original !== void 0 && _original.parent) {
189
+ var _original;
190
+ path.push(original);
191
+ original = original.parent;
192
+ }
193
+ let parent = after;
194
+ let parentPos = -1;
195
+ for (const child of path.reverse()) {
196
+ var _child$parent;
197
+ const originalParent = (_child$parent = child.parent) === null || _child$parent === void 0 ? void 0 : _child$parent.node;
198
+ if (!originalParent) {
199
+ return;
200
+ }
201
+ if (parent.type !== originalParent.type || parent.childCount !== originalParent.childCount) {
202
+ return;
203
+ }
204
+ const index = child.index;
205
+ if (index >= parent.childCount) {
206
+ return;
207
+ }
208
+ let childOffsets = offsets.get(parent);
209
+ if (!childOffsets) {
210
+ const computedOffsets = [];
211
+ parent.forEach((_child, offset) => computedOffsets.push(offset));
212
+ childOffsets = computedOffsets;
213
+ offsets.set(parent, childOffsets);
214
+ }
215
+ parentPos += 1 + childOffsets[index];
216
+ parent = parent.child(index);
217
+ }
218
+ return parent.isTextblock ? {
219
+ node: parent,
220
+ pos: parentPos
221
+ } : undefined;
222
+ };
223
+ const mapToFinalBlocks = (tr, index, range, indexes, options) => {
224
+ var _tr$docs;
225
+ const {
226
+ alignedOffsets,
227
+ requireUniqueSourceIds
228
+ } = options;
229
+ // Carried ranges can cover every block in a document. Repeated nodeAt calls
230
+ // scan preceding siblings, so reuse one positioned index per snapshot instead.
231
+ const nodeAt = (doc, pos) => alignedOffsets ? indexedNodeAt(doc, pos, indexes) : doc.nodeAt(pos);
232
+ let {
233
+ from,
234
+ to
235
+ } = range;
236
+ // Track inner content separately from outer node bounds: +1 skips the opening
237
+ // boundary and -1 excludes the closing boundary (see paragraph example above).
238
+ let contentFrom = from + 1;
239
+ let contentTo = to - 1;
240
+ // docs[i] is the document BEFORE step i, so docs[i + 1] is its result.
241
+ // The last step has no next snapshot; tr.doc is its result instead.
242
+ let node = nodeAt((_tr$docs = tr.docs[index + 1]) !== null && _tr$docs !== void 0 ? _tr$docs : tr.doc, from);
243
+ if (!node) {
244
+ return [];
245
+ }
246
+ // Later non-AI steps also shift/delete existing activity, even though they
247
+ // cannot create new activity themselves. Process every intermediate document.
248
+ for (let next = index + 1; next < tr.steps.length; next++) {
249
+ var _tr$docs2;
250
+ const map = tr.mapping.maps[next];
251
+ const after = (_tr$docs2 = tr.docs[next + 1]) !== null && _tr$docs2 !== void 0 ? _tr$docs2 : tr.doc;
252
+ // Mapping biases +1 (right) and -1 (left) keep boundary insertions outside
253
+ // the tracked content. These arguments do not add/subtract document positions.
254
+ const start = map.mapResult(contentFrom, 1);
255
+ const end = map.mapResult(contentTo, -1);
256
+ if (start.deleted && end.deleted || end.pos <= start.pos) {
257
+ var _indexes$get, _indexes$get2;
258
+ // A cumulative replacement can preserve the candidate content or owned markup.
259
+ // Check every intermediate doc so deletion then reinsertion never resurrects it.
260
+ let preserved = findPreservedNode(requireUniqueSourceIds ? tr.docs[next] : undefined, after, node, range.markupChanged, indexes);
261
+ const id = node.attrs.localId;
262
+ const sourceMatch = typeof id === 'string' && id ? (_indexes$get = indexes.get(tr.docs[next])) === null || _indexes$get === void 0 ? void 0 : _indexes$get.byId.get(id) : undefined;
263
+ const idMatch = typeof id === 'string' && id ? (_indexes$get2 = indexes.get(after)) === null || _indexes$get2 === void 0 ? void 0 : _indexes$get2.byId.get(id) : undefined;
264
+ if (!preserved && alignedOffsets && sourceMatch !== null && idMatch === undefined) {
265
+ // Inline replacements can keep a block's opening boundary while
266
+ // recreating its content and adding later blocks to the same chunk.
267
+ const mapped = map.mapResult(from, 1);
268
+ const mappedNode = !mapped.deleted ? nodeAt(after, mapped.pos) : undefined;
269
+ if (mappedNode !== null && mappedNode !== void 0 && mappedNode.isTextblock && sameNode(node, mappedNode)) {
270
+ preserved = {
271
+ node: mappedNode,
272
+ pos: mapped.pos
273
+ };
274
+ } else {
275
+ // Whole-block replacements may recreate unchanged blocks without IDs.
276
+ // Match their slot only while every parent keeps its shape.
277
+ const aligned = findAlignedTextblock(tr.docs[next], after, from, alignedOffsets, indexes);
278
+ if (aligned && sameNode(node, aligned.node)) {
279
+ preserved = aligned;
280
+ }
281
+ }
282
+ }
283
+ if (!preserved) {
284
+ return [];
285
+ }
286
+ from = preserved.pos;
287
+ to = from + preserved.node.nodeSize;
288
+ contentFrom = from + 1;
289
+ contentTo = to - 1;
290
+ node = preserved.node;
291
+ } else {
292
+ // Apply the same inward biases to the outer node bounds.
293
+ from = map.map(from, 1);
294
+ to = map.map(to, -1);
295
+ contentFrom = start.pos;
296
+ contentTo = end.pos;
297
+ const current = nodeAt(after, from);
298
+ if (current && current.nodeSize === to - from) {
299
+ node = current;
300
+ }
301
+ }
302
+ }
303
+ if (from < 0 || to > tr.doc.content.size || contentTo <= contentFrom) {
304
+ return [];
305
+ }
306
+ const finalNode = nodeAt(tr.doc, from);
307
+ if (finalNode !== null && finalNode !== void 0 && finalNode.isBlock && finalNode.nodeSize === to - from) {
308
+ return [{
309
+ from,
310
+ to
311
+ }];
312
+ }
313
+ // Joins/splits can leave mapped boundaries inside the final textblock.
314
+ const blocks = [];
315
+ if (alignedOffsets) {
316
+ const textblocks = getSnapshotIndex(tr.doc, indexes).textblocks;
317
+ let low = 0;
318
+ let high = textblocks.length;
319
+ while (low < high) {
320
+ const middle = low + high >>> 1;
321
+ const candidate = textblocks[middle];
322
+ if (candidate.pos + candidate.node.nodeSize <= contentFrom) {
323
+ low = middle + 1;
324
+ } else {
325
+ high = middle;
326
+ }
327
+ }
328
+ for (let index = low; index < textblocks.length && textblocks[index].pos < contentTo; index++) {
329
+ const {
330
+ node,
331
+ pos
332
+ } = textblocks[index];
333
+ blocks.push({
334
+ from: pos,
335
+ to: pos + node.nodeSize
336
+ });
337
+ }
338
+ return blocks;
339
+ }
340
+ tr.doc.nodesBetween(contentFrom, contentTo, (child, pos) => {
341
+ if (child.isTextblock) {
342
+ blocks.push({
343
+ from: pos,
344
+ to: pos + child.nodeSize
345
+ });
346
+ return false;
347
+ }
348
+ });
349
+ return blocks;
350
+ };
351
+
352
+ /**
353
+ * Maps existing textblock activity through each applied step into tr.doc coordinates.
354
+ * Replaced content is dropped unless an unchanged block survives with a unique ID
355
+ * or in the same structural slot. Newly changed blocks belong to the producer.
356
+ */
357
+ function mapAgentEditChromeRanges(tr, ranges) {
358
+ const indexes = new WeakMap();
359
+ const alignedOffsets = new WeakMap();
360
+ const options = {
361
+ alignedOffsets,
362
+ requireUniqueSourceIds: true
363
+ };
364
+ const validRanges = [];
365
+ for (const {
366
+ from,
367
+ to
368
+ } of ranges) {
369
+ if (!Number.isFinite(from) || !Number.isFinite(to) || to <= from) {
370
+ continue;
371
+ }
372
+ const start = Math.max(0, Math.min(from, tr.before.content.size));
373
+ const end = Math.max(start, Math.min(to, tr.before.content.size));
374
+ if (start === end) {
375
+ continue;
376
+ }
377
+ validRanges.push({
378
+ from: start,
379
+ to: end
380
+ });
381
+ }
382
+ if (!validRanges.length) {
383
+ return [];
384
+ }
385
+ validRanges.sort((left, right) => left.from - right.from);
386
+ const blocks = [];
387
+ let rangeIndex = 0;
388
+ for (const {
389
+ node,
390
+ pos
391
+ } of getSnapshotIndex(tr.before, indexes).textblocks) {
392
+ while (rangeIndex < validRanges.length && validRanges[rangeIndex].to <= pos) {
393
+ rangeIndex++;
394
+ }
395
+ if (rangeIndex === validRanges.length) {
396
+ break;
397
+ }
398
+ if (node.content.size && validRanges[rangeIndex].from < pos + node.nodeSize) {
399
+ blocks.push({
400
+ from: pos,
401
+ to: pos + node.nodeSize,
402
+ markupChanged: false
403
+ });
404
+ }
405
+ }
406
+ // Index -1 starts from docs[0], before the first step, instead of a step result.
407
+ return blocks.flatMap(range => mapToFinalBlocks(tr, -1, range, indexes, options));
408
+ }
409
+
410
+ /**
411
+ * Returns whole content-block ranges for selected applied steps, in tr.doc coordinates.
412
+ * Callers select actual AI writes (excluding restores, acknowledgments and preparation)
413
+ * and own experiment/lifecycle decisions. This helper does not dispatch or render.
414
+ *
415
+ * Comparing each step's snapshots also handles attribute/mark steps with empty maps.
416
+ * Shared unchanged subtrees are skipped by reference. Only localId changes are ignored;
417
+ * meaningful attributes and marks retain the affected node's full content scope.
418
+ */
419
+ function getAgentEditChangedRanges(tr, includedStepIndexes, requireUniqueSourceIds) {
420
+ // Positions belong to each intermediate document, not just tr.doc. Keep the
421
+ // lazy indexes local to this call so deletion/reinsertion checks stay intact
422
+ // and no cache is retained between transactions or streaming chunks.
423
+ const indexes = new WeakMap();
424
+ const options = {
425
+ requireUniqueSourceIds
426
+ };
427
+ const ranges = [];
428
+ for (const index of new Set(includedStepIndexes)) {
429
+ var _tr$docs3;
430
+ if (!Number.isInteger(index) || index < 0 || index >= tr.steps.length) {
431
+ continue;
432
+ }
433
+ const changed = [];
434
+ // The root document has no opening boundary. Using -1 makes the recursive
435
+ // parent.pos + 1 + offset formula place its first child at position 0.
436
+ collectChanges({
437
+ node: tr.docs[index],
438
+ pos: -1
439
+ }, {
440
+ node: (_tr$docs3 = tr.docs[index + 1]) !== null && _tr$docs3 !== void 0 ? _tr$docs3 : tr.doc,
441
+ pos: -1
442
+ }, tr.steps[index].getMap(), changed);
443
+ for (const range of changed) {
444
+ ranges.push(...mapToFinalBlocks(tr, index, range, indexes, options));
445
+ }
446
+ }
447
+
448
+ // Return only the surviving final-document ranges, never intermediate step states.
449
+ // Sort outer ranges before their children, then discard duplicates/contained ranges.
450
+ // Separate ranges stay separate; this must not highlight the untouched gaps.
451
+ const unique = [];
452
+ for (const range of ranges.sort((left, right) => left.from - right.from || right.to - left.to)) {
453
+ const previous = unique[unique.length - 1];
454
+ if (!previous || range.to > previous.to) {
455
+ unique.push(range);
456
+ }
457
+ }
458
+ return unique;
459
+ }
460
+ export const agentEditRangeMapping = {
461
+ getChangedRanges: getAgentEditChangedRanges,
462
+ mapChromeRanges: mapAgentEditChromeRanges
463
+ };
@@ -13,7 +13,7 @@ const NETWORK_FAILURE_REGEX = /^network failure/i;
13
13
  const RESIZE_OBSERVER_LOOP_REGEX = /ResizeObserver loop completed with undelivered notifications/;
14
14
  const SENTRY_DSN = 'https://0b10c8e02fb44d8796c047b102c9bee8@o55978.ingest.sentry.io/4505129224110080';
15
15
  const packageName = 'editor-common'; // Sentry doesn't accept '/' in its releases https://docs.sentry.io/platforms/javascript/configuration/releases/
16
- const packageVersion = "130.0.0";
16
+ const packageVersion = "130.2.0";
17
17
  const sanitiseSentryEvents = (data, _hint) => {
18
18
  // Remove URL as it has UGC
19
19
  // Ignored via go/ees007
@@ -14,7 +14,7 @@ import withAnalyticsEvents from '@atlaskit/analytics-next/withAnalyticsEvents';
14
14
  import { fg } from '@atlaskit/platform-feature-flags/fg';
15
15
  import Layer from '../Layer';
16
16
  const packageName = "@atlaskit/editor-common";
17
- const packageVersion = "130.0.0";
17
+ const packageVersion = "130.2.0";
18
18
  const halfFocusRing = 1;
19
19
  const dropOffset = '0, 8';
20
20
  const fadeIn = keyframes({
@@ -3,7 +3,6 @@ import React from 'react';
3
3
  import classnames from 'classnames';
4
4
  import { Resizable } from 're-resizable';
5
5
  import { akRichMediaResizeZIndex } from '@atlaskit/editor-shared-styles';
6
- import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
7
6
  import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '../../analytics';
8
7
  import { richMediaClassName } from '../../styles';
9
8
  import { gridTypeForLayout } from '../../utils';
@@ -147,15 +146,12 @@ export default class Resizer extends React.Component {
147
146
  const {
148
147
  isResizing
149
148
  } = this.state;
150
-
151
- // Tables supply the lower value; media outside tables retains its existing layer.
152
- const resizeHandleZIndex = isExperimentEnabled('platform_editor_sticky_headers_zindex') ? `var(--ak-editor-table-resize-handle-z-index, ${akRichMediaResizeZIndex})` : akRichMediaResizeZIndex;
153
149
  handleSides.forEach(side => {
154
150
  handles[side] = `richMedia-resize-handle-${side}`;
155
151
  baseHandleStyles[side] = {
156
152
  width: '24px',
157
153
  [side]: `${-13 - innerPadding}px`,
158
- zIndex: resizeHandleZIndex,
154
+ zIndex: akRichMediaResizeZIndex,
159
155
  pointerEvents: 'auto'
160
156
  };
161
157
  const sideHandleComponent = handleComponentFunc && handleComponentFunc(side);