@atlaskit/editor-plugin-show-diff 17.0.0 → 17.0.2

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 (37) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/cjs/pm-plugins/calculateDiff/calculateDiffDecorations.js +126 -49
  3. package/dist/cjs/pm-plugins/calculateDiff/groupDeletedColumnChanges.js +265 -0
  4. package/dist/cjs/pm-plugins/decorations/createChangedRowDecorationWidgets.js +100 -34
  5. package/dist/cjs/pm-plugins/decorations/createContributorTagWidget.js +18 -13
  6. package/dist/cjs/pm-plugins/decorations/createNodeChangedDecorationWidget.js +82 -20
  7. package/dist/cjs/pm-plugins/decorations/utils/createDeletedLineBreakWidget.js +106 -0
  8. package/dist/cjs/pm-plugins/decorations/utils/tableDiffMode.js +36 -0
  9. package/dist/cjs/pm-plugins/decorations/utils/wrapBlockNodeView.js +17 -6
  10. package/dist/cjs/pm-plugins/utils/emptyTextBlocks.js +66 -0
  11. package/dist/es2019/pm-plugins/calculateDiff/calculateDiffDecorations.js +61 -1
  12. package/dist/es2019/pm-plugins/calculateDiff/groupDeletedColumnChanges.js +210 -0
  13. package/dist/es2019/pm-plugins/decorations/createChangedRowDecorationWidgets.js +79 -13
  14. package/dist/es2019/pm-plugins/decorations/createContributorTagWidget.js +8 -2
  15. package/dist/es2019/pm-plugins/decorations/createNodeChangedDecorationWidget.js +85 -15
  16. package/dist/es2019/pm-plugins/decorations/utils/createDeletedLineBreakWidget.js +100 -0
  17. package/dist/es2019/pm-plugins/decorations/utils/tableDiffMode.js +27 -0
  18. package/dist/es2019/pm-plugins/decorations/utils/wrapBlockNodeView.js +15 -5
  19. package/dist/es2019/pm-plugins/utils/emptyTextBlocks.js +58 -0
  20. package/dist/esm/pm-plugins/calculateDiff/calculateDiffDecorations.js +127 -50
  21. package/dist/esm/pm-plugins/calculateDiff/groupDeletedColumnChanges.js +258 -0
  22. package/dist/esm/pm-plugins/decorations/createChangedRowDecorationWidgets.js +100 -34
  23. package/dist/esm/pm-plugins/decorations/createContributorTagWidget.js +18 -13
  24. package/dist/esm/pm-plugins/decorations/createNodeChangedDecorationWidget.js +84 -22
  25. package/dist/esm/pm-plugins/decorations/utils/createDeletedLineBreakWidget.js +100 -0
  26. package/dist/esm/pm-plugins/decorations/utils/tableDiffMode.js +30 -0
  27. package/dist/esm/pm-plugins/decorations/utils/wrapBlockNodeView.js +16 -5
  28. package/dist/esm/pm-plugins/utils/emptyTextBlocks.js +60 -0
  29. package/dist/types/pm-plugins/calculateDiff/groupDeletedColumnChanges.d.ts +9 -0
  30. package/dist/types/pm-plugins/decorations/createChangedRowDecorationWidgets.d.ts +3 -1
  31. package/dist/types/pm-plugins/decorations/createContributorTagWidget.d.ts +5 -1
  32. package/dist/types/pm-plugins/decorations/createNodeChangedDecorationWidget.d.ts +2 -1
  33. package/dist/types/pm-plugins/decorations/utils/createDeletedLineBreakWidget.d.ts +50 -0
  34. package/dist/types/pm-plugins/decorations/utils/tableDiffMode.d.ts +26 -0
  35. package/dist/types/pm-plugins/decorations/utils/wrapBlockNodeView.d.ts +4 -0
  36. package/dist/types/pm-plugins/utils/emptyTextBlocks.d.ts +16 -0
  37. package/package.json +4 -4
@@ -12,7 +12,8 @@ import { ChangeSet, simplifyChanges } from 'prosemirror-changeset';
12
12
  import { areDocsEqualByBlockStructureAndText } from '@atlaskit/editor-common/utils/areDocsEqualByBlockStructureAndText';
13
13
  import { areNodesEqualIgnoreAttrs } from '@atlaskit/editor-common/utils/document';
14
14
  import { Mapping } from '@atlaskit/editor-prosemirror/transform';
