Skip to content

Commit

Permalink
dep(): searchPossibleTargets => findTargets
Browse files Browse the repository at this point in the history
jsdoc
  • Loading branch information
ShaMan123 committed Sep 15, 2023
1 parent 40c725b commit 6e9942d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/canvas/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
* Override at will
*/
protected findDragTargets(e: DragEvent) {
const { target, targets } = this.searchPossibleTargets(
const { target, targets } = this.findTargets(
this._objects,
this.getPointer(e, true)
);
Expand Down Expand Up @@ -1487,10 +1487,10 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
const pointer = this.getPointer(e, true);
target =
// first search active objects for a target to remove
this.searchPossibleTargets(prevActiveObjects, pointer).target ||
this.findTargets(prevActiveObjects, pointer).target ||
// if not found, search under active selection for a target to add
// `prevActiveObjects` will be searched but we already know they will not be found
this.searchPossibleTargets(this._objects, pointer).target;
this.findTargets(this._objects, pointer).target;
// if nothing is found bail out
if (!target || !target.selectable) {
return false;
Expand Down
30 changes: 17 additions & 13 deletions src/canvas/SelectableCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents>

// check pointer is over active selection and possibly perform `subTargetCheck`
const { target: selectedTarget, targets: selectedTargets } =
this.searchPossibleTargets([activeObject], pointer);
this.findTargets([activeObject], pointer);

if (selectedTarget && aObjects.length > 1) {
// active selection does not select sub targets like normal groups
Expand All @@ -743,7 +743,7 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents>
);
return activeObject;
} else {
const { target, targets: canvasTargets } = this.searchPossibleTargets(
const { target, targets: canvasTargets } = this.findTargets(
this._objects,
pointer
);
Expand All @@ -769,10 +769,7 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents>
}
}

const { target, targets } = this.searchPossibleTargets(
this._objects,
pointer
);
const { target, targets } = this.findTargets(this._objects, pointer);
this.targets = target ? targets.slice(0, targets.indexOf(target)) : targets;
return target;
}
Expand Down Expand Up @@ -814,9 +811,9 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents>

/**
* Search for objects containing {@link pointer}.
* Search is not greedy, returning once a hit is found.
* @param {Array} [objects] objects array to look into
* @param {Object} [pointer] point coordinates to check
*
* @param {FabricObject[]} objects objects array to look into
* @param {Point} pointer point canvas element plane coordinates to check
* @param {boolean} [param2.searchStrategy] strategy
* @returns {FabricObject[]} path of objects starting from **top most** object on screen.
*/
Expand Down Expand Up @@ -854,13 +851,13 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents>
* Search objects for an object containing {@link pointer}
* depending on the tree's configuration (`subTargetCheck`, `interactive`, `selectable`)
*
* @see {@link findTargetsTraversal}
* @see {@link findTarget} and {@link findTargetsTraversal}
*
* @param {FabricObject[]} [objects] objects array to look into
* @param {Object} [pointer] canvas element plane coordinates to check
* @param {FabricObject[]} objects objects array to look into
* @param {Point} pointer canvas element plane coordinates to check
* @return {FabricObject} **top most selectable object on screen** that contains {@link pointer}
*/
searchPossibleTargets(
findTargets(
objects: FabricObject[],
pointer: Point,
{
Expand All @@ -883,6 +880,13 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents>
};
}

/**
* @deprecated see {@link findTargets}
*/
searchPossibleTargets(objects: FabricObject[], pointer: Point) {
return this.findTargets(objects, pointer).target;
}

/**
* Returns pointer coordinates without the effect of the viewport
* @param {Object} pointer with "x" and "y" number values in canvas HTML coordinates
Expand Down
12 changes: 5 additions & 7 deletions src/canvas/__tests__/eventData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ describe('Event targets', () => {
}
);

test('searchPossibleTargets', () => {
test('findTargets', () => {
const subTarget = new FabricObject();
const target = new Group([subTarget], {
subTargetCheck: true,
Expand All @@ -318,14 +318,14 @@ describe('Event targets', () => {
canvas.add(parent);

jest.spyOn(canvas, '_checkTarget').mockReturnValue(true);
expect(canvas.searchPossibleTargets([parent], new Point())).toEqual({
expect(canvas.findTargets([parent], new Point())).toEqual({
target,
targets: [subTarget, target, parent],
});
});

test.each([true, false])(
'searchPossibleTargets with selection and subTargetCheck %s',
'findTargets with selection and subTargetCheck %s',
(subTargetCheck) => {
const subTarget = new FabricObject();
const target = new Group([subTarget], {
Expand Down Expand Up @@ -355,17 +355,15 @@ describe('Event targets', () => {
: [activeSelection]
);

expect(
canvas.searchPossibleTargets([activeSelection], new Point())
).toEqual({
expect(canvas.findTargets([activeSelection], new Point())).toEqual({
target: activeSelection,
targets: subTargetCheck
? [other, activeSelection]
: [activeSelection],
});

expect(
canvas.searchPossibleTargets(canvas.getActiveObjects(), new Point())
canvas.findTargets(canvas.getActiveObjects(), new Point())
).toEqual({
target: other,
targets: [other],
Expand Down

0 comments on commit 6e9942d

Please sign in to comment.