@atlaskit/editor-plugin-table 7.11.3 → 7.11.5

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 (78) hide show
  1. package/CHANGELOG.md +1734 -700
  2. package/dist/cjs/commands/column-resize.js +5 -1
  3. package/dist/cjs/nodeviews/TableComponent.js +28 -6
  4. package/dist/cjs/nodeviews/table.js +17 -11
  5. package/dist/cjs/plugin.js +8 -9
  6. package/dist/cjs/pm-plugins/drag-and-drop/plugin.js +6 -1
  7. package/dist/cjs/pm-plugins/table-resizing/event-handlers.js +16 -10
  8. package/dist/cjs/pm-plugins/table-resizing/plugin.js +2 -2
  9. package/dist/cjs/pm-plugins/table-resizing/utils/resize-state.js +7 -2
  10. package/dist/cjs/pm-plugins/table-resizing/utils/scale-table.js +16 -11
  11. package/dist/cjs/pm-plugins/table-width.js +1 -5
  12. package/dist/cjs/pm-plugins/view-mode-sort/utils.js +37 -20
  13. package/dist/cjs/toolbar.js +12 -21
  14. package/dist/cjs/transforms/column-width.js +3 -0
  15. package/dist/cjs/ui/FloatingDragMenu/index.js +2 -5
  16. package/dist/cjs/ui/common-styles.js +8 -16
  17. package/dist/cjs/ui/ui-styles.js +18 -36
  18. package/dist/es2019/commands/column-resize.js +6 -2
  19. package/dist/es2019/nodeviews/TableComponent.js +30 -7
  20. package/dist/es2019/nodeviews/table.js +17 -12
  21. package/dist/es2019/plugin.js +8 -9
  22. package/dist/es2019/pm-plugins/drag-and-drop/plugin.js +6 -1
  23. package/dist/es2019/pm-plugins/table-resizing/event-handlers.js +13 -7
  24. package/dist/es2019/pm-plugins/table-resizing/plugin.js +2 -2
  25. package/dist/es2019/pm-plugins/table-resizing/utils/resize-state.js +7 -2
  26. package/dist/es2019/pm-plugins/table-resizing/utils/scale-table.js +16 -11
  27. package/dist/es2019/pm-plugins/table-width.js +1 -5
  28. package/dist/es2019/pm-plugins/view-mode-sort/utils.js +36 -21
  29. package/dist/es2019/toolbar.js +7 -16
  30. package/dist/es2019/transforms/column-width.js +4 -1
  31. package/dist/es2019/ui/FloatingDragMenu/index.js +2 -5
  32. package/dist/es2019/ui/common-styles.js +18 -30
  33. package/dist/es2019/ui/ui-styles.js +88 -174
  34. package/dist/esm/commands/column-resize.js +5 -1
  35. package/dist/esm/nodeviews/TableComponent.js +28 -6
  36. package/dist/esm/nodeviews/table.js +17 -11
  37. package/dist/esm/plugin.js +8 -9
  38. package/dist/esm/pm-plugins/drag-and-drop/plugin.js +6 -1
  39. package/dist/esm/pm-plugins/table-resizing/event-handlers.js +16 -10
  40. package/dist/esm/pm-plugins/table-resizing/plugin.js +2 -2
  41. package/dist/esm/pm-plugins/table-resizing/utils/resize-state.js +7 -2
  42. package/dist/esm/pm-plugins/table-resizing/utils/scale-table.js +16 -11
  43. package/dist/esm/pm-plugins/table-width.js +1 -5
  44. package/dist/esm/pm-plugins/view-mode-sort/utils.js +37 -20
  45. package/dist/esm/toolbar.js +12 -21
  46. package/dist/esm/transforms/column-width.js +3 -0
  47. package/dist/esm/ui/FloatingDragMenu/index.js +2 -5
  48. package/dist/esm/ui/common-styles.js +8 -16
  49. package/dist/esm/ui/ui-styles.js +18 -36
  50. package/dist/types/pm-plugins/table-resizing/event-handlers.d.ts +1 -1
  51. package/dist/types/pm-plugins/table-resizing/plugin.d.ts +1 -1
  52. package/dist/types/pm-plugins/table-resizing/utils/scale-table.d.ts +3 -3
  53. package/dist/types/pm-plugins/table-width.d.ts +1 -1
  54. package/dist/types/pm-plugins/view-mode-sort/utils.d.ts +3 -1
  55. package/dist/types/ui/FloatingDragMenu/index.d.ts +2 -1
  56. package/dist/types-ts4.5/pm-plugins/table-resizing/event-handlers.d.ts +1 -1
  57. package/dist/types-ts4.5/pm-plugins/table-resizing/plugin.d.ts +1 -1
  58. package/dist/types-ts4.5/pm-plugins/table-resizing/utils/scale-table.d.ts +3 -3
  59. package/dist/types-ts4.5/pm-plugins/table-width.d.ts +1 -1
  60. package/dist/types-ts4.5/pm-plugins/view-mode-sort/utils.d.ts +3 -1
  61. package/dist/types-ts4.5/ui/FloatingDragMenu/index.d.ts +2 -1
  62. package/package.json +3 -6
  63. package/src/commands/column-resize.ts +11 -2
  64. package/src/nodeviews/TableComponent.tsx +71 -9
  65. package/src/nodeviews/table.tsx +34 -18
  66. package/src/plugin.tsx +10 -4
  67. package/src/pm-plugins/drag-and-drop/plugin.ts +14 -1
  68. package/src/pm-plugins/table-resizing/event-handlers.ts +26 -4
  69. package/src/pm-plugins/table-resizing/plugin.ts +2 -0
  70. package/src/pm-plugins/table-resizing/utils/resize-state.ts +13 -2
  71. package/src/pm-plugins/table-resizing/utils/scale-table.ts +24 -10
  72. package/src/pm-plugins/table-width.ts +1 -3
  73. package/src/pm-plugins/view-mode-sort/utils.ts +49 -23
  74. package/src/toolbar.tsx +7 -11
  75. package/src/transforms/column-width.ts +8 -1
  76. package/src/ui/FloatingDragMenu/index.tsx +2 -3
  77. package/src/ui/common-styles.ts +18 -30
  78. package/src/ui/ui-styles.ts +92 -183