15
- import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
15
+ import { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view';
16
+ import { TableMap } from '@atlaskit/editor-tables/table-map';
16
17
  import { UNSAFE_expValNoExposure } from '@atlaskit/platform-feature-experiments/unsafe-exp-val-no-exposure';
17
18
  import { fg } from '@atlaskit/platform-feature-flags/fg';
18
19
  import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
@@ -23,15 +24,19 @@ import { createNodeChangedDecorationWidget } from '../decorations/createNodeChan
23
24
  import { createAttributionColorMap, getAttributionKey, getAttributionKeyForChange, getColorSchemeForChange, isAttributionColoringEnabled, isContributorTagsEnabled } from '../decorations/colorSchemes/attributions';
24
25
  import { extractDiffDescriptors } from '../decorations/decorationKeys';
25
26
  import { extractContributorTags } from '../decorations/extractContributorTags';
27
+ import { createDeletedLineBreakDecoration } from '../decorations/utils/createDeletedLineBreakWidget';
26
28
  import { getAttrChangeRanges, stepIsValidAttrChange } from '../decorations/utils/getAttrChangeRanges';
29
+ import { createRemovedLozenge } from '../decorations/utils/wrapBlockNodeView';
27
30
  import { getMarkChangeRanges } from '../decorations/utils/getMarkChangeRanges';
28
31
  import { getScrollableDecorations } from '../getScrollableDecorations';
29
32
  import { getDefaultDiffType, isExtendedEnabled } from '../isExtendedEnabled';
33
+ import { emptyTextBlockContentPositions } from '../utils/emptyTextBlocks';
30
34
  import { diffBySteps } from './diffBySteps';
31
35
  import { groupChangesByBlock } from './groupChangesByBlock';
32
36
  import { isMarkOnlyChange } from './isMarkOnlyChange';
33
37
  import { isOpenTokenOnlyChange } from './isOpenTokenOnlyChange';
34
38
  import { optimizeChanges } from './optimizeChanges';
39
+ import { CELL_CONTENT_OFFSET, groupDeletedColumnChanges } from './groupDeletedColumnChanges';
35
40
  import { selectTokenEncoder } from './selectTokenEncoder';
36
41
  import { collapseOverlappingChanges, simplifyChangesWithAttribution } from './simplifyChangesWithAttribution';
37
42
  import { simplifySteps, simplifyStepsWithAttribution } from './simplifySteps';
@@ -527,10 +532,38 @@ var calculateDiffDecorationsInner = function calculateDiffDecorationsInner(_ref8
527
532
 
528
533
  // Our default operations are insertions, so it should match the opposite of isInverted.
529
534
  var isInserted = !isInverted;
535
+ if (isExtendedEnabled(diffType)) {
536
+ changes = groupDeletedColumnChanges(changes, originalDoc, tr.doc, new Mapping(stepMaps), isInverted);
537
+ }
538
+ // A whole-table change owns every mark and attribute range inside its bounds, so those ranges
539
+ // do not also get their own decoration.
540
+ var tableChanges = changes.filter(function (change) {
541
+ return change.deletedColumns !== undefined;
542
+ });
543
+ var isHandledByTable = function isHandledByTable(from, to) {
544
+ return tableChanges.some(function (change) {
545
+ return from >= change.fromB && to <= change.toB;
546
+ });
547
+ };
530
548
  var createDecorationsForChange = function createDecorationsForChange(change) {
531
549
  var changeColorScheme = attributionColors ? getColorSchemeForChange(change, attributedChanges, attributionColors, colorScheme) : colorScheme;
532
550
  var attributionKey = showContributorTags ? getAttributionKeyForChange(change, attributedChanges) : undefined;
533
551
  var isActive = isRangeActive(activeIndexPos, change.fromB, change.toB);
552
+ var deletedColumns = change.deletedColumns;
553
+ // On an inverted diff the table that lost columns is the one in `tr.doc`, so the column
554
+ // indices address it directly and each label anchors inside its own cell.
555
+ var table = tr.doc.nodeAt(change.fromB);
556
+ if (isInverted && !hideDeletedDiffs && deletedColumns !== null && deletedColumns !== void 0 && deletedColumns.length && (table === null || table === void 0 ? void 0 : table.type.name) === 'table') {
557
+ var tableMap = TableMap.get(table);
558
+ deletedColumns.forEach(function (column) {
559
+ var offset = tableMap.positionAt(0, column, table);
560
+ decorations.push(Decoration.widget(change.fromB + CELL_CONTENT_OFFSET + offset, function () {
561
+ return createRemovedLozenge(intl, isActive, changeColorScheme, true);
562
+ }, {
563
+ side: -1
564
+ }));
565
+ });
566
+ }
534
567
 
535
568
  // The deleted side of a mark-only change is a byte-identical copy of the new text, so it is
536
569
  // suppressed in the clean view. `hideDeletedDiffs` is load-bearing: when false the reviewer
@@ -564,6 +597,41 @@ var calculateDiffDecorationsInner = function calculateDiffDecorationsInner(_ref8
564
597
  // On an inverted diff the inserted side is visually the deleted side.
565
598
  var shouldHideDeleted = isExtendedEnabled(diffType) ? isInverted && hideDeletedDiffs : false;
566
599
 
600
+ // A removed blank line — an empty paragraph or heading — has nothing to mark up. On an
601
+ // inverted diff, the shape AI suggested edits renders, the block is still in the displayed
602
+ // document, so the removal lands here on the inserted side rather than on the deleted side,
603
+ // and both the inline and the block decoration paint nothing: an empty textblock has no
604
+ // inline content to cover, and block decorations return no style for a paragraph or heading.
605
+ // The reviewer sees an unmarked blank line. Stand the struck-through return glyph in for each
606
+ // one, anchored inside the block so it renders on the blank line itself.
607
+ //
608
+ // One decoration per blank line rather than one per change: a run of adjacent blank lines is
609
+ // reported as a single change spanning all of them.
610
+ //
611
+ // Withheld in the clean view, where removals are hidden rather than struck through.
612
+ if (fg('platform_editor_ai_show_diff_patch_2') && !isInserted && !shouldHideDeleted) {
613
+ var _iterator3 = _createForOfIteratorHelper(emptyTextBlockContentPositions(tr.doc, change.fromB, change.toB)),
614
+ _step3;
615
+ try {
616
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
617
+ var pos = _step3.value;
618
+ decorations.push(createDeletedLineBreakDecoration({
619
+ attributionKey: attributionKey,
620
+ colorScheme: changeColorScheme,
621
+ diffType: diffType,
622
+ isActive: isActive,
623
+ pos: pos,
624
+ reveal: reveal,
625
+ side: -1
626
+ }));
627
+ }
628
+ } catch (err) {
629
+ _iterator3.e(err);
630
+ } finally {
631
+ _iterator3.f();
632
+ }
633
+ }
634
+
567
635
  // For `smart` NODE-level promotions the change range spans a whole container
568
636
  // (e.g. an entire list/table/layout, using outer node bounds). Applying a SINGLE
569
637
  // inline decoration across that whole range would paint the inserted style across
@@ -594,13 +662,13 @@ var calculateDiffDecorationsInner = function calculateDiffDecorationsInner(_ref8
594
662
  // table (the cell overlays added below provide the highlight without the
595
663
  // expensive content re-render); leaf blocks outside the table still get theirs.
596
664
  var coarseTables = useCoarseTableDecoration ? tableRanges(tr.doc, change.fromB, change.toB) : [];
597
- var _iterator3 = _createForOfIteratorHelper(leafTextblockRanges(tr.doc, change.fromB, change.toB)),
598
- _step3;
665
+ var _iterator4 = _createForOfIteratorHelper(leafTextblockRanges(tr.doc, change.fromB, change.toB)),
666
+ _step4;
599
667
  try {
600
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
601
- var _step3$value = _slicedToArray(_step3.value, 2),
602
- from = _step3$value[0],
603
- to = _step3$value[1];
668
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
669
+ var _step4$value = _slicedToArray(_step4.value, 2),
670
+ from = _step4$value[0],
671
+ to = _step4$value[1];
604
672
  if (useCoarseTableDecoration && isInsideTable(coarseTables, from)) {
605
673
  continue;
606
674
  }
@@ -627,9 +695,9 @@ var calculateDiffDecorationsInner = function calculateDiffDecorationsInner(_ref8
627
695
  }))));
628
696
  }
