Skip to content

Commit f1871d3

Browse files
authored
feat: 对象锁定状态下禁止被多选 (#551)
1 parent a7d487c commit f1871d3

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

packages/core/plugin/LockPlugin.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,55 @@ export default class LockPlugin implements IPluginTempl {
9191
});
9292
this.canvas.on('selection:created', () => this.renderCornerByActiveObj());
9393
this.canvas.on('selection:updated', () => this.renderCornerByActiveObj());
94+
95+
// 鼠标框选不能多选锁定元素
96+
(fabric.Canvas.prototype as any)._groupSelectedObjects = function (e: any) {
97+
const group = this._collectObjects(e);
98+
let aGroup;
99+
100+
for (let i = group.length - 1; i >= 0; i--) {
101+
if (group[i].lockMovementX) {
102+
group.splice(i, 1);
103+
}
104+
}
105+
106+
// do not create group for 1 element only
107+
if (group.length === 1) {
108+
this.setActiveObject(group[0], e);
109+
} else if (group.length > 1) {
110+
aGroup = new fabric.ActiveSelection(group.reverse(), {
111+
canvas: this,
112+
});
113+
this.setActiveObject(aGroup, e);
114+
}
115+
};
116+
117+
// shift+左键点选不能多选锁定元素
118+
(fabric.Canvas.prototype as any)._handleGrouping = function (e: any, target: fabric.Object) {
119+
const activeObject = this._activeObject;
120+
// avoid multi select when shift click on a corner
121+
if (activeObject.__corner) {
122+
return;
123+
}
124+
125+
if (target.lockMovementX) return;
126+
if (activeObject.lockMovementX) return;
127+
128+
if (target === activeObject) {
129+
// if it's a group, find target again, using activeGroup objects
130+
target = this.findTarget(e, true);
131+
// if even object is not found or we are on activeObjectCorner, bail out
132+
if (!target || !target.selectable) {
133+
return;
134+
}
135+
if (target.lockMovementX) return;
136+
}
137+
if (activeObject && activeObject.type === 'activeSelection') {
138+
this._updateActiveSelection(target, e);
139+
} else {
140+
this._createActiveSelection(target, e);
141+
}
142+
};
94143
}
95144

96145
controlCornersVisible(obj: fabric.Object) {

0 commit comments

Comments
 (0)