Blame view

online-office/src/controllers/zoom.js 8.41 KB
wangtao authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
import Store from '../store';
import locale from '../locale/locale';
import { replaceHtml } from '../utils/util';
import sheetmanage from './sheetmanage';
import {changeSheetContainerSize} from './resize';
import { jfrefreshgrid_rhcw } from '../global/refresh';
import server from './server';
import luckysheetPostil from './postil';
import imageCtrl from './imageCtrl';



let luckysheetZoomTimeout = null;

export function zoomChange(ratio){
    if(Store.flowdata==null || Store.flowdata.length==0){
        return;
    }

    clearTimeout(luckysheetZoomTimeout);
    luckysheetZoomTimeout = setTimeout(() => {
        if (Store.clearjfundo) {
            Store.jfredo.push({ 
                "type": "zoomChange", 
                "zoomRatio": Store.zoomRatio, 
                "curZoomRatio": ratio, 
                "sheetIndex": Store.currentSheetIndex, 
            });
        }
        currentWheelZoom = null;
        Store.zoomRatio = ratio;

        let currentSheet = sheetmanage.getSheetByIndex();

        //批注
        luckysheetPostil.buildAllPs(currentSheet.data);

        //图片
        imageCtrl.images = currentSheet.images;
        imageCtrl.allImagesShow();
        imageCtrl.init();

        if(currentSheet.config==null){
            currentSheet.config = {};
        }
    
        if(currentSheet.config.sheetViewZoom==null){
            currentSheet.config.sheetViewZoom = {};
        }

        let type = currentSheet.config.curentsheetView;
        if(type==null){
            type = "viewNormal";
        }
        currentSheet.config.sheetViewZoom[type+"ZoomScale"] = ratio;
    
        server.saveParam("all", Store.currentSheetIndex, Store.zoomRatio, { "k": "zoomRatio" });
        server.saveParam("cg", Store.currentSheetIndex, currentSheet.config["sheetViewZoom"], { "k": "sheetViewZoom" });

        zoomRefreshView();
    }, 100);
    
}

export function zoomRefreshView(){
    // let $scrollLeft = $("#luckysheet-scrollbar-x"), $scrollTop = $("#luckysheet-scrollbar-y");
    // let sl = $scrollLeft.scrollLeft(), st = $scrollTop.scrollTop();

    // let wp = $scrollLeft.find("div").width(), hp = $scrollTop.find("div").height();

    jfrefreshgrid_rhcw(Store.flowdata.length, Store.flowdata[0].length);
    changeSheetContainerSize();

    // let wc = $scrollLeft.find("div").width(), hc = $scrollTop.find("div").height();

    // $scrollLeft.scrollLeft(sl+wc-wp);
    // $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){
            currentRatio = Store.zoomRatio = 1;
        }
        else{
            currentRatio = Math.ceil(Store.zoomRatio*10)/10;
        }

        currentRatio = currentRatio-ZOOM_STEP;

        if(currentRatio==Store.zoomRatio){
            currentRatio = currentRatio-ZOOM_STEP;
        }

        if(currentRatio<=MIN_ZOOM_RATIO){
            currentRatio = MIN_ZOOM_RATIO;
        }

        // Store.zoomRatio = currentRatio;
        zoomChange(currentRatio);
        zoomNumberDomBind(currentRatio);
    });

    $("#luckysheet-zoom-plus").click(function(){
        let currentRatio;
        if(Store.zoomRatio==null){
            currentRatio = Store.zoomRatio = 1;
        }
        else{
            currentRatio = Math.floor(Store.zoomRatio*10)/10;
        }

        currentRatio = currentRatio+ZOOM_STEP;

        if(currentRatio==Store.zoomRatio){
            currentRatio = currentRatio+ZOOM_STEP;
        }

        if(currentRatio>=MAX_ZOOM_RATIO){
            currentRatio = MAX_ZOOM_RATIO;
        }

        // Store.zoomRatio = currentRatio;
        zoomChange(currentRatio);
        zoomNumberDomBind(currentRatio);
    });

    $("#luckysheet-zoom-slider").mousedown(function(e){
        let xoffset = $(this).offset().left, pageX = e.pageX;

        let currentRatio = positionToRatio(pageX-xoffset);
        // Store.zoomRatio = currentRatio;
        zoomChange(currentRatio);
        zoomNumberDomBind(currentRatio);
    });

    $("#luckysheet-zoom-cursor").mousedown(function(e){
        let curentX = e.pageX,cursorLeft = parseFloat($("#luckysheet-zoom-cursor").css("left"));
        $("#luckysheet-zoom-cursor").css("transition","none");
        $(document).off("mousemove.zoomCursor").on("mousemove.zoomCursor",function(event){
            let moveX = event.pageX;
            let offsetX = moveX - curentX;
            // console.log(moveX, curentX, offsetX);
            // curentX = moveX;
            // let left = parseFloat($("#luckysheet-zoom-cursor").css("left"));
            let pos = cursorLeft + offsetX; 
            let currentRatio = positionToRatio(pos);

            if(currentRatio>MAX_ZOOM_RATIO){
                currentRatio = MAX_ZOOM_RATIO;
                pos = 100;
            }

            if(currentRatio<MIN_ZOOM_RATIO){
                currentRatio = MIN_ZOOM_RATIO;
                pos = 0;
            }

            // Store.zoomRatio = currentRatio;
            zoomChange(currentRatio);
            let r = Math.round(currentRatio*100) + "%";
            $("#luckysheet-zoom-ratioText").html(r);
            $("#luckysheet-zoom-cursor").css("left", pos-4);
        });

        $(document).off("mouseup.zoomCursor").on("mouseup.zoomCursor",function(event){
            $(document).off(".zoomCursor");
            $("#luckysheet-zoom-cursor").css("transition","all 0.3s");
        });

        e.stopPropagation();
    }).click(function(e){
        e.stopPropagation();
    });

    $("#luckysheet-zoom-ratioText").click(function(){
        // Store.zoomRatio = 1;
        zoomChange(1);
        zoomNumberDomBind(1);
    });

    zoomNumberDomBind(Store.zoomRatio);

    currentWheelZoom = null;
    // 拦截系统缩放快捷键 Ctrl + wheel
    document.addEventListener(
        'wheel',
        function (ev) {
            if (!ev.ctrlKey || !ev.deltaY) {
                return;
            }
            if (currentWheelZoom === null) {
                currentWheelZoom = Store.zoomRatio || 1;
            }
            currentWheelZoom += ev.deltaY < 0 ? ZOOM_WHEEL_STEP : -ZOOM_WHEEL_STEP;
            if (currentWheelZoom >= 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 }
    );
}

function positionToRatio(pos){
    let ratio = 1;
    if(pos<50){
        ratio = Math.round((pos*1.8/100 + 0.1)*100)/100;
    }
    else if(pos>50){
        ratio = Math.round(((pos-50)*6/100 + 1)*100)/100;
    }

    return ratio;
}

function zoomSlierDomBind(ratio){
    let domPos = 50;
    if(ratio<1){
        domPos = Math.round((ratio - 0.1)*100 / 0.18)/10;
    }
    else if(ratio>1){
        domPos = Math.round((ratio - 1)*100 / 0.6)/10+50;
    }
    $("#luckysheet-zoom-cursor").css("left", domPos-4);
}

export function zoomNumberDomBind(ratio){
    let r = Math.round(ratio*100) + "%";
    $("#luckysheet-zoom-ratioText").html(r);
    zoomSlierDomBind(ratio);
}