629
697
  } catch (err) {
630
- _iterator3.e(err);
698
+ _iterator4.e(err);
631
699
  } finally {
632
- _iterator3.f();
700
+ _iterator4.f();
633
701
  }
634
702
  } else {
635
703
  decorations.push.apply(decorations, _toConsumableArray(createInlineChangedDecoration(_objectSpread({
@@ -681,6 +749,7 @@ var calculateDiffDecorationsInner = function calculateDiffDecorationsInner(_ref8
681
749
  });
682
750
  if (!_shouldHideDeleted) {
683
751
  decorations.push.apply(decorations, _toConsumableArray(createNodeChangedDecorationWidget(_objectSpread(_objectSpread({
752
+ deletedColumns: deletedColumns,
684
753
  // The deleted side has no inline decoration, so the widget carries the actor.
685
754
  attributionKey: attributionKey,
686
755
  // Extended pipeline only, matching the inline path: the non-extended styles have no
@@ -716,6 +785,10 @@ var calculateDiffDecorationsInner = function calculateDiffDecorationsInner(_ref8
716
785
  getMarkChangeRanges(simplifiedSteps, simplifiedStepAttributions, fg('confluence_ncs_step_diffing_version_history') ? stepMaps : undefined).filter(function (_ref11) {
717
786
  var fromB = _ref11.fromB,
718
787
  toB = _ref11.toB;
788
+ return !isHandledByTable(fromB, toB);
789
+ }).filter(function (_ref12) {
790
+ var fromB = _ref12.fromB,
791
+ toB = _ref12.toB;
719
792
  return !fg('confluence_ncs_step_diffing_version_history') || !changes.some(function (change) {
720
793
  return change.fromB === fromB && change.toB === toB;
721
794
  });
@@ -737,9 +810,13 @@ var calculateDiffDecorationsInner = function calculateDiffDecorationsInner(_ref8
737
810
  tagMountContext: tagMountContext
738
811
  })));
739
812
  });
740
- getAttrChangeRanges(tr.doc, attrStepContexts, originalDoc).filter(function (_ref12) {
741
- var fromB = _ref12.fromB,
742
- toB = _ref12.toB;
813
+ getAttrChangeRanges(tr.doc, attrStepContexts, originalDoc).filter(function (_ref13) {
814
+ var fromB = _ref13.fromB,
815
+ toB = _ref13.toB;
816
+ return !isHandledByTable(fromB, toB);
817
+ }).filter(function (_ref14) {
818
+ var fromB = _ref14.fromB,
819
+ toB = _ref14.toB;
743
820
  return !fg('confluence_ncs_step_diffing_version_history') || !changes.some(function (change) {
744
821
  return change.fromB === fromB && change.toB === toB;
745
822
  });
@@ -849,49 +926,49 @@ var calculateDiffDecorationsInner = function calculateDiffDecorationsInner(_ref8
849
926
  export var calculateDiffDecorations = memoizeOne(calculateDiffDecorationsInner,
850
927
  // Cache results unless relevant inputs change. `tagMountContext` is deliberately absent: it is
851
928
  // built once per plugin instance, so it can never be the reason a diff needs recalculating.
852
- function (_ref13, _ref14) {
853
- var _ref18;
854
- var _ref15 = _slicedToArray(_ref13, 1),
855
- _ref15$ = _ref15[0],
856
- pluginState = _ref15$.pluginState,
857
- state = _ref15$.state,
858
- colorScheme = _ref15$.colorScheme,
859
- intl = _ref15$.intl,
860
- activeIndexPos = _ref15$.activeIndexPos,
861
- isInverted = _ref15$.isInverted,
862
- diffType = _ref15$.diffType,
863
- hideDeletedDiffs = _ref15$.hideDeletedDiffs,
864
- hideAddedDiffsUnderline = _ref15$.hideAddedDiffsUnderline,
865
- showIndicators = _ref15$.showIndicators,
866
- smartThresholds = _ref15$.smartThresholds,
867
- deletedDiffPlacement = _ref15$.deletedDiffPlacement,
868
- inlineDeletedDiffPlacement = _ref15$.inlineDeletedDiffPlacement,
869
- reveal = _ref15$.reveal;
870
- var _ref16 = _slicedToArray(_ref14, 1),
871
- _ref16$ = _ref16[0],
872
- lastPluginState = _ref16$.pluginState,
873
- lastState = _ref16$.state,
874
- lastColorScheme = _ref16$.colorScheme,
875
- lastIntl = _ref16$.intl,
876
- lastActiveIndexPos = _ref16$.activeIndexPos,
877
- lastIsInverted = _ref16$.isInverted,
878
- lastDiffType = _ref16$.diffType,
879
- lastHideDeletedDiffs = _ref16$.hideDeletedDiffs,
880
- lastHideAddedDiffsUnderline = _ref16$.hideAddedDiffsUnderline,
881
- lastShowIndicators = _ref16$.showIndicators,
882
- lastSmartThresholds = _ref16$.smartThresholds,
883
- lastDeletedDiffPlacement = _ref16$.deletedDiffPlacement,
884
- lastInlineDeletedDiffPlacement = _ref16$.inlineDeletedDiffPlacement,
885
- lastReveal = _ref16$.reveal;
929
+ function (_ref15, _ref16) {
930
+ var _ref20;
931
+ var _ref17 = _slicedToArray(_ref15, 1),
932
+ _ref17$ = _ref17[0],
933
+ pluginState = _ref17$.pluginState,
934
+ state = _ref17$.state,
935
+ colorScheme = _ref17$.colorScheme,
936
+ intl = _ref17$.intl,
937
+ activeIndexPos = _ref17$.activeIndexPos,
938
+ isInverted = _ref17$.isInverted,
939
+ diffType = _ref17$.diffType,
940
+ hideDeletedDiffs = _ref17$.hideDeletedDiffs,
941
+ hideAddedDiffsUnderline = _ref17$.hideAddedDiffsUnderline,
942
+ showIndicators = _ref17$.showIndicators,
943
+ smartThresholds = _ref17$.smartThresholds,
944
+ deletedDiffPlacement = _ref17$.deletedDiffPlacement,
945
+ inlineDeletedDiffPlacement = _ref17$.inlineDeletedDiffPlacement,
946
+ reveal = _ref17$.reveal;
947
+ var _ref18 = _slicedToArray(_ref16, 1),
948
+ _ref18$ = _ref18[0],
949
+ lastPluginState = _ref18$.pluginState,
950
+ lastState = _ref18$.state,
951
+ lastColorScheme = _ref18$.colorScheme,
952
+ lastIntl = _ref18$.intl,
953
+ lastActiveIndexPos = _ref18$.activeIndexPos,
954
+ lastIsInverted = _ref18$.isInverted,
955
+ lastDiffType = _ref18$.diffType,
956
+ lastHideDeletedDiffs = _ref18$.hideDeletedDiffs,
957
+ lastHideAddedDiffsUnderline = _ref18$.hideAddedDiffsUnderline,
958
+ lastShowIndicators = _ref18$.showIndicators,
959
+ lastSmartThresholds = _ref18$.smartThresholds,
960
+ lastDeletedDiffPlacement = _ref18$.deletedDiffPlacement,
961
+ lastInlineDeletedDiffPlacement = _ref18$.inlineDeletedDiffPlacement,
962
+ lastReveal = _ref18$.reveal;
886
963
  var originalDocIsSame = lastPluginState.originalDoc && pluginState.originalDoc && pluginState.originalDoc.eq(lastPluginState.originalDoc);
887
964
  if (isExtendedEnabled(diffType)) {
888
- var _ref17;
889
- return (_ref17 = colorScheme === lastColorScheme && intl.locale === lastIntl.locale && isInverted === lastIsInverted && diffType === lastDiffType && isEqual(activeIndexPos, lastActiveIndexPos) && originalDocIsSame && isEqual(pluginState.steps, lastPluginState.steps) && isEqual(pluginState.stepAttributions, lastPluginState.stepAttributions) &&
965
+ var _ref19;
966
+ return (_ref19 = colorScheme === lastColorScheme && intl.locale === lastIntl.locale && isInverted === lastIsInverted && diffType === lastDiffType && isEqual(activeIndexPos, lastActiveIndexPos) && originalDocIsSame && isEqual(pluginState.steps, lastPluginState.steps) && isEqual(pluginState.stepAttributions, lastPluginState.stepAttributions) &&
890
967
  // Contributors can arrive after the first calculation; without this, tags never appear.
891
968
  isEqual(pluginState.contributors, lastPluginState.contributors) && state.doc.eq(lastState.doc) && hideDeletedDiffs === lastHideDeletedDiffs && hideAddedDiffsUnderline === lastHideAddedDiffsUnderline && showIndicators === lastShowIndicators && isEqual(smartThresholds, lastSmartThresholds) && deletedDiffPlacement === lastDeletedDiffPlacement && inlineDeletedDiffPlacement === lastInlineDeletedDiffPlacement &&
892
969
  // Turning the reveal on or off changes the emitted styles, so a cached result from the
893
970
  // other setting would leave the diff painted in the wrong resting state.
894
- isEqual(reveal, lastReveal)) !== null && _ref17 !== void 0 ? _ref17 : false;
971
+ isEqual(reveal, lastReveal)) !== null && _ref19 !== void 0 ? _ref19 : false;
895
972
  }
896
- return (_ref18 = originalDocIsSame && isEqual(pluginState.steps, lastPluginState.steps) && isEqual(pluginState.stepAttributions, lastPluginState.stepAttributions) && isEqual(pluginState.contributors, lastPluginState.contributors) && state.doc.eq(lastState.doc) && colorScheme === lastColorScheme && intl.locale === lastIntl.locale && isEqual(activeIndexPos, lastActiveIndexPos) && hideDeletedDiffs === lastHideDeletedDiffs) !== null && _ref18 !== void 0 ? _ref18 : false;
973
+ return (_ref20 = originalDocIsSame && isEqual(pluginState.steps, lastPluginState.steps) && isEqual(pluginState.stepAttributions, lastPluginState.stepAttributions) && isEqual(pluginState.contributors, lastPluginState.contributors) && state.doc.eq(lastState.doc) && colorScheme === lastColorScheme && intl.locale === lastIntl.locale && isEqual(activeIndexPos, lastActiveIndexPos) && hideDeletedDiffs === lastHideDeletedDiffs) !== null && _ref20 !== void 0 ? _ref20 : false;
897
974
  });
@@ -0,0 +1,258 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ 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; } } }; }
3
+ 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; } }
4
+ 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; }
5
+ import { areNodesEqualIgnoreAttrs } from '@atlaskit/editor-common/utils/document';
6
+ import { Fragment, Slice } from '@atlaskit/editor-prosemirror/model';
7
+ import { TableMap } from '@atlaskit/editor-tables/table-map';
8
+ export var CELL_CONTENT_OFFSET = 2;
9
+ var IGNORED_CELL_ATTRS = ['localId', 'colwidth'];
10
+ var tablesIn = function tablesIn(doc) {
11
+ var tables = [];
12
+ doc.descendants(function (node, pos) {
13
+ if (node.type.name === 'table') {
14
+ tables.push({
15
+ node: node,
16
+ pos: pos
17
+ });
18
+ return false;
19
+ }
20
+ return true;
21
+ });
22
+ return tables;
23
+ };
24
+ var tableEnd = function tableEnd(table) {
25
+ return table.pos + table.node.nodeSize;
26
+ };
27
+ var documentsDifferOnlyByTable = function documentsDifferOnlyByTable(originalDoc, newDoc, before, after) {
28
+ try {
29
+ return originalDoc.replace(before.pos, tableEnd(before), new Slice(Fragment.from(after.node), 0, 0)).eq(newDoc);
30
+ } catch (_unused) {
31
+ return false;
32
+ }
33
+ };
34
+ var columnOffsets = function columnOffsets(map, table) {
35
+ return Array.from({
36
+ length: map.width
37
+ }, function (_, column) {
38
+ return map.positionAt(0, column, table);
39
+ });
40
+ };
41
+ var positionMatchedColumns = function positionMatchedColumns(_ref) {
42
+ var before = _ref.before,
43
+ after = _ref.after,
44
+ afterToBefore = _ref.afterToBefore,
45
+ beforeOffsets = _ref.beforeOffsets;
46
+ var afterMap = TableMap.get(after.node);
47
+ var surviving = new Set();
48
+ for (var column = 0; column < afterMap.width; column++) {
49
+ var offset = afterMap.positionAt(0, column, after.node);
50
+ var mapped = afterToBefore.mapResult(after.pos + CELL_CONTENT_OFFSET + offset);
51
+ var beforeColumn = beforeOffsets.indexOf(mapped.pos - before.pos - CELL_CONTENT_OFFSET);
52
+ if (mapped.deleted || beforeColumn === -1) {
53
+ return new Set();
54
+ }
55
+ surviving.add(beforeColumn);
56
+ }
57
+ return surviving;
58
+ };
59
+ var addContentMatchedColumns = function addContentMatchedColumns(surviving, before, after) {
60
+ var _before$node$firstChi, _before$node$firstChi2, _after$node$firstChil, _after$node$firstChil2;
61
+ var beforeCells = (_before$node$firstChi = (_before$node$firstChi2 = before.node.firstChild) === null || _before$node$firstChi2 === void 0 ? void 0 : _before$node$firstChi2.children) !== null && _before$node$firstChi !== void 0 ? _before$node$firstChi : [];
62
+ var afterCells = (_after$node$firstChil = (_after$node$firstChil2 = after.node.firstChild) === null || _after$node$firstChil2 === void 0 ? void 0 : _after$node$firstChil2.children) !== null && _after$node$firstChil !== void 0 ? _after$node$firstChil : [];
63
+ var _iterator = _createForOfIteratorHelper(afterCells),
64
+ _step;
65
+ try {
66
+ var _loop = function _loop() {
67
+ var afterCell = _step.value;
68
+ var matches = beforeCells.flatMap(function (beforeCell, column) {
69
+ return areNodesEqualIgnoreAttrs(beforeCell, afterCell, IGNORED_CELL_ATTRS) ? [column] : [];
70
+ });
71
+ if (matches.length !== 1) {
72
+ return {
73
+ v: false
74
+ };
75
+ }
76
+ surviving.add(matches[0]);
77
+ },
78
+ _ret;
79
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
80
+ _ret = _loop();
81
+ if (_ret) return _ret.v;
82
+ }
83
+ } catch (err) {
84
+ _iterator.e(err);
85
+ } finally {
86
+ _iterator.f();
87
+ }
88
+ var ordered = _toConsumableArray(surviving);
89
+ return !ordered.some(function (column, index) {
90
+ return index > 0 && column <= ordered[index - 1];
91
+ });
92
+ };
93
+ var findDeletedColumns = function findDeletedColumns(before, after, afterToBefore) {
94
+ var beforeMap = TableMap.get(before.node);
95
+ var afterMap = TableMap.get(after.node);
96
+ // Merged or malformed tables do not have reliable column indices.
97
+ if (beforeMap.hasMergedCells() || afterMap.hasMergedCells() || beforeMap.problems || afterMap.problems) {
98
+ return [];
99
+ }
100
+ var beforeOffsets = columnOffsets(beforeMap, before.node);
101
+ var surviving = positionMatchedColumns({
102
+ before: before,
103
+ after: after,
104
+ afterToBefore: afterToBefore,
105
+ beforeOffsets: beforeOffsets
106
+ });
107
+ if (surviving.size !== afterMap.width && !addContentMatchedColumns(surviving, before, after)) {
108
+ return [];
109
+ }
110
+ return beforeOffsets.flatMap(function (_, column) {
111
+ return surviving.has(column) ? [] : [column];
112
+ });
113
+ };
114
+ var findCounterpart = function findCounterpart(_ref2) {
115
+ var table = _ref2.table,
116
+ index = _ref2.index,
117
+ originalTables = _ref2.originalTables,
118
+ newTables = _ref2.newTables,
119
+ mapping = _ref2.mapping;
120
+ var from = mapping.map(table.pos, -1);
121
+ var to = mapping.map(tableEnd(table), 1);
122
+ var inside = mapping.mapResult(table.pos + 1);
123
+ var candidates = newTables.filter(function (candidate) {
124
+ return inside.deleted ? candidate.pos >= from && tableEnd(candidate) <= to : candidate.pos < inside.pos && inside.pos < tableEnd(candidate);
125
+ });
126
+ if (candidates.length === 1) {
127
+ return candidates[0];
128
+ }
129
+ return originalTables.length === newTables.length ? newTables[index] : undefined;
130
+ };
131
+ var orient = function orient(_ref3) {
132
+ var table = _ref3.table,
133
+ counterpart = _ref3.counterpart,
134
+ mapping = _ref3.mapping,
135
+ isInverted = _ref3.isInverted;
136
+ return {
137
+ before: isInverted ? counterpart : table,
138
+ after: isInverted ? table : counterpart,
139
+ afterToBefore: (isInverted ? mapping.invert() : mapping).invert()
140
+ };
141
+ };
142
+ var changesInTable = function changesInTable(changes, table) {
143
+ return changes.filter(function (change) {
144
+ return change.fromA < tableEnd(table) && change.toA > table.pos;
145
+ });
146
+ };
147
+ var isNarrowingSafe = function isNarrowingSafe(_ref4) {
148
+ var replacedChanges = _ref4.replacedChanges,
149
+ table = _ref4.table,
150
+ counterpart = _ref4.counterpart,
151
+ originalDoc = _ref4.originalDoc,
152
+ newDoc = _ref4.newDoc;
153
+ var staysInside = replacedChanges.every(function (change) {
154
+ return change.fromA >= table.pos && change.toA <= tableEnd(table) && change.fromB >= counterpart.pos && change.toB <= tableEnd(counterpart);
155
+ });
156
+ return staysInside || documentsDifferOnlyByTable(originalDoc, newDoc, table, counterpart);
157
+ };
158
+ var planTableChange = function planTableChange(_ref5) {
159
+ var table = _ref5.table,
160
+ index = _ref5.index,
161
+ originalTables = _ref5.originalTables,
162
+ newTables = _ref5.newTables,
163
+ changes = _ref5.changes,
164
+ originalDoc = _ref5.originalDoc,
165
+ newDoc = _ref5.newDoc,
166
+ mapping = _ref5.mapping,
167
+ isInverted = _ref5.isInverted;
168
+ var counterpart = findCounterpart({
169
+ table: table,
170
+ index: index,
171
+ originalTables: originalTables,
172
+ newTables: newTables,
173
+ mapping: mapping
174
+ });
175
+ if (!counterpart) {
176
+ return undefined;
177
+ }
178
+ var _orient = orient({
179
+ table: table,
180
+ counterpart: counterpart,
181
+ mapping: mapping,
182
+ isInverted: isInverted
183
+ }),
184
+ before = _orient.before,
185
+ after = _orient.after,
186
+ afterToBefore = _orient.afterToBefore;
187
+ if (TableMap.get(before.node).width <= TableMap.get(after.node).width) {
188
+ return undefined;
189
+ }
190
+ var replacedChanges = changesInTable(changes, table);
191
+ if (!replacedChanges.length) {
192
+ return undefined;
193
+ }
194
+ if (!isNarrowingSafe({
195
+ replacedChanges: replacedChanges,
196
+ table: table,
197
+ counterpart: counterpart,
198
+ originalDoc: originalDoc,
199
+ newDoc: newDoc
200
+ })) {
201
+ return undefined;
202
+ }
203
+ var spans = replacedChanges.flatMap(function (change) {
204
+ return [].concat(_toConsumableArray(change.deleted), _toConsumableArray(change.inserted));
205
+ });
206
+ return {
207
+ replacedChanges: replacedChanges,
208
+ tableChange: {
209
+ deletedColumns: findDeletedColumns(before, after, afterToBefore),
210
+ fromA: table.pos,
211
+ toA: tableEnd(table),
212
+ fromB: counterpart.pos,
213
+ toB: tableEnd(counterpart),
214
+ deleted: spans,
215
+ inserted: spans
216
+ }
217
+ };
218
+ };
219
+
220
+ /** Groups a deleted column's cell changes into one whole-table change. */
221
+ export var groupDeletedColumnChanges = function groupDeletedColumnChanges(changes, originalDoc, newDoc, mapping, isInverted) {
222
+ var originalTables = tablesIn(originalDoc);
223
+ var newTables = tablesIn(newDoc);
224
+ var plans = [];
225
+ var replaced = new Set();
226
+ originalTables.forEach(function (table, index) {
227
+ var plan = planTableChange({
228
+ table: table,
229
+ index: index,
230
+ originalTables: originalTables,
231
+ newTables: newTables,
232
+ changes: changes.filter(function (change) {
233
+ return !replaced.has(change);
234
+ }),
235
+ originalDoc: originalDoc,
236
+ newDoc: newDoc,
237
+ mapping: mapping,
238
+ isInverted: isInverted
239
+ });
240
+ if (!plan) {
241
+ return;
242
+ }
243
+ plans.push(plan);
244
+ plan.replacedChanges.forEach(function (change) {
245
+ return replaced.add(change);
246
+ });
247
+ });
248
+ if (!plans.length) {
249
+ return _toConsumableArray(changes);
250
+ }
251
+ return [].concat(_toConsumableArray(changes.filter(function (change) {
252
+ return !replaced.has(change);
253
+ })), _toConsumableArray(plans.map(function (plan) {
254
+ return plan.tableChange;
255
+ }))).sort(function (left, right) {
256
+ return left.fromB - right.fromB;
257
+ });
258
+ };