Browse Source

fix: Undo、Redo返回值为0时更新指针重新渲染整个页面;解密功能全量保存

wzl 7 months ago
parent
commit
86ce7047c4

+ 62 - 72
packages/core/src/editor/content_container.js

@@ -24,8 +24,6 @@ export class ContentContainer {
     this.tool = this.pageViewer.tool || ''
     this.color = this.pageViewer.color || ''
     this._selectedFrameIndex = -1
-    this.removedEditorList = [] // 已删除的对象列表
-    this.removedStaging = []
 
     this.onHandleTool = this.handleTool.bind(this)
     this.onKeydown = this.handleKeyDown.bind(this)
@@ -315,12 +313,6 @@ export class ContentContainer {
     this._selectedFrameIndex = index
   }
 
-  removeEditor (index) {
-    const removedEditor = this.frameEditorList.splice(index, 1)
-    this.removedEditorList.push(...removedEditor)
-    this.frameEditorList.forEach((editor, i) => editor.editAreaIndex = i)
-  }
-
   handleKeyDown (e) {
     const selectedEditor = this.frameEditorList[this._selectedFrameIndex]
     if (!selectedEditor || selectedEditor.state !== 1) return
@@ -339,14 +331,14 @@ export class ContentContainer {
       const canRedo = await this.messageHandler.sendWithPromise('CanRedo', this.editPagePtr)
       canRedo && this.redoList.push(lastUndo)
       
-      await this.updateFrameEditor(res, lastUndo)
+      await this.updateFrameEditor(res)
 
     } else if (op === 'redo') {
       const lastRedo = this.redoList.pop()
       const res = await this.messageHandler.sendWithPromise('Redo', lastRedo.editPagePtr)
       this.undoList.push(lastRedo)
 
-      await this.updateFrameEditor(res, lastRedo)
+      await this.updateFrameEditor(res)
     }
 
     // console.log('undoList: ', this.undoList)
@@ -363,7 +355,6 @@ export class ContentContainer {
     if (!canUndo) return
     this.undoList.push(data)
     this.redoList.length = 0
-    this.removedStaging = []
 
     this.eventBus.dispatch('changeOperateList', {
       undoListLength: this.undoList.length,
@@ -372,74 +363,73 @@ export class ContentContainer {
   }
 
   // undo/redo后,更新视图
-  async updateFrameEditor (res, lastRec) {
+  async updateFrameEditor (res) {
     const { editAreaPtr, editCharPlace } = res
-    // console.log(res, lastRec)
-
-    let editor = null
-    // let targetPtr = editAreaPtr || lastRec.editAreaPtr
-    let targetPtr = lastRec.editAreaPtr
-
-    // console.log('frameEditorList: ')
-    // this.frameEditorList.forEach(item => console.log(item.editAreaIndex, item.editAreaPtr))
-    // console.log('removedStaging: ')
-    // this.removedStaging.forEach(item => console.log(item.editAreaIndex, item.editAreaPtr))
-    // console.log('removedEditorList:')
-    // this.removedEditorList.forEach(item => console.log(item.editAreaIndex, item.editAreaPtr))
-
-    const toRemoved = this.removedStaging.find(item => item.editAreaPtr === targetPtr)
-    if (toRemoved) {
-      const lastStaging = this.removedStaging.pop()
-      editor = this.frameEditorList.find(item => item.editAreaPtr === lastStaging.editAreaPtr)
-      editor.remove(true)
-      editor.updateCanvasAfterUndoRedo(true)
-      
-      // console.log('获取页面所有指针:')
-      // const editAreaCount = await this.messageHandler.sendWithPromise('GetEditAreaCount', this.editPagePtr)
-      // for (let i = 0; i < editAreaCount; i++) {
-      //   const editAreaPtr = await this.messageHandler.sendWithPromise('GetEditArea', {
-      //     editPagePtr: this.editPagePtr,
-      //     index: i
-      //   })
-      //   console.log('index: ', i, ' ---- Area指针: ', editAreaPtr)
-      // }
+
+    const editor = this.frameEditorList.find(item => item.editAreaPtr === editAreaPtr)
+    if (editor) {
+      editor.updateCanvasAfterUndoRedo()
+      editor.type === 'text' && editCharPlace && editor.updateCursorLine(editCharPlace)
       return
     }
 
-    editor = this.frameEditorList.find(item => item.editAreaPtr === targetPtr)
-
-    // console.log('frameEditorList: ')
-    // this.frameEditorList.forEach(item => console.log(item.editAreaIndex, item.editAreaPtr))
-
-    // console.log('获取页面所有指针:')
-    // const editAreaCount = await this.messageHandler.sendWithPromise('GetEditAreaCount', this.editPagePtr)
-    // for (let i = 0; i < editAreaCount; i++) {
-    //   const editAreaPtr = await this.messageHandler.sendWithPromise('GetEditArea', {
-    //     editPagePtr: this.editPagePtr,
-    //     index: i
-    //   })
-    //   console.log('index: ', i, ' ---- Area指针: ', editAreaPtr)
-    // }
-
-    if (!editor) {
-      const removedEditorIndex = this.removedEditorList.findIndex(item => item.editAreaPtr === targetPtr)
-      if (removedEditorIndex === -1) return
-
-      editor = this.removedEditorList[removedEditorIndex]
-      this.removedStaging.push({
-        editAreaIndex: editor.editAreaIndex,
-        editAreaPtr: editor.editAreaPtr
+    const newFrameEditorList = []
+    const editAreaCount = await this.messageHandler.sendWithPromise('GetEditAreaCount', this.editPagePtr)
+
+    for (let i = 0; i < editAreaCount; i++) {
+      const editAreaPtr = await this.messageHandler.sendWithPromise('GetEditArea', {
+        editPagePtr: this.editPagePtr,
+        index: i
       })
-      this.removedEditorList.splice(removedEditorIndex, 1)
-      this.frameEditorList.splice(editor.editAreaIndex, 0, editor)
-      this.frameEditorList.forEach((editor, i) => editor.editAreaIndex = i)
+      if (!editAreaPtr) continue
+
+      const isTextPtr = await this.messageHandler.sendWithPromise('IsTextArea', editAreaPtr)
+      if (isTextPtr) {
+        const frameEditor = new TextEditor({
+          eventBus: this.eventBus,
+          contentContainer: this,
+          container: this.contentContainer,
+          pagePtr: this.pagePtr,
+          editPagePtr: this.editPagePtr,
+          editAreaPtr,
+          editAreaIndex: i,
+          viewport: this.viewport,
+          scale: this.scale,
+          messageHandler: this.messageHandler,
+          pageViewer: this.pageViewer,
+          hidden: this.tool === 'addImage'
+        })
+        newFrameEditorList.push(frameEditor)
+        continue
+      }
+
+      const isImagePtr = await this.messageHandler.sendWithPromise('IsImageArea', editAreaPtr)
+      if (isImagePtr) {
+        const frameEditor = new ImageEditor({
+          eventBus: this.eventBus,
+          contentContainer: this,
+          container: this.contentContainer,
+          pagePtr: this.pagePtr,
+          editPagePtr: this.editPagePtr,
+          editAreaPtr,
+          editAreaIndex: i,
+          viewport: this.viewport,
+          scale: this.scale,
+          messageHandler: this.messageHandler,
+          pageViewer: this.pageViewer,
+          $t: this.$t,
+          hidden: this.tool === 'addText'
+        })
+        newFrameEditorList.push(frameEditor)
+      }
     }
-    // console.log('要去做处理的Area: ', ' - index:', editor.editAreaIndex, ' - 指针:', editor.editAreaPtr)
-    // console.log('editor: ', editor.editAreaIndex, editor.editAreaPtr, editor)
 
-    !editAreaPtr && !lastRec.replacedImage && editor.remove(true)
-    editor.updateCanvasAfterUndoRedo(!editAreaPtr)
-    editor.type === 'text' && editCharPlace && editor.updateCursorLine(editCharPlace)
+    this.frameEditorList.forEach(editor => {
+      editor.updateCanvas(null, true)
+      editor.removeViewer()
+    })
+    this.frameEditorList.length = 0
+    this.frameEditorList = newFrameEditorList
   }
 
   handlePageChanging ({pageNumber}) {

+ 21 - 9
packages/core/src/editor/image_editor.js

@@ -1169,7 +1169,7 @@ export class ImageEditor {
   }
 
   // 删除区域
-  async handleDelete (notUndo) {
+  async handleDelete () {
     await this.handleOutside()
     
     this.removed = true
@@ -1183,15 +1183,26 @@ export class ImageEditor {
       editAreaPtr: this.editAreaPtr
     })
     
-    notUndo = notUndo instanceof PointerEvent ? false : notUndo
-    this.needUndoRec = !notUndo
+    this.needUndoRec = true
     this.updateCanvas()
     this.contentContainer.removeEditor(this.editAreaIndex)
     this.eventBus.dispatch('contentPropertyChange', { type: 'image', isOpen: false })
   }
 
-  remove(notUndo) {
-    this.handleDelete(notUndo)
+  remove() {
+    this.handleDelete()
+  }
+
+  removeViewer () {
+    this.state = 0
+    // await this.handleOutside()
+    
+    this.removed = true
+    this.outerLineContainer.remove()
+    this.frameContainer.remove()
+    this.frameContainer = null
+    this.contentContainer.selectedFrameIndex = -1
+    this.eventBus.dispatch('contentPropertyChange', { type: 'image', isOpen: false })
   }
 
   // 上传图片并获取宽高
@@ -1465,7 +1476,7 @@ export class ImageEditor {
   }
 
   // undo/redo后 更新canvas图
-  async updateCanvasAfterUndoRedo(notRerender) {
+  async updateCanvasAfterUndoRedo() {
     await this.getRect()
 
     if (this.frameContainer && this.outerLineContainer) {
@@ -1480,9 +1491,6 @@ export class ImageEditor {
         width: this.rect.width + 'px',
         height: this.rect.height + 'px'
       })
-    } else if (!notRerender) {
-      this.removed = false
-      this.render()
     }
 
     const wholeAreaRect = this.getEntireArea(this.oldRect, this.rect)
@@ -1510,5 +1518,9 @@ export class ImageEditor {
     this.drawCanvas()
     this.saveEdit()
     this.updateOriginalImageUrl()
+
+    const opacity = await this.messageHandler.sendWithPromise('GetImageTransparency', this.editAreaPtr)
+    this.opacity = Math.round(opacity * 100) / 100
+    this.eventBus.dispatch('contentPropertyChange', { type: 'image', opacity: this.opacity * 100 })
   }
 }

+ 15 - 6
packages/core/src/editor/text_editor.js

@@ -1687,7 +1687,7 @@ export class TextEditor {
   }
 
   // 删除区域
-  async remove (notUndo) {
+  async remove () {
     this.state = 0
     await this.handleOutside()
 
@@ -1706,11 +1706,23 @@ export class TextEditor {
     //   editAreaPtr: this.editAreaPtr
     // })
 
-    this.needUndoRec = !notUndo
+    this.needUndoRec = true
     this.updateCanvas()
     this.contentContainer.removeEditor(this.editAreaIndex)
     this.eventBus.dispatch('contentPropertyChange', { type: 'text', isOpen: false })
   }
+  
+  removeViewer () {
+    this.state = 0
+    // await this.handleOutside()
+    
+    this.outerLine.remove()
+    this.frameContainer.remove()
+    this.frameContainer = null
+    this.contentContainer.selectedFrameIndex = -1
+    this.removed = true
+    this.eventBus.dispatch('contentPropertyChange', { type: 'text', isOpen: false })
+  }
 
   // 复制区域对象
   async copyArea () {
@@ -1756,7 +1768,7 @@ export class TextEditor {
   }
 
   // undo/redo后 更新canvas图
-  async updateCanvasAfterUndoRedo(notRerender) {
+  async updateCanvasAfterUndoRedo() {
     await this.getRect()
 
     if (this.frameContainer && this.outerLine) {
@@ -1771,9 +1783,6 @@ export class TextEditor {
         width: this.rect.width + 'px',
         height: this.rect.height + 'px'
       })
-    } else if (!notRerender) {
-      this.removed = false
-      this.render()
     }
 
     const wholeAreaRect = this.getEntireArea(this.oldRect, this.rect)

+ 1 - 1
packages/core/src/worker/compdfkit_worker.js

@@ -2970,7 +2970,7 @@ async function copyDocument(doc, password) {
 
 function saveDocument(doc, saveType) {
   DataArray = []
-  DataArray.push(ComPDFKitJS.opened_files[0])
+  saveType === 1 && DataArray.push(ComPDFKitJS.opened_files[0])
   Module._SaveDocumentByStream(doc, saveType)
   let pdfData = []
   for (let i = 0; i < DataArray.length; i++) {