@atlaskit/editor-plugin-table 2.5.5 → 2.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 (30) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/cjs/plugins/table/nodeviews/TableComponent.js +14 -1
  3. package/dist/cjs/plugins/table/nodeviews/TableContainer.js +6 -2
  4. package/dist/cjs/plugins/table/nodeviews/TableResizer.js +49 -18
  5. package/dist/cjs/plugins/table/nodeviews/table.js +2 -1
  6. package/dist/cjs/plugins/table/pm-plugins/table-resizing/utils/scale-table.js +8 -5
  7. package/dist/cjs/version.json +1 -1
  8. package/dist/es2019/plugins/table/nodeviews/TableComponent.js +14 -1
  9. package/dist/es2019/plugins/table/nodeviews/TableContainer.js +6 -2
  10. package/dist/es2019/plugins/table/nodeviews/TableResizer.js +48 -19
  11. package/dist/es2019/plugins/table/nodeviews/table.js +2 -1
  12. package/dist/es2019/plugins/table/pm-plugins/table-resizing/utils/scale-table.js +8 -5
  13. package/dist/es2019/version.json +1 -1
  14. package/dist/esm/plugins/table/nodeviews/TableComponent.js +14 -1
  15. package/dist/esm/plugins/table/nodeviews/TableContainer.js +6 -2
  16. package/dist/esm/plugins/table/nodeviews/TableResizer.js +50 -19
  17. package/dist/esm/plugins/table/nodeviews/table.js +2 -1
  18. package/dist/esm/plugins/table/pm-plugins/table-resizing/utils/scale-table.js +8 -5
  19. package/dist/esm/version.json +1 -1
  20. package/dist/types/plugins/table/nodeviews/TableResizer.d.ts +4 -1
  21. package/dist/types/plugins/table/pm-plugins/table-resizing/utils/scale-table.d.ts +1 -0
  22. package/dist/types-ts4.5/plugins/table/nodeviews/TableResizer.d.ts +4 -1
  23. package/dist/types-ts4.5/plugins/table/pm-plugins/table-resizing/utils/scale-table.d.ts +1 -0
  24. package/package.json +2 -2
  25. package/src/__tests__/unit/nodeviews/TableContainer.tsx +74 -11
  26. package/src/plugins/table/nodeviews/TableComponent.tsx +19 -1
  27. package/src/plugins/table/nodeviews/TableContainer.tsx +11 -4
  28. package/src/plugins/table/nodeviews/TableResizer.tsx +94 -32
  29. package/src/plugins/table/nodeviews/table.tsx +6 -1
  30. package/src/plugins/table/pm-plugins/table-resizing/utils/scale-table.ts +20 -12
@@ -7,9 +7,16 @@ import React, {
7
7
  } from 'react';
8
8
 
9
9
  import { Node as PMNode } from 'prosemirror-model';
10
+ import { Transaction } from 'prosemirror-state';
10
11
  import { EditorView } from 'prosemirror-view';
11
12
  import rafSchd from 'raf-schd';
12
13
 
14
+ import {
15
+ ACTION_SUBJECT,
16
+ EVENT_TYPE,
17
+ TABLE_ACTION,
18
+ TableEventPayload,
19
+ } from '@atlaskit/editor-common/analytics';
13
20
  import { getGuidelinesWithHighlights } from '@atlaskit/editor-common/guideline';
