@atlaskit/editor-plugin-table 5.4.15 → 5.4.17

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 (41) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/cjs/plugins/table/commands/hover.js +4 -2
  3. package/dist/cjs/plugins/table/nodeviews/TableComponent.js +1 -1
  4. package/dist/cjs/plugins/table/nodeviews/TableResizer.js +2 -2
  5. package/dist/cjs/plugins/table/ui/DragHandle/HandleIconComponent.js +3 -5
  6. package/dist/cjs/plugins/table/ui/DragHandle/index.js +3 -0
  7. package/dist/cjs/plugins/table/ui/FloatingDragMenu/DragMenu.js +2 -2
  8. package/dist/cjs/plugins/table/ui/TableFloatingColumnControls/ColumnControls/index.js +62 -33
  9. package/dist/cjs/plugins/table/ui/TableFloatingControls/RowControls/DragControls.js +62 -31
  10. package/dist/es2019/plugins/table/commands/hover.js +4 -2
  11. package/dist/es2019/plugins/table/nodeviews/TableComponent.js +1 -1
  12. package/dist/es2019/plugins/table/nodeviews/TableResizer.js +2 -2
  13. package/dist/es2019/plugins/table/ui/DragHandle/HandleIconComponent.js +3 -5
  14. package/dist/es2019/plugins/table/ui/DragHandle/index.js +2 -0
  15. package/dist/es2019/plugins/table/ui/FloatingDragMenu/DragMenu.js +2 -2
  16. package/dist/es2019/plugins/table/ui/TableFloatingColumnControls/ColumnControls/index.js +63 -33
  17. package/dist/es2019/plugins/table/ui/TableFloatingControls/RowControls/DragControls.js +61 -29
  18. package/dist/esm/plugins/table/commands/hover.js +4 -2
  19. package/dist/esm/plugins/table/nodeviews/TableComponent.js +1 -1
  20. package/dist/esm/plugins/table/nodeviews/TableResizer.js +2 -2
  21. package/dist/esm/plugins/table/ui/DragHandle/HandleIconComponent.js +3 -5
  22. package/dist/esm/plugins/table/ui/DragHandle/index.js +3 -0
  23. package/dist/esm/plugins/table/ui/FloatingDragMenu/DragMenu.js +2 -2
  24. package/dist/esm/plugins/table/ui/TableFloatingColumnControls/ColumnControls/index.js +62 -33
  25. package/dist/esm/plugins/table/ui/TableFloatingControls/RowControls/DragControls.js +60 -29
  26. package/dist/types/plugins/table/types.d.ts +2 -0
  27. package/dist/types/plugins/table/ui/DragHandle/HandleIconComponent.d.ts +1 -0
  28. package/dist/types/plugins/table/ui/DragHandle/index.d.ts +2 -1
  29. package/dist/types-ts4.5/plugins/table/types.d.ts +2 -0
  30. package/dist/types-ts4.5/plugins/table/ui/DragHandle/HandleIconComponent.d.ts +1 -0
  31. package/dist/types-ts4.5/plugins/table/ui/DragHandle/index.d.ts +2 -1
  32. package/package.json +1 -1
  33. package/src/plugins/table/commands/hover.ts +2 -0
  34. package/src/plugins/table/nodeviews/TableComponent.tsx +1 -1
  35. package/src/plugins/table/nodeviews/TableResizer.tsx +4 -2
  36. package/src/plugins/table/types.ts +3 -0
  37. package/src/plugins/table/ui/DragHandle/HandleIconComponent.tsx +10 -9
  38. package/src/plugins/table/ui/DragHandle/index.tsx +4 -0
  39. package/src/plugins/table/ui/FloatingDragMenu/DragMenu.tsx +3 -2
  40. package/src/plugins/table/ui/TableFloatingColumnControls/ColumnControls/index.tsx +118 -48
  41. package/src/plugins/table/ui/TableFloatingControls/RowControls/DragControls.tsx +126 -41
