From a5978c0250b16be1a9fa4aa7c3973230ee65cfea Mon Sep 17 00:00:00 2001 From: cdswyda Date: Mon, 1 Nov 2021 14:39:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=9B=91=E5=90=AC=E7=B3=BB=E7=BB=9F=20?= =?UTF-8?q?Ctrl=20+=20+/-/0/wheel=20=E7=9A=84=E7=BC=A9=E6=94=BE=EF=BC=8C?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=88=B0=E8=A1=A8=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/zoom.js | 95 +++++++++++++++++++++++++++++++++++------ 1 file changed, 81 insertions(+), 14 deletions(-) diff --git a/src/controllers/zoom.js b/src/controllers/zoom.js index 711e3952a..54751da45 100644 --- a/src/controllers/zoom.js +++ b/src/controllers/zoom.js @@ -27,7 +27,7 @@ export function zoomChange(ratio){ "sheetIndex": Store.currentSheetIndex, }); } - + currentWheelZoom = null; Store.zoomRatio = ratio; let currentSheet = sheetmanage.getSheetByIndex(); @@ -77,9 +77,17 @@ export function zoomRefreshView(){ // $scrollTop.scrollTop(st+hc-hp); } - +let currentWheelZoom = null; export function zoomInitial(){ + // 缩放步长 + const ZOOM_WHEEL_STEP = 0.02; // ctrl + 鼠标滚轮 + const ZOOM_STEP = 0.1; // 点击以及 Ctrl + +- + + // 缩放最大最小比例 + const MAX_ZOOM_RATIO = 4; + const MIN_ZOOM_RATIO = .1; + $("#luckysheet-zoom-minus").click(function(){ let currentRatio; if(Store.zoomRatio==null){ @@ -89,14 +97,14 @@ export function zoomInitial(){ currentRatio = Math.ceil(Store.zoomRatio*10)/10; } - currentRatio = currentRatio-0.1; + currentRatio = currentRatio-ZOOM_STEP; if(currentRatio==Store.zoomRatio){ - currentRatio = currentRatio-0.1; + currentRatio = currentRatio-ZOOM_STEP; } - if(currentRatio<=0.1){ - currentRatio = 0.1; + if(currentRatio<=MIN_ZOOM_RATIO){ + currentRatio = MIN_ZOOM_RATIO; } // Store.zoomRatio = currentRatio; @@ -113,14 +121,14 @@ export function zoomInitial(){ currentRatio = Math.floor(Store.zoomRatio*10)/10; } - currentRatio = currentRatio+0.1; + currentRatio = currentRatio+ZOOM_STEP; if(currentRatio==Store.zoomRatio){ - currentRatio = currentRatio+0.1; + currentRatio = currentRatio+ZOOM_STEP; } - if(currentRatio>=4){ - currentRatio = 4; + if(currentRatio>=MAX_ZOOM_RATIO){ + currentRatio = MAX_ZOOM_RATIO; } // Store.zoomRatio = currentRatio; @@ -149,13 +157,13 @@ export function zoomInitial(){ let pos = cursorLeft + offsetX; let currentRatio = positionToRatio(pos); - if(currentRatio>4){ - currentRatio =4; + if(currentRatio>MAX_ZOOM_RATIO){ + currentRatio = MAX_ZOOM_RATIO; pos = 100; } - if(currentRatio<0.1){ - currentRatio =0.1; + if(currentRatio= MAX_ZOOM_RATIO) { + currentWheelZoom = MAX_ZOOM_RATIO; + } else if (currentWheelZoom < MIN_ZOOM_RATIO) { + currentWheelZoom = MIN_ZOOM_RATIO; + } + zoomChange(currentWheelZoom); + zoomNumberDomBind(currentWheelZoom); + ev.preventDefault(); + ev.stopPropagation(); + }, + { capture: true, passive: false } + ); + + // 拦截系统缩放快捷键 Ctrl + +/- 0 + document.addEventListener( + 'keydown', + function (ev) { + if (!ev.ctrlKey) { + return; + } + let handled = false; + let zoom = Store.zoomRatio || 1; + if (ev.key === '-' || ev.which === 189) { + zoom -= ZOOM_STEP; + handled = true; + } else if (ev.key === '+' || ev.which === 187) { + zoom += ZOOM_STEP; + handled = true; + } else if (ev.key === '0' || ev.which === 48) { + zoom = 1; + handled = true; + } + + if (handled) { + ev.preventDefault(); + if (zoom >= MAX_ZOOM_RATIO) { + zoom = MAX_ZOOM_RATIO; + } else if (zoom < MIN_ZOOM_RATIO) { + zoom = MIN_ZOOM_RATIO; + } + zoomChange(zoom); + zoomNumberDomBind(zoom); + } + }, + { capture: true } + ); }