@@ -532,22 +532,23 @@ GridStack.prototype._leave = function(node: GridStackNode, el: GridItemHTMLEleme
532
532
533
533
/** @internal called when item is being dragged/resized */
534
534
GridStack . prototype . _dragOrResize = function ( el : GridItemHTMLElement , event : Event , ui : DDUIData , node : GridStackNode , cellWidth : number , cellHeight : number ) {
535
- // calculate the place where we're landing by offsetting margin so actual edge crosses mid point
536
- let left = ui . position . left + ( ui . position . left > node . _lastUiPosition . left ? - this . opts . marginRight : this . opts . marginLeft ) ;
537
- let top = ui . position . top + ( ui . position . top > node . _lastUiPosition . top ? - this . opts . marginBottom : this . opts . marginTop ) ;
538
- let x = Math . round ( left / cellWidth ) ;
539
- let y = Math . round ( top / cellHeight ) ;
540
- let w = node . w ;
541
- let h = node . h ;
535
+ let p = { ...node . _orig } ;
542
536
let resizing : boolean ;
543
537
544
538
if ( event . type === 'drag' ) {
545
539
if ( node . _isCursorOutside ) return ; // handled by dropover
546
540
let distance = ui . position . top - node . _prevYPix ;
547
541
node . _prevYPix = ui . position . top ;
548
542
Utils . updateScrollPosition ( el , ui . position , distance ) ;
543
+
544
+ // get new position taking into account the margin in the direction we are moving! (need to pass mid point by margin)
545
+ let left = ui . position . left + ( ui . position . left > node . _lastUiPosition . left ? - this . opts . marginRight : this . opts . marginLeft ) ;
546
+ let top = ui . position . top + ( ui . position . top > node . _lastUiPosition . top ? - this . opts . marginBottom : this . opts . marginTop ) ;
547
+ p . x = Math . round ( left / cellWidth ) ;
548
+ p . y = Math . round ( top / cellHeight ) ;
549
+
549
550
// if inTrash or outside of the bounds (but not external which is handled by 'dropout' event), temporarily remove it from us
550
- if ( node . _isAboutToRemove || ( ! node . _isExternal && this . engine . isOutside ( x , y , node ) ) ) {
551
+ if ( node . _isAboutToRemove || ( ! node . _isExternal && this . engine . isOutside ( p . x , p . y , node ) ) ) {
551
552
this . _leave ( node , event . target ) ;
552
553
} else {
553
554
if ( node . _temporaryRemoved ) {
@@ -558,28 +559,35 @@ GridStack.prototype._dragOrResize = function(el: GridItemHTMLElement, event: Eve
558
559
delete node . _temporaryRemoved ;
559
560
}
560
561
}
561
- if ( node . x === x && node . y === y ) return ; // skip same
562
+ if ( node . x === p . x && node . y === p . y ) return ; // skip same
562
563
// DON'T skip one we tried as we might have failed because of coverage <50% before
563
564
// if (node._lastTried && node._lastTried.x === x && node._lastTried.y === y) return;
564
565
} else if ( event . type === 'resize' ) {
565
- if ( x < 0 ) return ;
566
+ if ( p . x < 0 ) return ;
566
567
// Scrolling page if needed
567
568
Utils . updateScrollResize ( event as MouseEvent , el , cellHeight ) ;
568
- w = Math . round ( ui . size . width / cellWidth ) ;
569
- h = Math . round ( ui . size . height / cellHeight ) ;
570
- if ( node . w === w && node . h === h ) return ;
571
- if ( node . _lastTried && node . _lastTried . w === w && node . _lastTried . h === h ) return ; // skip one we tried (but failed)
569
+
570
+ // get new size
571
+ p . w = Math . round ( ( ui . size . width - this . opts . marginLeft ) / cellWidth ) ;
572
+ p . h = Math . round ( ( ui . size . height - this . opts . marginTop ) / cellHeight ) ;
573
+ if ( node . w === p . w && node . h === p . h ) return ;
574
+ if ( node . _lastTried && node . _lastTried . w === p . w && node . _lastTried . h === p . h ) return ; // skip one we tried (but failed)
575
+
576
+ // if we changed sizing on left side, move the item as well. Note: we don't support TOP resizing
577
+ if ( Math . round ( ui . position . left ) < Math . round ( node . _orig . x * cellWidth ) ) { // use round or we can get slightly off compare
578
+ p . x = node . _orig . x + node . _orig . w - p . w ;
579
+ }
572
580
resizing = true ;
573
581
}
574
582
575
- node . _lastTried = { x , y , w , h } ; // set as last tried (will nuke if we go there)
583
+ node . _lastTried = p ; // set as last tried (will nuke if we go there)
576
584
let rect : GridStackPosition = { // screen pix of the dragged box
577
585
x : ui . position . left + this . opts . marginLeft ,
578
586
y : ui . position . top + this . opts . marginTop ,
579
587
w : ( ui . size ? ui . size . width : node . w * cellWidth ) - this . opts . marginLeft - this . opts . marginRight ,
580
588
h : ( ui . size ? ui . size . height : node . h * cellHeight ) - this . opts . marginTop - this . opts . marginBottom
581
589
} ;
582
- if ( this . engine . moveNodeCheck ( node , { x , y , w , h , cellWidth, cellHeight, rect} ) ) {
590
+ if ( this . engine . moveNodeCheck ( node , { ... p , cellWidth, cellHeight, rect} ) ) {
583
591
node . _lastUiPosition = ui . position ;
584
592
this . engine . cacheRects ( cellWidth , cellHeight , this . opts . marginTop , this . opts . marginRight , this . opts . marginBottom , this . opts . marginLeft ) ;
585
593
delete node . _skipDown ;
0 commit comments