@atlaskit/editor-plugin-table 7.5.16 → 7.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/cjs/pm-plugins/drag-and-drop/commands-with-analytics.js +36 -1
  3. package/dist/cjs/pm-plugins/drag-and-drop/commands.js +17 -1
  4. package/dist/cjs/pm-plugins/drag-and-drop/plugin.js +8 -2
  5. package/dist/cjs/pm-plugins/drag-and-drop/utils/getDragBehaviour.js +13 -0
  6. package/dist/cjs/pm-plugins/drag-and-drop/utils/monitor.js +11 -4
  7. package/dist/cjs/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.js +31 -6
  8. package/dist/cjs/ui/TableFloatingControls/RowDropTarget/index.js +31 -6
  9. package/dist/cjs/ui/consts.js +3 -2
  10. package/dist/cjs/utils/snapping.js +2 -1
  11. package/dist/es2019/pm-plugins/drag-and-drop/commands-with-analytics.js +34 -1
  12. package/dist/es2019/pm-plugins/drag-and-drop/commands.js +15 -1
  13. package/dist/es2019/pm-plugins/drag-and-drop/plugin.js +9 -3
  14. package/dist/es2019/pm-plugins/drag-and-drop/utils/getDragBehaviour.js +8 -0
  15. package/dist/es2019/pm-plugins/drag-and-drop/utils/monitor.js +9 -4
  16. package/dist/es2019/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.js +27 -3
  17. package/dist/es2019/ui/TableFloatingControls/RowDropTarget/index.js +27 -3
  18. package/dist/es2019/ui/consts.js +2 -1
  19. package/dist/es2019/utils/snapping.js +3 -2
  20. package/dist/esm/pm-plugins/drag-and-drop/commands-with-analytics.js +36 -1
  21. package/dist/esm/pm-plugins/drag-and-drop/commands.js +17 -1
  22. package/dist/esm/pm-plugins/drag-and-drop/plugin.js +9 -3
  23. package/dist/esm/pm-plugins/drag-and-drop/utils/getDragBehaviour.js +7 -0
  24. package/dist/esm/pm-plugins/drag-and-drop/utils/monitor.js +10 -4
  25. package/dist/esm/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.js +31 -6
  26. package/dist/esm/ui/TableFloatingControls/RowDropTarget/index.js +31 -6
  27. package/dist/esm/ui/consts.js +2 -1
  28. package/dist/esm/utils/snapping.js +3 -2
  29. package/dist/types/pm-plugins/drag-and-drop/commands-with-analytics.d.ts +1 -0
  30. package/dist/types/pm-plugins/drag-and-drop/commands.d.ts +1 -0
  31. package/dist/types/pm-plugins/drag-and-drop/utils/getDragBehaviour.d.ts +3 -0
  32. package/dist/types/types.d.ts +12 -1
  33. package/dist/types/ui/consts.d.ts +1 -0
  34. package/dist/types-ts4.5/pm-plugins/drag-and-drop/commands-with-analytics.d.ts +1 -0
  35. package/dist/types-ts4.5/pm-plugins/drag-and-drop/commands.d.ts +1 -0
  36. package/dist/types-ts4.5/pm-plugins/drag-and-drop/utils/getDragBehaviour.d.ts +3 -0
  37. package/dist/types-ts4.5/types.d.ts +12 -1
  38. package/dist/types-ts4.5/ui/consts.d.ts +1 -0
  39. package/package.json +3 -3
  40. package/src/pm-plugins/drag-and-drop/commands-with-analytics.ts +56 -1
  41. package/src/pm-plugins/drag-and-drop/commands.ts +31 -2
  42. package/src/pm-plugins/drag-and-drop/plugin.ts +21 -7
  43. package/src/pm-plugins/drag-and-drop/utils/getDragBehaviour.ts +12 -0
  44. package/src/pm-plugins/drag-and-drop/utils/monitor.ts +17 -3
  45. package/src/types.ts +13 -1
  46. package/src/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.tsx +39 -5
  47. package/src/ui/TableFloatingControls/RowDropTarget/index.tsx +40 -5
  48. package/src/ui/consts.ts +2 -0
  49. package/src/utils/snapping.ts +3 -4
