@atlaskit/editor-plugin-table 2.10.1 → 2.10.3

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 (39) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/cjs/plugins/table/nodeviews/TableComponent.js +9 -3
  3. package/dist/cjs/plugins/table/nodeviews/TableContainer.js +28 -2
  4. package/dist/cjs/plugins/table/nodeviews/TableResizer.js +12 -4
  5. package/dist/cjs/plugins/table/nodeviews/table.js +4 -2
  6. package/dist/es2019/plugins/table/nodeviews/TableComponent.js +9 -3
  7. package/dist/es2019/plugins/table/nodeviews/TableContainer.js +29 -3
  8. package/dist/es2019/plugins/table/nodeviews/TableResizer.js +12 -4
  9. package/dist/es2019/plugins/table/nodeviews/table.js +3 -1
  10. package/dist/esm/plugins/table/nodeviews/TableComponent.js +9 -3
  11. package/dist/esm/plugins/table/nodeviews/TableContainer.js +29 -3
  12. package/dist/esm/plugins/table/nodeviews/TableResizer.js +12 -4
  13. package/dist/esm/plugins/table/nodeviews/table.js +4 -2
  14. package/dist/types/plugins/table/nodeviews/TableContainer.d.ts +4 -2
  15. package/dist/types/plugins/table/nodeviews/TableResizer.d.ts +3 -1
  16. package/dist/types/plugins/table/nodeviews/table.d.ts +4 -4
  17. package/dist/types-ts4.5/plugins/table/nodeviews/TableContainer.d.ts +4 -2
  18. package/dist/types-ts4.5/plugins/table/nodeviews/TableResizer.d.ts +3 -1
  19. package/dist/types-ts4.5/plugins/table/nodeviews/table.d.ts +4 -4
  20. package/package.json +3 -3
  21. package/src/__tests__/unit/commands/go-to-next-cell.ts +5 -5
  22. package/src/__tests__/unit/commands/insert.ts +5 -5
  23. package/src/__tests__/unit/commands/misc.ts +6 -8
  24. package/src/__tests__/unit/copy-paste.ts +10 -13
  25. package/src/__tests__/unit/event-handlers/index.ts +6 -6
  26. package/src/__tests__/unit/handlers.ts +6 -5
  27. package/src/__tests__/unit/hover-selection.ts +7 -6
  28. package/src/__tests__/unit/layout.ts +6 -6
  29. package/src/__tests__/unit/pm-plugins/decorations/column-resizing.ts +23 -23
  30. package/src/__tests__/unit/sort-column.ts +5 -5
  31. package/src/__tests__/unit/transforms/delete-columns.ts +6 -6
  32. package/src/__tests__/unit/transforms/delete-rows.ts +6 -6
  33. package/src/__tests__/unit/transforms/merging.ts +5 -5
  34. package/src/__tests__/unit/ui/TableFloatingControls.tsx +5 -5
  35. package/src/__tests__/unit/utils.ts +5 -5
  36. package/src/plugins/table/nodeviews/TableComponent.tsx +20 -3
  37. package/src/plugins/table/nodeviews/TableContainer.tsx +48 -3
  38. package/src/plugins/table/nodeviews/TableResizer.tsx +24 -4
  39. package/src/plugins/table/nodeviews/table.tsx +14 -7
@@ -8,15 +8,15 @@ import { contentInsertionPlugin } from '@atlaskit/editor-plugin-content-insertio
8
8
  import featureFlagsPlugin from '@atlaskit/editor-plugin-feature-flags';
9
9
  import { guidelinePlugin } from '@atlaskit/editor-plugin-guideline';
10
10
  import { widthPlugin } from '@atlaskit/editor-plugin-width';
11
- import { PluginKey } from '@atlaskit/editor-prosemirror/state';
11
+ import type { PluginKey } from '@atlaskit/editor-prosemirror/state';
12
+ import type { LightEditorPlugin } from '@atlaskit/editor-test-helpers/create-prosemirror-editor';
12
13
  import {
13
14
  createProsemirrorEditorFactory,
14
- LightEditorPlugin,
15
15
  Preset,
16
16
  } from '@atlaskit/editor-test-helpers/create-prosemirror-editor';
