@gravity-ui/dashkit 8.8.1 → 8.9.1-beta.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 (29) hide show
  1. package/build/cjs/components/DashKit/DashKit.d.ts +5 -0
  2. package/build/cjs/components/GridItem/GridItem.css +1 -0
  3. package/build/cjs/components/GridItem/GridItem.js +30 -2
  4. package/build/cjs/components/GridLayout/GridLayout.d.ts +8 -2
  5. package/build/cjs/components/GridLayout/GridLayout.js +113 -25
  6. package/build/cjs/components/GridLayout/ReactGridLayout.js +142 -24
  7. package/build/cjs/components/Item/Item.d.ts +2 -0
  8. package/build/cjs/components/Item/Item.js +13 -2
  9. package/build/cjs/hocs/prepareItem.d.ts +2 -0
  10. package/build/cjs/hocs/prepareItem.js +6 -3
  11. package/build/cjs/hocs/withContext.js +28 -6
  12. package/build/cjs/plugins/Text/Text.d.ts +1 -0
  13. package/build/cjs/plugins/Text/Text.js +2 -0
  14. package/build/cjs/typings/plugin.d.ts +1 -0
  15. package/build/esm/components/DashKit/DashKit.d.ts +5 -0
  16. package/build/esm/components/GridItem/GridItem.css +1 -0
  17. package/build/esm/components/GridItem/GridItem.js +30 -2
  18. package/build/esm/components/GridLayout/GridLayout.d.ts +8 -2
  19. package/build/esm/components/GridLayout/GridLayout.js +113 -25
  20. package/build/esm/components/GridLayout/ReactGridLayout.js +143 -25
  21. package/build/esm/components/Item/Item.d.ts +2 -0
  22. package/build/esm/components/Item/Item.js +13 -2
  23. package/build/esm/hocs/prepareItem.d.ts +2 -0
  24. package/build/esm/hocs/prepareItem.js +6 -3
  25. package/build/esm/hocs/withContext.js +28 -6
  26. package/build/esm/plugins/Text/Text.d.ts +1 -0
  27. package/build/esm/plugins/Text/Text.js +2 -0
  28. package/build/esm/typings/plugin.d.ts +1 -0
  29. package/package.json +1 -1
@@ -18,6 +18,11 @@ interface DashKitDefaultProps {
18
18
  groups?: DashKitGroup[];
19
19
  }) => void;
20
20
  onDrop: (dropProps: ItemDropProps) => void;
21
+ onItemMountChange?: (item: ConfigItem, state: {
22
+ isAsync: boolean;
23
+ isMounted: boolean;
24
+ }) => void;
25
+ onItemRender?: (item: ConfigItem) => void;
21
26
  defaultGlobalParams: GlobalParams;
22
27
  globalParams: GlobalParams;
23
28
  itemsStateAndParams: ItemsStateAndParams;
@@ -57,6 +57,7 @@
57
57
  transition: none;
58
58
  z-index: 3;
59
59
  will-change: transform;
60
+ pointer-events: none;
60
61
  }
61
62
 
62
63
  .react-grid-item.dashkit-grid-item_is-focused {
@@ -38,6 +38,7 @@ const windowFocusObserver = new WindowFocusObserver();
38
38
  class GridItem extends react_1.default.PureComponent {
39
39
  constructor() {
40
40
  super(...arguments);
41
+ this._isAsyncItem = false;
41
42
  this.state = {
42
43
  isFocused: false,
43
44
  };
@@ -56,6 +57,33 @@ class GridItem extends react_1.default.PureComponent {
56
57
  this.controller = null;
57
58
  });
58
59
  };
60
+ this.onMountChange = (isMounted) => {
61
+ var _a, _b, _c, _d, _e, _f;
62
+ if (isMounted) {
63
+ this._inited = true;
64
+ (_b = (_a = this.props).onItemMountChange) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.item, {
65
+ isAsync: this._isAsyncItem,
66
+ isMounted: isMounted,
67
+ });
68
+ if (!this._isAsyncItem) {
69
+ (_d = (_c = this.props).onItemRender) === null || _d === void 0 ? void 0 : _d.call(_c, this.props.item);
70
+ }
71
+ }
72
+ else {
73
+ (_f = (_e = this.props).onItemMountChange) === null || _f === void 0 ? void 0 : _f.call(_e, this.props.item, {
74
+ isAsync: this._isAsyncItem,
75
+ isMounted: isMounted,
76
+ });
77
+ }
78
+ };
79
+ this.onBeforeLoad = () => {
80
+ this._isAsyncItem = true;
81
+ return this.onLoad;
82
+ };
83
+ this.onLoad = () => {
84
+ var _a, _b;
85
+ (_b = (_a = this.props).onItemRender) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.item);
86
+ };
59
87
  }
