From f1871d3f128e32498338757cb6f20c5c5f15536d Mon Sep 17 00:00:00 2001 From: qcjiamin <37877488+qcjiamin@users.noreply.github.com> Date: Thu, 19 Dec 2024 08:57:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=B9=E8=B1=A1=E9=94=81=E5=AE=9A?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=B8=8B=E7=A6=81=E6=AD=A2=E8=A2=AB=E5=A4=9A?= =?UTF-8?q?=E9=80=89=20(#551)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/plugin/LockPlugin.ts | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/packages/core/plugin/LockPlugin.ts b/packages/core/plugin/LockPlugin.ts index 97d07a60..c184480d 100644 --- a/packages/core/plugin/LockPlugin.ts +++ b/packages/core/plugin/LockPlugin.ts @@ -91,6 +91,55 @@ export default class LockPlugin implements IPluginTempl { }); this.canvas.on('selection:created', () => this.renderCornerByActiveObj()); this.canvas.on('selection:updated', () => this.renderCornerByActiveObj()); + + // 鼠标框选不能多选锁定元素 + (fabric.Canvas.prototype as any)._groupSelectedObjects = function (e: any) { + const group = this._collectObjects(e); + let aGroup; + + for (let i = group.length - 1; i >= 0; i--) { + if (group[i].lockMovementX) { + group.splice(i, 1); + } + } + + // do not create group for 1 element only + if (group.length === 1) { + this.setActiveObject(group[0], e); + } else if (group.length > 1) { + aGroup = new fabric.ActiveSelection(group.reverse(), { + canvas: this, + }); + this.setActiveObject(aGroup, e); + } + }; + + // shift+左键点选不能多选锁定元素 + (fabric.Canvas.prototype as any)._handleGrouping = function (e: any, target: fabric.Object) { + const activeObject = this._activeObject; + // avoid multi select when shift click on a corner + if (activeObject.__corner) { + return; + } + + if (target.lockMovementX) return; + if (activeObject.lockMovementX) return; + + if (target === activeObject) { + // if it's a group, find target again, using activeGroup objects + target = this.findTarget(e, true); + // if even object is not found or we are on activeObjectCorner, bail out + if (!target || !target.selectable) { + return; + } + if (target.lockMovementX) return; + } + if (activeObject && activeObject.type === 'activeSelection') { + this._updateActiveSelection(target, e); + } else { + this._createActiveSelection(target, e); + } + }; } controlCornersVisible(obj: fabric.Object) {