@atlaskit/editor-plugin-table 7.4.9 → 7.5.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 (184) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/commands/column-resize.js +4 -2
  3. package/dist/cjs/commands/delete.js +2 -1
  4. package/dist/cjs/commands/insert.js +6 -3
  5. package/dist/cjs/commands-with-analytics.js +4 -2
  6. package/dist/cjs/event-handlers.js +2 -1
  7. package/dist/cjs/nodeviews/TableComponent.js +116 -47
  8. package/dist/cjs/nodeviews/TableContainer.js +8 -4
  9. package/dist/cjs/nodeviews/TableResizer.js +6 -5
  10. package/dist/cjs/nodeviews/table.js +12 -4
  11. package/dist/cjs/plugin.js +50 -47
  12. package/dist/cjs/pm-plugins/drag-and-drop/plugin.js +9 -6
  13. package/dist/cjs/pm-plugins/keymap.js +6 -3
  14. package/dist/cjs/pm-plugins/main.js +4 -1
  15. package/dist/cjs/pm-plugins/table-resizing/event-handlers.js +7 -3
  16. package/dist/cjs/pm-plugins/table-resizing/utils/colgroup.js +32 -11
  17. package/dist/cjs/pm-plugins/table-resizing/utils/consts.js +4 -2
  18. package/dist/cjs/pm-plugins/table-resizing/utils/index.js +24 -0
  19. package/dist/cjs/pm-plugins/table-resizing/utils/misc.js +20 -1
  20. package/dist/cjs/pm-plugins/table-resizing/utils/resize-column.js +2 -1
  21. package/dist/cjs/pm-plugins/table-resizing/utils/resize-state.js +52 -11
  22. package/dist/cjs/pm-plugins/table-resizing/utils/scale-table.js +19 -8
  23. package/dist/cjs/toolbar.js +17 -10
  24. package/dist/cjs/transforms/column-width.js +22 -7
  25. package/dist/cjs/transforms/delete-columns.js +2 -1
  26. package/dist/cjs/ui/FloatingContextualMenu/ContextualMenu.js +45 -38
  27. package/dist/cjs/ui/FloatingContextualMenu/index.js +4 -2
  28. package/dist/cjs/ui/FloatingDragMenu/DragMenu.js +3 -2
  29. package/dist/cjs/ui/FloatingDragMenu/index.js +7 -2
  30. package/dist/cjs/ui/FloatingInsertButton/index.js +5 -1
  31. package/dist/cjs/utils/create.js +1 -1
  32. package/dist/cjs/utils/drag-menu.js +4 -3
  33. package/dist/es2019/commands/column-resize.js +4 -3
  34. package/dist/es2019/commands/delete.js +2 -2
  35. package/dist/es2019/commands/insert.js +6 -6
  36. package/dist/es2019/commands-with-analytics.js +4 -4
  37. package/dist/es2019/event-handlers.js +2 -2
  38. package/dist/es2019/nodeviews/TableComponent.js +77 -8
  39. package/dist/es2019/nodeviews/TableContainer.js +8 -4
  40. package/dist/es2019/nodeviews/TableResizer.js +6 -5
  41. package/dist/es2019/nodeviews/table.js +12 -4
  42. package/dist/es2019/plugin.js +14 -8
  43. package/dist/es2019/pm-plugins/drag-and-drop/plugin.js +7 -4
  44. package/dist/es2019/pm-plugins/keymap.js +6 -3
  45. package/dist/es2019/pm-plugins/main.js +4 -1
  46. package/dist/es2019/pm-plugins/table-resizing/event-handlers.js +7 -3
  47. package/dist/es2019/pm-plugins/table-resizing/utils/colgroup.js +31 -12
  48. package/dist/es2019/pm-plugins/table-resizing/utils/consts.js +3 -1
  49. package/dist/es2019/pm-plugins/table-resizing/utils/index.js +2 -2
  50. package/dist/es2019/pm-plugins/table-resizing/utils/misc.js +20 -2
  51. package/dist/es2019/pm-plugins/table-resizing/utils/resize-column.js +2 -2
  52. package/dist/es2019/pm-plugins/table-resizing/utils/resize-state.js +47 -10
  53. package/dist/es2019/pm-plugins/table-resizing/utils/scale-table.js +19 -12
  54. package/dist/es2019/toolbar.js +15 -12
  55. package/dist/es2019/transforms/column-width.js +24 -10
  56. package/dist/es2019/transforms/delete-columns.js +2 -2
  57. package/dist/es2019/ui/FloatingContextualMenu/ContextualMenu.js +12 -5
  58. package/dist/es2019/ui/FloatingContextualMenu/index.js +4 -2
  59. package/dist/es2019/ui/FloatingDragMenu/DragMenu.js +3 -2
  60. package/dist/es2019/ui/FloatingDragMenu/index.js +7 -2
  61. package/dist/es2019/ui/FloatingInsertButton/index.js +5 -1
  62. package/dist/es2019/utils/create.js +1 -1
  63. package/dist/es2019/utils/drag-menu.js +4 -4
  64. package/dist/esm/commands/column-resize.js +4 -2
  65. package/dist/esm/commands/delete.js +2 -1
  66. package/dist/esm/commands/insert.js +6 -3
  67. package/dist/esm/commands-with-analytics.js +4 -2
  68. package/dist/esm/event-handlers.js +2 -1
  69. package/dist/esm/nodeviews/TableComponent.js +118 -49
  70. package/dist/esm/nodeviews/TableContainer.js +8 -4
  71. package/dist/esm/nodeviews/TableResizer.js +6 -5
  72. package/dist/esm/nodeviews/table.js +12 -4
  73. package/dist/esm/plugin.js +50 -47
  74. package/dist/esm/pm-plugins/drag-and-drop/plugin.js +9 -6
  75. package/dist/esm/pm-plugins/keymap.js +6 -3
  76. package/dist/esm/pm-plugins/main.js +4 -1
  77. package/dist/esm/pm-plugins/table-resizing/event-handlers.js +7 -3
  78. package/dist/esm/pm-plugins/table-resizing/utils/colgroup.js +32 -11
  79. package/dist/esm/pm-plugins/table-resizing/utils/consts.js +3 -1
  80. package/dist/esm/pm-plugins/table-resizing/utils/index.js +2 -2
  81. package/dist/esm/pm-plugins/table-resizing/utils/misc.js +22 -2
  82. package/dist/esm/pm-plugins/table-resizing/utils/resize-column.js +2 -1
  83. package/dist/esm/pm-plugins/table-resizing/utils/resize-state.js +52 -11
  84. package/dist/esm/pm-plugins/table-resizing/utils/scale-table.js +19 -8
  85. package/dist/esm/toolbar.js +17 -10
  86. package/dist/esm/transforms/column-width.js +23 -8
  87. package/dist/esm/transforms/delete-columns.js +2 -1
  88. package/dist/esm/ui/FloatingContextualMenu/ContextualMenu.js +45 -38
  89. package/dist/esm/ui/FloatingContextualMenu/index.js +4 -2
  90. package/dist/esm/ui/FloatingDragMenu/DragMenu.js +3 -2
  91. package/dist/esm/ui/FloatingDragMenu/index.js +7 -2
  92. package/dist/esm/ui/FloatingInsertButton/index.js +5 -1
  93. package/dist/esm/utils/create.js +1 -1
  94. package/dist/esm/utils/drag-menu.js +4 -3
  95. package/dist/types/commands/column-resize.d.ts +1 -1
  96. package/dist/types/commands/delete.d.ts +1 -1
  97. package/dist/types/commands/insert.d.ts +3 -3
  98. package/dist/types/commands-with-analytics.d.ts +2 -2
  99. package/dist/types/event-handlers.d.ts +1 -1
  100. package/dist/types/nodeviews/TableComponent.d.ts +3 -0
  101. package/dist/types/nodeviews/TableContainer.d.ts +4 -2
  102. package/dist/types/nodeviews/TableResizer.d.ts +2 -1
  103. package/dist/types/nodeviews/table.d.ts +1 -0
  104. package/dist/types/pm-plugins/drag-and-drop/plugin.d.ts +3 -2
  105. package/dist/types/pm-plugins/keymap.d.ts +2 -2
  106. package/dist/types/pm-plugins/table-resizing/utils/colgroup.d.ts +2 -2
  107. package/dist/types/pm-plugins/table-resizing/utils/consts.d.ts +2 -0
  108. package/dist/types/pm-plugins/table-resizing/utils/index.d.ts +2 -2
  109. package/dist/types/pm-plugins/table-resizing/utils/misc.d.ts +7 -0
  110. package/dist/types/pm-plugins/table-resizing/utils/resize-column.d.ts +1 -1
  111. package/dist/types/pm-plugins/table-resizing/utils/resize-state.d.ts +4 -3
  112. package/dist/types/pm-plugins/table-resizing/utils/scale-table.d.ts +4 -4
  113. package/dist/types/toolbar.d.ts +2 -2
  114. package/dist/types/transforms/column-width.d.ts +1 -1
  115. package/dist/types/transforms/delete-columns.d.ts +1 -1
  116. package/dist/types/ui/FloatingContextualMenu/ContextualMenu.d.ts +2 -1
  117. package/dist/types/ui/FloatingContextualMenu/index.d.ts +1 -1
  118. package/dist/types/ui/FloatingDragMenu/DragMenu.d.ts +2 -1
  119. package/dist/types/ui/FloatingDragMenu/index.d.ts +3 -2
  120. package/dist/types/ui/FloatingInsertButton/index.d.ts +2 -1
  121. package/dist/types/utils/drag-menu.d.ts +1 -1
  122. package/dist/types-ts4.5/commands/column-resize.d.ts +1 -1
  123. package/dist/types-ts4.5/commands/delete.d.ts +1 -1
  124. package/dist/types-ts4.5/commands/insert.d.ts +3 -3
  125. package/dist/types-ts4.5/commands-with-analytics.d.ts +2 -2
  126. package/dist/types-ts4.5/event-handlers.d.ts +1 -1
  127. package/dist/types-ts4.5/nodeviews/TableComponent.d.ts +3 -0
  128. package/dist/types-ts4.5/nodeviews/TableContainer.d.ts +4 -2
  129. package/dist/types-ts4.5/nodeviews/TableResizer.d.ts +2 -1
  130. package/dist/types-ts4.5/nodeviews/table.d.ts +1 -0
  131. package/dist/types-ts4.5/pm-plugins/drag-and-drop/plugin.d.ts +3 -2
  132. package/dist/types-ts4.5/pm-plugins/keymap.d.ts +2 -2
  133. package/dist/types-ts4.5/pm-plugins/table-resizing/utils/colgroup.d.ts +2 -2
  134. package/dist/types-ts4.5/pm-plugins/table-resizing/utils/consts.d.ts +2 -0
  135. package/dist/types-ts4.5/pm-plugins/table-resizing/utils/index.d.ts +2 -2
  136. package/dist/types-ts4.5/pm-plugins/table-resizing/utils/misc.d.ts +7 -0
  137. package/dist/types-ts4.5/pm-plugins/table-resizing/utils/resize-column.d.ts +1 -1
  138. package/dist/types-ts4.5/pm-plugins/table-resizing/utils/resize-state.d.ts +4 -3
  139. package/dist/types-ts4.5/pm-plugins/table-resizing/utils/scale-table.d.ts +4 -4
  140. package/dist/types-ts4.5/toolbar.d.ts +2 -2
  141. package/dist/types-ts4.5/transforms/column-width.d.ts +1 -1
  142. package/dist/types-ts4.5/transforms/delete-columns.d.ts +1 -1
  143. package/dist/types-ts4.5/ui/FloatingContextualMenu/ContextualMenu.d.ts +2 -1
  144. package/dist/types-ts4.5/ui/FloatingContextualMenu/index.d.ts +1 -1
  145. package/dist/types-ts4.5/ui/FloatingDragMenu/DragMenu.d.ts +2 -1
  146. package/dist/types-ts4.5/ui/FloatingDragMenu/index.d.ts +3 -2
  147. package/dist/types-ts4.5/ui/FloatingInsertButton/index.d.ts +2 -1
  148. package/dist/types-ts4.5/utils/drag-menu.d.ts +1 -1
  149. package/docs/0-intro.tsx +3 -2
  150. package/package.json +2 -2
  151. package/src/commands/column-resize.ts +3 -0
  152. package/src/commands/delete.ts +2 -1
  153. package/src/commands/insert.ts +15 -6
  154. package/src/commands-with-analytics.ts +9 -3
  155. package/src/event-handlers.ts +2 -0
  156. package/src/nodeviews/TableComponent.tsx +91 -5
  157. package/src/nodeviews/TableContainer.tsx +7 -1
  158. package/src/nodeviews/TableResizer.tsx +7 -1
  159. package/src/nodeviews/table.tsx +15 -10
  160. package/src/plugin.tsx +17 -5
  161. package/src/pm-plugins/drag-and-drop/plugin.ts +16 -8
  162. package/src/pm-plugins/keymap.ts +11 -3
  163. package/src/pm-plugins/main.ts +3 -0
  164. package/src/pm-plugins/table-resizing/event-handlers.ts +11 -1
  165. package/src/pm-plugins/table-resizing/utils/colgroup.ts +49 -17
  166. package/src/pm-plugins/table-resizing/utils/consts.ts +2 -0
  167. package/src/pm-plugins/table-resizing/utils/index.ts +8 -1
  168. package/src/pm-plugins/table-resizing/utils/misc.ts +25 -1
  169. package/src/pm-plugins/table-resizing/utils/resize-column.ts +2 -1
  170. package/src/pm-plugins/table-resizing/utils/resize-state.ts +56 -11
  171. package/src/pm-plugins/table-resizing/utils/scale-table.ts +24 -6
  172. package/src/toolbar.tsx +18 -5
  173. package/src/transforms/column-width.ts +33 -11
  174. package/src/transforms/delete-columns.ts +7 -2
  175. package/src/ui/FloatingContextualMenu/ContextualMenu.tsx +20 -5
  176. package/src/ui/FloatingContextualMenu/index.tsx +2 -0
  177. package/src/ui/FloatingDragMenu/DragMenu.tsx +3 -0
  178. package/src/ui/FloatingDragMenu/index.tsx +9 -1
  179. package/src/ui/FloatingInsertButton/index.tsx +17 -5
  180. package/src/ui/TableFloatingColumnControls/ColumnControls/index.tsx +4 -1
  181. package/src/ui/TableFloatingControls/RowControls/DragControls.tsx +4 -1
  182. package/src/utils/create.ts +1 -3
  183. package/src/utils/drag-menu.ts +4 -1
  184. package/tsconfig.dev.json +1 -1
