@atlaskit/editor-plugin-table 7.2.1 → 7.2.3

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 (124) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cjs/nodeviews/TableComponent.js +11 -7
  3. package/dist/cjs/pm-plugins/drag-and-drop/commands-with-analytics.js +3 -19
  4. package/dist/cjs/ui/TableFloatingColumnControls/ColumnControls/index.js +11 -25
  5. package/dist/cjs/ui/TableFloatingColumnControls/ColumnDropTargets/index.js +13 -3
  6. package/dist/cjs/ui/TableFloatingColumnControls/index.js +7 -3
  7. package/dist/cjs/utils/drag-menu.js +37 -23
  8. package/dist/cjs/utils/merged-cells.js +66 -1
  9. package/dist/es2019/nodeviews/TableComponent.js +9 -5
  10. package/dist/es2019/pm-plugins/drag-and-drop/commands-with-analytics.js +4 -20
  11. package/dist/es2019/ui/TableFloatingColumnControls/ColumnControls/index.js +7 -21
  12. package/dist/es2019/ui/TableFloatingColumnControls/ColumnDropTargets/index.js +10 -2
  13. package/dist/es2019/ui/TableFloatingColumnControls/index.js +7 -3
  14. package/dist/es2019/utils/drag-menu.js +38 -14
  15. package/dist/es2019/utils/merged-cells.js +73 -0
  16. package/dist/esm/nodeviews/TableComponent.js +11 -7
  17. package/dist/esm/pm-plugins/drag-and-drop/commands-with-analytics.js +4 -20
  18. package/dist/esm/ui/TableFloatingColumnControls/ColumnControls/index.js +7 -21
  19. package/dist/esm/ui/TableFloatingColumnControls/ColumnDropTargets/index.js +10 -2
  20. package/dist/esm/ui/TableFloatingColumnControls/index.js +7 -3
  21. package/dist/esm/utils/drag-menu.js +36 -22
  22. package/dist/esm/utils/merged-cells.js +65 -0
  23. package/dist/types/ui/TableFloatingColumnControls/ColumnControls/index.d.ts +2 -1
  24. package/dist/types/ui/TableFloatingColumnControls/ColumnDropTargets/index.d.ts +2 -0
  25. package/dist/types/ui/TableFloatingColumnControls/index.d.ts +1 -0
  26. package/dist/types/utils/drag-menu.d.ts +4 -1
  27. package/dist/types/utils/merged-cells.d.ts +2 -0
  28. package/dist/types-ts4.5/ui/TableFloatingColumnControls/ColumnControls/index.d.ts +2 -1
  29. package/dist/types-ts4.5/ui/TableFloatingColumnControls/ColumnDropTargets/index.d.ts +2 -0
  30. package/dist/types-ts4.5/ui/TableFloatingColumnControls/index.d.ts +1 -0
  31. package/dist/types-ts4.5/utils/drag-menu.d.ts +4 -1
  32. package/dist/types-ts4.5/utils/merged-cells.d.ts +2 -0
  33. package/package.json +9 -16
  34. package/src/nodeviews/TableComponent.tsx +1 -0
  35. package/src/pm-plugins/drag-and-drop/commands-with-analytics.ts +11 -32
  36. package/src/ui/TableFloatingColumnControls/ColumnControls/index.tsx +6 -27
  37. package/src/ui/TableFloatingColumnControls/ColumnDropTargets/index.tsx +12 -1
  38. package/src/ui/TableFloatingColumnControls/index.tsx +5 -0
  39. package/src/utils/drag-menu.ts +94 -20
  40. package/src/utils/merged-cells.ts +78 -0
  41. package/tsconfig.dev.json +0 -69
  42. package/tsconfig.json +2 -871
  43. package/examples/99-testing.tsx +0 -140
  44. package/examples/config.jsonc +0 -14
  45. package/src/__tests__/unit/analytics.ts +0 -888
  46. package/src/__tests__/unit/collab.ts +0 -93
  47. package/src/__tests__/unit/commands/go-to-next-cell.ts +0 -173
  48. package/src/__tests__/unit/commands/insert.ts +0 -137
  49. package/src/__tests__/unit/commands/misc.ts +0 -185
  50. package/src/__tests__/unit/commands/sort.ts +0 -128
  51. package/src/__tests__/unit/commands.ts +0 -745
  52. package/src/__tests__/unit/copy-button.ts +0 -22
  53. package/src/__tests__/unit/copy-paste.ts +0 -677
  54. package/src/__tests__/unit/event-handlers/index.ts +0 -125
  55. package/src/__tests__/unit/event-handlers.ts +0 -296
  56. package/src/__tests__/unit/fix-tables.ts +0 -164
  57. package/src/__tests__/unit/get-toolbar-config.ts +0 -127
  58. package/src/__tests__/unit/handlers.ts +0 -98
  59. package/src/__tests__/unit/hover-selection.ts +0 -230
  60. package/src/__tests__/unit/index-with-fake-timers.ts +0 -111
  61. package/src/__tests__/unit/index.ts +0 -912
  62. package/src/__tests__/unit/layout.ts +0 -146
  63. package/src/__tests__/unit/nodeviews/OverflowShadowsObserver.ts +0 -162
  64. package/src/__tests__/unit/nodeviews/TableComponent.tsx +0 -280
  65. package/src/__tests__/unit/nodeviews/TableContainer.tsx +0 -525
  66. package/src/__tests__/unit/nodeviews/cell.ts +0 -132
  67. package/src/__tests__/unit/nodeviews/table.ts +0 -129
  68. package/src/__tests__/unit/pm-plugins/analytics.ts +0 -327
  69. package/src/__tests__/unit/pm-plugins/decorations/column-controls.ts +0 -94
  70. package/src/__tests__/unit/pm-plugins/decorations/column-resizing.ts +0 -176
  71. package/src/__tests__/unit/pm-plugins/decorations/plugin.ts +0 -211
  72. package/src/__tests__/unit/pm-plugins/main.ts +0 -214
  73. package/src/__tests__/unit/pm-plugins/safari-delete-composition-text-issue-workaround.ts +0 -101
  74. package/src/__tests__/unit/pm-plugins/sticky-headers/tableRow.tsx +0 -562
  75. package/src/__tests__/unit/pm-plugins/table-local-id.ts +0 -507
  76. package/src/__tests__/unit/pm-plugins/table-resizing/colgroup.ts +0 -269
  77. package/src/__tests__/unit/pm-plugins/table-resizing/event-handlers.ts +0 -192
  78. package/src/__tests__/unit/pm-plugins/table-resizing/utils/resize-state.ts +0 -33
  79. package/src/__tests__/unit/pm-plugins/table-width.ts +0 -292
  80. package/src/__tests__/unit/sort-column.ts +0 -399
  81. package/src/__tests__/unit/toolbar.ts +0 -512
  82. package/src/__tests__/unit/transforms/delete-columns.ts +0 -597
  83. package/src/__tests__/unit/transforms/delete-rows.ts +0 -620
  84. package/src/__tests__/unit/transforms/merging.ts +0 -392
  85. package/src/__tests__/unit/ui/ContextualMenu.tsx +0 -71
  86. package/src/__tests__/unit/ui/CornerControls.tsx +0 -99
  87. package/src/__tests__/unit/ui/DeleteButton.tsx +0 -38
  88. package/src/__tests__/unit/ui/FixedButton.tsx +0 -217
  89. package/src/__tests__/unit/ui/FloatingContextualButton.tsx +0 -123
  90. package/src/__tests__/unit/ui/FloatingContextualMenu.tsx +0 -68
  91. package/src/__tests__/unit/ui/FloatingDeleteButton.tsx +0 -178
  92. package/src/__tests__/unit/ui/FloatingDragMenu.tsx +0 -511
  93. package/src/__tests__/unit/ui/FloatingInsertButton.tsx +0 -322
  94. package/src/__tests__/unit/ui/NumberColumn.tsx +0 -146
  95. package/src/__tests__/unit/ui/RowControls.tsx +0 -294
  96. package/src/__tests__/unit/ui/RowDragControls.tsx +0 -129
  97. package/src/__tests__/unit/ui/TableFloatingColumnControls.tsx +0 -189
  98. package/src/__tests__/unit/ui/TableFloatingControls.tsx +0 -118
  99. package/src/__tests__/unit/undo-redo.ts +0 -220
  100. package/src/__tests__/unit/utils/analytics.ts +0 -98
  101. package/src/__tests__/unit/utils/collapse.ts +0 -57
  102. package/src/__tests__/unit/utils/column-controls.ts +0 -205
  103. package/src/__tests__/unit/utils/dom.ts +0 -180
  104. package/src/__tests__/unit/utils/merged-cells.ts +0 -156
  105. package/src/__tests__/unit/utils/nodes.ts +0 -79
  106. package/src/__tests__/unit/utils/row-controls.ts +0 -195
  107. package/src/__tests__/unit/utils/table.ts +0 -96
  108. package/src/__tests__/unit/utils.ts +0 -670
  109. package/src/__tests__/visual-regression/__fixtures__/sticky-header-with-horizontal-scroll.json +0 -5228
  110. package/src/__tests__/visual-regression/__fixtures__/table-with-100-numbered-list-items.json +0 -20272
  111. package/src/__tests__/visual-regression/__image_snapshots__/cell-options-menu-ts-table-cell-options-menu-delete-column-menu-item-should-remove-the-table-column-on-click-1-snap.png +0 -3
  112. package/src/__tests__/visual-regression/__image_snapshots__/cell-options-menu-ts-table-cell-options-menu-delete-column-menu-item-visual-hints-should-be-added-to-the-table-column-on-hover-1-snap.png +0 -3
  113. package/src/__tests__/visual-regression/__image_snapshots__/cell-options-menu-ts-table-cell-options-menu-delete-row-menu-item-should-remove-the-table-row-on-click-1-snap.png +0 -3
  114. package/src/__tests__/visual-regression/__image_snapshots__/cell-options-menu-ts-table-cell-options-menu-delete-row-menu-item-visual-hints-should-be-added-to-the-table-row-on-hover-1-snap.png +0 -3
  115. package/src/__tests__/visual-regression/__image_snapshots__/copy-button-ts-floating-toolbar-copy-button-table-target-node-displays-blue-border-when-copy-button-is-hovered-1-snap.png +0 -3
  116. package/src/__tests__/visual-regression/__image_snapshots__/index-ts-snapshot-test-table-numbered-list-should-not-overflow-table-cell-when-there-are-more-than-100-ordered-list-items-1-snap.png +0 -3
  117. package/src/__tests__/visual-regression/__image_snapshots__/index-ts-snapshot-test-table-numbered-list-should-not-overflow-table-cell-when-there-are-more-than-100-ordered-list-items-2-snap.png +0 -3
  118. package/src/__tests__/visual-regression/__image_snapshots__/index-ts-snapshot-test-table-numbered-list-should-not-overflow-table-cell-when-there-are-more-than-100-ordered-list-items-3-snap.png +0 -3
  119. package/src/__tests__/visual-regression/__image_snapshots__/sticky-header-ts-snapshot-test-table-sticky-header-should-align-with-table-cell-when-active-1-snap.png +0 -3
  120. package/src/__tests__/visual-regression/__image_snapshots__/sticky-header-ts-snapshot-test-table-sticky-header-should-align-with-table-cell-when-active-2-snap.png +0 -3
  121. package/src/__tests__/visual-regression/cell-options-menu.ts +0 -101
  122. package/src/__tests__/visual-regression/copy-button.ts +0 -181
  123. package/src/__tests__/visual-regression/index.ts +0 -62
  124. package/src/__tests__/visual-regression/sticky-header.ts +0 -61