14
21
  import {
15
22
  HandleHeightSizeType,
@@ -17,18 +24,20 @@ import {
17
24
  ResizerNext,
18
25
  } from '@atlaskit/editor-common/resizer';
19
26
  import type { GuidelineConfig } from '@atlaskit/editor-plugin-guideline';
27
+ import { TableMap } from '@atlaskit/editor-tables';
20
28
 
21
29
  import {
22
30
  COLUMN_MIN_WIDTH,
23
31
  getColgroupChildrenLength,
32
+ hasTableBeenResized,
24
33
  previewScaleTable,
25
34
  scaleTable,
26
35
  } from '../pm-plugins/table-resizing/utils';
27
36
  import { pluginKey as tableWidthPluginKey } from '../pm-plugins/table-width';
28
37
  import { TABLE_HIGHLIGHT_GAP, TABLE_SNAP_GAP } from '../ui/consts';
38
+ import { getTableWidth } from '../utils';
29
39
  import { defaultGuidelines, defaultGuidelineWidths } from '../utils/guidelines';
30
40
  import { findClosestSnap } from '../utils/snapping';
31
-
32
41
  interface TableResizerProps {
33
42
  width: number;
34
43
  maxWidth: number;
@@ -38,11 +47,37 @@ interface TableResizerProps {
38
47
  node: PMNode;
39
48
  tableRef: HTMLTableElement;
40
49
  displayGuideline: (guideline: GuidelineConfig[]) => boolean;
50
+ attachAnalyticsEvent: (
51
+ payload: TableEventPayload,
52
+ ) => ((tr: Transaction) => boolean) | undefined;
41
53
  }
42
54
 
43
55
  const handles = { right: true };
44
56
  const tableHandleMarginTop = 11;
45
57
 
58
+ const generateResizedPayload = (props: {
59
+ originalNode: PMNode;
60
+ resizedNode: PMNode;
61
+ }): TableEventPayload => {
62
+ const tableMap = TableMap.get(props.resizedNode);
63
+
64
+ return {
65
+ action: TABLE_ACTION.RESIZED,
66
+ actionSubject: ACTION_SUBJECT.TABLE,
67
+ eventType: EVENT_TYPE.TRACK,
68
+ attributes: {
69
+ newWidth: props.resizedNode.attrs.width,
70
+ prevWidth: props.originalNode.attrs.width ?? null,
71
+ nodeSize: props.resizedNode.nodeSize,
72
+ totalTableWidth: hasTableBeenResized(props.resizedNode)
73
+ ? getTableWidth(props.resizedNode)
74
+ : null,
75
+ totalRowCount: tableMap.height,
76
+ totalColumnCount: tableMap.width,
77
+ },
78
+ };
79
+ };
80
+
46
81
  export const TableResizer = ({
47
82
  children,
48
83
  width,
@@ -53,6 +88,7 @@ export const TableResizer = ({
53
88
  node,
54
89
  tableRef,
55
90
  displayGuideline,
91
+ attachAnalyticsEvent,
56
92
  }: PropsWithChildren<TableResizerProps>) => {
57
93
  const currentColumnCount = getColgroupChildrenLength(node);
58
94
  const minColumnWidth =
@@ -111,6 +147,7 @@ export const TableResizer = ({
111
147
  dispatch,
112
148
  state: { tr },
113
149
  } = editorView;
150
+
114
151
  dispatch(tr.setMeta(tableWidthPluginKey, { resizing: true }));
115
152
 
116
153
  setSnappingEnabled(displayGuideline(defaultGuidelines));
@@ -142,19 +179,73 @@ export const TableResizer = ({
142
179
  },
143
180
  editorView.domAtPos.bind(editorView),
144
181
  )(tr);
182
+
183
+ const scaledNode = tr.doc.nodeAt(pos)!;
184
+
185
+ attachAnalyticsEvent(
186
+ generateResizedPayload({
187
+ originalNode: node,
188
+ resizedNode: scaledNode,
189
+ }),
190
+ )?.(tr);
145
191
  }
146
192
 
147
193
  dispatch(tr);
148
194
 
149
195
  // Hide guidelines when resizing stops
150
196
  displayGuideline([]);
197
+ updateWidth(newWidth);
198
+
199
+ return newWidth;
200
+ },
201
+ [
202
+ updateWidth,
203
+ editorView,
204
+ getPos,
205
+ node,
206
+ tableRef,
207
+ displayGuideline,
208
+ attachAnalyticsEvent,
209
+ ],
210
+ );
211
+
212
+ const handleResize = useCallback(
213
+ (originalState, delta) => {
214
+ const newWidth = originalState.width + delta.width;
215
+ const pos = getPos();
216
+ if (typeof pos !== 'number') {
217
+ return;
218
+ }
219
+
220
+ previewScaleTable(
221
+ tableRef,
222
+ {
223
+ node,
224
+ prevNode: node,
225
+ start: pos + 1,
226
+ parentWidth: newWidth,
227
+ },
228
+ editorView.domAtPos.bind(editorView),
229
+ );
230
+
231
+ updateActiveGuidelines(
232
+ findClosestSnap(
233
+ newWidth,
234
+ defaultGuidelineWidths,
235
+ defaultGuidelines,
236
+ TABLE_HIGHLIGHT_GAP,
237
+ ),
238
+ );
151
239
 
152
240
  updateWidth(newWidth);
241
+
153
242
  return newWidth;
154
243
  },
155
- [updateWidth, editorView, getPos, node, tableRef, displayGuideline],
244
+ [editorView, getPos, node, tableRef, updateWidth, updateActiveGuidelines],
156
245
  );
157
246
 
247
+ const scheduleResize = useMemo(() => rafSchd(handleResize), [handleResize]);
248
+
158
249
  return (
159
250
  <ResizerNext
160
251
  enable={handles}
@@ -163,36 +254,7 @@ export const TableResizer = ({
163
254
  handleHeightSize={handleHeightSize}
164
255
  handleMarginTop={tableHandleMarginTop}
165
256
  handleResizeStart={handleResizeStart}
166
- handleResize={rafSchd((originalState, delta) => {
167
- const newWidth = originalState.width + delta.width;
168
- const pos = getPos();
169
- if (typeof pos !== 'number') {
170
- return;
171
- }
172
-
173
- previewScaleTable(
174
- tableRef,
175
- {
176
- node,
177
- prevNode: node,
178
- start: pos + 1,
179
- parentWidth: newWidth,
180
- },
181
- editorView.domAtPos.bind(editorView),
182
- );
183
-
184
- updateActiveGuidelines(
185
- findClosestSnap(
186
- newWidth,
187
- defaultGuidelineWidths,
188
- defaultGuidelines,
189
- TABLE_HIGHLIGHT_GAP,
190
- ),
191
- );
192
-
193
- updateWidth(newWidth);
194
- return newWidth;
195
- })}
257
+ handleResize={scheduleResize}
196
258
  handleResizeStop={handleResizeStop}
197
259
  resizeRatio={2}
198
260
  minWidth={minColumnWidth}
@@ -20,6 +20,7 @@ import type {
20
20
  getPosHandlerNode,
21
21
  } from '@atlaskit/editor-common/types';
22
22
  import { WithPluginState } from '@atlaskit/editor-common/with-plugin-state';
23
+ import { akEditorTableNumberColumnWidth } from '@atlaskit/editor-shared-styles';
23
24
  import { TableMap } from '@atlaskit/editor-tables/table-map';
24
25
 
25
26
  import { pluginConfig as getPluginConfig } from '../create-plugin-config';
@@ -49,7 +50,11 @@ const tableAttributes = (
49
50
  options?.isTableResizingEnabled && !isTableNested(state, pos);
50
51
 
51
52
  let style = shouldHaveInlineWidth
52
- ? `width: ${getTableContainerWidth(node)}px`
53
+ ? `width: ${
54
+ node.attrs.isNumberColumnEnabled
55
+ ? getTableContainerWidth(node) - akEditorTableNumberColumnWidth
56
+ : getTableContainerWidth(node)
57
+ }px`
53
58
  : undefined;
54
59
 
55
60
  return {
@@ -2,6 +2,7 @@ import { Node as PMNode } from 'prosemirror-model';
2
2
  import { Transaction } from 'prosemirror-state';
3
3
  import type { DomAtPos } from 'prosemirror-utils';
4
4
 
5
+ import { getTableContainerWidth } from '@atlaskit/editor-common/node-width';
5
6
  import { tableCellMinWidth } from '@atlaskit/editor-common/styles';
6
7
  import { akEditorTableNumberColumnWidth } from '@atlaskit/editor-shared-styles';
7
8
 
@@ -29,6 +30,7 @@ export interface ScaleOptions {
29
30
  layoutChanged?: boolean;
30
31
  isBreakoutEnabled?: boolean;
31
32
  isFullWidthModeEnabled?: boolean;
33
+ isTableResizingEnabled?: boolean;
32
34
  }
33
35
 
34
36
  // Base function to trigger the actual scale on a table node.
@@ -51,24 +53,27 @@ export const scale = (
51
53
  start,
52
54
  isBreakoutEnabled,
53
55
  layoutChanged,
56
+ isTableResizingEnabled,
54
57
  } = options;
55
58
 
56
- const maxSize = getLayoutSize(node.attrs.layout, containerWidth, {
57
- isBreakoutEnabled,
58
- });
59
+ const maxSize = isTableResizingEnabled
60
+ ? getTableContainerWidth(node)
61
+ : getLayoutSize(node.attrs.layout, containerWidth, {
62
+ isBreakoutEnabled,
63
+ });
64
+
59
65
  const prevTableWidth = getTableWidth(prevNode);
60
- const previousMaxSize = getLayoutSize(
61
- prevNode.attrs.layout,
62
- previousContainerWidth,
63
- {
64
- isBreakoutEnabled,
65
- },
66
- );
66
+ const previousMaxSize = isTableResizingEnabled
67
+ ? getTableContainerWidth(node)
68
+ : getLayoutSize(prevNode.attrs.layout, previousContainerWidth, {
69
+ isBreakoutEnabled,
70
+ });
67
71
 
68
72
  let newWidth = maxSize;
69
73
 
70
74
  // adjust table width if layout is updated
71
75
  const hasOverflow = prevTableWidth > previousMaxSize;
76
+
72
77
  if (layoutChanged && hasOverflow) {
73
78
  // No keep overflow if the old content can be in the new size
74
79
  const canFitInNewSize = prevTableWidth < maxSize;
@@ -157,7 +162,11 @@ export const previewScaleTable = (
157
162
  }
158
163
 
159
164
  if (parentWidth) {
160
- tableRef.style.width = `${parentWidth}px`;
165
+ const isNumberColumnEnabled = node.attrs.isNumberColumnEnabled;
166
+ const width = isNumberColumnEnabled
167
+ ? parentWidth - akEditorTableNumberColumnWidth
168
+ : parentWidth;
169
+ tableRef.style.width = `${width}px`;
161
170
  }
162
171
 
163
172
  if (!hasTableBeenResized(node)) {
@@ -186,7 +195,6 @@ export const scaleTable =
186
195
  }
187
196
 
188
197
  const { node, start, parentWidth, layoutChanged } = options;
189
-
190
198
  // If a table has not been resized yet, columns should be auto.
191
199
  if (hasTableBeenResized(node) === false) {
192
200
  // If its not a re-sized table, we still want to re-create cols