Skip to content

Commit 82959fd

Browse files
committed
feat(extension): 独占模式支持单击取消全选
1 parent 6498110 commit 82959fd

File tree

1 file changed

+22
-17
lines changed
  • packages/extension/src/components/selection-select

1 file changed

+22
-17
lines changed

packages/extension/src/components/selection-select/index.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export class SelectionSelect {
7272
}
7373
this.startPoint = undefined
7474
this.endPoint = undefined
75+
this.mouseDownInfo = null
7576

7677
// 移除事件监听
7778
document.removeEventListener('mousemove', this.draw)
@@ -134,9 +135,14 @@ export class SelectionSelect {
134135
// 禁用右键框选
135136
const isRightClick = e.button === 2
136137
if (isRightClick) return
137-
138138
// 清理之前可能存在的选区状态
139139
this.cleanupSelectionState()
140+
// 记录鼠标按下时的位置和时间
141+
this.mouseDownInfo = {
142+
x: e.clientX,
143+
y: e.clientY,
144+
time: Date.now(),
145+
}
140146

141147
// 记录原始设置并临时禁止画布移动
142148
this.originalStopMoveGraph = this.lf.getEditConfig().stopMoveGraph!
@@ -165,22 +171,6 @@ export class SelectionSelect {
165171
document.addEventListener('mouseup', this.drawOff)
166172
}
167173

168-
onToolContainerMouseUp = (e: MouseEvent) => {
169-
if (this.mouseDownInfo) {
170-
const { x, y, time } = this.mouseDownInfo
171-
const now = Date.now()
172-
// 用 mouseDown 和 mouseUp 的位置偏移及时间间隔来判断是否是点击事件
173-
const isClickEvent =
174-
Math.abs(e.clientX - x) < 10 &&
175-
Math.abs(e.clientY - y) < 10 &&
176-
now - time < 100
177-
if (isClickEvent) {
178-
this.lf.clearSelectElements()
179-
}
180-
this.mouseDownInfo = null
181-
}
182-
}
183-
184174
/**
185175
* 设置选中的灵敏度
186176
* @param isWholeEdge 是否要边的起点终点都在选区范围才算选中。默认true
@@ -259,6 +249,21 @@ export class SelectionSelect {
259249
}
260250
}
261251
private drawOff = (e: MouseEvent) => {
252+
// 处理鼠标抬起事件
253+
// 首先判断是否是点击,如果是,则清空框选
254+
if (this.mouseDownInfo) {
255+
const { x, y, time } = this.mouseDownInfo
256+
const isClick =
257+
Math.abs(e.clientX - x) < 5 &&
258+
Math.abs(e.clientY - y) < 5 &&
259+
Date.now() - time < 200
260+
if (isClick) {
261+
this.lf.clearSelectElements()
262+
this.cleanupSelectionState()
263+
return
264+
}
265+
}
266+
262267
const curStartPoint = cloneDeep(this.startPoint)
263268
const curEndPoint = cloneDeep(this.endPoint)
264269
document.removeEventListener('mousemove', this.draw)

0 commit comments

Comments
 (0)