@atlaskit/editor-plugin-table 5.8.2 → 5.8.3

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.
@@ -3,7 +3,9 @@ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
3
3
  import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
4
4
  import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
5
5
  import { getCellsInRow } from '@atlaskit/editor-tables/utils';
6
+ import { autoScrollForElements } from '@atlaskit/pragmatic-drag-and-drop-auto-scroll/element';
6
7
  import { monitorForElements } from '@atlaskit/pragmatic-drag-and-drop/adapter/element';
8
+ import { combine } from '@atlaskit/pragmatic-drag-and-drop/util/combine';
7
9
  import { findNearestCellIndexToPoint, hasMergedCellsInBetween } from '../../utils';
8
10
  import { getPluginState as getTablePluginState } from '../plugin-factory';
9
11
  import { pluginKey as tablePluginKey } from '../plugin-key';
@@ -15,6 +17,161 @@ import { DRAGGABLE_TABLE_NODE_SIZE_LIMIT, DropTargetType } from './consts';
15
17
  import { createPluginState, getPluginState } from './plugin-factory';
16
18
  import { pluginKey } from './plugin-key';
17
19
  import { getDraggableDataFromEvent } from './utils/monitor';
20
+ const destroyFn = (editorView, editorAnalyticsAPI) => {
21
+ const editorPageScrollContainer = document.querySelector('.fabric-editor-popup-scroll-parent');
22
+ const rowAutoScrollers = editorPageScrollContainer ? [monitorForElements({
23
+ canMonitor({
24
+ source
25
+ }) {
26
+ const {
27
+ type
28
+ } = source.data;
29
+ return type === 'table-row';
30
+ },
31
+ onDragStart() {
32
+ // auto scroller doesn't work when scroll-behavior: smooth is set, this monitor temporarily removes it via inline styles
33
+ editorPageScrollContainer.style.setProperty('scroll-behavior', 'unset');
34
+ },
35
+ onDrop() {
36
+ // 'null' will remove the inline style
37
+ editorPageScrollContainer.style.setProperty('scroll-behavior', null);
38
+ }
39
+ }), autoScrollForElements({
40
+ element: editorPageScrollContainer,
41
+ canScroll: ({
42
+ source
43
+ }) => {
44
+ const {
45
+ type
46
+ } = source.data;
47
+ return type === 'table-row';
48
+ }
49
+ })] : [];
50
+ return combine(...rowAutoScrollers, monitorForElements({
51
+ canMonitor({
52
+ source
53
+ }) {
54
+ const {
55
+ type,
56
+ localId,
57
+ indexes
58
+ } = source.data;
59
+
60
+ // First; Perform any quick checks so we can abort early.
61
+ if (!indexes || !localId ||
62
+ // FIXME: We currently don't support DragNDrop of multiple elements. For now we will not bother to monitor drags
63
+ // of more then 1 item.
64
+ indexes.length !== 1 || !(type === 'table-row' || type === 'table-column')) {
65
+ return false;
66
+ }
67
+ const {
68
+ tableNode
69
+ } = getTablePluginState(editorView.state);
70
+ // If the draggable localId is the same as the current selected table localId then we will allow the monitor
71
+ // watch for changes
72
+ return localId === (tableNode === null || tableNode === void 0 ? void 0 : tableNode.attrs.localId);
73
+ },
74
+ onDragStart: ({
75
+ location
76
+ }) => {
77
+ toggleDragMenu(false)(editorView.state, editorView.dispatch);
78
+ },
79
+ onDrag(event) {
80
+ const data = getDraggableDataFromEvent(event);
81
+ // If no data can be found then it's most like we do not want to perform any drag actions
82
+ if (!data) {
83
+ clearDropTarget()(editorView.state, editorView.dispatch);
84
+ return;
85
+ }
86
+
87
+ // TODO: as we drag an element around we are going to want to update the state to acurately reflect the current
88
+ // insert location as to where the draggable will most likely be go. For example;
89
+ const {
90
+ sourceType,
91
+ targetAdjustedIndex
92
+ } = data;
93
+ const dropTargetType = sourceType === 'table-row' ? DropTargetType.ROW : DropTargetType.COLUMN;
94
+ const hasMergedCells = hasMergedCellsInBetween([targetAdjustedIndex - 1, targetAdjustedIndex], dropTargetType)(editorView.state.selection);
95
+ setDropTarget(dropTargetType, targetAdjustedIndex, hasMergedCells)(editorView.state, editorView.dispatch);
96
+ },
97
+ onDrop(event) {
98
+ var _cell$row, _cell$col;
99
+ const data = getDraggableDataFromEvent(event);
100
+
101
+ // On Drop we need to update the table main plugin hoveredCell value with the current row/col that the mouse is
102
+ // over. This is so the drag handles update their positions to correctly align with the users mouse. Unfortunately
103
+ // at this point in time and during the drag opertation, the drop targets are eating all the mouse events so
104
+ // it's not possible to know what row/col the mouse is over (via mouse events). This attempts to locate the nearest cell and
105
+ // then tries to update the main table hoveredCell value by piggy-backing the transaction onto the command
106
+ // triggered by this on drop event.
107
+ const {
108
+ hoveredCell
109
+ } = getTablePluginState(editorView.state);
110
+ const cell = findNearestCellIndexToPoint(event.location.current.input.clientX, event.location.current.input.clientY);
111
+ const tr = editorView.state.tr;
112
+ const action = {
113
+ type: 'HOVER_CELL',
114
+ data: {
115
+ hoveredCell: {
116
+ rowIndex: (_cell$row = cell === null || cell === void 0 ? void 0 : cell.row) !== null && _cell$row !== void 0 ? _cell$row : hoveredCell.rowIndex,
117
+ colIndex: (_cell$col = cell === null || cell === void 0 ? void 0 : cell.col) !== null && _cell$col !== void 0 ? _cell$col : hoveredCell.colIndex
118
+ }
119
+ }
120
+ };
121
+ tr.setMeta(tablePluginKey, action);
122
+
123
+ // If no data can be found then it's most like we do not want to perform any drop action
124
+ if (!data) {
125
+ var _event$source, _event$source$data, _event$source2, _event$source2$data;
126
+ // If we're able to determine the source type of the dropped element then we should report to analytics that
127
+ // the drop event was cancelled. Otherwise we will cancel silently.
128
+ if ((event === null || event === void 0 ? void 0 : (_event$source = event.source) === null || _event$source === void 0 ? void 0 : (_event$source$data = _event$source.data) === null || _event$source$data === void 0 ? void 0 : _event$source$data.type) === 'table-row' || (event === null || event === void 0 ? void 0 : (_event$source2 = event.source) === null || _event$source2 === void 0 ? void 0 : (_event$source2$data = _event$source2.data) === null || _event$source2$data === void 0 ? void 0 : _event$source2$data.type) === 'table-column') {
129
+ return clearDropTargetWithAnalytics(editorAnalyticsAPI)(INPUT_METHOD.DRAG_AND_DROP, event.source.data.type, TABLE_STATUS.CANCELLED, tr)(editorView.state, editorView.dispatch);
130
+ }
131
+ return clearDropTarget(tr)(editorView.state, editorView.dispatch);
132
+ }
133
+ const {
134
+ sourceType,
135
+ sourceIndexes,
136
+ targetIndex,
137
+ targetAdjustedIndex,
138
+ direction
139
+ } = data;
140
+
141
+ // When we drop on a target we will know the targets row/col index for certain,
142
+ if (sourceType === 'table-row') {
143
+ action.data.hoveredCell.rowIndex = targetIndex;
144
+ } else {
145
+ action.data.hoveredCell.colIndex = targetIndex;
146
+ }
147
+
148
+ // If the drop target index contains merged cells then we should not allow the drop to occur.
149
+ if (hasMergedCellsInBetween([targetAdjustedIndex - 1, targetAdjustedIndex], sourceType === 'table-row' ? DropTargetType.ROW : DropTargetType.COLUMN)(editorView.state.selection)) {
150
+ clearDropTargetWithAnalytics(editorAnalyticsAPI)(INPUT_METHOD.DRAG_AND_DROP, sourceType,
151
+ // This event is mrked as invalid because the user is attempting to drop an element in an area which has merged cells.
152
+ TABLE_STATUS.INVALID, tr)(editorView.state, editorView.dispatch);
153
+ return;
154
+ }
155
+ const [sourceIndex] = sourceIndexes;
156
+ requestAnimationFrame(() => {
157
+ moveSourceWithAnalytics(editorAnalyticsAPI)(INPUT_METHOD.DRAG_AND_DROP, sourceType, sourceIndex, targetAdjustedIndex + (direction === -1 ? 0 : -1), tr)(editorView.state, editorView.dispatch);
158
+
159
+ // force a colgroup update here, otherwise dropped columns don't have
160
+ // the correct width immediately after the drop
161
+ if (sourceType === 'table-column') {
162
+ const {
163
+ tableRef,
164
+ tableNode
165
+ } = getTablePluginState(editorView.state);
166
+ if (tableRef && tableNode) {
167
+ insertColgroupFromNode(tableRef, tableNode);
168
+ }
169
+ }
170
+ editorView.focus();
171
+ });
172
+ }
173
+ }));
174
+ };
18
175
  export const createPlugin = (dispatch, eventDispatcher, editorAnalyticsAPI) => {
19
176
  return new SafePlugin({
20
177
  state: createPluginState(dispatch, state => ({
@@ -93,130 +250,7 @@ export const createPlugin = (dispatch, eventDispatcher, editorAnalyticsAPI) => {
93
250
  },
94
251
  view: editorView => {
95
252
  return {
96
- destroy: monitorForElements({
97
- canMonitor({
98
- source
99
- }) {
100
- const {
101
- type,
102
- localId,
103
- indexes
104
- } = source.data;
105
-
106
- // First; Perform any quick checks so we can abort early.
107
- if (!indexes || !localId ||
108
- // FIXME: We currently don't support DragNDrop of multiple elements. For now we will not bother to monitor drags
109
- // of more then 1 item.
110
- indexes.length !== 1 || !(type === 'table-row' || type === 'table-column')) {
111
- return false;
112
- }
113
- const {
114
- tableNode
115
- } = getTablePluginState(editorView.state);
116
- // If the draggable localId is the same as the current selected table localId then we will allow the monitor
117
- // watch for changes
118
- return localId === (tableNode === null || tableNode === void 0 ? void 0 : tableNode.attrs.localId);
119
- },
120
- onDragStart: ({
121
- location
122
- }) => {
123
- toggleDragMenu(false)(editorView.state, editorView.dispatch);
124
- },
125
- onDrag(event) {
126
- const data = getDraggableDataFromEvent(event);
127
- // If no data can be found then it's most like we do not want to perform any drag actions
128
- if (!data) {
129
- clearDropTarget()(editorView.state, editorView.dispatch);
130
- return;
131
- }
132
-
133
- // TODO: as we drag an element around we are going to want to update the state to acurately reflect the current
134
- // insert location as to where the draggable will most likely be go. For example;
135
- const {
136
- sourceType,
137
- targetAdjustedIndex
138
- } = data;
139
- const dropTargetType = sourceType === 'table-row' ? DropTargetType.ROW : DropTargetType.COLUMN;
140
- const hasMergedCells = hasMergedCellsInBetween([targetAdjustedIndex - 1, targetAdjustedIndex], dropTargetType)(editorView.state.selection);
141
- setDropTarget(dropTargetType, targetAdjustedIndex, hasMergedCells)(editorView.state, editorView.dispatch);
142
- },
143
- onDrop(event) {
144
- var _cell$row, _cell$col;
145
- const data = getDraggableDataFromEvent(event);
146
-
147
- // On Drop we need to update the table main plugin hoveredCell value with the current row/col that the mouse is
148
- // over. This is so the drag handles update their positions to correctly align with the users mouse. Unfortunately
149
- // at this point in time and during the drag opertation, the drop targets are eating all the mouse events so
150
- // it's not possible to know what row/col the mouse is over (via mouse events). This attempts to locate the nearest cell and
151
- // then tries to update the main table hoveredCell value by piggy-backing the transaction onto the command
152
- // triggered by this on drop event.
153
- const {
154
- hoveredCell
155
- } = getTablePluginState(editorView.state);
156
- const cell = findNearestCellIndexToPoint(event.location.current.input.clientX, event.location.current.input.clientY);
157
- const tr = editorView.state.tr;
158
- const action = {
159
- type: 'HOVER_CELL',
160
- data: {
161
- hoveredCell: {
162
- rowIndex: (_cell$row = cell === null || cell === void 0 ? void 0 : cell.row) !== null && _cell$row !== void 0 ? _cell$row : hoveredCell.rowIndex,
163
- colIndex: (_cell$col = cell === null || cell === void 0 ? void 0 : cell.col) !== null && _cell$col !== void 0 ? _cell$col : hoveredCell.colIndex
164
- }
165
- }
166
- };
167
- tr.setMeta(tablePluginKey, action);
168
-
169
- // If no data can be found then it's most like we do not want to perform any drop action
170
- if (!data) {
171
- var _event$source, _event$source$data, _event$source2, _event$source2$data;
172
- // If we're able to determine the source type of the dropped element then we should report to analytics that
173
- // the drop event was cancelled. Otherwise we will cancel silently.
174
- if ((event === null || event === void 0 ? void 0 : (_event$source = event.source) === null || _event$source === void 0 ? void 0 : (_event$source$data = _event$source.data) === null || _event$source$data === void 0 ? void 0 : _event$source$data.type) === 'table-row' || (event === null || event === void 0 ? void 0 : (_event$source2 = event.source) === null || _event$source2 === void 0 ? void 0 : (_event$source2$data = _event$source2.data) === null || _event$source2$data === void 0 ? void 0 : _event$source2$data.type) === 'table-column') {
175
- return clearDropTargetWithAnalytics(editorAnalyticsAPI)(INPUT_METHOD.DRAG_AND_DROP, event.source.data.type, TABLE_STATUS.CANCELLED, tr)(editorView.state, editorView.dispatch);
176
- }
177
- return clearDropTarget(tr)(editorView.state, editorView.dispatch);
178
- }
179
- const {
180
- sourceType,
181
- sourceIndexes,
182
- targetIndex,
183
- targetAdjustedIndex,
184
- direction
185
- } = data;
186
-
187
- // When we drop on a target we will know the targets row/col index for certain,
188
- if (sourceType === 'table-row') {
189
- action.data.hoveredCell.rowIndex = targetIndex;
190
- } else {
191
- action.data.hoveredCell.colIndex = targetIndex;
192
- }
193
-
194
- // If the drop target index contains merged cells then we should not allow the drop to occur.
195
- if (hasMergedCellsInBetween([targetAdjustedIndex - 1, targetAdjustedIndex], sourceType === 'table-row' ? DropTargetType.ROW : DropTargetType.COLUMN)(editorView.state.selection)) {
196
- clearDropTargetWithAnalytics(editorAnalyticsAPI)(INPUT_METHOD.DRAG_AND_DROP, sourceType,
197
- // This event is mrked as invalid because the user is attempting to drop an element in an area which has merged cells.
198
- TABLE_STATUS.INVALID, tr)(editorView.state, editorView.dispatch);
199
- return;
200
- }
201
- const [sourceIndex] = sourceIndexes;
202
- requestAnimationFrame(() => {
203
- moveSourceWithAnalytics(editorAnalyticsAPI)(INPUT_METHOD.DRAG_AND_DROP, sourceType, sourceIndex, targetAdjustedIndex + (direction === -1 ? 0 : -1), tr)(editorView.state, editorView.dispatch);
204
-
205
- // force a colgroup update here, otherwise dropped columns don't have
206
- // the correct width immediately after the drop
207
- if (sourceType === 'table-column') {
208
- const {
209
- tableRef,
210
- tableNode
211
- } = getTablePluginState(editorView.state);
212
- if (tableRef && tableNode) {
213
- insertColgroupFromNode(tableRef, tableNode);
214
- }
215
- }
216
- editorView.focus();
217
- });
218
- }
219
- })
253
+ destroy: destroyFn(editorView, editorAnalyticsAPI)
220
254
  };
221
255
  },
222
256
  props: {
@@ -0,0 +1,50 @@
1
+ import { autoScrollForElements } from '@atlaskit/pragmatic-drag-and-drop-auto-scroll/element';
2
+ import { unsafeOverflowAutoScrollForElements } from '@atlaskit/pragmatic-drag-and-drop-auto-scroll/unsafe-overflow/element';
3
+ import { dropTargetExtendedWidth } from '../../../ui/consts';
4
+ export const autoScrollerFactory = ({
5
+ tableWrapper,
6
+ getNode
7
+ }) => {
8
+ return [autoScrollForElements({
9
+ element: tableWrapper,
10
+ canScroll: ({
11
+ source
12
+ }) => {
13
+ const {
14
+ localId,
15
+ type
16
+ } = source.data;
17
+ const node = getNode();
18
+ return localId === (node === null || node === void 0 ? void 0 : node.attrs.localId) && type === 'table-column';
19
+ }
20
+ }), unsafeOverflowAutoScrollForElements({
21
+ element: tableWrapper,
22
+ canScroll: ({
23
+ source
24
+ }) => {
25
+ const {
26
+ localId,
27
+ type
28
+ } = source.data;
29
+ const node = getNode();
30
+ return localId === (node === null || node === void 0 ? void 0 : node.attrs.localId) && type === 'table-column';
31
+ },
32
+ getOverflow: () => ({
33
+ fromTopEdge: {
34
+ top: dropTargetExtendedWidth,
35
+ right: dropTargetExtendedWidth,
36
+ left: dropTargetExtendedWidth
37
+ },
38
+ fromRightEdge: {
39
+ right: dropTargetExtendedWidth,
40
+ top: dropTargetExtendedWidth,
41
+ bottom: dropTargetExtendedWidth
42
+ },
43
+ fromLeftEdge: {
44
+ top: dropTargetExtendedWidth,
45
+ left: dropTargetExtendedWidth,
46
+ bottom: dropTargetExtendedWidth
47
+ }
48
+ })
49
+ })];
50
+ };
@@ -1 +1,2 @@
1
- export { getDraggableDataFromEvent } from './monitor';
1
+ export { getDraggableDataFromEvent } from './monitor';
2
+ export { autoScrollerFactory } from './autoscrollers';
@@ -1,3 +1,4 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
1
2
  import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
3
  import _createClass from "@babel/runtime/helpers/createClass";
3
4
  import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
@@ -20,10 +21,9 @@ import { browser, isValidPosition } from '@atlaskit/editor-common/utils';
20
21
  import { MAX_BROWSER_SCROLLBAR_HEIGHT, akEditorTableToolbarSize as tableToolbarSize } from '@atlaskit/editor-shared-styles';
21
22
  import { findTable, isTableSelected } from '@atlaskit/editor-tables/utils';
22
23
  import { getBooleanFF } from '@atlaskit/platform-feature-flags';
23
- import { autoScrollForElements, autoScrollWindowForElements } from '@atlaskit/pragmatic-drag-and-drop-auto-scroll/element';
24
- import { unsafeOverflowAutoScrollForElements } from '@atlaskit/pragmatic-drag-and-drop-auto-scroll/unsafe-overflow/element';
25
24
  import { combine } from '@atlaskit/pragmatic-drag-and-drop/util/combine';
26
25
  import { autoSizeTable, clearHoverSelection } from '../commands';
26
+ import { autoScrollerFactory } from '../pm-plugins/drag-and-drop/utils';
27
27
  import { getPluginState } from '../pm-plugins/plugin-factory';
28
28
  import { findStickyHeaderForTable, pluginKey as stickyHeadersPluginKey } from '../pm-plugins/sticky-headers';
29
29
  import { META_KEYS } from '../pm-plugins/table-analytics';
@@ -31,7 +31,7 @@ import { getLayoutSize, insertColgroupFromNode as recreateResizeColsByNode, scal
31
31
  import { hasTableBeenResized } from '../pm-plugins/table-resizing/utils/colgroup';
32
32
  import { updateControls } from '../pm-plugins/table-resizing/utils/dom';
33
33
  import { TableCssClassName as ClassName, ShadowEvent } from '../types';
34
- import { dropTargetExtendedWidth, tableOverflowShadowWidth, tableOverflowShadowWidthWide } from '../ui/consts';
34
+ import { tableOverflowShadowWidth, tableOverflowShadowWidthWide } from '../ui/consts';
35
35
  import TableFloatingColumnControls from '../ui/TableFloatingColumnControls';
36
36
  import TableFloatingControls from '../ui/TableFloatingControls';
37
37
  import { containsHeaderRow, isTableNested, tablesHaveDifferentColumnWidths, tablesHaveDifferentNoOfColumns } from '../utils';
@@ -320,55 +320,10 @@ var TableComponent = /*#__PURE__*/function (_React$Component) {
320
320
  }
321
321
  }
322
322
  if (isDragAndDropEnabled) {
323
- this.dragAndDropCleanupFn = combine(autoScrollForElements({
324
- element: this.wrapper,
325
- canScroll: function canScroll(_ref) {
326
- var source = _ref.source;
327
- var _ref2 = source.data,
328
- localId = _ref2.localId,
329
- type = _ref2.type;
330
- var node = getNode();
331
- return localId === (node === null || node === void 0 ? void 0 : node.attrs.localId) && type === 'table-column';
332
- }
333
- }), autoScrollWindowForElements({
334
- canScroll: function canScroll(_ref3) {
335
- var source = _ref3.source;
336
- var _ref4 = source.data,
337
- localId = _ref4.localId,
338
- type = _ref4.type;
339
- var node = getNode();
340
- return localId === (node === null || node === void 0 ? void 0 : node.attrs.localId) && type === 'table-row';
341
- }
342
- }), unsafeOverflowAutoScrollForElements({
343
- element: this.wrapper,
344
- canScroll: function canScroll(_ref5) {
345
- var source = _ref5.source;
346
- var _ref6 = source.data,
347
- localId = _ref6.localId,
348
- type = _ref6.type;
349
- var node = getNode();
350
- return localId === (node === null || node === void 0 ? void 0 : node.attrs.localId) && type === 'table-column';
351
- },
352
- getOverflow: function getOverflow() {
353
- return {
354
- fromTopEdge: {
355
- top: dropTargetExtendedWidth,
356
- right: dropTargetExtendedWidth,
357
- left: dropTargetExtendedWidth
358
- },
359
- fromRightEdge: {
360
- right: dropTargetExtendedWidth,
361
- top: dropTargetExtendedWidth,
362
- bottom: dropTargetExtendedWidth
363
- },
364
- fromLeftEdge: {
365
- top: dropTargetExtendedWidth,
366
- left: dropTargetExtendedWidth,
367
- bottom: dropTargetExtendedWidth
368
- }
369
- };
370
- }
371
- }));
323
+ this.dragAndDropCleanupFn = combine.apply(void 0, _toConsumableArray(autoScrollerFactory({
324
+ tableWrapper: this.wrapper,
325
+ getNode: getNode
326
+ })));
372
327
  }
373
328
  }
374
329
  if (allowColumnResizing) {