@atlaskit/editor-plugin-table 12.2.6 → 12.3.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 (207) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/cjs/pm-plugins/view-mode-sort/index.js +1 -3
  3. package/dist/es2019/pm-plugins/view-mode-sort/index.js +1 -3
  4. package/dist/esm/pm-plugins/view-mode-sort/index.js +1 -3
  5. package/package.json +4 -4
  6. package/afm-cc/tsconfig.json +0 -123
  7. package/afm-dev-agents/tsconfig.json +0 -123
  8. package/afm-jira/tsconfig.json +0 -123
  9. package/afm-passionfruit/tsconfig.json +0 -123
  10. package/afm-post-office/tsconfig.json +0 -123
  11. package/afm-rovo-extension/tsconfig.json +0 -123
  12. package/afm-townsquare/tsconfig.json +0 -123
  13. package/afm-volt/tsconfig.json +0 -114
  14. package/build/tsconfig.json +0 -23
  15. package/docs/0-intro.tsx +0 -57
  16. package/src/index.ts +0 -21
  17. package/src/nodeviews/ExternalDropTargets.tsx +0 -91
  18. package/src/nodeviews/OverflowShadowsObserver.ts +0 -156
  19. package/src/nodeviews/TableCell.ts +0 -134
  20. package/src/nodeviews/TableComponent.tsx +0 -1590
  21. package/src/nodeviews/TableComponentWithSharedState.tsx +0 -278
  22. package/src/nodeviews/TableContainer.tsx +0 -926
  23. package/src/nodeviews/TableNodeViewBase.ts +0 -29
  24. package/src/nodeviews/TableResizer.tsx +0 -884
  25. package/src/nodeviews/TableRow.ts +0 -830
  26. package/src/nodeviews/TableStickyScrollbar.ts +0 -211
  27. package/src/nodeviews/__mocks__/OverflowShadowsObserver.ts +0 -15
  28. package/src/nodeviews/__mocks__/OverridableMock.ts +0 -26
  29. package/src/nodeviews/table-container-styles.ts +0 -9
  30. package/src/nodeviews/table-node-views.ts +0 -76
  31. package/src/nodeviews/table.tsx +0 -530
  32. package/src/nodeviews/toDOM.ts +0 -244
  33. package/src/nodeviews/types.ts +0 -36
  34. package/src/nodeviews/update-overflow-shadows.ts +0 -11
  35. package/src/pm-plugins/analytics/actions.ts +0 -21
  36. package/src/pm-plugins/analytics/commands.ts +0 -47
  37. package/src/pm-plugins/analytics/plugin-factory.ts +0 -9
  38. package/src/pm-plugins/analytics/plugin-key.ts +0 -5
  39. package/src/pm-plugins/analytics/plugin.ts +0 -80
  40. package/src/pm-plugins/analytics/reducer.ts +0 -27
  41. package/src/pm-plugins/analytics/types.ts +0 -20
  42. package/src/pm-plugins/analytics/utils/moved-event.ts +0 -51
  43. package/src/pm-plugins/commands/clear.ts +0 -43
  44. package/src/pm-plugins/commands/collapse.ts +0 -17
  45. package/src/pm-plugins/commands/column-resize.ts +0 -478
  46. package/src/pm-plugins/commands/commands-with-analytics.ts +0 -715
  47. package/src/pm-plugins/commands/delete.ts +0 -42
  48. package/src/pm-plugins/commands/display-mode.ts +0 -18
  49. package/src/pm-plugins/commands/go-to-next-cell.ts +0 -198
  50. package/src/pm-plugins/commands/hover.ts +0 -242
  51. package/src/pm-plugins/commands/index.ts +0 -51
  52. package/src/pm-plugins/commands/insert.ts +0 -438
  53. package/src/pm-plugins/commands/misc.ts +0 -811
  54. package/src/pm-plugins/commands/referentiality.ts +0 -15
  55. package/src/pm-plugins/commands/selection.ts +0 -537
  56. package/src/pm-plugins/commands/sort.ts +0 -102
  57. package/src/pm-plugins/commands/split-cell.ts +0 -28
  58. package/src/pm-plugins/commands/toggle.ts +0 -109
  59. package/src/pm-plugins/create-plugin-config.ts +0 -17
  60. package/src/pm-plugins/decorations/plugin.ts +0 -107
  61. package/src/pm-plugins/decorations/utils/column-controls.ts +0 -91
  62. package/src/pm-plugins/decorations/utils/column-resizing.ts +0 -71
  63. package/src/pm-plugins/decorations/utils/compose-decorations.ts +0 -9
  64. package/src/pm-plugins/decorations/utils/types.ts +0 -16
  65. package/src/pm-plugins/default-table-selection.ts +0 -14
  66. package/src/pm-plugins/drag-and-drop/actions.ts +0 -48
  67. package/src/pm-plugins/drag-and-drop/commands-with-analytics.ts +0 -222
  68. package/src/pm-plugins/drag-and-drop/commands.ts +0 -194
  69. package/src/pm-plugins/drag-and-drop/consts.ts +0 -7
  70. package/src/pm-plugins/drag-and-drop/plugin-factory.ts +0 -33
  71. package/src/pm-plugins/drag-and-drop/plugin-key.ts +0 -5
  72. package/src/pm-plugins/drag-and-drop/plugin.ts +0 -398
  73. package/src/pm-plugins/drag-and-drop/reducer.ts +0 -38
  74. package/src/pm-plugins/drag-and-drop/types.ts +0 -18
  75. package/src/pm-plugins/drag-and-drop/utils/autoscrollers.ts +0 -49
  76. package/src/pm-plugins/drag-and-drop/utils/getDragBehaviour.ts +0 -9
  77. package/src/pm-plugins/drag-and-drop/utils/monitor.ts +0 -73
  78. package/src/pm-plugins/handlers.ts +0 -161
  79. package/src/pm-plugins/keymap.ts +0 -436
  80. package/src/pm-plugins/main.ts +0 -433
  81. package/src/pm-plugins/plugin-factory.ts +0 -42
  82. package/src/pm-plugins/plugin-key.ts +0 -8
  83. package/src/pm-plugins/reducer.ts +0 -145
  84. package/src/pm-plugins/safari-delete-composition-text-issue-workaround.ts +0 -102
  85. package/src/pm-plugins/sticky-headers/commands.ts +0 -8
  86. package/src/pm-plugins/sticky-headers/plugin-key.ts +0 -5
  87. package/src/pm-plugins/sticky-headers/plugin-state.ts +0 -52
  88. package/src/pm-plugins/sticky-headers/plugin.ts +0 -12
  89. package/src/pm-plugins/sticky-headers/types.ts +0 -20
  90. package/src/pm-plugins/sticky-headers/util.ts +0 -18
  91. package/src/pm-plugins/table-analytics.ts +0 -100
  92. package/src/pm-plugins/table-local-id.ts +0 -213
  93. package/src/pm-plugins/table-resizing/commands.ts +0 -116
  94. package/src/pm-plugins/table-resizing/event-handlers.ts +0 -352
  95. package/src/pm-plugins/table-resizing/plugin-factory.ts +0 -29
  96. package/src/pm-plugins/table-resizing/plugin-key.ts +0 -5
  97. package/src/pm-plugins/table-resizing/plugin.ts +0 -94
  98. package/src/pm-plugins/table-resizing/reducer.ts +0 -37
  99. package/src/pm-plugins/table-resizing/utils/colgroup.ts +0 -306
  100. package/src/pm-plugins/table-resizing/utils/column-state.ts +0 -120
  101. package/src/pm-plugins/table-resizing/utils/consts.ts +0 -11
  102. package/src/pm-plugins/table-resizing/utils/content-width.ts +0 -118
  103. package/src/pm-plugins/table-resizing/utils/dom.ts +0 -132
  104. package/src/pm-plugins/table-resizing/utils/misc.ts +0 -282
  105. package/src/pm-plugins/table-resizing/utils/resize-column.ts +0 -34
  106. package/src/pm-plugins/table-resizing/utils/resize-logic.ts +0 -289
  107. package/src/pm-plugins/table-resizing/utils/resize-state.ts +0 -417
  108. package/src/pm-plugins/table-resizing/utils/scale-table.ts +0 -290
  109. package/src/pm-plugins/table-resizing/utils/types.ts +0 -25
  110. package/src/pm-plugins/table-resizing/utils/unit-to-number.ts +0 -1
  111. package/src/pm-plugins/table-selection-keymap.ts +0 -64
  112. package/src/pm-plugins/table-size-selector.ts +0 -39
  113. package/src/pm-plugins/table-width-in-comment-fix.ts +0 -113
  114. package/src/pm-plugins/table-width.ts +0 -153
  115. package/src/pm-plugins/transforms/column-width.ts +0 -249
  116. package/src/pm-plugins/transforms/delete-columns.ts +0 -281
  117. package/src/pm-plugins/transforms/delete-rows.ts +0 -154
  118. package/src/pm-plugins/transforms/fix-tables.ts +0 -249
  119. package/src/pm-plugins/transforms/merge.ts +0 -301
  120. package/src/pm-plugins/transforms/replace-table.ts +0 -38
  121. package/src/pm-plugins/transforms/split.ts +0 -90
  122. package/src/pm-plugins/utils/alignment.ts +0 -33
  123. package/src/pm-plugins/utils/analytics.ts +0 -238
  124. package/src/pm-plugins/utils/collapse.ts +0 -93
  125. package/src/pm-plugins/utils/column-controls.ts +0 -250
  126. package/src/pm-plugins/utils/create.ts +0 -64
  127. package/src/pm-plugins/utils/decoration.ts +0 -672
  128. package/src/pm-plugins/utils/dom.ts +0 -251
  129. package/src/pm-plugins/utils/drag-menu.tsx +0 -491
  130. package/src/pm-plugins/utils/get-allow-add-column-custom-step.ts +0 -10
  131. package/src/pm-plugins/utils/guidelines.ts +0 -30
  132. package/src/pm-plugins/utils/merged-cells.ts +0 -239
  133. package/src/pm-plugins/utils/nodes.ts +0 -162
  134. package/src/pm-plugins/utils/paste.ts +0 -386
  135. package/src/pm-plugins/utils/row-controls.ts +0 -211
  136. package/src/pm-plugins/utils/selection.ts +0 -17
  137. package/src/pm-plugins/utils/snapping.ts +0 -136
  138. package/src/pm-plugins/utils/table.ts +0 -60
  139. package/src/pm-plugins/utils/update-plugin-state-decorations.ts +0 -13
  140. package/src/pm-plugins/view-mode-sort/consts.ts +0 -3
  141. package/src/pm-plugins/view-mode-sort/index.ts +0 -291
  142. package/src/pm-plugins/view-mode-sort/plugin-key.ts +0 -7
  143. package/src/pm-plugins/view-mode-sort/types.ts +0 -23
  144. package/src/pm-plugins/view-mode-sort/utils.ts +0 -136
  145. package/src/tablePlugin.tsx +0 -971
  146. package/src/tablePluginType.ts +0 -102
  147. package/src/types/index.ts +0 -592
  148. package/src/ui/ColumnResizeWidget/index.tsx +0 -61
  149. package/src/ui/ContentComponent.tsx +0 -311
  150. package/src/ui/DragHandle/HandleIconComponent.tsx +0 -21
  151. package/src/ui/DragHandle/index.tsx +0 -391
  152. package/src/ui/DragPreview/index.tsx +0 -51
  153. package/src/ui/FloatingAlignmentButtons/FloatingAlignmentButtons.tsx +0 -59
  154. package/src/ui/FloatingContextualButton/FixedButton.tsx +0 -203
  155. package/src/ui/FloatingContextualButton/index.tsx +0 -168
  156. package/src/ui/FloatingContextualButton/styles.ts +0 -69
  157. package/src/ui/FloatingContextualMenu/ContextualMenu.tsx +0 -931
  158. package/src/ui/FloatingContextualMenu/index.tsx +0 -141
  159. package/src/ui/FloatingContextualMenu/styles.ts +0 -77
  160. package/src/ui/FloatingDeleteButton/DeleteButton.tsx +0 -54
  161. package/src/ui/FloatingDeleteButton/getPopUpOptions.ts +0 -65
  162. package/src/ui/FloatingDeleteButton/index.tsx +0 -383
  163. package/src/ui/FloatingDeleteButton/types.ts +0 -3
  164. package/src/ui/FloatingDragMenu/DragMenu.tsx +0 -668
  165. package/src/ui/FloatingDragMenu/DropdownMenu.tsx +0 -221
  166. package/src/ui/FloatingDragMenu/index.tsx +0 -136
  167. package/src/ui/FloatingDragMenu/styles.ts +0 -83
  168. package/src/ui/FloatingInsertButton/InsertButton.tsx +0 -263
  169. package/src/ui/FloatingInsertButton/getPopupOptions.ts +0 -131
  170. package/src/ui/FloatingInsertButton/index.tsx +0 -314
  171. package/src/ui/FloatingToolbarLabel/FloatingToolbarLabel.tsx +0 -31
  172. package/src/ui/SizeSelector/index.tsx +0 -74
  173. package/src/ui/TableFloatingColumnControls/ColumnControls/index.tsx +0 -397
  174. package/src/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.tsx +0 -105
  175. package/src/ui/TableFloatingColumnControls/ColumnDropTargets/index.tsx +0 -63
  176. package/src/ui/TableFloatingColumnControls/index.tsx +0 -151
  177. package/src/ui/TableFloatingControls/CornerControls/ClassicCornerControls.tsx +0 -106
  178. package/src/ui/TableFloatingControls/CornerControls/DragCornerControls.tsx +0 -143
  179. package/src/ui/TableFloatingControls/CornerControls/types.ts +0 -12
  180. package/src/ui/TableFloatingControls/FloatingControlsWithSelection.tsx +0 -88
  181. package/src/ui/TableFloatingControls/NumberColumn/index.tsx +0 -175
  182. package/src/ui/TableFloatingControls/RowControls/ClassicControls.tsx +0 -131
  183. package/src/ui/TableFloatingControls/RowControls/DragControls.tsx +0 -429
  184. package/src/ui/TableFloatingControls/RowDropTarget/index.tsx +0 -96
  185. package/src/ui/TableFloatingControls/index.tsx +0 -275
  186. package/src/ui/TableFullWidthLabel/index.tsx +0 -38
  187. package/src/ui/common-styles.ts +0 -1218
  188. package/src/ui/consts.ts +0 -109
  189. package/src/ui/event-handlers.ts +0 -662
  190. package/src/ui/global-styles.tsx +0 -55
  191. package/src/ui/hooks/useInternalTablePluginStateSelector.ts +0 -38
  192. package/src/ui/icons/AddColLeftIcon.tsx +0 -37
  193. package/src/ui/icons/AddColRightIcon.tsx +0 -37
  194. package/src/ui/icons/AddRowAboveIcon.tsx +0 -22
  195. package/src/ui/icons/AddRowBelowIcon.tsx +0 -39
  196. package/src/ui/icons/DragHandleDisabledIcon.tsx +0 -25
  197. package/src/ui/icons/DragHandleIcon.tsx +0 -16
  198. package/src/ui/icons/DragInMotionIcon.tsx +0 -54
  199. package/src/ui/icons/MergeCellsIcon.tsx +0 -26
  200. package/src/ui/icons/MinimisedHandle.tsx +0 -15
  201. package/src/ui/icons/SortingIconWrapper.tsx +0 -43
  202. package/src/ui/icons/SplitCellIcon.tsx +0 -34
  203. package/src/ui/toolbar.tsx +0 -1153
  204. package/src/ui/ui-styles.ts +0 -960
  205. package/tsconfig.app.json +0 -135
  206. package/tsconfig.dev.json +0 -54
  207. package/tsconfig.json +0 -18
