Skip to content

Commit e46df91

Browse files
authored
Merge pull request #1552 from adumesny/develop
fix resize delay causing potential error msg
2 parents 471036c + 2d47710 commit e46df91

File tree

2 files changed

+29
-48
lines changed

2 files changed

+29
-48
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Change log
4848
- fix [1535](https://github.com/gridstack/gridstack.js/issues/1535) use batchUpdate() around grid init to make sure gs-y attributes are respected.
4949
- fix [1540](https://github.com/gridstack/gridstack.js/issues/1540) Safari H5 drag&drop fixed
5050
- fix [1545](https://github.com/gridstack/gridstack.js/issues/1545) `disableMove()` correctly prevents drag later (remove events and draggable attribute)
51+
- fix [1546](https://github.com/gridstack/gridstack.js/issues/1546) resize no longer delayed, which caused race conditions errors
5152

5253
## 3.1.2 (2020-12-7)
5354

src/h5/dd-resizable-handle.ts

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ export class DDResizableHandle {
2020
private option: DDResizableHandleOpt;
2121
/** @internal */
2222
private dir: string;
23-
/** @internal */
24-
private mouseMoving = false;
25-
/** @internal */
26-
private started = false;
23+
/** @internal true after we've moved enough pixels to start a resize */
24+
private moving = false;
2725
/** @internal */
2826
private mouseDownEvent: MouseEvent;
2927
/** @internal */
@@ -38,10 +36,11 @@ export class DDResizableHandle {
3836
this._mouseMove = this._mouseMove.bind(this);
3937
this._mouseUp = this._mouseUp.bind(this);
4038

41-
this.init();
39+
this._init();
4240
}
4341

44-
public init(): DDResizableHandle {
42+
/** @internal */
43+
private _init(): DDResizableHandle {
4544
const el = document.createElement('div');
4645
el.classList.add('ui-resizable-handle');
4746
el.classList.add(`${DDResizableHandle.prefix}${this.dir}`);
@@ -53,68 +52,49 @@ export class DDResizableHandle {
5352
return this;
5453
}
5554

55+
/** call this when resize handle needs to be removed and cleaned up */
5656
public destroy(): DDResizableHandle {
57+
if (this.moving) this._mouseUp(this.mouseDownEvent);
58+
this.el.removeEventListener('mousedown', this._mouseDown);
5759
this.host.removeChild(this.el);
60+
delete this.el;
61+
delete this.host;
5862
return this;
5963
}
6064

61-
/** @internal */
62-
private _mouseDown(event: MouseEvent): void {
63-
this.mouseDownEvent = event;
64-
setTimeout(() => {
65-
document.addEventListener('mousemove', this._mouseMove, true);
66-
document.addEventListener('mouseup', this._mouseUp);
67-
setTimeout(() => {
68-
if (!this.mouseMoving) {
69-
document.removeEventListener('mousemove', this._mouseMove, true);
70-
document.removeEventListener('mouseup', this._mouseUp);
71-
delete this.mouseDownEvent;
72-
}
73-
}, 300);
74-
}, 100);
65+
/** @internal called on mouse down on us: capture move on the entire document (mouse might not stay on us) until we release the mouse */
66+
private _mouseDown(e: MouseEvent): void {
67+
this.mouseDownEvent = e;
68+
document.addEventListener('mousemove', this._mouseMove, true); // capture, not bubble
69+
document.addEventListener('mouseup', this._mouseUp);
7570
}
7671

7772
/** @internal */
78-
private _mouseMove(event: MouseEvent): void {
79-
if (!this.started && !this.mouseMoving) {
80-
if (this._hasMoved(event, this.mouseDownEvent)) {
81-
this.mouseMoving = true;
82-
this._triggerEvent('start', this.mouseDownEvent);
83-
this.started = true;
84-
}
85-
}
86-
if (this.started) {
87-
this._triggerEvent('move', event);
73+
private _mouseMove(e: MouseEvent): void {
74+
let s = this.mouseDownEvent;
75+
// don't start unless we've moved at least 3 pixels
76+
if (!this.moving && Math.abs(e.x - s.x) + Math.abs(e.y - s.y) > 2) {
77+
this.moving = true;
78+
this._triggerEvent('start', this.mouseDownEvent);
79+
} else if (this.moving) {
80+
this._triggerEvent('move', e);
8881
}
8982
}
9083

9184
/** @internal */
92-
private _mouseUp(event: MouseEvent): void {
93-
if (this.mouseMoving) {
94-
this._triggerEvent('stop', event);
85+
private _mouseUp(e: MouseEvent): void {
86+
if (this.moving) {
87+
this._triggerEvent('stop', e);
9588
}
9689
document.removeEventListener('mousemove', this._mouseMove, true);
9790
document.removeEventListener('mouseup', this._mouseUp);
98-
this.mouseMoving = false;
99-
this.started = false;
91+
delete this.moving;
10092
delete this.mouseDownEvent;
10193
}
10294

103-
/** @internal */
104-
private _hasMoved(event: MouseEvent, oEvent: MouseEvent): boolean {
105-
const { clientX, clientY } = event;
106-
const { clientX: oClientX, clientY: oClientY } = oEvent;
107-
return (
108-
Math.abs(clientX - oClientX) > 1
109-
|| Math.abs(clientY - oClientY) > 1
110-
);
111-
}
112-
11395
/** @internal */
11496
private _triggerEvent(name: string, event: MouseEvent): DDResizableHandle {
115-
if (this.option[name]) {
116-
this.option[name](event);
117-
}
97+
if (this.option[name]) this.option[name](event);
11898
return this;
11999
}
120100
}

0 commit comments

Comments
 (0)