Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix resize delay causing potential error msg #1552

Merged
merged 1 commit into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Change log
- fix [1535](https://github.com/gridstack/gridstack.js/issues/1535) use batchUpdate() around grid init to make sure gs-y attributes are respected.
- fix [1540](https://github.com/gridstack/gridstack.js/issues/1540) Safari H5 drag&drop fixed
- fix [1545](https://github.com/gridstack/gridstack.js/issues/1545) `disableMove()` correctly prevents drag later (remove events and draggable attribute)
- fix [1546](https://github.com/gridstack/gridstack.js/issues/1546) resize no longer delayed, which caused race conditions errors

## 3.1.2 (2020-12-7)

Expand Down
76 changes: 28 additions & 48 deletions src/h5/dd-resizable-handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ export class DDResizableHandle {
private option: DDResizableHandleOpt;
/** @internal */
private dir: string;
/** @internal */
private mouseMoving = false;
/** @internal */
private started = false;
/** @internal true after we've moved enough pixels to start a resize */
private moving = false;
/** @internal */
private mouseDownEvent: MouseEvent;
/** @internal */
Expand All @@ -38,10 +36,11 @@ export class DDResizableHandle {
this._mouseMove = this._mouseMove.bind(this);
this._mouseUp = this._mouseUp.bind(this);

this.init();
this._init();
}

public init(): DDResizableHandle {
/** @internal */
private _init(): DDResizableHandle {
const el = document.createElement('div');
el.classList.add('ui-resizable-handle');
el.classList.add(`${DDResizableHandle.prefix}${this.dir}`);
Expand All @@ -53,68 +52,49 @@ export class DDResizableHandle {
return this;
}

/** call this when resize handle needs to be removed and cleaned up */
public destroy(): DDResizableHandle {
if (this.moving) this._mouseUp(this.mouseDownEvent);
this.el.removeEventListener('mousedown', this._mouseDown);
this.host.removeChild(this.el);
delete this.el;
delete this.host;
return this;
}

/** @internal */
private _mouseDown(event: MouseEvent): void {
this.mouseDownEvent = event;
setTimeout(() => {
document.addEventListener('mousemove', this._mouseMove, true);
document.addEventListener('mouseup', this._mouseUp);
setTimeout(() => {
if (!this.mouseMoving) {
document.removeEventListener('mousemove', this._mouseMove, true);
document.removeEventListener('mouseup', this._mouseUp);
delete this.mouseDownEvent;
}
}, 300);
}, 100);
/** @internal called on mouse down on us: capture move on the entire document (mouse might not stay on us) until we release the mouse */
private _mouseDown(e: MouseEvent): void {
this.mouseDownEvent = e;
document.addEventListener('mousemove', this._mouseMove, true); // capture, not bubble
document.addEventListener('mouseup', this._mouseUp);
}

/** @internal */
private _mouseMove(event: MouseEvent): void {
if (!this.started && !this.mouseMoving) {
if (this._hasMoved(event, this.mouseDownEvent)) {
this.mouseMoving = true;
this._triggerEvent('start', this.mouseDownEvent);
this.started = true;
}
}
if (this.started) {
this._triggerEvent('move', event);
private _mouseMove(e: MouseEvent): void {
let s = this.mouseDownEvent;
// don't start unless we've moved at least 3 pixels
if (!this.moving && Math.abs(e.x - s.x) + Math.abs(e.y - s.y) > 2) {
this.moving = true;
this._triggerEvent('start', this.mouseDownEvent);
} else if (this.moving) {
this._triggerEvent('move', e);
}
}

/** @internal */
private _mouseUp(event: MouseEvent): void {
if (this.mouseMoving) {
this._triggerEvent('stop', event);
private _mouseUp(e: MouseEvent): void {
if (this.moving) {
this._triggerEvent('stop', e);
}
document.removeEventListener('mousemove', this._mouseMove, true);
document.removeEventListener('mouseup', this._mouseUp);
this.mouseMoving = false;
this.started = false;
delete this.moving;
delete this.mouseDownEvent;
}

/** @internal */
private _hasMoved(event: MouseEvent, oEvent: MouseEvent): boolean {
const { clientX, clientY } = event;
const { clientX: oClientX, clientY: oClientY } = oEvent;
return (
Math.abs(clientX - oClientX) > 1
|| Math.abs(clientY - oClientY) > 1
);
}

/** @internal */
private _triggerEvent(name: string, event: MouseEvent): DDResizableHandle {
if (this.option[name]) {
this.option[name](event);
}
if (this.option[name]) this.option[name](event);
return this;
}
}