@@ -1,10 +1,8 @@
1
1
  import { INPUT_METHOD, TABLE_STATUS } from '@atlaskit/editor-common/analytics';
2
2
  import type { EditorAnalyticsAPI } from '@atlaskit/editor-common/analytics';
3
- import type {
4
- Dispatch,
5
- EventDispatcher,
6
- } from '@atlaskit/editor-common/event-dispatcher';
3
+ import type { Dispatch } from '@atlaskit/editor-common/event-dispatcher';
7
4
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
5
+ import type { GetEditorFeatureFlags } from '@atlaskit/editor-common/types';
8
6
  import type { EditorView } from '@atlaskit/editor-prosemirror/view';
9
7
  import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
10
8
  import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
@@ -36,7 +34,11 @@ import { createPluginState, getPluginState } from './plugin-factory';
36
34
  import { pluginKey } from './plugin-key';
37
35
  import { getDraggableDataFromEvent } from './utils/monitor';
38
36
 
39
- const destroyFn = (editorView: EditorView, editorAnalyticsAPI: any) => {
37
+ const destroyFn = (
38
+ editorView: EditorView,
39
+ editorAnalyticsAPI: any,
40
+ getEditorFeatureFlags?: GetEditorFeatureFlags,
41
+ ) => {
40
42
  const editorPageScrollContainer = document.querySelector(
41
43
  '.fabric-editor-popup-scroll-parent',
42
44
  );
@@ -221,7 +223,9 @@ const destroyFn = (editorView: EditorView, editorAnalyticsAPI: any) => {
221
223
  editorView.state,
222
224
  );
223
225
  if (tableRef && tableNode) {
224
- insertColgroupFromNode(tableRef, tableNode);
226
+ const { tablePreserveWidth = false } =
227
+ getEditorFeatureFlags?.() || {};
228
+ insertColgroupFromNode(tableRef, tableNode, tablePreserveWidth);
225
229
  }
226
230
  }
227
231
 
@@ -234,7 +238,7 @@ const destroyFn = (editorView: EditorView, editorAnalyticsAPI: any) => {
234
238
 
235
239
  export const createPlugin = (
236
240
  dispatch: Dispatch,
237
- eventDispatcher: EventDispatcher,
241
+ getEditorFeatureFlags?: GetEditorFeatureFlags,
238
242
  editorAnalyticsAPI?: EditorAnalyticsAPI,
239
243
  ) => {
240
244
  return new SafePlugin({
@@ -307,7 +311,11 @@ export const createPlugin = (
307
311
  },
308
312
  view: (editorView: EditorView) => {
309
313
  return {
310
- destroy: destroyFn(editorView, editorAnalyticsAPI),
314
+ destroy: destroyFn(
315
+ editorView,
316
+ editorAnalyticsAPI,
317
+ getEditorFeatureFlags,
318
+ ),
311
319
  };
312
320
  },
313
321
  props: {
@@ -30,7 +30,10 @@ import {
30
30
  toggleTable,
31
31
  } from '@atlaskit/editor-common/keymaps';
32
32
  import type { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
33
- import type { GetEditorContainerWidth } from '@atlaskit/editor-common/types';
33
+ import type {
34
+ GetEditorContainerWidth,
35
+ GetEditorFeatureFlags,
36
+ } from '@atlaskit/editor-common/types';
34
37
  import { chainCommands } from '@atlaskit/editor-prosemirror/commands';
35
38
  import { keymap } from '@atlaskit/editor-prosemirror/keymap';
36
39
  import { getBooleanFF } from '@atlaskit/platform-feature-flags';
@@ -69,6 +72,7 @@ const createTableWithAnalytics = (
69
72
  export function keymapPlugin(
70
73
  getEditorContainerWidth: GetEditorContainerWidth,
71
74
  editorAnalyticsAPI: EditorAnalyticsAPI | undefined | null,
75
+ getEditorFeatureFlags?: GetEditorFeatureFlags,
72
76
  dragAndDropEnabled?: boolean,
73
77
  ): SafePlugin {
74
78
  const list = {};
@@ -178,6 +182,10 @@ export function keymapPlugin(
178
182
  }
179
183
 
180
184
  if (getBooleanFF('platform.editor.a11y-column-resizing_emcvz')) {
185
+ const { tablePreserveWidth = false } = getEditorFeatureFlags
186
+ ? getEditorFeatureFlags()
187
+ : {};
188
+
181
189
  bindKeymapWithCommand(
182
190
  startColumnResizing.common!,
183
191
  initiateKeyboardColumnResizing,
@@ -190,13 +198,13 @@ export function keymapPlugin(
190
198
 
191
199
  bindKeymapWithCommand(
192
200
  decreaseMediaSize.common!,
193
- changeColumnWidthByStep(-10, getEditorContainerWidth),
201
+ changeColumnWidthByStep(-10, getEditorContainerWidth, tablePreserveWidth),
194
202
  list,
195
203
  );
196
204
 
197
205
  bindKeymapWithCommand(
198
206
  increaseMediaSize.common!,
199
- changeColumnWidthByStep(10, getEditorContainerWidth),
207
+ changeColumnWidthByStep(10, getEditorContainerWidth, tablePreserveWidth),
200
208
  list,
201
209
  );
202
210
  bindKeymapWithCommand(escape.common!, stopKeyboardColumnResizing(), list);
@@ -188,6 +188,8 @@ export const createPlugin = (
188
188
  });
189
189
  }
190
190
 
191
+ const { tablePreserveWidth = false } = getEditorFeatureFlags();
192
+
191
193
  if (tr) {
192
194
  // "fixTables" removes empty rows as we don't allow that in schema
193
195
  const updatedTr = handleCut(
@@ -196,6 +198,7 @@ export const createPlugin = (
196
198
  newState,
197
199
  editorAnalyticsAPI,
198
200
  editorViewRef || undefined,
201
+ tablePreserveWidth,
199
202
  );
200
203
  return fixTables(updatedTr) || updatedTr;
201
204
  }
@@ -48,6 +48,7 @@ export const handleMouseDown = (
48
48
  const { state, dispatch } = view;
49
49
  const editorDisabled = !view.editable;
50
50
  const domAtPos = view.domAtPos.bind(view);
51
+ const { tablePreserveWidth = false } = getEditorFeatureFlags();
51
52
 
52
53
  if (
53
54
  editorDisabled ||
@@ -117,6 +118,7 @@ export const handleMouseDown = (
117
118
  tableRef: dom,
118
119
  start,
119
120
  domAtPos,
121
+ tablePreserveWidth,
120
122
  });
121
123
 
122
124
  if (
@@ -207,6 +209,7 @@ export const handleMouseDown = (
207
209
  clientX - startX,
208
210
  dom,
209
211
  resizingSelectedColumns ? selectedColumns : undefined,
212
+ tablePreserveWidth,
210
213
  );
211
214
  tr = updateColumnWidths(newResizeState, table, start)(tr);
212
215
  if (colIndex === map.width - 1) {
@@ -265,7 +268,14 @@ export const handleMouseDown = (
265
268
  $cell.nodeAfter!.attrs.colspan -
266
269
  1;
267
270
 
268
- resizeColumn(resizeState, colIndex, clientX - dragging.startX, dom);
271
+ resizeColumn(
272
+ resizeState,
273
+ colIndex,
274
+ clientX - dragging.startX,
275
+ dom,
276
+ undefined,
277
+ tablePreserveWidth,
278
+ );
269
279
 
270
280
  updateControls()(state);
271
281
  }
@@ -5,6 +5,8 @@ import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
5
5
  import { TableMap } from '@atlaskit/editor-tables/table-map';
6
6
  import { getBooleanFF } from '@atlaskit/platform-feature-flags';
7
7
 
8
+ import { MAX_SCALING_PERCENT } from './consts';
9
+
8
10
  type Col = Array<string | { [name: string]: string }>;
9
11
 
10
12
  /**
@@ -16,7 +18,7 @@ type Col = Array<string | { [name: string]: string }>;
16
18
  export const getColWidthFix = (colwidth: number, tableColumnCount: number) =>
17
19
  colwidth - 1 / tableColumnCount;
18
20
 
19
- export const generateColgroup = (table: PmNode) => {
21
+ export const generateColgroup = (table: PmNode, tableRef?: HTMLElement) => {
20
22
  const cols: Col[] = [];
21
23
 
22
24
  if (getBooleanFF('platform.editor.custom-table-width')) {
@@ -26,17 +28,37 @@ export const generateColgroup = (table: PmNode) => {
26
28
  if (Array.isArray(cell.attrs.colwidth)) {
27
29
  // We slice here to guard against our colwidth array having more entries
28
30
  // Than the we actually span. We'll patch the document at a later point.
29
- cell.attrs.colwidth.slice(0, colspan).forEach((width) => {
30
- cols.push([
31
- 'col',
32
- {
33
- style: `width: ${getColWidthFix(
34
- width ? Math.max(width, tableCellMinWidth) : tableCellMinWidth,
35
- map.width,
36
- )}px;`,
37
- },
38
- ]);
39
- });
31
+ if (tableRef) {
32
+ const tableWidth = table.attrs && table.attrs.width;
33
+ let renderWidth = tableRef.parentElement?.clientWidth || 760;
34
+ let scalePercent = renderWidth / tableWidth;
35
+ scalePercent = Math.max(scalePercent, 1 - MAX_SCALING_PERCENT);
36
+ cell.attrs.colwidth.slice(0, colspan).forEach((width) => {
37
+ const fixedColWidth = getColWidthFix(width, map.width);
38
+ const scaledWidth = fixedColWidth * Math.min(scalePercent, 1);
39
+ const finalWidth = Math.max(scaledWidth, tableCellMinWidth);
40
+ cols.push([
41
+ 'col',
42
+ {
43
+ style: `width: ${finalWidth}px;`,
44
+ },
45
+ ]);
46
+ });
47
+ } else {
48
+ cell.attrs.colwidth.slice(0, colspan).forEach((width) => {
49
+ cols.push([
50
+ 'col',
51
+ {
52
+ style: `width: ${getColWidthFix(
53
+ width
54
+ ? Math.max(width, tableCellMinWidth)
55
+ : tableCellMinWidth,
56
+ map.width,
57
+ )}px;`,
58
+ },
59
+ ]);
60
+ });
61
+ }
40
62
  } else {
41
63
  // When we have merged cells on the first row (firstChild),
42
64
  // We want to ensure we're creating the appropriate amount of
@@ -73,14 +95,21 @@ export const generateColgroup = (table: PmNode) => {
73
95
  export const insertColgroupFromNode = (
74
96
  tableRef: HTMLTableElement,
75
97
  table: PmNode,
98
+ tablePreserveWidth = false,
99
+ shouldRemove = true,
76
100
  ): HTMLCollection => {
77
101
  let colgroup = tableRef.querySelector('colgroup') as HTMLElement;
78
- if (colgroup) {
102
+ if (colgroup && shouldRemove) {
79
103
  tableRef.removeChild(colgroup);
80
104
  }
81
105
 
82
- colgroup = renderColgroupFromNode(table);
83
- tableRef.insertBefore(colgroup, tableRef.firstChild);
106
+ colgroup = renderColgroupFromNode(
107
+ table,
108
+ tablePreserveWidth ? tableRef : undefined,
109
+ );
110
+ if (shouldRemove) {
111
+ tableRef.insertBefore(colgroup, tableRef.firstChild);
112
+ }
84
113
 
85
114
  return colgroup.children;
86
115
  };
@@ -109,11 +138,14 @@ export const isMinCellWidthTable = (table: PmNode) => {
109
138
  return isTableMinCellWidth;
110
139
  };
111
140
 
112
- function renderColgroupFromNode(table: PmNode): HTMLElement {
141
+ function renderColgroupFromNode(
142
+ table: PmNode,
143
+ maybeTableRef: HTMLElement | undefined,
144
+ ): HTMLElement {
113
145
  const rendered = DOMSerializer.renderSpec(document, [
114
146
  'colgroup',
115
147
  {},
116
- ...generateColgroup(table),
148
+ ...generateColgroup(table, maybeTableRef),
117
149
  ]);
118
150
 
119
151
  return rendered.dom as HTMLElement;
@@ -1,2 +1,4 @@
1
1
  export const COLUMN_MIN_WIDTH = 48;
2
+ export const TABLE_DEFAULT_WIDTH = 760;
2
3
  export const TABLE_MAX_WIDTH = 1800;
4
+ export const MAX_SCALING_PERCENT = 0.3;
@@ -30,6 +30,8 @@ export {
30
30
  currentColWidth,
31
31
  domCellAround,
32
32
  getTableMaxWidth,
33
+ getTableElementWidth,
34
+ getTableContainerElement,
33
35
  } from './misc';
34
36
  export { updateControls, isClickNear, getResizeCellPos } from './dom';
35
37
  export {
@@ -41,4 +43,9 @@ export {
41
43
  export type { ScaleOptions } from './scale-table';
42
44
  export type { ResizeState, ResizeStateWithAnalytics } from './types';
43
45
  export { resizeColumn } from './resize-column';
44
- export { COLUMN_MIN_WIDTH, TABLE_MAX_WIDTH } from './consts';
46
+ export {
47
+ COLUMN_MIN_WIDTH,
48
+ TABLE_MAX_WIDTH,
49
+ TABLE_DEFAULT_WIDTH,
50
+ MAX_SCALING_PERCENT,
51
+ } from './consts';
@@ -1,6 +1,7 @@
1
1
  import type { CellAttributes, TableLayout } from '@atlaskit/adf-schema';
2
2
  import {
3
3
  getParentNodeWidth,
4
+ getTableContainerWidth,
4
5
  layoutToWidth,
5
6
  } from '@atlaskit/editor-common/node-width';
6
7
  import { calcTableWidth } from '@atlaskit/editor-common/styles';
@@ -9,7 +10,10 @@ import {
9
10
  getBreakpoint,
10
11
  mapBreakpointToLayoutMaxWidth,
11
12
  } from '@atlaskit/editor-common/ui';
12
- import { containsClassName } from '@atlaskit/editor-common/utils';
13
+ import {
14
+ calcTableColumnWidths,
15
+ containsClassName,
16
+ } from '@atlaskit/editor-common/utils';
13
17
  import type {
14
18
  NodeSpec,
15
19
  Node as PMNode,
@@ -26,6 +30,8 @@ import { getBooleanFF } from '@atlaskit/platform-feature-flags';
26
30
 
27
31
  import type { TableOptions } from '../../../nodeviews/types';
28
32
 
33
+ import { hasTableBeenResized } from './colgroup';
34
+
29
35
  // Translates named layouts in number values.
30
36
  export function getLayoutSize(
31
37
  tableLayout: TableLayout,
@@ -132,3 +138,21 @@ export const getTableMaxWidth = ({
132
138
 
133
139
  return maxWidth as number;
134
140
  };
141
+
142
+ /**
143
+ *
144
+ * @param table
145
+ * @returns calculated width of <table /> element derived from sum of colwidths on tableCell or tableHeader nodes or falls back to container width
146
+ */
147
+ export const getTableElementWidth = (table: PMNode) => {
148
+ if (hasTableBeenResized(table)) {
149
+ // TODO: is there a scenario where ADF columns are SMALLER than container width?
150
+ return calcTableColumnWidths(table).reduce((sum, width) => sum + width, 0);
151
+ }
152
+
153
+ return getTableContainerElement(table);
154
+ };
155
+
156
+ export const getTableContainerElement = (table: PMNode) => {
157
+ return getTableContainerWidth(table);
158
+ };
@@ -9,6 +9,7 @@ export const resizeColumn = (
9
9
  amount: number,
10
10
  tableRef: HTMLElement,
11
11
  selectedColumns?: number[],
12
+ tablePreserveWidth = false,
12
13
  ): ResizeState => {
13
14
  const newState =
14
15
  amount > 0
@@ -17,7 +18,7 @@ export const resizeColumn = (
17
18
  ? shrinkColumn(resizeState, colIndex, amount, selectedColumns)
18
19
  : resizeState;
19
20
 
20
- updateColgroup(newState, tableRef);
21
+ updateColgroup(newState, tableRef, tablePreserveWidth);
21
22
 
22
23
  return newState;
23
24
  };
@@ -23,6 +23,8 @@ import { syncStickyRowToTable } from './dom';
23
23
  import { getTableMaxWidth } from './misc';
24
24
  import type { ResizeState, ResizeStateWithAnalytics } from './types';
25
25
 
26
+ import { MAX_SCALING_PERCENT, TABLE_DEFAULT_WIDTH } from './index';
27
+
26
28
  export const getResizeState = ({
27
29
  minWidth,
28
30
  maxSize,
@@ -30,6 +32,7 @@ export const getResizeState = ({
30
32
  tableRef,
31
33
  start,
32
34
  domAtPos,
35
+ tablePreserveWidth = false,
33
36
  }: {
34
37
  minWidth: number;
35
38
  maxSize: number;
@@ -37,7 +40,9 @@ export const getResizeState = ({
37
40
  tableRef: HTMLTableElement;
38
41
  start: number;
39
42
  domAtPos: (pos: number) => { node: Node; offset: number };
43
+ tablePreserveWidth: boolean;
40
44
  }): ResizeState => {
45
+ // If the table has been resized, we can use the column widths from the table node
41
46
  if (hasTableBeenResized(table)) {
42
47
  const cols = calcTableColumnWidths(table).map((width, index) => ({
43
48
  width: width === 0 ? tableNewColumnMinWidth : width,
@@ -58,9 +63,25 @@ export const getResizeState = ({
58
63
  };
59
64
  }
60
65
 
66
+ const shouldReinsertColgroup = !tablePreserveWidth;
67
+
61
68
  // Getting the resize state from DOM
62
- const colgroupChildren = insertColgroupFromNode(tableRef, table);
69
+ const colgroupChildren = insertColgroupFromNode(
70
+ tableRef,
71
+ table,
72
+ tablePreserveWidth,
73
+ shouldReinsertColgroup, // don't reinsert colgroup when preserving table width - this causes widths to jump
74
+ );
63
75
  const cols = Array.from(colgroupChildren).map((_, index) => {
76
+ // If the table hasn't been resized and we have a table width attribute, we can use it
77
+ // to calculate the widths of the columns
78
+ if (tablePreserveWidth && table.attrs.width) {
79
+ return {
80
+ index,
81
+ width: table.attrs.width / colgroupChildren.length,
82
+ minWidth: 48,
83
+ };
84
+ }
64
85
  const cellsRefs = getCellsRefsInColumn(index, table, start, domAtPos);
65
86
  return getColumnStateFromDOM(cellsRefs, index, minWidth);
66
87
  });
@@ -83,21 +104,43 @@ export const getResizeState = ({
83
104
  export const updateColgroup = (
84
105
  state: ResizeState,
85
106
  tableRef: HTMLElement,
107
+ tablePreserveWidth?: boolean,
86
108
  ): void => {
87
109
  const cols = tableRef.querySelectorAll('col');
88
110
 
89
111
  if (getBooleanFF('platform.editor.custom-table-width')) {
90
112
  const columnsCount = cols.length;
91
- state.cols
92
- .filter((column) => column && !!column.width) // if width is 0, we dont want to apply that.
93
- .forEach((column, i) => {
94
- if (cols[i]) {
95
- cols[i].style.width = `${getColWidthFix(
96
- column.width,
97
- columnsCount,
98
- )}px`;
99
- }
100
- });
113
+ if (tablePreserveWidth) {
114
+ const tableWidth = parseInt(tableRef.dataset?.tableWidth || ''); // TODO - get this value from the table node, not the dom
115
+ if (tableWidth) {
116
+ let renderWidth =
117
+ tableRef.parentElement?.clientWidth || TABLE_DEFAULT_WIDTH;
118
+ let scalePercent = renderWidth / tableWidth;
119
+ scalePercent = Math.max(scalePercent, 1 - MAX_SCALING_PERCENT);
120
+ state.cols
121
+ .filter((column) => column && !!column.width) // if width is 0, we dont want to apply that.
122
+ .forEach((column, i) => {
123
+ const fixedColWidth = getColWidthFix(column.width, columnsCount);
124
+ const scaledWidth = fixedColWidth * Math.min(scalePercent, 1);
125
+ const finalWidth = Math.max(scaledWidth, tableCellMinWidth);
126
+ // we aren't handling the remaining pixels here when the 48px min width is reached
127
+ if (cols[i]) {
128
+ cols[i].style.width = `${finalWidth}px`;
129
+ }
130
+ });
131
+ }
132
+ } else {
133
+ state.cols
134
+ .filter((column) => column && !!column.width) // if width is 0, we dont want to apply that.
135
+ .forEach((column, i) => {
136
+ if (cols[i]) {
137
+ cols[i].style.width = `${getColWidthFix(
138
+ column.width,
139
+ columnsCount,
140
+ )}px`;
141
+ }
142
+ });
143
+ }
101
144
  } else {
102
145
  state.cols
103
146
  .filter((column) => column && !!column.width) // if width is 0, we dont want to apply that.
@@ -306,6 +349,7 @@ export const getNewResizeStateFromSelectedColumns = (
306
349
  state: EditorState,
307
350
  domAtPos: (pos: number) => { node: Node; offset: number },
308
351
  getEditorContainerWidth: GetEditorContainerWidth,
352
+ tablePreserveWidth = false,
309
353
  ): ResizeStateWithAnalytics | undefined => {
310
354
  // Fail early so that we don't do complex calculations for no reason
311
355
  const numColumnsSelected = rect.right - rect.left;
@@ -350,6 +394,7 @@ export const getNewResizeStateFromSelectedColumns = (
350
394
  tableRef,
351
395
  start: table.start,
352
396
  domAtPos,
397
+ tablePreserveWidth,
353
398
  });
354
399
 
355
400
  const newResizeState = evenSelectedColumnsWidths(resizeState, rect);
@@ -39,6 +39,7 @@ export const scale = (
39
39
  tableRef: HTMLTableElement,
40
40
  options: ScaleOptions,
41
41
  domAtPos: DomAtPos,
42
+ tablePreserveWidth = false,
42
43
  ): ResizeState | undefined => {
43
44
  /**
44
45
  * isBreakoutEnabled === true -> default center aligned
@@ -97,6 +98,7 @@ export const scale = (
97
98
  tableRef,
98
99
  start,
99
100
  domAtPos,
101
+ tablePreserveWidth,
100
102
  });
101
103
 
102
104
  return scaleTableTo(resizeState, newWidth);
@@ -108,6 +110,7 @@ export const scaleWithParent = (
108
110
  table: PMNode,
109
111
  start: number,
110
112
  domAtPos: DomAtPos,
113
+ tablePreserveWidth = false,
111
114
  ) => {
112
115
  const resizeState = getResizeState({
113
116
  minWidth: tableCellMinWidth,
@@ -116,6 +119,7 @@ export const scaleWithParent = (
116
119
  tableRef,
117
120
  start,
118
121
  domAtPos,
122
+ tablePreserveWidth,
119
123
  });
120
124
 
121
125
  if (table.attrs.isNumberColumnEnabled) {
@@ -154,6 +158,7 @@ export const previewScaleTable = (
154
158
  tableRef: HTMLTableElement | null | undefined,
155
159
  options: ScaleOptions,
156
160
  domAtPos: DomAtPos,
161
+ tablePreserveWidth: boolean = false,
157
162
  ) => {
158
163
  const { node, start, parentWidth } = options;
159
164
 
@@ -169,17 +174,26 @@ export const previewScaleTable = (
169
174
  tableRef.style.width = `${width}px`;
170
175
  }
171
176
 
172
- if (!hasTableBeenResized(node)) {
177
+ // If the table hasn't been resize, the colgroup 48px width values will gracefully scale down.
178
+ // If we are scaling the table down with tablePreserveWidth, the colgroup widths may be scaled to a value that is not 48px.
179
+ if (!hasTableBeenResized(node) && !tablePreserveWidth) {
173
180
  syncStickyRowToTable(tableRef);
174
181
  return;
175
182
  }
176
183
 
177
184
  const resizeState = parentWidth
178
- ? scaleWithParent(tableRef, parentWidth, node, start, domAtPos)
179
- : scale(tableRef, options, domAtPos);
185
+ ? scaleWithParent(
186
+ tableRef,
187
+ parentWidth,
188
+ node,
189
+ start,
190
+ domAtPos,
191
+ tablePreserveWidth,
192
+ )
193
+ : scale(tableRef, options, domAtPos, tablePreserveWidth);
180
194
 
181
195
  if (resizeState) {
182
- updateColgroup(resizeState, tableRef);
196
+ updateColgroup(resizeState, tableRef, tablePreserveWidth);
183
197
  }
184
198
  };
185
199
 
@@ -189,6 +203,7 @@ export const scaleTable =
189
203
  tableRef: HTMLTableElement | null | undefined,
190
204
  options: ScaleOptions,
191
205
  domAtPos: DomAtPos,
206
+ tablePreserveWidth = false,
192
207
  ) =>
193
208
  (tr: Transaction) => {
194
209
  if (!tableRef) {
@@ -200,7 +215,9 @@ export const scaleTable =
200
215
  if (hasTableBeenResized(node) === false) {
201
216
  // If its not a re-sized table, we still want to re-create cols
202
217
  // To force reflow of columns upon delete.
203
- insertColgroupFromNode(tableRef, node);
218
+ if (!tablePreserveWidth) {
219
+ insertColgroupFromNode(tableRef, node);
220
+ }
204
221
  tr.setMeta('scrollIntoView', false);
205
222
  return tr;
206
223
  }
@@ -213,9 +230,10 @@ export const scaleTable =
213
230
  node,
214
231
  start,
215
232
  domAtPos,
233
+ tablePreserveWidth,
216
234
  );
217
235
  } else {
218
- resizeState = scale(tableRef, options, domAtPos);
236
+ resizeState = scale(tableRef, options, domAtPos, tablePreserveWidth);
219
237
  }
220
238
 
221
239
  if (resizeState) {