Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4ea9474

Browse files
authored
Merge pull request #2908 from adumesny/master
'Esc' to cancel now works on sidebar
2 parents b533ef9 + 8db4c44 commit 4ea9474

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

doc/CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ Change log
120120
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
121121

122122
## 11.1.2-dev (TBD)
123-
* fix: [#2852](https://github.com/gridstack/gridstack.js/pull/2852) better React example. Thank you [CNine](https://github.com/Aysnine)
123+
* feat: [#2695](https://github.com/gridstack/gridstack.js/issues/2695) 'Esc' to cancel now works on sidebar external items, also works dragging over trash.
124+
* feat: [#2852](https://github.com/gridstack/gridstack.js/pull/2852) better React example. Thank you [CNine](https://github.com/Aysnine)
124125
* fix: [#2852](https://github.com/gridstack/gridstack.js/pull/2852) grid in tabs correctly handles CSS. Thank you [Luciano Martorella](https://github.com/lmartorella)
125126
* fix: [#2900](https://github.com/gridstack/gridstack.js/issues/2900) use attr `data-gs-widget` instead of `gridstacknode` (supported as well for backward compatibility)
126127

src/dd-draggable.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { DDBaseImplement, HTMLElementExtendOpt } from './dd-base-impl';
99
import { GridItemHTMLElement, DDUIData, GridStackNode, GridStackPosition, DDDragOpt } from './types';
1010
import { DDElementHost } from './dd-element';
1111
import { isTouch, touchend, touchmove, touchstart, pointerdown } from './dd-touch';
12+
import { GridHTMLElement } from './gridstack';
1213

1314
interface DragOffset {
1415
left: number;
@@ -264,17 +265,16 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
264265
/** @internal call when keys are being pressed - use Esc to cancel, R to rotate */
265266
protected _keyEvent(e: KeyboardEvent): void {
266267
const n = this.el.gridstackNode as GridStackNodeRotate;
267-
if (!n?.grid) return;
268-
const grid = n.grid;
268+
const grid = n?.grid || (DDManager.dropElement?.el as GridHTMLElement)?.gridstack;
269269

270270
if (e.key === 'Escape') {
271-
if (n._origRotate) {
271+
if (n && n._origRotate) {
272272
n._orig = n._origRotate;
273273
delete n._origRotate;
274274
}
275-
grid.engine.restoreInitial();
275+
grid?.cancelDrag();
276276
this._mouseUp(this.mouseDownEvent);
277-
} else if (e.key === 'r' || e.key === 'R') {
277+
} else if (n && grid && (e.key === 'r' || e.key === 'R')) {
278278
if (!Utils.canBeRotated(n)) return;
279279
n._origRotate = n._origRotate || { ...n._orig }; // store the real orig size in case we Esc after doing rotation
280280
delete n._moving; // force rotate to happen (move waits for >50% coverage otherwise)

src/gridstack-engine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ export class GridStackEngine {
506506
/** @internal restore all the nodes back to initial values (called when we leave) */
507507
public restoreInitial(): GridStackEngine {
508508
this.nodes.forEach(n => {
509-
if (Utils.samePos(n, n._orig)) return;
509+
if (!n._orig || Utils.samePos(n, n._orig)) return;
510510
Utils.copyPos(n, n._orig);
511511
n._dirty = true;
512512
});

src/gridstack.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,19 +233,16 @@ export class GridStack {
233233
/** @internal create placeholder DIV as needed */
234234
public get placeholder(): GridItemHTMLElement {
235235
if (!this._placeholder) {
236-
const placeholderChild = document.createElement('div'); // child so padding match item-content
237-
placeholderChild.className = 'placeholder-content';
236+
this._placeholder = Utils.createDiv([this.opts.placeholderClass, gridDefaults.itemClass, this.opts.itemClass]);
237+
const placeholderChild = Utils.createDiv(['placeholder-content'], this._placeholder);
238238
if (this.opts.placeholderText) {
239239
placeholderChild.textContent = this.opts.placeholderText;
240240
}
241-
this._placeholder = document.createElement('div');
242-
this._placeholder.classList.add(this.opts.placeholderClass, gridDefaults.itemClass, this.opts.itemClass);
243-
this.placeholder.appendChild(placeholderChild);
244241
}
245242
return this._placeholder;
246243
}
247244
/** @internal */
248-
protected _placeholder: HTMLElement;
245+
protected _placeholder: GridItemHTMLElement;
249246
/** @internal prevent cached layouts from being updated when loading into small column layouts */
250247
protected _ignoreLayoutsNodeChange: boolean;
251248
/** @internal */
@@ -998,6 +995,7 @@ export class GridStack {
998995
if (this.parentGridNode) delete this.parentGridNode.subGrid;
999996
delete this.parentGridNode;
1000997
delete this.opts;
998+
delete this._placeholder.gridstackNode;
1001999
delete this._placeholder;
10021000
delete this.engine;
10031001
delete this.el.gridstack; // remove circular dependency that would prevent a freeing
@@ -2074,6 +2072,22 @@ export class GridStack {
20742072
return this;
20752073
}
20762074

2075+
/** @internal call when drag (and drop) needs to be cancelled (Esc key) */
2076+
public cancelDrag() {
2077+
const n = this._placeholder?.gridstackNode;
2078+
if (!n) return;
2079+
if (n._isExternal) {
2080+
// remove any newly inserted nodes (from outside)
2081+
n._isAboutToRemove = true;
2082+
this.engine.removeNode(n);
2083+
} else if (n._isAboutToRemove) {
2084+
// restore any temp removed (dragged over trash)
2085+
GridStack._itemRemoving(n.el, false);
2086+
}
2087+
2088+
this.engine.restoreInitial();
2089+
}
2090+
20772091
/** @internal removes any drag&drop present (called during destroy) */
20782092
protected _removeDD(el: DDElementHost): GridStack {
20792093
dd.draggable(el, 'destroy').resizable(el, 'destroy');
@@ -2279,6 +2293,7 @@ export class GridStack {
22792293
const wasAdded = !!this.placeholder.parentElement; // skip items not actually added to us because of constrains, but do cleanup #1419
22802294
const wasSidebar = el !== helper;
22812295
this.placeholder.remove();
2296+
delete this.placeholder.gridstackNode;
22822297

22832298
// disable animation when replacing a placeholder (already positioned) with actual content
22842299
const noAnim = wasAdded && this.opts.animate;
@@ -2420,6 +2435,7 @@ export class GridStack {
24202435
/** called when the item stops moving/resizing */
24212436
const onEndMoving = (event: Event) => {
24222437
this.placeholder.remove();
2438+
delete this.placeholder.gridstackNode;
24232439
delete node._moving;
24242440
delete node._event;
24252441
delete node._lastTried;

0 commit comments

Comments
 (0)