@atlaskit/editor-plugin-table 1.2.5 → 1.2.7

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 (38) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cjs/plugins/table/commands/selection.js +4 -4
  3. package/dist/cjs/plugins/table/event-handlers.js +3 -3
  4. package/dist/cjs/plugins/table/nodeviews/OverflowShadowsObserver.js +5 -6
  5. package/dist/cjs/plugins/table/pm-plugins/sticky-headers/nodeviews/tableRow.js +9 -16
  6. package/dist/cjs/plugins/table/pm-plugins/table-local-id.js +65 -37
  7. package/dist/cjs/plugins/table/pm-plugins/table-resizing/utils/resize-logic.js +4 -4
  8. package/dist/cjs/plugins/table/transforms/delete-rows.js +2 -2
  9. package/dist/cjs/plugins/table/types.js +8 -8
  10. package/dist/cjs/plugins/table/ui/FloatingContextualMenu/ContextualMenu.js +1 -1
  11. package/dist/cjs/plugins/table/ui/FloatingDeleteButton/index.js +6 -7
  12. package/dist/cjs/plugins/table/ui/TableFloatingControls/index.js +1 -1
  13. package/dist/cjs/version.json +1 -1
  14. package/dist/es2019/plugins/table/commands/selection.js +3 -3
  15. package/dist/es2019/plugins/table/nodeviews/OverflowShadowsObserver.js +5 -7
  16. package/dist/es2019/plugins/table/pm-plugins/sticky-headers/nodeviews/tableRow.js +8 -14
  17. package/dist/es2019/plugins/table/pm-plugins/table-local-id.js +47 -38
  18. package/dist/es2019/plugins/table/pm-plugins/table-resizing/utils/resize-logic.js +4 -4
  19. package/dist/es2019/plugins/table/types.js +6 -6
  20. package/dist/es2019/plugins/table/ui/FloatingContextualMenu/ContextualMenu.js +1 -1
  21. package/dist/es2019/plugins/table/ui/FloatingDeleteButton/index.js +6 -8
  22. package/dist/es2019/version.json +1 -1
  23. package/dist/esm/plugins/table/commands/selection.js +3 -3
  24. package/dist/esm/plugins/table/event-handlers.js +3 -3
  25. package/dist/esm/plugins/table/nodeviews/OverflowShadowsObserver.js +5 -6
  26. package/dist/esm/plugins/table/pm-plugins/sticky-headers/nodeviews/tableRow.js +9 -16
  27. package/dist/esm/plugins/table/pm-plugins/table-local-id.js +66 -38
  28. package/dist/esm/plugins/table/pm-plugins/table-resizing/utils/resize-logic.js +4 -4
  29. package/dist/esm/plugins/table/transforms/delete-rows.js +2 -2
  30. package/dist/esm/plugins/table/types.js +6 -6
  31. package/dist/esm/plugins/table/ui/FloatingContextualMenu/ContextualMenu.js +1 -1
  32. package/dist/esm/plugins/table/ui/FloatingDeleteButton/index.js +6 -7
  33. package/dist/esm/plugins/table/ui/TableFloatingControls/index.js +1 -1
  34. package/dist/esm/version.json +1 -1
  35. package/package.json +4 -4
  36. package/src/__tests__/unit/pm-plugins/table-local-id.ts +2 -14
  37. package/src/plugins/table/pm-plugins/table-local-id.ts +57 -41
  38. package/src/plugins/table/ui/FloatingContextualMenu/ContextualMenu.tsx +3 -1
@@ -12,13 +12,13 @@
12
12
  */
13
13
  import { Dispatch } from '@atlaskit/editor-common/event-dispatcher';
14
14
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
15
- import { EditorState, PluginKey, Transaction } from 'prosemirror-state';
16
- import { NodeType } from 'prosemirror-model';
15
+ import { EditorState, PluginKey } from 'prosemirror-state';
16
+ import { Node as ProsemirrorNode } from 'prosemirror-model';
17
17
  import rafSchedule from 'raf-schd';
18
18
 
19
19
  import { uuid } from '@atlaskit/adf-schema';
20
20
 
21
- import { stepAddsOneOf } from '@atlaskit/editor-common/utils';
21
+ import { stepHasSlice } from '@atlaskit/editor-common/utils';
22
22
 
