1
0
tangning 6 ساعت پیش
والد
کامیت
ea315e1853
3فایلهای تغییر یافته به همراه31 افزوده شده و 36 حذف شده
  1. 15 23
      src/util/index.ts
  2. 13 12
      src/view/case/photos/canvas-photo-editor.js
  3. 3 1
      src/view/case/photos/index.vue

+ 15 - 23
src/util/index.ts

@@ -57,30 +57,22 @@ export const debounce = <T extends (...args: any) => any>(
     }, delay);
   };
 };
-export const throttle = <Args extends any[]>(
-  fn: (...args: Args) => void,
-  deley: number
-) => {
-  let valib = false;
-  let lastCtx: { self: any; args: Args } | null = null;
-
-  return function (this: any, ...args: Args) {
-    lastCtx = {
-      args,
-      self: this,
-    };
-    if (valib) {
-      return;
+export function throttle<T extends (...args: any[]) => Promise<any> | any>(
+  func: T,
+  delay: number
+): T {
+  let lastTime = 0;
+
+  return function (...args: Parameters<T>) {
+    const now = Date.now();
+
+    // 立即执行模式:距离上次执行超过延迟时间就立刻跑
+    if (now - lastTime >= delay) {
+      lastTime = now;
+      return func.apply(this, args);
     }
-    const currentCtx = lastCtx;
-    valib = true;
-
-    setTimeout(() => {
-      fn.apply(currentCtx.self, currentCtx.args);
-      valib = false;
-    }, deley);
-  };
-};
+  } as T;
+}
 
 function randomWord(randomFlag: boolean, min: number, max?: number) {
   let str = "";

+ 13 - 12
src/view/case/photos/canvas-photo-editor.js

@@ -300,6 +300,10 @@ export class CanvasPhotoEditor {
     const rect = this.canvas.getBoundingClientRect()
     const mouseX = (e.clientX - rect.left - this.drawOffsetX) / this.scale
     const mouseY = (e.clientY - rect.top - this.drawOffsetY) / this.scale
+    if(this.dragPageData.drawing && this.indexingLineList.length){
+        await this.clearLineThrottle()
+        return
+    }
     if (this.indexing && drawing) {
       const PhotoIndex = this.getPhotoPositionInPage(e)
       console.log('PhotoIndex', PhotoIndex, this.tempArrow)
@@ -312,7 +316,6 @@ export class CanvasPhotoEditor {
       this.dragPageData.end = { x: mouseX, y: mouseY }
       let newEndIndex = endIndex<-1?-1:endIndex>this.pages.length-1?this.pages.length:endIndex
       if(newEndIndex != this.dragPageData.movedIndex){
-        await this.handleClearLine()
         this.dragPageData.movedIndex = newEndIndex
         this.drawAllPages()
       }
@@ -330,16 +333,13 @@ export class CanvasPhotoEditor {
     // 【关键】通过Canvas临时绘制偏移效果(替代重绘),避免拖拽时画面静止
     this.drawAllPages()
   }
-   handleClearLine(){
-    throttle(async ()=>{
-      if(this.indexingLineList.length){
-        await this.checkIndexing('修改页面排序将会清除所有标引是否继续?')
-        this.drawAllPages()
-        return
-      }
-    }, 2000)
-
-  }
+  // 先把节流函数定义在外面(关键!)
+  clearLineThrottle = throttle(async () => {
+    if (this.indexingLineList.length) {
+      await this.checkIndexing('修改页面排序将会清除所有标引是否继续?')
+      this.drawAllPages()
+    }
+  }, 1000, {leading: true, trailing: false})
 
   async handleMouseUp(e) {
     this.isDragging = false
@@ -351,6 +351,7 @@ export class CanvasPhotoEditor {
       this.pages = newPagelist;
       this.dragPageData.drawing = false//退出page编辑模式
       this.dragPageData.movedIndex = -1
+      this.saveHistory()
       this.drawAllPages()
     }
     this.dragPageData.drawing = false
@@ -1084,8 +1085,8 @@ export class CanvasPhotoEditor {
       layoutMode: this.layoutMode, //页码布局类型
       // coordinate: this.getCoordinate(pageX, layout), //坐标信息
     }
-    this._pages = this._pages.map(ele =>({...ele, layoutMode: this.layoutMode, }))//coordinate: this.getCoordinate(pageX, layout)
     if(selectedPhotos.length == 0){
+      this._pages = this._pages.map(ele =>({...ele, layoutMode: this.layoutMode, }))//coordinate: this.getCoordinate(pageX, layout)
       let list = []
       const newArr = [...newList, ...selectedPhotos]
       newArr.forEach((photoId, photoIndex) => {

+ 3 - 1
src/view/case/photos/index.vue

@@ -589,7 +589,9 @@ const deleteSelectedPage = async () => {
 };
 const handleKeyDown = (e) => {
   console.log("handleKeyDown", e);
-
+  if(selectedPageIndex.value === -1){
+    return
+  }
   if (e.key === "Delete" || e.key === "Backspace") {
     e.preventDefault();
     deleteSelectedPage();