@atlaskit/editor-plugin-table 7.7.3 → 7.8.0

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 (59) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/cjs/commands/display-mode.js +23 -0
  3. package/dist/cjs/commands/index.js +8 -1
  4. package/dist/cjs/nodeviews/table.js +2 -1
  5. package/dist/cjs/plugin.js +3 -2
  6. package/dist/cjs/pm-plugins/table-resizing/utils/resize-column.js +19 -5
  7. package/dist/cjs/pm-plugins/table-resizing/utils/resize-state.js +5 -0
  8. package/dist/cjs/toolbar.js +43 -12
  9. package/dist/cjs/ui/icons/DisplayModeIcon.js +46 -0
  10. package/dist/cjs/ui/icons/index.js +7 -0
  11. package/dist/es2019/commands/display-mode.js +17 -0
  12. package/dist/es2019/commands/index.js +2 -1
  13. package/dist/es2019/nodeviews/table.js +2 -1
  14. package/dist/es2019/plugin.js +4 -3
  15. package/dist/es2019/pm-plugins/table-resizing/utils/resize-column.js +19 -5
  16. package/dist/es2019/pm-plugins/table-resizing/utils/resize-state.js +5 -0
  17. package/dist/es2019/toolbar.js +41 -12
  18. package/dist/es2019/ui/icons/DisplayModeIcon.js +39 -0
  19. package/dist/es2019/ui/icons/index.js +1 -0
  20. package/dist/esm/commands/display-mode.js +16 -0
  21. package/dist/esm/commands/index.js +2 -1
  22. package/dist/esm/nodeviews/table.js +2 -1
  23. package/dist/esm/plugin.js +4 -3
  24. package/dist/esm/pm-plugins/table-resizing/utils/resize-column.js +19 -5
  25. package/dist/esm/pm-plugins/table-resizing/utils/resize-state.js +5 -0
  26. package/dist/esm/toolbar.js +44 -13
  27. package/dist/esm/ui/icons/DisplayModeIcon.js +39 -0
  28. package/dist/esm/ui/icons/index.js +1 -0
  29. package/dist/types/commands/display-mode.d.ts +2 -0
  30. package/dist/types/commands/index.d.ts +1 -0
  31. package/dist/types/commands/toggle.d.ts +1 -1
  32. package/dist/types/pm-plugins/table-resizing/utils/misc.d.ts +3 -3
  33. package/dist/types/pm-plugins/table-resizing/utils/resize-state.d.ts +1 -2
  34. package/dist/types/toolbar.d.ts +2 -1
  35. package/dist/types/ui/icons/DisplayModeIcon.d.ts +4 -0
  36. package/dist/types/ui/icons/index.d.ts +1 -0
  37. package/dist/types-ts4.5/commands/display-mode.d.ts +2 -0
  38. package/dist/types-ts4.5/commands/index.d.ts +1 -0
  39. package/dist/types-ts4.5/commands/toggle.d.ts +1 -1
  40. package/dist/types-ts4.5/pm-plugins/table-resizing/utils/misc.d.ts +3 -3
  41. package/dist/types-ts4.5/pm-plugins/table-resizing/utils/resize-state.d.ts +1 -2
  42. package/dist/types-ts4.5/toolbar.d.ts +2 -1
  43. package/dist/types-ts4.5/ui/icons/DisplayModeIcon.d.ts +4 -0
  44. package/dist/types-ts4.5/ui/icons/index.d.ts +1 -0
  45. package/package.json +16 -13
  46. package/src/commands/display-mode.ts +19 -0
  47. package/src/commands/index.ts +1 -0
  48. package/src/commands/toggle.ts +1 -1
  49. package/src/commands-with-analytics.ts +3 -2
  50. package/src/nodeviews/table.tsx +1 -0
  51. package/src/plugin.tsx +15 -2
  52. package/src/pm-plugins/table-resizing/utils/misc.ts +3 -3
  53. package/src/pm-plugins/table-resizing/utils/resize-column.ts +35 -7
  54. package/src/pm-plugins/table-resizing/utils/resize-state.ts +6 -5
  55. package/src/toolbar.tsx +56 -22
  56. package/src/transforms/fix-tables.ts +1 -2
  57. package/src/types.ts +5 -0
  58. package/src/ui/icons/DisplayModeIcon.tsx +41 -0
  59. package/src/ui/icons/index.ts +1 -0
