@atlaskit/editor-plugin-table 5.7.6 → 5.7.8

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 (35) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cjs/commands-with-analytics.js +29 -5
  3. package/dist/cjs/plugin.js +54 -52
  4. package/dist/cjs/pm-plugins/drag-and-drop/commands-with-analytics.js +49 -4
  5. package/dist/cjs/pm-plugins/keymap.js +13 -1
  6. package/dist/cjs/ui/consts.js +1 -1
  7. package/dist/cjs/utils/drag-menu.js +14 -7
  8. package/dist/es2019/commands-with-analytics.js +26 -1
  9. package/dist/es2019/plugin.js +6 -1
  10. package/dist/es2019/pm-plugins/drag-and-drop/commands-with-analytics.js +46 -2
  11. package/dist/es2019/pm-plugins/keymap.js +15 -3
  12. package/dist/es2019/ui/consts.js +1 -1
  13. package/dist/es2019/utils/drag-menu.js +15 -8
  14. package/dist/esm/commands-with-analytics.js +29 -5
  15. package/dist/esm/plugin.js +54 -52
  16. package/dist/esm/pm-plugins/drag-and-drop/commands-with-analytics.js +47 -2
  17. package/dist/esm/pm-plugins/keymap.js +15 -3
  18. package/dist/esm/ui/consts.js +1 -1
  19. package/dist/esm/utils/drag-menu.js +15 -8
  20. package/dist/types/commands-with-analytics.d.ts +3 -2
  21. package/dist/types/pm-plugins/drag-and-drop/commands-with-analytics.d.ts +7 -5
  22. package/dist/types/pm-plugins/keymap.d.ts +1 -1
  23. package/dist/types/ui/consts.d.ts +1 -1
  24. package/dist/types-ts4.5/commands-with-analytics.d.ts +3 -2
  25. package/dist/types-ts4.5/pm-plugins/drag-and-drop/commands-with-analytics.d.ts +7 -5
  26. package/dist/types-ts4.5/pm-plugins/keymap.d.ts +1 -1
  27. package/dist/types-ts4.5/ui/consts.d.ts +1 -1
  28. package/package.json +4 -4
  29. package/src/__tests__/unit/ui/FloatingDragMenu.tsx +6 -6
  30. package/src/commands-with-analytics.ts +39 -2
  31. package/src/plugin.tsx +10 -2
  32. package/src/pm-plugins/drag-and-drop/commands-with-analytics.ts +85 -7
  33. package/src/pm-plugins/keymap.ts +52 -0
  34. package/src/ui/consts.ts +1 -1
  35. package/src/utils/drag-menu.ts +13 -1