23
23
  interface TableLocalIdPluginState {
24
24
  // One time parse for initial load with existing tables without localIds
@@ -31,15 +31,6 @@ const getPluginState = (
31
31
  ): TableLocalIdPluginState | undefined | null =>
32
32
  state && pluginKey.getState(state);
33
33
 
34
- /**
35
- * Traverses a transaction's steps to see if we're inserting any tables
36
- */
37
- const checkIsAddingTable = (transaction: Transaction, nodeType: NodeType) => {
38
- return transaction.steps.some((step) =>
39
- stepAddsOneOf(step, new Set([nodeType])),
40
- );
41
- };
42
-
43
34
  /**
44
35
  * Ensures uniqueness of `localId`s on tables being created or edited
45
36
  */
@@ -143,7 +134,9 @@ const createPlugin = (dispatch: Dispatch) =>
143
134
  const tr = newState.tr;
144
135
  const { table } = newState.schema.nodes;
145
136
 
146
- const idsObserved = new Set<string>();
137
+ const addedTableNodes: Set<ProsemirrorNode> = new Set();
138
+ const addedTableNodePos: Map<ProsemirrorNode, number> = new Map();
139
+ const localIds: Set<string> = new Set();
147
140
 
148
141
  transactions.forEach((transaction) => {
149
142
  if (!transaction.docChanged) {
@@ -157,43 +150,66 @@ const createPlugin = (dispatch: Dispatch) =>
157
150
  return;
158
151
  }
159
152
 
160
- /** Check if we're actually inserting a table, otherwise we can ignore this tr */
161
- const isAddingTable = checkIsAddingTable(transaction, table);
162
- if (!isAddingTable) {
163
- return;
164
- }
153
+ /** Get the tables we are adding and their position */
154
+ for (const step of transaction.steps) {
155
+ if (!stepHasSlice(step)) {
156
+ continue;
157
+ }
165
158
 
166
- // Can't simply look at changed nodes, as we could be adding a table
167
- newState.doc.descendants((node, pos) => {
168
- const isTable = node.type === table;
169
- const localId = node.attrs.localId;
170
-
171
- // Dealing with a table - make sure it's a unique ID
172
- if (isTable) {
173
- if (localId && !idsObserved.has(localId)) {
174
- idsObserved.add(localId);
175
- // Also add a localId if it happens to not have one,
176
- } else if (!localId || idsObserved.has(localId)) {
177
- modified = true;
178
- tr.setNodeMarkup(pos, undefined, {
179
- ...node.attrs,
180
- localId: uuid.generate(),
181
- });
159
+ step.slice.content.descendants((node, pos) => {
160
+ if (node.type === table) {
161
+ addedTableNodes.add(node);
182
162
  }
183
- return false;
184
- }
185
163
 
186
- /**
187
- * Otherwise continue traversing, we can encounter tables nested in
188
- * expands/bodiedExtensions
189
- */
164
+ return true;
165
+ });
166
+ }
167
+ });
168
+
169
+ if (!addedTableNodes.size) {
170
+ return;
171
+ }
172
+
173
+ // Get the existing localIds on the page
174
+ newState.doc.descendants((node, pos) => {
175
+ // Skip if this is position of added table
176
+ if (addedTableNodes.has(node)) {
177
+ addedTableNodePos.set(node, pos);
178
+ return false;
179
+ }
180
+
181
+ if (node.type !== table) {
190
182
  return true;
191
- });
183
+ }
184
+
185
+ localIds.add(node.attrs.localId);
186
+
187
+ // can't have table within a table
188
+ return false;
192
189
  });
193
190
 
191
+ // If the added nodes have duplicate id, generate a new one
192
+ for (const node of addedTableNodes) {
193
+ if (!node.attrs.localId || localIds.has(node.attrs.localId)) {
194
+ const pos = addedTableNodePos.get(node);
195
+
196
+ if (pos === undefined) {
197
+ continue;
198
+ }
199
+
200
+ tr.setNodeMarkup(pos, undefined, {
201
+ ...node.attrs,
202
+ localId: uuid.generate(),
203
+ });
204
+
205
+ modified = true;
206
+ }
207
+ }
208
+
194
209
  if (modified) {
195
210
  return tr;
196
211
  }
212
+
197
213
  return;
198
214
  },
199
215
  });
@@ -230,7 +230,9 @@ export class ContextualMenu extends Component<
230
230
  isOpen && targetCellPosition
231
231
  ? state.doc.nodeAt(targetCellPosition)
232
232
  : null;
233
- const background = node?.attrs?.background || '#ffffff';
233
+ const background = hexToEditorBackgroundPaletteColor(
234
+ node?.attrs?.background || '#ffffff',
235
+ );
234
236
  items.push({
235
237
  content: formatMessage(messages.cellBackground),
236
238
  value: { name: 'background' },