@@ -9,6 +9,7 @@ import {
9
9
  type HandleIconProps = {
10
10
  hasMergedCells: boolean;
11
11
  direction: 'row' | 'column';
12
+ forceDefaultHandle: boolean;
12
13
  isDragMenuOpen: boolean | undefined;
13
14
  isRowHandleHovered: boolean;
14
15
  isColumnHandleHovered: boolean;
@@ -20,6 +21,7 @@ type HandleIconProps = {
20
21
  export const HandleIconComponent = (props: HandleIconProps) => {
21
22
  const {
22
23
  direction,
24
+ forceDefaultHandle,
23
25
  isDragMenuOpen,
24
26
  isRowHandleHovered,
25
27
  isColumnHandleHovered,
@@ -29,22 +31,21 @@ export const HandleIconComponent = (props: HandleIconProps) => {
29
31
  dragMenuDirection,
30
32
  } = props;
31
33
  const isHandleHovered = isRowHandleHovered || isColumnHandleHovered;
34
+
32
35
  const isCurrentRowOrColumnSelected =
33
36
  isCurrentRowSelected || isCurrentColumnSelected;
37
+
34
38
  const isDragMenuOpenOnCurrentRowOrColumn =
35
39
  isDragMenuOpen &&
36
40
  dragMenuDirection === direction &&
37
41
  isCurrentRowOrColumnSelected;
38
42
 
39
- const showNormalHandle = hasMergedCells ? (
40
- <DragHandleDisabledIcon />
41
- ) : (
42
- <DragHandleIcon />
43
- );
44
-
45
- // hoverred handle or open drag menu
46
- if (isHandleHovered || isDragMenuOpenOnCurrentRowOrColumn) {
47
- return showNormalHandle;
43
+ if (
44
+ forceDefaultHandle ||
45
+ isHandleHovered ||
46
+ isDragMenuOpenOnCurrentRowOrColumn
47
+ ) {
48
+ return hasMergedCells ? <DragHandleDisabledIcon /> : <DragHandleIcon />;
48
49
  }
49
50
 
50
51
  return <MinimisedHandleIcon />;
@@ -22,6 +22,7 @@ type DragHandleAppearance = 'default' | 'selected' | 'disabled' | 'danger';
22
22
  type DragHandleProps = {
23
23
  tableLocalId: string;
24
24
  indexes: number[];
25
+ forceDefaultHandle?: boolean;
25
26
  previewWidth?: number;
26
27
  previewHeight?: number;
27
28
  direction?: TableDirection;
@@ -38,6 +39,7 @@ export const DragHandle = ({
38
39
  direction = 'row',
39
40
  appearance = 'default',
40
41
  indexes,
42
+ forceDefaultHandle = false,
41
43
  previewWidth,
42
44
  previewHeight,
43
45
  onMouseOver,
@@ -63,6 +65,7 @@ export const DragHandle = ({
63
65
  isDragMenuOpen &&
64
66
  direction === 'row' &&
65
67
  hoveredCell.rowIndex === dragMenuIndex;
68
+
66
69
  const isCurrentColumnSelected =
67
70
  isDragMenuOpen &&
68
71
  direction === 'column' &&
@@ -83,6 +86,7 @@ export const DragHandle = ({
83
86
  const handleIconProps = {
84
87
  hasMergedCells,
85
88
  direction,
89
+ forceDefaultHandle,
86
90
  isDragMenuOpen,
87
91
  isRowHandleHovered,
88
92
  isColumnHandleHovered,
@@ -131,12 +131,13 @@ export const DragMenu = ({
131
131
  const hasMergedCells =
132
132
  direction === 'row' ? hasMergedCellsInRow : hasMergedCellsInColumn;
133
133
 
134
- const mergedCells = index !== undefined && hasMergedCells(index)(selection);
134
+ const shouldMoveDisabled =
135
+ index !== undefined && hasMergedCells(index)(selection);
135
136
 
136
137
  const dragMenuConfig = getDragMenuConfig(
137
138
  direction,
138
139
  getEditorContainerWidth,
139
- mergedCells,
140
+ shouldMoveDisabled,
140
141
  tableMap,
141
142
  index,
142
143
  targetCellPosition,
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @atlaskit/design-system/prefer-primitives */
1
2
  import type { MouseEvent } from 'react';
2
3
  import React, { useCallback, useMemo } from 'react';
3
4
 
@@ -13,7 +14,8 @@ import {
13
14
  selectColumn,
14
15
  } from '../../../commands';
15
16
  import { toggleDragMenu } from '../../../pm-plugins/drag-and-drop/commands';
16
- import type { CellHoverMeta } from '../../../types';
17
+ import { getPluginState as getDragDropPluginState } from '../../../pm-plugins/drag-and-drop/plugin-factory';
18
+ import type { CellHoverMeta, HandleTypes } from '../../../types';
17
19
  import { TableCssClassName as ClassName } from '../../../types';
18
20
  import { getRowsParams, getSelectedColumnIndexes } from '../../../utils';
19
21
  import { DragHandle } from '../../DragHandle';
@@ -66,14 +68,6 @@ export const ColumnControls = ({
66
68
  const colIndex = hoveredCell?.colIndex;
67
69
  const selectedColIndexes = getSelectedColumns(editorView.state.selection);
68
70
 
69
- const gridColumnPosition = useMemo(() => {
70
- // if more than one row is selected, ensure the handle spans over the selected range
71
- if (selectedColIndexes.includes(colIndex!)) {
72
- return `${selectedColIndexes[0] + 1} / span ${selectedColIndexes.length}`;
73
- }
74
- return `${colIndex! + 1} / span 1`;
75
- }, [colIndex, selectedColIndexes]);
76
-
77
71
  const firstRow = tableRef.querySelector('tr');
78
72
  const hasHeaderRow = firstRow
79
73
  ? firstRow.getAttribute('data-header-row')
@@ -140,6 +134,120 @@ export const ColumnControls = ({
140
134
 
141
135
  const previewHeight = rowHeights?.reduce((sum, cur) => sum + cur, 0) ?? 0;
142
136
 
137
+ const generateHandleByType = useCallback(
138
+ (type: HandleTypes): JSX.Element | null => {
139
+ if (!hoveredCell) {
140
+ return null;
141
+ }
142
+ const { isDragMenuOpen, dragMenuIndex, dragMenuDirection } =
143
+ getDragDropPluginState(editorView.state);
144
+ const isHover = type === 'hover';
145
+
146
+ const isHoveredOnSelectedColumn =
147
+ isDragMenuOpen &&
148
+ dragMenuDirection === 'column' &&
149
+ dragMenuIndex === colIndex;
150
+
151
+ const showCondition = isHover
152
+ ? !isHoveredOnSelectedColumn && Number.isFinite(hoveredCell?.colIndex)
153
+ : isDragMenuOpen && dragMenuDirection === 'column';
154
+
155
+ if (!showCondition) {
156
+ return null;
157
+ }
158
+
159
+ const selectedColumnPosition = `${dragMenuIndex + 1} / span ${
160
+ selectedColIndexes.length
161
+ }`;
162
+
163
+ const gridColumnPosition = selectedColIndexes.includes(colIndex!)
164
+ ? `${selectedColIndexes[0] + 1} / span ${selectedColIndexes.length}`
165
+ : `${colIndex! + 1} / span 1`;
166
+
167
+ const selectedApprearance = isInDanger ? 'danger' : 'selected';
168
+ const hoveredAppearance = selectedColIndexes.includes(colIndex!)
169
+ ? isInDanger
170
+ ? 'danger'
171
+ : 'selected'
172
+ : 'default';
173
+
174
+ return (
175
+ showCondition && (
176
+ <div
177
+ key={type}
178
+ style={{
179
+ gridColumn: isHover ? gridColumnPosition : selectedColumnPosition,
180
+ display: 'flex',
181
+ justifyContent: 'center',
182
+ alignItems: 'center',
183
+ height: 'fit-content',
184
+ placeSelf: 'center',
185
+ zIndex: 99,
186
+ }}
187
+ data-column-control-index={hoveredCell.colIndex}
188
+ data-testid={
189
+ isHover
190
+ ? 'table-floating-column-control'
191
+ : 'table-floating-column-control-selected'
192
+ }
193
+ >
194
+ <DragHandle
195
+ direction="column"
196
+ tableLocalId={localId || ''}
197
+ indexes={isHover ? colIndexes : selectedColIndexes}
198
+ forceDefaultHandle={!isHover}
199
+ previewWidth={colWidths?.[colIndex!] ?? 48}
200
+ previewHeight={previewHeight}
201
+ appearance={isHover ? hoveredAppearance : selectedApprearance}
202
+ onClick={handleClick}
203
+ onMouseOver={handleMouseOver}
204
+ onMouseOut={handleMouseOut}
205
+ onMouseUp={handleMouseUp}
206
+ editorView={editorView}
207
+ />
208
+ </div>
209
+ )
210
+ );
211
+ },
212
+ [
213
+ colIndex,
214
+ previewHeight,
215
+ colWidths,
216
+ colIndexes,
217
+ editorView,
218
+ handleClick,
219
+ handleMouseOut,
220
+ handleMouseOver,
221
+ handleMouseUp,
222
+ hoveredCell,
223
+ isInDanger,
224
+ localId,
225
+ selectedColIndexes,
226
+ ],
227
+ );
228
+
229
+ const columnHandles = useCallback(
230
+ (hoveredCell: CellHoverMeta | undefined) => {
231
+ if (!hoveredCell) {
232
+ return null;
233
+ }
234
+
235
+ if (hoveredCell.colIndex === undefined) {
236
+ return generateHandleByType('selected');
237
+ }
238
+
239
+ const sortedHandles = [
240
+ generateHandleByType('hover'),
241
+ generateHandleByType('selected'),
242
+ ];
243
+
244
+ return hoveredCell.colIndex < selectedColIndexes[0]
245
+ ? sortedHandles
246
+ : sortedHandles.reverse();
247
+ },
248
+ [generateHandleByType, selectedColIndexes],
249
+ );
250
+
143
251
  return (
144
252
  <div
145
253
  className={ClassName.DRAG_COLUMN_CONTROLS}
@@ -181,45 +289,7 @@ export const ColumnControls = ({
181
289
  />
182
290
  </div>
183
291
  ))}
184
- {tableActive &&
185
- !isResizing &&
186
- isTableHovered &&
187
- !!hoveredCell &&
188
- Number.isFinite(hoveredCell.colIndex) && (
189
- <div
190
- style={{
191
- gridColumn: gridColumnPosition,
192
- display: 'flex',
193
- justifyContent: 'center',
194
- alignItems: 'center',
195
- height: 'fit-content',
196
- placeSelf: 'center',
197
- zIndex: 99,
198
- }}
199
- data-column-control-index={hoveredCell.colIndex}
200
- data-testid="table-floating-column-control"
201
- >
202
- <DragHandle
203
- direction="column"
204
- tableLocalId={localId || ''}
205
- indexes={colIndexes}
206
- previewWidth={colWidths?.[colIndex!] ?? 48}
207
- previewHeight={previewHeight}
208
- appearance={
209
- selectedColIndexes.includes(hoveredCell.colIndex!)
210
- ? isInDanger
211
- ? 'danger'
212
- : 'selected'
213
- : 'default'
214
- }
215
- onClick={handleClick}
216
- onMouseOver={handleMouseOver}
217
- onMouseOut={handleMouseOut}
218
- onMouseUp={handleMouseUp}
219
- editorView={editorView}
220
- />
221
- </div>
222
- )}
292
+ {tableActive && !isResizing && columnHandles(hoveredCell)}
223
293
  </div>
224
294
  </div>
225
295
  );
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @atlaskit/design-system/prefer-primitives */
1
2
  import type { MouseEvent } from 'react';
2
3
  import React, {
3
4
  Fragment,
@@ -20,9 +21,14 @@ import { token } from '@atlaskit/tokens';
20
21
 
21
22
  import { clearHoverSelection } from '../../../commands';
22
23
  import { toggleDragMenu } from '../../../pm-plugins/drag-and-drop/commands';
24
+ import { getPluginState as getDragDropPluginState } from '../../../pm-plugins/drag-and-drop/plugin-factory';
23
25
  import { getPluginState as getTablePluginState } from '../../../pm-plugins/plugin-factory';
24
26
  import { TableCssClassName as ClassName } from '../../../types';
25
- import type { CellHoverMeta, DraggableSourceData } from '../../../types';
27
+ import type {
28
+ CellHoverMeta,
29
+ DraggableSourceData,
30
+ HandleTypes,
31
+ } from '../../../types';
26
32
  import {
27
33
  getRowHeights,
28
34
  getRowsParams,
@@ -114,14 +120,6 @@ const DragControlsComponent = ({
114
120
 
115
121
  const rowIndex = hoveredCell?.rowIndex;
116
122
 
117
- const gridRowPosition = useMemo(() => {
118
- // if more than one row is selected, ensure the handle spans over the selected range
119
- if (selectedRowIndexes.includes(rowIndex!)) {
120
- return `${selectedRowIndexes[0] + 1} / span ${selectedRowIndexes.length}`;
121
- }
122
- return `${rowIndex! + 1} / span 1`;
123
- }, [rowIndex, selectedRowIndexes]);
124
-
125
123
  const handleMouseOut = useCallback(() => {
126
124
  if (tableActive) {
127
125
  const { state, dispatch } = editorView;
@@ -163,6 +161,124 @@ const DragControlsComponent = ({
163
161
  [rowIndex, selectRow],
164
162
  );
165
163
 
164
+ const generateHandleByType = useCallback(
165
+ (type: HandleTypes): JSX.Element | null => {
166
+ if (!hoveredCell) {
167
+ return null;
168
+ }
169
+ const { isDragMenuOpen, dragMenuIndex, dragMenuDirection } =
170
+ getDragDropPluginState(editorView.state);
171
+ const isHover = type === 'hover';
172
+
173
+ const isHoveredOnSelectedRow =
174
+ isDragMenuOpen &&
175
+ dragMenuDirection === 'row' &&
176
+ dragMenuIndex === rowIndex;
177
+
178
+ const showCondition = isHover
179
+ ? !isHoveredOnSelectedRow &&
180
+ !selectedRowIndexes.includes(rowIndex!) &&
181
+ Number.isFinite(hoveredCell?.colIndex)
182
+ : isDragMenuOpen &&
183
+ dragMenuDirection === 'row' &&
184
+ Number.isFinite(hoveredCell?.colIndex);
185
+
186
+ if (!showCondition) {
187
+ return null;
188
+ }
189
+
190
+ const gridRowPosition =
191
+ // if more than one row is selected, ensure the handle spans over the selected range
192
+ selectedRowIndexes.includes(rowIndex!)
193
+ ? `${selectedRowIndexes[0] + 1} / span ${selectedRowIndexes.length}`
194
+ : `${rowIndex! + 1} / span 1`;
195
+
196
+ const selectedRowPosition = `${dragMenuIndex + 1} / span ${
197
+ selectedRowIndexes.length
198
+ }`;
199
+
200
+ const selectedApprearance = isInDanger ? 'danger' : 'selected';
201
+ const hoveredAppearance = selectedRowIndexes.includes(rowIndex!)
202
+ ? isInDanger
203
+ ? 'danger'
204
+ : 'selected'
205
+ : 'default';
206
+
207
+ return (
208
+ showCondition && (
209
+ <div
210
+ key={type}
211
+ style={{
212
+ gridRow: isHover ? gridRowPosition : selectedRowPosition,
213
+ gridColumn: '2',
214
+ display: 'flex',
215
+ height: '100%',
216
+ alignItems: 'center',
217
+ justifyContent: 'center',
218
+ }}
219
+ data-testid={
220
+ isHover
221
+ ? 'table-floating-row-drag-handle'
222
+ : 'table-floating-row-control-selected'
223
+ }
224
+ >
225
+ <DragHandle
226
+ direction="row"
227
+ tableLocalId={currentNodeLocalId}
228
+ indexes={isHover ? rowIndexes : selectedRowIndexes}
229
+ forceDefaultHandle={!isHover}
230
+ previewWidth={tableWidth}
231
+ previewHeight={rowHeights[rowIndex!]}
232
+ appearance={isHover ? hoveredAppearance : selectedApprearance}
233
+ onClick={handleClick}
234
+ onMouseOver={handleMouseOver}
235
+ onMouseOut={handleMouseOut}
236
+ onMouseUp={onMouseUp}
237
+ editorView={editorView}
238
+ />
239
+ </div>
240
+ )
241
+ );
242
+ },
243
+ [
244
+ currentNodeLocalId,
245
+ editorView,
246
+ handleClick,
247
+ handleMouseOut,
248
+ handleMouseOver,
249
+ hoveredCell,
250
+ isInDanger,
251
+ onMouseUp,
252
+ rowHeights,
253
+ rowIndex,
254
+ rowIndexes,
255
+ selectedRowIndexes,
256
+ tableWidth,
257
+ ],
258
+ );
259
+
260
+ const rowHandles = useCallback(
261
+ (hoveredCell: CellHoverMeta | undefined) => {
262
+ if (!hoveredCell) {
263
+ return null;
264
+ }
265
+
266
+ if (hoveredCell.rowIndex === undefined) {
267
+ return generateHandleByType('selected');
268
+ }
269
+
270
+ const sortedHandles = [
271
+ generateHandleByType('hover'),
272
+ generateHandleByType('selected'),
273
+ ];
274
+
275
+ return hoveredCell.rowIndex < selectedRowIndexes[0]
276
+ ? sortedHandles
277
+ : sortedHandles.reverse();
278
+ },
279
+ [generateHandleByType, selectedRowIndexes],
280
+ );
281
+
166
282
  return (
167
283
  <div
168
284
  className={ClassName.DRAG_ROW_CONTROLS}
@@ -219,38 +335,7 @@ const DragControlsComponent = ({
219
335
  )}
220
336
  </Fragment>
221
337
  ))}
222
- {!isResizing && isTableHovered && Number.isFinite(rowIndex) && (
223
- <div
224
- style={{
225
- gridRow: gridRowPosition,
226
- gridColumn: '2',
227
- display: 'flex',
228
- height: '100%',
229
- alignItems: 'center',
230
- justifyContent: 'center',
231
- }}
232
- data-testid="table-floating-row-drag-handle"
233
- >
234
- <DragHandle
235
- tableLocalId={currentNodeLocalId}
236
- indexes={rowIndexes}
237
- previewWidth={tableWidth}
238
- previewHeight={rowHeights[rowIndex!]}
239
- appearance={
240
- selectedRowIndexes.includes(rowIndex!)
241
- ? isInDanger
242
- ? 'danger'
243
- : 'selected'
244
- : 'default'
245
- }
246
- onClick={handleClick}
247
- onMouseOver={handleMouseOver}
248
- onMouseOut={handleMouseOut}
249
- onMouseUp={onMouseUp}
250
- editorView={editorView}
251
- />
252
- </div>
253
- )}
338
+ {!isResizing && Number.isFinite(rowIndex) && rowHandles(hoveredCell)}
254
339
  </div>
255
340
  );
256
341
  };