Bläddra i källkod

【菜单栏】需要通查是否支持Undo/Redo的功能,比如Annotate--删除所有注释无法支持(已修复)

tangchao 1 år sedan
förälder
incheckning
649c8967ee

+ 44 - 0
PDF Office/PDF Master/Class/PDFWindowController/Tools/KMPDFViewAnnotationOnceModeStore.swift

@@ -21,3 +21,47 @@ class KMPDFViewAnnotationOnceModeStore: KMMemorandumPattern<CPDFListView> {
         t.annotationType = self.annotationType
     }
 }
+
+class KMPDFViewRemoveAllAnnotationsStore: KMMemorandumPattern<CPDFListView> {
+    internal var annotations: [CPDFAnnotation] = []
+    internal var undoManager = UndoManager()
+    
+    override func store(t: CPDFListView) {
+        // 清空
+        self.annotations.removeAll()
+        
+        // 存储所有注释
+        for pageIdx in 0 ..< t.document.pageCount {
+            let page = t.document.page(at: pageIdx)
+            if let annotations = page?.annotations {
+                self.annotations.append(contentsOf: annotations)
+            }
+                
+            page?.removeAllAnnotations()
+        }
+        // 刷新UI
+        t.setNeedsDisplayForVisiblePages()
+        // undo
+        t.undoManager?.registerUndo(withTarget: self) { target in
+            target.restore(t: t)
+        }
+    }
+    
+    override func restore(t: CPDFListView) {
+        // 恢复所有注释
+        for anno in self.annotations {
+            if (anno.page.annotations.contains(anno)) {
+                continue
+            }
+            anno.page.addAnnotation(anno)
+        }
+        // 刷新UI
+        t.setNeedsDisplayForVisiblePages()
+        // 清空
+        self.annotations.removeAll()
+        // undo
+        t.undoManager?.registerUndo(withTarget: self) { target in
+            target.store(t: t)
+        }
+    }
+}

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

@@ -398,6 +398,7 @@ import Cocoa
         mianSplitView.subviews[1].frame = frame2
     }
     
+    internal var removeAllAnnotationsStore = KMPDFViewRemoveAllAnnotationsStore()
     internal func removeAllAnnotations() {
         let alert = NSAlert()
         alert.messageText = NSLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
@@ -408,11 +409,7 @@ import Cocoa
         }
         
         DispatchQueue.main.async {
-            for i in 0 ..< self.listView.document.pageCount {
-                let page = self.listView.document.page(at: i)
-                page?.removeAllAnnotations()
-            }
-            self.listView.setNeedsDisplayForVisiblePages()
+            self.removeAllAnnotationsStore.store(t: self.listView)
         }
     }