Skip to content

Commit

Permalink
fix(drag): Shows correct drag-image while moving images to folders
Browse files Browse the repository at this point in the history
  • Loading branch information
castaway committed Jan 21, 2025
1 parent 3c72817 commit e89f3cb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/app/canvastable/canvastable.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
(dragstart)="dragColumnOverlay($event)"
>
</div>
<canvas id="thedragimage"></canvas>
27 changes: 17 additions & 10 deletions src/app/canvastable/canvastable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ export class CanvasTableComponent implements AfterViewInit, DoCheck, OnInit {
}
}
this.hoverRowIndex = newHoverRowIndex;
this.updateDragImage(newHoverRowIndex);
}

if (this.dragSelectionDirectionIsDown === null) {
Expand Down Expand Up @@ -590,7 +589,7 @@ export class CanvasTableComponent implements AfterViewInit, DoCheck, OnInit {
);
}

private updateDragImage(selectedRowIndex: number) {
private updateDragImage(selectedRowIndex: number) :HTMLCanvasElement {
const dragImageYCoords: number[][] = [];
let dragImageDestY = 0;

Expand All @@ -609,21 +608,29 @@ export class CanvasTableComponent implements AfterViewInit, DoCheck, OnInit {
}
});

const dragImageCanvas = document.getElementById('thedragimage') as HTMLCanvasElement;
const dragImageCanvas = document.createElement("canvas");
dragImageCanvas.width = this.canv.width - 20;
dragImageCanvas.height = dragImageYCoords.length * this.rowheight;

const dragContext = dragImageCanvas.getContext('2d');
dragImageYCoords.forEach(ycoords =>
dragContext.clearRect(0,0,dragImageCanvas.width,dragImageCanvas.height);
dragContext.fillStyle = 'red';
dragContext.fillRect(0,0,dragImageCanvas.width,dragImageCanvas.height);
dragImageYCoords.forEach(ycoords => {
dragContext.drawImage(this.canv,
0, ycoords[0], this.canv.width - 20, this.rowheight,
0,
ycoords[1],
this.canv.width - 20, this.rowheight
));
);
});

document.body.append(dragImageCanvas);
dragImageCanvas.setAttribute('id', 'thedragcanvas');
dragImageCanvas.style.position = "absolute"; dragImageCanvas.style.top = "0px"; dragImageCanvas.style.left = "-"+ dragImageCanvas.width + "px";

// const dragImage = document.getElementById('thedragimage') as HTMLImageElement;
// dragImage.src = dragImageCanvas.toDataURL();

return dragImageCanvas;
}

public dragColumnOverlay(event: DragEvent) {
Expand All @@ -632,10 +639,11 @@ export class CanvasTableComponent implements AfterViewInit, DoCheck, OnInit {
const selectedRowIndex = this.getRowIndexByClientY(event.clientY);

if (!this.columns[selectedColIndex].checkbox) {
this.selectListener.rowSelected(selectedRowIndex, -1);
const dragCanvas = this.updateDragImage(selectedRowIndex);
event.dataTransfer.dropEffect = 'move';
event.dataTransfer.setDragImage(document.getElementById('thedragimage'), 0, 0);
event.dataTransfer.setDragImage(dragCanvas, 0, 0);
event.dataTransfer.setData('text/plain', 'rowIndex:' + selectedRowIndex);
this.selectListener.rowSelected(selectedRowIndex, -1);
} else {
event.preventDefault();
this.lastMouseDownEvent = event;
Expand Down Expand Up @@ -763,7 +771,6 @@ export class CanvasTableComponent implements AfterViewInit, DoCheck, OnInit {
this.getColIndexByClientX(clientX),
multiSelect);

this.updateDragImage(selectedRowIndex);
this.hasChanges = true;
}

Expand Down
1 change: 1 addition & 0 deletions src/app/folder/folderlist.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export class FolderListComponent implements OnChanges {
this.dropAboveOrBelowOrInside = DropPosition.NONE;
this.dragFolderInProgress = false;
this.dropFolderId = 0;
document.getElementById('thedragcanvas').remove();
}

dragFolderStart(event, folderId: NumberConstructor): void {
Expand Down

0 comments on commit e89f3cb

Please sign in to comment.