@atlaskit/editor-tables 2.1.5 → 2.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.
Files changed (88) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/cell-bookmark/package.json +8 -1
  3. package/cell-selection/package.json +8 -1
  4. package/dist/cjs/pm-plugins/table-editing.js +3 -2
  5. package/dist/cjs/utils/analytics-helpers.js +63 -0
  6. package/dist/cjs/utils/fix-tables.js +11 -3
  7. package/dist/cjs/utils/replace-table.js +31 -0
  8. package/dist/cjs/utils.js +23 -1
  9. package/dist/cjs/version.json +1 -1
  10. package/dist/es2019/pm-plugins/table-editing.js +3 -2
  11. package/dist/es2019/utils/analytics-helpers.js +51 -0
  12. package/dist/es2019/utils/fix-tables.js +14 -5
  13. package/dist/es2019/utils/replace-table.js +18 -0
  14. package/dist/es2019/utils.js +3 -1
  15. package/dist/es2019/version.json +1 -1
  16. package/dist/esm/pm-plugins/table-editing.js +3 -2
  17. package/dist/esm/utils/analytics-helpers.js +51 -0
  18. package/dist/esm/utils/fix-tables.js +14 -5
  19. package/dist/esm/utils/replace-table.js +18 -0
  20. package/dist/esm/utils.js +3 -1
  21. package/dist/esm/version.json +1 -1
  22. package/dist/types/pm-plugins/table-editing.d.ts +4 -2
  23. package/dist/types/utils/analytics-helpers.d.ts +15 -0
  24. package/dist/types/utils/fix-tables.d.ts +7 -2
  25. package/dist/types/utils/replace-table.d.ts +3 -0
  26. package/dist/types/utils.d.ts +2 -0
  27. package/dist/types-ts4.0/cell-bookmark.d.ts +10 -0
  28. package/dist/types-ts4.0/cell-selection.d.ts +25 -0
  29. package/dist/types-ts4.0/index.d.ts +27 -0
  30. package/dist/types-ts4.0/pm-plugins/input.d.ts +4 -0
  31. package/dist/types-ts4.0/pm-plugins/plugin-key.d.ts +3 -0
  32. package/dist/types-ts4.0/pm-plugins/table-editing.d.ts +8 -0
  33. package/dist/types-ts4.0/pm-plugins.d.ts +2 -0
  34. package/dist/types-ts4.0/table-map.d.ts +62 -0
  35. package/dist/types-ts4.0/types.d.ts +36 -0
  36. package/dist/types-ts4.0/utils/add-column-at.d.ts +2 -0
  37. package/dist/types-ts4.0/utils/add-column.d.ts +5 -0
  38. package/dist/types-ts4.0/utils/add-row-at.d.ts +3 -0
  39. package/dist/types-ts4.0/utils/add-row.d.ts +3 -0
  40. package/dist/types-ts4.0/utils/analytics-helpers.d.ts +15 -0
  41. package/dist/types-ts4.0/utils/cells.d.ts +6 -0
  42. package/dist/types-ts4.0/utils/clone-tr.d.ts +2 -0
  43. package/dist/types-ts4.0/utils/colspan.d.ts +4 -0
  44. package/dist/types-ts4.0/utils/copy-paste.d.ts +8 -0
  45. package/dist/types-ts4.0/utils/create-table.d.ts +10 -0
  46. package/dist/types-ts4.0/utils/draw-cell-selection.d.ts +3 -0
  47. package/dist/types-ts4.0/utils/empty-cells.d.ts +4 -0
  48. package/dist/types-ts4.0/utils/find.d.ts +8 -0
  49. package/dist/types-ts4.0/utils/fix-tables.d.ts +9 -0
  50. package/dist/types-ts4.0/utils/for-each-cell.d.ts +4 -0
  51. package/dist/types-ts4.0/utils/get-cell-selection-ranges.d.ts +3 -0
  52. package/dist/types-ts4.0/utils/get-cells-in-column.d.ts +3 -0
  53. package/dist/types-ts4.0/utils/get-cells-in-row.d.ts +3 -0
  54. package/dist/types-ts4.0/utils/get-cells-in-table.d.ts +3 -0
  55. package/dist/types-ts4.0/utils/get-selection-range-in-column.d.ts +3 -0
  56. package/dist/types-ts4.0/utils/get-selection-range-in-row.d.ts +3 -0
  57. package/dist/types-ts4.0/utils/get-selection-rect.d.ts +3 -0
  58. package/dist/types-ts4.0/utils/go-to-next-cell.d.ts +2 -0
  59. package/dist/types-ts4.0/utils/handle-paste.d.ts +3 -0
  60. package/dist/types-ts4.0/utils/is-selected.d.ts +6 -0
  61. package/dist/types-ts4.0/utils/is-selection-type.d.ts +11 -0
  62. package/dist/types-ts4.0/utils/move-column.d.ts +5 -0
  63. package/dist/types-ts4.0/utils/move-row.d.ts +5 -0
  64. package/dist/types-ts4.0/utils/normalize-selection.d.ts +2 -0
  65. package/dist/types-ts4.0/utils/remove-column.d.ts +5 -0
  66. package/dist/types-ts4.0/utils/remove-row.d.ts +5 -0
  67. package/dist/types-ts4.0/utils/remove-table.d.ts +2 -0
  68. package/dist/types-ts4.0/utils/reorder-utils.d.ts +10 -0
  69. package/dist/types-ts4.0/utils/replace-table.d.ts +3 -0
  70. package/dist/types-ts4.0/utils/select-nodes.d.ts +4 -0
  71. package/dist/types-ts4.0/utils/selection-cell.d.ts +3 -0
  72. package/dist/types-ts4.0/utils/selection-rect.d.ts +4 -0
  73. package/dist/types-ts4.0/utils/set-cell-attrs.d.ts +4 -0
  74. package/dist/types-ts4.0/utils/split-cell-with-type.d.ts +11 -0
  75. package/dist/types-ts4.0/utils/split-cell.d.ts +2 -0
  76. package/dist/types-ts4.0/utils/table-node-types.d.ts +5 -0
  77. package/dist/types-ts4.0/utils/tables.d.ts +4 -0
  78. package/dist/types-ts4.0/utils/test-utils.d.ts +5 -0
  79. package/dist/types-ts4.0/utils/toggle-header.d.ts +3 -0
  80. package/dist/types-ts4.0/utils/uuid.d.ts +9 -0
  81. package/dist/types-ts4.0/utils.d.ts +43 -0
  82. package/package.json +10 -3
  83. package/pm-plugins/package.json +8 -1
  84. package/report.api.md +4 -2
  85. package/table-map/package.json +8 -1
  86. package/tsconfig.json +0 -1
  87. package/types/package.json +8 -1
  88. package/utils/package.json +8 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @atlaskit/editor-tables