17
+ import type { DocBuilder } from '@atlaskit/editor-test-helpers/doc-builder';
17
18
  import {
18
19
  doc,
19
- DocBuilder,
20
20
  p,
21
21
  table,
22
22
  tdCursor,
@@ -28,7 +28,7 @@ import tablePlugin from '../../../plugins/table-plugin';
28
28
  import { hoverTable } from '../../../plugins/table/commands';
29
29
  import { getDecorations } from '../../../plugins/table/pm-plugins/decorations/plugin';
30
30
  import { pluginKey } from '../../../plugins/table/pm-plugins/plugin-key';
31
- import { TablePluginState } from '../../../plugins/table/types';
31
+ import type { TablePluginState } from '../../../plugins/table/types';
32
32
  import TableFloatingControls from '../../../plugins/table/ui/TableFloatingControls';
33
33
 
34
34
  describe('TableFloatingControls', () => {
@@ -43,7 +43,7 @@ describe('TableFloatingControls', () => {
43
43
  .add(tablePlugin);
44
44
 
45
45
  const editor = (doc: DocBuilder) =>
46
- createEditor<TablePluginState, PluginKey>({
46
+ createEditor<TablePluginState, PluginKey, typeof preset>({
47
47
  doc,
48
48
  preset,
49
49
  pluginKey: pluginKey,
@@ -3,15 +3,15 @@ import { contentInsertionPlugin } from '@atlaskit/editor-plugin-content-insertio
3
3
  import featureFlagsPlugin from '@atlaskit/editor-plugin-feature-flags';
4
4
  import { guidelinePlugin } from '@atlaskit/editor-plugin-guideline';
5
5
  import { widthPlugin } from '@atlaskit/editor-plugin-width';
6
- import { PluginKey } from '@atlaskit/editor-prosemirror/state';
6
+ import type { PluginKey } from '@atlaskit/editor-prosemirror/state';
7
+ import type { LightEditorPlugin } from '@atlaskit/editor-test-helpers/create-prosemirror-editor';
7
8
  import {
8
9
  createProsemirrorEditorFactory,
9
- LightEditorPlugin,
10
10
  Preset,
11
11
  } from '@atlaskit/editor-test-helpers/create-prosemirror-editor';
12
+ import type { DocBuilder } from '@atlaskit/editor-test-helpers/doc-builder';
12
13
  import {
13
14
  doc,
14
- DocBuilder,
15
15
  p,
16
16
  table,
17
17
  td,
@@ -22,7 +22,7 @@ import {
22
22
 
23
23
  import tablePlugin from '../../plugins/table';
24
24
  import { pluginKey } from '../../plugins/table/pm-plugins/plugin-key';
25
- import { TablePluginState } from '../../plugins/table/types';
25
+ import type { TablePluginState } from '../../plugins/table/types';
26
26
  import {
27
27
  getColumnClassNames,
28
28
  getColumnDeleteButtonParams,
@@ -46,7 +46,7 @@ describe('table plugin: utils', () => {
46
46
  .add(tablePlugin);
47
47
 
48
48
  const editor = (doc: DocBuilder) =>
49
- createEditor<TablePluginState, PluginKey>({
49
+ createEditor<TablePluginState, PluginKey, typeof preset>({
50
50
  doc,
51
51
  preset,
52
52
  pluginKey,
@@ -184,8 +184,14 @@ class TableComponent extends React.Component<ComponentProps, TableState> {
184
184
  }
185
185
 
186
186
  componentDidUpdate() {
187
- const { view, getNode, isMediaFullscreen, allowColumnResizing } =
188
- this.props;
187
+ const {
188
+ view,
189
+ getNode,
190
+ isMediaFullscreen,
191
+ allowColumnResizing,
192
+ isResizing,
193
+ options,
194
+ } = this.props;
189
195
  const { isInDanger } = getPluginState(view.state);
190
196
  const table = findTable(view.state.selection);
191
197
 
@@ -227,7 +233,17 @@ class TableComponent extends React.Component<ComponentProps, TableState> {
227
233
  tablesHaveDifferentNoOfColumns(currentTable, previousTable)
228
234
  ) {
229
235
  const { view } = this.props;
230
- recreateResizeColsByNode(this.table, currentTable);
236
+
237
+ const shouldRecreateResizeCols =
238
+ !options?.isTableResizingEnabled ||
239
+ !isResizing ||
240
+ (tablesHaveDifferentNoOfColumns(currentTable, previousTable) &&
241
+ isResizing);
242
+
243
+ if (shouldRecreateResizeCols) {
244
+ recreateResizeColsByNode(this.table, currentTable);
245
+ }
246
+
231
247
  updateControls()(view.state);
232
248
  }
233
249
 
@@ -345,6 +361,7 @@ class TableComponent extends React.Component<ComponentProps, TableState> {
345
361
  isNested={isNested}
346
362
  pluginInjectionApi={pluginInjectionApi}
347
363
  isTableResizingEnabled={options?.isTableResizingEnabled}
364
+ isResizing={isResizing}
348
365
  >
349
366
  <div
350
367
  className={ClassName.TABLE_STICKY_SENTINEL_TOP}
@@ -10,7 +10,10 @@ import { calcTableWidth } from '@atlaskit/editor-common/styles';
10
10
  import type { EditorContainerWidth } from '@atlaskit/editor-common/types';
11
11
  import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
12
12
  import type { EditorView } from '@atlaskit/editor-prosemirror/view';
13
- import { akEditorMobileBreakoutPoint } from '@atlaskit/editor-shared-styles';
13
+ import {
14
+ akEditorDefaultLayoutWidth,
15
+ akEditorMobileBreakoutPoint,
16
+ } from '@atlaskit/editor-shared-styles';
14
17
 
15
18
  import { TABLE_MAX_WIDTH } from '../pm-plugins/table-resizing/utils';
16
19
  import type { PluginInjectionAPI } from '../types';
@@ -66,6 +69,7 @@ type ResizableTableContainerProps = {
66
69
  editorView: EditorView;
67
70
  getPos: () => number | undefined;
68
71
  tableRef: HTMLTableElement;
72
+ isResizing?: boolean;
69
73
  pluginInjectionApi?: PluginInjectionAPI;
70
74
  };
71
75
 
@@ -78,10 +82,39 @@ export const ResizableTableContainer = ({
78
82
  editorView,
79
83
  getPos,
80
84
  tableRef,
85
+ isResizing,
81
86
  pluginInjectionApi,
82
87
  }: PropsWithChildren<ResizableTableContainerProps>) => {
83
88
  const containerRef = useRef<HTMLDivElement | null>(null);
84
89
  const marginLeftRef = useRef<number | undefined>(0);
90
+ const tableWidthRef = useRef<number>(akEditorDefaultLayoutWidth);
91
+
92
+ const updateContainerHeight = useCallback((height: number | 'auto') => {
93
+ containerRef.current?.style.setProperty(
94
+ 'height',
95
+ typeof height === 'number' ? `${height + 29}px` : 'auto',
96
+ );
97
+ }, []);
98
+
99
+ const resizeObserverRef = useRef(
100
+ new ResizeObserver((entries) => {
101
+ updateContainerHeight(entries[entries.length - 1].contentRect.height);
102
+ }),
103
+ );
104
+
105
+ const onResizeStart = useCallback(() => {
106
+ updateContainerHeight(tableRef?.clientHeight);
107
+
108
+ if (tableRef) {
109
+ resizeObserverRef.current.observe(tableRef);
110
+ }
111
+ }, [tableRef, updateContainerHeight]);
112
+
113
+ const onResizeStop = useCallback(() => {
114
+ updateContainerHeight('auto');
115
+
116
+ resizeObserverRef.current.disconnect();
117
+ }, [updateContainerHeight]);
85
118
 
86
119
  const updateWidth = useCallback(
87
120
  (width: number) => {
@@ -134,16 +167,25 @@ export const ResizableTableContainer = ({
134
167
  // 76 is currently an accepted padding value considering the spacing for resizer handle
135
168
  const responsiveContainerWidth = containerWidth - 76;
136
169
  const width = Math.min(tableWidth, responsiveContainerWidth);
137
- marginLeftRef.current = getMarginLeft(lineLength, width);
170
+
171
+ if (!isResizing) {
172
+ tableWidthRef.current = width;
173
+ marginLeftRef.current = getMarginLeft(lineLength, width);
174
+ }
138
175
  const maxResizerWidth = Math.min(responsiveContainerWidth, TABLE_MAX_WIDTH);
139
176
 
140
177
  return (
141
178
  <div
142
- style={{ marginLeft: marginLeftRef.current, width }}
179
+ style={{
180
+ marginLeft: marginLeftRef.current,
181
+ width: tableWidthRef.current,
182
+ }}
143
183
  className={ClassName.TABLE_RESIZER_CONTAINER}
144
184
  ref={containerRef}
145
185
  >
146
186
  <TableResizer
187
+ onResizeStart={onResizeStart}
188
+ onResizeStop={onResizeStop}
147
189
  width={width}
148
190
  maxWidth={maxResizerWidth}
149
191
  containerWidth={containerWidth}
@@ -173,6 +215,7 @@ type TableContainerProps = {
173
215
  getPos: () => number | undefined;
174
216
  tableRef: HTMLTableElement;
175
217
  isNested: boolean;
218
+ isResizing?: boolean;
176
219
  pluginInjectionApi?: PluginInjectionAPI;
177
220
  };
178
221
 
@@ -187,6 +230,7 @@ export const TableContainer = ({
187
230
  getPos,
188
231
  tableRef,
189
232
  isNested,
233
+ isResizing,
190
234
  pluginInjectionApi,
191
235
  }: PropsWithChildren<TableContainerProps>) => {
192
236
  if (isTableResizingEnabled && !isNested) {
@@ -199,6 +243,7 @@ export const TableContainer = ({
199
243
  editorView={editorView}
200
244
  getPos={getPos}
201
245
  tableRef={tableRef}
246
+ isResizing={isResizing}
202
247
  pluginInjectionApi={pluginInjectionApi}
203
248
  >
204
249
  {children}
@@ -43,6 +43,8 @@ interface TableResizerProps {
43
43
  maxWidth: number;
44
44
  containerWidth: number;
45
45
  updateWidth: (width: number) => void;
46
+ onResizeStop: () => void;
47
+ onResizeStart: () => void;
46
48
  editorView: EditorView;
47
49
  getPos: () => number | undefined;
48
50
  node: PMNode;
@@ -57,9 +59,14 @@ const handles = { right: true };
57
59
  const tableHandleMarginTop = 12;
58
60
  const tableHandlePosition = 14;
59
61
 
60
- const getResizerHandleHeight = (tableRef: HTMLTableElement) => {
62
+ const getResizerHandleHeight = (tableRef: HTMLTableElement | undefined) => {
61
63
  const tableHeight = tableRef?.clientHeight;
62
64
  let handleHeightSize: HandleHeightSizeType | undefined = 'small';
65
+
66
+ if (!tableHeight) {
67
+ return handleHeightSize;
68
+ }
69
+
63
70
  /*
64
71
  - One row table height (minimum possible table height) ~ 45px
65
72
  - Two row table height ~ 90px
@@ -69,9 +76,9 @@ const getResizerHandleHeight = (tableRef: HTMLTableElement) => {
69
76
  - > 46 because the height of the table can be a float number like 45.44.
70
77
  - < 96 is the height of large resize handle.
71
78
  */
72
- if (tableHeight && tableHeight > 46 && tableHeight < 96) {
79
+ if (tableHeight > 46 && tableHeight < 96) {
73
80
  handleHeightSize = 'medium';
74
- } else if (tableHeight && tableHeight >= 96) {
81
+ } else if (tableHeight >= 96) {
75
82
  handleHeightSize = 'large';
76
83
  }
77
84
 
@@ -115,6 +122,8 @@ export const TableResizer = ({
115
122
  maxWidth,
116
123
  containerWidth,
117
124
  updateWidth,
125
+ onResizeStop,
126
+ onResizeStart,
118
127
  editorView,
119
128
  getPos,
120
129
  node,
@@ -177,7 +186,15 @@ export const TableResizer = ({
177
186
  containerWidth,
178
187
  );
179
188
  setSnappingEnabled(displayGuideline(visibleGuidelines));
180
- }, [displayGuideline, containerWidth, editorView, startMeasure]);
189
+
190
+ onResizeStart();
191
+ }, [
192
+ displayGuideline,
193
+ editorView,
194
+ startMeasure,
195
+ onResizeStart,
196
+ containerWidth,
197
+ ]);
181
198
 
182
199
  const handleResize = useCallback(
183
200
  (originalState, delta) => {
@@ -281,6 +298,8 @@ export const TableResizer = ({
281
298
  updateWidth(newWidth);
282
299
  scheduleResize.cancel();
283
300
 
301
+ onResizeStop();
302
+
284
303
  return newWidth;
285
304
  },
286
305
  [
@@ -293,6 +312,7 @@ export const TableResizer = ({
293
312
  displayGuideline,
294
313
  attachAnalyticsEvent,
295
314
  endMeasure,
315
+ onResizeStop,
296
316
  ],
297
317
  );
298
318
 
@@ -12,13 +12,16 @@ import type {
12
12
  getPosHandlerNode,
13
13
  } from '@atlaskit/editor-common/types';
14
14
  import { WithPluginState } from '@atlaskit/editor-common/with-plugin-state';
15
- import {
15
+ import type {
16
16
  DOMOutputSpec,
17
- DOMSerializer,
18
17
  Node as PmNode,
19
18
  } from '@atlaskit/editor-prosemirror/model';
20
- import { EditorState, PluginKey } from '@atlaskit/editor-prosemirror/state';
21
- import { EditorView, NodeView } from '@atlaskit/editor-prosemirror/view';
19
+ import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
20
+ import type {
21
+ EditorState,
22
+ PluginKey,
23
+ } from '@atlaskit/editor-prosemirror/state';
24
+ import type { EditorView, NodeView } from '@atlaskit/editor-prosemirror/view';
22
25
  import { akEditorTableNumberColumnWidth } from '@atlaskit/editor-shared-styles';
23
26
  import { TableMap } from '@atlaskit/editor-tables/table-map';
24
27
 
@@ -28,11 +31,11 @@ import { pluginKey } from '../pm-plugins/plugin-key';
28
31
  import { pluginKey as tableResizingPluginKey } from '../pm-plugins/table-resizing';
29
32
  import { generateColgroup } from '../pm-plugins/table-resizing/utils';
30
33
  import { pluginKey as tableWidthPluginKey } from '../pm-plugins/table-width';
31
- import { PluginInjectionAPI } from '../types';
34
+ import type { PluginInjectionAPI } from '../types';
32
35
  import { isTableNested } from '../utils';
33
36
 
34
37
  import TableComponent from './TableComponent';
35
- import { Props, TableOptions } from './types';
38
+ import type { Props, TableOptions } from './types';
36
39
 
37
40
  type ForwardRef = (node: HTMLElement | null) => void;
38
41
 
@@ -160,7 +163,11 @@ export default class TableView extends ReactNodeView<Props> {
160
163
  this.getPos(),
161
164
  );
162
165
 
163
- if (tableInlineWidth) {
166
+ const isTableResizing = tableWidthPluginKey.getState(
167
+ this.view.state,
168
+ )?.resizing;
169
+
170
+ if (!isTableResizing && tableInlineWidth) {
164
171
  handleInlineTableWidth(this.table, tableInlineWidth);
165
172
  }
166
173
  }