Skip to content

Commit

Permalink
chore(Object): pass ctx to dnd methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaMan123 committed May 3, 2024
1 parent aa2a331 commit b746927
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/canvas/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,14 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
if (source) {
ctx.save();
source.transform(ctx);
source.renderDragSourceEffect(e);
source.renderDragSourceEffect(e, ctx);
ctx.restore();
dirty = true;
}
if (target) {
ctx.save();
target.transform(ctx);
target.renderDropTargetEffect(e);
target.renderDropTargetEffect(e, ctx);
ctx.restore();
dirty = true;
}
Expand Down
9 changes: 4 additions & 5 deletions src/shapes/IText/IText.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Canvas } from '../../canvas/Canvas';
import { Canvas } from '../../canvas/Canvas';
import '../../../jest.extend';
import { Group } from '../Group';
import { IText } from './IText';
Expand Down Expand Up @@ -27,11 +27,10 @@ describe('IText', () => {
group.setCoords();
const fillRect = jest.fn();
const getZoom = jest.fn().mockReturnValue(zoom);
const mockContext = { fillRect };
const mockCanvas = { contextTop: mockContext, getZoom };
jest.replaceProperty(text, 'canvas', mockCanvas as unknown as Canvas);
const canvas = new Canvas();
jest.replaceProperty(text, 'canvas', canvas);

text.renderCursorAt(1);
text.renderCursorAt(canvas.getTopContext(), 1);
const call = fillRect.mock.calls[0];
expect({ width: call[2], height: call[3] }).toMatchSnapshot({
cloneDeepWith: (value) =>
Expand Down
17 changes: 8 additions & 9 deletions src/shapes/IText/IText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,12 @@ export class IText<
}

/**
* Renders cursor on context Top, outside the animation cycle, on request
* Renders cursor on {@link ctx}, outside the animation cycle, on request.
* Used for the drag/drop effect.
* If contextTop is not available, do nothing.
*/
renderCursorAt(selectionStart: number) {
renderCursorAt(ctx: CanvasRenderingContext2D, selectionStart: number) {
const boundaries = this._getCursorBoundaries(selectionStart, true);
this._renderCursor(this.canvas!.contextTop, boundaries, selectionStart);
this._renderCursor(ctx, boundaries, selectionStart);
}

/**
Expand Down Expand Up @@ -550,21 +549,21 @@ export class IText<
}

/**
* Renders drag start text selection
* @override Render drag start text selection
*/
renderDragSourceEffect() {
renderDragSourceEffect(e: DragEvent, ctx: CanvasRenderingContext2D) {
const dragStartSelection =
this.draggableTextDelegate.getDragStartSelection()!;
this._renderSelection(
this.canvas!.contextTop,
ctx,
dragStartSelection,
this._getCursorBoundaries(dragStartSelection.selectionStart, true)
);
}

renderDropTargetEffect(e: DragEvent) {
renderDropTargetEffect(e: DragEvent, ctx: CanvasRenderingContext2D) {
const dragSelection = this.getSelectionStartFromPointer(e);
this.renderCursorAt(dragSelection);
this.renderCursorAt(ctx, dragSelection);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/shapes/Object/InteractiveObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,20 +682,22 @@ export class InteractiveFabricObject<
* example: render the selection status for the part of text that is being dragged from a text object
* @public
* @param {DragEvent} e
* @param {CanvasRenderingContext2D} ctx transformed into object plane
*/
renderDragSourceEffect(e: DragEvent) {
renderDragSourceEffect(e: DragEvent, ctx: CanvasRenderingContext2D) {
// for subclasses
}

/**
* Override to customize drag and drop behavior
* render a specific effect when an object is the target of a drag event
* used to show that the underly object can receive a drop, or to show how the
* used to show that the underlay object can receive a drop, or to show how the
* object will change when dropping. example: show the cursor where the text is about to be dropped
* @public
* @param {DragEvent} e
* @param {CanvasRenderingContext2D} ctx transformed into object plane
*/
renderDropTargetEffect(e: DragEvent) {
renderDropTargetEffect(e: DragEvent, ctx: CanvasRenderingContext2D) {
// for subclasses
}
}

0 comments on commit b746927

Please sign in to comment.