Skip to content

Commit d233788

Browse files
committed
feat: expose ghost element and starting mouse position on ghost element created event
Closes #81 Closes #85
1 parent 5ad4d0d commit d233788

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

projects/angular-draggable-droppable/src/lib/draggable.directive.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,21 @@ describe('draggable directive', () => {
689689
expect(draggableElement.style.visibility).not.to.be.ok;
690690
});
691691

692+
it('should expose the mouse coordinates and ghost element', () => {
693+
const draggableElement =
694+
fixture.componentInstance.draggableElement.nativeElement;
695+
triggerDomEvent('mousedown', draggableElement, { clientX: 5, clientY: 10 });
696+
triggerDomEvent('mousemove', draggableElement, { clientX: 6, clientY: 10 });
697+
const ghostElement = draggableElement.nextSibling as HTMLElement;
698+
expect(
699+
fixture.componentInstance.ghostElementCreated
700+
).to.have.been.calledWith({
701+
element: ghostElement,
702+
clientX: 5,
703+
clientY: 10
704+
});
705+
});
706+
692707
it('should create a clone of the element and leave old element visible', () => {
693708
fixture.componentInstance.showOriginalElementWhileDragging = true;
694709
fixture.detectChanges();

projects/angular-draggable-droppable/src/lib/draggable.directive.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ export interface TimeLongPress {
7272
timerEnd: number;
7373
}
7474

75+
export interface GhostElementCreatedEvent {
76+
clientX: number;
77+
clientY: number;
78+
element: HTMLElement;
79+
}
80+
7581
@Directive({
7682
selector: '[mwlDraggable]'
7783
})
@@ -154,7 +160,7 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
154160
* Called after the ghost element has been created
155161
*/
156162
@Output()
157-
ghostElementCreated = new EventEmitter();
163+
ghostElementCreated = new EventEmitter<GhostElementCreatedEvent>();
158164

159165
/**
160166
* Called when the element is being dragged
@@ -338,7 +344,7 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
338344
share()
339345
);
340346

341-
dragStarted$.subscribe(() => {
347+
dragStarted$.subscribe(({ clientX, clientY, x, y }) => {
342348
this.zone.run(() => {
343349
this.dragStart.next({ cancelDrag$ });
344350
});
@@ -398,7 +404,11 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
398404
}
399405

400406
this.zone.run(() => {
401-
this.ghostElementCreated.emit();
407+
this.ghostElementCreated.emit({
408+
clientX: clientX - x,
409+
clientY: clientY - y,
410+
element: clone
411+
});
402412
});
403413

404414
dragEnded$.subscribe(() => {

projects/angular-draggable-droppable/src/public_api.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export {
88
DragPointerDownEvent,
99
DragStartEvent,
1010
DragMoveEvent,
11-
DragEndEvent
11+
DragEndEvent,
12+
GhostElementCreatedEvent
1213
} from './lib/draggable.directive';

0 commit comments

Comments
 (0)