@atlaskit/editor-plugin-table 7.4.6 → 7.4.8

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.
@@ -1,5 +1,5 @@
1
1
  import type { EditorAnalyticsAPI, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
2
- import type { Command, EditorCommand, GetEditorContainerWidth } from '@atlaskit/editor-common/types';
2
+ import type { Command, EditorCommand, GetEditorContainerWidth, GetEditorFeatureFlags } from '@atlaskit/editor-common/types';
3
3
  import type { Transaction } from '@atlaskit/editor-prosemirror/state';
4
4
  import type { EditorView } from '@atlaskit/editor-prosemirror/view';
5
5
  export declare function addColumnAt(getEditorContainerWidth: GetEditorContainerWidth): (column: number, allowAddColumnCustomStep: boolean | undefined, view: EditorView | undefined) => (tr: Transaction) => Transaction;
@@ -7,5 +7,5 @@ export declare const addColumnBefore: (getEditorContainerWidth: GetEditorContain
7
7
  export declare const addColumnAfter: (getEditorContainerWidth: GetEditorContainerWidth) => Command;
8
8
  export declare const insertColumn: (getEditorContainerWidth: GetEditorContainerWidth) => (column: number) => Command;
9
9
  export declare const insertRow: (row: number, moveCursorToTheNewRow: boolean) => Command;
10
- export declare const createTable: () => Command;
11
- export declare const insertTableWithSize: (editorAnalyticsAPI: EditorAnalyticsAPI | undefined) => (rowsCount: number, colsCount: number, inputMethod?: INPUT_METHOD.PICKER) => EditorCommand;
10
+ export declare const createTable: (isFullWidthModeEnabled?: boolean, getEditorFeatureFlags?: GetEditorFeatureFlags) => Command;
11
+ export declare const insertTableWithSize: (isFullWidthModeEnabled?: boolean, getEditorFeatureFlags?: GetEditorFeatureFlags, editorAnalyticsAPI?: EditorAnalyticsAPI) => (rowsCount: number, colsCount: number, inputMethod?: INPUT_METHOD.PICKER) => EditorCommand;
@@ -23,6 +23,10 @@ export type TablePlugin = NextEditorPlugin<'table', {
23
23
  actions: {
24
24
  insertTable: InsertTableAction;
25
25
  };
26
+ sharedState: {
27
+ isFullWidthModeEnabled: boolean;
28
+ wasFullWidthModeEnabled: boolean;
29
+ };
26
30
  commands: {
27
31
  insertTableWithSize: (rowsCount: number, colsCount: number, inputMethod?: INPUT_METHOD.PICKER) => EditorCommand;
28
32
  };
@@ -6,12 +6,13 @@
6
6
  import type { DispatchAnalyticsEvent } from '@atlaskit/editor-common/analytics';
7
7
  import type { Dispatch } from '@atlaskit/editor-common/event-dispatcher';
8
8
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
9
+ import type { GetEditorFeatureFlags } from '@atlaskit/editor-common/types';
9
10
  import { PluginKey } from '@atlaskit/editor-prosemirror/state';
10
11
  type TableWidthPluginState = {
11
12
  resizing: boolean;
12
13
  };
13
14
  export declare const pluginKey: PluginKey<TableWidthPluginState>;
14
- declare const createPlugin: (dispatch: Dispatch, dispatchAnalyticsEvent: DispatchAnalyticsEvent, fullWidthEnabled: boolean) => SafePlugin<{
15
+ declare const createPlugin: (dispatch: Dispatch, dispatchAnalyticsEvent: DispatchAnalyticsEvent, fullWidthEnabled: boolean, getEditorFeatureFlags?: GetEditorFeatureFlags) => SafePlugin<{
15
16
  resizing: boolean;
16
17
  }>;
17
18
  export { createPlugin };
@@ -0,0 +1,6 @@
1
+ import type { GetEditorFeatureFlags } from '@atlaskit/editor-common/types';
2
+ import type { Schema } from '@atlaskit/editor-prosemirror/model';
3
+ export declare const createTableWithWidth: (isFullWidthModeEnabled?: boolean, getEditorFeatureFlags?: GetEditorFeatureFlags, createTableProps?: {
4
+ rowsCount?: number;
5
+ colsCount?: number;
6
+ }) => (schema: Schema) => import("prosemirror-model").Node;
@@ -10,3 +10,4 @@ export { getSelectedTableInfo, getSelectedCellInfo } from './analytics';
10
10
  export { getMergedCellsPositions } from './table';
11
11
  export { updatePluginStateDecorations } from './update-plugin-state-decorations';
12
12
  export { hasMergedCellsInColumn, hasMergedCellsInRow, hasMergedCellsInBetween, hasMergedCellsInSelection, findDuplicatePosition, checkEdgeHasMergedCells, } from './merged-cells';
13
+ export { createTableWithWidth } from './create';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-table",
3
- "version": "7.4.6",
3
+ "version": "7.4.8",
4
4
  "description": "Table plugin for the @atlaskit/editor",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"
@@ -28,9 +28,9 @@
28
28
  "runReact18": false
29
29
  },
30
30
  "dependencies": {
31
- "@atlaskit/adf-schema": "^35.5.1",
32
- "@atlaskit/custom-steps": "^0.0.13",
33
- "@atlaskit/editor-common": "^78.9.0",
31
+ "@atlaskit/adf-schema": "^35.5.2",
32
+ "@atlaskit/custom-steps": "^0.0.14",
33
+ "@atlaskit/editor-common": "^78.10.0",
34
34
  "@atlaskit/editor-palette": "1.5.2",
35
35
  "@atlaskit/editor-plugin-analytics": "^1.0.0",
36
36
  "@atlaskit/editor-plugin-content-insertion": "^1.0.0",
@@ -15,6 +15,7 @@ import type {
15
15
  Command,
16
16
  EditorCommand,
17
17
  GetEditorContainerWidth,
18
+ GetEditorFeatureFlags,
18
19
  } from '@atlaskit/editor-common/types';
19
20
  import type { Transaction } from '@atlaskit/editor-prosemirror/state';
20
21
  import { Selection } from '@atlaskit/editor-prosemirror/state';
@@ -24,7 +25,6 @@ import { TableMap } from '@atlaskit/editor-tables/table-map';
24
25
  import {
25
26
  addColumnAt as addColumnAtPMUtils,
26
27
  addRowAt,
27
- createTable as createTableNode,
28
28
  findTable,
29
29
  selectedRect,
30
30
  } from '@atlaskit/editor-tables/utils';
@@ -33,7 +33,11 @@ import { getBooleanFF } from '@atlaskit/platform-feature-flags';
33
33
  import { updateRowOrColumnMovedTransform } from '../pm-plugins/analytics/commands';
34
34
  import { META_KEYS } from '../pm-plugins/table-analytics';
35
35
  import { rescaleColumns } from '../transforms/column-width';
36
- import { checkIfHeaderRowEnabled, copyPreviousRow } from '../utils';
36
+ import {
37
+ checkIfHeaderRowEnabled,
38
+ copyPreviousRow,
39
+ createTableWithWidth,
40
+ } from '../utils';
37
41
  import { getAllowAddColumnCustomStep } from '../utils/get-allow-add-column-custom-step';
38
42
 
39
43
  function addColumnAtCustomStep(column: number) {
@@ -128,7 +132,6 @@ export const addColumnAfter =
128
132
  return true;
129
133
  };
130
134
 
131
- // #region Commands
132
135
  export const insertColumn =
133
136
  (getEditorContainerWidth: GetEditorContainerWidth) =>
134
137
  (column: number): Command =>
@@ -199,31 +202,44 @@ export const insertRow =
199
202
  return true;
200
203
  };
201
204
 
202
- export const createTable = (): Command => (state, dispatch) => {
203
- const table = createTableNode({
204
- schema: state.schema,
205
- });
205
+ export const createTable =
206
+ (
207
+ isFullWidthModeEnabled?: boolean,
208
+ getEditorFeatureFlags?: GetEditorFeatureFlags,
209
+ ): Command =>
210
+ (state, dispatch) => {
211
+ const table = createTableWithWidth(
212
+ isFullWidthModeEnabled,
213
+ getEditorFeatureFlags,
214
+ )(state.schema);
206
215
 
207
- if (dispatch) {
208
- dispatch(safeInsert(table)(state.tr).scrollIntoView());
209
- }
210
- return true;
211
- };
212
- // #endregion
216
+ if (dispatch) {
217
+ dispatch(safeInsert(table)(state.tr).scrollIntoView());
218
+ }
219
+ return true;
220
+ };
213
221
 
214
222
  export const insertTableWithSize =
215
- (editorAnalyticsAPI: EditorAnalyticsAPI | undefined) =>
223
+ (
224
+ isFullWidthModeEnabled?: boolean,
225
+ getEditorFeatureFlags?: GetEditorFeatureFlags,
226
+ editorAnalyticsAPI?: EditorAnalyticsAPI,
227
+ ) =>
216
228
  (
217
229
  rowsCount: number,
218
230
  colsCount: number,
219
231
  inputMethod?: INPUT_METHOD.PICKER,
220
232
  ): EditorCommand => {
221
233
  return ({ tr }) => {
222
- const tableNode = createTableNode({
223
- schema: tr.doc.type.schema,
224
- rowsCount: rowsCount,
225
- colsCount: colsCount,
226
- });
234
+ const tableNode = createTableWithWidth(
235
+ isFullWidthModeEnabled,
236
+ getEditorFeatureFlags,
237
+ {
238
+ rowsCount: rowsCount,
239
+ colsCount: colsCount,
240
+ },
241
+ )(tr.doc.type.schema);
242
+
227
243
  const newTr = safeInsert(tableNode)(tr).scrollIntoView();
228
244
  if (inputMethod) {
229
245
  editorAnalyticsAPI?.attachAnalyticsEvent({
package/src/plugin.tsx CHANGED
@@ -39,7 +39,6 @@ import type { WidthPlugin } from '@atlaskit/editor-plugin-width';
39
39
  import type { Transaction } from '@atlaskit/editor-prosemirror/state';
40
40
  import type { EditorView } from '@atlaskit/editor-prosemirror/view';
41
41
  import { tableEditing } from '@atlaskit/editor-tables/pm-plugins';
42
- import { createTable } from '@atlaskit/editor-tables/utils';
43
42
  import { getBooleanFF } from '@atlaskit/platform-feature-flags';
44
43
 
45
44
  import { insertTableWithSize } from './commands/insert';
@@ -79,7 +78,7 @@ import FloatingDeleteButton from './ui/FloatingDeleteButton';
79
78
  import FloatingDragMenu from './ui/FloatingDragMenu';
80
79
  import FloatingInsertButton from './ui/FloatingInsertButton';
81
80
  import LayoutButton from './ui/LayoutButton';
82
- import { isLayoutSupported } from './utils';
81
+ import { createTableWithWidth, isLayoutSupported } from './utils';
83
82
 
84
83
  export interface TablePluginOptions {
85
84
  tableOptions: PluginConfig;
@@ -106,6 +105,10 @@ export type TablePlugin = NextEditorPlugin<
106
105
  actions: {
107
106
  insertTable: InsertTableAction;
108
107
  };
108
+ sharedState: {
109
+ isFullWidthModeEnabled: boolean;
110
+ wasFullWidthModeEnabled: boolean;
111
+ };
109
112
  commands: {
110
113
  insertTableWithSize: (
111
114
  rowsCount: number,
@@ -140,13 +143,23 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
140
143
  return {
141
144
  name: 'table',
142
145
 
146
+ // Use getSharedState to store fullWidthEnabled and wasFullWidthModeEnabled to guarantee access
147
+ // to most up to date values - passing to createPluginState will not re-initialise the state
148
+ getSharedState: () => {
149
+ return {
150
+ isFullWidthModeEnabled: !!options?.fullWidthEnabled,
151
+ wasFullWidthModeEnabled: !!options?.wasFullWidthEnabled,
152
+ };
153
+ },
154
+
143
155
  actions: {
144
156
  insertTable:
145
157
  (analyticsPayload): Command =>
146
158
  (state, dispatch) => {
147
- const node = createTable({
148
- schema: state.schema,
149
- });
159
+ const node = createTableWithWidth(
160
+ options?.fullWidthEnabled,
161
+ options?.getEditorFeatureFlags,
162
+ )(state.schema);
150
163
 
151
164
  return (
152
165
  api?.contentInsertion?.actions?.insert({
@@ -162,8 +175,13 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
162
175
  );
163
176
  },
164
177
  },
178
+
165
179
  commands: {
166
- insertTableWithSize: insertTableWithSize(api?.analytics?.actions),
180
+ insertTableWithSize: insertTableWithSize(
181
+ options?.fullWidthEnabled,
182
+ options?.getEditorFeatureFlags,
183
+ api?.analytics?.actions,
184
+ ),
167
185
  },
168
186
 
169
187
  nodes() {
@@ -321,6 +339,7 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
321
339
  dispatch,
322
340
  dispatchAnalyticsEvent,
323
341
  options?.fullWidthEnabled ?? false,
342
+ options?.getEditorFeatureFlags,
324
343
  )
325
344
  : undefined,
326
345
  },
@@ -558,10 +577,14 @@ const tablesPlugin: TablePlugin = ({ config: options, api }) => {
558
577
  keyshortcut: tooltip(toggleTable),
559
578
  icon: () => <IconTable />,
560
579
  action(insert, state) {
580
+ // see comment on tablesPlugin.getSharedState on usage
581
+ const tableState = api?.table?.sharedState.currentState();
582
+
561
583
  const tr = insert(
562
- createTable({
563
- schema: state.schema,
564
- }),
584
+ createTableWithWidth(
585
+ tableState?.isFullWidthModeEnabled,
586
+ options?.getEditorFeatureFlags,
587
+ )(state.schema),
565
588
  );
566
589
  editorAnalyticsAPI?.attachAnalyticsEvent({
567
590
  action: ACTION.INSERTED,
@@ -14,6 +14,7 @@ import {
14
14
  import type { DispatchAnalyticsEvent } from '@atlaskit/editor-common/analytics';
15
15
  import type { Dispatch } from '@atlaskit/editor-common/event-dispatcher';
16
16
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
17
+ import type { GetEditorFeatureFlags } from '@atlaskit/editor-common/types';
17
18
  import { PluginKey } from '@atlaskit/editor-prosemirror/state';
18
19
  import { ReplaceStep } from '@atlaskit/editor-prosemirror/transform';
19
20
  import {
@@ -23,6 +24,8 @@ import {
23
24
  } from '@atlaskit/editor-shared-styles';
24
25
  import { findTable } from '@atlaskit/editor-tables/utils';
25
26
 
27
+ import { TABLE_MAX_WIDTH } from './table-resizing/utils';
28
+
26
29
  type __ReplaceStep = ReplaceStep & {
27
30
  // Properties `to` and `from` are private attributes of ReplaceStep.
28
31
  to: number;
@@ -41,8 +44,9 @@ const createPlugin = (
41
44
  dispatch: Dispatch,
42
45
  dispatchAnalyticsEvent: DispatchAnalyticsEvent,
43
46
  fullWidthEnabled: boolean,
44
- ) =>
45
- new SafePlugin({
47
+ getEditorFeatureFlags?: GetEditorFeatureFlags,
48
+ ) => {
49
+ return new SafePlugin({
46
50
  key: pluginKey,
47
51
  state: {
48
52
  init() {
@@ -83,6 +87,7 @@ const createPlugin = (
83
87
  },
84
88
  });
85
89
  }
90
+
86
91
  // When document first load in Confluence, initially it is an empty document
87
92
  // and Collab service triggers a transaction to replace the empty document with the real document that should be rendered.
88
93
  // what we need to do is to add width attr to all tables in the real document
@@ -114,12 +119,24 @@ const createPlugin = (
114
119
  return hasStepReplacingEntireDocument;
115
120
  });
116
121
 
117
- if (!isReplaceDocumentOperation) {
122
+ const referentialityTr = transactions.find((tr) =>
123
+ tr.getMeta('referentialityTableInserted'),
124
+ );
125
+
126
+ const shouldPatchTable =
127
+ fullWidthEnabled &&
128
+ getEditorFeatureFlags &&
129
+ getEditorFeatureFlags()['tablePreserveWidth'];
130
+
131
+ if (
132
+ !isReplaceDocumentOperation &&
133
+ (!shouldPatchTable || !referentialityTr)
134
+ ) {
118
135
  return null;
119
136
  }
120
137
 
121
- const tr = newState.tr;
122
138
  const { table } = newState.schema.nodes;
139
+ const tr = newState.tr;
123
140
 
124
141
  /**
125
142
  * Select table event
@@ -138,47 +155,63 @@ const createPlugin = (
138
155
  });
139
156
  }
140
157
 
141
- newState.doc.forEach((node, offset) => {
142
- if (node.type === table) {
143
- const width = node.attrs.width;
144
- const layout = node.attrs.layout;
145
-
146
- if (!width && layout) {
147
- let tableWidthCal;
148
-
149
- if (fullWidthEnabled) {
150
- tableWidthCal = akEditorFullWidthLayoutWidth;
151
- } else {
152
- switch (layout) {
153
- case 'wide':
154
- tableWidthCal = akEditorWideLayoutWidth;
155
- break;
156
- case 'full-width':
157
- tableWidthCal = akEditorFullWidthLayoutWidth;
158
- break;
159
- // when in fix-width appearance, no need to assign value to table width attr
160
- // as when table is created, width attr is null by default, table rendered using layout attr
161
- default:
162
- tableWidthCal = akEditorDefaultLayoutWidth;
163
- break;
158
+ if (isReplaceDocumentOperation) {
159
+ newState.doc.forEach((node, offset) => {
160
+ if (node.type === table) {
161
+ const width = node.attrs.width;
162
+ const layout = node.attrs.layout;
163
+
164
+ if (!width && layout) {
165
+ let tableWidthCal;
166
+
167
+ if (fullWidthEnabled) {
168
+ tableWidthCal = akEditorFullWidthLayoutWidth;
169
+ } else {
170
+ switch (layout) {
171
+ case 'wide':
172
+ tableWidthCal = akEditorWideLayoutWidth;
173
+ break;
174
+ case 'full-width':
175
+ tableWidthCal = akEditorFullWidthLayoutWidth;
176
+ break;
177
+ // when in fix-width appearance, no need to assign value to table width attr
178
+ // as when table is created, width attr is null by default, table rendered using layout attr
179
+ default:
180
+ tableWidthCal = akEditorDefaultLayoutWidth;
181
+ break;
182
+ }
164
183
  }
165
- }
166
184
 
167
- const { width, ...rest } = node.attrs;
185
+ const { width, ...rest } = node.attrs;
168
186
 
169
- if (tableWidthCal) {
170
- tr.step(
171
- new SetAttrsStep(offset, {
172
- width: tableWidthCal,
173
- ...rest,
174
- }),
175
- );
187
+ if (tableWidthCal) {
188
+ tr.step(
189
+ new SetAttrsStep(offset, {
190
+ width: tableWidthCal,
191
+ ...rest,
192
+ }),
193
+ );
194
+ }
176
195
  }
177
196
  }
178
- }
179
- });
197
+ });
198
+ }
199
+
200
+ if (referentialityTr) {
201
+ referentialityTr.steps.forEach((step) => {
202
+ step.getMap().forEach((oldStart, oldEnd, newStart, newEnd) => {
203
+ newState.doc.nodesBetween(newStart, newEnd, (node, pos) => {
204
+ if (node.type === table && node.attrs.width !== TABLE_MAX_WIDTH) {
205
+ tr.setNodeAttribute(pos, 'width', TABLE_MAX_WIDTH);
206
+ }
207
+ });
208
+ });
209
+ });
210
+ }
211
+
180
212
  return tr;
181
213
  },
182
214
  });
215
+ };
183
216
 
184
217
  export { createPlugin };
@@ -0,0 +1,32 @@
1
+ import type { GetEditorFeatureFlags } from '@atlaskit/editor-common/types';
2
+ import type { Schema } from '@atlaskit/editor-prosemirror/model';
3
+ import { createTable } from '@atlaskit/editor-tables/utils';
4
+
5
+ import { TABLE_MAX_WIDTH } from '../pm-plugins/table-resizing/utils';
6
+
7
+ export const createTableWithWidth =
8
+ (
9
+ isFullWidthModeEnabled?: boolean,
10
+ getEditorFeatureFlags?: GetEditorFeatureFlags,
11
+ createTableProps?: {
12
+ rowsCount?: number;
13
+ colsCount?: number;
14
+ },
15
+ ) =>
16
+ (schema: Schema) => {
17
+ const { tablePreserveWidth = false } = getEditorFeatureFlags
18
+ ? getEditorFeatureFlags()
19
+ : {};
20
+
21
+ if (tablePreserveWidth && isFullWidthModeEnabled) {
22
+ return createTable({
23
+ schema,
24
+ tableWidth: TABLE_MAX_WIDTH,
25
+ ...createTableProps,
26
+ });
27
+ }
28
+ return createTable({
29
+ schema,
30
+ ...createTableProps,
31
+ });
32
+ };
@@ -86,3 +86,4 @@ export {
86
86
  findDuplicatePosition,
87
87
  checkEdgeHasMergedCells,
88
88
  } from './merged-cells';
89
+ export { createTableWithWidth } from './create';