@atlaskit/editor-plugin-table 7.16.11 → 7.16.12

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 (242) hide show
  1. package/.eslintrc.js +3 -3
  2. package/CHANGELOG.md +8 -0
  3. package/dist/cjs/commands/misc.js +3 -3
  4. package/dist/cjs/nodeviews/TableCell.js +10 -10
  5. package/dist/cjs/nodeviews/TableContainer.js +41 -6
  6. package/dist/cjs/nodeviews/TableResizer.js +7 -7
  7. package/dist/cjs/nodeviews/TableRow.js +23 -23
  8. package/dist/cjs/pm-plugins/table-resizing/plugin.js +3 -3
  9. package/dist/cjs/pm-plugins/table-resizing/utils/resize-state.js +4 -4
  10. package/dist/cjs/pm-plugins/table-resizing/utils/scale-table.js +3 -3
  11. package/dist/cjs/ui/FloatingContextualMenu/styles.js +1 -1
  12. package/dist/cjs/ui/FloatingDragMenu/styles.js +1 -1
  13. package/dist/cjs/ui/common-styles.js +13 -13
  14. package/dist/cjs/ui/ui-styles.js +25 -25
  15. package/dist/cjs/utils/merged-cells.js +3 -3
  16. package/dist/es2019/commands/misc.js +3 -3
  17. package/dist/es2019/nodeviews/TableContainer.js +43 -5
  18. package/dist/es2019/nodeviews/TableResizer.js +7 -7
  19. package/dist/es2019/nodeviews/TableRow.js +21 -21
  20. package/dist/es2019/pm-plugins/table-resizing/plugin.js +3 -3
  21. package/dist/es2019/pm-plugins/table-resizing/utils/resize-state.js +4 -4
  22. package/dist/es2019/pm-plugins/table-resizing/utils/scale-table.js +3 -3
  23. package/dist/es2019/ui/FloatingContextualMenu/styles.js +47 -47
  24. package/dist/es2019/ui/FloatingDragMenu/styles.js +30 -30
  25. package/dist/es2019/ui/common-styles.js +802 -816
  26. package/dist/es2019/ui/ui-styles.js +665 -678
  27. package/dist/es2019/utils/merged-cells.js +3 -3
  28. package/dist/esm/commands/misc.js +3 -3
  29. package/dist/esm/nodeviews/TableCell.js +10 -10
  30. package/dist/esm/nodeviews/TableContainer.js +42 -7
  31. package/dist/esm/nodeviews/TableResizer.js +7 -7
  32. package/dist/esm/nodeviews/TableRow.js +23 -23
  33. package/dist/esm/pm-plugins/table-resizing/plugin.js +3 -3
  34. package/dist/esm/pm-plugins/table-resizing/utils/resize-state.js +4 -4
  35. package/dist/esm/pm-plugins/table-resizing/utils/scale-table.js +3 -3
  36. package/dist/esm/ui/FloatingContextualMenu/styles.js +1 -1
  37. package/dist/esm/ui/FloatingDragMenu/styles.js +1 -1
  38. package/dist/esm/ui/common-styles.js +13 -13
  39. package/dist/esm/ui/ui-styles.js +25 -25
  40. package/dist/esm/utils/merged-cells.js +3 -3
  41. package/dist/types/pm-plugins/decorations/utils/index.d.ts +1 -1
  42. package/dist/types/pm-plugins/drag-and-drop/utils/autoscrollers.d.ts +1 -1
  43. package/dist/types/pm-plugins/drag-and-drop/utils/getDragBehaviour.d.ts +1 -1
  44. package/dist/types/pm-plugins/table-resizing/utils/index.d.ts +1 -1
  45. package/dist/types/ui/ColumnResizeWidget/index.d.ts +1 -1
  46. package/dist/types/ui/FloatingAlignmentButtons/FloatingAlignmentButtons.d.ts +1 -1
  47. package/dist/types/ui/FloatingToolbarLabel/FloatingToolbarLabel.d.ts +1 -1
  48. package/dist/types/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.d.ts +1 -1
  49. package/dist/types/ui/TableFloatingControls/CornerControls/index.d.ts +1 -1
  50. package/dist/types/ui/icons/DragHandleDisabledIcon.d.ts +1 -1
  51. package/dist/types-ts4.5/pm-plugins/decorations/utils/index.d.ts +1 -1
  52. package/dist/types-ts4.5/pm-plugins/drag-and-drop/utils/autoscrollers.d.ts +1 -1
  53. package/dist/types-ts4.5/pm-plugins/drag-and-drop/utils/getDragBehaviour.d.ts +1 -1
  54. package/dist/types-ts4.5/pm-plugins/table-resizing/utils/index.d.ts +1 -1
  55. package/dist/types-ts4.5/ui/ColumnResizeWidget/index.d.ts +1 -1
  56. package/dist/types-ts4.5/ui/FloatingAlignmentButtons/FloatingAlignmentButtons.d.ts +1 -1
  57. package/dist/types-ts4.5/ui/FloatingToolbarLabel/FloatingToolbarLabel.d.ts +1 -1
  58. package/dist/types-ts4.5/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.d.ts +1 -1
  59. package/dist/types-ts4.5/ui/TableFloatingControls/CornerControls/index.d.ts +1 -1
  60. package/dist/types-ts4.5/ui/icons/DragHandleDisabledIcon.d.ts +1 -1
  61. package/docs/0-intro.tsx +9 -7
  62. package/package.json +2 -2
  63. package/report.api.md +67 -66
  64. package/src/commands/clear.ts +36 -44
  65. package/src/commands/collapse.ts +8 -8
  66. package/src/commands/column-resize.ts +412 -452
  67. package/src/commands/delete.ts +14 -14
  68. package/src/commands/display-mode.ts +10 -11
  69. package/src/commands/go-to-next-cell.ts +48 -54
  70. package/src/commands/hover.ts +210 -227
  71. package/src/commands/index.ts +35 -35
  72. package/src/commands/insert.ts +208 -235
  73. package/src/commands/misc.ts +655 -748
  74. package/src/commands/referentiality.ts +9 -9
  75. package/src/commands/selection.ts +433 -563
  76. package/src/commands/sort.ts +68 -86
  77. package/src/commands/split-cell.ts +14 -14
  78. package/src/commands/toggle.ts +69 -67
  79. package/src/commands-with-analytics.ts +570 -639
  80. package/src/create-plugin-config.ts +13 -13
  81. package/src/event-handlers.ts +513 -612
  82. package/src/handlers.ts +120 -133
  83. package/src/nodeviews/ExternalDropTargets.tsx +68 -73
  84. package/src/nodeviews/OverflowShadowsObserver.ts +148 -157
  85. package/src/nodeviews/TableCell.ts +47 -54
  86. package/src/nodeviews/TableComponent.tsx +1018 -1112
  87. package/src/nodeviews/TableComponentWithSharedState.tsx +91 -94
  88. package/src/nodeviews/TableContainer.tsx +363 -339
  89. package/src/nodeviews/TableNodeViewBase.ts +19 -24
  90. package/src/nodeviews/TableResizer.tsx +565 -657
  91. package/src/nodeviews/TableRow.ts +580 -629
  92. package/src/nodeviews/TableStickyScrollbar.ts +173 -190
  93. package/src/nodeviews/__mocks__/OverflowShadowsObserver.ts +8 -8
  94. package/src/nodeviews/__mocks__/OverridableMock.ts +14 -15
  95. package/src/nodeviews/table.tsx +345 -375
  96. package/src/nodeviews/types.ts +21 -24
  97. package/src/nodeviews/update-overflow-shadows.ts +8 -14
  98. package/src/plugin.tsx +578 -603
  99. package/src/pm-plugins/analytics/actions.ts +10 -12
  100. package/src/pm-plugins/analytics/commands.ts +31 -37
  101. package/src/pm-plugins/analytics/plugin-factory.ts +4 -2
  102. package/src/pm-plugins/analytics/plugin-key.ts +1 -3
  103. package/src/pm-plugins/analytics/plugin.ts +60 -70
  104. package/src/pm-plugins/analytics/reducer.ts +19 -19
  105. package/src/pm-plugins/analytics/types.ts +10 -10
  106. package/src/pm-plugins/analytics/utils/moved-event.ts +38 -38
  107. package/src/pm-plugins/decorations/plugin.ts +58 -77
  108. package/src/pm-plugins/decorations/utils/column-controls.ts +59 -71
  109. package/src/pm-plugins/decorations/utils/column-resizing.ts +50 -57
  110. package/src/pm-plugins/decorations/utils/compose-decorations.ts +6 -6
  111. package/src/pm-plugins/decorations/utils/index.ts +3 -6
  112. package/src/pm-plugins/decorations/utils/types.ts +7 -12
  113. package/src/pm-plugins/default-table-selection.ts +3 -3
  114. package/src/pm-plugins/drag-and-drop/actions.ts +25 -25
  115. package/src/pm-plugins/drag-and-drop/commands-with-analytics.ts +158 -190
  116. package/src/pm-plugins/drag-and-drop/commands.ts +154 -170
  117. package/src/pm-plugins/drag-and-drop/consts.ts +4 -5
  118. package/src/pm-plugins/drag-and-drop/plugin-factory.ts +23 -20
  119. package/src/pm-plugins/drag-and-drop/plugin-key.ts +1 -3
  120. package/src/pm-plugins/drag-and-drop/plugin.ts +329 -383
  121. package/src/pm-plugins/drag-and-drop/reducer.ts +30 -30
  122. package/src/pm-plugins/drag-and-drop/types.ts +8 -8
  123. package/src/pm-plugins/drag-and-drop/utils/autoscrollers.ts +38 -41
  124. package/src/pm-plugins/drag-and-drop/utils/getDragBehaviour.ts +3 -6
  125. package/src/pm-plugins/drag-and-drop/utils/monitor.ts +57 -70
  126. package/src/pm-plugins/keymap.ts +208 -220
  127. package/src/pm-plugins/main.ts +348 -400
  128. package/src/pm-plugins/plugin-factory.ts +32 -34
  129. package/src/pm-plugins/safari-delete-composition-text-issue-workaround.ts +83 -97
  130. package/src/pm-plugins/sticky-headers/commands.ts +2 -6
  131. package/src/pm-plugins/sticky-headers/plugin-key.ts +1 -3
  132. package/src/pm-plugins/sticky-headers/plugin-state.ts +41 -44
  133. package/src/pm-plugins/sticky-headers/plugin.ts +4 -4
  134. package/src/pm-plugins/sticky-headers/types.ts +8 -8
  135. package/src/pm-plugins/sticky-headers/util.ts +10 -10
  136. package/src/pm-plugins/table-analytics.ts +70 -72
  137. package/src/pm-plugins/table-local-id.ts +180 -184
  138. package/src/pm-plugins/table-resizing/commands.ts +72 -85
  139. package/src/pm-plugins/table-resizing/event-handlers.ts +298 -317
  140. package/src/pm-plugins/table-resizing/plugin-factory.ts +10 -10
  141. package/src/pm-plugins/table-resizing/plugin-key.ts +1 -3
  142. package/src/pm-plugins/table-resizing/plugin.ts +61 -68
  143. package/src/pm-plugins/table-resizing/reducer.ts +30 -33
  144. package/src/pm-plugins/table-resizing/utils/colgroup.ts +84 -84
  145. package/src/pm-plugins/table-resizing/utils/column-state.ts +78 -81
  146. package/src/pm-plugins/table-resizing/utils/content-width.ts +94 -114
  147. package/src/pm-plugins/table-resizing/utils/dom.ts +93 -110
  148. package/src/pm-plugins/table-resizing/utils/index.ts +29 -34
  149. package/src/pm-plugins/table-resizing/utils/misc.ts +94 -119
  150. package/src/pm-plugins/table-resizing/utils/resize-column.ts +93 -106
  151. package/src/pm-plugins/table-resizing/utils/resize-logic.ts +240 -257
  152. package/src/pm-plugins/table-resizing/utils/resize-state.ts +343 -372
  153. package/src/pm-plugins/table-resizing/utils/scale-table.ts +202 -207
  154. package/src/pm-plugins/table-resizing/utils/types.ts +17 -17
  155. package/src/pm-plugins/table-resizing/utils/unit-to-number.ts +1 -2
  156. package/src/pm-plugins/table-selection-keymap.ts +25 -51
  157. package/src/pm-plugins/table-width.ts +191 -204
  158. package/src/pm-plugins/view-mode-sort/index.ts +223 -227
  159. package/src/pm-plugins/view-mode-sort/plugin-key.ts +3 -2
  160. package/src/pm-plugins/view-mode-sort/types.ts +12 -12
  161. package/src/pm-plugins/view-mode-sort/utils.ts +108 -117
  162. package/src/reducer.ts +139 -155
  163. package/src/toolbar.tsx +815 -905
  164. package/src/transforms/column-width.ts +186 -213
  165. package/src/transforms/delete-columns.ts +208 -222
  166. package/src/transforms/delete-rows.ts +117 -121
  167. package/src/transforms/fix-tables.ts +190 -215
  168. package/src/transforms/merge.ts +263 -269
  169. package/src/transforms/replace-table.ts +27 -43
  170. package/src/transforms/split.ts +65 -75
  171. package/src/types.ts +421 -427
  172. package/src/ui/ColumnResizeWidget/index.tsx +40 -47
  173. package/src/ui/DragHandle/HandleIconComponent.tsx +9 -13
  174. package/src/ui/DragHandle/index.tsx +221 -250
  175. package/src/ui/DragPreview/index.tsx +35 -35
  176. package/src/ui/FloatingAlignmentButtons/FloatingAlignmentButtons.tsx +33 -41
  177. package/src/ui/FloatingContextualButton/FixedButton.tsx +154 -157
  178. package/src/ui/FloatingContextualButton/index.tsx +109 -115
  179. package/src/ui/FloatingContextualButton/styles.ts +43 -46
  180. package/src/ui/FloatingContextualMenu/ContextualMenu.tsx +634 -694
  181. package/src/ui/FloatingContextualMenu/index.tsx +83 -101
  182. package/src/ui/FloatingContextualMenu/styles.ts +57 -65
  183. package/src/ui/FloatingDeleteButton/DeleteButton.tsx +37 -37
  184. package/src/ui/FloatingDeleteButton/getPopUpOptions.ts +47 -57
  185. package/src/ui/FloatingDeleteButton/index.tsx +319 -350
  186. package/src/ui/FloatingDragMenu/DragMenu.tsx +555 -596
  187. package/src/ui/FloatingDragMenu/DropdownMenu.tsx +152 -162
  188. package/src/ui/FloatingDragMenu/index.tsx +88 -102
  189. package/src/ui/FloatingDragMenu/styles.ts +51 -54
  190. package/src/ui/FloatingInsertButton/InsertButton.tsx +204 -217
  191. package/src/ui/FloatingInsertButton/getPopupOptions.ts +100 -115
  192. package/src/ui/FloatingInsertButton/index.tsx +248 -292
  193. package/src/ui/FloatingToolbarLabel/FloatingToolbarLabel.tsx +24 -29
  194. package/src/ui/TableFloatingColumnControls/ColumnControls/index.tsx +308 -329
  195. package/src/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.tsx +85 -94
  196. package/src/ui/TableFloatingColumnControls/ColumnDropTargets/index.tsx +46 -46
  197. package/src/ui/TableFloatingColumnControls/index.tsx +116 -136
  198. package/src/ui/TableFloatingControls/CornerControls/ClassicCornerControls.tsx +79 -91
  199. package/src/ui/TableFloatingControls/CornerControls/DragCornerControls.tsx +95 -102
  200. package/src/ui/TableFloatingControls/CornerControls/index.tsx +1 -4
  201. package/src/ui/TableFloatingControls/CornerControls/types.ts +8 -8
  202. package/src/ui/TableFloatingControls/FloatingControlsWithSelection.tsx +50 -50
  203. package/src/ui/TableFloatingControls/NumberColumn/index.tsx +111 -124
  204. package/src/ui/TableFloatingControls/RowControls/ClassicControls.tsx +86 -105
  205. package/src/ui/TableFloatingControls/RowControls/DragControls.tsx +305 -341
  206. package/src/ui/TableFloatingControls/RowDropTarget/index.tsx +72 -75
  207. package/src/ui/TableFloatingControls/index.tsx +191 -193
  208. package/src/ui/TableFullWidthLabel/index.tsx +20 -20
  209. package/src/ui/common-styles.ts +880 -912
  210. package/src/ui/consts.ts +29 -74
  211. package/src/ui/icons/AddColLeftIcon.tsx +33 -39
  212. package/src/ui/icons/AddColRightIcon.tsx +33 -39
  213. package/src/ui/icons/AddRowAboveIcon.tsx +16 -22
  214. package/src/ui/icons/AddRowBelowIcon.tsx +33 -39
  215. package/src/ui/icons/DisplayModeIcon.tsx +31 -31
  216. package/src/ui/icons/DragHandleDisabledIcon.tsx +19 -21
  217. package/src/ui/icons/DragHandleIcon.tsx +12 -12
  218. package/src/ui/icons/DragInMotionIcon.tsx +45 -52
  219. package/src/ui/icons/MergeCellsIcon.tsx +22 -28
  220. package/src/ui/icons/MinimisedHandle.tsx +9 -9
  221. package/src/ui/icons/SplitCellIcon.tsx +30 -36
  222. package/src/ui/ui-styles.ts +769 -798
  223. package/src/utils/alignment.ts +1 -1
  224. package/src/utils/analytics.ts +192 -208
  225. package/src/utils/collapse.ts +55 -64
  226. package/src/utils/column-controls.ts +237 -254
  227. package/src/utils/create.ts +30 -30
  228. package/src/utils/decoration.ts +482 -502
  229. package/src/utils/dom.ts +127 -134
  230. package/src/utils/drag-menu.ts +322 -373
  231. package/src/utils/get-allow-add-column-custom-step.ts +4 -5
  232. package/src/utils/guidelines.ts +10 -21
  233. package/src/utils/index.ts +68 -68
  234. package/src/utils/merged-cells.ts +245 -254
  235. package/src/utils/nodes.ts +91 -106
  236. package/src/utils/paste.ts +119 -135
  237. package/src/utils/row-controls.ts +199 -213
  238. package/src/utils/selection.ts +77 -87
  239. package/src/utils/snapping.ts +84 -97
  240. package/src/utils/table.ts +44 -44
  241. package/src/utils/transforms.ts +5 -5
  242. package/src/utils/update-plugin-state-decorations.ts +5 -9