@@ -1,811 +0,0 @@
1
- import isEqual from 'lodash/isEqual';
2
-
3
- import type { CellAttributes, TableAttributes, TableLayout } from '@atlaskit/adf-schema';
4
- import { getTableContainerWidth } from '@atlaskit/editor-common/node-width';
5
- import { type PortalProviderAPI } from '@atlaskit/editor-common/portal';
6
- import type { Command, EditorCommand } from '@atlaskit/editor-common/types';
7
- import {
8
- closestElement,
9
- isParagraph,
10
- isTextSelection,
11
- mapSlice,
12
- } from '@atlaskit/editor-common/utils';
13
- import type { Node as PMNode, Schema, Slice } from '@atlaskit/editor-prosemirror/model';
14
- import { TextSelection } from '@atlaskit/editor-prosemirror/state';
15
- import type { EditorState, Selection, Transaction } from '@atlaskit/editor-prosemirror/state';
16
- import type { ContentNodeWithPos, NodeWithPos } from '@atlaskit/editor-prosemirror/utils';
17
- import type { EditorView } from '@atlaskit/editor-prosemirror/view';
18
- import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
19
- import { TableMap } from '@atlaskit/editor-tables/table-map';
20
- import {
21
- findCellClosestToPos,
22
- findTable,
23
- getCellsInColumn,
24
- getCellsInRow,
25
- getSelectionRect,
26
- isSelectionType,
27
- isTableSelected,
28
- removeTable,
29
- selectColumns as selectColumnsTransform,
30
- selectColumn as selectColumnTransform,
31
- selectionCell,
32
- selectRows as selectRowsTransform,
33
- selectRow as selectRowTransform,
34
- setCellAttrs,
35
- } from '@atlaskit/editor-tables/utils';
36
-
37
- import type { WidthToWidest } from '../../types';
38
- import { TableCssClassName as ClassName, TableDecorations } from '../../types';
39
- import { getDecorations } from '../decorations/plugin';
40
- import {
41
- buildColumnResizingDecorations,
42
- clearColumnResizingDecorations,
43
- } from '../decorations/utils/column-resizing';
44
- import { createCommand, getPluginState } from '../plugin-factory';
45
- import { fixAutoSizedTable } from '../transforms/fix-tables';
46
- import {
47
- createColumnControlsDecoration,
48
- createColumnSelectedDecoration,
49
- } from '../utils/decoration';
50
- import {
51
- checkIfHeaderColumnEnabled,
52
- checkIfHeaderRowEnabled,
53
- checkIfNumberColumnEnabled,
54
- isIsolating,
55
- } from '../utils/nodes';
56
- import { updatePluginStateDecorations } from '../utils/update-plugin-state-decorations';
57
-
58
- export const setEditorFocus = (editorHasFocus: boolean) =>
59
- createCommand({
60
- type: 'SET_EDITOR_FOCUS',
61
- data: {
62
- editorHasFocus,
63
- },
64
- });
65
-
66
- export const setTableRef = (ref?: HTMLTableElement) =>
67
- createCommand(
68
- (state) => {
69
- const tableRef = ref;
70
- const foundTable = findTable(state.selection);
71
- const tableNode = ref && foundTable ? foundTable.node : undefined;
72
- const tablePos = ref && foundTable ? foundTable.pos : undefined;
73
- const tableWrapperTarget =
74
- closestElement(tableRef, `.${ClassName.TABLE_NODE_WRAPPER}`) || undefined;
75
- const { isDragAndDropEnabled } = getPluginState(state);
76
-
77
- return {
78
- type: 'SET_TABLE_REF',
79
- data: {
80
- tableRef,
81
- tableNode,
82
- tablePos,
83
- tableWrapperTarget,
84
- isNumberColumnEnabled: checkIfNumberColumnEnabled(state.selection),
85
- isHeaderRowEnabled: checkIfHeaderRowEnabled(state.selection),
86
- isHeaderColumnEnabled: checkIfHeaderColumnEnabled(state.selection),
87
- // decoration set is drawn by the decoration plugin, skip this for DnD as all controls are floating
88
- decorationSet: !isDragAndDropEnabled
89
- ? updatePluginStateDecorations(
90
- state,
91
- createColumnControlsDecoration(state.selection),
92
- TableDecorations.COLUMN_CONTROLS_DECORATIONS,
93
- )
94
- : undefined,
95
- resizeHandleRowIndex: undefined,
96
- resizeHandleColumnIndex: undefined,
97
- },
98
- };
99
- },
100
- (tr) => tr.setMeta('addToHistory', false),
101
- );
102
-
103
- export const setCellAttr =
104
- (
105
- name: string,
106
- // Ignored via go/ees005
107
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
108
- value: any,
109
- ): Command =>
110
- (state, dispatch) => {
111
- const { tr, selection } = state;
112
- if (selection instanceof CellSelection) {
113
- let updated = false;
114
- selection.forEachCell((cell, pos) => {
115
- if (cell.attrs[name] !== value) {
116
- tr.setNodeMarkup(pos, cell.type, { ...cell.attrs, [name]: value });
117
- updated = true;
118
- }
119
- });
120
- if (updated) {
121
- if (dispatch) {
122
- dispatch(tr);
123
- }
124
- return true;
125
- }
126
- } else {
127
- const cell = selectionCell(state.selection);
128
- if (cell) {
129
- if (dispatch) {
130
- dispatch(
131
- tr.setNodeMarkup(cell.pos, cell.nodeAfter?.type, {
132
- ...cell.nodeAfter?.attrs,
133
- [name]: value,
134
- }),
135
- );
136
- }
137
- return true;
138
- }
139
- }
140
- return false;
141
- };
142
-
143
- export const triggerUnlessTableHeader =
144
- (command: Command): Command =>
145
- (state, dispatch, view) => {
146
- const {
147
- selection,
148
- schema: {
149
- nodes: { tableHeader },
150
- },
151
- } = state;
152
-
153
- if (selection instanceof TextSelection) {
154
- const cell = findCellClosestToPos(selection.$from);
155
- if (cell && cell.node.type !== tableHeader) {
156
- return command(state, dispatch, view);
157
- }
158
- }
159
-
160
- if (selection instanceof CellSelection) {
161
- const rect = getSelectionRect(selection);
162
- if (!checkIfHeaderRowEnabled(selection) || (rect && rect.top > 0)) {
163
- return command(state, dispatch, view);
164
- }
165
- }
166
-
167
- return false;
168
- };
169
-
170
- export const transformSliceRemoveCellBackgroundColor = (slice: Slice, schema: Schema): Slice => {
171
- const { tableCell, tableHeader } = schema.nodes;
172
- return mapSlice(slice, (maybeCell) => {
173
- if (maybeCell.type === tableCell || maybeCell.type === tableHeader) {
174
- const cellAttrs: CellAttributes = { ...maybeCell.attrs };
175
- cellAttrs.background = undefined;
176
- return maybeCell.type.createChecked(cellAttrs, maybeCell.content, maybeCell.marks);
177
- }
178
- return maybeCell;
179
- });
180
- };
181
-
182
- export const transformSliceToAddTableHeaders = (slice: Slice, schema: Schema): Slice => {
183
- const { table, tableHeader, tableRow } = schema.nodes;
184
-
185
- return mapSlice(slice, (maybeTable) => {
186
- if (maybeTable.type === table) {
187
- const firstRow = maybeTable.firstChild;
188
- if (firstRow) {
189
- const headerCols = [] as PMNode[];
190
- firstRow.forEach((oldCol) => {
191
- headerCols.push(tableHeader.createChecked(oldCol.attrs, oldCol.content, oldCol.marks));
192
- });
193
- const headerRow = tableRow.createChecked(firstRow.attrs, headerCols, firstRow.marks);
194
- return maybeTable.copy(maybeTable.content.replaceChild(0, headerRow));
195
- }
196
- }
197
- return maybeTable;
198
- });
199
- };
200
-
201
- export const transformSliceToRemoveColumnsWidths = (slice: Slice, schema: Schema): Slice => {
202
- const { tableHeader, tableCell } = schema.nodes;
203
-
204
- return mapSlice(slice, (maybeCell) => {
205
- if (maybeCell.type === tableCell || maybeCell.type === tableHeader) {
206
- if (!maybeCell.attrs.colwidth) {
207
- return maybeCell;
208
- }
209
- return maybeCell.type.createChecked(
210
- { ...maybeCell.attrs, colwidth: undefined },
211
- maybeCell.content,
212
- maybeCell.marks,
213
- );
214
- }
215
- return maybeCell;
216
- });
217
- };
218
-
219
- export const countCellsInSlice = (slice: Slice, schema: Schema, type?: 'row' | 'column') => {
220
- const { tableHeader, tableCell } = schema.nodes;
221
- let count = 0;
222
-
223
- if (!type) {
224
- return count;
225
- }
226
-
227
- slice.content.descendants((maybeCell) => {
228
- if (maybeCell.type === tableCell || maybeCell.type === tableHeader) {
229
- count += type === 'row' ? maybeCell.attrs.colspan : maybeCell.attrs.rowspan;
230
-
231
- return false;
232
- }
233
- });
234
-
235
- return count;
236
- };
237
-
238
- export const getTableSelectionType = (selection: Selection) => {
239
- if (selection instanceof CellSelection) {
240
- return selection.isRowSelection() ? 'row' : selection.isColSelection() ? 'column' : undefined;
241
- }
242
- };
243
-
244
- export const getTableElementMoveTypeBySlice = (slice: Slice, state: EditorState) => {
245
- const {
246
- schema: {
247
- nodes: { tableRow, table },
248
- },
249
- } = state;
250
- const currentTable = findTable(state.tr.selection);
251
-
252
- // check if copied slice is a table or table row
253
- if (
254
- !slice.content.firstChild ||
255
- (slice.content.firstChild.type !== table && slice.content.firstChild.type !== tableRow) ||
256
- !currentTable
257
- ) {
258
- return undefined;
259
- }
260
-
261
- // if the slice only contains one table row, assume it's a row
262
- if (slice.content.childCount === 1 && slice.content.firstChild.type === tableRow) {
263
- return 'row';
264
- }
265
-
266
- // `TableMap.get` can throw if the content is invalid - in which case we should just
267
- // return undefined
268
- try {
269
- const map = TableMap.get(currentTable.node);
270
- const slicedMap = TableMap.get(slice.content.firstChild);
271
-
272
- return map.width === slicedMap.width
273
- ? 'row'
274
- : map.height === slicedMap.height
275
- ? 'column'
276
- : undefined;
277
- } catch (e) {
278
- return undefined;
279
- }
280
- };
281
-
282
- export const isInsideFirstCellOfRowOrColumn = (selection: Selection, type?: 'row' | 'column') => {
283
- const table = findTable(selection);
284
-
285
- if (!table || !type) {
286
- return false;
287
- }
288
-
289
- // Ignored via go/ees005
290
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
291
- const map = TableMap.get(table.node!);
292
- const cell = findCellClosestToPos(selection.$anchor);
293
- if (!cell) {
294
- return false;
295
- }
296
- const pos = cell.pos - table.pos - 1;
297
- // cell positions in table map always start at 1, as they're offsets not positions
298
- const index = map.map.findIndex((value) => value === pos);
299
-
300
- return type === 'row' ? index % map.width === 0 : index < map.width;
301
- };
302
-
303
- export const deleteTable: Command = (state, dispatch) => {
304
- if (dispatch) {
305
- dispatch(removeTable(state.tr));
306
- }
307
- return true;
308
- };
309
-
310
- export const deleteTableIfSelected: Command = (state, dispatch) => {
311
- if (isTableSelected(state.selection)) {
312
- return deleteTable(state, dispatch);
313
- }
314
- return false;
315
- };
316
-
317
- export const convertFirstRowToHeader =
318
- (schema: Schema) =>
319
- (tr: Transaction): Transaction => {
320
- // Ignored via go/ees005
321
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
322
- const table = findTable(tr.selection)!;
323
- const map = TableMap.get(table.node);
324
- for (let i = 0; i < map.width; i++) {
325
- const cell = table.node.child(0).child(i);
326
- tr.setNodeMarkup(table.start + map.map[i], schema.nodes.tableHeader, cell.attrs);
327
- }
328
- return tr;
329
- };
330
-
331
- export const moveCursorBackward: Command = (state, dispatch) => {
332
- const { $cursor } = state.selection as TextSelection;
333
- // if cursor is in the middle of a text node, do nothing
334
- if (!$cursor || $cursor.parentOffset > 0) {
335
- return false;
336
- }
337
-
338
- // find the node before the cursor
339
- let before;
340
- let cut: number | undefined;
341
- if (!isIsolating($cursor.parent)) {
342
- for (let i = $cursor.depth - 1; !before && i >= 0; i--) {
343
- if ($cursor.index(i) > 0) {
344
- cut = $cursor.before(i + 1);
345
- before = $cursor.node(i).child($cursor.index(i) - 1);
346
- }
347
- if (isIsolating($cursor.node(i))) {
348
- break;
349
- }
350
- }
351
- }
352
-
353
- // if the node before is not a table node - do nothing
354
- if (!before || before.type !== state.schema.nodes.table) {
355
- return false;
356
- }
357
-
358
- /*
359
- ensure we're just at a top level paragraph
360
- otherwise, perform regular backspace behaviour
361
- */
362
- const grandparent = $cursor.node($cursor.depth - 1);
363
- if (
364
- $cursor.parent.type !== state.schema.nodes.paragraph ||
365
- (grandparent && grandparent.type !== state.schema.nodes.doc)
366
- ) {
367
- return false;
368
- }
369
-
370
- const { tr } = state;
371
- const lastCellPos = (cut || 0) - 4;
372
- // need to move cursor inside the table to be able to calculate table's offset
373
- tr.setSelection(new TextSelection(state.doc.resolve(lastCellPos)));
374
- const { $from } = tr.selection;
375
- const start = $from.start(-1);
376
- const pos = start + $from.parent.nodeSize - 1;
377
-
378
- // move cursor to the last cell
379
- // it doesn't join node before (last cell) with node after (content after the cursor)
380
- // due to ridiculous amount of PM code that would have been required to overwrite
381
- tr.setSelection(new TextSelection(state.doc.resolve(pos)));
382
-
383
- // if we are inside an empty paragraph not at the end of the doc we delete it
384
- const cursorNode = $cursor.node();
385
- const docEnd = state.doc.content.size;
386
- const paragraphWrapStart = $cursor.pos - 1;
387
- const paragraphWrapEnd = $cursor.pos + 1;
388
- if (cursorNode.content.size === 0 && $cursor.pos + 1 !== docEnd) {
389
- tr.delete(paragraphWrapStart, paragraphWrapEnd);
390
- }
391
-
392
- if (dispatch) {
393
- dispatch(tr);
394
- }
395
-
396
- return true;
397
- };
398
-
399
- export const setMultipleCellAttrs =
400
- (attrs: Object, editorView?: EditorView | null): Command =>
401
- (state, dispatch) => {
402
- let cursorPos: number | undefined;
403
- let { tr } = state;
404
- const { targetCellPosition } = getPluginState(state);
405
-
406
- if (isSelectionType(tr.selection, 'cell')) {
407
- // Ignored via go/ees005
408
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
409
- const selection = tr.selection as any as CellSelection;
410
- selection.forEachCell((_cell, pos) => {
411
- const $pos = tr.doc.resolve(tr.mapping.map(pos + 1));
412
- // Ignored via go/ees005
413
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
414
- tr = setCellAttrs(findCellClosestToPos($pos)!, attrs)(tr);
415
- });
416
- cursorPos = selection.$headCell.pos;
417
- } else if (targetCellPosition) {
418
- // Ignored via go/ees005
419
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
420
- const cell = findCellClosestToPos(tr.doc.resolve(targetCellPosition + 1))!;
421
- tr = setCellAttrs(cell, attrs)(tr);
422
- cursorPos = cell.pos;
423
- }
424
-
425
- if (tr.docChanged && cursorPos !== undefined) {
426
- if (dispatch) {
427
- if (cursorPos !== undefined) {
428
- editorView?.focus();
429
- tr.setSelection(new TextSelection(tr.doc.resolve(cursorPos)));
430
- }
431
- dispatch(tr);
432
- }
433
- return true;
434
- }
435
- return false;
436
- };
437
-
438
- export const selectColumn = (column: number, expand?: boolean, triggeredByKeyboard = false) =>
439
- createCommand(
440
- (state) => {
441
- const cells = getCellsInColumn(column)(state.tr.selection);
442
- if (!cells || !cells.length || typeof cells[0].pos !== 'number') {
443
- return false;
444
- }
445
-
446
- const decorations = createColumnSelectedDecoration(
447
- selectColumnTransform(column, expand)(state.tr),
448
- );
449
- const decorationSet = updatePluginStateDecorations(
450
- state,
451
- decorations,
452
- TableDecorations.COLUMN_SELECTED,
453
- );
454
- const targetCellPosition = cells[0].pos;
455
-
456
- return {
457
- type: 'SELECT_COLUMN',
458
- data: { targetCellPosition, decorationSet },
459
- };
460
- },
461
- (tr: Transaction) =>
462
- selectColumnTransform(column, expand)(tr)
463
- .setMeta('addToHistory', false)
464
- .setMeta('selectedColumnViaKeyboard', triggeredByKeyboard),
465
- );
466
-
467
- export const selectColumns = (columnIndexes: number[]) =>
468
- createCommand(
469
- (state) => {
470
- if (!columnIndexes) {
471
- return false;
472
- }
473
- const cells = columnIndexes
474
- .map((column) => getCellsInColumn(column)(state.tr.selection))
475
- .flat();
476
- if (!cells || !cells.length || cells.some((cell) => cell && typeof cell.pos !== 'number')) {
477
- return false;
478
- }
479
- const decorations = createColumnSelectedDecoration(
480
- selectColumnsTransform(columnIndexes)(state.tr),
481
- );
482
-
483
- const decorationSet = updatePluginStateDecorations(
484
- state,
485
- decorations,
486
- TableDecorations.COLUMN_SELECTED,
487
- );
488
-
489
- const cellsInFirstColumn = getCellsInColumn(Math.min(...columnIndexes))(state.tr.selection);
490
- if (!cellsInFirstColumn || cellsInFirstColumn.length === 0) {
491
- return false;
492
- }
493
- const targetCellPosition = cellsInFirstColumn[0].pos;
494
-
495
- return {
496
- type: 'SELECT_COLUMN',
497
- data: { targetCellPosition, decorationSet },
498
- };
499
- },
500
- (tr: Transaction) => {
501
- return selectColumnsTransform(columnIndexes)(tr).setMeta('addToHistory', false);
502
- },
503
- );
504
-
505
- export const selectRow = (row: number, expand?: boolean, triggeredByKeyboard = false) =>
506
- createCommand(
507
- (state) => {
508
- let targetCellPosition;
509
- const cells = getCellsInRow(row)(state.tr.selection);
510
- if (cells && cells.length) {
511
- targetCellPosition = cells[0].pos;
512
- }
513
-
514
- return { type: 'SET_TARGET_CELL_POSITION', data: { targetCellPosition } };
515
- },
516
- (tr) =>
517
- selectRowTransform(row, expand)(tr)
518
- .setMeta('addToHistory', false)
519
- .setMeta('selectedRowViaKeyboard', triggeredByKeyboard),
520
- );
521
-
522
- export const selectRows = (rowIndexes: number[]) =>
523
- createCommand(
524
- (state) => {
525
- if (rowIndexes.length === 0) {
526
- return false;
527
- }
528
- const cells = rowIndexes.map((row) => getCellsInRow(row)(state.tr.selection)).flat();
529
- if (!cells || !cells.length || cells.some((cell) => cell && typeof cell.pos !== 'number')) {
530
- return false;
531
- }
532
- const cellsInFirstRow = getCellsInRow(Math.min(...rowIndexes))(state.tr.selection);
533
- if (!cellsInFirstRow || cellsInFirstRow.length === 0) {
534
- return false;
535
- }
536
- const targetCellPosition = cellsInFirstRow[0].pos;
537
-
538
- return { type: 'SET_TARGET_CELL_POSITION', data: { targetCellPosition } };
539
- },
540
- (tr) => selectRowsTransform(rowIndexes)(tr).setMeta('addToHistory', false),
541
- );
542
-
543
- export const showInsertColumnButton = (columnIndex: number) =>
544
- createCommand(
545
- (_) =>
546
- columnIndex > -1
547
- ? {
548
- type: 'SHOW_INSERT_COLUMN_BUTTON',
549
- data: { insertColumnButtonIndex: columnIndex },
550
- }
551
- : false,
552
- (tr) => tr.setMeta('addToHistory', false),
553
- );
554
-
555
- export const showInsertRowButton = (rowIndex: number) =>
556
- createCommand(
557
- (_) =>
558
- rowIndex > -1
559
- ? {
560
- type: 'SHOW_INSERT_ROW_BUTTON',
561
- data: { insertRowButtonIndex: rowIndex },
562
- }
563
- : false,
564
- (tr) => tr.setMeta('addToHistory', false),
565
- );
566
-
567
- export const hideInsertColumnOrRowButton = () =>
568
- createCommand(
569
- {
570
- type: 'HIDE_INSERT_COLUMN_OR_ROW_BUTTON',
571
- },
572
- (tr) => tr.setMeta('addToHistory', false),
573
- );
574
-
575
- export const addResizeHandleDecorations = (
576
- rowIndex: number,
577
- columnIndex: number,
578
- includeTooltip: boolean,
579
- nodeViewPortalProviderAPI: PortalProviderAPI,
580
- isKeyboardResize?: boolean,
581
- ) =>
582
- createCommand(
583
- (state) => {
584
- const tableNode = findTable(state.selection);
585
- const {
586
- pluginConfig: { allowColumnResizing },
587
- getIntl,
588
- } = getPluginState(state);
589
-
590
- if (!tableNode || !allowColumnResizing) {
591
- return false;
592
- }
593
-
594
- return {
595
- type: 'ADD_RESIZE_HANDLE_DECORATIONS',
596
- data: {
597
- decorationSet: buildColumnResizingDecorations(
598
- rowIndex,
599
- columnIndex,
600
- includeTooltip,
601
- getIntl,
602
- nodeViewPortalProviderAPI,
603
- )({
604
- tr: state.tr,
605
- decorationSet: getDecorations(state),
606
- }),
607
- resizeHandleRowIndex: rowIndex,
608
- resizeHandleColumnIndex: columnIndex,
609
- resizeHandleIncludeTooltip: includeTooltip,
610
- isKeyboardResize: isKeyboardResize || false,
611
- },
612
- };
613
- },
614
- (tr: Transaction) => tr.setMeta('addToHistory', false),
615
- );
616
-
617
- export const updateResizeHandleDecorations = (
618
- nodeViewPortalProviderAPI: PortalProviderAPI,
619
- rowIndex?: number,
620
- columnIndex?: number,
621
- includeTooltip?: boolean,
622
- ) =>
623
- createCommand(
624
- (state) => {
625
- const tableNode = findTable(state.selection);
626
- const {
627
- resizeHandleRowIndex,
628
- resizeHandleColumnIndex,
629
- resizeHandleIncludeTooltip,
630
- pluginConfig: { allowColumnResizing },
631
- getIntl,
632
- } = getPluginState(state);
633
-
634
- if (!tableNode || !allowColumnResizing) {
635
- return false;
636
- }
637
-
638
- const resolvedRowIndex = rowIndex ?? resizeHandleRowIndex;
639
- const resolvedColumnIndex = columnIndex ?? resizeHandleColumnIndex;
640
- const resolvedIncludeTooltip = includeTooltip ?? resizeHandleIncludeTooltip;
641
-
642
- if (
643
- resolvedRowIndex === undefined ||
644
- resolvedColumnIndex === undefined ||
645
- resolvedIncludeTooltip === undefined
646
- ) {
647
- return false;
648
- }
649
-
650
- return {
651
- type: 'UPDATE_RESIZE_HANDLE_DECORATIONS',
652
- data: {
653
- decorationSet: buildColumnResizingDecorations(
654
- resolvedRowIndex,
655
- resolvedColumnIndex,
656
- resolvedIncludeTooltip,
657
- getIntl,
658
- nodeViewPortalProviderAPI,
659
- )({
660
- tr: state.tr,
661
- decorationSet: getDecorations(state),
662
- }),
663
- resizeHandleRowIndex: rowIndex,
664
- resizeHandleColumnIndex: columnIndex,
665
- resizeHandleIncludeTooltip: includeTooltip,
666
- },
667
- };
668
- },
669
- (tr: Transaction) => tr.setMeta('addToHistory', false),
670
- );
671
-
672
- export const removeResizeHandleDecorations = () =>
673
- createCommand(
674
- (state) => ({
675
- type: 'REMOVE_RESIZE_HANDLE_DECORATIONS',
676
- data: {
677
- decorationSet: clearColumnResizingDecorations()({
678
- tr: state.tr,
679
- decorationSet: getDecorations(state),
680
- }),
681
- },
682
- }),
683
- (tr) => tr.setMeta('addToHistory', false),
684
- );
685
-
686
- export const autoSizeTable = (
687
- view: EditorView,
688
- node: PMNode,
689
- table: HTMLTableElement,
690
- basePos: number | undefined,
691
- opts: { containerWidth: number },
692
- ) => {
693
- if (typeof basePos !== 'number') {
694
- return false;
695
- }
696
-
697
- view.dispatch(fixAutoSizedTable(view, node, table, basePos, opts));
698
- return true;
699
- };
700
-
701
- export const addBoldInEmptyHeaderCells =
702
- (tableCellHeader: ContentNodeWithPos): Command =>
703
- (state, dispatch): boolean => {
704
- const { tr } = state;
705
- if (
706
- // Avoid infinite loop when the current selection is not a TextSelection
707
- isTextSelection(tr.selection) &&
708
- tr.selection.$cursor &&
709
- // When storedMark is null that means this is the initial state
710
- // if the user press to remove the mark storedMark will be an empty array
711
- // and we shouldn't apply the strong mark
712
- tr.storedMarks == null &&
713
- // Check if the current node is a direct child from paragraph
714
- tr.selection.$from.depth === tableCellHeader.depth + 1 &&
715
- // this logic is applied only for empty paragraph
716
- tableCellHeader.node.nodeSize === 4 &&
717
- isParagraph(tableCellHeader.node.firstChild, state.schema)
718
- ) {
719
- const { strong } = state.schema.marks;
720
- tr.setStoredMarks([strong.create()]).setMeta('addToHistory', false);
721
-
722
- if (dispatch) {
723
- dispatch(tr);
724
- }
725
-
726
- return true;
727
- }
728
-
729
- return false;
730
- };
731
-
732
- export const updateWidthToWidest = (widthToWidest: WidthToWidest) =>
733
- createCommand((state) => {
734
- const { widthToWidest: prevWidthToWidest } = getPluginState(state);
735
-
736
- if (isEqual(widthToWidest, prevWidthToWidest)) {
737
- return false;
738
- }
739
-
740
- return {
741
- type: 'UPDATE_TABLE_WIDTH_TO_WIDEST',
742
- data: {
743
- widthToWidest: { ...prevWidthToWidest, ...widthToWidest },
744
- },
745
- };
746
- });
747
-
748
- export const setTableAlignment =
749
- (newAlignment: TableLayout, isCommentEditor: boolean): EditorCommand =>
750
- ({ tr }) => {
751
- const tableObject = findTable(tr.selection);
752
-
753
- if (!tableObject) {
754
- return null;
755
- }
756
-
757
- const nextTableAttrs = {
758
- ...tableObject.node.attrs,
759
- layout: newAlignment,
760
- } as TableAttributes;
761
-
762
- // table uses old breakout values in layout attribute to determine width
763
- // but that information is lost when alignment changes, so we need to ensure we retain that info
764
- // If table width is not set in the Comment editor, it means that the table width is inherited from the editor and is "full width".
765
- // In that case when switching between alignment options in the Comment editor we should keep the table width unset.
766
- if (!tableObject.node.attrs.width && !isCommentEditor) {
767
- const tableWidth = getTableContainerWidth(tableObject.node);
768
- nextTableAttrs.width = tableWidth;
769
- }
770
-
771
- tr.setNodeMarkup(tableObject.pos, undefined, nextTableAttrs).setMeta('scrollIntoView', false);
772
-
773
- return tr;
774
- };
775
-
776
- export const setTableAlignmentWithTableContentWithPos =
777
- (newAlignment: TableLayout, tableNodeWithPos: NodeWithPos): EditorCommand =>
778
- ({ tr }) => {
779
- const table = tableNodeWithPos.node;
780
- const nextTableAttrs = {
781
- ...table.attrs,
782
- layout: newAlignment,
783
- } as TableAttributes;
784
-
785
- // table uses old breakout values in layout attribute to determine width
786
- // but that information is lost when alignment changes, so we need to ensure we retain that info
787
- if (!table.attrs.width) {
788
- const tableWidth = getTableContainerWidth(table);
789
- nextTableAttrs.width = tableWidth;
790
- }
791
-
792
- tr.setNodeMarkup(tableNodeWithPos.pos, undefined, nextTableAttrs).setMeta(
793
- 'scrollIntoView',
794
- false,
795
- );
796
-
797
- return tr;
798
- };
799
-
800
- export const setFocusToCellMenu = (isCellMenuOpenByKeyboard = true, originalTr?: Transaction) =>
801
- createCommand(
802
- () => {
803
- return {
804
- type: 'SET_CELL_MENU_OPEN',
805
- data: {
806
- isCellMenuOpenByKeyboard: isCellMenuOpenByKeyboard,
807
- },
808
- };
809
- },
810
- (tr) => (originalTr || tr).setMeta('addToHistory', false),
811
- );