@atlaskit/editor-tables 2.2.4 → 2.2.6

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 (158) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/cell-bookmark.js +0 -10
  3. package/dist/cjs/cell-selection.js +37 -102
  4. package/dist/cjs/index.js +0 -4
  5. package/dist/cjs/pm-plugins/input.js +17 -83
  6. package/dist/cjs/pm-plugins/plugin-key.js +0 -2
  7. package/dist/cjs/pm-plugins/table-editing.js +17 -18
  8. package/dist/cjs/pm-plugins.js +0 -2
  9. package/dist/cjs/table-map.js +67 -138
  10. package/dist/cjs/utils/add-column-at.js +0 -9
  11. package/dist/cjs/utils/add-column.js +8 -24
  12. package/dist/cjs/utils/add-row-at.js +8 -37
  13. package/dist/cjs/utils/add-row.js +2 -23
  14. package/dist/cjs/utils/analytics-helpers.js +4 -15
  15. package/dist/cjs/utils/cells.js +0 -12
  16. package/dist/cjs/utils/clone-tr.js +0 -2
  17. package/dist/cjs/utils/colspan.js +2 -18
  18. package/dist/cjs/utils/copy-paste.js +51 -139
  19. package/dist/cjs/utils/create-table.js +14 -28
  20. package/dist/cjs/utils/draw-cell-selection.js +0 -4
  21. package/dist/cjs/utils/empty-cells.js +0 -6
  22. package/dist/cjs/utils/find.js +6 -18
  23. package/dist/cjs/utils/fix-tables.js +13 -56
  24. package/dist/cjs/utils/for-each-cell.js +3 -17
  25. package/dist/cjs/utils/get-cell-selection-ranges.js +2 -9
  26. package/dist/cjs/utils/get-cells-in-column.js +0 -7
  27. package/dist/cjs/utils/get-cells-in-row.js +0 -7
  28. package/dist/cjs/utils/get-cells-in-table.js +0 -8
  29. package/dist/cjs/utils/get-selection-range-in-column.js +6 -27
  30. package/dist/cjs/utils/get-selection-range-in-row.js +6 -27
  31. package/dist/cjs/utils/get-selection-rect.js +0 -7
  32. package/dist/cjs/utils/go-to-next-cell.js +3 -22
  33. package/dist/cjs/utils/handle-paste.js +5 -34
  34. package/dist/cjs/utils/is-selected.js +6 -23
  35. package/dist/cjs/utils/is-selection-type.js +0 -2
  36. package/dist/cjs/utils/move-column.js +0 -12
  37. package/dist/cjs/utils/move-row.js +0 -11
  38. package/dist/cjs/utils/normalize-selection.js +3 -27
  39. package/dist/cjs/utils/remove-column.js +10 -46
  40. package/dist/cjs/utils/remove-row.js +8 -49
  41. package/dist/cjs/utils/remove-table.js +0 -6
  42. package/dist/cjs/utils/reorder-utils.js +13 -37
  43. package/dist/cjs/utils/replace-table.js +0 -8
  44. package/dist/cjs/utils/select-nodes.js +11 -31
  45. package/dist/cjs/utils/selection-cell.js +0 -5
  46. package/dist/cjs/utils/selection-rect.js +0 -10
  47. package/dist/cjs/utils/set-cell-attrs.js +0 -4
  48. package/dist/cjs/utils/split-cell-with-type.js +2 -35
  49. package/dist/cjs/utils/split-cell.js +0 -4
  50. package/dist/cjs/utils/table-node-types.js +4 -8
  51. package/dist/cjs/utils/tables.js +0 -4
  52. package/dist/cjs/utils/test-utils.js +9 -26
  53. package/dist/cjs/utils/toggle-header.js +3 -16
  54. package/dist/cjs/utils/uuid.js +0 -3
  55. package/dist/cjs/utils.js +0 -42
  56. package/dist/cjs/version.json +1 -1
  57. package/dist/es2019/cell-bookmark.js +0 -5
  58. package/dist/es2019/cell-selection.js +37 -81
  59. package/dist/es2019/index.js +1 -0
  60. package/dist/es2019/pm-plugins/input.js +15 -67
  61. package/dist/es2019/pm-plugins/table-editing.js +3 -12
  62. package/dist/es2019/table-map.js +57 -127
  63. package/dist/es2019/utils/add-column-at.js +2 -4
  64. package/dist/es2019/utils/add-column.js +6 -16
  65. package/dist/es2019/utils/add-row-at.js +8 -22
  66. package/dist/es2019/utils/add-row.js +2 -17
  67. package/dist/es2019/utils/analytics-helpers.js +0 -5
  68. package/dist/es2019/utils/cells.js +4 -9
  69. package/dist/es2019/utils/colspan.js +6 -11
  70. package/dist/es2019/utils/copy-paste.js +41 -115
  71. package/dist/es2019/utils/create-table.js +3 -12
  72. package/dist/es2019/utils/draw-cell-selection.js +0 -1
  73. package/dist/es2019/utils/empty-cells.js +2 -3
  74. package/dist/es2019/utils/find.js +8 -8
  75. package/dist/es2019/utils/fix-tables.js +17 -47
  76. package/dist/es2019/utils/for-each-cell.js +6 -11
  77. package/dist/es2019/utils/get-cell-selection-ranges.js +2 -4
  78. package/dist/es2019/utils/get-cells-in-column.js +2 -3
  79. package/dist/es2019/utils/get-cells-in-row.js +2 -3
  80. package/dist/es2019/utils/get-cells-in-table.js +2 -4
  81. package/dist/es2019/utils/get-selection-range-in-column.js +8 -22
  82. package/dist/es2019/utils/get-selection-range-in-row.js +8 -22
  83. package/dist/es2019/utils/get-selection-rect.js +2 -2
  84. package/dist/es2019/utils/go-to-next-cell.js +3 -19
  85. package/dist/es2019/utils/handle-paste.js +3 -14
  86. package/dist/es2019/utils/is-selected.js +8 -10
  87. package/dist/es2019/utils/is-selection-type.js +0 -1
  88. package/dist/es2019/utils/move-column.js +3 -8
  89. package/dist/es2019/utils/move-row.js +3 -7
  90. package/dist/es2019/utils/normalize-selection.js +0 -16
  91. package/dist/es2019/utils/remove-column.js +10 -27
  92. package/dist/es2019/utils/remove-row.js +12 -32
  93. package/dist/es2019/utils/remove-table.js +2 -4
  94. package/dist/es2019/utils/reorder-utils.js +11 -26
  95. package/dist/es2019/utils/replace-table.js +0 -2
  96. package/dist/es2019/utils/select-nodes.js +9 -20
  97. package/dist/es2019/utils/selection-cell.js +0 -2
  98. package/dist/es2019/utils/selection-rect.js +0 -4
  99. package/dist/es2019/utils/set-cell-attrs.js +2 -2
  100. package/dist/es2019/utils/split-cell-with-type.js +8 -28
  101. package/dist/es2019/utils/split-cell.js +3 -2
  102. package/dist/es2019/utils/table-node-types.js +4 -7
  103. package/dist/es2019/utils/tables.js +0 -2
  104. package/dist/es2019/utils/test-utils.js +12 -16
  105. package/dist/es2019/utils/toggle-header.js +3 -10
  106. package/dist/es2019/utils/uuid.js +0 -2
  107. package/dist/es2019/version.json +1 -1
  108. package/dist/esm/cell-bookmark.js +0 -5
  109. package/dist/esm/cell-selection.js +37 -79
  110. package/dist/esm/index.js +1 -0
  111. package/dist/esm/pm-plugins/input.js +17 -71
  112. package/dist/esm/pm-plugins/table-editing.js +8 -13
  113. package/dist/esm/table-map.js +68 -133
  114. package/dist/esm/utils/add-column-at.js +2 -4
  115. package/dist/esm/utils/add-column.js +8 -20
  116. package/dist/esm/utils/add-row-at.js +8 -23
  117. package/dist/esm/utils/add-row.js +2 -21
  118. package/dist/esm/utils/analytics-helpers.js +4 -10
  119. package/dist/esm/utils/cells.js +0 -5
  120. package/dist/esm/utils/colspan.js +2 -14
  121. package/dist/esm/utils/copy-paste.js +44 -125
  122. package/dist/esm/utils/create-table.js +14 -25
  123. package/dist/esm/utils/draw-cell-selection.js +0 -1
  124. package/dist/esm/utils/empty-cells.js +2 -3
  125. package/dist/esm/utils/find.js +8 -8
  126. package/dist/esm/utils/fix-tables.js +13 -49
  127. package/dist/esm/utils/for-each-cell.js +6 -11
  128. package/dist/esm/utils/get-cell-selection-ranges.js +2 -4
  129. package/dist/esm/utils/get-cells-in-column.js +2 -3
  130. package/dist/esm/utils/get-cells-in-row.js +2 -3
  131. package/dist/esm/utils/get-cells-in-table.js +2 -4
  132. package/dist/esm/utils/get-selection-range-in-column.js +8 -24
  133. package/dist/esm/utils/get-selection-range-in-row.js +8 -24
  134. package/dist/esm/utils/get-selection-rect.js +2 -2
  135. package/dist/esm/utils/go-to-next-cell.js +3 -19
  136. package/dist/esm/utils/handle-paste.js +5 -23
  137. package/dist/esm/utils/is-selected.js +8 -10
  138. package/dist/esm/utils/is-selection-type.js +0 -1
  139. package/dist/esm/utils/move-column.js +3 -8
  140. package/dist/esm/utils/move-row.js +3 -7
  141. package/dist/esm/utils/normalize-selection.js +3 -22
  142. package/dist/esm/utils/remove-column.js +10 -31
  143. package/dist/esm/utils/remove-row.js +8 -35
  144. package/dist/esm/utils/remove-table.js +2 -4
  145. package/dist/esm/utils/reorder-utils.js +11 -26
  146. package/dist/esm/utils/replace-table.js +0 -2
  147. package/dist/esm/utils/select-nodes.js +11 -22
  148. package/dist/esm/utils/selection-cell.js +0 -2
  149. package/dist/esm/utils/selection-rect.js +0 -4
  150. package/dist/esm/utils/set-cell-attrs.js +2 -2
  151. package/dist/esm/utils/split-cell-with-type.js +2 -28
  152. package/dist/esm/utils/split-cell.js +3 -2
  153. package/dist/esm/utils/table-node-types.js +4 -7
  154. package/dist/esm/utils/tables.js +0 -2
  155. package/dist/esm/utils/test-utils.js +12 -16
  156. package/dist/esm/utils/toggle-header.js +3 -10
  157. package/dist/esm/version.json +1 -1
  158. package/package.json +6 -4