@@ -4,7 +4,12 @@ import type {
4
4
  } from '@atlaskit/editor-prosemirror/state';
5
5
  import type { Decoration } from '@atlaskit/editor-prosemirror/view';
6
6
  import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
7
- import { moveColumn, moveRow } from '@atlaskit/editor-tables/utils';
7
+ import {
8
+ cloneColumn,
9
+ cloneRow,
10
+ moveColumn,
11
+ moveRow,
12
+ } from '@atlaskit/editor-tables/utils';
8
13
 
9
14
  import type { DraggableType, TableDirection } from '../../types';
10
15
  import { TableDecorations } from '../../types';
@@ -59,7 +64,6 @@ export const setDropTarget = (
59
64
  TableDecorations.ROW_INSERT_LINE,
60
65
  );
61
66
  }
62
-
63
67
  return {
64
68
  type: DragAndDropActionType.SET_DROP_TARGET,
65
69
  data: {
@@ -175,3 +179,28 @@ export const toggleDragMenu = (
175
179
  return tr.setMeta('addToHistory', false);
176
180
  },
177
181
  );
182
+
183
+ export const cloneSource = (
184
+ sourceType: DraggableType,
185
+ sourceIndexes: number[],
186
+ targetIndex: number,
187
+ targetDirection: 'start' | 'end',
188
+ tr?: Transaction,
189
+ ) =>
190
+ createCommand(
191
+ (state) => {
192
+ return {
193
+ type: DragAndDropActionType.CLEAR_DROP_TARGET,
194
+ data: {
195
+ decorationSet: DecorationSet.empty,
196
+ },
197
+ };
198
+ },
199
+ (originalTr: Transaction, state) => {
200
+ const nextTr = tr || originalTr;
201
+ const clone = sourceType === 'table-row' ? cloneRow : cloneColumn;
202
+ return clone(state, sourceIndexes, targetIndex, targetDirection, {
203
+ selectAfterClone: true,
204
+ })(nextTr);
205
+ },
206
+ );
@@ -26,6 +26,7 @@ import { DragAndDropActionType } from './actions';
26
26
  import { clearDropTarget, setDropTarget, toggleDragMenu } from './commands';
27
27
  import {
28
28
  clearDropTargetWithAnalytics,
29
+ cloneSourceWithAnalytics,
29
30
  moveSourceWithAnalytics,
30
31
  } from './commands-with-analytics';
31
32
  import { DropTargetType } from './consts';
@@ -172,7 +173,9 @@ const destroyFn = (editorView: EditorView, editorAnalyticsAPI: any) => {
172
173
  sourceIndexes,
173
174
  targetIndex,
174
175
  targetAdjustedIndex,
176
+ targetDirection,
175
177
  direction,
178
+ behaviour,
176
179
  } = data;
177
180
 
178
181
  // When we drop on a target we will know the targets row/col index for certain,
@@ -203,13 +206,24 @@ const destroyFn = (editorView: EditorView, editorAnalyticsAPI: any) => {
203
206
  }
204
207
 
205
208
  requestAnimationFrame(() => {
206
- moveSourceWithAnalytics(editorAnalyticsAPI)(
207
- INPUT_METHOD.DRAG_AND_DROP,
208
- sourceType,
209
- sourceIndexes,
210
- targetAdjustedIndex + (direction === -1 ? 0 : -1),
211
- tr,
212
- )(editorView.state, editorView.dispatch);
209
+ if (behaviour === 'clone') {
210
+ cloneSourceWithAnalytics(editorAnalyticsAPI)(
211
+ INPUT_METHOD.DRAG_AND_DROP,
212
+ sourceType,
213
+ sourceIndexes,
214
+ targetIndex,
215
+ targetDirection,
216
+ tr,
217
+ )(editorView.state, editorView.dispatch);
218
+ } else {
219
+ moveSourceWithAnalytics(editorAnalyticsAPI)(
220
+ INPUT_METHOD.DRAG_AND_DROP,
221
+ sourceType,
222
+ sourceIndexes,
223
+ targetAdjustedIndex + (direction === 1 ? -1 : 0),
224
+ tr,
225
+ )(editorView.state, editorView.dispatch);
226
+ }
213
227
 
214
228
  // force a colgroup update here, otherwise dropped columns don't have
215
229
  // the correct width immediately after the drop
@@ -0,0 +1,12 @@
1
+ import { browser } from '@atlaskit/editor-common/utils';
2
+ import type { Input } from '@atlaskit/pragmatic-drag-and-drop/types';
3
+
4
+ import type { DraggableBehaviour } from '../../../types';
5
+
6
+ export const getDragBehaviour = ({
7
+ altKey,
8
+ ctrlKey,
9
+ }: Input): DraggableBehaviour => {
10
+ const isCloneModifierKeyPressed = browser.mac ? altKey : ctrlKey;
11
+ return isCloneModifierKeyPressed ? 'clone' : 'move';
12
+ };
@@ -8,6 +8,8 @@ import type {
8
8
  DraggableTargetData,
9
9
  } from '../../../types';
10
10
 
11
+ import { getDragBehaviour } from './getDragBehaviour';
12
+
11
13
  export const getDraggableDataFromEvent = ({
12
14
  location,
13
15
  source,
@@ -54,9 +56,16 @@ export const getDraggableDataFromEvent = ({
54
56
  const targetOffset =
55
57
  targetClosestEdge === 'right' || targetClosestEdge === 'bottom' ? 1 : 0;
56
58
 
57
- // since only consecutive rows/cols can be moved we can assume that if the first index is greater then
58
- // the target index, the then the direction of the DnD is decreasing
59
- const direction = sourceIndexes[0] > targetIndex ? -1 : 1;
59
+ // if the min index is greater then the target index, the then the direction of the DnD is decreasing
60
+ // if the target is within the min/max index then we can assume that no direction exists so it will be 0.
61
+ const srcMin = Math.min(...sourceIndexes);
62
+ const srcMax = Math.max(...sourceIndexes);
63
+ const direction =
64
+ targetIndex >= srcMin && targetIndex <= srcMax
65
+ ? 0
66
+ : srcMin >= targetIndex
67
+ ? -1
68
+ : 1;
60
69
 
61
70
  return {
62
71
  sourceType,
@@ -67,6 +76,11 @@ export const getDraggableDataFromEvent = ({
67
76
  targetIndex,
68
77
  targetAdjustedIndex: targetIndex + targetOffset,
69
78
  targetClosestEdge,
79
+ targetDirection:
80
+ targetClosestEdge === 'top' || targetClosestEdge === 'left'
81
+ ? 'start'
82
+ : 'end',
70
83
  direction,
84
+ behaviour: getDragBehaviour(location.current.input),
71
85
  };
72
86
  };
package/src/types.ts CHANGED
@@ -477,6 +477,8 @@ export type TableDirection = 'row' | 'column';
477
477
  * Drag and Drop interfaces
478
478
  */
479
479
  export type DraggableType = 'table-row' | 'table-column';
480
+ export type DraggableBehaviour = 'move' | 'clone';
481
+
480
482
  export interface DraggableSourceData extends Record<string, unknown> {
481
483
  type: DraggableType;
482
484
  localId: string;
@@ -498,7 +500,17 @@ export interface DraggableData {
498
500
  targetIndex: number;
499
501
  targetAdjustedIndex: number;
500
502
  targetClosestEdge: Edge;
501
- direction: 1 | -1;
503
+ /**
504
+ * The target direction identifies where relative to the target index is the item being dropped. A value of 'start' would
505
+ * mean that the item is being inserted before the index, and 'end would be after.
506
+ */
507
+ targetDirection: 'start' | 'end';
508
+ /**
509
+ * This represents a hollistic movement direction; a value of 1 means the source->target index would shift in a positive direction.
510
+ * A value of 0 indicates that the target index is inside the the source indexes.
511
+ */
512
+ direction: 1 | -1 | 0;
513
+ behaviour: DraggableBehaviour;
502
514
  }
503
515
 
504
516
  export type HandleTypes = 'hover' | 'selected';
@@ -1,8 +1,12 @@
1
1
  import React, { useEffect, useRef } from 'react';
2
2
 
3
- import { attachClosestEdge } from '@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge';
3
+ import {
4
+ attachClosestEdge,
5
+ type Edge,
6
+ } from '@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge';
4
7
  import { dropTargetForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter';
5
8
 
9
+ import { getDragBehaviour } from '../../../pm-plugins/drag-and-drop/utils/getDragBehaviour';
6
10
  import type { DraggableSourceData } from '../../../types';
7
11
 
8
12
  export interface Props {
@@ -29,8 +33,16 @@ export const ColumnDropTarget = ({
29
33
 
30
34
  return dropTargetForElements({
31
35
  element: dropTargetRef.current,
32
- canDrop({ source }) {
36
+ canDrop({ source, input }) {
33
37
  const data = source.data as DraggableSourceData;
38
+ const behaviour = getDragBehaviour(input);
39
+
40
+ // A move drop is limited too where it can go, however a clone can drop can go anywhere.
41
+ const isDropValid =
42
+ behaviour === 'move'
43
+ ? data.indexes?.indexOf(index) === -1
44
+ : behaviour === 'clone';
45
+
34
46
  return (
35
47
  // Only draggables of row type can be dropped on this target
36
48
  data.type === 'table-column' &&
@@ -38,20 +50,42 @@ export const ColumnDropTarget = ({
38
50
  data.localId === localId &&
39
51
  // Only draggables which DO NOT include this drop targets index can be dropped
40
52
  !!data.indexes?.length &&
41
- data.indexes?.indexOf(index) === -1
53
+ isDropValid
42
54
  );
43
55
  },
56
+ getDropEffect: ({ input }) =>
57
+ getDragBehaviour(input) === 'clone' ? 'copy' : 'move',
44
58
  getIsSticky: () => true,
45
- getData({ input, element }) {
59
+ getData({ source, input, element }) {
46
60
  const data = {
47
61
  localId,
48
62
  type: 'table-column',
49
63
  targetIndex: index,
50
64
  };
65
+ const srcData = source.data as DraggableSourceData;
66
+ const behaviour = getDragBehaviour(input);
67
+
68
+ // During a move op there's no point in allowing edges to be dropped on which result in no change/move to occur.
69
+ const allowedEdges: Edge[] =
70
+ behaviour === 'move'
71
+ ? srcData.indexes?.reduce(
72
+ (acc, v) => {
73
+ if (v - index === -1) {
74
+ acc.shift();
75
+ }
76
+ if (v - index === 1) {
77
+ acc.pop();
78
+ }
79
+ return acc;
80
+ },
81
+ ['left', 'right'],
82
+ )
83
+ : ['left', 'right'];
84
+
51
85
  return attachClosestEdge(data, {
52
86
  input,
53
87
  element,
54
- allowedEdges: ['left', 'right'],
88
+ allowedEdges,
55
89
  });
56
90
  },
57
91
  });
@@ -1,8 +1,12 @@
1
1
  import React, { useEffect, useRef } from 'react';
2
2
 
3
- import { attachClosestEdge } from '@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge';
3
+ import {
4
+ attachClosestEdge,
5
+ type Edge,
6
+ } from '@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge';
4
7
  import { dropTargetForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter';
5
8
 
9
+ import { getDragBehaviour } from '../../../pm-plugins/drag-and-drop/utils/getDragBehaviour';
6
10
  import type { DraggableSourceData } from '../../../types';
7
11
 
8
12
  export type RowDropTargetProps = {
@@ -21,8 +25,16 @@ const RowDropTarget = ({ index, localId, style }: RowDropTargetProps) => {
21
25
 
22
26
  return dropTargetForElements({
23
27
  element: dropTargetRef.current,
24
- canDrop({ source }) {
28
+ canDrop({ source, input }) {
25
29
  const data = source.data as DraggableSourceData;
30
+ const behaviour = getDragBehaviour(input);
31
+
32
+ // A move drop is limited too where it can go, however a clone can drop can go anywhere.
33
+ const isDropValid =
34
+ behaviour === 'move'
35
+ ? data.indexes?.indexOf(index) === -1
36
+ : behaviour === 'clone';
37
+
26
38
  return (
27
39
  // Only draggables of row type can be dropped on this target
28
40
  data.type === 'table-row' &&
@@ -30,20 +42,43 @@ const RowDropTarget = ({ index, localId, style }: RowDropTargetProps) => {
30
42
  data.localId === localId &&
31
43
  // Only draggables which DO NOT include this drop targets index can be dropped
32
44
  !!data.indexes?.length &&
33
- data.indexes?.indexOf(index) === -1
45
+ isDropValid
34
46
  );
35
47
  },
48
+ getDropEffect: ({ input }) =>
49
+ getDragBehaviour(input) === 'clone' ? 'copy' : 'move',
36
50
  getIsSticky: () => true,
37
- getData({ input, element }) {
51
+ getData({ source, input, element }) {
38
52
  const data = {
39
53
  localId,
40
54
  type: 'table-row',
41
55
  targetIndex: index,
42
56
  };
57
+
58
+ const srcData = source.data as DraggableSourceData;
59
+ const behaviour = getDragBehaviour(input);
60
+
61
+ // During a move op there's no point in allowing edges to be dropped on which result in no change/move to occur.
62
+ const allowedEdges: Edge[] =
63
+ behaviour === 'move'
64
+ ? srcData.indexes?.reduce(
65
+ (acc, v) => {
66
+ if (v - index === -1) {
67
+ acc.shift();
68
+ }
69
+ if (v - index === 1) {
70
+ acc.pop();
71
+ }
72
+ return acc;
73
+ },
74
+ ['top', 'bottom'],
75
+ )
76
+ : ['top', 'bottom'];
77
+
43
78
  return attachClosestEdge(data, {
44
79
  input,
45
80
  element,
46
- allowedEdges: ['top', 'bottom'],
81
+ allowedEdges,
47
82
  });
48
83
  },
49
84
  });
package/src/ui/consts.ts CHANGED
@@ -143,3 +143,5 @@ export const dragMenuDropdownWidth = 250;
143
143
  export const dragTableInsertColumnButtonSize = 16;
144
144
  export const dropTargetExtendedWidth = 150;
145
145
  export const colorPalletteColumns = 7;
146
+
147
+ export const tableResizerWidth = 8;
@@ -5,9 +5,10 @@ import {
5
5
  akEditorDefaultLayoutWidth,
6
6
  akEditorFullWidthLayoutWidth,
7
7
  akEditorGutterPadding,
8
- akEditorMediaResizeHandlerPaddingWide,
9
8
  } from '@atlaskit/editor-shared-styles';
10
9
 
10
+ import { tableResizerWidth } from '../ui/consts';
11
+
11
12
  const numberOfLanesInDefaultLayoutWidth = 12;
12
13
 
13
14
  const calculateSubSnappingWidths = (totalLanes: number, totalWidth: number) =>
@@ -34,9 +35,7 @@ export const calculateDefaultTablePreserveSnappings = (
34
35
  editorContainerWith - akEditorGutterPadding * 2 >=
35
36
  akEditorFullWidthLayoutWidth
36
37
  ? akEditorFullWidthLayoutWidth
37
- : editorContainerWith -
38
- akEditorGutterPadding * 2 -
39
- akEditorMediaResizeHandlerPaddingWide;
38
+ : editorContainerWith - akEditorGutterPadding * 2 - tableResizerWidth;
40
39
 
41
40
  return [
42
41
  ...calculateSubSnappingWidths(