Skip to content

Commit b3634a8

Browse files
authored
Merge pull request #297 from ycloud/master
perf: Restrict drag area after canvas is scaled down
2 parents 4da2238 + 438ede9 commit b3634a8

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/mouse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default function (mind: MindElixirInstance) {
5959
mind.map.addEventListener('mousemove', e => {
6060
// click trigger mousemove in windows chrome
6161
if ((e.target as HTMLElement).contentEditable !== 'true') {
62-
dragMoveHelper.onMove(e, mind.container)
62+
dragMoveHelper.onMove(e, mind)
6363
}
6464
})
6565
mind.map.addEventListener('mousedown', e => {

src/utils/dragMoveHelper.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
import type { MindElixirInstance } from '../types/index'
2+
13
export default {
24
moved: false, // diffrentiate click and move
35
mousedown: false,
4-
onMove(e: MouseEvent, container: HTMLElement) {
6+
onMove(e: MouseEvent, mind: MindElixirInstance) {
57
if (this.mousedown) {
68
this.moved = true
79
const deltaX = e.movementX
810
const deltaY = e.movementY
9-
container.scrollTo(container.scrollLeft - deltaX, container.scrollTop - deltaY)
11+
const { container, map, scaleVal } = mind
12+
let scrollLeft = container.scrollLeft - deltaX
13+
let scrollTop = container.scrollTop - deltaY
14+
if (scaleVal < 1) {
15+
const minScrollLeft = (container.scrollWidth - map.clientWidth * scaleVal) / 2
16+
const minScrollTop = (container.scrollHeight - map.clientHeight * scaleVal) / 2
17+
scrollLeft = Math.max(minScrollLeft, Math.min(container.scrollWidth - minScrollLeft - container.clientWidth, scrollLeft))
18+
scrollTop = Math.max(minScrollTop, Math.min(container.scrollHeight - minScrollTop - container.clientHeight, scrollTop))
19+
}
20+
container.scrollTo(scrollLeft, scrollTop)
1021
}
1122
},
1223
clear() {

0 commit comments

Comments
 (0)