2
2
 
3
+ ## 2.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`d8acf7254db`](https://bitbucket.org/atlassian/atlassian-frontend/commits/d8acf7254db) - ED-8567 added tracking for fixTables()
8
+
9
+ ## 2.1.6
10
+
11
+ ### Patch Changes
12
+
13
+ - [`95f007063cf`](https://bitbucket.org/atlassian/atlassian-frontend/commits/95f007063cf) - Decoupling paste plugin and table plugin, copied `replaceSelectedTable` and `getSelectedTableInfo` utils to editor-tables utils package and analytics dependency shifted to paste plugin.
14
+
3
15
  ## 2.1.5
4
16
 
5
17
  ### Patch Changes
@@ -4,5 +4,12 @@
4
4
  "module": "../dist/esm/cell-bookmark.js",
5
5
  "module:es2019": "../dist/es2019/cell-bookmark.js",
6
6
  "sideEffects": false,
7
- "types": "../dist/types/cell-bookmark.d.ts"
7
+ "types": "../dist/types/cell-bookmark.d.ts",
8
+ "typesVersions": {
9
+ ">=4.0 <4.5": {
10
+ "*": [
11
+ "../dist/types-ts4.0/cell-bookmark.d.ts"
12
+ ]
13
+ }
14
+ }
8
15
  }
@@ -4,5 +4,12 @@
4
4
  "module": "../dist/esm/cell-selection.js",
5
5
  "module:es2019": "../dist/es2019/cell-selection.js",
6
6
  "sideEffects": false,
7
- "types": "../dist/types/cell-selection.d.ts"
7
+ "types": "../dist/types/cell-selection.d.ts",
8
+ "typesVersions": {
9
+ ">=4.0 <4.5": {
10
+ "*": [
11
+ "../dist/types-ts4.0/cell-selection.d.ts"
12
+ ]
13
+ }
14
+ }
8
15
  }