@@ -1,912 +0,0 @@
1
- import { uuid } from '@atlaskit/adf-schema';
2
- import type {
3
- DocBuilder,
4
- GetEditorContainerWidth,
5
- } from '@atlaskit/editor-common/types';
6
- import { setNodeSelection } from '@atlaskit/editor-common/utils';
7
- import { analyticsPlugin } from '@atlaskit/editor-plugin-analytics';
8
- import { contentInsertionPlugin } from '@atlaskit/editor-plugin-content-insertion';
9
- import { featureFlagsPlugin } from '@atlaskit/editor-plugin-feature-flags';
10
- import { gridPlugin } from '@atlaskit/editor-plugin-grid';
11
- import { guidelinePlugin } from '@atlaskit/editor-plugin-guideline';
12
- import { selectionPlugin } from '@atlaskit/editor-plugin-selection';
13
- import { widthPlugin } from '@atlaskit/editor-plugin-width';
14
- import { TextSelection } from '@atlaskit/editor-prosemirror/state';
15
- import { uuid as tablesUuid } from '@atlaskit/editor-tables';
16
- import type { CellSelection } from '@atlaskit/editor-tables/cell-selection';
17
- import { TableMap } from '@atlaskit/editor-tables/table-map';
18
- import {
19
- selectColumn,
20
- selectRow,
21
- selectTable,
22
- } from '@atlaskit/editor-tables/utils';
23
- // eslint-disable-next-line import/no-extraneous-dependencies
24
- import type { LightEditorPlugin } from '@atlaskit/editor-test-helpers/create-prosemirror-editor';
25
- // eslint-disable-next-line import/no-extraneous-dependencies -- Removed import for fixing circular dependencies
26
- import {
27
- createProsemirrorEditorFactory,
28
- Preset,
29
- } from '@atlaskit/editor-test-helpers/create-prosemirror-editor';
30
- // eslint-disable-next-line import/no-extraneous-dependencies -- Removed import for fixing circular dependencies
31
- import {
32
- doc,
33
- media,
34
- mediaGroup,
35
- p,
36
- table,
37
- td,
38
- tdCursor,
39
- tdEmpty,
40
- th,
41
- thCursor,
42
- thEmpty,
43
- tr,
44
- } from '@atlaskit/editor-test-helpers/doc-builder';
45
- // eslint-disable-next-line import/no-extraneous-dependencies -- Removed import for fixing circular dependencies
46
- import sendKeyToPm from '@atlaskit/editor-test-helpers/send-key-to-pm';
47
-
48
- import {
49
- createTable,
50
- insertColumn,
51
- insertRow,
52
- setEditorFocus,
53
- toggleHeaderColumn,
54
- toggleHeaderRow,
55
- } from '../../commands';
56
- import tablePlugin from '../../plugin';
57
- import { getPluginState } from '../../pm-plugins/plugin-factory';
58
- import { pluginKey } from '../../pm-plugins/plugin-key';
59
- import type { PluginConfig } from '../../types';
60
- import {
61
- checkIfHeaderColumnEnabled,
62
- checkIfHeaderRowEnabled,
63
- checkIfNumberColumnEnabled,
64
- } from '../../utils';
65
-
66
- const TABLE_LOCAL_ID = 'test-table-local-id';
67
-
68
- describe('table plugin', () => {
69
- const getEditorContainerWidth: GetEditorContainerWidth = () => {
70
- return {
71
- width: 500,
72
- };
73
- };
74
- beforeAll(() => {
75
- uuid.setStatic(TABLE_LOCAL_ID);
76
- tablesUuid.setStatic(TABLE_LOCAL_ID);
77
- });
78
-
79
- afterAll(() => {
80
- uuid.setStatic(false);
81
- tablesUuid.setStatic(false);
82
- });
83
- const createEditor = createProsemirrorEditorFactory();
84
-
85
- const editor = (doc: DocBuilder) => {
86
- const tableOptions = {
87
- allowNumberColumn: true,
88
- allowHeaderRow: true,
89
- allowHeaderColumn: true,
90
- permittedLayouts: 'all',
91
- } as PluginConfig;
92
-
93
- const preset = new Preset<LightEditorPlugin>()
94
- .add([featureFlagsPlugin, {}])
95
- .add([analyticsPlugin, {}])
96
- .add(widthPlugin)
97
- .add(guidelinePlugin)
98
- .add(gridPlugin)
99
- .add(contentInsertionPlugin)
100
- .add(selectionPlugin)
101
- .add([tablePlugin, { tableOptions }]);
102
-
103
- return createEditor({
104
- doc,
105
- preset,
106
- pluginKey,
107
- });
108
- };
109
-
110
- describe('createTable()', () => {
111
- describe('when the cursor is outside the table', () => {
112
- it('it should create a new table and return true', () => {
113
- const { editorView } = editor(doc(p('{<>}')));
114
- expect(createTable()(editorView.state, editorView.dispatch)).toEqual(
115
- true,
116
- );
117
- const tableNode = table({ localId: TABLE_LOCAL_ID })(
118
- tr(thEmpty, thEmpty, thEmpty),
119
- tr(tdEmpty, tdEmpty, tdEmpty),
120
- tr(tdEmpty, tdEmpty, tdEmpty),
121
- );
122
- expect(editorView.state.doc).toEqualDocument(doc(tableNode));
123
- });
124
- });
125
- });
126
-
127
- describe('insertColumn(number)', () => {
128
- it('should not insert a column if selection is not in table', () => {
129
- const { editorView } = editor(doc(p('{<>}')));
130
-
131
- expect(
132
- insertColumn(getEditorContainerWidth)(2)(
133
- editorView.state,
134
- editorView.dispatch,
135
- ),
136
- ).toBe(false);
137
- });
138
-
139
- describe('when table has 2 columns', () => {
140
- describe('when it called with 0', () => {
141
- it("it should prepend a new column and move cursor inside it's first cell", () => {
142
- const { editorView } = editor(
143
- doc(p('text'), table()(tr(td({})(p('c1')), td({})(p('c2{<>}'))))),
144
- );
145
-
146
- insertColumn(getEditorContainerWidth)(0)(
147
- editorView.state,
148
- editorView.dispatch,
149
- );
150
- expect(editorView.state.doc).toEqualDocument(
151
- doc(
152
- p('text'),
153
- table({ localId: TABLE_LOCAL_ID })(
154
- tr(tdCursor, td({})(p('c1')), td({})(p('c2'))),
155
- ),
156
- ),
157
- );
158
- expect(editorView.state.selection.$from.pos).toEqual(10);
159
- });
160
- });
161
-
162
- describe('when it called with 1', () => {
163
- it("it should insert a new column in the middle and move cursor inside it's first cell", () => {
164
- const { editorView } = editor(
165
- doc(p('text'), table()(tr(td({})(p('c1{<>}')), td({})(p('c2'))))),
166
- );
167
-
168
- insertColumn(getEditorContainerWidth)(1)(
169
- editorView.state,
170
- editorView.dispatch,
171
- );
172
- expect(editorView.state.doc).toEqualDocument(
173
- doc(
174
- p('text'),
175
- table({ localId: TABLE_LOCAL_ID })(
176
- tr(td({})(p('c1')), tdCursor, td({})(p('c2'))),
177
- ),
178
- ),
179
- );
180
- expect(editorView.state.selection.$from.pos).toEqual(16);
181
- });
182
- });
183
-
184
- describe('when it called with 2', () => {
185
- it("it should append a new column and move cursor inside it's first cell", () => {
186
- const { editorView } = editor(
187
- doc(p('text'), table()(tr(td({})(p('c1{<>}')), td({})(p('c2'))))),
188
- );
189
-
190
- insertColumn(getEditorContainerWidth)(2)(
191
- editorView.state,
192
- editorView.dispatch,
193
- );
194
- expect(editorView.state.doc).toEqualDocument(
195
- doc(
196
- p('text'),
197
- table({ localId: TABLE_LOCAL_ID })(
198
- tr(td({})(p('c1')), td({})(p('c2')), tdCursor),
199
- ),
200
- ),
201
- );
202
- expect(editorView.state.selection.$from.pos).toEqual(22);
203
- });
204
- });
205
- });
206
- });
207
-
208
- describe('insertRow(number)', () => {
209
- const MOVE_SELECTION_TO_THE_NEW_LINE = true;
210
- const KEEP_THE_CURRENT_SELECTION = false;
211
-
212
- describe('when the cursor should not move', () => {
213
- it('should keep the selection', () => {
214
- const { editorView } = editor(
215
- doc(
216
- p('text'),
217
- table()(tr(td({})(p('row1'))), tr(td({})(p('row2{<>}')))),
218
- ),
219
- );
220
-
221
- insertRow(0, KEEP_THE_CURRENT_SELECTION)(
222
- editorView.state,
223
- editorView.dispatch,
224
- );
225
-
226
- expect(editorView.state).toEqualDocumentAndSelection(
227
- doc(
228
- p('text'),
229
- table({ localId: TABLE_LOCAL_ID })(
230
- tr(tdEmpty),
231
- tr(td({})(p('row1'))),
232
- tr(td({})(p('row2{<>}'))),
233
- ),
234
- ),
235
- );
236
- });
237
- });
238
-
239
- describe('when table has 2 rows', () => {
240
- describe('when it called with 0', () => {
241
- it("it should prepend a new row and move cursor inside it's first cell", () => {
242
- const { editorView } = editor(
243
- doc(
244
- p('text'),
245
- table()(tr(td({})(p('row1'))), tr(td({})(p('row2{<>}')))),
246
- ),
247
- );
248
-
249
- insertRow(0, MOVE_SELECTION_TO_THE_NEW_LINE)(
250
- editorView.state,
251
- editorView.dispatch,
252
- );
253
- expect(editorView.state.doc).toEqualDocument(
254
- doc(
255
- p('text'),
256
- table({ localId: TABLE_LOCAL_ID })(
257
- tr(tdCursor),
258
- tr(td({})(p('row1'))),
259
- tr(td({})(p('row2'))),
260
- ),
261
- ),
262
- );
263
- expect(editorView.state.selection.$from.pos).toEqual(10);
264
- });
265
- });
266
-
267
- describe('when it called with 1', () => {
268
- it("it should insert a new row in the middle and move cursor inside it's first cell", () => {
269
- const { editorView } = editor(
270
- doc(
271
- p('text'),
272
- table()(tr(td({})(p('row1{<>}'))), tr(td({})(p('row2')))),
273
- ),
274
- );
275
-
276
- insertRow(1, MOVE_SELECTION_TO_THE_NEW_LINE)(
277
- editorView.state,
278
- editorView.dispatch,
279
- );
280
- expect(editorView.state.doc).toEqualDocument(
281
- doc(
282
- p('text'),
283
- table({ localId: TABLE_LOCAL_ID })(
284
- tr(td({})(p('row1'))),
285
- tr(tdCursor),
286
- tr(td({})(p('row2'))),
287
- ),
288
- ),
289
- );
290
- expect(editorView.state.selection.$from.pos).toEqual(20);
291
- });
292
- });
293
- });
294
-
295
- describe('when table has 2 row', () => {
296
- describe('when it called with 2', () => {
297
- it("it should append a new row and move cursor inside it's first cell", () => {
298
- const { editorView } = editor(
299
- doc(
300
- p('text'),
301
- table()(tr(td({})(p('row1{<>}'))), tr(td({})(p('row2')))),
302
- ),
303
- );
304
-
305
- insertRow(2, MOVE_SELECTION_TO_THE_NEW_LINE)(
306
- editorView.state,
307
- editorView.dispatch,
308
- );
309
- expect(editorView.state.doc).toEqualDocument(
310
- doc(
311
- p('text'),
312
- table({ localId: TABLE_LOCAL_ID })(
313
- tr(td({})(p('row1'))),
314
- tr(td({})(p('row2'))),
315
- tr(tdCursor),
316
- ),
317
- ),
318
- );
319
- expect(editorView.state.selection.$from.pos).toEqual(30);
320
- });
321
- });
322
- });
323
-
324
- describe('when adding a new row', () => {
325
- describe('when table has merged columns in rows', () => {
326
- it('copies the structure', () => {
327
- const { editorView } = editor(
328
- doc(
329
- table()(
330
- tr(td({})(p('row1')), td()(p())),
331
- tr(td({ colspan: 2, background: '#e6fcff' })(p('row2{<>}'))),
332
- ),
333
- ),
334
- );
335
-
336
- insertRow(2, MOVE_SELECTION_TO_THE_NEW_LINE)(
337
- editorView.state,
338
- editorView.dispatch,
339
- );
340
-
341
- expect(editorView.state.doc).toEqualDocument(
342
- doc(
343
- table({ localId: TABLE_LOCAL_ID })(
344
- tr(td({})(p('row1')), td()(p())),
345
- tr(td({ colspan: 2, background: '#e6fcff' })(p('row2'))),
346
- tr(td({ colspan: 2, background: '#e6fcff' })(p('{<>}'))),
347
- ),
348
- ),
349
- );
350
- });
351
- });
352
-
353
- it('copies the structure from a tableCell', () => {
354
- const { editorView } = editor(
355
- doc(
356
- table()(
357
- tr(th({})(p()), th({})(p())),
358
- tr(td({ background: '#e6fcff' })(p('row1')), td()(p('{<>}'))),
359
- tr(td({ colspan: 2, background: '#e6fcff' })(p('row2'))),
360
- ),
361
- ),
362
- );
363
-
364
- insertRow(2, MOVE_SELECTION_TO_THE_NEW_LINE)(
365
- editorView.state,
366
- editorView.dispatch,
367
- );
368
-
369
- expect(editorView.state.doc).toEqualDocument(
370
- doc(
371
- table({ localId: TABLE_LOCAL_ID })(
372
- tr(th({})(p()), th({})(p())),
373
- tr(td({ background: '#e6fcff' })(p('row1')), td()(p())),
374
- tr(td({ background: '#e6fcff' })(p('{<>}')), td()(p())),
375
- tr(td({ colspan: 2, background: '#e6fcff' })(p('row2'))),
376
- ),
377
- ),
378
- );
379
- });
380
-
381
- it('should not insert a row if selection is not in table', () => {
382
- const { editorView } = editor(doc(p('{<>}')));
383
-
384
- expect(
385
- insertRow(2, MOVE_SELECTION_TO_THE_NEW_LINE)(
386
- editorView.state,
387
- editorView.dispatch,
388
- ),
389
- ).toBe(false);
390
- });
391
-
392
- it('copies the structure from a tableHeader', () => {
393
- const { editorView } = editor(
394
- doc(
395
- table()(
396
- tr(th({})(p('row1')), th()(p()), th()(p('{<>}'))),
397
- tr(
398
- th({ colspan: 2, background: '#e6fcff' })(p('row2')),
399
- td()(p()),
400
- ),
401
- ),
402
- ),
403
- );
404
-
405
- insertRow(2, MOVE_SELECTION_TO_THE_NEW_LINE)(
406
- editorView.state,
407
- editorView.dispatch,
408
- );
409
-
410
- expect(editorView.state.doc).toEqualDocument(
411
- doc(
412
- table({ localId: TABLE_LOCAL_ID })(
413
- tr(th({})(p('row1')), th()(p()), th()(p())),
414
- tr(
415
- th({ colspan: 2, background: '#e6fcff' })(p('row2')),
416
- td()(p()),
417
- ),
418
- tr(th({ colspan: 2, background: '#e6fcff' })(p()), td()(p())),
419
- ),
420
- ),
421
- );
422
- });
423
- });
424
- });
425
-
426
- describe('selectColumn(number)', () => {
427
- describe('when table has 3 columns', () => {
428
- [0, 1, 2].forEach((column) => {
429
- describe(`when called with ${column}`, () => {
430
- it(`it should select ${column} column`, () => {
431
- const { editorView } = editor(
432
- doc(p('text'), table()(tr(tdCursor, tdEmpty, tdEmpty))),
433
- );
434
-
435
- editorView.dispatch(selectColumn(column)(editorView.state.tr));
436
- const selection = editorView.state
437
- .selection as any as CellSelection;
438
- const tableNode = selection.$anchorCell.node(-1);
439
- const map = TableMap.get(tableNode);
440
- const start = selection.$anchorCell.start(-1);
441
- const anchor = map.colCount(selection.$anchorCell.pos - start);
442
- const head = map.colCount(selection.$headCell.pos - start);
443
- expect(anchor).toEqual(column);
444
- expect(head).toEqual(column);
445
- expect(selection.isColSelection()).toEqual(true);
446
- });
447
- });
448
- });
449
- });
450
- });
451
-
452
- describe('selectRow(number)', () => {
453
- describe('when table has 3 rows', () => {
454
- [0, 1, 2].forEach((row) => {
455
- describe(`when called with ${row}`, () => {
456
- it(`it should select ${row} row`, () => {
457
- const { editorView } = editor(
458
- doc(p('text'), table()(tr(tdCursor), tr(tdEmpty), tr(tdEmpty))),
459
- );
460
-
461
- editorView.dispatch(selectRow(row)(editorView.state.tr));
462
- const selection = editorView.state
463
- .selection as any as CellSelection;
464
- const anchor = selection.$anchorCell.index(-1);
465
- const head = selection.$headCell.index(-1);
466
- expect(anchor).toEqual(row);
467
- expect(head).toEqual(row);
468
- expect(selection.isRowSelection()).toEqual(true);
469
- });
470
- });
471
- });
472
- });
473
- });
474
-
475
- describe('selectTable()', () => {
476
- it('it should select the whole table', () => {
477
- const { editorView } = editor(
478
- doc(p('text'), table()(tr(tdCursor), tr(tdEmpty), tr(tdEmpty))),
479
- );
480
-
481
- editorView.dispatch(selectTable(editorView.state.tr));
482
- const selection = editorView.state.selection as any as CellSelection;
483
- expect(selection.isRowSelection()).toEqual(true);
484
- expect(selection.isColSelection()).toEqual(true);
485
- });
486
- });
487
-
488
- describe('toggleHeaderRow()', () => {
489
- describe('when the table has rowspan and colspan', () => {
490
- const buildTableDoc = (hasHeaderRow: boolean) => {
491
- const cell = hasHeaderRow ? th : td;
492
- return doc(
493
- p('text'),
494
- table({ localId: TABLE_LOCAL_ID })(
495
- tr(cell({ colspan: 2 })(p('')), cell({ rowspan: 2 })(p(''))),
496
- tr(tdEmpty, tdEmpty),
497
- tr(tdEmpty, tdEmpty, tdEmpty),
498
- ),
499
- );
500
- };
501
-
502
- it('should add header cells on the first line', () => {
503
- const { editorView } = editor(buildTableDoc(false));
504
-
505
- toggleHeaderRow(editorView.state, editorView.dispatch);
506
-
507
- expect(editorView.state.doc).toEqualDocument(buildTableDoc(true));
508
- });
509
-
510
- it('should remove header cells on first line', () => {
511
- const { editorView } = editor(buildTableDoc(true));
512
-
513
- toggleHeaderRow(editorView.state, editorView.dispatch);
514
-
515
- expect(editorView.state.doc).toEqualDocument(buildTableDoc(false));
516
- });
517
- });
518
-
519
- describe("when there's no header row yet", () => {
520
- it('it should convert first row to a header row', () => {
521
- // p('text') goes before table to ensure that conversion uses absolute position of cells relative to the document
522
- const { editorView } = editor(
523
- doc(p('text'), table()(tr(tdCursor, tdEmpty), tr(tdEmpty, tdEmpty))),
524
- );
525
- toggleHeaderRow(editorView.state, editorView.dispatch);
526
- expect(editorView.state.doc).toEqualDocument(
527
- doc(
528
- p('text'),
529
- table({ localId: TABLE_LOCAL_ID })(
530
- tr(thEmpty, thEmpty),
531
- tr(tdEmpty, tdEmpty),
532
- ),
533
- ),
534
- );
535
- });
536
-
537
- describe('when header column is enabled', () => {
538
- it('it should convert the rest of the cells from the first row to header cells', () => {
539
- const { editorView } = editor(
540
- doc(
541
- p('text'),
542
- table()(tr(thCursor, tdEmpty), tr(thEmpty, tdEmpty)),
543
- ),
544
- );
545
- toggleHeaderRow(editorView.state, editorView.dispatch);
546
- expect(editorView.state.doc).toEqualDocument(
547
- doc(
548
- p('text'),
549
- table({ localId: TABLE_LOCAL_ID })(
550
- tr(thEmpty, thEmpty),
551
- tr(thEmpty, tdEmpty),
552
- ),
553
- ),
554
- );
555
- });
556
- });
557
- });
558
-
559
- describe('when header row is enabled', () => {
560
- it('it should convert first row to a normal row', () => {
561
- const { editorView } = editor(
562
- doc(p('text'), table()(tr(thEmpty, thEmpty), tr(tdEmpty, tdEmpty))),
563
- );
564
- toggleHeaderRow(editorView.state, editorView.dispatch);
565
- expect(editorView.state.doc).toEqualDocument(
566
- doc(
567
- p('text'),
568
- table({ localId: TABLE_LOCAL_ID })(
569
- tr(tdEmpty, tdEmpty),
570
- tr(tdEmpty, tdEmpty),
571
- ),
572
- ),
573
- );
574
- });
575
-
576
- describe('when header column is enabled', () => {
577
- it('it should convert the rest of the cells from the first row to normal cells', () => {
578
- const { editorView } = editor(
579
- doc(
580
- p('text'),
581
- table()(tr(thCursor, thEmpty), tr(thEmpty, tdEmpty)),
582
- ),
583
- );
584
- toggleHeaderRow(editorView.state, editorView.dispatch);
585
- expect(editorView.state.doc).toEqualDocument(
586
- doc(
587
- p('text'),
588
- table({ localId: TABLE_LOCAL_ID })(
589
- tr(thEmpty, tdEmpty),
590
- tr(thEmpty, tdEmpty),
591
- ),
592
- ),
593
- );
594
- });
595
- });
596
- });
597
- });
598
-
599
- describe('toggleHeaderColumn()', () => {
600
- describe("when there's no header column yet", () => {
601
- it('it should convert first column to a header column', () => {
602
- // p('text') goes before table to ensure that conversion uses absolute position of cells relative to the document
603
- const { editorView } = editor(
604
- doc(p('text'), table()(tr(tdEmpty, tdEmpty), tr(tdEmpty, tdEmpty))),
605
- );
606
- toggleHeaderColumn(editorView.state, editorView.dispatch);
607
- expect(editorView.state.doc).toEqualDocument(
608
- doc(
609
- p('text'),
610
- table({ localId: TABLE_LOCAL_ID })(
611
- tr(thEmpty, tdEmpty),
612
- tr(thEmpty, tdEmpty),
613
- ),
614
- ),
615
- );
616
- });
617
-
618
- describe('when header row is enabled', () => {
619
- it('it should convert the rest of the cells from the first column to header cells', () => {
620
- const { editorView } = editor(
621
- doc(p('text'), table()(tr(thEmpty, thEmpty), tr(tdEmpty, tdEmpty))),
622
- );
623
- toggleHeaderColumn(editorView.state, editorView.dispatch);
624
- expect(editorView.state.doc).toEqualDocument(
625
- doc(
626
- p('text'),
627
- table({ localId: TABLE_LOCAL_ID })(
628
- tr(thEmpty, thEmpty),
629
- tr(thEmpty, tdEmpty),
630
- ),
631
- ),
632
- );
633
- });
634
- });
635
- });
636
-
637
- describe('when header column is enabled', () => {
638
- it('it should convert first column to a normal column', () => {
639
- const { editorView } = editor(
640
- doc(p('text'), table()(tr(thEmpty, tdEmpty), tr(thEmpty, tdEmpty))),
641
- );
642
- toggleHeaderColumn(editorView.state, editorView.dispatch);
643
- expect(editorView.state.doc).toEqualDocument(
644
- doc(
645
- p('text'),
646
- table({ localId: TABLE_LOCAL_ID })(
647
- tr(tdEmpty, tdEmpty),
648
- tr(tdEmpty, tdEmpty),
649
- ),
650
- ),
651
- );
652
- });
653
-
654
- describe('when header row is enabled', () => {
655
- it('it should convert the rest of the cells from the first column to normal cells', () => {
656
- const { editorView } = editor(
657
- doc(p('text'), table()(tr(thEmpty, thEmpty), tr(thEmpty, tdEmpty))),
658
- );
659
- toggleHeaderColumn(editorView.state, editorView.dispatch);
660
- expect(editorView.state.doc).toEqualDocument(
661
- doc(
662
- p('text'),
663
- table({ localId: TABLE_LOCAL_ID })(
664
- tr(thEmpty, thEmpty),
665
- tr(tdEmpty, tdEmpty),
666
- ),
667
- ),
668
- );
669
- });
670
- });
671
- });
672
- });
673
-
674
- describe.skip('when the cells contains only an image', () => {
675
- it('should add a paragraph below when arrow down is pressed', () => {
676
- const { editorView } = editor(
677
- doc(
678
- table()(
679
- tr(
680
- td()(
681
- mediaGroup(
682
- media({
683
- id: 'af9310df-fee5-459a-a968-99062ecbb756',
684
- type: 'file',
685
- collection: 'MediaServicesSample',
686
- __fileMimeType: 'image/jpeg',
687
- })(),
688
- ),
689
- ),
690
- tdEmpty,
691
- tdEmpty,
692
- ),
693
- tr(td()(p('2')), tdEmpty, tdEmpty),
694
- ),
695
- ),
696
- );
697
-
698
- setNodeSelection(editorView, 4);
699
- sendKeyToPm(editorView, 'ArrowDown');
700
-
701
- expect(editorView.state.doc).toEqualDocument(
702
- doc(
703
- table({ localId: TABLE_LOCAL_ID })(
704
- tr(
705
- td()(
706
- mediaGroup(
707
- media({
708
- id: 'af9310df-fee5-459a-a968-99062ecbb756',
709
- type: 'file',
710
- collection: 'MediaServicesSample',
711
- __fileMimeType: 'image/jpeg',
712
- })(),
713
- ),
714
- p(''),
715
- ),
716
- tdEmpty,
717
- tdEmpty,
718
- ),
719
- tr(td()(p('2')), tdEmpty, tdEmpty),
720
- ),
721
- ),
722
- );
723
- });
724
-
725
- it('should add a paragraph above when arrow up is pressed', () => {
726
- const { editorView } = editor(
727
- doc(
728
- table()(
729
- tr(
730
- td()(
731
- mediaGroup(
732
- media({
733
- id: 'af9310df-fee5-459a-a968-99062ecbb756',
734
- type: 'file',
735
- collection: 'MediaServicesSample',
736
- __fileMimeType: 'image/jpeg',
737
- })(),
738
- ),
739
- ),
740
- tdEmpty,
741
- tdEmpty,
742
- ),
743
- tr(td()(p('2')), tdEmpty, tdEmpty),
744
- ),
745
- ),
746
- );
747
-
748
- setNodeSelection(editorView, 4);
749
- sendKeyToPm(editorView, 'ArrowUp');
750
-
751
- expect(editorView.state.doc).toEqualDocument(
752
- doc(
753
- table({ localId: TABLE_LOCAL_ID })(
754
- tr(
755
- td()(
756
- p(''),
757
- mediaGroup(
758
- media({
759
- id: 'af9310df-fee5-459a-a968-99062ecbb756',
760
- type: 'file',
761
- collection: 'MediaServicesSample',
762
- __fileMimeType: 'image/jpeg',
763
- })(),
764
- ),
765
- ),
766
- tdEmpty,
767
- tdEmpty,
768
- ),
769
- tr(td()(p('2')), tdEmpty, tdEmpty),
770
- ),
771
- ),
772
- );
773
- });
774
-
775
- it('should not add a paragraph, if there already is a paragraph below when arrow down is pressed', () => {
776
- const docWithTable = doc(
777
- table({ localId: TABLE_LOCAL_ID })(
778
- tr(
779
- td()(
780
- mediaGroup(
781
- media({
782
- id: 'af9310df-fee5-459a-a968-99062ecbb756',
783
- type: 'file',
784
- collection: 'MediaServicesSample',
785
- __fileMimeType: 'image/jpeg',
786
- })(),
787
- ),
788
- p('1'),
789
- p('2'),
790
- ),
791
- tdEmpty,
792
- tdEmpty,
793
- ),
794
- tr(td()(p('3')), tdEmpty, tdEmpty),
795
- ),
796
- );
797
-
798
- const { editorView } = editor(docWithTable);
799
-
800
- setNodeSelection(editorView, 4);
801
- sendKeyToPm(editorView, 'ArrowDown');
802
-
803
- expect(editorView.state.doc).toEqualDocument(docWithTable);
804
- });
805
-
806
- it('should not add a paragraph, if there already is a paragraph above when arrow up is pressed', () => {
807
- const docWithTable = doc(
808
- table({ localId: TABLE_LOCAL_ID })(
809
- tr(
810
- td()(
811
- p('1'),
812
- p('2'),
813
- mediaGroup(
814
- media({
815
- id: 'af9310df-fee5-459a-a968-99062ecbb756',
816
- type: 'file',
817
- collection: 'MediaServicesSample',
818
- __fileMimeType: 'image/jpeg',
819
- })(),
820
- ),
821
- ),
822
- tdEmpty,
823
- tdEmpty,
824
- ),
825
- tr(td()(p('3')), tdEmpty, tdEmpty),
826
- ),
827
- );
828
-
829
- const { editorView } = editor(docWithTable);
830
-
831
- setNodeSelection(editorView, 6);
832
- sendKeyToPm(editorView, 'ArrowUp');
833
-
834
- expect(editorView.state.doc).toEqualDocument(docWithTable);
835
- });
836
- });
837
-
838
- describe('checkIfNumberColumnEnabled', () => {
839
- it('should return false if table is not in focus', () => {
840
- const { editorView } = editor(
841
- doc(table()(tr(tdCursor, tdEmpty, tdEmpty))),
842
- );
843
- expect(checkIfNumberColumnEnabled(editorView.state.selection)).toBe(
844
- false,
845
- );
846
- });
847
- });
848
-
849
- describe('checkIfHeaderColumnEnabled', () => {
850
- it('should return false if table is not in focus', () => {
851
- const { editorView } = editor(
852
- doc(table()(tr(tdCursor, tdEmpty, tdEmpty))),
853
- );
854
- expect(checkIfHeaderColumnEnabled(editorView.state.selection)).toBe(
855
- false,
856
- );
857
- });
858
- });
859
-
860
- describe('checkIfHeaderRowEnabled', () => {
861
- it('should return false if table is not in focus', () => {
862
- const { editorView } = editor(
863
- doc(table()(tr(tdCursor, tdEmpty, tdEmpty))),
864
- );
865
- expect(checkIfHeaderRowEnabled(editorView.state.selection)).toBe(false);
866
- });
867
- });
868
-
869
- describe('checkIfHeaderRowEnabled', () => {
870
- it('should return false if table is not in focus', () => {
871
- const { editorView } = editor(
872
- doc(table()(tr(tdCursor, tdEmpty, tdEmpty))),
873
- );
874
- expect(checkIfHeaderRowEnabled(editorView.state.selection)).toBe(false);
875
- });
876
- });
877
-
878
- describe('table plugin state', () => {
879
- it('should update tableNode when cursor enters the table', () => {
880
- const {
881
- editorView: view,
882
- refs: { nextPos },
883
- } = editor(doc(table()(tr(td()(p('{nextPos}')))), p('te{<>}xt')));
884
-
885
- setEditorFocus(true)(view.state, view.dispatch);
886
-
887
- view.dispatch(
888
- view.state.tr.setSelection(
889
- new TextSelection(view.state.doc.resolve(nextPos)),
890
- ),
891
- );
892
- const { tableNode } = getPluginState(view.state);
893
- expect(tableNode).toBeDefined();
894
- expect(tableNode!.type.name).toEqual('table');
895
- });
896
-
897
- it('should update targetCellPosition when document changes', () => {
898
- const { editorView } = editor(doc(table()(tr(tdCursor, tdEmpty))));
899
- const { state, dispatch } = editorView;
900
- setEditorFocus(true)(state, dispatch);
901
- let pluginState = getPluginState(editorView.state);
902
- expect(pluginState.targetCellPosition).toEqual(2);
903
-
904
- let documentChangeTr = editorView.state.tr.insertText('hello world', 1);
905
- // Don't use dispatch to mimic collab provider
906
- editorView.updateState(editorView.state.apply(documentChangeTr));
907
-
908
- pluginState = getPluginState(editorView.state);
909
- expect(pluginState.targetCellPosition).toEqual(23);
910
- });
911
- });
912
- });