60
88
  renderOverlay() {
61
89
  const { isPlaceholder } = this.props;
@@ -71,7 +99,7 @@ class GridItem extends react_1.default.PureComponent {
71
99
  render() {
72
100
  // из-за бага, что Grid Items unmounts при изменении static, isDraggable, isResaizable
73
101
  // https://github.com/STRML/react-grid-layout/issues/721
74
- const { style, onMouseDown, onMouseUp, onTouchEnd, onTouchStart, children, className, isDragging, noOverlay, focusable, withCustomHandle, isPlaceholder, } = this.props;
102
+ const { style, onMouseDown, onMouseUp, onTouchEnd, onTouchStart, children, className, isDragging, noOverlay, focusable, withCustomHandle, isPlaceholder = false, } = this.props;
75
103
  const { editMode } = this.context;
76
104
  const width = Number.parseInt(style.width, 10);
77
105
  const height = Number.parseInt(style.height, 10);
@@ -96,7 +124,7 @@ class GridItem extends react_1.default.PureComponent {
96
124
  }
97
125
  : {})),
98
126
  react_1.default.createElement("div", { className: b('item', { editMode: editMode && !_editActive && !noOverlay }) },
99
- react_1.default.createElement(Item_1.default, { id: this.props.id, item: this.props.item, shouldItemUpdate: !isDragging, width: width, height: height, transform: transform, isPlaceholder: isPlaceholder, adjustWidgetLayout: this.props.adjustWidgetLayout, layout: this.props.layout, forwardedPluginRef: this.props.forwardedPluginRef })),
127
+ react_1.default.createElement(Item_1.default, { id: this.props.id, item: this.props.item, shouldItemUpdate: !isDragging, width: width, height: height, transform: transform, isPlaceholder: isPlaceholder, adjustWidgetLayout: this.props.adjustWidgetLayout, layout: this.props.layout, forwardedPluginRef: this.props.forwardedPluginRef, onMountChange: this.onMountChange, onBeforeLoad: this.onBeforeLoad })),
100
128
  !noOverlay && this.renderOverlay(),
101
129
  preparedChildren));
102
130
  }
