@atlaskit/editor-plugin-table 5.3.29 → 5.3.31

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 (75) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/plugins/table/event-handlers.js +10 -2
  3. package/dist/cjs/plugins/table/nodeviews/ExternalDropTargets.js +75 -0
  4. package/dist/cjs/plugins/table/nodeviews/TableComponent.js +75 -4
  5. package/dist/cjs/plugins/table/nodeviews/TableRow.js +36 -112
  6. package/dist/cjs/plugins/table/nodeviews/TableStickyScrollbar.js +9 -9
  7. package/dist/cjs/plugins/table/pm-plugins/drag-and-drop/plugin.js +0 -8
  8. package/dist/cjs/plugins/table/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.js +2 -1
  9. package/dist/cjs/plugins/table/ui/TableFloatingColumnControls/index.js +2 -4
  10. package/dist/cjs/plugins/table/ui/TableFloatingControls/RowControls/DragControls.js +62 -10
  11. package/dist/cjs/plugins/table/ui/TableFloatingControls/RowDropTarget/index.js +61 -0
  12. package/dist/cjs/plugins/table/ui/TableFloatingControls/index.js +21 -15
  13. package/dist/cjs/plugins/table/ui/common-styles.js +1 -1
  14. package/dist/cjs/plugins/table/ui/consts.js +4 -2
  15. package/dist/es2019/plugins/table/event-handlers.js +11 -3
  16. package/dist/es2019/plugins/table/nodeviews/ExternalDropTargets.js +64 -0
  17. package/dist/es2019/plugins/table/nodeviews/TableComponent.js +80 -5
  18. package/dist/es2019/plugins/table/nodeviews/TableRow.js +4 -78
  19. package/dist/es2019/plugins/table/nodeviews/TableStickyScrollbar.js +6 -6
  20. package/dist/es2019/plugins/table/pm-plugins/drag-and-drop/plugin.js +0 -8
  21. package/dist/es2019/plugins/table/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.js +2 -1
  22. package/dist/es2019/plugins/table/ui/TableFloatingColumnControls/index.js +2 -4
  23. package/dist/es2019/plugins/table/ui/TableFloatingControls/RowControls/DragControls.js +58 -8
  24. package/dist/es2019/plugins/table/ui/TableFloatingControls/RowDropTarget/index.js +55 -0
  25. package/dist/es2019/plugins/table/ui/TableFloatingControls/index.js +17 -11
  26. package/dist/es2019/plugins/table/ui/common-styles.js +2 -2
  27. package/dist/es2019/plugins/table/ui/consts.js +3 -1
  28. package/dist/esm/plugins/table/event-handlers.js +10 -2
  29. package/dist/esm/plugins/table/nodeviews/ExternalDropTargets.js +65 -0
  30. package/dist/esm/plugins/table/nodeviews/TableComponent.js +76 -5
  31. package/dist/esm/plugins/table/nodeviews/TableRow.js +36 -112
  32. package/dist/esm/plugins/table/nodeviews/TableStickyScrollbar.js +9 -9
  33. package/dist/esm/plugins/table/pm-plugins/drag-and-drop/plugin.js +0 -8
  34. package/dist/esm/plugins/table/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.js +2 -1
  35. package/dist/esm/plugins/table/ui/TableFloatingColumnControls/index.js +2 -4
  36. package/dist/esm/plugins/table/ui/TableFloatingControls/RowControls/DragControls.js +62 -11
  37. package/dist/esm/plugins/table/ui/TableFloatingControls/RowDropTarget/index.js +52 -0
  38. package/dist/esm/plugins/table/ui/TableFloatingControls/index.js +21 -15
  39. package/dist/esm/plugins/table/ui/common-styles.js +2 -2
  40. package/dist/esm/plugins/table/ui/consts.js +3 -1
  41. package/dist/types/plugins/table/nodeviews/ExternalDropTargets.d.ts +9 -0
  42. package/dist/types/plugins/table/nodeviews/TableComponent.d.ts +1 -0
  43. package/dist/types/plugins/table/nodeviews/TableRow.d.ts +0 -4
  44. package/dist/types/plugins/table/nodeviews/TableStickyScrollbar.d.ts +3 -3
  45. package/dist/types/plugins/table/ui/TableFloatingControls/RowControls/DragControls.d.ts +1 -1
  46. package/dist/types/plugins/table/ui/TableFloatingControls/RowDropTarget/index.d.ts +8 -0
  47. package/dist/types/plugins/table/ui/TableFloatingControls/index.d.ts +3 -1
  48. package/dist/types/plugins/table/ui/consts.d.ts +2 -0
  49. package/dist/types/plugins/table/utils/column-controls.d.ts +2 -2
  50. package/dist/types-ts4.5/plugins/table/nodeviews/ExternalDropTargets.d.ts +9 -0
  51. package/dist/types-ts4.5/plugins/table/nodeviews/TableComponent.d.ts +1 -0
  52. package/dist/types-ts4.5/plugins/table/nodeviews/TableRow.d.ts +0 -4
  53. package/dist/types-ts4.5/plugins/table/nodeviews/TableStickyScrollbar.d.ts +3 -3
  54. package/dist/types-ts4.5/plugins/table/ui/TableFloatingControls/RowControls/DragControls.d.ts +1 -1
  55. package/dist/types-ts4.5/plugins/table/ui/TableFloatingControls/RowDropTarget/index.d.ts +8 -0
  56. package/dist/types-ts4.5/plugins/table/ui/TableFloatingControls/index.d.ts +3 -1
  57. package/dist/types-ts4.5/plugins/table/ui/consts.d.ts +2 -0
  58. package/dist/types-ts4.5/plugins/table/utils/column-controls.d.ts +2 -2
  59. package/package.json +3 -3
  60. package/src/__tests__/unit/ui/RowDragControls.tsx +2 -0
  61. package/src/plugins/table/event-handlers.ts +14 -1
  62. package/src/plugins/table/nodeviews/ExternalDropTargets.tsx +86 -0
  63. package/src/plugins/table/nodeviews/TableComponent.tsx +92 -7
  64. package/src/plugins/table/nodeviews/TableRow.ts +2 -65
  65. package/src/plugins/table/nodeviews/TableStickyScrollbar.ts +6 -6
  66. package/src/plugins/table/pm-plugins/drag-and-drop/plugin.ts +0 -7
  67. package/src/plugins/table/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.tsx +1 -0
  68. package/src/plugins/table/ui/TableFloatingColumnControls/index.tsx +2 -7
  69. package/src/plugins/table/ui/TableFloatingControls/RowControls/DragControls.tsx +80 -21
  70. package/src/plugins/table/ui/TableFloatingControls/RowDropTarget/index.tsx +62 -0
  71. package/src/plugins/table/ui/TableFloatingControls/index.tsx +29 -8
  72. package/src/plugins/table/ui/common-styles.ts +2 -1
  73. package/src/plugins/table/ui/consts.ts +3 -0
  74. package/src/plugins/table/utils/column-controls.ts +2 -2
  75. package/tsconfig.app.json +2 -2
