Skip to content

Commit 2746147

Browse files
committed
BREAKING: renderDragSourceEffect/renderDropSourceEffect signature
1 parent 20dcd66 commit 2746147

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

src/EventTypeDefs.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ interface OutEvent {
165165
nextTarget?: FabricObject;
166166
}
167167

168+
export type DragEventRenderingEffectData = {
169+
e: DragEvent;
170+
dragSource: FabricObject | undefined;
171+
dropTarget: FabricObject | undefined;
172+
prevDropTarget: FabricObject | undefined;
173+
viewportPoint: Point;
174+
scenePoint: Point;
175+
};
176+
168177
export interface DragEventData extends TEvent<DragEvent> {
169178
target?: FabricObject;
170179
subTargets?: FabricObject[];

src/canvas/Canvas.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { NONE } from '../constants';
33
import type {
44
CanvasEvents,
55
DragEventData,
6+
DragEventRenderingEffectData,
67
ObjectEvents,
78
TPointerEvent,
89
TPointerEventNames,
@@ -311,35 +312,35 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
311312
* Doing so will render the correct effect for all cases including an overlap between `source` and `target`.
312313
* @private
313314
*/
314-
private _renderDragEffects(
315-
e: DragEvent,
316-
source?: FabricObject,
317-
target?: FabricObject
318-
) {
315+
private _renderDragEffects(context: DragEventRenderingEffectData) {
316+
const { dragSource, dropTarget, prevDropTarget } = context;
319317
let dirty = false;
320318
// clear top context
321-
const dropTarget = this._dropTarget;
322-
if (dropTarget && dropTarget !== source && dropTarget !== target) {
323-
dropTarget.clearContextTop();
319+
if (
320+
prevDropTarget &&
321+
prevDropTarget !== dragSource &&
322+
prevDropTarget !== dropTarget
323+
) {
324+
prevDropTarget.clearContextTop();
324325
dirty = true;
325326
}
326-
source?.clearContextTop();
327-
target !== source && target?.clearContextTop();
327+
dragSource?.clearContextTop();
328+
dropTarget !== dragSource && dropTarget?.clearContextTop();
328329
// render effects
329330
const ctx = this.contextTop;
330331
ctx.save();
331332
ctx.transform(...this.viewportTransform);
332-
if (source) {
333+
if (dragSource) {
333334
ctx.save();
334-
source.transform(ctx);
335-
source.renderDragSourceEffect(e, ctx);
335+
dragSource.transform(ctx);
336+
dragSource.renderDragSourceEffect(ctx, context);
336337
ctx.restore();
337338
dirty = true;
338339
}
339-
if (target) {
340+
if (dropTarget) {
340341
ctx.save();
341-
target.transform(ctx);
342-
target.renderDropTargetEffect(e, ctx);
342+
dropTarget.transform(ctx);
343+
dropTarget.renderDropTargetEffect(ctx, context);
343344
ctx.restore();
344345
dirty = true;
345346
}
@@ -455,7 +456,13 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
455456
subTarget.fire(eventType, options);
456457
}
457458
// render drag effects now that relations between source and target is clear
458-
this._renderDragEffects(e, dragSource, dropTarget);
459+
this._renderDragEffects({
460+
...points,
461+
e,
462+
dragSource,
463+
dropTarget,
464+
prevDropTarget: this._dropTarget,
465+
});
459466
this._dropTarget = dropTarget;
460467
}
461468

@@ -484,8 +491,9 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
484491
* @param {Event} [e] Event object fired on Event.js shake
485492
*/
486493
private _onDragLeave(e: DragEvent) {
494+
const points = getEventPoints(this, e);
487495
const options: DragEventData = {
488-
...getEventPoints(this, e),
496+
...points,
489497
e,
490498
target: this._draggedoverTarget,
491499
subTargets: this.targets,
@@ -495,7 +503,13 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
495503

496504
// fire dragleave on targets
497505
this._fireEnterLeaveEvents(undefined, options);
498-
this._renderDragEffects(e, this._dragSource);
506+
this._renderDragEffects({
507+
...points,
508+
e,
509+
dragSource: this._dragSource,
510+
dropTarget: undefined,
511+
prevDropTarget: this._dropTarget,
512+
});
499513
this._dropTarget = undefined;
500514
// clear targets
501515
this.targets = [];

src/shapes/IText/IText.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from '../Text/constants';
1919
import { CENTER, LEFT, RIGHT } from '../../constants';
2020
import type { ObjectToCanvasElementOptions } from '../Object/Object';
21+
import type { DragEventRenderingEffectData } from '../../EventTypeDefs';
2122

2223
type CursorBoundaries = {
2324
left: number;
@@ -551,7 +552,7 @@ export class IText<
551552
/**
552553
* @override Render drag start text selection
553554
*/
554-
renderDragSourceEffect(e: DragEvent, ctx: CanvasRenderingContext2D) {
555+
renderDragSourceEffect(ctx: CanvasRenderingContext2D) {
555556
const dragStartSelection =
556557
this.draggableTextDelegate.getDragStartSelection()!;
557558
this._renderSelection(
@@ -561,7 +562,10 @@ export class IText<
561562
);
562563
}
563564

564-
renderDropTargetEffect(e: DragEvent, ctx: CanvasRenderingContext2D) {
565+
renderDropTargetEffect(
566+
ctx: CanvasRenderingContext2D,
567+
{ e }: DragEventRenderingEffectData
568+
) {
565569
const dragSelection = this.getSelectionStartFromPointer(e);
566570
this.renderCursorAt(ctx, dragSelection);
567571
}

src/shapes/Object/InteractiveObject.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import {
1212
} from '../../util/misc/matrix';
1313
import type { Control } from '../../controls/Control';
1414
import { sizeAfterTransform } from '../../util/misc/objectTransforms';
15-
import type { ObjectEvents, TPointerEvent } from '../../EventTypeDefs';
15+
import type {
16+
DragEventRenderingEffectData,
17+
ObjectEvents,
18+
TPointerEvent,
19+
} from '../../EventTypeDefs';
1620
import type { Canvas } from '../../canvas/Canvas';
1721
import type { ControlRenderingStyleOverride } from '../../controls/controlRendering';
1822
import type { FabricObjectProps } from './types/FabricObjectProps';
@@ -684,7 +688,10 @@ export class InteractiveFabricObject<
684688
* @param {DragEvent} e
685689
* @param {CanvasRenderingContext2D} ctx transformed into object plane
686690
*/
687-
renderDragSourceEffect(e: DragEvent, ctx: CanvasRenderingContext2D) {
691+
renderDragSourceEffect(
692+
ctx: CanvasRenderingContext2D,
693+
context: DragEventRenderingEffectData
694+
) {
688695
// for subclasses
689696
}
690697

@@ -697,7 +704,10 @@ export class InteractiveFabricObject<
697704
* @param {DragEvent} e
698705
* @param {CanvasRenderingContext2D} ctx transformed into object plane
699706
*/
700-
renderDropTargetEffect(e: DragEvent, ctx: CanvasRenderingContext2D) {
707+
renderDropTargetEffect(
708+
ctx: CanvasRenderingContext2D,
709+
context: DragEventRenderingEffectData
710+
) {
701711
// for subclasses
702712
}
703713
}

0 commit comments

Comments
 (0)