@@ -15,7 +15,7 @@ import { META_KEYS } from '../pm-plugins/table-analytics';
15
15
  * Table layout toggle logic
16
16
  * default -> wide -> full-width -> default
17
17
  */
18
- export const getNextLayout = (currentLayout: TableLayout): TableLayout => {
18
+ export const getNextLayout = (currentLayout: TableLayout) => {
19
19
  switch (currentLayout) {
20
20
  case 'default':
21
21
  return 'wide';
@@ -1,6 +1,5 @@
1
1
  import type { IntlShape } from 'react-intl-next/src/types';
2
2
 
3
- import type { TableLayout } from '@atlaskit/adf-schema';
4
3
  import { tableBackgroundColorPalette } from '@atlaskit/adf-schema';
5
4
  import type { TableSortOrder as SortOrder } from '@atlaskit/custom-steps';
6
5
  import {
@@ -535,7 +534,9 @@ export const toggleTableLayoutWithAnalytics = (
535
534
  );
536
535
 
537
536
  if (table) {
538
- const { layout } = table.node.attrs as { layout: TableLayout };
537
+ const { layout } = table.node.attrs as {
538
+ layout: 'default' | 'wide' | 'full-width';
539
+ };
539
540
  return {
540
541
  action: TABLE_ACTION.CHANGED_BREAKOUT_MODE,
541
542
  actionSubject: ACTION_SUBJECT.TABLE,
@@ -50,6 +50,7 @@ const tableAttributes = (node: PmNode) => {
50
50
  'data-autosize': node.attrs.__autoSize,
51
51
  'data-table-local-id': node.attrs.localId || '',
52
52
  'data-table-width': node.attrs.width,
53
+ 'data-table-display-mode': node.attrs.displayMode,
53
54
  };
54
55
  };
55
56
 
package/src/plugin.tsx CHANGED
@@ -1,6 +1,12 @@
1
1
  import React from 'react';
2
2
 
3
- import { table, tableCell, tableHeader, tableRow } from '@atlaskit/adf-schema';
3
+ import {
4
+ table,
5
+ tableCell,
6
+ tableHeader,
7
+ tableRow,
8
+ tableStage0,
9
+ } from '@atlaskit/adf-schema';
4
10
  import type { AnalyticsEventPayload } from '@atlaskit/editor-common/analytics';
5
11
  import {
6
12
  ACTION,
@@ -223,8 +229,14 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
223
229
  },
224
230
 
225
231
  nodes() {
232
+ const tableNode =
233
+ options?.isTableScalingEnabled &&
234
+ getBooleanFF('platform.editor.table.preserve-widths-with-lock-button')
235
+ ? tableStage0
236
+ : table;
237
+
226
238
  return [
227
- { name: 'table', node: table },
239
+ { name: 'table', node: tableNode },
228
240
  { name: 'tableHeader', node: tableHeader },
229
241
  { name: 'tableRow', node: tableRow },
230
242
  { name: 'tableCell', node: tableCell },
@@ -610,6 +622,7 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
610
622
  editorAnalyticsAPI,
611
623
  options?.getEditorFeatureFlags || defaultGetEditorFeatureFlags,
612
624
  () => editorViewRef.current,
625
+ options,
613
626
  )(pluginConfig(options?.tableOptions)),
614
627
  },
615
628
  };
@@ -1,4 +1,4 @@
1
- import type { CellAttributes, TableLayout } from '@atlaskit/adf-schema';
1
+ import type { CellAttributes } from '@atlaskit/adf-schema';
2
2
  import {
3
3
  getParentNodeWidth,
4
4
  getTableContainerWidth,
@@ -34,7 +34,7 @@ import { MAX_SCALING_PERCENT } from './consts';
34
34
 
35
35
  // Translates named layouts in number values.
36
36
  export function getLayoutSize(
37
- tableLayout: TableLayout,
37
+ tableLayout: 'default' | 'wide' | 'full-width',
38
38
  containerWidth: number = 0,
39
39
  options: TableOptions,
40
40
  ): number {
@@ -112,7 +112,7 @@ interface getTableMaxWidthProps {
112
112
  table: PMNode;
113
113
  tableStart: number;
114
114
  state: EditorState;
115
- layout: TableLayout;
115
+ layout: 'default' | 'wide' | 'full-width';
116
116
  getEditorContainerWidth: GetEditorContainerWidth;
117
117
  }
118
118
 
@@ -18,12 +18,39 @@ export const resizeColumn = (
18
18
  selectedColumns?: number[],
19
19
  isTableScalingEnabled = false,
20
20
  ): ResizeState => {
21
+ // If table resize to show overflowed content, we need to re calculate the resize amount because now offset = amount amount instead of amount * 2
21
22
  let scalePercent = 1;
22
- let resizeAmount = getBooleanFF(
23
- 'platform.editor.table.colum-resizing-improvements',
24
- )
25
- ? amount * 2
26
- : amount;
23
+ let resizeAmount: number;
24
+
25
+ if (getBooleanFF('platform.editor.table.colum-resizing-improvements')) {
26
+ // when table initially not overflow, but enter overflow, we need to calculate the resize amount by two part, before and after overflow, before overflow should be double, after should not be double.
27
+ const tableWidth = tableRef?.clientWidth;
28
+ const tableContainerWidth = tableRef.closest('.resizer-item')?.clientWidth;
29
+
30
+ const isOverflowed = !!(
31
+ tableWidth &&
32
+ tableContainerWidth &&
33
+ tableWidth > tableContainerWidth
34
+ );
35
+
36
+ resizeAmount = amount * 2;
37
+
38
+ if (isOverflowed) {
39
+ resizeAmount =
40
+ amount < 0
41
+ ? amount
42
+ : resizeAmount -
43
+ (tableNode.attrs.width + resizeAmount - tableContainerWidth) / 2;
44
+ } else {
45
+ resizeAmount =
46
+ amount > 0 && tableContainerWidth
47
+ ? resizeAmount -
48
+ (tableNode.attrs.width + resizeAmount - tableContainerWidth) / 2
49
+ : resizeAmount;
50
+ }
51
+ } else {
52
+ resizeAmount = amount;
53
+ }
27
54
 
28
55
  // This need to be clean up if clean up the FF, we will not need this scale logic because now full table width changed/updated in resizeColumn
29
56
  if (
@@ -44,7 +71,9 @@ export const resizeColumn = (
44
71
  ? shrinkColumn(resizeState, colIndex, resizeAmount, selectedColumns)
45
72
  : resizeState;
46
73
 
47
- updateColgroup(newState, tableRef, tableNode, isTableScalingEnabled);
74
+ getBooleanFF('platform.editor.table.colum-resizing-improvements')
75
+ ? updateColgroup(newState, tableRef, tableNode, false)
76
+ : updateColgroup(newState, tableRef, tableNode, isTableScalingEnabled);
48
77
 
49
78
  if (getBooleanFF('platform.editor.table.colum-resizing-improvements')) {
50
79
  // use the difference in width from affected column to update overall table width
@@ -65,7 +94,6 @@ const updateTable = (
65
94
  resizeAmount: number,
66
95
  tableRef: HTMLElement,
67
96
  tableNode: PmNode,
68
- // isTableScalingEnabled: boolean,
69
97
  ) => {
70
98
  const currentWidth = getTableContainerElementWidth(tableNode);
71
99
  const resizingContainer = tableRef.closest(
@@ -1,4 +1,3 @@
1
- import type { TableLayout } from '@atlaskit/adf-schema';
2
1
  import { getTableContainerWidth } from '@atlaskit/editor-common/node-width';
3
2
  import {
4
3
  tableCellMinWidth,
@@ -113,8 +112,12 @@ export const updateColgroup = (
113
112
  isTableScalingEnabled?: boolean,
114
113
  ): void => {
115
114
  const cols = tableRef.querySelectorAll('col');
116
-
117
115
  const columnsCount = cols.length;
116
+ /**
117
+ updateColgroup will update whole table scale when click the column resize handle, this behavior will affect when table overflowed, when now resize handle been dragged and extend to make table overflowed, table will show overflow. This need to be confirmed because it conflict with how isTableScalingEnabled work.
118
+ We don't want to scale the table when resizing columns, only when viewpoint shrinks the table.
119
+ We need to remove !isColumnResizing if we handled auto scale table when mouseUp event.
120
+ * */
118
121
  if (isTableScalingEnabled && tableNode) {
119
122
  const scalePercent = getTableScalingPercent(tableNode, tableRef);
120
123
  state.cols
@@ -321,9 +324,7 @@ export const areColumnsEven = (resizeState: ResizeState): boolean => {
321
324
  };
322
325
 
323
326
  // Get the layout
324
- export const normaliseTableLayout = (
325
- input: string | undefined | null,
326
- ): TableLayout => {
327
+ export const normaliseTableLayout = (input: string | undefined | null) => {
327
328
  switch (input) {
328
329
  case 'wide':
329
330
  return input;
package/src/toolbar.tsx CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  import commonMessages, {
14
14
  tableMessages as messages,
15
15
  } from '@atlaskit/editor-common/messages';
16
+ import { editorCommandToPMCommand } from '@atlaskit/editor-common/preset';
16
17
  import type { typeOption } from '@atlaskit/editor-common/src/types/floating-toolbar';
17
18
  import type {
18
19
  Command,
@@ -61,6 +62,7 @@ import {
61
62
  hoverRows,
62
63
  hoverTable,
63
64
  removeDescendantNodes,
65
+ setTableDisplayMode,
64
66
  } from './commands';
65
67
  import {
66
68
  deleteColumnsWithAnalytics,
@@ -79,6 +81,7 @@ import {
79
81
  toggleNumberColumnWithAnalytics,
80
82
  wrapTableInExpandWithAnalytics,
81
83
  } from './commands-with-analytics';
84
+ import type { TablePluginOptions } from './plugin';
82
85
  import { getPluginState } from './pm-plugins/plugin-factory';
83
86
  import { pluginKey as tableResizingPluginKey } from './pm-plugins/table-resizing';
84
87
  import { getNewResizeStateFromSelectedColumns } from './pm-plugins/table-resizing/utils/resize-state';
@@ -91,6 +94,7 @@ import type {
91
94
  ToolbarMenuState,
92
95
  } from './types';
93
96
  import { TableCssClassName } from './types';
97
+ import { DisplayModeIcon } from './ui/icons';
94
98
  import { FullWidthDisplay } from './ui/TableFullWidthLabel';
95
99
  import {
96
100
  getMergedCellsPositions,
@@ -450,6 +454,7 @@ export const getToolbarConfig =
450
454
  editorAnalyticsAPI: EditorAnalyticsAPI | undefined | null,
451
455
  getEditorFeatureFlags: GetEditorFeatureFlags,
452
456
  getEditorView: () => EditorView | null,
457
+ options?: TablePluginOptions,
453
458
  ) =>
454
459
  (config: PluginConfig): FloatingToolbarHandler =>
455
460
  (state, intl) => {
@@ -465,7 +470,7 @@ export const getToolbarConfig =
465
470
  const currentTableNodeLocalId = tableObject?.node?.attrs?.localId ?? '';
466
471
 
467
472
  if (
468
- isTableScalingEnabled &&
473
+ isTableScalingEnabled && // TODO ED-21670: check if need options.isTableScalingEnabled
469
474
  isWidthResizing &&
470
475
  widthToWidest &&
471
476
  currentTableNodeLocalId &&
@@ -526,7 +531,7 @@ export const getToolbarConfig =
526
531
  editorAnalyticsAPI,
527
532
  );
528
533
 
529
- const { isTableScalingEnabled = false } = getPluginState(state);
534
+ const { isTableScalingEnabled = false } = getPluginState(state); // TODO ED-21670: check if we need options.isTableScalingEnabled and not an acutual state of displayNode attr on the table
530
535
 
531
536
  let cellItems: Array<FloatingToolbarItem<Command>>;
532
537
  cellItems = pluginState.isDragAndDropEnabled
@@ -548,6 +553,7 @@ export const getToolbarConfig =
548
553
  intl,
549
554
  getEditorContainerWidth,
550
555
  editorAnalyticsAPI,
556
+ options,
551
557
  isTableScalingEnabled,
552
558
  )
553
559
  : [];
@@ -728,6 +734,7 @@ const getColumnSettingItems = (
728
734
  { formatMessage }: ToolbarMenuContext,
729
735
  getEditorContainerWidth: GetEditorContainerWidth,
730
736
  editorAnalyticsAPI: EditorAnalyticsAPI | undefined | null,
737
+ options: TablePluginOptions | undefined,
731
738
  isTableScalingEnabled = false,
732
739
  ): Array<FloatingToolbarItem<Command>> => {
733
740
  const pluginState = getPluginState(editorState);
@@ -742,35 +749,62 @@ const getColumnSettingItems = (
742
749
  editorState,
743
750
  editorView.domAtPos.bind(editorView),
744
751
  getEditorContainerWidth,
745
- isTableScalingEnabled,
752
+ isTableScalingEnabled, // TODO ED-21670: Here should be actual state of tableNode's displayMode attr
746
753
  );
747
754
 
748
755
  const wouldChange = newResizeStateWithAnalytics?.changed ?? false;
749
756
 
757
+ const items: Array<FloatingToolbarItem<Command>> = [];
758
+
759
+ const isTableScalingLockBtnEnabled =
760
+ options?.isTableScalingEnabled &&
761
+ getBooleanFF('platform.editor.table.preserve-widths-with-lock-button');
762
+
763
+ if (isTableScalingLockBtnEnabled) {
764
+ const areColumnWidthsLocked =
765
+ pluginState?.tableNode?.attrs.displayMode === 'fixed';
766
+
767
+ const title = areColumnWidthsLocked
768
+ ? formatMessage(messages.unlockColumnWidths)
769
+ : formatMessage(messages.lockColumnWidths);
770
+
771
+ items.push({
772
+ id: 'editor.table.lockColumns',
773
+ type: 'button',
774
+ title,
775
+ icon: () => <DisplayModeIcon size="medium" label={title} />,
776
+ onClick: editorCommandToPMCommand(setTableDisplayMode),
777
+ selected: areColumnWidthsLocked,
778
+ testId: 'table-lock-column-widths-btn',
779
+ });
780
+ }
781
+
750
782
  if (
751
783
  pluginState?.pluginConfig?.allowDistributeColumns &&
752
784
  pluginState.isDragAndDropEnabled
753
785
  ) {
754
- return [
755
- {
756
- id: 'editor.table.distributeColumns',
757
- type: 'button',
758
- title: formatMessage(messages.distributeColumns),
759
- icon: DistributeColumnIcon,
760
- onClick: (state, dispatch, view) =>
761
- getDistributeConfig(
762
- getEditorContainerWidth,
763
- editorAnalyticsAPI,
764
- isTableScalingEnabled,
765
- )(state, dispatch, view),
766
- disabled: !wouldChange,
767
- },
768
- {
769
- type: 'separator',
770
- },
771
- ];
786
+ items.push({
787
+ id: 'editor.table.distributeColumns',
788
+ type: 'button',
789
+ title: formatMessage(messages.distributeColumns),
790
+ icon: DistributeColumnIcon,
791
+ onClick: (state, dispatch, view) =>
792
+ getDistributeConfig(
793
+ getEditorContainerWidth,
794
+ editorAnalyticsAPI,
795
+ isTableScalingEnabled, // TODO ED-21670: Maybe here too we an actual state of tableNode's displayMode attr
796
+ )(state, dispatch, view),
797
+ disabled: !wouldChange,
798
+ });
772
799
  }
773
- return [];
800
+
801
+ if (items.length !== 0) {
802
+ items.push({
803
+ type: 'separator',
804
+ });
805
+ }
806
+
807
+ return items;
774
808
  };
775
809
 
776
810
  const getColorPicker = (
@@ -1,4 +1,3 @@
1
- import type { TableLayout } from '@atlaskit/adf-schema';
2
1
  import { tableCellMinWidth } from '@atlaskit/editor-common/styles';
3
2
  import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
4
3
  import type { Transaction } from '@atlaskit/editor-prosemirror/state';
@@ -186,7 +185,7 @@ export const fixAutoSizedTable = (
186
185
  .setMeta('addToHistory', false);
187
186
  };
188
187
 
189
- const getLayoutBasedOnWidth = (totalWidth: number): TableLayout => {
188
+ const getLayoutBasedOnWidth = (totalWidth: number) => {
190
189
  if (totalWidth > akEditorWideLayoutWidth) {
191
190
  return 'full-width';
192
191
  } else if (
package/src/types.ts CHANGED
@@ -174,6 +174,11 @@ export interface TablePluginState {
174
174
  isTableResizingEnabled?: boolean;
175
175
  isDragAndDropEnabled?: boolean;
176
176
  isTableHovered?: boolean;
177
+
178
+ // Currently isTableScalingEnabled is the same as options.isTableScalingEnabled from TablePluginOptions.
179
+ // However, if you want to learn if tablePlugin is configured to enable Preserve Table Widths feature,
180
+ // use options.isTableScalingEnabled and avoid using pluginState.isTableScalingEnabled or
181
+ // const { isTableScalingEnabled } = getPluginState(state) for that purpose.
177
182
  isTableScalingEnabled?: boolean;
178
183
  }
179
184
 
@@ -0,0 +1,41 @@
1
+ import React from 'react';
2
+
3
+ import Icon from '@atlaskit/icon';
4
+ import type { CustomGlyphProps, GlyphProps } from '@atlaskit/icon/types';
5
+
6
+ export const DisplayModeGlyth = (props: CustomGlyphProps) => {
7
+ return (
8
+ <svg
9
+ width="24"
10
+ height="24"
11
+ viewBox="0 0 24 24"
12
+ fill="none"
13
+ xmlns="http://www.w3.org/2000/svg"
14
+ {...props}
15
+ >
16
+ <rect x="4" y="5" width="4" height="14" rx="1" fill="currentColor" />
17
+ <path
18
+ fillRule="evenodd"
19
+ clipRule="evenodd"
20
+ d="M11 5C10.4477 5 10 5.44772 10 6V9.10102C11.0503 8.02921 12.4424 7.29368 14 7.07089V6C14 5.44772 13.5523 5 13 5H11Z"
21
+ fill="currentColor"
22
+ />
23
+ <path
24
+ fillRule="evenodd"
25
+ clipRule="evenodd"
26
+ d="M17 5C16.4477 5 16 5.44772 16 6V7.07089C17.5576 7.29368 18.9497 8.02921 20 9.10102V6C20 5.44772 19.5523 5 19 5H17Z"
27
+ fill="currentColor"
28
+ />
29
+ <path
30
+ fillRule="evenodd"
31
+ clipRule="evenodd"
32
+ d="M21 14C21 17.3137 18.3137 20 15 20C11.6863 20 9 17.3137 9 14C9 10.6863 11.6863 8 15 8C18.3137 8 21 10.6863 21 14ZM12 15C12 14.4477 12.4477 14 13 14V12C13 10.8954 13.8954 10 15 10C16.1046 10 17 10.8954 17 12V14C17.5523 14 18 14.4477 18 15V17C18 17.5523 17.5523 18 17 18H13C12.4477 18 12 17.5523 12 17V15ZM16 14V12C16 11.4477 15.5523 11 15 11C14.4477 11 14 11.4477 14 12V14H16Z"
33
+ fill="currentColor"
34
+ />
35
+ </svg>
36
+ );
37
+ };
38
+
39
+ export const DisplayModeIcon = (props: GlyphProps) => {
40
+ return <Icon glyph={DisplayModeGlyth} {...props} />;
41
+ };
@@ -1,6 +1,7 @@
1
1
  export { DragHandleIcon } from './DragHandleIcon';
2
2
  export { DragInMotionIcon } from './DragInMotionIcon';
3
3
  export { DragHandleDisabledIcon } from './DragHandleDisabledIcon';
4
+ export { DisplayModeIcon } from './DisplayModeIcon';
4
5
  export { MinimisedHandleIcon } from './MinimisedHandle';
5
6
  export { MergeCellsIcon } from './MergeCellsIcon';
6
7
  export { SplitCellIcon } from './SplitCellIcon';