@@ -1,5 +1,11 @@
1
1
  import type { MouseEvent } from 'react';
2
- import React, { useCallback, useMemo } from 'react';
2
+ import React, {
3
+ Fragment,
4
+ useCallback,
5
+ useEffect,
6
+ useMemo,
7
+ useState,
8
+ } from 'react';
3
9
 
4
10
  import { injectIntl } from 'react-intl-next';
5
11
  import type { WrappedComponentProps } from 'react-intl-next';
@@ -9,27 +15,32 @@ import type { Selection } from '@atlaskit/editor-prosemirror/state';
9
15
  import type { EditorView } from '@atlaskit/editor-prosemirror/view';
10
16
  import { CellSelection } from '@atlaskit/editor-tables';
11
17
  import { getSelectionRect } from '@atlaskit/editor-tables/utils';
18
+ import { monitorForElements } from '@atlaskit/pragmatic-drag-and-drop/adapter/element';
19
+ import { token } from '@atlaskit/tokens';
12
20
 
13
21
  import { clearHoverSelection } from '../../../commands';
14
22
  import { toggleDragMenu } from '../../../pm-plugins/drag-and-drop/commands';
15
- import type { CellHoverMeta } from '../../../types';
23
+ import { getPluginState as getTablePluginState } from '../../../pm-plugins/plugin-factory';
16
24
  import { TableCssClassName as ClassName } from '../../../types';
