fabric-vectr 6.7.14 → 6.7.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +86 -11
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.min.mjs +1 -1
- package/dist/index.min.mjs.map +1 -1
- package/dist/index.mjs +86 -11
- package/dist/index.mjs.map +1 -1
- package/dist/index.node.cjs +86 -11
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs +86 -11
- package/dist/index.node.mjs.map +1 -1
- package/dist/package.json.min.mjs +1 -1
- package/dist/package.json.mjs +1 -1
- package/dist/src/canvas/Canvas.d.ts +10 -0
- package/dist/src/canvas/Canvas.d.ts.map +1 -1
- package/dist/src/canvas/Canvas.min.mjs +1 -1
- package/dist/src/canvas/Canvas.min.mjs.map +1 -1
- package/dist/src/canvas/Canvas.mjs +85 -10
- package/dist/src/canvas/Canvas.mjs.map +1 -1
- package/dist/src/canvas/SelectableCanvas.d.ts +6 -0
- package/dist/src/canvas/SelectableCanvas.d.ts.map +1 -1
- package/dist/src/canvas/SelectableCanvas.min.mjs.map +1 -1
- package/dist/src/canvas/SelectableCanvas.mjs.map +1 -1
- package/dist-extensions/src/canvas/Canvas.d.ts +10 -0
- package/dist-extensions/src/canvas/Canvas.d.ts.map +1 -1
- package/dist-extensions/src/canvas/SelectableCanvas.d.ts +6 -0
- package/dist-extensions/src/canvas/SelectableCanvas.d.ts.map +1 -1
- package/package.json +33 -33
- package/src/canvas/Canvas.ts +89 -8
- package/src/canvas/SelectableCanvas.ts +6 -0
- package/src/canvas/__tests__/additiveSelection.test.ts +118 -0
package/src/canvas/Canvas.ts
CHANGED
|
@@ -1124,11 +1124,17 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
|
|
|
1124
1124
|
shouldRender = true;
|
|
1125
1125
|
} else if (this._shouldClearSelection(e, target)) {
|
|
1126
1126
|
const objs = this.getActiveObjects();
|
|
1127
|
-
|
|
1128
|
-
//
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1127
|
+
// holding the selection key keeps the current selection alive so that the
|
|
1128
|
+
// following box selection (or click) can add to it instead of replacing it
|
|
1129
|
+
const additive =
|
|
1130
|
+
!!objs.length && !!this.selection && this._isSelectionKeyPressed(e);
|
|
1131
|
+
if (!additive) {
|
|
1132
|
+
this.discardActiveObject(e);
|
|
1133
|
+
// 多选取消,重置Coords, 防止无法再次选中
|
|
1134
|
+
objs.forEach((obj) => obj.setCoords());
|
|
1135
|
+
if (!target && this.switchIsolateObject(e)) {
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1132
1138
|
}
|
|
1133
1139
|
}
|
|
1134
1140
|
// we start a group selector rectangle if
|
|
@@ -1150,6 +1156,9 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
|
|
|
1150
1156
|
y: p.y,
|
|
1151
1157
|
deltaY: 0,
|
|
1152
1158
|
deltaX: 0,
|
|
1159
|
+
// capture the modifier now: releasing the key during the drag
|
|
1160
|
+
// must not turn an additive selection into a replacing one
|
|
1161
|
+
...(this._isSelectionKeyPressed(e) ? { additive: true } : {}),
|
|
1153
1162
|
};
|
|
1154
1163
|
}
|
|
1155
1164
|
|
|
@@ -1575,13 +1584,15 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
|
|
|
1575
1584
|
if (!this.selection || !this._groupSelector) {
|
|
1576
1585
|
return false;
|
|
1577
1586
|
}
|
|
1578
|
-
const { x, y, deltaX, deltaY } = this._groupSelector,
|
|
1587
|
+
const { x, y, deltaX, deltaY, additive } = this._groupSelector,
|
|
1579
1588
|
point1 = new Point(x, y),
|
|
1580
1589
|
point2 = point1.add(new Point(deltaX, deltaY)),
|
|
1581
1590
|
tl = point1.min(point2),
|
|
1582
1591
|
br = point1.max(point2),
|
|
1583
1592
|
size = br.subtract(tl);
|
|
1584
1593
|
|
|
1594
|
+
// the box only collects the first level objects of the current selection scope,
|
|
1595
|
+
// meaning the children of the isolated group, or the canvas root objects
|
|
1585
1596
|
const collectedObjects = this.collectObjects(
|
|
1586
1597
|
this.getSearchTargets(),
|
|
1587
1598
|
{
|
|
@@ -1593,6 +1604,16 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
|
|
|
1593
1604
|
{ includeIntersecting: !this.selectionFullyContained },
|
|
1594
1605
|
) as FabricObject[];
|
|
1595
1606
|
|
|
1607
|
+
// cleanup
|
|
1608
|
+
this._groupSelector = null;
|
|
1609
|
+
|
|
1610
|
+
// the selection key was held while dragging and there is something to add to:
|
|
1611
|
+
// keep the current selection and merge the collected objects into it
|
|
1612
|
+
if (additive && this.getActiveObjects().length) {
|
|
1613
|
+
this.addToActiveSelection(e, collectedObjects);
|
|
1614
|
+
return true;
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1596
1617
|
const objects =
|
|
1597
1618
|
// though this method runs only after mouse move the pointer could do a mouse up on the same position as mouse down
|
|
1598
1619
|
// should it be handled as is?
|
|
@@ -1618,11 +1639,71 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
|
|
|
1618
1639
|
this.setActiveObject(new klass(objects, { canvas: this }), e);
|
|
1619
1640
|
}
|
|
1620
1641
|
|
|
1621
|
-
// cleanup
|
|
1622
|
-
this._groupSelector = null;
|
|
1623
1642
|
return true;
|
|
1624
1643
|
}
|
|
1625
1644
|
|
|
1645
|
+
/**
|
|
1646
|
+
* Merges the objects collected by a box selection into the current selection.
|
|
1647
|
+
* Only objects belonging to the current selection scope are taken into account:
|
|
1648
|
+
* the first level children of the isolated group, or the canvas root objects
|
|
1649
|
+
* when no group is isolated. Previously selected objects outside of that scope
|
|
1650
|
+
* are removed from the selection, since a selection can't mix objects coming
|
|
1651
|
+
* from different containers.
|
|
1652
|
+
* @private
|
|
1653
|
+
*/
|
|
1654
|
+
protected addToActiveSelection(
|
|
1655
|
+
e: TPointerEvent,
|
|
1656
|
+
collectedObjects: FabricObject[],
|
|
1657
|
+
) {
|
|
1658
|
+
const prevActiveObjects = this.getActiveObjects(),
|
|
1659
|
+
scope = this.getSearchTargets(),
|
|
1660
|
+
kept = prevActiveObjects.filter((object) => scope.includes(object)),
|
|
1661
|
+
dropped = prevActiveObjects.filter((object) => !scope.includes(object)),
|
|
1662
|
+
added = collectedObjects.filter(
|
|
1663
|
+
(object) =>
|
|
1664
|
+
!prevActiveObjects.includes(object) && !object.onSelect({ e }),
|
|
1665
|
+
);
|
|
1666
|
+
|
|
1667
|
+
if (!kept.length && !added.length) {
|
|
1668
|
+
// the box didn't add anything and nothing of the current selection
|
|
1669
|
+
// belongs to this scope
|
|
1670
|
+
dropped.length && this.discardActiveObject(e);
|
|
1671
|
+
return;
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
const activeObject = this._activeObject;
|
|
1675
|
+
if (isActiveSelection(activeObject)) {
|
|
1676
|
+
dropped.length && activeObject.remove(...dropped);
|
|
1677
|
+
added.length && activeObject.multiSelectAdd(...added);
|
|
1678
|
+
if (activeObject.size() === 1) {
|
|
1679
|
+
// deselecting the active selection returns the last remaining object to the canvas
|
|
1680
|
+
this._setActiveObject(activeObject.item(0), e);
|
|
1681
|
+
}
|
|
1682
|
+
this._fireSelectionEvents(prevActiveObjects, e);
|
|
1683
|
+
return;
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
const objects = kept.concat(added);
|
|
1687
|
+
if (objects.length === 1) {
|
|
1688
|
+
objects[0] !== activeObject && this.setActiveObject(objects[0], e);
|
|
1689
|
+
return;
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
// build the active selection out of the kept and the newly collected objects
|
|
1693
|
+
const klass =
|
|
1694
|
+
classRegistry.getClass<typeof ActiveSelection>('ActiveSelection');
|
|
1695
|
+
const newActiveSelection = new klass([], {
|
|
1696
|
+
/**
|
|
1697
|
+
* it is crucial to pass the canvas ref before calling {@link ActiveSelection#multiSelectAdd}
|
|
1698
|
+
* since it uses {@link FabricObject#isInFrontOf} which relies on the canvas ref
|
|
1699
|
+
*/
|
|
1700
|
+
canvas: this,
|
|
1701
|
+
});
|
|
1702
|
+
newActiveSelection.multiSelectAdd(...objects);
|
|
1703
|
+
this._setActiveObject(newActiveSelection, e);
|
|
1704
|
+
this._fireSelectionEvents(prevActiveObjects, e);
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1626
1707
|
/**
|
|
1627
1708
|
* @override clear {@link textEditingManager}
|
|
1628
1709
|
*/
|
|
@@ -273,6 +273,12 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents>
|
|
|
273
273
|
y: number;
|
|
274
274
|
deltaX: number;
|
|
275
275
|
deltaY: number;
|
|
276
|
+
/**
|
|
277
|
+
* `selectionKey` was held when the box selection started, meaning the
|
|
278
|
+
* objects collected by the box have to be added to the current selection
|
|
279
|
+
* instead of replacing it
|
|
280
|
+
*/
|
|
281
|
+
additive?: boolean;
|
|
276
282
|
} | null = null;
|
|
277
283
|
|
|
278
284
|
/**
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { Canvas } from '../Canvas';
|
|
3
|
+
import { Rect } from '../../shapes/Rect';
|
|
4
|
+
import { Group } from '../../shapes/Group';
|
|
5
|
+
import { ActiveSelection } from '../../shapes/ActiveSelection';
|
|
6
|
+
|
|
7
|
+
const createCanvas = () =>
|
|
8
|
+
new Canvas(undefined, { width: 400, height: 400, renderOnAddRemove: false });
|
|
9
|
+
|
|
10
|
+
const event = (x: number, y: number, shiftKey = false) =>
|
|
11
|
+
({
|
|
12
|
+
clientX: x,
|
|
13
|
+
clientY: y,
|
|
14
|
+
button: 0,
|
|
15
|
+
shiftKey,
|
|
16
|
+
}) as any;
|
|
17
|
+
|
|
18
|
+
const dragSelect = (
|
|
19
|
+
canvas: Canvas,
|
|
20
|
+
from: [number, number],
|
|
21
|
+
to: [number, number],
|
|
22
|
+
shiftKey = false,
|
|
23
|
+
) => {
|
|
24
|
+
canvas.__onMouseDown(event(from[0], from[1], shiftKey));
|
|
25
|
+
canvas.__onMouseMove(event((from[0] + to[0]) / 2, (from[1] + to[1]) / 2, shiftKey));
|
|
26
|
+
canvas.__onMouseMove(event(to[0], to[1], shiftKey));
|
|
27
|
+
canvas.__onMouseUp(event(to[0], to[1], shiftKey));
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const makeRect = (left: number, top: number) =>
|
|
31
|
+
new Rect({ left, top, width: 20, height: 20 });
|
|
32
|
+
|
|
33
|
+
describe('additive box selection', () => {
|
|
34
|
+
it('keeps the previous selection and adds the collected objects when the selection key is held', () => {
|
|
35
|
+
const canvas = createCanvas();
|
|
36
|
+
const a = makeRect(10, 10);
|
|
37
|
+
const b = makeRect(100, 100);
|
|
38
|
+
canvas.add(a, b);
|
|
39
|
+
|
|
40
|
+
canvas.setActiveObject(a);
|
|
41
|
+
dragSelect(canvas, [90, 90], [200, 200], true);
|
|
42
|
+
|
|
43
|
+
const active = canvas.getActiveObject();
|
|
44
|
+
expect(active).toBeInstanceOf(ActiveSelection);
|
|
45
|
+
expect(canvas.getActiveObjects()).toEqual([a, b]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('still replaces the selection when the selection key is not held', () => {
|
|
49
|
+
const canvas = createCanvas();
|
|
50
|
+
const a = makeRect(10, 10);
|
|
51
|
+
const b = makeRect(100, 100);
|
|
52
|
+
canvas.add(a, b);
|
|
53
|
+
|
|
54
|
+
canvas.setActiveObject(a);
|
|
55
|
+
dragSelect(canvas, [90, 90], [200, 200]);
|
|
56
|
+
|
|
57
|
+
expect(canvas.getActiveObjects()).toEqual([b]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('leaves the selection untouched when the box collects nothing', () => {
|
|
61
|
+
const canvas = createCanvas();
|
|
62
|
+
const a = makeRect(10, 10);
|
|
63
|
+
canvas.add(a);
|
|
64
|
+
|
|
65
|
+
canvas.setActiveObject(a);
|
|
66
|
+
dragSelect(canvas, [200, 200], [300, 300], true);
|
|
67
|
+
|
|
68
|
+
expect(canvas.getActiveObjects()).toEqual([a]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('only collects the first level objects of the canvas root', () => {
|
|
72
|
+
const canvas = createCanvas();
|
|
73
|
+
const child = makeRect(20, 20);
|
|
74
|
+
const group = new Group([child], { left: 0, top: 0 });
|
|
75
|
+
canvas.add(group);
|
|
76
|
+
|
|
77
|
+
// box covering only the child, which lives inside the group
|
|
78
|
+
dragSelect(canvas, [15, 15], [35, 35]);
|
|
79
|
+
expect(canvas.getActiveObjects()).toEqual([group]);
|
|
80
|
+
|
|
81
|
+
// the same box adds the group to the current selection
|
|
82
|
+
dragSelect(canvas, [15, 15], [35, 35], true);
|
|
83
|
+
expect(canvas.getActiveObjects()).toEqual([group]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('collects the children of the isolated group instead of the canvas root', () => {
|
|
87
|
+
const canvas = createCanvas();
|
|
88
|
+
const child = makeRect(20, 20);
|
|
89
|
+
const other = makeRect(200, 200);
|
|
90
|
+
const group = new Group([child, other], { left: 0, top: 0 });
|
|
91
|
+
const rootRect = makeRect(30, 30);
|
|
92
|
+
canvas.add(group, rootRect);
|
|
93
|
+
|
|
94
|
+
canvas.isolatedObject = group;
|
|
95
|
+
dragSelect(canvas, [10, 10], [60, 60]);
|
|
96
|
+
|
|
97
|
+
// `rootRect` is above the group, but out of the isolated scope
|
|
98
|
+
expect(canvas.getActiveObjects()).toEqual([child]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('drops selected objects that are outside of the current scope', () => {
|
|
102
|
+
const canvas = createCanvas();
|
|
103
|
+
const child = makeRect(20, 20);
|
|
104
|
+
const group = new Group([child], { left: 0, top: 0 });
|
|
105
|
+
const rootRect = makeRect(100, 100);
|
|
106
|
+
canvas.add(group, rootRect);
|
|
107
|
+
|
|
108
|
+
// child is selected while the group is isolated
|
|
109
|
+
canvas.isolatedObject = group;
|
|
110
|
+
canvas.setActiveObject(child);
|
|
111
|
+
|
|
112
|
+
// back to the root scope, the box adds the root rect only
|
|
113
|
+
canvas.isolatedObject = null;
|
|
114
|
+
dragSelect(canvas, [90, 90], [200, 200], true);
|
|
115
|
+
|
|
116
|
+
expect(canvas.getActiveObjects()).toEqual([rootRect]);
|
|
117
|
+
});
|
|
118
|
+
});
|