@@ -223,7 +223,18 @@ class TableComponent extends React.Component<ComponentProps, TableState> {
223
223
  }
224
224
  }
225
225
 
226
- if (isTableScalingEnabled) {
226
+ if (
227
+ isTableScalingEnabled &&
228
+ !getBooleanFF('platform.editor.table.preserve-widths-with-lock-button')
229
+ ) {
230
+ this.handleColgroupUpdates(true);
231
+ }
232
+
233
+ if (
234
+ isTableScalingEnabled &&
235
+ getBooleanFF('platform.editor.table.preserve-widths-with-lock-button') &&
236
+ getNode().attrs.displayMode !== 'fixed'
237
+ ) {
227
238
  this.handleColgroupUpdates(true);
228
239
  }
229
240
 
@@ -382,12 +393,17 @@ class TableComponent extends React.Component<ComponentProps, TableState> {
382
393
  const wasTableResized = hasTableBeenResized(this.node);
383
394
  const isTableResized = hasTableBeenResized(tableNode);
384
395
  const isColumnsDistributed = wasTableResized && !isTableResized;
385
- const shouldScale =
396
+ const isTableDisplayModeChanged =
397
+ this.node.attrs.displayMode !== tableNode.attrs.displayMode;
398
+
399
+ const shouldUpdateColgroup =
386
400
  isWidthChanged ||
387
401
  isColumnsDistributed ||
388
402
  isTableResizedFullWidth ||
389
- isTableWidthChanged;
390
- if (force || (!isResizing && shouldScale)) {
403
+ isTableWidthChanged ||
404
+ isTableDisplayModeChanged;
405
+
406
+ if (force || (!isResizing && shouldUpdateColgroup)) {
391
407
  const resizeState = getResizeState({
392
408
  minWidth: COLUMN_MIN_WIDTH,
393
409
  maxSize: tableRenderWidth,
@@ -398,9 +414,34 @@ class TableComponent extends React.Component<ComponentProps, TableState> {
398
414
  isTableScalingEnabled: true,
399
415
  });
400
416
 
417
+ let shouldScaleOnColgroupUpdate = false;
418
+ if (
419
+ this.props.options?.isTableScalingEnabled &&
420
+ !getBooleanFF(
421
+ 'platform.editor.table.preserve-widths-with-lock-button',
422
+ )
423
+ ) {
424
+ shouldScaleOnColgroupUpdate = true;
425
+ }
426
+
427
+ if (
428
+ this.props.options?.isTableScalingEnabled &&
429
+ getBooleanFF(
430
+ 'platform.editor.table.preserve-widths-with-lock-button',
431
+ ) &&
432
+ tableNode.attrs.displayMode !== 'fixed'
433
+ ) {
434
+ shouldScaleOnColgroupUpdate = true;
435
+ }
436
+
401
437
  // Request animation frame required for Firefox
402
438
  requestAnimationFrame(() => {
403
- updateColgroup(resizeState, this.table!, tableNode, true);
439
+ updateColgroup(
440
+ resizeState,
441
+ this.table!,
442
+ tableNode,
443
+ shouldScaleOnColgroupUpdate,
444
+ );
404
445
  });
405
446
  }
406
447
  }
@@ -417,7 +458,7 @@ class TableComponent extends React.Component<ComponentProps, TableState> {
417
458
  allowColumnResizing,
418
459
  isResizing,
419
460
  options,
420
- isTableScalingEnabled,
461
+ isTableScalingEnabled, // we could use options.isTableScalingEnabled here
421
462
  getPos,
422
463
  } = this.props;
423
464
  let { isInDanger } = this.props;
@@ -429,7 +470,27 @@ class TableComponent extends React.Component<ComponentProps, TableState> {
429
470
  isInDanger = pluginState.isInDanger;
430
471
  }
431
472
 
432
- if (isTableScalingEnabled) {
473
+ let shouldScale = false;
474
+ let shouldHandleColgroupUpdates = false;
475
+
476
+ if (
477
+ isTableScalingEnabled &&
478
+ !getBooleanFF('platform.editor.table.preserve-widths-with-lock-button')
479
+ ) {
480
+ shouldScale = true;
481
+ shouldHandleColgroupUpdates = true;
482
+ }
483
+
484
+ if (
485
+ isTableScalingEnabled &&
486
+ getBooleanFF('platform.editor.table.preserve-widths-with-lock-button') &&
487
+ getNode().attrs.displayMode !== 'fixed'
488
+ ) {
489
+ shouldScale = true;
490
+ shouldHandleColgroupUpdates = true;
491
+ }
492
+
493
+ if (shouldHandleColgroupUpdates) {
433
494
  this.handleColgroupUpdates();
434
495
  }
435
496
 
@@ -506,7 +567,8 @@ class TableComponent extends React.Component<ComponentProps, TableState> {
506
567
  if (shouldRecreateResizeCols) {
507
568
  const start = getPos() || 0;
508
569
  const depth = view.state.doc.resolve(start).depth;
509
- const shouldScale = depth === 0 && isTableScalingEnabled;
570
+ shouldScale = depth === 0 && shouldScale;
571
+
510
572
  insertColgroupFromNode(this.table, currentTable, shouldScale);
511
573
  }
512
574
 
@@ -610,7 +672,7 @@ class TableComponent extends React.Component<ComponentProps, TableState> {
610
672
  pluginInjectionApi,
611
673
  isDragAndDropEnabled,
612
674
  getEditorFeatureFlags,
613
- isTableScalingEnabled,
675
+ isTableScalingEnabled, // here we can use options.isTableScalingEnabled
614
676
  } = this.props;
615
677
 
616
678
  let {
@@ -136,8 +136,15 @@ export default class TableView extends ReactNodeView<Props> {
136
136
 
137
137
  if (rendered.dom) {
138
138
  this.table = rendered.dom;
139
- // Preserve Table Width cannot have inline width set on the table
140
- if (!this.options?.isTableScalingEnabled) {
139
+
140
+ if (
141
+ !this.options?.isTableScalingEnabled ||
142
+ (this.options?.isTableScalingEnabled &&
143
+ getBooleanFF(
144
+ 'platform.editor.table.preserve-widths-with-lock-button',
145
+ ) &&
146
+ this.node.attrs.displayMode === 'fixed')
147
+ ) {
141
148
  const tableInlineWidth = getInlineWidth(
142
149
  this.node,
143
150
  this.reactComponentProps.options,
@@ -157,26 +164,35 @@ export default class TableView extends ReactNodeView<Props> {
157
164
  if (!this.table) {
158
165
  return;
159
166
  }
160
-
161
167
  const attrs = tableAttributes(node);
162
168
  (Object.keys(attrs) as Array<keyof typeof attrs>).forEach((attr) => {
163
169
  this.table!.setAttribute(attr, attrs[attr]);
164
170
  });
165
171
 
166
- // handle inline style when table been resized
167
- const tableInlineWidth = getInlineWidth(
168
- node,
169
- (this.reactComponentProps as Props).options,
170
- this.view.state,
171
- this.getPos(),
172
- );
172
+ // Preserve Table Width cannot have inline width set on the table
173
+ if (
174
+ !this.options?.isTableScalingEnabled ||
175
+ (this.options?.isTableScalingEnabled &&
176
+ getBooleanFF(
177
+ 'platform.editor.table.preserve-widths-with-lock-button',
178
+ ) &&
179
+ node.attrs.displayMode === 'fixed')
180
+ ) {
181
+ // handle inline style when table been resized
182
+ const tableInlineWidth = getInlineWidth(
183
+ node,
184
+ (this.reactComponentProps as Props).options,
185
+ this.view.state,
186
+ this.getPos(),
187
+ );
173
188
 
174
- const isTableResizing = tableWidthPluginKey.getState(
175
- this.view.state,
176
- )?.resizing;
189
+ const isTableResizing = tableWidthPluginKey.getState(
190
+ this.view.state,
191
+ )?.resizing;
177
192
 
178
- if (!isTableResizing && tableInlineWidth) {
179
- handleInlineTableWidth(this.table, tableInlineWidth);
193
+ if (!isTableResizing && tableInlineWidth) {
194
+ handleInlineTableWidth(this.table, tableInlineWidth);
195
+ }
180
196
  }
181
197
  }
182
198
 
@@ -275,7 +291,7 @@ export default class TableView extends ReactNodeView<Props> {
275
291
  isHeaderRowEnabled={pluginState!.isHeaderRowEnabled}
276
292
  isHeaderColumnEnabled={pluginState!.isHeaderColumnEnabled}
277
293
  isDragAndDropEnabled={pluginState!.isDragAndDropEnabled}
278
- isTableScalingEnabled={pluginState!.isTableScalingEnabled}
294
+ isTableScalingEnabled={props.options?.isTableScalingEnabled} // this.options?.isTableScalingEnabled sams as TableOptions.isTableScalingEnabled same as pluginState.isTableScalingEnabled
279
295
  tableActive={tableActive}
280
296
  ordering={pluginState!.ordering as TableColumnOrdering}
281
297
  isResizing={isResizing}
@@ -381,7 +397,7 @@ export const createTableView = (
381
397
  wasFullWidthModeEnabled,
382
398
  isTableResizingEnabled,
383
399
  isDragAndDropEnabled,
384
- isTableScalingEnabled,
400
+ isTableScalingEnabled, // same as options.isTableScalingEnabled
385
401
  } = getPluginState(view.state);
386
402
  const { allowColumnResizing, allowControls } = getPluginConfig(pluginConfig);
387
403
  const hasIntlContext = true;
@@ -399,7 +415,7 @@ export const createTableView = (
399
415
  wasFullWidthModeEnabled,
400
416
  isTableResizingEnabled,
401
417
  isDragAndDropEnabled,
402
- isTableScalingEnabled,
418
+ isTableScalingEnabled, // same as options.isTableScalingEnabled
403
419
  },
404
420
  getEditorContainerWidth,
405
421
  getEditorFeatureFlags,
package/src/plugin.tsx CHANGED
@@ -287,8 +287,12 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
287
287
  {
288
288
  name: 'tablePMColResizing',
289
289
  plugin: ({ dispatch }) => {
290
- const { fullWidthEnabled, tableOptions, getEditorFeatureFlags } =
291
- options || ({} as TablePluginOptions);
290
+ const {
291
+ fullWidthEnabled,
292
+ tableOptions,
293
+ getEditorFeatureFlags,
294
+ isTableScalingEnabled,
295
+ } = options || ({} as TablePluginOptions);
292
296
  const { allowColumnResizing } = pluginConfig(tableOptions);
293
297
  return allowColumnResizing
294
298
  ? createFlexiResizingPlugin(
@@ -299,6 +303,7 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
299
303
  defaultGetEditorContainerWidth,
300
304
  getEditorFeatureFlags || defaultGetEditorFeatureFlags,
301
305
  editorAnalyticsAPI,
306
+ isTableScalingEnabled || false,
302
307
  )
303
308
  : undefined;
304
309
  },
@@ -394,6 +399,7 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
394
399
  dispatch,
395
400
  dispatchAnalyticsEvent,
396
401
  options?.fullWidthEnabled ?? false,
402
+ options?.isTableScalingEnabled ?? false,
397
403
  )
398
404
  : undefined,
399
405
  },
@@ -569,6 +575,7 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
569
575
  editorAnalyticsAPI={editorAnalyticsAPI}
570
576
  stickyHeaders={stickyHeader}
571
577
  pluginConfig={pluginConfig}
578
+ isTableScalingEnabled={options?.isTableScalingEnabled}
572
579
  />
573
580
  )}
574
581
  {allowControls && !isDragAndDropEnabled && !isResizing && (
@@ -607,11 +614,10 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
607
614
  action(insert, state) {
608
615
  // see comment on tablesPlugin.getSharedState on usage
609
616
  const tableState = api?.table?.sharedState.currentState();
610
- const { isTableScalingEnabled = false } = getPluginState(state);
611
617
 
612
618
  const tr = insert(
613
619
  createTableWithWidth(
614
- isTableScalingEnabled,
620
+ options?.isTableScalingEnabled,
615
621
  tableState?.isFullWidthModeEnabled,
616
622
  )(state.schema),
617
623
  );
@@ -9,6 +9,7 @@ import {
9
9
  getCellsInRow,
10
10
  getSelectedCellInfo,
11
11
  } from '@atlaskit/editor-tables/utils';
12
+ import { getBooleanFF } from '@atlaskit/platform-feature-flags';
12
13
  import { autoScrollForElements } from '@atlaskit/pragmatic-drag-and-drop-auto-scroll/element';
13
14
  import { combine } from '@atlaskit/pragmatic-drag-and-drop/combine';
14
15
  import { monitorForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter';
@@ -235,10 +236,22 @@ const destroyFn = (editorView: EditorView, editorAnalyticsAPI: any) => {
235
236
  const { isTableScalingEnabled = false } = getTablePluginState(
236
237
  editorView.state,
237
238
  );
239
+
240
+ let isTableScalingEnabledOnCurrentTable = isTableScalingEnabled;
241
+ if (
242
+ isTableScalingEnabled &&
243
+ getBooleanFF(
244
+ 'platform.editor.table.preserve-widths-with-lock-button',
245
+ )
246
+ ) {
247
+ isTableScalingEnabledOnCurrentTable =
248
+ tableNode.attrs.displayMode !== 'fixed';
249
+ }
250
+
238
251
  insertColgroupFromNode(
239
252
  tableRef,
240
253
  tableNode,
241
- isTableScalingEnabled,
254
+ isTableScalingEnabledOnCurrentTable,
242
255
  );
243
256
  }
244
257
  }
@@ -41,12 +41,12 @@ export const handleMouseDown = (
41
41
  localResizeHandlePos: number,
42
42
  getEditorContainerWidth: GetEditorContainerWidth,
43
43
  getEditorFeatureFlags: GetEditorFeatureFlags,
44
+ isTableScalingEnabled: boolean,
44
45
  editorAnalyticsAPI?: EditorAnalyticsAPI,
45
46
  ): boolean => {
46
47
  const { state, dispatch } = view;
47
48
  const editorDisabled = !view.editable;
48
49
  const domAtPos = view.domAtPos.bind(view);
49
- const { isTableScalingEnabled = false } = getTablePluginState(state);
50
50
 
51
51
  if (
52
52
  editorDisabled ||
@@ -85,7 +85,14 @@ export const handleMouseDown = (
85
85
  getEditorContainerWidth,
86
86
  });
87
87
 
88
- const shouldScale = tableDepth === 0 && isTableScalingEnabled;
88
+ let shouldScale = tableDepth === 0 && isTableScalingEnabled;
89
+ if (
90
+ isTableScalingEnabled &&
91
+ getBooleanFF('platform.editor.table.preserve-widths-with-lock-button')
92
+ ) {
93
+ shouldScale = shouldScale && originalTable.attrs.displayMode !== 'fixed';
94
+ }
95
+
89
96
  const resizeState = getResizeState({
90
97
  minWidth: tableCellMinWidth,
91
98
  maxSize,
@@ -181,7 +188,15 @@ export const handleMouseDown = (
181
188
  selectedColumns.indexOf(colIndex) > -1 ||
182
189
  selectedColumns.indexOf(colIndex + 1) > -1;
183
190
 
184
- const shouldScale = tableDepth === 0 && isTableScalingEnabled;
191
+ let shouldScale = tableDepth === 0 && isTableScalingEnabled;
192
+ if (
193
+ isTableScalingEnabled &&
194
+ getBooleanFF('platform.editor.table.preserve-widths-with-lock-button')
195
+ ) {
196
+ shouldScale =
197
+ shouldScale && originalTable.attrs.displayMode !== 'fixed';
198
+ }
199
+
185
200
  const newResizeState = resizeColumn(
186
201
  resizeState,
187
202
  colIndex,
@@ -277,7 +292,14 @@ export const handleMouseDown = (
277
292
  $cell.nodeAfter!.attrs.colspan -
278
293
  1;
279
294
 
280
- const shouldScale = tableDepth === 0 && isTableScalingEnabled;
295
+ let shouldScale = tableDepth === 0 && isTableScalingEnabled;
296
+ if (
297
+ isTableScalingEnabled &&
298
+ getBooleanFF('platform.editor.table.preserve-widths-with-lock-button')
299
+ ) {
300
+ shouldScale = shouldScale && originalTable.attrs.displayMode !== 'fixed';
301
+ }
302
+
281
303
  resizeColumn(
282
304
  resizeState,
283
305
  colIndex,
@@ -25,6 +25,7 @@ export function createPlugin(
25
25
  getEditorContainerWidth: GetEditorContainerWidth,
26
26
  getEditorFeatureFlags: GetEditorFeatureFlags,
27
27
  editorAnalyticsAPI?: EditorAnalyticsAPI,
28
+ isTableScalingEnabled?: boolean,
28
29
  ) {
29
30
  return new SafePlugin({
30
31
  key: pluginKey,
@@ -79,6 +80,7 @@ export function createPlugin(
79
80
  resizeHandlePos,
80
81
  getEditorContainerWidth,
81
82
  getEditorFeatureFlags,
83
+ isTableScalingEnabled || false,
82
84
  editorAnalyticsAPI,
83
85
  )
84
86
  ) {
@@ -384,14 +384,25 @@ export const getNewResizeStateFromSelectedColumns = (
384
384
  getEditorContainerWidth,
385
385
  });
386
386
 
387
- const resizeState = getResizeState({
387
+ let resizeState;
388
+
389
+ let isTableScalingEnabledOnCurrentTable = isTableScalingEnabled;
390
+ if (
391
+ isTableScalingEnabled &&
392
+ getBooleanFF('platform.editor.table.preserve-widths-with-lock-button')
393
+ ) {
394
+ isTableScalingEnabledOnCurrentTable =
395
+ table.node.attrs.displayMode !== 'fixed';
396
+ }
397
+
398
+ resizeState = getResizeState({
388
399
  minWidth: tableCellMinWidth,
389
400
  maxSize,
390
401
  table: table.node,
391
402
  tableRef,
392
403
  start: table.start,
393
404
  domAtPos,
394
- isTableScalingEnabled,
405
+ isTableScalingEnabled: isTableScalingEnabledOnCurrentTable,
395
406
  });
396
407
 
397
408
  const newResizeState = evenSelectedColumnsWidths(resizeState, rect);
@@ -4,6 +4,7 @@ import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
4
4
  import type { Transaction } from '@atlaskit/editor-prosemirror/state';
5
5
  import type { DomAtPos } from '@atlaskit/editor-prosemirror/utils';
6
6
  import { akEditorTableNumberColumnWidth } from '@atlaskit/editor-shared-styles';
7
+ import { getBooleanFF } from '@atlaskit/platform-feature-flags';
7
8
 
8
9
  import { updateColumnWidths } from '../../../transforms';
9
10
  import { getTableWidth } from '../../../utils';
@@ -38,7 +39,7 @@ export const scale = (
38
39
  tableRef: HTMLTableElement,
39
40
  options: ScaleOptions,
40
41
  domAtPos: DomAtPos,
41
- isTableScalingEnabled = false,
42
+ isTableScalingEnabledOnCurrentTable = false,
42
43
  ): ResizeState | undefined => {
43
44
  const {
44
45
  node,
@@ -87,7 +88,7 @@ export const scale = (
87
88
  tableRef,
88
89
  start,
89
90
  domAtPos,
90
- isTableScalingEnabled,
91
+ isTableScalingEnabled: isTableScalingEnabledOnCurrentTable,
91
92
  });
92
93
 
93
94
  return scaleTableTo(resizeState, newWidth);
@@ -99,7 +100,7 @@ export const scaleWithParent = (
99
100
  table: PMNode,
100
101
  start: number,
101
102
  domAtPos: DomAtPos,
102
- isTableScalingEnabled = false,
103
+ isTableScalingEnabledOnCurrentTable = false,
103
104
  ) => {
104
105
  const resizeState = getResizeState({
105
106
  minWidth: tableCellMinWidth,
@@ -108,7 +109,7 @@ export const scaleWithParent = (
108
109
  tableRef,
109
110
  start,
110
111
  domAtPos,
111
- isTableScalingEnabled,
112
+ isTableScalingEnabled: isTableScalingEnabledOnCurrentTable,
112
113
  });
113
114
 
114
115
  if (table.attrs.isNumberColumnEnabled) {
@@ -163,15 +164,23 @@ export const previewScaleTable = (
163
164
  tableRef.style.width = `${width}px`;
164
165
  }
165
166
 
167
+ let isTableScalingEnabledOnCurrentTable = isTableScalingEnabled;
168
+ if (
169
+ isTableScalingEnabled &&
170
+ getBooleanFF('platform.editor.table.preserve-widths-with-lock-button')
171
+ ) {
172
+ isTableScalingEnabledOnCurrentTable =
173
+ isTableScalingEnabled && node.attrs.displayMode !== 'fixed';
174
+ }
166
175
  // If the table hasn't been resize, the colgroup 48px width values will gracefully scale down.
167
176
  // If we are scaling the table down with isTableScalingEnabled, the colgroup widths may be scaled to a value that is not 48px.
168
- if (!hasTableBeenResized(node) && !isTableScalingEnabled) {
177
+ if (!hasTableBeenResized(node) && !isTableScalingEnabledOnCurrentTable) {
169
178
  syncStickyRowToTable(tableRef);
170
179
  return;
171
180
  }
172
181
 
173
182
  const resizeState = parentWidth
174
- ? scaleWithParent(tableRef, parentWidth, node, start, domAtPos, false)
183
+ ? scaleWithParent(tableRef, parentWidth, node, start, domAtPos, false) // Here last value is isTableScalingEnabled = false
175
184
  : scale(tableRef, options, domAtPos, false);
176
185
 
177
186
  if (resizeState) {
@@ -185,7 +194,7 @@ export const scaleTable =
185
194
  tableRef: HTMLTableElement | null | undefined,
186
195
  options: ScaleOptions,
187
196
  domAtPos: DomAtPos,
188
- isTableScalingEnabled = false,
197
+ isTableScalingEnabledOnCurrentTable = false,
189
198
  ) =>
190
199
  (tr: Transaction) => {
191
200
  if (!tableRef) {
@@ -197,7 +206,7 @@ export const scaleTable =
197
206
  if (hasTableBeenResized(node) === false) {
198
207
  // If its not a re-sized table, we still want to re-create cols
199
208
  // To force reflow of columns upon delete.
200
- if (!isTableScalingEnabled) {
209
+ if (!isTableScalingEnabledOnCurrentTable) {
201
210
  insertColgroupFromNode(tableRef, node);
202
211
  }
203
212
  tr.setMeta('scrollIntoView', false);
@@ -212,10 +221,15 @@ export const scaleTable =
212
221
  node,
213
222
  start,
214
223
  domAtPos,
215
- isTableScalingEnabled,
224
+ isTableScalingEnabledOnCurrentTable,
216
225
  );
217
226
  } else {
218
- resizeState = scale(tableRef, options, domAtPos, isTableScalingEnabled);
227
+ resizeState = scale(
228
+ tableRef,
229
+ options,
230
+ domAtPos,
231
+ isTableScalingEnabledOnCurrentTable,
232
+ );
219
233
  }
220
234
 
221
235
  if (resizeState) {
@@ -23,7 +23,6 @@ import {
23
23
  } from '@atlaskit/editor-shared-styles';
24
24
  import { findTable } from '@atlaskit/editor-tables/utils';
25
25
 
26
- import { getPluginState } from './plugin-factory';
27
26
  import { TABLE_MAX_WIDTH } from './table-resizing/utils';
28
27
 
29
28
  type __ReplaceStep = ReplaceStep & {
@@ -44,6 +43,7 @@ const createPlugin = (
44
43
  dispatch: Dispatch,
45
44
  dispatchAnalyticsEvent: DispatchAnalyticsEvent,
46
45
  fullWidthEnabled: boolean,
46
+ isTableScalingEnabled: boolean,
47
47
  ) => {
48
48
  return new SafePlugin({
49
49
  key: pluginKey,
@@ -122,8 +122,6 @@ const createPlugin = (
122
122
  tr.getMeta('referentialityTableInserted'),
123
123
  );
124
124
 
125
- const { isTableScalingEnabled = false } = getPluginState(newState);
126
-
127
125
  const shouldPatchTable = fullWidthEnabled && isTableScalingEnabled;
128
126
 
129
127
  if (
@@ -1,4 +1,9 @@
1
1
  import { SortOrder } from '@atlaskit/editor-common/types';
2
+ import {
3
+ convertProsemirrorTableNodeToArrayOfRows,
4
+ createCompareNodes,
5
+ } from '@atlaskit/editor-common/utils';
6
+ import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
2
7
  import type { EditorView } from '@atlaskit/editor-prosemirror/view';
3
8
 
4
9
  import {
@@ -21,30 +26,42 @@ export const unsort = (
21
26
  });
22
27
  };
23
28
 
24
- // TODO - reuse sort logic from the Renderer and support switching between ASC, DESC and NO_ORDER
25
29
  export const getSortOrderFromTable = (
26
- tableElement: HTMLElement,
30
+ tableNode: PMNode,
27
31
  sortIndex: number,
28
- direction: string,
32
+ direction: SortOrder,
29
33
  ) => {
30
- const sortOrder = direction === SortOrder.DESC ? -1 : 1;
31
- const strings: string[] = [];
32
- tableElement
33
- .querySelectorAll('tr:not([data-header-row="true"])')
34
- .forEach((tr) => {
35
- strings.push(tr.querySelectorAll('td')[sortIndex]?.textContent || '');
36
- });
37
- const order = Array.from(strings.keys())
34
+ const tableArray = convertProsemirrorTableNodeToArrayOfRows(tableNode);
35
+ tableArray.shift(); // remove header row
36
+
37
+ // Keep track of the origin row index
38
+ const tableArrayWithIndex = tableArray.map((node, index) => ({
39
+ node,
40
+ originalIndex: index,
41
+ }));
42
+
43
+ const compareNodesInOrder = createCompareNodes(
44
+ {
45
+ // TODO - add inline card support
46
+ getInlineCardTextFromStore() {
47
+ return null;
48
+ },
49
+ },
50
+ direction,
51
+ );
52
+ const order = tableArrayWithIndex
38
53
  .sort((a, b) => {
39
- const string = strings[a] || '';
40
- return string.localeCompare(strings[b] || '') * sortOrder;
54
+ return compareNodesInOrder(a.node[sortIndex], b.node[sortIndex]);
41
55
  })
42
- .map((value, index) => ({
43
- value,
56
+ .map((tableRow, index) => ({
57
+ value: tableRow.originalIndex,
44
58
  index,
45
59
  }));
46
- // TODO - improve this. right now this is a workaround to ensure the first tr is always first in the order
47
- return [{ value: -1, index: -1 }, ...order];
60
+ return [
61
+ // Ensures the first tr is always first in the order
62
+ { value: -1, index: -1 },
63
+ ...order,
64
+ ];
48
65
  };
49
66
 
50
67
  export const toggleSort = (
@@ -52,20 +69,29 @@ export const toggleSort = (
52
69
  event: Event,
53
70
  pluginState: TableSortMeta,
54
71
  ) => {
55
- const target = event.target as HTMLElement;
72
+ const target = event.target;
73
+ if (!(target instanceof HTMLElement)) {
74
+ return;
75
+ }
56
76
  const widget = target.closest(`.${SORTING_ICON_CLASS_NAME}`);
57
77
  if (widget?.classList.contains(IS_DISABLED_CLASS_NAME) || !widget) {
58
78
  return;
59
79
  }
60
- let datasetortIndex = target
80
+ let dataSortIndex = target
61
81
  ?.closest('.ProseMirror-widget')
62
82
  ?.getAttribute(SORT_INDEX_DATA_ATTRIBUTE);
63
83
  const tr = view.state.tr;
64
84
  const tableElement = target.closest('table');
65
- if (!tableElement || !datasetortIndex) {
85
+ if (!tableElement || !dataSortIndex) {
86
+ return;
87
+ }
88
+ const tablePos = view.posAtDOM(tableElement, 0);
89
+ const tableNode = view.state.doc.nodeAt(tablePos - 1);
90
+ const tableId = tableNode?.attrs.localId;
91
+
92
+ if (!tableId || !tableNode || tableNode?.type?.name !== 'table') {
66
93
  return;
67
94
  }
68
- const tableId = tableElement.getAttribute('data-table-local-id') || '';
69
95
 
70
96
  let { index, direction, order: oldOrder } = pluginState?.[tableId] || {};
71
97
 
@@ -74,7 +100,7 @@ export const toggleSort = (
74
100
  unsort(oldOrder, tableElement);
75
101
  }
76
102
 
77
- const sortIndex = parseInt(datasetortIndex);
103
+ const sortIndex = parseInt(dataSortIndex);
78
104
  if (sortIndex === index) {
79
105
  switch (direction) {
80
106
  case SortOrder.NO_ORDER:
@@ -91,7 +117,7 @@ export const toggleSort = (
91
117
  direction = SortOrder.ASC; // default direction when a new index is clicked
92
118
  }
93
119
 
94
- const order = getSortOrderFromTable(tableElement, sortIndex, direction);
120
+ const order = getSortOrderFromTable(tableNode, sortIndex, direction);
95
121
 
96
122
  if (direction === SortOrder.NO_ORDER) {
97
123
  tr.setMeta('tableSortMeta', {