@@ -5,6 +5,8 @@ export default class GridLayout extends React.PureComponent<any, any, any> {
5
5
  state: {
6
6
  isDragging: boolean;
7
7
  isPageHidden: boolean;
8
+ currentDraggingElement: null;
9
+ draggedOverGroup: null;
8
10
  };
9
11
  componentDidMount(): void;
10
12
  componentDidUpdate(): void;
@@ -36,10 +38,14 @@ export default class GridLayout extends React.PureComponent<any, any, any> {
36
38
  getMemoForwardRefCallback: (refIndex: any) => any;
37
39
  mergeGroupsLayout(group: any, newLayout: any, temporaryItem: any): any;
38
40
  reloadItems(): void;
39
- _onStart: () => void;
41
+ _onDragStart: (group: any, _newLayout: any, layoutItem: any) => void;
42
+ _onResizeStart: () => void;
43
+ _onTargetRestore: () => void;
40
44
  _onStop: (group: any, newLayout: any) => void;
41
- _onDropDragOver: (group: any, e: any) => any;
45
+ _onSharedDrop: (targetGroup: any, newLayout: any, tempItem: any) => void;
46
+ _onExternalDrop: (group: any, newLayout: any, item: any, e: any) => void;
42
47
  _onDrop: (group: any, newLayout: any, item: any, e: any) => false | undefined;
48
+ _onDropDragOver: (group: any, e: any) => any;
43
49
  renderTemporaryPlaceholder(): JSX.Element | null;
44
50
  renderGroup(group: any, renderLayout: any, renderItems: any, offset: number | undefined, groupGridProperties: any): JSX.Element;
45
51
  render(): any;
@@ -56,17 +56,18 @@ class GridLayout extends react_1.default.PureComponent {
56
56
  };
57
57
  this.getMemoGroupCallbacks = (group) => {
58
58
  if (!this._memoCallbacksForGroups[group]) {
59
- const onStart = this._onStart;
59
+ const onDragStart = this._onDragStart.bind(this, group);
60
60
  const onStop = this._onStop.bind(this, group);
61
61
  const onDrop = this._onDrop.bind(this, group);
62
62
  const onDropDragOver = this._onDropDragOver.bind(this, group);
63
+ const onDragTargetRestore = this._onTargetRestore.bind(this, group);
63
64
  this._memoCallbacksForGroups[group] = {
64
- onDragStart: onStart,
65
- onResizeStart: onStart,
65
+ onDragStart,
66
66
  onDragStop: onStop,
67
67
  onResizeStop: onStop,
68
- onDrop: onDrop,
68
+ onDrop,
69
69
  onDropDragOver,
70
+ onDragTargetRestore,
70
71
  };
71
72
  }
72
73
  return this._memoCallbacksForGroups[group];
@@ -93,48 +94,131 @@ class GridLayout extends react_1.default.PureComponent {
93
94
  }
94
95
  return this._memoForwardedPluginRef[refIndex];
95
96
  };
96
- this._onStart = () => {
97
+ this._onDragStart = (group, _newLayout, layoutItem) => {
97
98
  if (this.temporaryLayout)
98
99
  return;
99
- this.setState({ isDragging: true });
100
+ if (this.context.dragOverPlugin) {
101
+ this.setState({ isDragging: true });
102
+ }
103
+ else {
104
+ let currentDraggingElement = this.state.currentDraggingElement;
105
+ if (!currentDraggingElement) {
106
+ const _id = layoutItem.i;
107
+ const item = this.context.config.items.find(({ id }) => id === _id);
108
+ currentDraggingElement = [group, layoutItem, item];
109
+ }
110
+ this.setState({
111
+ isDragging: true,
112
+ currentDraggingElement,
113
+ draggedOverGroup: group,
114
+ });
115
+ }
116
+ };
117
+ this._onResizeStart = () => {
118
+ this.setState({
119
+ isDragging: true,
120
+ });
121
+ };
122
+ this._onTargetRestore = () => {
123
+ const { currentDraggingElement } = this.state;
124
+ if (currentDraggingElement) {
125
+ this.setState({
126
+ draggedOverGroup: currentDraggingElement[0],
127
+ });
128
+ }
100
129
  };
101
130
  this._onStop = (group, newLayout) => {
102
131
  const { layoutChange, onDrop, temporaryLayout } = this.context;
132
+ const { draggedOverGroup, currentDraggingElement } = this.state;
133
+ if (currentDraggingElement &&
134
+ draggedOverGroup !== null &&
135
+ draggedOverGroup !== currentDraggingElement[0]) {
136
+ // Skipping layout update when change event called for source grid
137
+ // and waiting _onDrop
138
+ return;
139
+ }
103
140
  const groupedLayout = this.mergeGroupsLayout(group, newLayout);
141
+ this.setState({
142
+ isDragging: false,
143
+ currentDraggingElement: null,
144
+ draggedOverGroup: null,
145
+ });
104
146
  if (temporaryLayout) {
105
147
  onDrop === null || onDrop === void 0 ? void 0 : onDrop(groupedLayout, groupedLayout.find(({ i }) => i === constants_1.TEMPORARY_ITEM_ID));
106
148
  }
107
149
  else {
108
150
  layoutChange(groupedLayout);
109
151
  }
110
- this.setState({ isDragging: false });
111
152
  };
112
- this._onDropDragOver = (group, e) => {
113
- const { editMode, dragOverPlugin } = this.context;
114
- if (!editMode || !dragOverPlugin) {
115
- return false;
153
+ this._onSharedDrop = (targetGroup, newLayout, tempItem) => {
154
+ const { currentDraggingElement } = this.state;
155
+ const { layoutChange } = this.context;
156
+ if (!currentDraggingElement) {
157
+ return;
116
158
  }
117
- const { properties, layout } = this.getLayoutAndPropsByGroup(group);
118
- return this.context.onDropDragOver(e, properties, layout);
159
+ const [, sourceItem] = currentDraggingElement;
160
+ const groupedLayout = this.mergeGroupsLayout(targetGroup, newLayout, tempItem).map((item) => {
161
+ if (item.i === sourceItem.i) {
162
+ const copy = Object.assign({}, tempItem);
163
+ delete copy.parent;
164
+ if (targetGroup !== constants_1.DEFAULT_GROUP) {
165
+ copy.parent = targetGroup;
166
+ }
167
+ copy.i = sourceItem.i;
168
+ return copy;
169
+ }
170
+ return item;
171
+ });
172
+ this.setState({
173
+ isDragging: false,
174
+ currentDraggingElement: null,
175
+ draggedOverGroup: null,
176
+ });
177
+ layoutChange(groupedLayout);
178
+ };
179
+ this._onExternalDrop = (group, newLayout, item, e) => {
180
+ const { onDrop } = this.context;
181
+ if (group !== constants_1.DEFAULT_GROUP) {
182
+ item.parent = group;
183
+ }
184
+ const groupedLayout = this.mergeGroupsLayout(group, newLayout, item);
185
+ this.setState({ isDragging: false });
186
+ onDrop === null || onDrop === void 0 ? void 0 : onDrop(groupedLayout, item, e);
119
187
  };
120
188
  this._onDrop = (group, newLayout, item, e) => {
121
- if (!item) {
189
+ if (!item || !this.context.editMode) {
122
190
  return false;
123
191
  }
124
- const { editMode, onDrop } = this.context;
125
- if (!editMode) {
192
+ const { draggedOverGroup, currentDraggingElement } = this.state;
193
+ if (currentDraggingElement && draggedOverGroup === group) {
194
+ this._onSharedDrop(group, newLayout, item);
195
+ }
196
+ else {
197
+ this._onExternalDrop(group, newLayout, item, e);
198
+ }
199
+ };
200
+ this._onDropDragOver = (group, e) => {
201
+ const { editMode, dragOverPlugin, onDropDragOver } = this.context;
202
+ const { currentDraggingElement } = this.state;
203
+ if (!editMode || (!dragOverPlugin && !currentDraggingElement)) {
126
204
  return false;
127
205
  }
128
- if (group !== constants_1.DEFAULT_GROUP) {
129
- item.parent = group;
206
+ const { properties, layout } = this.getLayoutAndPropsByGroup(group);
207
+ if (currentDraggingElement) {
208
+ const [, { h, w, i }, { type }] = currentDraggingElement;
209
+ return onDropDragOver(e, properties, layout, { h, w, i, type });
130
210
  }
131
- const groupedLayout = this.mergeGroupsLayout(group, newLayout, item);
132
- onDrop === null || onDrop === void 0 ? void 0 : onDrop(groupedLayout, item, e);
211
+ if (dragOverPlugin) {
212
+ return onDropDragOver(e, properties, layout);
213
+ }
214
+ return false;
133
215
  };
134
216
  this.pluginsRefs = [];
135
217
  this.state = {
136
218
  isDragging: false,
137
219
  isPageHidden: false,
220
+ currentDraggingElement: null,
221
+ draggedOverGroup: null,
138
222
  };
139
223
  }
140
224
  componentDidMount() {
@@ -203,7 +287,8 @@ class GridLayout extends react_1.default.PureComponent {
203
287
  return (react_1.default.createElement(GridItem_1.default, { key: id, id: id, item: { id, type, data: {} }, layout: temporaryLayout.data, adjustWidgetLayout: this.adjustWidgetLayout, isDragging: this.state.isDragging, isPlaceholder: true, noOverlay: noOverlay, withCustomHandle: Boolean(draggableHandleClassName) }));
204
288
  }
205
289
  renderGroup(group, renderLayout, renderItems, offset = 0, groupGridProperties) {
206
- const { registerManager, editMode, noOverlay, focusable, draggableHandleClassName, outerDnDEnable, } = this.context;
290
+ const { registerManager, editMode, noOverlay, focusable, draggableHandleClassName, outerDnDEnable, onItemMountChange, onItemRender, } = this.context;
291
+ const { currentDraggingElement, draggedOverGroup } = this.state;
207
292
  const properties = groupGridProperties
208
293
  ? groupGridProperties(Object.assign({}, registerManager.gridLayout))
209
294
  : registerManager.gridLayout;
@@ -212,17 +297,20 @@ class GridLayout extends react_1.default.PureComponent {
212
297
  compactType = 'horizontal';
213
298
  }
214
299
  const { callbacks, layout } = this.getMemoGroupProps(group, renderLayout, properties);
215
- return (react_1.default.createElement(ReactGridLayout_1.Layout, Object.assign({}, properties, { compactType: compactType, layout: layout, key: `group_${group}`, isDraggable: editMode, isResizable: editMode, onDragStart: callbacks.onDragStart, onDragStop: callbacks.onDragStop, onResizeStart: callbacks.onResizeStart, onResizeStop: callbacks.onResizeStop }, (draggableHandleClassName
300
+ const hasSharedDragItem = Boolean(currentDraggingElement && currentDraggingElement[0] !== group);
301
+ const isDragCaptured = currentDraggingElement &&
302
+ group === currentDraggingElement[0] &&
303
+ draggedOverGroup !== null &&
304
+ draggedOverGroup !== group;
305
+ return (react_1.default.createElement(ReactGridLayout_1.Layout, Object.assign({}, properties, { compactType: compactType, layout: layout, key: `group_${group}`, isDraggable: editMode, isResizable: editMode, onResizeStart: this._onResizeStart, onDragStart: callbacks.onDragStart, onDragStop: callbacks.onDragStop, onResizeStop: callbacks.onResizeStop, onDragTargetRestore: callbacks.onDragTargetRestore, onDropDragOver: callbacks.onDropDragOver, onDrop: callbacks.onDrop, hasSharedDragItem: hasSharedDragItem, isDragCaptured: isDragCaptured }, (draggableHandleClassName
216
306
  ? { draggableHandle: `.${draggableHandleClassName}` }
217
307
  : null), (outerDnDEnable
218
308
  ? {
219
309
  isDroppable: true,
220
- onDropDragOver: callbacks.onDropDragOver,
221
- onDrop: callbacks.onDrop,
222
310
  }
223
311
  : null), { draggableCancel: `.${constants_1.OVERLAY_CONTROLS_CLASS_NAME}` }),
224
312
  renderItems.map((item, i) => {
225
- return (react_1.default.createElement(GridItem_1.default, { forwardedPluginRef: this.getMemoForwardRefCallback(offset + i), key: item.id, id: item.id, item: item, layout: layout, adjustWidgetLayout: this.adjustWidgetLayout, isDragging: this.state.isDragging, noOverlay: noOverlay, focusable: focusable, withCustomHandle: Boolean(draggableHandleClassName) }));
313
+ return (react_1.default.createElement(GridItem_1.default, { forwardedPluginRef: this.getMemoForwardRefCallback(offset + i), key: item.id, id: item.id, item: item, layout: layout, adjustWidgetLayout: this.adjustWidgetLayout, isDragging: this.state.isDragging, noOverlay: noOverlay, focusable: focusable, withCustomHandle: Boolean(draggableHandleClassName), onItemMountChange: onItemMountChange, onItemRender: onItemRender }));
226
314
  }),
227
315
  this.renderTemporaryPlaceholder()));
228
316
  }
@@ -8,59 +8,177 @@ const constants_1 = require("../../constants");
8
8
  class DragOverLayout extends react_grid_layout_1.default {
9
9
  constructor(...args) {
10
10
  super(...args);
11
- this.dragReset = () => {
11
+ this._savedDraggedOutLayout = null;
12
+ // Reset placeholder when item dragged from outside
13
+ this.resetExternalPlaceholder = () => {
12
14
  if (this.dragEnterCounter) {
13
15
  this.dragEnterCounter = 0;
14
16
  this.removeDroppingPlaceholder();
15
17
  }
16
18
  };
19
+ // Hide placeholder when element is dragged out
20
+ this.hideLocalPlaceholder = (i) => {
21
+ const { layout } = this.state;
22
+ const { cols } = this.props;
23
+ const savedLayout = layout.map((item) => (Object.assign({}, item)));
24
+ let hiddenElement;
25
+ const newLayout = react_grid_layout_1.utils.compact(layout.filter((item) => {
26
+ if (item.i === i) {
27
+ hiddenElement = item;
28
+ return false;
29
+ }
30
+ return true;
31
+ }), react_grid_layout_1.utils.compactType(this.props), cols);
32
+ if (hiddenElement) {
33
+ newLayout.push(hiddenElement);
34
+ }
35
+ this.setState({
36
+ activeDrag: null,
37
+ layout: newLayout,
38
+ });
39
+ return savedLayout;
40
+ };
41
+ this.extendedOnDrag = (i, x, y, sintEv) => {
42
+ if (this.props.isDragCaptured) {
43
+ if (!this._savedDraggedOutLayout) {
44
+ this._savedDraggedOutLayout = this.hideLocalPlaceholder(i);
45
+ }
46
+ return;
47
+ }
48
+ this._savedDraggedOutLayout = null;
49
+ // parent onDrag will show new placeholder again
50
+ this.parentOnDrag(i, x, y, sintEv);
51
+ };
52
+ this.extendedOnDragStop = (i, x, y, sintEv) => {
53
+ // Restoring layout if item was dropped outside of the grid
54
+ if (this._savedDraggedOutLayout) {
55
+ const savedLayout = this._savedDraggedOutLayout;
56
+ const l = react_grid_layout_1.utils.getLayoutItem(savedLayout, i);
57
+ // Create placeholder (display only)
58
+ const placeholder = {
59
+ w: l.w,
60
+ h: l.h,
61
+ x: l.x,
62
+ y: l.y,
63
+ placeholder: true,
64
+ i: i,
65
+ };
66
+ this.setState({
67
+ layout: savedLayout,
68
+ activeDrag: placeholder,
69
+ }, () => {
70
+ this.parentOnDragStop(i, x, y, sintEv);
71
+ });
72
+ this._savedDraggedOutLayout = null;
73
+ }
74
+ else {
75
+ this.parentOnDragStop(i, x, y, sintEv);
76
+ }
77
+ };
78
+ // Proxy mouse events -> drag methods for dnd between groups
79
+ this.mouseEnterHandler = (e) => {
80
+ var _a, _b;
81
+ if (this.props.hasSharedDragItem) {
82
+ this.onDragEnter(e);
83
+ }
84
+ else if (this.props.isDragCaptured) {
85
+ (_b = (_a = this.props).onDragTargetRestore) === null || _b === void 0 ? void 0 : _b.call(_a);
86
+ }
87
+ };
17
88
  this.mouseLeaveHandler = (e) => {
18
- var _a;
19
- const rect = ((_a = this.props.innerRef) === null || _a === void 0 ? void 0 : _a.current.getBoundingClientRect()) || {};
20
- if (rect.bottom <= e.clientY ||
21
- rect.top >= e.clientY ||
22
- rect.left >= e.clientX ||
23
- rect.right <= e.clientX) {
24
- this.dragReset();
89
+ var _a, _b;
90
+ if (this.props.hasSharedDragItem) {
91
+ this.onDragLeave(e);
92
+ (_b = (_a = this.props).onDragTargetRestore) === null || _b === void 0 ? void 0 : _b.call(_a);
25
93
  }
26
94
  };
27
- this.parentOnDrag = this.onDrag;
28
- this.onDrag = (i, x, y, sintE) => {
29
- this.parentOnDrag(i, x, y, sintE);
30
- this.mouseLeaveHandler(sintE.e);
95
+ this.mouseMoveHandler = (e) => {
96
+ if (this.props.hasSharedDragItem) {
97
+ this.onDragOver(e);
98
+ }
31
99
  };
100
+ this.mouseUpHandler = (e) => {
101
+ var _a, _b;
102
+ if (this.props.hasSharedDragItem) {
103
+ e.preventDefault();
104
+ const { droppingItem } = this.props;
105
+ const { layout } = this.state;
106
+ const item = layout.find((l) => l.i === droppingItem.i);
107
+ // reset dragEnter counter on drop
108
+ this.resetExternalPlaceholder();
109
+ (_b = (_a = this.props).onDrop) === null || _b === void 0 ? void 0 : _b.call(_a, layout, item, e);
110
+ }
111
+ };
112
+ this.parentOnDrag = this.onDrag;
113
+ this.onDrag = this.extendedOnDrag;
114
+ this.parentOnDragStop = this.onDragStop;
115
+ this.onDragStop = this.extendedOnDragStop;
32
116
  }
33
117
  componentDidMount() {
34
118
  var _a;
35
119
  (_a = super.componentDidMount) === null || _a === void 0 ? void 0 : _a.call(this);
36
- window.addEventListener('dragend', this.dragReset);
120
+ // If cursor is moved out of the window there is a bug
121
+ // which leaves placeholder element in grid, this action needed to reset this state
122
+ window.addEventListener('dragend', this.resetExternalPlaceholder);
123
+ const innerElement = this.getInnerElement();
124
+ if (innerElement) {
125
+ innerElement.addEventListener('mouseup', this.mouseUpHandler);
126
+ innerElement.addEventListener('mouseenter', this.mouseEnterHandler);
127
+ innerElement.addEventListener('mouseleave', this.mouseLeaveHandler);
128
+ innerElement.addEventListener('mousemove', this.mouseMoveHandler);
129
+ }
37
130
  }
38
131
  componentWillUnmount() {
39
- window.removeEventListener('dragend', this.dragReset);
132
+ window.removeEventListener('dragend', this.resetExternalPlaceholder);
133
+ const innerElement = this.getInnerElement();
134
+ if (innerElement) {
135
+ innerElement.removeEventListener('mouseup', this.mouseUpHandler);
136
+ innerElement.removeEventListener('mouseenter', this.mouseEnterHandler);
137
+ innerElement.removeEventListener('mouseleave', this.mouseLeaveHandler);
138
+ innerElement.removeEventListener('mousemove', this.mouseMoveHandler);
139
+ }
40
140
  }
141
+ // react-grid-layout doens't calculate it's height when last element is removed
142
+ // and just keeps the previous value
143
+ // so for autosize to work in that case we are resetting it's height value
41
144
  containerHeight() {
42
145
  if (this.props.autoSize && this.state.layout.length === 0) {
43
- return 'unset';
146
+ return;
44
147
  }
148
+ // eslint-disable-next-line consistent-return
45
149
  return super.containerHeight();
46
150
  }
151
+ // innerRef is passed by WithProvider without this wrapper there are only
152
+ // * findDOMNode - deprecated
153
+ // * rewrite whole ReactGridLayout.render method
154
+ // so in that case don't try to use this class on it's own
155
+ // or pass innerRef: React.MutableRef as it's not optional prop
156
+ getInnerElement() {
157
+ var _a;
158
+ return ((_a = this.props.innerRef) === null || _a === void 0 ? void 0 : _a.current) || null;
159
+ }
160
+ // Drop item from outside gets 0,0 droppingPosition
161
+ // centering cursor on newly creted grid item
162
+ // And cause grid-layout using it's own GridItem to make it look
163
+ // like overlay adding className
47
164
  processGridItem(child, isDroppingItem) {
48
165
  var _a;
49
166
  const gridItem = (_a = super.processGridItem) === null || _a === void 0 ? void 0 : _a.call(this, child, isDroppingItem);
167
+ if (!gridItem) {
168
+ return gridItem;
169
+ }
170
+ const { props: itemProps } = gridItem;
50
171
  if (isDroppingItem) {
51
- // Drop item from outside gets 0,0 droppingPosition
52
- // centering cursor on newly creted grid item
53
- // And cause grid-layout using it's own GridItem to make it look
54
- // like overlay adding className
55
- if (!gridItem)
56
- return null;
57
- const { props } = gridItem;
58
- const { containerWidth, cols, w, h, rowHeight, margin, transformScale } = props;
172
+ const { containerWidth, cols, w, h, rowHeight, margin, transformScale, droppingPosition, } = itemProps;
59
173
  const leftOffset = (((containerWidth / cols) * w) / 2 || 0) * transformScale;
60
174
  const topOffset = ((h * rowHeight + (h - 1) * margin[1]) / 2 || 0) * transformScale;
175
+ const style = gridItem.props.style || null;
176
+ // React.cloneElement is just cleaner then copy-paste whole processGridItem method
61
177
  return react_1.default.cloneElement(gridItem, {
178
+ // hiding previre if dragging shared item
179
+ style: this.props.hasSharedDragItem ? Object.assign(Object.assign({}, style), { opacity: 0 }) : style,
62
180
  className: constants_1.OVERLAY_CLASS_NAME,
63
- droppingPosition: Object.assign(Object.assign({}, props.droppingPosition), { left: props.droppingPosition.left - leftOffset, top: props.droppingPosition.top - topOffset }),
181
+ droppingPosition: Object.assign(Object.assign({}, droppingPosition), { left: droppingPosition.left - leftOffset, top: droppingPosition.top - topOffset }),
64
182
  });
65
183
  }
66
184
  return gridItem;
@@ -61,7 +61,9 @@ declare const _default: {
61
61
  height: PropTypes.Requireable<number>;
62
62
  transform: PropTypes.Requireable<string>;
63
63
  isPlaceholder: PropTypes.Requireable<boolean>;
64
+ onBeforeLoad: PropTypes.Requireable<(...args: any[]) => any>;
64
65
  forwardedPluginRef: PropTypes.Requireable<any>;
66
+ onMountChange: PropTypes.Requireable<(...args: any[]) => any>;
65
67
  };
66
68
  contextType: React.Context<any>;
67
69
  };
@@ -7,9 +7,18 @@ const prepareItem_1 = require("../../hocs/prepareItem");
7
7
  const cn_1 = require("../../utils/cn");
8
8
  const b = (0, cn_1.cn)('dashkit-item');
9
9
  // TODO: getDerivedStateFromError и заглушка с ошибкой
10
- const Item = ({ registerManager, rendererProps, type, isPlaceholder, forwardedPluginRef }) => {
10
+ const Item = ({ registerManager, rendererProps, type, isPlaceholder, forwardedPluginRef, onMountChange, }) => {
11
11
  var _a, _b;
12
- if (!registerManager.check(type)) {
12
+ const isRegisteredType = registerManager.check(type);
13
+ react_1.default.useLayoutEffect(() => {
14
+ if (isRegisteredType && !isPlaceholder) {
15
+ onMountChange === null || onMountChange === void 0 ? void 0 : onMountChange(true);
16
+ return () => {
17
+ onMountChange === null || onMountChange === void 0 ? void 0 : onMountChange(false);
18
+ };
19
+ }
20
+ }, []);
21
+ if (!isRegisteredType) {
13
22
  console.warn(`type [${type}] не зарегистрирован`);
14
23
  return null;
15
24
  }
@@ -25,5 +34,7 @@ Item.propTypes = {
25
34
  registerManager: prop_types_1.default.object,
26
35
  type: prop_types_1.default.string,
27
36
  isPlaceholder: prop_types_1.default.bool,
37
+ onMountChange: prop_types_1.default.func,
38
+ onBeforeLoad: prop_types_1.default.func,
28
39
  };
29
40
  exports.default = (0, prepareItem_1.prepareItem)(Item);
@@ -61,7 +61,9 @@ export function prepareItem(Component: any): {
61
61
  height: PropTypes.Requireable<number>;
62
62
  transform: PropTypes.Requireable<string>;
63
63
  isPlaceholder: PropTypes.Requireable<boolean>;
64
+ onBeforeLoad: PropTypes.Requireable<(...args: any[]) => any>;
64
65
  forwardedPluginRef: PropTypes.Requireable<any>;
66
+ onMountChange: PropTypes.Requireable<(...args: any[]) => any>;
65
67
  };
66
68
  contextType: React.Context<any>;
67
69
  };
@@ -15,7 +15,7 @@ function prepareItem(Component) {
15
15
  };
16
16
  this._currentRenderProps = {};
17
17
  this.getRenderProps = () => {
18
- const { id, width, height, item, adjustWidgetLayout, layout, isPlaceholder } = this.props;
18
+ const { id, width, height, item, adjustWidgetLayout, layout, isPlaceholder, onBeforeLoad, } = this.props;
19
19
  const { itemsState, itemsParams, registerManager, settings, context, editMode } = this.context;
20
20
  const { data, defaults, namespace } = item;
21
21
  const rendererProps = {
@@ -24,6 +24,7 @@ function prepareItem(Component) {
24
24
  params: itemsParams[id],
25
25
  state: itemsState[id],
26
26
  onStateAndParamsChange: this._onStateAndParamsChange,
27
+ onBeforeLoad,
27
28
  width,
28
29
  height,
29
30
  id,
@@ -55,10 +56,10 @@ function prepareItem(Component) {
55
56
  return true;
56
57
  }
57
58
  render() {
58
- const { item, isPlaceholder } = this.props;
59
+ const { item, isPlaceholder, forwardedPluginRef, onMountChange } = this.props;
59
60
  const { registerManager } = this.context;
60
61
  const { type } = item;
61
- return (react_1.default.createElement(Component, { forwardedPluginRef: this.props.forwardedPluginRef, rendererProps: this.getRenderProps(), registerManager: registerManager, type: type, isPlaceholder: isPlaceholder }));
62
+ return (react_1.default.createElement(Component, { forwardedPluginRef: forwardedPluginRef, rendererProps: this.getRenderProps(), registerManager: registerManager, type: type, isPlaceholder: isPlaceholder, onMountChange: onMountChange }));
62
63
  }
63
64
  },
64
65
  _a.propTypes = {
@@ -71,7 +72,9 @@ function prepareItem(Component) {
71
72
  height: prop_types_1.default.number,
72
73
  transform: prop_types_1.default.string,
73
74
  isPlaceholder: prop_types_1.default.bool,
75
+ onBeforeLoad: prop_types_1.default.func,
74
76
  forwardedPluginRef: prop_types_1.default.any,
77
+ onMountChange: prop_types_1.default.func,
75
78
  },
76
79
  _a.contextType = DashKitContext_1.DashKitContext,
77
80
  _a;