Procházet zdrojové kódy

【综合】进入文档,转换注释优化

tangchao před 6 měsíci
rodič
revize
0bc3962015

+ 86 - 40
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+UI.swift

@@ -137,54 +137,100 @@ extension KMMainViewController {
         }
     }
     
-    func convertNotesUsingPDFDocument(_ pdfDocument: CPDFDocument) {
-        beginProgressSheet(withMessage: NSLocalizedString("Converting notes", comment: "Message for progress sheet").appending("..."), maxValue: 0)
-        
-        let count = pdfDocument.pageCount
-        let pdfView = listView
+    func showConvertNotesProgress() {
+        guard let pdfDocument = self.document else {
+            return
+        }
+        guard let _ = self.listView?.document else {
+            return
+        }
+        guard let _ = self.view.window else {
+            return
+        }
         
-        for i in 0..<count {
-            let page = pdfDocument.page(at: i)
-            
-            var addAnnotations = [CPDFAnnotation]()
-            var removeAnnotations = [CPDFAnnotation]()
+        self.model.needConvertNotes = false
+        Task { @MainActor in
+            self.convertNotesUsingPDFDocument(pdfDocument)
+        }
+    }
+    
+    func convertNotesUsingPDFDocument(_ pdfDocument: CPDFDocument) {
+        guard let doc = self.listView?.document else {
+            return
+        }
+        DispatchQueue.main.async {
+            self.beginProgressSheet(withMessage: NSLocalizedString("Converting notes", comment: "Message for progress sheet").appending("..."), maxValue: 0)
             
-            for annotation in page?.annotations ?? [] {
-                var newAnnotation: CPDFAnnotation?
-                if let inkAnnotation = annotation as? CPDFInkAnnotation, inkAnnotation.contents.hasPrefix("<?xml version=\"1.0\" encoding=\"utf-8\"?>") {
-                    let table = KMTableAnnotation(KMNoteBounds: inkAnnotation.bounds, document: pdfView!.document)
-                    table.border = inkAnnotation.border
-                    table.color = inkAnnotation.color
-                    table.createForm(withList: inkAnnotation.contents, andPaths: inkAnnotation.bezierPaths())
-                    table.updateAppearanceInk(withIsAdd: false)
-                    table.contents = annotation.contents
-                    newAnnotation = table
-                }
-                if let newAnnotation = newAnnotation {
-                    addAnnotations.append(newAnnotation)
-                    removeAnnotations.append(annotation)
+            let count = pdfDocument.pageCount
+            DispatchQueue.global().async {
+//                var addAnnotations = [CPDFAnnotation]()
+//                var removeAnnotations = [CPDFAnnotation]()
+                self.model.addAnnotations.removeAll()
+                self.model.removeAnnotations.removeAll()
+                for i in 0..<count {
+                    let page = pdfDocument.page(at: i)
+                    
+                    //                var addAnnotations = [CPDFAnnotation]()
+                    //                var removeAnnotations = [CPDFAnnotation]()
+                    for annotation in page?.annotations ?? [] {
+                        var newAnnotation: CPDFAnnotation?
+                        if let inkAnnotation = annotation as? CPDFInkAnnotation, inkAnnotation.contents.hasPrefix("<?xml version=\"1.0\" encoding=\"utf-8\"?>") {
+                            let table = KMTableAnnotation(KMNoteBounds: inkAnnotation.bounds, document: doc)
+                            table.border = inkAnnotation.border
+                            table.color = inkAnnotation.color
+                            table.createForm(withList: inkAnnotation.contents, andPaths: inkAnnotation.bezierPaths())
+                            table.updateAppearanceInk(withIsAdd: false)
+                            table.contents = annotation.contents
+                            newAnnotation = table
+                        }
+                        if let newAnnotation = newAnnotation {
+                            self.model.addAnnotations.append(newAnnotation)
+                            self.model.removeAnnotations.append(annotation)
+                        }
+                    }
+                    
+                    //                for i in 0..<addAnnotations.count {
+                    //                    let newAnnotation = addAnnotations[i]
+                    //                    let annotation = removeAnnotations[i]
+                    //
+                    //                    // this is only to make sure markup annotations generate the lineRects, for thread safety
+                    //                    pdfView?.addAnnotation(with: newAnnotation, to: page)
+                    //                    pdfView?.remove(annotation)
+                    //                    if newAnnotation.contents != nil {
+                    //                        if newAnnotation.contents.count == 0 {
+                    //                            newAnnotation.autoUpdateString()
+                    //                        }
+                    //                    }
+                    //                }
                 }
-            }
-            
-            for i in 0..<addAnnotations.count {
-                let newAnnotation = addAnnotations[i]
-                let annotation = removeAnnotations[i]
                 
-                // this is only to make sure markup annotations generate the lineRects, for thread safety
-                pdfView?.addAnnotation(with: newAnnotation, to: page)
-                pdfView?.remove(annotation)
-                if newAnnotation.contents != nil {
-                    if newAnnotation.contents.count == 0 {
-                        newAnnotation.autoUpdateString()
+                DispatchQueue.main.async {
+                    for i in 0..<self.model.addAnnotations.count {
+                        let newAnnotation = self.model.addAnnotations[i]
+                        let annotation = self.model.removeAnnotations[i]
+                        
+                        let page = annotation.page
+                        // this is only to make sure markup annotations generate the lineRects, for thread safety
+                        self.listView?.addAnnotation(with: newAnnotation, to: page)
+                        self.listView?.remove(annotation)
+                        if newAnnotation.contents != nil {
+                            if newAnnotation.contents.count == 0 {
+                                newAnnotation.autoUpdateString()
+                            }
+                        }
                     }
+                    
+                    self.dismissProgressSheet()
+                    
+                    self.listView?.undoManager?.removeAllActions()
+                    self.undoManager?.removeAllActions()
+                    
+                    // 清空数据
+                    self.model.addAnnotations.removeAll()
+                    self.model.removeAnnotations.removeAll()
                 }
             }
         }
-        
-        dismissProgressSheet()
-        
-        pdfView?.undoManager?.removeAllActions()
-        undoManager?.removeAllActions()
     }
     
     // MARK: - KMInterfaceThemeChangedProtocol

+ 12 - 2
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController.swift

@@ -273,6 +273,9 @@ import Cocoa
         }
         
         if (self.document?.isLocked == false) {
+            if self.model.needConvertNotes {
+                self.showConvertNotesProgress()
+            }
             return
         }
         if (self.view.window == nil) {
@@ -281,6 +284,9 @@ import Cocoa
         if (self.model.password != nil) {
             if self.listView.document.unlock(withPassword: self.model.password) {
                 self.model.isSaveKeyChain = false
+                if self.model.needConvertNotes {
+                    self.showConvertNotesProgress()
+                }
                 return
             }
         }
@@ -300,6 +306,10 @@ import Cocoa
             } else {
                 self.passwordWindow = nil
             }
+            
+            if self.model.needConvertNotes {
+                self.showConvertNotesProgress()
+            }
         }
     }
     
@@ -1927,8 +1937,8 @@ import Cocoa
             self.showSecureLimitTip()
         }
         
-        if self.document != nil {
-            self.convertNotesUsingPDFDocument(self.document!)
+        if self.model.needConvertNotes {
+            self.showConvertNotesProgress()
         }
         if (self._documentFirstLoad) {
             self.checkShouldAutoOpenLeftVC()

+ 5 - 0
PDF Office/PDF Master/Class/PDFWindowController/ViewController/Model/KMMainModel.swift

@@ -54,4 +54,9 @@ extension KMMainModel.Key {
     var markedPageIndex = NSNotFound
     var beforeMarkedPagePoint = NSPoint.zero
     var markedPagePoint = NSPoint.zero
+    
+    var needConvertNotes = true
+    
+    var addAnnotations: [CPDFAnnotation] = []
+    var removeAnnotations: [CPDFAnnotation] = []
 }