25
+ import type { CellHoverMeta, DraggableSourceData } from '../../../types';
17
26
  import {
18
27
  getRowHeights,
19
28
  getRowsParams,
20
29
  getSelectedRowIndexes,
21
30
  } from '../../../utils';
31
+ import { dropTargetExtendedWidth } from '../../consts';
22
32
  import { DragHandle } from '../../DragHandle';
33
+ import RowDropTarget from '../RowDropTarget';
23
34
 
24
35
  type DragControlsProps = {
25
36
  editorView: EditorView;
26
37
  tableRef: HTMLTableElement;
27
38
  tableNode?: PmNode;
39
+ tableWidth: number;
28
40
  tableActive?: boolean;
29
41
  hoveredCell?: CellHoverMeta;
30
42
  isInDanger?: boolean;
31
43
  isResizing?: boolean;
32
- hasHeaderRow?: boolean;
33
44
  hoverRows: (rows: number[], danger?: boolean) => void;
34
45
  selectRow: (row: number, expand: boolean) => void;
35
46
  updateCellHoverLocation: (rowIndex: number) => void;
@@ -49,21 +60,47 @@ const getSelectedRows = (selection: Selection) => {
49
60
  const DragControlsComponent = ({
50
61
  tableRef,
51
62
  tableNode,
63
+ tableWidth,
52
64
  hoveredCell,
53
65
  tableActive,
54
66
  editorView,
55
67
  isInDanger,
56
68
  isResizing,
57
- hasHeaderRow,
58
69
  hoverRows,
59
70
  selectRow,
60
71
  updateCellHoverLocation,
61
72
  }: DragControlsProps & WrappedComponentProps) => {
73
+ const [isDragging, setIsDragging] = useState(false);
74
+
62
75
  const rowHeights = getRowHeights(tableRef);
63
76
  const rowsParams = getRowsParams(rowHeights);
64
77
  const heights = rowHeights.map((height) => `${height - 1}px`).join(' ');
65
78
  const selectedRowIndexes = getSelectedRows(editorView.state.selection);
66
- const rowWidth = tableRef.offsetWidth;
79
+ const currentNodeLocalId = tableNode?.attrs?.localId ?? '';
80
+
81
+ useEffect(() => {
82
+ return monitorForElements({
83
+ canMonitor({ source }) {
84
+ const { type, localId, indexes } =
85
+ source.data as Partial<DraggableSourceData>;
86
+
87
+ if (!indexes || !localId || type !== 'table-row') {
88
+ return false;
89
+ }
90
+
91
+ const { tableNode } = getTablePluginState(editorView.state);
92
+ // If the draggable localId is the same as the current selected table localId then we will allow the monitor
93
+ // watch for changes
94
+ return localId === tableNode?.attrs.localId;
95
+ },
96
+ onDragStart() {
97
+ setIsDragging(true);
98
+ },
99
+ onDrop() {
100
+ setIsDragging(false);
101
+ },
102
+ });
103
+ }, [editorView]);
67
104
 
68
105
  const onMouseUp = useCallback(() => {
69
106
  toggleDragMenu(
@@ -125,23 +162,28 @@ const DragControlsComponent = ({
125
162
  className={ClassName.DRAG_ROW_CONTROLS}
126
163
  style={{
127
164
  gridTemplateRows: heights,
165
+ gridTemplateColumns: isDragging
166
+ ? `${dropTargetExtendedWidth}px 24px ${tableWidth}px`
167
+ : `${dropTargetExtendedWidth}px 24px`,
128
168
  }}
129
169
  onMouseMove={handleMouseMove}
130
170
  >
131
171
  {!isResizing &&
132
172
  rowsParams.map(({ startIndex, endIndex }, index) => (
133
- <div
134
- style={{
135
- gridRow: `${index + 1} / span 1`,
136
- }}
137
- data-start-index={startIndex}
138
- data-end-index={endIndex}
139
- className={ClassName.DRAG_ROW_FLOATING_INSERT_DOT_WRAPPER}
140
- contentEditable={false}
141
- key={index}
142
- >
143
- {/* TODO: Disabling first column insert button https://atlassian.slack.com/archives/C05U8HRQM50/p1698363744682219?thread_ts=1698209039.104909&cid=C05U8HRQM50 */}
144
- {/* {!hasHeaderRow && index === 0 && (
173
+ <Fragment key={index}>
174
+ <div
175
+ style={{
176
+ gridRow: `${index + 1} / span 1`,
177
+ gridColumn: '2',
178
+ }}
179
+ data-start-index={startIndex}
180
+ data-end-index={endIndex}
181
+ className={ClassName.DRAG_ROW_FLOATING_INSERT_DOT_WRAPPER}
182
+ contentEditable={false}
183
+ key={index}
184
+ >
185
+ {/* TODO: Disabling first column insert button https://atlassian.slack.com/archives/C05U8HRQM50/p1698363744682219?thread_ts=1698209039.104909&cid=C05U8HRQM50 */}
186
+ {/* {!hasHeaderRow && index === 0 && (
145
187
  <div
146
188
  style={{
147
189
  top: '0px',
@@ -150,13 +192,30 @@ const DragControlsComponent = ({
150
192
  className={ClassName.DRAG_ROW_FLOATING_INSERT_DOT}
151
193
  />
152
194
  )} */}
153
- <div className={ClassName.DRAG_ROW_FLOATING_INSERT_DOT} />
154
- </div>
195
+ <div className={ClassName.DRAG_ROW_FLOATING_INSERT_DOT} />
196
+ </div>
197
+ {isDragging && (
198
+ <RowDropTarget
199
+ key={index}
200
+ index={index}
201
+ localId={currentNodeLocalId}
202
+ style={{
203
+ gridColumn: '1 / span 3',
204
+ gridRow: `${index + 1} / span 1`,
205
+ height: '100%',
206
+ pointerEvents: 'auto',
207
+ position: 'relative',
208
+ left: token('space.negative.100', '-8px'),
209
+ }}
210
+ />
211
+ )}
212
+ </Fragment>
155
213
  ))}
156
214
  {!isResizing && Number.isFinite(rowIndex) && (
157
215
  <div
158
216
  style={{
159
217
  gridRow: gridRowPosition,
218
+ gridColumn: '2',
160
219
  display: 'flex',
161
220
  height: '100%',
162
221
  alignItems: 'center',
@@ -165,9 +224,9 @@ const DragControlsComponent = ({
165
224
  data-testid="table-floating-row-drag-handle"
166
225
  >
167
226
  <DragHandle
168
- tableLocalId={tableNode?.attrs?.localId ?? ''}
227
+ tableLocalId={currentNodeLocalId}
169
228
  indexes={[rowIndex!]}
170
- previewWidth={rowWidth}
229
+ previewWidth={tableWidth}
171
230
  previewHeight={rowHeights[rowIndex!]}
172
231
  appearance={
173
232
  selectedRowIndexes.includes(rowIndex!)
@@ -0,0 +1,62 @@
1
+ import React, { useEffect, useRef } from 'react';
2
+
3
+ import { dropTargetForElements } from '@atlaskit/pragmatic-drag-and-drop/adapter/element';
4
+ import { attachClosestEdge } from '@atlaskit/pragmatic-drag-and-drop-hitbox/addon/closest-edge';
5
+
6
+ import type { DraggableSourceData } from '../../../types';
7
+
8
+ export type RowDropTargetProps = {
9
+ index: number;
10
+ localId?: string;
11
+ style?: React.CSSProperties;
12
+ };
13
+
14
+ const RowDropTarget = ({ index, localId, style }: RowDropTargetProps) => {
15
+ const dropTargetRef = useRef<HTMLDivElement | null>(null);
16
+
17
+ useEffect(() => {
18
+ if (!dropTargetRef.current) {
19
+ return;
20
+ }
21
+
22
+ return dropTargetForElements({
23
+ element: dropTargetRef.current,
24
+ canDrop({ source }) {
25
+ const data = source.data as DraggableSourceData;
26
+ return (
27
+ // Only draggables of row type can be dropped on this target
28
+ data.type === 'table-row' &&
29
+ // Only draggables which came from the same table can be dropped on this target
30
+ data.localId === localId &&
31
+ // Only draggables which DO NOT include this drop targets index can be dropped
32
+ !!data.indexes?.length &&
33
+ data.indexes?.indexOf(index) === -1
34
+ );
35
+ },
36
+ getData({ input, element }) {
37
+ const data = {
38
+ localId,
39
+ type: 'table-row',
40
+ targetIndex: index,
41
+ };
42
+ return attachClosestEdge(data, {
43
+ input,
44
+ element,
45
+ allowedEdges: ['top', 'bottom'],
46
+ });
47
+ },
48
+ });
49
+ }, [index, localId]);
50
+
51
+ return (
52
+ <div
53
+ ref={dropTargetRef}
54
+ style={style}
55
+ data-drop-target-index={index}
56
+ data-drop-target-localid={localId}
57
+ data-testid="table-floating-row-controls-drop-target"
58
+ ></div>
59
+ );
60
+ };
61
+
62
+ export default RowDropTarget;
@@ -9,6 +9,7 @@ import type { EditorView } from '@atlaskit/editor-prosemirror/view';
9
9
  import { hoverCell, hoverRows, selectRow } from '../../commands';
10
10
  import { getPluginState } from '../../pm-plugins/plugin-factory';
11
11
  import type { RowStickyState } from '../../pm-plugins/sticky-headers';
12
+ import { TableCssClassName as ClassName } from '../../types';
12
13
  import type { CellHoverMeta } from '../../types';
13
14
  import { isSelectionUpdated } from '../../utils';
14
15
 
@@ -38,13 +39,24 @@ export interface Props {
38
39
  }
39
40
 
40
41
  interface State {
41
- tableHeight: number;
42
+ tableWrapperWidth: number;
43
+ tableWrapperHeight: number;
42
44
  }
43
45
 
44
46
  export default class TableFloatingControls extends Component<Props, State> {
45
47
  static displayName = 'TableFloatingControls';
46
48
  private resizeObserver?: ResizeObserver;
47
49
 
50
+ constructor(props: Props) {
51
+ super(props);
52
+
53
+ this.state = {
54
+ tableWrapperWidth: 0,
55
+ /** Height needs to be tracked to re-render decorations correctly, do not remove */
56
+ tableWrapperHeight: 0,
57
+ };
58
+ }
59
+
48
60
  componentDidMount() {
49
61
  this.tryInitResizeObserver();
50
62
  }
@@ -58,13 +70,23 @@ export default class TableFloatingControls extends Component<Props, State> {
58
70
  private tryInitResizeObserver() {
59
71
  let { tableRef } = this.props;
60
72
  if (tableRef && !this.resizeObserver && window?.ResizeObserver) {
73
+ const tableWrapper = tableRef.closest(
74
+ `.${ClassName.TABLE_NODE_WRAPPER}`,
75
+ ) as Element;
61
76
  this.resizeObserver = new ResizeObserver((entries) => {
62
77
  for (let entry of entries) {
63
- const tableHeight = entry.contentRect.height;
64
- this.setState({ tableHeight });
78
+ this.setState((prev) => {
79
+ return prev?.tableWrapperWidth === entry.contentRect.width &&
80
+ prev.tableWrapperHeight === entry.contentRect.height
81
+ ? prev
82
+ : {
83
+ tableWrapperWidth: entry.contentRect.width,
84
+ tableWrapperHeight: entry.contentRect.height,
85
+ };
86
+ });
65
87
  }
66
88
  });
67
- this.resizeObserver.observe(tableRef);
89
+ this.resizeObserver.observe(tableWrapper);
68
90
  }
69
91
  }
70
92
 
@@ -84,12 +106,11 @@ export default class TableFloatingControls extends Component<Props, State> {
84
106
  stickyHeader,
85
107
  hoveredCell,
86
108
  } = this.props;
87
- const tableHeight = this.state?.tableHeight;
88
- const nextTableHeight = nextState?.tableHeight;
89
109
  return (
110
+ this.state.tableWrapperWidth !== nextState.tableWrapperWidth ||
111
+ this.state.tableWrapperHeight !== nextState.tableWrapperHeight ||
90
112
  ordering !== nextProps.ordering ||
91
113
  tableRef !== nextProps.tableRef ||
92
- tableHeight !== nextTableHeight ||
93
114
  tableActive !== nextProps.tableActive ||
94
115
  isInDanger !== nextProps.isInDanger ||
95
116
  isResizing !== nextProps.isResizing ||
@@ -174,7 +195,7 @@ export default class TableFloatingControls extends Component<Props, State> {
174
195
  tableActive={tableActive}
175
196
  isInDanger={isInDanger}
176
197
  isResizing={isResizing}
177
- hasHeaderRow={hasHeaderRow}
198
+ tableWidth={this.state.tableWrapperWidth}
178
199
  hoverRows={this.hoverRows}
179
200
  selectRow={this.selectRow}
180
201
  updateCellHoverLocation={this.updateCellHoverLocation}
@@ -29,6 +29,7 @@ import { TableCssClassName as ClassName } from '../types';
29
29
 
30
30
  import {
31
31
  columnControlsDecorationHeight,
32
+ dropTargetExtendedWidth,
32
33
  resizeHandlerAreaWidth,
33
34
  resizeLineWidth,
34
35
  rowControlsZIndex,
@@ -712,7 +713,7 @@ export const tableStyles = (
712
713
  display: grid;
713
714
  align-items: center;
714
715
  position: absolute;
715
- left: -4px;
716
+ left: -${dropTargetExtendedWidth + 4}px;
716
717
  z-index: ${akEditorUnitZIndex};
717
718
 
718
719
  .${ClassName.DRAG_ROW_FLOATING_INSERT_DOT_WRAPPER} {
@@ -151,9 +151,12 @@ export const stickyHeaderBorderBottomWidth = 1;
151
151
  export const tableOverflowShadowWidth = 8;
152
152
  export const tableOverflowShadowWidthWide = 32;
153
153
 
154
+ export const dropTargetsZIndex = 14;
155
+
154
156
  export const TABLE_SNAP_GAP = 9;
155
157
  export const TABLE_HIGHLIGHT_GAP = 10;
156
158
  export const TABLE_HIGHLIGHT_TOLERANCE = 2;
157
159
  export const STICKY_HEADER_TOGGLE_TOLERANCE_MS = 5;
158
160
 
159
161
  export const dragMenuDropdownWidth = 240;
162
+ export const dropTargetExtendedWidth = 150;
@@ -1,7 +1,7 @@
1
1
  import { maphElem } from '@atlaskit/editor-common/utils';
2
- import { Selection } from '@atlaskit/editor-prosemirror/state';
2
+ import type { Selection } from '@atlaskit/editor-prosemirror/state';
3
3
  import { findDomRefAtPos } from '@atlaskit/editor-prosemirror/utils';
4
- import { EditorView } from '@atlaskit/editor-prosemirror/view';
4
+ import type { EditorView } from '@atlaskit/editor-prosemirror/view';
5
5
  import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
6
6
  import { TableMap } from '@atlaskit/editor-tables/table-map';
7
7
  import {
package/tsconfig.app.json CHANGED
@@ -73,10 +73,10 @@
73
73
  "path": "../../pragmatic-drag-and-drop/core/tsconfig.app.json"
74
74
  },
75
75
  {
76
- "path": "../../pragmatic-drag-and-drop/hitbox/tsconfig.app.json"
76
+ "path": "../../pragmatic-drag-and-drop/auto-scroll/tsconfig.app.json"
77
77
  },
78
78
  {
79
- "path": "../../pragmatic-drag-and-drop/react-beautiful-dnd-autoscroll/tsconfig.app.json"
79
+ "path": "../../pragmatic-drag-and-drop/hitbox/tsconfig.app.json"
80
80
  },
81
81
  {
82
82
  "path": "../../design-system/primitives/tsconfig.app.json"