@@ -144,9 +144,9 @@ describe('FloatingDragMenu', () => {
144
144
  "Add row aboveCtrl+Alt+↑",
145
145
  "Add row belowCtrl+Alt+↓",
146
146
  "Clear cells⌫",
147
- "Delete row",
148
- "Move row up",
149
- "Move row down",
147
+ "Delete rowCtrl+⌫",
148
+ "Move row upCtrl+Alt+Shift+↑",
149
+ "Move row downCtrl+Alt+Shift+↓",
150
150
  ]
151
151
  `);
152
152
  });
@@ -180,9 +180,9 @@ describe('FloatingDragMenu', () => {
180
180
  "Add column rightCtrl+Alt+→",
181
181
  "Distribute columns",
182
182
  "Clear cells⌫",
183
- "Delete column",
184
- "Move column left",
185
- "Move column right",
183
+ "Delete columnCtrl+⌫",
184
+ "Move column leftCtrl+Alt+Shift+←",
185
+ "Move column rightCtrl+Alt+Shift+→",
186
186
  "Sort increasing",
187
187
  "Sort decreasing",
188
188
  ]
@@ -32,6 +32,7 @@ import { insertColumn, insertRow } from './commands/insert';
32
32
  import {
33
33
  deleteTable,
34
34
  deleteTableIfSelected,
35
+ getTableSelectionType,
35
36
  setMultipleCellAttrs,
36
37
  } from './commands/misc';
37
38
  import { sortByColumn } from './commands/sort';
@@ -283,7 +284,8 @@ export const deleteRowsWithAnalytics =
283
284
  inputMethod:
284
285
  | INPUT_METHOD.CONTEXT_MENU
285
286
  | INPUT_METHOD.BUTTON
286
- | INPUT_METHOD.FLOATING_TB,
287
+ | INPUT_METHOD.FLOATING_TB
288
+ | INPUT_METHOD.SHORTCUT,
287
289
  rect: Rect,
288
290
  isHeaderRowRequired: boolean,
289
291
  ) =>
@@ -317,7 +319,8 @@ export const deleteColumnsWithAnalytics =
317
319
  inputMethod:
318
320
  | INPUT_METHOD.CONTEXT_MENU
319
321
  | INPUT_METHOD.BUTTON
320
- | INPUT_METHOD.FLOATING_TB,
322
+ | INPUT_METHOD.FLOATING_TB
323
+ | INPUT_METHOD.SHORTCUT,
321
324
  rect: Rect,
322
325
  ) =>
323
326
  withEditorAnalyticsAPI(({ selection }) => {
@@ -339,6 +342,40 @@ export const deleteColumnsWithAnalytics =
339
342
  };
340
343
  })(editorAnalyticsAPI)(deleteColumnsCommand(rect));
341
344
 
345
+ export const deleteSelectedRowsOrColumnsWithAnalyticsViaShortcut =
346
+ (editorAnalyticsAPI: EditorAnalyticsAPI | undefined | null): Command =>
347
+ (state, dispatch) => {
348
+ const { selection } = state;
349
+ const isCellSelection = selection instanceof CellSelection;
350
+ if (!isCellSelection) {
351
+ return false;
352
+ }
353
+
354
+ const rect = getSelectionRect(selection);
355
+ if (!rect) {
356
+ return false;
357
+ }
358
+
359
+ const selectionType = getTableSelectionType(selection);
360
+ if (selectionType === 'row') {
361
+ const { pluginConfig } = getPluginState(state);
362
+ const isHeaderRowRequired = pluginConfig.isHeaderRowRequired || false;
363
+
364
+ return deleteRowsWithAnalytics(editorAnalyticsAPI)(
365
+ INPUT_METHOD.SHORTCUT,
366
+ rect,
367
+ isHeaderRowRequired,
368
+ )(state, dispatch);
369
+ } else if (selectionType === 'column') {
370
+ return deleteColumnsWithAnalytics(editorAnalyticsAPI)(
371
+ INPUT_METHOD.SHORTCUT,
372
+ rect,
373
+ )(state, dispatch);
374
+ } else {
375
+ return false;
376
+ }
377
+ };
378
+
342
379
  const getTableDeletedAnalytics = (
343
380
  selection: Selection,
344
381
  inputMethod: INPUT_METHOD.FLOATING_TB | INPUT_METHOD.KEYBOARD,
package/src/plugin.tsx CHANGED
@@ -245,8 +245,16 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
245
245
  // plugin as it is currently swallowing backspace events inside tables
246
246
  {
247
247
  name: 'tableKeymap',
248
- plugin: () =>
249
- keymapPlugin(defaultGetEditorContainerWidth, editorAnalyticsAPI),
248
+ plugin: () => {
249
+ const { dragAndDropEnabled } =
250
+ options || ({} as TablePluginOptions);
251
+
252
+ return keymapPlugin(
253
+ defaultGetEditorContainerWidth,
254
+ editorAnalyticsAPI,
255
+ dragAndDropEnabled,
256
+ );
257
+ },
250
258
  },
251
259
  {
252
260
  name: 'tableSelectionKeymap',
@@ -1,20 +1,30 @@
1
1
  import {
2
2
  ACTION_SUBJECT,
3
3
  EVENT_TYPE,
4
+ INPUT_METHOD,
4
5
  TABLE_ACTION,
5
6
  TABLE_STATUS,
6
7
  } from '@atlaskit/editor-common/analytics';
7
- import type {
8
- EditorAnalyticsAPI,
9
- INPUT_METHOD,
10
- } from '@atlaskit/editor-common/analytics';
8
+ import type { EditorAnalyticsAPI } from '@atlaskit/editor-common/analytics';
9
+ import type { Command } from '@atlaskit/editor-common/types';
11
10
  import type {
12
11
  EditorState,
13
12
  Transaction,
14
13
  } from '@atlaskit/editor-prosemirror/state';
14
+ import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
15
+ import {
16
+ findCellRectClosestToPos,
17
+ getSelectionRect,
18
+ } from '@atlaskit/editor-tables/utils';
15
19
 
16
- import type { DraggableType } from '../../types';
17
- import { getSelectedTableInfo } from '../../utils';
20
+ import type { DraggableData, DraggableType } from '../../types';
21
+ import {
22
+ getSelectedColumnIndexes,
23
+ getSelectedRowIndexes,
24
+ getSelectedTableInfo,
25
+ hasMergedCellsInColumn,
26
+ hasMergedCellsInRow,
27
+ } from '../../utils';
18
28
  import { withEditorAnalyticsAPI } from '../../utils/analytics';
19
29
 
20
30
  import { clearDropTarget, moveSource } from './commands';
@@ -59,7 +69,10 @@ export const clearDropTargetWithAnalytics =
59
69
  export const moveSourceWithAnalytics =
60
70
  (editorAnalyticsAPI: EditorAnalyticsAPI | undefined | null) =>
61
71
  (
62
- inputMethod: INPUT_METHOD.TABLE_CONTEXT_MENU | INPUT_METHOD.DRAG_AND_DROP,
72
+ inputMethod:
73
+ | INPUT_METHOD.TABLE_CONTEXT_MENU
74
+ | INPUT_METHOD.DRAG_AND_DROP
75
+ | INPUT_METHOD.SHORTCUT,
63
76
  sourceType: DraggableType,
64
77
  sourceIndex: number,
65
78
  targetIndex: number,
@@ -98,3 +111,68 @@ export const moveSourceWithAnalytics =
98
111
  return true;
99
112
  });
100
113
  };
114
+
115
+ export const moveSourceWithAnalyticsViaShortcut =
116
+ (editorAnalyticsAPI: EditorAnalyticsAPI | undefined | null) =>
117
+ (sourceType: DraggableType, direction: DraggableData['direction']): Command =>
118
+ (state, dispatch) => {
119
+ const { selection } = state;
120
+ const isCellSelection = selection instanceof CellSelection;
121
+ const selectionRect = isCellSelection
122
+ ? getSelectionRect(selection)
123
+ : findCellRectClosestToPos(selection.$from);
124
+
125
+ if (!selectionRect) {
126
+ return false;
127
+ }
128
+
129
+ const isRow = sourceType === 'table-row';
130
+ const selectedIndexes = isRow
131
+ ? getSelectedRowIndexes(selectionRect)
132
+ : getSelectedColumnIndexes(selectionRect);
133
+
134
+ // We can move if only one row/column selected
135
+ if (selectedIndexes.length === 0 || selectedIndexes.length > 1) {
136
+ return false;
137
+ }
138
+
139
+ const sourceIndex = selectedIndexes[0];
140
+ // we can move only by one row/column
141
+ // 'direction' can only be 1 (for right or down) or -1 (for left or up)
142
+ const targetIndex = sourceIndex + direction;
143
+
144
+ // We can move only if targetIndex is a positive number and is not higher than the total number of rows/columns.
145
+ const { totalRowCount, totalColumnCount } = getSelectedTableInfo(selection);
146
+ const isValidTargetIndex =
147
+ targetIndex < 0
148
+ ? false
149
+ : isRow
150
+ ? targetIndex <= totalRowCount - 1
151
+ : targetIndex <= totalColumnCount - 1;
152
+
153
+ if (!isValidTargetIndex) {
154
+ return false;
155
+ }
156
+
157
+ // We can move only if there are no merged cells in the source or target row/column
158
+ const hasMergedCellsInSource = isRow
159
+ ? hasMergedCellsInRow(sourceIndex)(selection)
160
+ : hasMergedCellsInColumn(sourceIndex)(selection);
161
+ if (hasMergedCellsInSource) {
162
+ return false;
163
+ }
164
+
165
+ const hasMergedCellsInTarget = isRow
166
+ ? hasMergedCellsInRow(targetIndex)(selection)
167
+ : hasMergedCellsInColumn(targetIndex)(selection);
168
+ if (hasMergedCellsInTarget) {
169
+ return false;
170
+ }
171
+
172
+ return moveSourceWithAnalytics(editorAnalyticsAPI)(
173
+ INPUT_METHOD.SHORTCUT,
174
+ sourceType,
175
+ sourceIndex,
176
+ targetIndex,
177
+ )(state, dispatch);
178
+ };
@@ -13,6 +13,12 @@ import {
13
13
  addRowBefore,
14
14
  backspace,
15
15
  bindKeymapWithCommand,
16
+ deleteColumn,
17
+ deleteRow,
18
+ moveColumnLeft,
19
+ moveColumnRight,
20
+ moveRowDown,
21
+ moveRowUp,
16
22
  nextCell,
17
23
  previousCell,
18
24
  toggleTable,
@@ -30,6 +36,7 @@ import {
30
36
  } from '../commands';
31
37
  import {
32
38
  addRowAroundSelection,
39
+ deleteSelectedRowsOrColumnsWithAnalyticsViaShortcut,
33
40
  deleteTableIfSelectedWithAnalytics,
34
41
  emptyMultipleCellsWithAnalytics,
35
42
  } from '../commands-with-analytics';
@@ -37,6 +44,7 @@ import {
37
44
  addColumnAfter as addColumnAfterCommand,
38
45
  addColumnBefore as addColumnBeforeCommand,
39
46
  } from '../commands/insert';
47
+ import { moveSourceWithAnalyticsViaShortcut } from '../pm-plugins/drag-and-drop/commands-with-analytics';
40
48
  import { withEditorAnalyticsAPI } from '../utils/analytics';
41
49
 
42
50
  const createTableWithAnalytics = (
@@ -53,6 +61,7 @@ const createTableWithAnalytics = (
53
61
  export function keymapPlugin(
54
62
  getEditorContainerWidth: GetEditorContainerWidth,
55
63
  editorAnalyticsAPI: EditorAnalyticsAPI | undefined | null,
64
+ dragAndDropEnabled?: boolean,
56
65
  ): SafePlugin {
57
66
  const list = {};
58
67
 
@@ -110,6 +119,49 @@ export function keymapPlugin(
110
119
  list,
111
120
  );
112
121
 
122
+ if (dragAndDropEnabled) {
123
+ // Move row/column shortcuts
124
+ bindKeymapWithCommand(
125
+ moveRowDown.common!,
126
+ moveSourceWithAnalyticsViaShortcut(editorAnalyticsAPI)('table-row', 1),
127
+ list,
128
+ );
129
+
130
+ bindKeymapWithCommand(
131
+ moveRowUp.common!,
132
+ moveSourceWithAnalyticsViaShortcut(editorAnalyticsAPI)('table-row', -1),
133
+ list,
134
+ );
135
+
136
+ bindKeymapWithCommand(
137
+ moveColumnLeft.common!,
138
+ moveSourceWithAnalyticsViaShortcut(editorAnalyticsAPI)(
139
+ 'table-column',
140
+ -1,
141
+ ),
142
+ list,
143
+ );
144
+
145
+ bindKeymapWithCommand(
146
+ moveColumnRight.common!,
147
+ moveSourceWithAnalyticsViaShortcut(editorAnalyticsAPI)('table-column', 1),
148
+ list,
149
+ );
150
+
151
+ // Delete row/column shortcuts
152
+ bindKeymapWithCommand(
153
+ deleteColumn.common!,
154
+ deleteSelectedRowsOrColumnsWithAnalyticsViaShortcut(editorAnalyticsAPI),
155
+ list,
156
+ );
157
+
158
+ bindKeymapWithCommand(
159
+ deleteRow.common!,
160
+ deleteSelectedRowsOrColumnsWithAnalyticsViaShortcut(editorAnalyticsAPI),
161
+ list,
162
+ );
163
+ }
164
+
113
165
  return keymap(list) as SafePlugin;
114
166
  }
115
167
 
package/src/ui/consts.ts CHANGED
@@ -137,6 +137,6 @@ export const STICKY_HEADER_TOGGLE_TOLERANCE_MS = 5;
137
137
  // BUT it cannot be snapped to during resize due to padding being applied to the resizer wrapper. This accommodates that difference.
138
138
  export const TABLE_GUIDELINE_VISIBLE_ADJUSTMENT = -68;
139
139
 
140
- export const dragMenuDropdownWidth = 240;
140
+ export const dragMenuDropdownWidth = 250;
141
141
  export const dragTableInsertColumnButtonSize = 16;
142
142
  export const dropTargetExtendedWidth = 150;
@@ -7,6 +7,12 @@ import {
7
7
  addRowAfter,
8
8
  addRowBefore,
9
9
  backspace,
10
+ deleteColumn,
11
+ deleteRow,
12
+ moveColumnLeft,
13
+ moveColumnRight,
14
+ moveRowDown,
15
+ moveRowUp,
10
16
  tooltip,
11
17
  } from '@atlaskit/editor-common/keymaps';
12
18
  import type {
@@ -121,6 +127,7 @@ export const getDragMenuConfig = (
121
127
  offset: -1,
122
128
  canMove: (index?: number) => canDrag && canDecrease(index),
123
129
  icon: ArrowUpIcon,
130
+ keymap: moveRowUp,
124
131
  },
125
132
  {
126
133
  label: 'down',
@@ -128,6 +135,7 @@ export const getDragMenuConfig = (
128
135
  canMove: (index?: number) =>
129
136
  canDrag && canIncrease(index, (tableMap?.height ?? 0) - 1),
130
137
  icon: ArrowDownIcon,
138
+ keymap: moveRowDown,
131
139
  },
132
140
  ]
133
141
  : [
@@ -136,6 +144,7 @@ export const getDragMenuConfig = (
136
144
  offset: -1,
137
145
  canMove: (index?: number) => canDrag && canDecrease(index),
138
146
  icon: ArrowLeftIcon,
147
+ keymap: moveColumnLeft,
139
148
  },
140
149
  {
141
150
  label: 'right',
@@ -143,6 +152,7 @@ export const getDragMenuConfig = (
143
152
  canMove: (index?: number) =>
144
153
  canDrag && canIncrease(index, (tableMap?.width ?? 0) - 1),
145
154
  icon: ArrowRightIcon,
155
+ keymap: moveColumnRight,
146
156
  },
147
157
  ];
148
158
 
@@ -229,8 +239,9 @@ export const getDragMenuConfig = (
229
239
  return true;
230
240
  },
231
241
  icon: RemoveIcon,
242
+ keymap: direction === 'row' ? tooltip(deleteRow) : tooltip(deleteColumn),
232
243
  },
233
- ...moveOptions.map(({ label, offset, canMove, icon }) => ({
244
+ ...moveOptions.map(({ label, offset, canMove, icon, keymap }) => ({
234
245
  id: `move_${direction}_${label}`,
235
246
  title: `Move ${direction} ${label}`,
236
247
  disabled: !canMove(index),
@@ -249,6 +260,7 @@ export const getDragMenuConfig = (
249
260
  }
250
261
  return false;
251
262
  },
263
+ keymap: keymap && tooltip(keymap),
252
264
  })),
253
265
 
254
266
  ...sortOptions.map(({ label, order, icon }) => ({