@@ -22,7 +22,8 @@ var _pluginKey = require("./plugin-key");
22
22
  function tableEditing() {
23
23
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
24
24
  _ref$allowTableNodeSe = _ref.allowTableNodeSelection,
25
- allowTableNodeSelection = _ref$allowTableNodeSe === void 0 ? false : _ref$allowTableNodeSe;
25
+ allowTableNodeSelection = _ref$allowTableNodeSe === void 0 ? false : _ref$allowTableNodeSe,
26
+ reportFixedTable = _ref.reportFixedTable;
26
27
 
27
28
  return new _prosemirrorState.Plugin({
28
29
  key: _pluginKey.tableEditingKey,
@@ -69,7 +70,7 @@ function tableEditing() {
69
70
  handlePaste: _utils.handlePaste
70
71
  },
71
72
  appendTransaction: function appendTransaction(_, oldState, state) {
72
- return (0, _normalizeSelection.normalizeSelection)(state, (0, _fixTables.fixTables)(state, oldState), allowTableNodeSelection);
73
+ return (0, _normalizeSelection.normalizeSelection)(state, (0, _fixTables.fixTables)(state, oldState, reportFixedTable), allowTableNodeSelection);
73
74
  }
74
75
  });
75
76
  }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getSelectedCellInfo = getSelectedCellInfo;
