Merge branch 'feature' of https://github.com/wanglin2/mind-map into feature

This commit is contained in:
街角小林 2023-12-18 09:12:24 +08:00
commit f3694d0c00
2 changed files with 46 additions and 41 deletions

View File

@ -128,22 +128,22 @@ class Event extends EventEmitter {
this.emit('mouseup', e, this) this.emit('mouseup', e, this)
} }
// 鼠标滚动 // 鼠标滚动/触控板滑动
onMousewheel(e) { onMousewheel(e) {
e.stopPropagation() e.stopPropagation()
e.preventDefault() e.preventDefault()
let dir const dirs = []
if (e.deltaY < 0) dir = CONSTANTS.DIR.UP if (e.deltaY < 0) dirs.push(CONSTANTS.DIR.UP)
if (e.deltaY > 0) dir = CONSTANTS.DIR.DOWN if (e.deltaY > 0) dirs.push(CONSTANTS.DIR.DOWN)
if (e.deltaX < 0) dir = CONSTANTS.DIR.LEFT if (e.deltaX < 0) dirs.push(CONSTANTS.DIR.LEFT)
if (e.deltaX > 0) dir = CONSTANTS.DIR.RIGHT if (e.deltaX > 0) dirs.push(CONSTANTS.DIR.RIGHT)
// 判断是否是触控板 // 判断是否是触控板
let isTouchPad = false let isTouchPad = false
// mac、windows // mac、windows
if (e.wheelDeltaY === e.deltaY * -3 || Math.abs(e.wheelDeltaY) <= 10) { if (e.wheelDeltaY === e.deltaY * -3 || Math.abs(e.wheelDeltaY) <= 10) {
isTouchPad = true isTouchPad = true
} }
this.emit('mousewheel', e, dir, this, isTouchPad) this.emit('mousewheel', e, dirs, this, isTouchPad)
} }
// 鼠标右键菜单事件 // 鼠标右键菜单事件

View File

@ -55,8 +55,8 @@ class View {
this.firstDrag = true this.firstDrag = true
}) })
// 放大缩小视图 // 放大缩小视图
this.mindMap.event.on('mousewheel', (e, dir, event, isTouchPad) => { this.mindMap.event.on('mousewheel', (e, dirs, event, isTouchPad) => {
let { const {
customHandleMousewheel, customHandleMousewheel,
mousewheelAction, mousewheelAction,
mouseScaleCenterUseMousePosition, mouseScaleCenterUseMousePosition,
@ -71,55 +71,61 @@ class View {
) { ) {
return customHandleMousewheel(e) return customHandleMousewheel(e)
} }
// 鼠标滚轮事件控制缩放 // 1.鼠标滚轮事件控制缩放
if (mousewheelAction === CONSTANTS.MOUSE_WHEEL_ACTION.ZOOM || e.ctrlKey) { if (mousewheelAction === CONSTANTS.MOUSE_WHEEL_ACTION.ZOOM || e.ctrlKey) {
if (disableMouseWheelZoom) return if (disableMouseWheelZoom) return
const { x: clientX, y: clientY } = this.mindMap.toPos( const { x: clientX, y: clientY } = this.mindMap.toPos(
e.clientX, e.clientX,
e.clientY e.clientY
) )
let cx = mouseScaleCenterUseMousePosition ? clientX : undefined const cx = mouseScaleCenterUseMousePosition ? clientX : undefined
let cy = mouseScaleCenterUseMousePosition ? clientY : undefined const cy = mouseScaleCenterUseMousePosition ? clientY : undefined
switch (dir) { // 如果来自触控板,那么过滤掉左右的移动
if (
isTouchPad &&
(dirs.includes(CONSTANTS.DIR.LEFT) ||
dirs.includes(CONSTANTS.DIR.RIGHT))
) {
dirs = dirs.filter(dir => {
return ![CONSTANTS.DIR.LEFT, CONSTANTS.DIR.RIGHT].includes(dir)
})
}
switch (true) {
// 鼠标滚轮,向上和向左,都是缩小 // 鼠标滚轮,向上和向左,都是缩小
case CONSTANTS.DIR.UP: case dirs.includes(CONSTANTS.DIR.UP || CONSTANTS.DIR.LEFT):
case CONSTANTS.DIR.LEFT:
mousewheelZoomActionReverse mousewheelZoomActionReverse
? this.enlarge(cx, cy, isTouchPad) ? this.enlarge(cx, cy, isTouchPad)
: this.narrow(cx, cy, isTouchPad) : this.narrow(cx, cy, isTouchPad)
break break
// 鼠标滚轮,向下和向右,都是放大 // 鼠标滚轮,向下和向右,都是放大
case CONSTANTS.DIR.DOWN: case dirs.includes(CONSTANTS.DIR.DOWN || CONSTANTS.DIR.RIGHT):
case CONSTANTS.DIR.RIGHT:
mousewheelZoomActionReverse mousewheelZoomActionReverse
? this.narrow(cx, cy, isTouchPad) ? this.narrow(cx, cy, isTouchPad)
: this.enlarge(cx, cy, isTouchPad) : this.enlarge(cx, cy, isTouchPad)
break break
} }
} else { } else {
// 鼠标滚轮事件控制画布移动 // 2.鼠标滚轮事件控制画布移动
let step = mousewheelMoveStep const step = isTouchPad ? 5 : mousewheelMoveStep
if (isTouchPad) { let mx = 0
step = 5 let my = 0
// 上移
if (dirs.includes(CONSTANTS.DIR.DOWN)) {
my = -step
} }
switch (dir) { // 下移
// 上移 if (dirs.includes(CONSTANTS.DIR.UP)) {
case CONSTANTS.DIR.DOWN: my = step
this.translateY(-step)
break
// 下移
case CONSTANTS.DIR.UP:
this.translateY(step)
break
// 右移
case CONSTANTS.DIR.LEFT:
this.translateX(-step)
break
// 左移
case CONSTANTS.DIR.RIGHT:
this.translateX(step)
break
} }
// 右移
if (dirs.includes(CONSTANTS.DIR.LEFT)) {
mx = step
}
// 左移
if (dirs.includes(CONSTANTS.DIR.RIGHT)) {
mx = -step
}
this.translateXY(mx, my)
} }
}) })
} }
@ -261,8 +267,7 @@ class View {
let drawWidth = rect.width / origTransform.scaleX let drawWidth = rect.width / origTransform.scaleX
let drawHeight = rect.height / origTransform.scaleY let drawHeight = rect.height / origTransform.scaleY
let drawRatio = drawWidth / drawHeight let drawRatio = drawWidth / drawHeight
let { width: elWidth, height: elHeight } = let { width: elWidth, height: elHeight } = this.mindMap.elRect
this.mindMap.elRect
elWidth = elWidth - fitPadding * 2 elWidth = elWidth - fitPadding * 2
elHeight = elHeight - fitPadding * 2 elHeight = elHeight - fitPadding * 2
let elRatio = elWidth / elHeight let elRatio = elWidth / elHeight