@atlaskit/editor-tables 2.5.8 → 2.6.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @atlaskit/editor-tables
2
2
 
3
+ ## 2.6.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#86621](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/86621) [`819e262c9b0b`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/819e262c9b0b) - remove table-shift-click-selection-backward feature flag
8
+
9
+ ## 2.6.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [#83612](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/83612) [`25b32cbfbb7e`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/25b32cbfbb7e) - [ux] Added new clone row/column behaviour to tables drag N drop. When the user holds the alt modifier during the operation the row/column will be duplicated rather then moved.
14
+
3
15
  ## 2.5.8
4
16
 
5
17
  ### Patch Changes
@@ -6,7 +6,7 @@
6
6
  "sideEffects": false,
7
7
  "types": "../dist/types/cell-bookmark.d.ts",
8
8
  "typesVersions": {
9
- ">=4.5 <4.9": {
9
+ ">=4.5 <5.4": {
10
10
  "*": [
11
11
  "../dist/types-ts4.5/cell-bookmark.d.ts"
12
12
  ]
@@ -6,7 +6,7 @@
6
6
  "sideEffects": false,
7
7
  "types": "../dist/types/cell-selection.d.ts",
8
8
  "typesVersions": {
9
- ">=4.5 <4.9": {
9
+ ">=4.5 <5.4": {
10
10
  "*": [
11
11
  "../dist/types-ts4.5/cell-selection.d.ts"
12
12
  ]
@@ -143,8 +143,7 @@ function handleMouseDown(view, event, dragAndDropEnabled) {
143
143
  if (startEvent.shiftKey && view.state.selection instanceof _cellSelection.CellSelection) {
144
144
  var isDragHandleElement;
145
145
  var targetElement = event.target instanceof HTMLElement || event.target instanceof SVGElement ? event.target : null;
146
- isDragHandleElement = !!targetElement && !!targetElement.closest('button.pm-table-drag-handle-button-container');
147
- // Adding to an existing cell selection
146
+ isDragHandleElement = !!targetElement && !!targetElement.closest('button.pm-table-drag-handle-button-container, button.pm-table-drag-handle-button-clickable-zone');
148
147
  if (dragAndDropEnabled && isDragHandleElement) {
149
148
  return false;
150
149
  }
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.cloneColumn = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
+ var _cellSelection = require("../cell-selection");
10
+ var _tableMap = require("../table-map");
11
+ var _analyseTable = require("./analyse-table");
12
+ var _cloneTr = require("./clone-tr");
13
+ var _find = require("./find");
14
+ var _getSelectionRangeInColumn = require("./get-selection-range-in-column");
15
+ var _reorderUtils = require("./reorder-utils");
16
+ var _tableNodeTypes = require("./table-node-types");
17
+ function normalizeDirection(targetDirection, options) {
18
+ var _options$direction;
19
+ var override = ((_options$direction = options === null || options === void 0 ? void 0 : options.direction) !== null && _options$direction !== void 0 ? _options$direction : 0) < 0 ? 'start' : 'end';
20
+ return options !== null && options !== void 0 && options.tryToFit && !!(options !== null && options !== void 0 && options.direction) ? override : targetDirection;
21
+ }
22
+ var cloneColumn = exports.cloneColumn = function cloneColumn(state, originColumnIndex, targetColumnIndex, targetDirection) {
23
+ var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {
24
+ tryToFit: false,
25
+ direction: 0,
26
+ selectAfterClone: false
27
+ };
28
+ return function (tr) {
29
+ var _originalColumnRanges, _targetColumnRanges$i;
30
+ var table = (0, _find.findTable)(tr.selection);
31
+ if (!table) {
32
+ return tr;
33
+ }
34
+
35
+ // normalize the origin index to an array since this supports moving both a single & multiple cols in a single action.
36
+ if (!Array.isArray(originColumnIndex)) {
37
+ originColumnIndex = [originColumnIndex];
38
+ }
39
+ var tableMap = _tableMap.TableMap.get(table.node);
40
+ var originalColumnRanges = (0, _getSelectionRangeInColumn.getSelectionRangeInColumn)(Math.min.apply(Math, (0, _toConsumableArray2.default)(originColumnIndex)), Math.max.apply(Math, (0, _toConsumableArray2.default)(originColumnIndex)))(tr);
41
+ var targetColumnRanges = (0, _getSelectionRangeInColumn.getSelectionRangeInColumn)(targetColumnIndex)(tr);
42
+ var indexesOriginColumn = (_originalColumnRanges = originalColumnRanges === null || originalColumnRanges === void 0 ? void 0 : originalColumnRanges.indexes) !== null && _originalColumnRanges !== void 0 ? _originalColumnRanges : [];
43
+ var indexesTargetColumn = (_targetColumnRanges$i = targetColumnRanges === null || targetColumnRanges === void 0 ? void 0 : targetColumnRanges.indexes) !== null && _targetColumnRanges$i !== void 0 ? _targetColumnRanges$i : [];
44
+ var min = indexesOriginColumn[0];
45
+ var max = indexesOriginColumn[indexesOriginColumn.length - 1];
46
+ if (!options.tryToFit && indexesTargetColumn.length > 1) {
47
+ (0, _reorderUtils.isValidReorder)(originColumnIndex[0], targetColumnIndex, indexesTargetColumn, 'column');
48
+ }
49
+ var types = (0, _tableNodeTypes.tableNodeTypes)(state.schema);
50
+ var direction = normalizeDirection(targetDirection, options);
51
+ var actualTargetIndex = Math[direction === 'start' ? 'min' : 'max'].apply(Math, (0, _toConsumableArray2.default)(indexesTargetColumn));
52
+ var _determineTableHeader = (0, _analyseTable.determineTableHeaderStateFromTableNode)(table.node, tableMap, types),
53
+ rowHeaderEnabled = _determineTableHeader.rowHeaderEnabled,
54
+ columnHeaderEnabled = _determineTableHeader.columnHeaderEnabled;
55
+ var createContentNode = createContentNodeFactory(table);
56
+ var newTr = (0, _cloneTr.cloneTr)(tr);
57
+ var origins = [];
58
+ for (var y = 0; y < tableMap.height; y++) {
59
+ origins.push([]);
60
+ for (var x = min; x <= max; x++) {
61
+ if (tableMap.isCellMergedTopLeft(y, x)) {
62
+ continue;
63
+ }
64
+ var nodePos = tableMap.map[y * tableMap.width + x];
65
+ origins[y].push(createContentNode(nodePos));
66
+ }
67
+ if (columnHeaderEnabled && actualTargetIndex === 0 && direction === 'start') {
68
+ // This block is handling the situation where a col is moved in/out of the header position. If the header col option
69
+ // is enabled then;
70
+ // When a col is moved out, the col will be converted to a normal col and the col to the right will become the header.
71
+ // When a col is moved in, the old col header needs to be made normal, and the incoming col needs to be made a header.
72
+ // This section only manages what happens to the other col, not the one being moved.
73
+ var nearHeaderCol = min === 0 ? max + 1 : actualTargetIndex;
74
+ var _nodePos = tableMap.map[y * tableMap.width + nearHeaderCol];
75
+ var _createContentNode = createContentNode(_nodePos),
76
+ pos = _createContentNode.pos,
77
+ node = _createContentNode.node;
78
+ newTr.setNodeMarkup(pos, actualTargetIndex !== 0 || rowHeaderEnabled && y === 0 ? types.header_cell : types.cell, node.attrs);
79
+ }
80
+ }
81
+ origins.forEach(function (row, y) {
82
+ if (!row.length) {
83
+ // If the origin has no cells to be moved then we can skip moving for this row. This can occur when a cell above rowspans
84
+ // into the current row.
85
+ return;
86
+ }
87
+
88
+ // The actual target index needs to be translated per row, this is because row/col spans can affect the amount of
89
+ // cells each row contains.
90
+ var rowTargetPosition = translateTargetPosition(y, actualTargetIndex, tableMap);
91
+ var node = table.node.nodeAt(rowTargetPosition);
92
+ var pos = table.start + rowTargetPosition;
93
+ var insertPos = direction === 'end' ? newTr.mapping.map(pos + node.nodeSize, 1) : newTr.mapping.map(pos, -1);
94
+ return newTr.insert(insertPos, row.map(function (_ref, x) {
95
+ var node = _ref.node;
96
+ return normalizeCellNode(node, rowHeaderEnabled && y === 0, columnHeaderEnabled && actualTargetIndex === 0 && x === 0 && direction === 'start', types);
97
+ }));
98
+ });
99
+ if (options.selectAfterClone) {
100
+ var offset = direction === 'end' ? 1 : 0;
101
+ var selectionRange = (0, _getSelectionRangeInColumn.getSelectionRangeInColumn)(actualTargetIndex + offset, actualTargetIndex + offset + indexesOriginColumn.length - 1)(newTr);
102
+ if (selectionRange) {
103
+ newTr.setSelection(new _cellSelection.CellSelection(selectionRange.$anchor, selectionRange.$head));
104
+ }
105
+ }
106
+ return newTr;
107
+ };
108
+ };
109
+ function normalizeCellNode(cellNode, rowHeaderEnabled, columnHeaderEnabled, types) {
110
+ var newTargetType = rowHeaderEnabled || columnHeaderEnabled ? types.header_cell : types.cell;
111
+ return cellNode.type !== newTargetType ? newTargetType.create(cellNode.attrs, cellNode.content, cellNode.marks) : cellNode;
112
+ }
113
+ function createContentNodeFactory(table) {
114
+ return function (nodePos) {
115
+ var node = table.node.nodeAt(nodePos);
116
+ var pos = nodePos + table.start;
117
+ return {
118
+ pos: pos,
119
+ start: pos + 1,
120
+ node: node,
121
+ depth: table.depth + 2
122
+ };
123
+ };
124
+ }
125
+ function translateTargetPosition(row, startIndex, tableMap) {
126
+ if (tableMap.isCellMergedTopLeft(row, startIndex)) {
127
+ // find the closet unmerged position to the left of the target. We scan left first because merged cells will actually
128
+ // reduce the amount of cells in a row.
129
+ for (var x = startIndex - 1; x >= 0; x--) {
130
+ if (!tableMap.isCellMergedTopLeft(row, x)) {
131
+ return tableMap.map[row * tableMap.width + x];
132
+ }
133
+ }
134
+
135
+ // If no index found then we need to look to the right, this can occur when the first cell in the row is merged.
136
+ for (var _x = startIndex + 1; _x < tableMap.width; _x++) {
137
+ if (!tableMap.isCellMergedTopLeft(row, _x)) {
138
+ return tableMap.map[row * tableMap.width + _x];
139
+ }
140
+ }
141
+ }
142
+ return tableMap.map[row * tableMap.width + startIndex];
143
+ }
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.cloneRow = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
10
+ var _cellSelection = require("../cell-selection");
11
+ var _tableMap = require("../table-map");
12
+ var _analyseTable = require("./analyse-table");
13
+ var _cloneTr = require("./clone-tr");
14
+ var _find = require("./find");
15
+ var _getSelectionRangeInRow = require("./get-selection-range-in-row");
16
+ var _reorderUtils = require("./reorder-utils");
17
+ var _tableNodeTypes = require("./table-node-types");
18
+ function normalizeDirection(targetDirection, options) {
19
+ var _options$direction;
20
+ var override = ((_options$direction = options === null || options === void 0 ? void 0 : options.direction) !== null && _options$direction !== void 0 ? _options$direction : 0) < 0 ? 'start' : 'end';
21
+ return options !== null && options !== void 0 && options.tryToFit && !!(options !== null && options !== void 0 && options.direction) ? override : targetDirection;
22
+ }
23
+ var cloneRow = exports.cloneRow = function cloneRow(state, originRowIndex, targetRowIndex, targetDirection) {
24
+ var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {
25
+ tryToFit: false,
26
+ direction: 0,
27
+ selectAfterClone: false
28
+ };
29
+ return function (tr) {
30
+ var table = (0, _find.findTable)(tr.selection);
31
+ if (!table) {
32
+ return tr;
33
+ }
34
+
35
+ // normalize the origin index to an array since move row support moving both a single & multiple rows in a single action.
36
+ if (!Array.isArray(originRowIndex)) {
37
+ originRowIndex = [originRowIndex];
38
+ }
39
+ var tableMap = _tableMap.TableMap.get(table.node);
40
+ var _originRowIndex$reduc = originRowIndex.reduce(function (_ref, cur) {
41
+ var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
42
+ min = _ref2[0],
43
+ max = _ref2[1];
44
+ return [Math.min(min, cur), Math.max(max, cur)];
45
+ }, [Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER]),
46
+ _originRowIndex$reduc2 = (0, _slicedToArray2.default)(_originRowIndex$reduc, 2),
47
+ originMin = _originRowIndex$reduc2[0],
48
+ originMax = _originRowIndex$reduc2[1];
49
+ var originalRowRanges = (0, _getSelectionRangeInRow.getSelectionRangeInRow)(originMin, originMax)(tr);
50
+ var targetRowRanges = (0, _getSelectionRangeInRow.getSelectionRangeInRow)(targetRowIndex)(tr);
51
+ var indexesOriginRow = (originalRowRanges === null || originalRowRanges === void 0 ? void 0 : originalRowRanges.indexes) || [];
52
+ var indexesTargetRow = (targetRowRanges === null || targetRowRanges === void 0 ? void 0 : targetRowRanges.indexes) || [];
53
+ if (originMin < 0 || originMin === Number.MAX_SAFE_INTEGER || originMax >= tableMap.height || originMax === Number.MIN_SAFE_INTEGER) {
54
+ return tr;
55
+ }
56
+ if (!options.tryToFit && indexesTargetRow.length > 1) {
57
+ (0, _reorderUtils.isValidReorder)(originMin, targetRowIndex, indexesTargetRow, 'row');
58
+ }
59
+ var types = (0, _tableNodeTypes.tableNodeTypes)(state.schema);
60
+ var direction = normalizeDirection(targetDirection, options);
61
+ var actualTargetIndex = Math[direction === 'start' ? 'min' : 'max'].apply(Math, (0, _toConsumableArray2.default)(indexesTargetRow));
62
+ var originPositions = indexesOriginRow.map(function (index) {
63
+ return tableMap.positionAt(index, 0, table.node) + table.pos;
64
+ });
65
+ var originNodes = originPositions.reduce(function (acc, pos) {
66
+ var node = tr.doc.nodeAt(tr.mapping.map(pos));
67
+ if (node) {
68
+ return [].concat((0, _toConsumableArray2.default)(acc), [{
69
+ pos: pos,
70
+ node: node
71
+ }]);
72
+ }
73
+ return acc;
74
+ }, []);
75
+ var targetPos = tableMap.positionAt(actualTargetIndex, 0, table.node) + table.pos;
76
+ var targetNode = tr.doc.nodeAt(tr.mapping.map(targetPos));
77
+ if (originNodes !== null && originNodes !== void 0 && originNodes.length && targetNode) {
78
+ var newTr = (0, _cloneTr.cloneTr)(tr);
79
+ var _determineTableHeader = (0, _analyseTable.determineTableHeaderStateFromTableNode)(table.node, tableMap, types),
80
+ rowHeaderEnabled = _determineTableHeader.rowHeaderEnabled,
81
+ columnHeaderEnabled = _determineTableHeader.columnHeaderEnabled;
82
+ if (rowHeaderEnabled && actualTargetIndex === 0 && direction === 'start') {
83
+ // This block is handling the situation where a row is moved in/out of the header position. If the header row option
84
+ // is enabled then;
85
+ // When a row is moved out, the row will be converted to a normal row and the row below it will become the header.
86
+ // When a row is moved in, the old row header needs to be made normal, and the incoming row needs to be made a header.
87
+ // This section only manages what happens to the other row, no the one being moved.
88
+ var nearHeaderPos = tableMap.positionAt(originMin === 0 ? originMax + 1 : actualTargetIndex, 0, table.node) + table.pos;
89
+ var nearHeaderNode = newTr.doc.nodeAt(newTr.mapping.map(nearHeaderPos));
90
+ if (nearHeaderNode) {
91
+ nearHeaderNode.forEach(function (node, offset, index) {
92
+ var start = newTr.mapping.map(nearHeaderPos + 1 + offset);
93
+ newTr.setNodeMarkup(start, actualTargetIndex !== 0 || columnHeaderEnabled && index === 0 ? types.header_cell : types.cell, node.attrs);
94
+ });
95
+ }
96
+ }
97
+ var insertPos = direction === 'end' ? newTr.mapping.map(targetPos + targetNode.nodeSize, 1) : newTr.mapping.map(targetPos, -1);
98
+ newTr.insert(insertPos, originNodes.map(function (_ref3, index) {
99
+ var node = _ref3.node;
100
+ return normalizeRowNode(node, rowHeaderEnabled && actualTargetIndex === 0 && index === 0 && direction === 'start', columnHeaderEnabled, types);
101
+ }));
102
+ if (options.selectAfterClone) {
103
+ var offset = direction === 'end' ? 1 : 0;
104
+ var selectionRange = (0, _getSelectionRangeInRow.getSelectionRangeInRow)(actualTargetIndex + offset, actualTargetIndex + offset + originNodes.length - 1)(newTr);
105
+ if (selectionRange) {
106
+ newTr.setSelection(new _cellSelection.CellSelection(selectionRange.$anchor, selectionRange.$head));
107
+ }
108
+ }
109
+ return newTr;
110
+ }
111
+ return tr;
112
+ };
113
+ };
114
+
115
+ /**
116
+ * This ensures the row node cell type correctly reflect what they should be.
117
+ * @returns A copy of the rowNode
118
+ */
119
+ function normalizeRowNode(rowNode, rowHeaderEnabled, columnHeaderEnabled, types) {
120
+ var content = [];
121
+ rowNode.forEach(function (node, offset, index) {
122
+ var newTargetType = rowHeaderEnabled || columnHeaderEnabled && index === 0 ? types.header_cell : types.cell;
123
+ content.push(node.type !== newTargetType ? newTargetType.create(node.attrs, node.content, node.marks) : node);
124
+ });
125
+ return rowNode.type.create(rowNode.attrs, content, rowNode.marks);
126
+ }
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.selectTableClosestToPos = exports.selectTable = exports.selectRows = exports.selectRow = exports.selectColumns = exports.selectColumn = void 0;
8
8
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
- var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
10
9
  var _cellSelection = require("../cell-selection");
11
10
  var _tableMap = require("../table-map");
12
11
  var _cloneTr = require("./clone-tr");
@@ -37,52 +36,48 @@ var select = function select(type) {
37
36
  if (isRowSelection) {
38
37
  top = Math.min(top, selRect.top);
39
38
  bottom = Math.max(bottom, selRect.bottom);
40
- if ((0, _platformFeatureFlags.getBooleanFF)('platform.editor.table-shift-click-selection-backward')) {
41
- cellsInFirstRow = map.cellsInRect({
42
- left: left,
43
- top: top,
44
- right: right,
45
- bottom: top + 1
46
- });
47
- var targetRowCells = map.cellsInRect({
48
- left: left,
49
- top: index,
50
- right: right,
51
- bottom: index + 1
52
- });
53
- var isBackwardSelection = targetRowCells[0] < prevSelection.$head.pos - table.start;
54
- if (isBackwardSelection && isPrevRowSelection) {
55
- var _head = table.start + cellsInFirstRow[0];
56
- var _anchor = prevSelection.$anchorCell.pos;
57
- var _$head = tr.doc.resolve(_head);
58
- var _$anchor = tr.doc.resolve(_anchor);
59
- return (0, _cloneTr.cloneTr)(tr.setSelection(new _cellSelection.CellSelection(_$anchor, _$head)));
60
- }
39
+ cellsInFirstRow = map.cellsInRect({
40
+ left: left,
41
+ top: top,
42
+ right: right,
43
+ bottom: top + 1
44
+ });
45
+ var targetRowCells = map.cellsInRect({
46
+ left: left,
47
+ top: index,
48
+ right: right,
49
+ bottom: index + 1
50
+ });
51
+ var isBackwardSelection = targetRowCells[0] < prevSelection.$head.pos - table.start;
52
+ if (isBackwardSelection && isPrevRowSelection) {
53
+ var _head = table.start + cellsInFirstRow[0];
54
+ var _anchor = prevSelection.$anchorCell.pos;
55
+ var _$head = tr.doc.resolve(_head);
56
+ var _$anchor = tr.doc.resolve(_anchor);
57
+ return (0, _cloneTr.cloneTr)(tr.setSelection(new _cellSelection.CellSelection(_$anchor, _$head)));
61
58
  }
62
59
  } else {
63
60
  left = Math.min(left, selRect.left);
64
61
  right = Math.max(right, selRect.right);
65
- if ((0, _platformFeatureFlags.getBooleanFF)('platform.editor.table-shift-click-selection-backward')) {
66
- cellsInFirstRow = map.cellsInRect({
67
- left: left,
68
- top: top,
69
- right: left + 1,
70
- bottom: bottom
71
- });
72
- var _targetRowCells = map.cellsInRect({
73
- left: index,
74
- top: top,
75
- right: index + 1,
76
- bottom: bottom
77
- });
78
- var _isBackwardSelection = _targetRowCells[0] < prevSelection.$head.pos - table.start;
79
- if (_isBackwardSelection && isPrevRowSelection) {
80
- var _head2 = table.start + cellsInFirstRow[0];
81
- var _anchor2 = prevSelection.$anchorCell.pos;
82
- var _$head2 = tr.doc.resolve(_head2);
83
- var _$anchor2 = tr.doc.resolve(_anchor2);
84
- return (0, _cloneTr.cloneTr)(tr.setSelection(new _cellSelection.CellSelection(_$anchor2, _$head2)));
85
- }
62
+ cellsInFirstRow = map.cellsInRect({
63
+ left: left,
64
+ top: top,
65
+ right: left + 1,
66
+ bottom: bottom
67
+ });
68
+ var _targetRowCells = map.cellsInRect({
69
+ left: index,
70
+ top: top,
71
+ right: index + 1,
72
+ bottom: bottom
73
+ });
74
+ var _isBackwardSelection = _targetRowCells[0] < prevSelection.$head.pos - table.start;
75
+ if (_isBackwardSelection && isPrevRowSelection) {
76
+ var _head2 = table.start + cellsInFirstRow[0];
77
+ var _anchor2 = prevSelection.$anchorCell.pos;
78
+ var _$head2 = tr.doc.resolve(_head2);
79
+ var _$anchor2 = tr.doc.resolve(_anchor2);
80
+ return (0, _cloneTr.cloneTr)(tr.setSelection(new _cellSelection.CellSelection(_$anchor2, _$head2)));
86
81
  }
87
82
  }
88
83
  }
package/dist/cjs/utils.js CHANGED
@@ -51,6 +51,18 @@ Object.defineProperty(exports, "cellWrapping", {
51
51
  return _splitCellWithType.cellWrapping;
52
52
  }
53
53
  });
54
+ Object.defineProperty(exports, "cloneColumn", {
55
+ enumerable: true,
56
+ get: function get() {
57
+ return _cloneColumn.cloneColumn;
58
+ }
59
+ });
60
+ Object.defineProperty(exports, "cloneRow", {
61
+ enumerable: true,
62
+ get: function get() {
63
+ return _cloneRow.cloneRow;
64
+ }
65
+ });
54
66
  Object.defineProperty(exports, "cloneTr", {
55
67
  enumerable: true,
56
68
  get: function get() {
@@ -430,6 +442,8 @@ var _isSelected = require("./utils/is-selected");
430
442
  var _isSelectionType = require("./utils/is-selection-type");
431
443
  var _moveColumn = require("./utils/move-column");
432
444
  var _moveRow = require("./utils/move-row");
445
+ var _cloneColumn = require("./utils/clone-column");
446
+ var _cloneRow = require("./utils/clone-row");
433
447
  var _normalizeSelection = require("./utils/normalize-selection");
434
448
  var _removeColumn = require("./utils/remove-column");
435
449
  var _removeRow = require("./utils/remove-row");
@@ -142,8 +142,7 @@ export function handleMouseDown(view, event, dragAndDropEnabled) {
142
142
  if (startEvent.shiftKey && view.state.selection instanceof CellSelection) {
143
143
  let isDragHandleElement;
144
144
  const targetElement = event.target instanceof HTMLElement || event.target instanceof SVGElement ? event.target : null;
145
- isDragHandleElement = !!targetElement && !!targetElement.closest('button.pm-table-drag-handle-button-container');
146
- // Adding to an existing cell selection
145
+ isDragHandleElement = !!targetElement && !!targetElement.closest('button.pm-table-drag-handle-button-container, button.pm-table-drag-handle-button-clickable-zone');
147
146
  if (dragAndDropEnabled && isDragHandleElement) {
148
147
  return false;
149
148
  }
@@ -0,0 +1,133 @@
1
+ import { CellSelection } from '../cell-selection';
2
+ import { TableMap } from '../table-map';
3
+ import { determineTableHeaderStateFromTableNode } from './analyse-table';
4
+ import { cloneTr } from './clone-tr';
5
+ import { findTable } from './find';
6
+ import { getSelectionRangeInColumn } from './get-selection-range-in-column';
7
+ import { isValidReorder } from './reorder-utils';
8
+ import { tableNodeTypes } from './table-node-types';
9
+ function normalizeDirection(targetDirection, options) {
10
+ var _options$direction;
11
+ const override = ((_options$direction = options === null || options === void 0 ? void 0 : options.direction) !== null && _options$direction !== void 0 ? _options$direction : 0) < 0 ? 'start' : 'end';
12
+ return options !== null && options !== void 0 && options.tryToFit && !!(options !== null && options !== void 0 && options.direction) ? override : targetDirection;
13
+ }
14
+ export const cloneColumn = (state, originColumnIndex, targetColumnIndex, targetDirection, options = {
15
+ tryToFit: false,
16
+ direction: 0,
17
+ selectAfterClone: false
18
+ }) => tr => {
19
+ var _originalColumnRanges, _targetColumnRanges$i;
20
+ const table = findTable(tr.selection);
21
+ if (!table) {
22
+ return tr;
23
+ }
24
+
25
+ // normalize the origin index to an array since this supports moving both a single & multiple cols in a single action.
26
+ if (!Array.isArray(originColumnIndex)) {
27
+ originColumnIndex = [originColumnIndex];
28
+ }
29
+ const tableMap = TableMap.get(table.node);
30
+ const originalColumnRanges = getSelectionRangeInColumn(Math.min(...originColumnIndex), Math.max(...originColumnIndex))(tr);
31
+ const targetColumnRanges = getSelectionRangeInColumn(targetColumnIndex)(tr);
32
+ const indexesOriginColumn = (_originalColumnRanges = originalColumnRanges === null || originalColumnRanges === void 0 ? void 0 : originalColumnRanges.indexes) !== null && _originalColumnRanges !== void 0 ? _originalColumnRanges : [];
33
+ const indexesTargetColumn = (_targetColumnRanges$i = targetColumnRanges === null || targetColumnRanges === void 0 ? void 0 : targetColumnRanges.indexes) !== null && _targetColumnRanges$i !== void 0 ? _targetColumnRanges$i : [];
34
+ const min = indexesOriginColumn[0];
35
+ const max = indexesOriginColumn[indexesOriginColumn.length - 1];
36
+ if (!options.tryToFit && indexesTargetColumn.length > 1) {
37
+ isValidReorder(originColumnIndex[0], targetColumnIndex, indexesTargetColumn, 'column');
38
+ }
39
+ const types = tableNodeTypes(state.schema);
40
+ const direction = normalizeDirection(targetDirection, options);
41
+ const actualTargetIndex = Math[direction === 'start' ? 'min' : 'max'](...indexesTargetColumn);
42
+ const {
43
+ rowHeaderEnabled,
44
+ columnHeaderEnabled
45
+ } = determineTableHeaderStateFromTableNode(table.node, tableMap, types);
46
+ const createContentNode = createContentNodeFactory(table);
47
+ const newTr = cloneTr(tr);
48
+ const origins = [];
49
+ for (let y = 0; y < tableMap.height; y++) {
50
+ origins.push([]);
51
+ for (let x = min; x <= max; x++) {
52
+ if (tableMap.isCellMergedTopLeft(y, x)) {
53
+ continue;
54
+ }
55
+ const nodePos = tableMap.map[y * tableMap.width + x];
56
+ origins[y].push(createContentNode(nodePos));
57
+ }
58
+ if (columnHeaderEnabled && actualTargetIndex === 0 && direction === 'start') {
59
+ // This block is handling the situation where a col is moved in/out of the header position. If the header col option
60
+ // is enabled then;
61
+ // When a col is moved out, the col will be converted to a normal col and the col to the right will become the header.
62
+ // When a col is moved in, the old col header needs to be made normal, and the incoming col needs to be made a header.
63
+ // This section only manages what happens to the other col, not the one being moved.
64
+ const nearHeaderCol = min === 0 ? max + 1 : actualTargetIndex;
65
+ const nodePos = tableMap.map[y * tableMap.width + nearHeaderCol];
66
+ const {
67
+ pos,
68
+ node
69
+ } = createContentNode(nodePos);
70
+ newTr.setNodeMarkup(pos, actualTargetIndex !== 0 || rowHeaderEnabled && y === 0 ? types.header_cell : types.cell, node.attrs);
71
+ }
72
+ }
73
+ origins.forEach((row, y) => {
74
+ if (!row.length) {
75
+ // If the origin has no cells to be moved then we can skip moving for this row. This can occur when a cell above rowspans
76
+ // into the current row.
77
+ return;
78
+ }
79
+
80
+ // The actual target index needs to be translated per row, this is because row/col spans can affect the amount of
81
+ // cells each row contains.
82
+ const rowTargetPosition = translateTargetPosition(y, actualTargetIndex, tableMap);
83
+ const node = table.node.nodeAt(rowTargetPosition);
84
+ const pos = table.start + rowTargetPosition;
85
+ const insertPos = direction === 'end' ? newTr.mapping.map(pos + node.nodeSize, 1) : newTr.mapping.map(pos, -1);
86
+ return newTr.insert(insertPos, row.map(({
87
+ node
88
+ }, x) => normalizeCellNode(node, rowHeaderEnabled && y === 0, columnHeaderEnabled && actualTargetIndex === 0 && x === 0 && direction === 'start', types)));
89
+ });
90
+ if (options.selectAfterClone) {
91
+ const offset = direction === 'end' ? 1 : 0;
92
+ const selectionRange = getSelectionRangeInColumn(actualTargetIndex + offset, actualTargetIndex + offset + indexesOriginColumn.length - 1)(newTr);
93
+ if (selectionRange) {
94
+ newTr.setSelection(new CellSelection(selectionRange.$anchor, selectionRange.$head));
95
+ }
96
+ }
97
+ return newTr;
98
+ };
99
+ function normalizeCellNode(cellNode, rowHeaderEnabled, columnHeaderEnabled, types) {
100
+ const newTargetType = rowHeaderEnabled || columnHeaderEnabled ? types.header_cell : types.cell;
101
+ return cellNode.type !== newTargetType ? newTargetType.create(cellNode.attrs, cellNode.content, cellNode.marks) : cellNode;
102
+ }
103
+ function createContentNodeFactory(table) {
104
+ return nodePos => {
105
+ const node = table.node.nodeAt(nodePos);
106
+ const pos = nodePos + table.start;
107
+ return {
108
+ pos,
109
+ start: pos + 1,
110
+ node,
111
+ depth: table.depth + 2
112
+ };
113
+ };
114
+ }
115
+ function translateTargetPosition(row, startIndex, tableMap) {
116
+ if (tableMap.isCellMergedTopLeft(row, startIndex)) {
117
+ // find the closet unmerged position to the left of the target. We scan left first because merged cells will actually
118
+ // reduce the amount of cells in a row.
119
+ for (let x = startIndex - 1; x >= 0; x--) {
120
+ if (!tableMap.isCellMergedTopLeft(row, x)) {
121
+ return tableMap.map[row * tableMap.width + x];
122
+ }
123
+ }
124
+
125
+ // If no index found then we need to look to the right, this can occur when the first cell in the row is merged.
126
+ for (let x = startIndex + 1; x < tableMap.width; x++) {
127
+ if (!tableMap.isCellMergedTopLeft(row, x)) {
128
+ return tableMap.map[row * tableMap.width + x];
129
+ }
130
+ }
131
+ }
132
+ return tableMap.map[row * tableMap.width + startIndex];
133
+ }