7
+ exports.getSelectedTableInfo = getSelectedTableInfo;
8
+
9
+ var _tableMap = require("../table-map");
10
+
11
+ var _find = require("./find");
12
+
13
+ var _getSelectionRect = require("./get-selection-rect");
14
+
15
+ function getSelectedTableInfo(selection) {
16
+ var map;
17
+ var totalRowCount = 0;
18
+ var totalColumnCount = 0;
19
+ var table = (0, _find.findTable)(selection);
20
+
21
+ if (table) {
22
+ map = _tableMap.TableMap.get(table.node);
23
+ totalRowCount = map.height;
24
+ totalColumnCount = map.width;
25
+ }
26
+
27
+ return {
28
+ table: table,
29
+ map: map,
30
+ totalRowCount: totalRowCount,
31
+ totalColumnCount: totalColumnCount
32
+ };
33
+ }
34
+
35
+ function getSelectedCellInfo(selection) {
36
+ var horizontalCells = 1;
37
+ var verticalCells = 1;
38
+ var totalCells = 1;
39
+
40
+ var _getSelectedTableInfo = getSelectedTableInfo(selection),
41
+ table = _getSelectedTableInfo.table,
42
+ map = _getSelectedTableInfo.map,
43
+ totalRowCount = _getSelectedTableInfo.totalRowCount,
44
+ totalColumnCount = _getSelectedTableInfo.totalColumnCount;
45
+
46
+ if (table && map) {
47
+ var rect = (0, _getSelectionRect.getSelectionRect)(selection);
48
+
49
+ if (rect) {
50
+ totalCells = map.cellsInRect(rect).length;
51
+ horizontalCells = rect.right - rect.left;
52
+ verticalCells = rect.bottom - rect.top;
53
+ }
54
+ }
55
+
56
+ return {
57
+ totalRowCount: totalRowCount,
58
+ totalColumnCount: totalColumnCount,
59
+ horizontalCells: horizontalCells,
60
+ verticalCells: verticalCells,
61
+ totalCells: totalCells
62
+ };
63
+ }
@@ -60,12 +60,12 @@ function changedDescendants(old, cur, offsetStart, f) {
60
60
  // document.
61
61
 
62
62
 
63
- function fixTables(state, oldState) {
63
+ function fixTables(state, oldState, reportFixedTable) {
64
64
  var tr;
65
65
 
66
66
  var check = function check(node, pos) {
67
67
  if (node.type.spec.tableRole === 'table') {
68
- tr = fixTable(state, node, pos, tr);
68
+ tr = fixTable(state, node, pos, tr, reportFixedTable);
69
69
  }
70
70
  };
71
71
 
@@ -81,7 +81,7 @@ function fixTables(state, oldState) {
81
81
  // it was given, if non-null, or create a new one if necessary.
82
82
 
83
83
 
84
- function fixTable(state, table, tablePos, transaction) {
84
+ function fixTable(state, table, tablePos, transaction, reportFixedTable) {
85
85
  var tr = transaction;
86
86
 
87
87
  var map = _tableMap.TableMap.get(table);
@@ -105,6 +105,14 @@ function fixTable(state, table, tablePos, transaction) {
105
105
  for (var _i = 0; _i < map.problems.length; _i++) {
106
106
  var prob = map.problems[_i];
107
107
 
108
+ if (reportFixedTable) {
109
+ reportFixedTable({
110
+ state: state,
111
+ tr: tr,
112
+ reason: prob.type || 'unknown'
113
+ });
114
+ }
115
+
108
116
  if (prob.type === _tableMap.TableProblemTypes.COLLISION) {
109
117
  var collision = prob;
110
118
  var cell = table.nodeAt(prob.pos);
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.replaceSelectedTable = void 0;
7
+
8
+ var _prosemirrorModel = require("prosemirror-model");
9
+
10
+ var _prosemirrorState = require("prosemirror-state");
11
+
12
+ var _find = require("./find");
13
+
14
+ var _isSelected = require("./is-selected");
15
+
16
+ var replaceSelectedTable = function replaceSelectedTable(state, content) {
17
+ if ((0, _isSelected.isTableSelected)(state.selection)) {
18
+ var table = (0, _find.findTable)(state.selection);
19
+
20
+ if (table) {
21
+ var slice = typeof content === 'string' ? new _prosemirrorModel.Slice(_prosemirrorModel.Fragment.from(state.schema.text(content)), 0, 0) : content;
22
+ var tr = state.tr.replace(table.pos, table.pos + table.node.nodeSize, slice);
23
+ tr.setSelection(_prosemirrorState.TextSelection.create(tr.doc, table.pos + slice.size + 1));
24
+ return tr;
25
+ }
26
+ }
27
+
28
+ return state.tr;
29
+ };
30
+
31
+ exports.replaceSelectedTable = replaceSelectedTable;
package/dist/cjs/utils.js CHANGED
@@ -159,6 +159,18 @@ Object.defineProperty(exports, "getCellsInTable", {
159
159
  return _getCellsInTable.getCellsInTable;
160
160
  }
161
161
  });
162
+ Object.defineProperty(exports, "getSelectedCellInfo", {
163
+ enumerable: true,
164
+ get: function get() {
165
+ return _analyticsHelpers.getSelectedCellInfo;
166
+ }
167
+ });
168
+ Object.defineProperty(exports, "getSelectedTableInfo", {
169
+ enumerable: true,
170
+ get: function get() {
171
+ return _analyticsHelpers.getSelectedTableInfo;
172
+ }
173
+ });
162
174
  Object.defineProperty(exports, "getSelectionRangeInColumn", {
163
175
  enumerable: true,
164
176
  get: function get() {
@@ -309,6 +321,12 @@ Object.defineProperty(exports, "removeTable", {
309
321
  return _removeTable.removeTable;
310
322
  }
311
323
  });
324
+ Object.defineProperty(exports, "replaceSelectedTable", {
325
+ enumerable: true,
326
+ get: function get() {
327
+ return _replaceTable.replaceSelectedTable;
328
+ }
329
+ });
312
330
  Object.defineProperty(exports, "selectColumn", {
313
331
  enumerable: true,
314
332
  get: function get() {
@@ -448,4 +466,8 @@ var _toggleHeader = require("./utils/toggle-header");
448
466
 
449
467
  var _reorderUtils = require("./utils/reorder-utils");
450
468
 
451
- var _handlePaste = require("./utils/handle-paste");
469
+ var _handlePaste = require("./utils/handle-paste");
470
+
471
+ var _replaceTable = require("./utils/replace-table");
472
+
473
+ var _analyticsHelpers = require("./utils/analytics-helpers");
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-tables",
3
- "version": "2.1.5",
3
+ "version": "2.2.0",
4
4
  "sideEffects": false
5
5
  }
@@ -18,7 +18,8 @@ import { tableEditingKey } from './plugin-key'; // :: () → Plugin
18
18
  // perform more specific behavior.
19
19
 
20
20
  export function tableEditing({
21
- allowTableNodeSelection = false
21
+ allowTableNodeSelection = false,
22
+ reportFixedTable
22
23
  } = {}) {
23
24
  return new Plugin({
24
25
  key: tableEditingKey,
@@ -70,7 +71,7 @@ export function tableEditing({
70
71
  },
71
72
 
72
73
  appendTransaction(_, oldState, state) {
73
- return normalizeSelection(state, fixTables(state, oldState), allowTableNodeSelection);
74
+ return normalizeSelection(state, fixTables(state, oldState, reportFixedTable), allowTableNodeSelection);
74
75
  }
75
76
 
76
77
  });
@@ -0,0 +1,51 @@
1
+ import { TableMap } from '../table-map';
2
+ import { findTable } from './find';
3
+ import { getSelectionRect } from './get-selection-rect';
4
+ export function getSelectedTableInfo(selection) {
5
+ let map;
6
+ let totalRowCount = 0;
7
+ let totalColumnCount = 0;
8
+ const table = findTable(selection);
9
+
10
+ if (table) {
11
+ map = TableMap.get(table.node);
12
+ totalRowCount = map.height;
13
+ totalColumnCount = map.width;
14
+ }
15
+
16
+ return {
17
+ table,
18
+ map,
19
+ totalRowCount,
20
+ totalColumnCount
21
+ };
22
+ }
23
+ export function getSelectedCellInfo(selection) {
24
+ let horizontalCells = 1;
25
+ let verticalCells = 1;
26
+ let totalCells = 1;
27
+ const {
28
+ table,
29
+ map,
30
+ totalRowCount,
31
+ totalColumnCount
32
+ } = getSelectedTableInfo(selection);
33
+
34
+ if (table && map) {
35
+ const rect = getSelectionRect(selection);
36
+
37
+ if (rect) {
38
+ totalCells = map.cellsInRect(rect).length;
39
+ horizontalCells = rect.right - rect.left;
40
+ verticalCells = rect.bottom - rect.top;
41
+ }
42
+ }
43
+
44
+ return {
45
+ totalRowCount,
46
+ totalColumnCount,
47
+ horizontalCells,
48
+ verticalCells,
49
+ totalCells
50
+ };
51
+ }
@@ -1,10 +1,11 @@
1
1
  import { fixTablesKey } from '../pm-plugins/plugin-key';
2
2
  import { TableMap, TableProblemTypes } from '../table-map';
3
3
  import { removeColSpan } from './colspan';
4
- import { tableNodeTypes } from './table-node-types'; // Helper for iterating through the nodes in a document that changed
4
+ import { tableNodeTypes } from './table-node-types';
5
+
6
+ // Helper for iterating through the nodes in a document that changed
5
7
  // compared to the given previous document. Useful for avoiding
6
8
  // duplicate work on each transaction.
7
-
8
9
  function changedDescendants(old, cur, offsetStart, f) {
9
10
  let offset = offsetStart;
10
11
  const oldSize = old.childCount;
@@ -40,12 +41,12 @@ function changedDescendants(old, cur, offsetStart, f) {
40
41
  // document.
41
42
 
42
43
 
43
- export function fixTables(state, oldState) {
44
+ export function fixTables(state, oldState, reportFixedTable) {
44
45
  let tr;
45
46
 
46
47
  const check = (node, pos) => {
47
48
  if (node.type.spec.tableRole === 'table') {
48
- tr = fixTable(state, node, pos, tr);
49
+ tr = fixTable(state, node, pos, tr, reportFixedTable);
49
50
  }
50
51
  };
51
52
 
@@ -60,7 +61,7 @@ export function fixTables(state, oldState) {
60
61
  // Fix the given table, if necessary. Will append to the transaction
61
62
  // it was given, if non-null, or create a new one if necessary.
62
63
 
63
- export function fixTable(state, table, tablePos, transaction) {
64
+ export function fixTable(state, table, tablePos, transaction, reportFixedTable) {
64
65
  let tr = transaction;
65
66
  const map = TableMap.get(table);
66
67
 
@@ -83,6 +84,14 @@ export function fixTable(state, table, tablePos, transaction) {
83
84
  for (let i = 0; i < map.problems.length; i++) {
84
85
  const prob = map.problems[i];
85
86
 
87
+ if (reportFixedTable) {
88
+ reportFixedTable({
89
+ state,
90
+ tr,
91
+ reason: prob.type || 'unknown'
92
+ });
93
+ }
94
+
86
95
  if (prob.type === TableProblemTypes.COLLISION) {
87
96
  const collision = prob;
88
97
  const cell = table.nodeAt(prob.pos);
@@ -0,0 +1,18 @@
1
+ import { Fragment, Slice } from 'prosemirror-model';
2
+ import { TextSelection } from 'prosemirror-state';
3
+ import { findTable } from './find';
4
+ import { isTableSelected } from './is-selected';
5
+ export const replaceSelectedTable = (state, content) => {
6
+ if (isTableSelected(state.selection)) {
7
+ const table = findTable(state.selection);
8
+
9
+ if (table) {
10
+ const slice = typeof content === 'string' ? new Slice(Fragment.from(state.schema.text(content)), 0, 0) : content;
11
+ let tr = state.tr.replace(table.pos, table.pos + table.node.nodeSize, slice);
12
+ tr.setSelection(TextSelection.create(tr.doc, table.pos + slice.size + 1));
13
+ return tr;
14
+ }
15
+ }
16
+
17
+ return state.tr;
18
+ };
@@ -37,4 +37,6 @@ export { tableNodeTypes } from './utils/table-node-types';
37
37
  export { isInTable, inSameTable } from './utils/tables';
38
38
  export { toggleHeader } from './utils/toggle-header';
39
39
  export { convertArrayOfRowsToTableNode, convertTableNodeToArrayOfRows } from './utils/reorder-utils';
40
- export { handlePaste } from './utils/handle-paste';
40
+ export { handlePaste } from './utils/handle-paste';
41
+ export { replaceSelectedTable } from './utils/replace-table';
42
+ export { getSelectedTableInfo, getSelectedCellInfo } from './utils/analytics-helpers';
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-tables",
3
- "version": "2.1.5",
3
+ "version": "2.2.0",
4
4
  "sideEffects": false
5
5
  }
@@ -20,7 +20,8 @@ import { tableEditingKey } from './plugin-key'; // :: () → Plugin
20
20
  export function tableEditing() {
21
21
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
22
22
  _ref$allowTableNodeSe = _ref.allowTableNodeSelection,
23
- allowTableNodeSelection = _ref$allowTableNodeSe === void 0 ? false : _ref$allowTableNodeSe;
23
+ allowTableNodeSelection = _ref$allowTableNodeSe === void 0 ? false : _ref$allowTableNodeSe,
24
+ reportFixedTable = _ref.reportFixedTable;
24
25
 
25
26
  return new Plugin({
26
27
  key: tableEditingKey,
@@ -67,7 +68,7 @@ export function tableEditing() {
67
68
  handlePaste: handlePaste
68
69
  },
69
70
  appendTransaction: function appendTransaction(_, oldState, state) {
70
- return normalizeSelection(state, fixTables(state, oldState), allowTableNodeSelection);
71
+ return normalizeSelection(state, fixTables(state, oldState, reportFixedTable), allowTableNodeSelection);
71
72
  }
72
73
  });
73
74
  }
@@ -0,0 +1,51 @@
1
+ import { TableMap } from '../table-map';
2
+ import { findTable } from './find';
3
+ import { getSelectionRect } from './get-selection-rect';
4
+ export function getSelectedTableInfo(selection) {
5
+ var map;
6
+ var totalRowCount = 0;
7
+ var totalColumnCount = 0;
8
+ var table = findTable(selection);
9
+
10
+ if (table) {
11
+ map = TableMap.get(table.node);
12
+ totalRowCount = map.height;
13
+ totalColumnCount = map.width;
14
+ }
15
+
16
+ return {
17
+ table: table,
18
+ map: map,
19
+ totalRowCount: totalRowCount,
20
+ totalColumnCount: totalColumnCount
21
+ };
22
+ }
23
+ export function getSelectedCellInfo(selection) {
24
+ var horizontalCells = 1;
25
+ var verticalCells = 1;
26
+ var totalCells = 1;
27
+
28
+ var _getSelectedTableInfo = getSelectedTableInfo(selection),
29
+ table = _getSelectedTableInfo.table,
30
+ map = _getSelectedTableInfo.map,
31
+ totalRowCount = _getSelectedTableInfo.totalRowCount,
32
+ totalColumnCount = _getSelectedTableInfo.totalColumnCount;
33
+
34
+ if (table && map) {
35
+ var rect = getSelectionRect(selection);
36
+
37
+ if (rect) {
38
+ totalCells = map.cellsInRect(rect).length;
39
+ horizontalCells = rect.right - rect.left;
40
+ verticalCells = rect.bottom - rect.top;
41
+ }
42
+ }
43
+
44
+ return {
45
+ totalRowCount: totalRowCount,
46
+ totalColumnCount: totalColumnCount,
47
+ horizontalCells: horizontalCells,
48
+ verticalCells: verticalCells,
49
+ totalCells: totalCells
50
+ };
51
+ }
@@ -7,10 +7,11 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
7
7
  import { fixTablesKey } from '../pm-plugins/plugin-key';
8
8
  import { TableMap, TableProblemTypes } from '../table-map';
9
9
  import { removeColSpan } from './colspan';
10
- import { tableNodeTypes } from './table-node-types'; // Helper for iterating through the nodes in a document that changed
10
+ import { tableNodeTypes } from './table-node-types';
11
+
12
+ // Helper for iterating through the nodes in a document that changed
11
13
  // compared to the given previous document. Useful for avoiding
12
14
  // duplicate work on each transaction.
13
-
14
15
  function changedDescendants(old, cur, offsetStart, f) {
15
16
  var offset = offsetStart;
16
17
  var oldSize = old.childCount;
@@ -46,12 +47,12 @@ function changedDescendants(old, cur, offsetStart, f) {
46
47
  // document.
47
48
 
48
49
 
49
- export function fixTables(state, oldState) {
50
+ export function fixTables(state, oldState, reportFixedTable) {
50
51
  var tr;
51
52
 
52
53
  var check = function check(node, pos) {
53
54
  if (node.type.spec.tableRole === 'table') {
54
- tr = fixTable(state, node, pos, tr);
55
+ tr = fixTable(state, node, pos, tr, reportFixedTable);
55
56
  }
56
57
  };
57
58
 
@@ -66,7 +67,7 @@ export function fixTables(state, oldState) {
66
67
  // Fix the given table, if necessary. Will append to the transaction
67
68
  // it was given, if non-null, or create a new one if necessary.
68
69
 
69
- export function fixTable(state, table, tablePos, transaction) {
70
+ export function fixTable(state, table, tablePos, transaction, reportFixedTable) {
70
71
  var tr = transaction;
71
72
  var map = TableMap.get(table);
72
73
 
@@ -89,6 +90,14 @@ export function fixTable(state, table, tablePos, transaction) {
89
90
  for (var _i = 0; _i < map.problems.length; _i++) {
90
91
  var prob = map.problems[_i];
91
92
 
93
+ if (reportFixedTable) {
94
+ reportFixedTable({
95
+ state: state,
96
+ tr: tr,
97
+ reason: prob.type || 'unknown'
98
+ });
99
+ }
100
+
92
101
  if (prob.type === TableProblemTypes.COLLISION) {
93
102
  var collision = prob;
94
103
  var cell = table.nodeAt(prob.pos);
@@ -0,0 +1,18 @@
1
+ import { Fragment, Slice } from 'prosemirror-model';
2
+ import { TextSelection } from 'prosemirror-state';
3
+ import { findTable } from './find';
4
+ import { isTableSelected } from './is-selected';
5
+ export var replaceSelectedTable = function replaceSelectedTable(state, content) {
6
+ if (isTableSelected(state.selection)) {
7
+ var table = findTable(state.selection);
8
+
9
+ if (table) {
10
+ var slice = typeof content === 'string' ? new Slice(Fragment.from(state.schema.text(content)), 0, 0) : content;
11
+ var tr = state.tr.replace(table.pos, table.pos + table.node.nodeSize, slice);
12
+ tr.setSelection(TextSelection.create(tr.doc, table.pos + slice.size + 1));
13
+ return tr;
14
+ }
15
+ }
16
+
17
+ return state.tr;
18
+ };
package/dist/esm/utils.js CHANGED
@@ -37,4 +37,6 @@ export { tableNodeTypes } from './utils/table-node-types';
37
37
  export { isInTable, inSameTable } from './utils/tables';
38
38
  export { toggleHeader } from './utils/toggle-header';
39
39
  export { convertArrayOfRowsToTableNode, convertTableNodeToArrayOfRows } from './utils/reorder-utils';
40
- export { handlePaste } from './utils/handle-paste';
40
+ export { handlePaste } from './utils/handle-paste';
41
+ export { replaceSelectedTable } from './utils/replace-table';
42
+ export { getSelectedTableInfo, getSelectedCellInfo } from './utils/analytics-helpers';
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-tables",
3
- "version": "2.1.5",
3
+ "version": "2.2.0",
4
4
  "sideEffects": false
5
5
  }
@@ -1,6 +1,8 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
+ import { ReportFixedTable } from '../utils/fix-tables';
2
3
  declare type PluginState = number | null;
3
- export declare function tableEditing({ allowTableNodeSelection }?: {
4
- allowTableNodeSelection?: boolean | undefined;
4
+ export declare function tableEditing({ allowTableNodeSelection, reportFixedTable, }?: {
5
+ allowTableNodeSelection?: boolean;
6
+ reportFixedTable?: ReportFixedTable;
5
7
  }): Plugin<PluginState>;
6
8
  export {};
@@ -0,0 +1,15 @@
1
+ import { Selection } from 'prosemirror-state';
2
+ import { TableMap } from '../table-map';
3
+ export declare function getSelectedTableInfo(selection: Selection): {
4
+ table: import("prosemirror-utils").ContentNodeWithPos | undefined;
5
+ map: TableMap | undefined;
6
+ totalRowCount: number;
7
+ totalColumnCount: number;
8
+ };
9
+ export declare function getSelectedCellInfo(selection: Selection): {
10
+ totalRowCount: number;
11
+ totalColumnCount: number;
12
+ horizontalCells: number;
13
+ verticalCells: number;
14
+ totalCells: number;
15
+ };
@@ -1,4 +1,9 @@
1
1
  import { Node } from 'prosemirror-model';
2
2
  import { EditorState, Transaction } from 'prosemirror-state';
3
- export declare function fixTables(state: EditorState, oldState?: EditorState): Transaction | undefined;
4
- export declare function fixTable(state: EditorState, table: Node, tablePos: number, transaction?: Transaction): Transaction | undefined;
3
+ export declare type ReportFixedTable = ({ state, tr, reason, }: {
4
+ state: EditorState;
5
+ tr: Transaction;
6
+ reason: string;
7
+ }) => void;
8
+ export declare function fixTables(state: EditorState, oldState?: EditorState, reportFixedTable?: ReportFixedTable): Transaction | undefined;
9
+ export declare function fixTable(state: EditorState, table: Node, tablePos: number, transaction?: Transaction, reportFixedTable?: ReportFixedTable): Transaction | undefined;
@@ -0,0 +1,3 @@
1
+ import { Slice } from 'prosemirror-model';
2
+ import { EditorState, Transaction } from 'prosemirror-state';
3
+ export declare const replaceSelectedTable: (state: EditorState, content: string | Slice) => Transaction;
@@ -39,3 +39,5 @@ export { isInTable, inSameTable } from './utils/tables';
39
39
  export { toggleHeader } from './utils/toggle-header';
40
40
  export { convertArrayOfRowsToTableNode, convertTableNodeToArrayOfRows, } from './utils/reorder-utils';
41
41
  export { handlePaste } from './utils/handle-paste';
42
+ export { replaceSelectedTable } from './utils/replace-table';
43
+ export { getSelectedTableInfo, getSelectedCellInfo, } from './utils/analytics-helpers';
@@ -0,0 +1,10 @@
1
+ import { Node as PMNode } from 'prosemirror-model';
2
+ import { Selection } from 'prosemirror-state';
3
+ import { Mapping } from 'prosemirror-transform';
4
+ export declare class CellBookmark {
5
+ readonly anchor: number;
6
+ readonly head: number;
7
+ constructor(anchor: number, head: number);
8
+ map(mapping: Mapping): CellBookmark;
9
+ resolve(doc: PMNode): Selection;
10
+ }
@@ -0,0 +1,25 @@
1
+ import { Node as PMNode, ResolvedPos, Slice } from 'prosemirror-model';
2
+ import { Selection, Transaction } from 'prosemirror-state';
3
+ import { Mapping } from 'prosemirror-transform';
4
+ import { CellBookmark } from './cell-bookmark';
5
+ import { SerializedCellSelection } from './types';
6
+ export declare class CellSelection extends Selection {
7
+ readonly $anchorCell: ResolvedPos;
8
+ readonly $headCell: ResolvedPos;
9
+ readonly visible: boolean;
10
+ constructor($anchorCell: ResolvedPos, $headCell?: ResolvedPos);
11
+ map(doc: PMNode, mapping: Mapping): Selection;
12
+ content(): Slice;
13
+ replace(tr: Transaction, content?: Slice<any>): void;
14
+ replaceWith(tr: Transaction, node: PMNode): void;
15
+ forEachCell(f: (node: PMNode, pos: number) => void): void;
16
+ isColSelection(): boolean;
17
+ static colSelection($anchorCell: ResolvedPos, $headCell?: ResolvedPos): CellSelection;
18
+ isRowSelection(): boolean;
19
+ eq(other: CellSelection): boolean;
20
+ static rowSelection($anchorCell: ResolvedPos, $headCell?: ResolvedPos): CellSelection;
21
+ toJSON(): SerializedCellSelection;
22
+ static fromJSON(doc: PMNode, json: SerializedCellSelection): CellSelection;
23
+ static create(doc: PMNode, anchorCell: number, headCell?: number): CellSelection;
24
+ getBookmark(): CellBookmark;
25
+ }