@@ -6,11 +6,9 @@ import { findTable } from './find';
6
6
  import { getCellsInRow } from './get-cells-in-row';
7
7
  import { setCellAttrs } from './set-cell-attrs';
8
8
  import { tableNodeTypes } from './table-node-types';
9
-
10
9
  const filterCellsInRow = (rowIndex, predicate) => tr => {
11
10
  let foundCells = [];
12
11
  const cells = getCellsInRow(rowIndex)(tr.selection);
13
-
14
12
  if (cells) {
15
13
  for (let j = cells.length - 1; j >= 0; j--) {
16
14
  if (predicate(cells[j], tr)) {
@@ -18,28 +16,23 @@ const filterCellsInRow = (rowIndex, predicate) => tr => {
18
16
  }
19
17
  }
20
18
  }
21
-
22
19
  return foundCells;
23
- }; // Returns a new transaction that adds a new row after `cloneRowIndex`, cloning the row attributes at `cloneRowIndex`.
24
-
20
+ };
25
21
 
22
+ // Returns a new transaction that adds a new row after `cloneRowIndex`, cloning the row attributes at `cloneRowIndex`.
26
23
  export const cloneRowAt = rowIndex => tr => {
27
24
  const table = findTable(tr.selection);
28
-
29
25
  if (table) {
30
26
  const map = TableMap.get(table.node);
31
-
32
27
  if (rowIndex >= 0 && rowIndex <= map.height) {
33
28
  const tableNode = table.node;
34
29
  const tableNodes = tableNodeTypes(tableNode.type.schema);
35
30
  let rowPos = table.start;
36
-
37
31
  for (let i = 0; i < rowIndex + 1; i++) {
38
32
  rowPos += tableNode.child(i).nodeSize;
39
33
  }
40
-
41
- const cloneRow = tableNode.child(rowIndex); // Re-create the same nodes with same attrs, dropping the node content.
42
-
34
+ const cloneRow = tableNode.child(rowIndex);
35
+ // Re-create the same nodes with same attrs, dropping the node content.
43
36
  let cells = [];
44
37
  let rowWidth = 0;
45
38
  cloneRow.forEach(cell => {
@@ -48,16 +41,15 @@ export const cloneRowAt = rowIndex => tr => {
48
41
  if (cell.attrs.rowspan === 1) {
49
42
  rowWidth += cell.attrs.colspan;
50
43
  const node = tableNodes[cell.type.spec.tableRole].createAndFill(cell.attrs, [], cell.marks);
51
-
52
44
  if (node) {
53
45
  cells.push(node);
54
46
  }
55
47
  }
56
- }); // If a higher row spans past our clone row, bump the higher row to cover this new row too.
48
+ });
57
49
 
50
+ // If a higher row spans past our clone row, bump the higher row to cover this new row too.
58
51
  if (rowWidth < map.width) {
59
52
  let rowSpanCells = [];
60
-
61
53
  for (let i = rowIndex; i >= 0; i--) {
62
54
  let foundCells = filterCellsInRow(i, (cell, tr) => {
63
55
  const rowspan = cell.node.attrs.rowspan;
@@ -66,7 +58,6 @@ export const cloneRowAt = rowIndex => tr => {
66
58
  })(tr);
67
59
  rowSpanCells.push(...foundCells);
68
60
  }
69
-
70
61
  if (rowSpanCells.length) {
71
62
  rowSpanCells.forEach(cell => {
72
63
  tr = setCellAttrs(cell, {
@@ -75,25 +66,21 @@ export const cloneRowAt = rowIndex => tr => {
75
66
  });
76
67
  }
77
68
  }
78
-
79
69
  return safeInsert(tableNodes.row.create(cloneRow.attrs, cells), rowPos)(tr);
80
70
  }
81
71
  }
82
-
83
72
  return tr;
84
- }; // Returns a new transaction that adds a new row at index `rowIndex`. Optionally clone the previous row.
73
+ };
85
74
 
75
+ // Returns a new transaction that adds a new row at index `rowIndex`. Optionally clone the previous row.
86
76
  export const addRowAt = (rowIndex, clonePreviousRow) => tr => {
87
77
  const table = findTable(tr.selection);
88
-
89
78
  if (table) {
90
79
  const map = TableMap.get(table.node);
91
80
  const cloneRowIndex = rowIndex - 1;
92
-
93
81
  if (clonePreviousRow && cloneRowIndex >= 0) {
94
82
  return cloneTr(cloneRowAt(cloneRowIndex)(tr));
95
83
  }
96
-
97
84
  if (rowIndex >= 0 && rowIndex <= map.height) {
98
85
  return cloneTr(addRow(tr, {
99
86
  map,
@@ -102,6 +89,5 @@ export const addRowAt = (rowIndex, clonePreviousRow) => tr => {
102
89
  }, rowIndex));
103
90
  }
104
91
  }
105
-
106
92
  return tr;
107
93
  };
@@ -1,74 +1,59 @@
1
1
  import { tableNodeTypes } from './table-node-types';
2
-
3
2
  function rowIsHeader(map, table, row) {
4
3
  const headerCell = tableNodeTypes(table.type.schema).header_cell;
5
-
6
4
  for (let col = 0; col < map.width; col++) {
7
5
  const cell = table.nodeAt(map.map[col + row * map.width]);
8
-
9
6
  if (cell && cell.type !== headerCell) {
10
7
  return false;
11
8
  }
12
9
  }
13
-
14
10
  return true;
15
11
  }
16
-
17
12
  export function addRow(tr, {
18
13
  map,
19
14
  tableStart,
20
15
  table
21
16
  }, row) {
22
17
  let rowPos = tableStart;
23
-
24
18
  for (let i = 0; i < row; i++) {
25
19
  rowPos += table.child(i).nodeSize;
26
20
  }
27
-
28
21
  const cells = [];
29
22
  let refRow = row > 0 ? -1 : 0;
30
-
31
23
  if (rowIsHeader(map, table, row + refRow)) {
32
24
  refRow = row === 0 || row === map.height ? null : 0;
33
25
  }
34
-
35
26
  for (let col = 0, index = map.width * row; col < map.width; col++, index++) {
36
27
  // Covered by a rowspan cell
37
28
  if (row > 0 && row < map.height && map.map[index] === map.map[index - map.width]) {
38
29
  const pos = map.map[index];
39
30
  const node = table.nodeAt(pos);
40
-
41
31
  if (!node) {
42
32
  throw new Error(`addRow: node not found at pos ${pos}`);
43
33
  }
44
-
45
34
  const {
46
35
  attrs
47
36
  } = node;
48
- tr.setNodeMarkup(tableStart + pos, undefined, { ...attrs,
37
+ tr.setNodeMarkup(tableStart + pos, undefined, {
38
+ ...attrs,
49
39
  rowspan: attrs.rowspan + 1
50
40
  });
51
41
  col += attrs.colspan - 1;
52
42
  } else {
53
43
  let type;
54
-
55
44
  if (refRow == null) {
56
45
  type = tableNodeTypes(table.type.schema).cell;
57
46
  } else {
58
47
  const mappedPos = map.map[index + refRow * map.width];
59
48
  const cell = table.nodeAt(mappedPos);
60
-
61
49
  if (!cell) {
62
50
  throw new Error(`addRow: invalid node at mapped pos ${mappedPos}`);
63
51
  }
64
-
65
52
  type = cell.type;
66
53
  }
67
-
68
54
  cells.push(type.createAndFill());
69
55
  }
70
56
  }
71
-
72
57
  const rowType = tableNodeTypes(table.type.schema).row;
73
58
  const rowCells = rowType.create(null, cells);
74
59
  tr.insert(rowPos, rowCells);
@@ -6,13 +6,11 @@ export function getSelectedTableInfo(selection) {
6
6
  let totalRowCount = 0;
7
7
  let totalColumnCount = 0;
8
8
  const table = findTable(selection);
9
-
10
9
  if (table) {
11
10
  map = TableMap.get(table.node);
12
11
  totalRowCount = map.height;
13
12
  totalColumnCount = map.width;
14
13
  }
15
-
16
14
  return {
17
15
  table,
18
16
  map,
@@ -30,17 +28,14 @@ export function getSelectedCellInfo(selection) {
30
28
  totalRowCount,
31
29
  totalColumnCount
32
30
  } = getSelectedTableInfo(selection);
33
-
34
31
  if (table && map) {
35
32
  const rect = getSelectionRect(selection);
36
-
37
33
  if (rect) {
38
34
  totalCells = map.cellsInRect(rect).length;
39
35
  horizontalCells = rect.right - rect.left;
40
36
  verticalCells = rect.bottom - rect.top;
41
37
  }
42
38
  }
43
-
44
39
  return {
45
40
  totalRowCount,
46
41
  totalColumnCount,
@@ -4,25 +4,21 @@ export function pointsAtCell($pos) {
4
4
  }
5
5
  export function cellNear($pos) {
6
6
  for (let after = $pos.nodeAfter, {
7
- pos
8
- } = $pos; after; after = after.firstChild, pos++) {
7
+ pos
8
+ } = $pos; after; after = after.firstChild, pos++) {
9
9
  const role = after.type.spec.tableRole;
10
-
11
10
  if (role === 'cell' || role === 'header_cell') {
12
11
  return $pos.doc.resolve(pos);
13
12
  }
14
13
  }
15
-
16
14
  for (let before = $pos.nodeBefore, {
17
- pos
18
- } = $pos; before; before = before.lastChild, pos--) {
15
+ pos
16
+ } = $pos; before; before = before.lastChild, pos--) {
19
17
  const role = before.type.spec.tableRole;
20
-
21
18
  if (role === 'cell' || role === 'header_cell') {
22
19
  return $pos.doc.resolve(pos - before.nodeSize);
23
20
  }
24
21
  }
25
-
26
22
  return null;
27
23
  }
28
24
  export function cellAround($pos) {
@@ -31,7 +27,6 @@ export function cellAround($pos) {
31
27
  return $pos.node(0).resolve($pos.before(d + 1));
32
28
  }
33
29
  }
34
-
35
30
  return null;
36
31
  }
37
32
  export function nextCell($pos, axis, dir) {
@@ -2,45 +2,40 @@ export function removeColSpan(attrs, pos, n = 1) {
2
2
  if (!attrs.colspan) {
3
3
  throw new Error('removeColSpan(): attrs.colspan not defined');
4
4
  }
5
-
6
- const result = { ...attrs,
5
+ const result = {
6
+ ...attrs,
7
7
  colspan: attrs.colspan - n
8
8
  };
9
-
10
9
  if (result.colwidth) {
11
10
  result.colwidth = result.colwidth.slice();
12
11
  result.colwidth.splice(pos, n);
13
-
14
12
  if (!result.colwidth.some(w => w > 0)) {
15
13
  result.colwidth = undefined;
16
14
  }
17
15
  }
18
-
19
16
  return result;
20
17
  }
21
18
  export function assertColspan(attrs) {
22
19
  if (typeof attrs.colspan === 'undefined') {
23
20
  throw new Error('addColSpan: attrs.colspan is not defined');
24
21
  }
25
-
26
22
  if (typeof attrs.colspan !== 'number' || Number.isNaN(attrs.colspan) || attrs.colspan < 1) {
27
23
  throw new Error(`addColSpan: attrs.colspan must be number >= 1, received: ${attrs.colspan}`);
28
24
  }
29
- } // TODO: replace "addColSpan" from table plugin with this function
25
+ }
30
26
 
27
+ // TODO: replace "addColSpan" from table plugin with this function
31
28
  export function addColSpan(attrs, pos, n = 1) {
32
29
  assertColspan(attrs);
33
- const result = { ...attrs,
30
+ const result = {
31
+ ...attrs,
34
32
  colspan: attrs.colspan + n
35
33
  };
36
-
37
34
  if (result.colwidth) {
38
35
  result.colwidth = result.colwidth.slice();
39
-
40
36
  for (let i = 0; i < n; i++) {
41
37
  result.colwidth.splice(pos, 0, 0);
42
38
  }
43
39
  }
44
-
45
40
  return result;
46
41
  }