@@ -20,238 +20,234 @@ import type tablePlugin from '../../plugin';
20
20
  import { getPluginState } from '../plugin-factory';
21
21
 
22
22
  import {
23
- IS_DISABLED_CLASS_NAME,
24
- SORT_INDEX_DATA_ATTRIBUTE,
25
- SORTING_ICON_CLASS_NAME,
23
+ IS_DISABLED_CLASS_NAME,
24
+ SORT_INDEX_DATA_ATTRIBUTE,
25
+ SORTING_ICON_CLASS_NAME,
26
26
  } from './consts';
27
27
  import { tableViewModeSortPluginKey as key } from './plugin-key';
28
28
  import type { ViewModeSortPluginState } from './types';
29
29
  import { getTableElements, toggleSort } from './utils';
30
30
 
31
31
  export const createPlugin = (
32
- editorViewModeAPI: ExtractInjectionAPI<typeof tablePlugin>['editorViewMode'],
32
+ editorViewModeAPI: ExtractInjectionAPI<typeof tablePlugin>['editorViewMode'],
33
33
  ) => {
34
- return new SafePlugin({
35
- state: {
36
- init: () => ({
37
- decorations: DecorationSet.empty,
38
- sort: {},
39
- allTables: [],
40
- }),
41
- apply(tr, pluginState: ViewModeSortPluginState, oldState) {
42
- // TODO - move this mode check to plugin creation if possible. Right now it's here because the initial state
43
- // does not appear correct when the plugin is created.
44
- const { mode } = editorViewModeAPI?.sharedState.currentState() || {};
45
- if (mode !== 'view') {
46
- return pluginState;
47
- }
48
- let { decorations, sort, allTables } = pluginState;
49
-
50
- const sortMeta = tr.getMeta('tableSortMeta');
51
-
52
- let hoverTableMeta = tr.getMeta('mouseEnterTable');
53
- let removeTableMeta = tr.getMeta('removeTable');
54
- let tableId = '';
55
-
56
- // Remove the table from the state
57
- if (removeTableMeta) {
58
- allTables = allTables.filter(([id]) => id !== removeTableMeta);
59
- } else {
60
- tableId = hoverTableMeta?.[0];
61
- }
62
-
63
- sort = { ...sort, ...sortMeta };
64
-
65
- const isTableInState = allTables.some(([id]) => id === tableId);
66
-
67
- // Update the table in the state
68
- if (hoverTableMeta) {
69
- allTables = allTables.filter(([id]) => id !== hoverTableMeta[0]);
70
- allTables.push(hoverTableMeta);
71
- }
72
-
73
- /**
74
- * Create decorations for the sorting icons
75
- */
76
- const decs: Decoration[] = [];
77
-
78
- // TODO - add support for keyboard only users
79
- if ((hoverTableMeta && !isTableInState) || sortMeta) {
80
- allTables.forEach((table) => {
81
- const [tableId, _node, pos] = table;
82
- const tableNode = tr.doc.nodeAt(tr.mapping.map(pos));
83
- if (!tableNode || tableNode.type.name !== 'table') {
84
- return pluginState;
85
- }
86
- const map = TableMap.get(tableNode);
87
- const hasMergedCells = new Set(map.map).size !== map.map.length;
88
- map.mapByRow[0].forEach((cell, index) => {
89
- // return pluginState;
90
- decs.push(
91
- Decoration.widget(cell + pos + 2, () => {
92
- const element = document.createElement('div');
93
- element.setAttribute(SORT_INDEX_DATA_ATTRIBUTE, `${index}`);
94
- element.classList.add(SORTING_ICON_CLASS_NAME);
95
- if (hasMergedCells) {
96
- element.classList.add(IS_DISABLED_CLASS_NAME);
97
- }
98
-
99
- let sortOrdered;
100
- if (index === sort[tableId]?.index) {
101
- sortOrdered = sort[tableId]?.direction;
102
- } else {
103
- sortOrdered = SortOrder.NO_ORDER;
104
- }
105
-
106
- const { getIntl } = getPluginState(oldState);
107
-
108
- ReactDOM.render(
109
- createElement(
110
- RawIntlProvider,
111
- { value: getIntl() },
112
- createElement(SortingIcon, {
113
- isSortingAllowed: !hasMergedCells,
114
- sortOrdered,
115
- onClick: () => {},
116
- onKeyDown: () => {},
117
- }),
118
- ),
119
- element,
120
- );
121
- return element;
122
- }),
123
- );
124
- });
125
- });
126
- decorations = DecorationSet.create(tr.doc, decs);
127
- }
128
-
129
- /**
130
- * Map the decorations to the new document if there are changes
131
- */
132
- if (tr.docChanged) {
133
- decorations = decorations.map(tr.mapping, tr.doc);
134
- allTables = allTables.map((table) => {
135
- return [table[0], table[1], tr.mapping.map(table[2])];
136
- });
137
- }
138
-
139
- return {
140
- decorations,
141
- sort,
142
- allTables,
143
- };
144
- },
145
- },
146
- key,
147
- appendTransaction: (trs, oldState, newState) => {
148
- // return newState.tr;
149
- const { mode } = editorViewModeAPI?.sharedState.currentState() || {};
150
- if (mode !== 'view') {
151
- return newState.tr;
152
- }
153
-
154
- let allTables = key.getState(newState)?.allTables || [];
155
-
156
- /**
157
- * If incoming changes have affected a table node, remove the sorting. This prevents the
158
- * table from breaking if changes like merged cells are incoming.
159
- */
160
- for (const tr of trs) {
161
- const hoverTableMeta = tr.getMeta('mouseEnterTable');
162
- if (hoverTableMeta) {
163
- allTables = allTables.filter(([id]) => id !== hoverTableMeta[0]);
164
- allTables.push(hoverTableMeta);
165
- }
166
- const isRemote = tr.getMeta('isRemote');
167
- const isDocChanged = tr.docChanged;
168
- const isChangesIncoming = isRemote && isDocChanged;
169
-
170
- const oldPluginState = key.getState(oldState);
171
- const newPluginState = key.getState(newState);
172
-
173
- for (const table of allTables) {
174
- const [tableId, node, pos] = table;
175
- const {
176
- order: oldOrder,
177
- direction: oldDirection,
178
- index: oldIndex,
179
- } = oldPluginState?.sort?.[tableId] || {};
180
-
181
- if (isChangesIncoming) {
182
- const maybeTableNode = tr.doc.nodeAt(pos);
183
- const isTableNodeChanged =
184
- maybeTableNode?.attrs?.localId !== tableId ||
185
- !node.eq(maybeTableNode);
186
-
187
- if (isTableNodeChanged) {
188
- const newtr = newState.tr;
189
- newtr.setMeta('tableSortMeta', {
190
- [tableId]: {},
191
- });
192
- newtr.setMeta('removeTable', tableId);
193
-
194
- // Unsort the table here
195
- if (oldOrder !== undefined) {
196
- const { rows, tbody } = getTableElements(tableId);
197
- if (!rows || !tbody) {
198
- return newtr;
199
- }
200
- const sortedOrder = [...oldOrder].sort(
201
- (a, b) => a.value - b.value,
202
- );
203
- sortedOrder.forEach((index, i) => {
204
- tbody.appendChild(rows[index.index + 1]);
205
- });
206
- return newtr;
207
- }
208
- }
209
- }
210
-
211
- /**
212
- * Sort the table if the sort order has changed
213
- */
214
- const {
215
- order: newOrder,
216
- direction: newDirection,
217
- index: newIndex,
218
- } = newPluginState?.sort?.[tableId] || {};
219
- const orderChanged =
220
- oldDirection !== newDirection || oldIndex !== newIndex;
221
-
222
- if (orderChanged) {
223
- if (!isRemote && newDirection !== SortOrder.NO_ORDER) {
224
- const { rows, tbody } = getTableElements(tableId);
225
- if (rows && newOrder) {
226
- newOrder.forEach((index, i) => {
227
- tbody?.appendChild(rows[index.value + 1]);
228
- });
229
- }
230
- }
231
- }
232
- }
233
- }
234
- return newState.tr;
235
- },
236
- props: {
237
- handleDOMEvents: {
238
- keydown: (view, event) => {
239
- // TODO - fix the focus issue here, where toggling sort with a keypress loses focus
240
- if (event.key === 'Enter' || event.key === ' ') {
241
- const pluginState = key.getState(view.state)?.sort || {};
242
- toggleSort(view, event, pluginState);
243
- }
244
- },
245
-
246
- click: (view, event) => {
247
- const pluginState = key.getState(view.state)?.sort || {};
248
- toggleSort(view, event, pluginState);
249
- },
250
- },
251
- decorations(state) {
252
- const decs = key.getState(state)?.decorations || DecorationSet.empty;
253
- return decs;
254
- },
255
- },
256
- });
34
+ return new SafePlugin({
35
+ state: {
36
+ init: () => ({
37
+ decorations: DecorationSet.empty,
38
+ sort: {},
39
+ allTables: [],
40
+ }),
41
+ apply(tr, pluginState: ViewModeSortPluginState, oldState) {
42
+ // TODO - move this mode check to plugin creation if possible. Right now it's here because the initial state
43
+ // does not appear correct when the plugin is created.
44
+ const { mode } = editorViewModeAPI?.sharedState.currentState() || {};
45
+ if (mode !== 'view') {
46
+ return pluginState;
47
+ }
48
+ let { decorations, sort, allTables } = pluginState;
49
+
50
+ const sortMeta = tr.getMeta('tableSortMeta');
51
+
52
+ let hoverTableMeta = tr.getMeta('mouseEnterTable');
53
+ let removeTableMeta = tr.getMeta('removeTable');
54
+ let tableId = '';
55
+
56
+ // Remove the table from the state
57
+ if (removeTableMeta) {
58
+ allTables = allTables.filter(([id]) => id !== removeTableMeta);
59
+ } else {
60
+ tableId = hoverTableMeta?.[0];
61
+ }
62
+
63
+ sort = { ...sort, ...sortMeta };
64
+
65
+ const isTableInState = allTables.some(([id]) => id === tableId);
66
+
67
+ // Update the table in the state
68
+ if (hoverTableMeta) {
69
+ allTables = allTables.filter(([id]) => id !== hoverTableMeta[0]);
70
+ allTables.push(hoverTableMeta);
71
+ }
72
+
73
+ /**
74
+ * Create decorations for the sorting icons
75
+ */
76
+ const decs: Decoration[] = [];
77
+
78
+ // TODO - add support for keyboard only users
79
+ if ((hoverTableMeta && !isTableInState) || sortMeta) {
80
+ allTables.forEach((table) => {
81
+ const [tableId, _node, pos] = table;
82
+ const tableNode = tr.doc.nodeAt(tr.mapping.map(pos));
83
+ if (!tableNode || tableNode.type.name !== 'table') {
84
+ return pluginState;
85
+ }
86
+ const map = TableMap.get(tableNode);
87
+ const hasMergedCells = new Set(map.map).size !== map.map.length;
88
+ map.mapByRow[0].forEach((cell, index) => {
89
+ // return pluginState;
90
+ decs.push(
91
+ Decoration.widget(cell + pos + 2, () => {
92
+ const element = document.createElement('div');
93
+ element.setAttribute(SORT_INDEX_DATA_ATTRIBUTE, `${index}`);
94
+ element.classList.add(SORTING_ICON_CLASS_NAME);
95
+ if (hasMergedCells) {
96
+ element.classList.add(IS_DISABLED_CLASS_NAME);
97
+ }
98
+
99
+ let sortOrdered;
100
+ if (index === sort[tableId]?.index) {
101
+ sortOrdered = sort[tableId]?.direction;
102
+ } else {
103
+ sortOrdered = SortOrder.NO_ORDER;
104
+ }
105
+
106
+ const { getIntl } = getPluginState(oldState);
107
+
108
+ ReactDOM.render(
109
+ createElement(
110
+ RawIntlProvider,
111
+ { value: getIntl() },
112
+ createElement(SortingIcon, {
113
+ isSortingAllowed: !hasMergedCells,
114
+ sortOrdered,
115
+ onClick: () => {},
116
+ onKeyDown: () => {},
117
+ }),
118
+ ),
119
+ element,
120
+ );
121
+ return element;
122
+ }),
123
+ );
124
+ });
125
+ });
126
+ decorations = DecorationSet.create(tr.doc, decs);
127
+ }
128
+
129
+ /**
130
+ * Map the decorations to the new document if there are changes
131
+ */
132
+ if (tr.docChanged) {
133
+ decorations = decorations.map(tr.mapping, tr.doc);
134
+ allTables = allTables.map((table) => {
135
+ return [table[0], table[1], tr.mapping.map(table[2])];
136
+ });
137
+ }
138
+
139
+ return {
140
+ decorations,
141
+ sort,
142
+ allTables,
143
+ };
144
+ },
145
+ },
146
+ key,
147
+ appendTransaction: (trs, oldState, newState) => {
148
+ // return newState.tr;
149
+ const { mode } = editorViewModeAPI?.sharedState.currentState() || {};
150
+ if (mode !== 'view') {
151
+ return newState.tr;
152
+ }
153
+
154
+ let allTables = key.getState(newState)?.allTables || [];
155
+
156
+ /**
157
+ * If incoming changes have affected a table node, remove the sorting. This prevents the
158
+ * table from breaking if changes like merged cells are incoming.
159
+ */
160
+ for (const tr of trs) {
161
+ const hoverTableMeta = tr.getMeta('mouseEnterTable');
162
+ if (hoverTableMeta) {
163
+ allTables = allTables.filter(([id]) => id !== hoverTableMeta[0]);
164
+ allTables.push(hoverTableMeta);
165
+ }
166
+ const isRemote = tr.getMeta('isRemote');
167
+ const isDocChanged = tr.docChanged;
168
+ const isChangesIncoming = isRemote && isDocChanged;
169
+
170
+ const oldPluginState = key.getState(oldState);
171
+ const newPluginState = key.getState(newState);
172
+
173
+ for (const table of allTables) {
174
+ const [tableId, node, pos] = table;
175
+ const {
176
+ order: oldOrder,
177
+ direction: oldDirection,
178
+ index: oldIndex,
179
+ } = oldPluginState?.sort?.[tableId] || {};
180
+
181
+ if (isChangesIncoming) {
182
+ const maybeTableNode = tr.doc.nodeAt(pos);
183
+ const isTableNodeChanged =
184
+ maybeTableNode?.attrs?.localId !== tableId || !node.eq(maybeTableNode);
185
+
186
+ if (isTableNodeChanged) {
187
+ const newtr = newState.tr;
188
+ newtr.setMeta('tableSortMeta', {
189
+ [tableId]: {},
190
+ });
191
+ newtr.setMeta('removeTable', tableId);
192
+
193
+ // Unsort the table here
194
+ if (oldOrder !== undefined) {
195
+ const { rows, tbody } = getTableElements(tableId);
196
+ if (!rows || !tbody) {
197
+ return newtr;
198
+ }
199
+ const sortedOrder = [...oldOrder].sort((a, b) => a.value - b.value);
200
+ sortedOrder.forEach((index, i) => {
201
+ tbody.appendChild(rows[index.index + 1]);
202
+ });
203
+ return newtr;
204
+ }
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Sort the table if the sort order has changed
210
+ */
211
+ const {
212
+ order: newOrder,
213
+ direction: newDirection,
214
+ index: newIndex,
215
+ } = newPluginState?.sort?.[tableId] || {};
216
+ const orderChanged = oldDirection !== newDirection || oldIndex !== newIndex;
217
+
218
+ if (orderChanged) {
219
+ if (!isRemote && newDirection !== SortOrder.NO_ORDER) {
220
+ const { rows, tbody } = getTableElements(tableId);
221
+ if (rows && newOrder) {
222
+ newOrder.forEach((index, i) => {
223
+ tbody?.appendChild(rows[index.value + 1]);
224
+ });
225
+ }
226
+ }
227
+ }
228
+ }
229
+ }
230
+ return newState.tr;
231
+ },
232
+ props: {
233
+ handleDOMEvents: {
234
+ keydown: (view, event) => {
235
+ // TODO - fix the focus issue here, where toggling sort with a keypress loses focus
236
+ if (event.key === 'Enter' || event.key === ' ') {
237
+ const pluginState = key.getState(view.state)?.sort || {};
238
+ toggleSort(view, event, pluginState);
239
+ }
240
+ },
241
+
242
+ click: (view, event) => {
243
+ const pluginState = key.getState(view.state)?.sort || {};
244
+ toggleSort(view, event, pluginState);
245
+ },
246
+ },
247
+ decorations(state) {
248
+ const decs = key.getState(state)?.decorations || DecorationSet.empty;
249
+ return decs;
250
+ },
251
+ },
252
+ });
257
253
  };
@@ -2,5 +2,6 @@ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
2
2
 
3
3
  import type { ViewModeSortPluginState } from './types';
4
4
 
5
- export const tableViewModeSortPluginKey =
6
- new PluginKey<ViewModeSortPluginState>('tableViewModeSortPlugin');
5
+ export const tableViewModeSortPluginKey = new PluginKey<ViewModeSortPluginState>(
6
+ 'tableViewModeSortPlugin',
7
+ );
@@ -3,21 +3,21 @@ import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
3
3
  import type { DecorationSet } from '@atlaskit/editor-prosemirror/view';
4
4
 
5
5
  export type TableSortMeta = Record<
6
- string,
7
- {
8
- index: number;
9
- order: {
10
- index: number;
11
- value: number;
12
- }[];
13
- direction: SortOrder;
14
- }
6
+ string,
7
+ {
8
+ index: number;
9
+ order: {
10
+ index: number;
11
+ value: number;
12
+ }[];
13
+ direction: SortOrder;
14
+ }
15
15
  >;
16
16
 
17
17
  export interface ViewModeSortPluginState {
18
- decorations: DecorationSet;
19
- sort: TableSortMeta;
20
- allTables: HoverTableMeta[];
18
+ decorations: DecorationSet;
19
+ sort: TableSortMeta;
20
+ allTables: HoverTableMeta[];
21
21
  }
22
22
 
23
23
  export type HoverTableMeta = [string, PMNode, number];