Browse Source

标记密文-新增擦除文档功能与调式

tangchao 2 years ago
parent
commit
c0f5699b21

+ 16 - 0
PDF Office/PDF Office.xcodeproj/project.pbxproj

@@ -1549,6 +1549,9 @@
 		BBA8B7B3293635D80097D183 /* KMPasswordInputWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = BBA8B7B2293635D80097D183 /* KMPasswordInputWindow.xib */; };
 		BBA8B7B4293635D80097D183 /* KMPasswordInputWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = BBA8B7B2293635D80097D183 /* KMPasswordInputWindow.xib */; };
 		BBA8B7B5293635D80097D183 /* KMPasswordInputWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = BBA8B7B2293635D80097D183 /* KMPasswordInputWindow.xib */; };
+		BBB14A532978DD5400936EDB /* KMRedactTools.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB14A522978DD5400936EDB /* KMRedactTools.swift */; };
+		BBB14A542978DD5400936EDB /* KMRedactTools.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB14A522978DD5400936EDB /* KMRedactTools.swift */; };
+		BBB14A552978DD5400936EDB /* KMRedactTools.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB14A522978DD5400936EDB /* KMRedactTools.swift */; };
 		BBC2BCC6295DA8F30036B983 /* KMCropPreviewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBC2BCC4295DA8F30036B983 /* KMCropPreviewController.swift */; };
 		BBC2BCC7295DA8F30036B983 /* KMCropPreviewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBC2BCC4295DA8F30036B983 /* KMCropPreviewController.swift */; };
 		BBC2BCC8295DA8F30036B983 /* KMCropPreviewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBC2BCC4295DA8F30036B983 /* KMCropPreviewController.swift */; };
@@ -2634,6 +2637,7 @@
 		BBA8B7A92935DC120097D183 /* KMRemovePasswordResultTipView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMRemovePasswordResultTipView.swift; sourceTree = "<group>"; };
 		BBA8B7AE293600D70097D183 /* KMPasswordInputWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMPasswordInputWindow.swift; sourceTree = "<group>"; };
 		BBA8B7B2293635D80097D183 /* KMPasswordInputWindow.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMPasswordInputWindow.xib; sourceTree = "<group>"; };
+		BBB14A522978DD5400936EDB /* KMRedactTools.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMRedactTools.swift; sourceTree = "<group>"; };
 		BBC2BCC4295DA8F30036B983 /* KMCropPreviewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMCropPreviewController.swift; sourceTree = "<group>"; };
 		BBC2BCC5295DA8F30036B983 /* KMCropPreviewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMCropPreviewController.xib; sourceTree = "<group>"; };
 		BBC347FC295448DE008D2CD1 /* KMWatermarkTemplateModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMWatermarkTemplateModel.swift; sourceTree = "<group>"; };
@@ -4384,6 +4388,7 @@
 		BB35C49C297535C800D46EE2 /* Redact */ = {
 			isa = PBXGroup;
 			children = (
+				BBB14A512978DD4200936EDB /* Tools */,
 				BB4EEF2A29763EC7003A3537 /* Base */,
 				BB4EEF2929763EC7003A3537 /* Window */,
 				BB35C49D297535C800D46EE2 /* View */,
@@ -4834,6 +4839,14 @@
 			path = Window;
 			sourceTree = "<group>";
 		};
+		BBB14A512978DD4200936EDB /* Tools */ = {
+			isa = PBXGroup;
+			children = (
+				BBB14A522978DD5400936EDB /* KMRedactTools.swift */,
+			);
+			path = Tools;
+			sourceTree = "<group>";
+		};
 		BBC2BCC2295DA8700036B983 /* Crop */ = {
 			isa = PBXGroup;
 			children = (
@@ -6539,6 +6552,7 @@
 				8942F817292B678100389627 /* KMAnnotationTableCellView.swift in Sources */,
 				9F1FE4F629406E4700E952CA /* CTFloatingBarBackingView.m in Sources */,
 				BB897256294C559F0045787C /* KMWatermarkPropertyCreateController.swift in Sources */,
+				BBB14A532978DD5400936EDB /* KMRedactTools.swift in Sources */,
 				89E4E6F52963D1E9002DBA6F /* KMAnnotationPropertiesColorManager.m in Sources */,
 				9FDD0F8C2952FCE0000C4DAD /* KMGlobalParser.swift in Sources */,
 				BB897251294C3F660045787C /* KMMenuTableView.swift in Sources */,
@@ -7020,6 +7034,7 @@
 				9F8539D32943121100DF644E /* KMSegmentedBox.swift in Sources */,
 				AD9527C3295294EF0039D2BC /* KMPrintPaperModel.swift in Sources */,
 				9F1FE4DC29406E4700E952CA /* NSURL+Utils.m in Sources */,
+				BBB14A542978DD5400936EDB /* KMRedactTools.swift in Sources */,
 				89E4E6F62963D1E9002DBA6F /* KMAnnotationPropertiesColorManager.m in Sources */,
 				F359916A29261F0E000D25DE /* CPDFListView+Tool.m in Sources */,
 				9F1FE4F729406E4700E952CA /* CTFloatingBarBackingView.m in Sources */,
@@ -7360,6 +7375,7 @@
 				BBEC00AA295BDECF00A26C98 /* KMHeaderFooterContentInfoView.swift in Sources */,
 				F37322FE292DF9410013862C /* CPDFWidgetAnnotation+PDFListView.m in Sources */,
 				ADCB9970292C65EE00B51A03 /* KMOCROperation.m in Sources */,
+				BBB14A552978DD5400936EDB /* KMRedactTools.swift in Sources */,
 				BBEC00CE295C31F900A26C98 /* KMBatesModel.swift in Sources */,
 				9FAAA340290FECA70046FFCE /* NSImage+QuickLook.swift in Sources */,
 				BB49ECF8293F44DC00C82CA2 /* KMConvertExcelWindowController.swift in Sources */,

BIN
PDF Office/PDF Office.xcodeproj/project.xcworkspace/xcuserdata/kdanmobile.xcuserdatad/UserInterfaceState.xcuserstate


+ 2 - 2
PDF Office/PDF Office.xcodeproj/xcuserdata/kdanmobile.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -270,8 +270,8 @@
             filePath = "PDF Office/Class/PDFWindowController/ViewController/KMMainViewController.swift"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "294"
-            endingLineNumber = "294"
+            startingLineNumber = "362"
+            endingLineNumber = "362"
             landmarkName = "pdfListViewMenuItemsEditing(at:for:menuItems:)"
             landmarkType = "7">
          </BreakpointContent>

+ 29 - 0
PDF Office/PDF Office/Class/PDFTools/Redact/Tools/KMRedactTools.swift

@@ -0,0 +1,29 @@
+//
+//  KMRedactTools.swift
+//  PDF Office
+//
+//  Created by tangchao on 2023/1/19.
+//
+
+import Cocoa
+
+class KMRedactTools: NSObject {
+    class func eraserDocument(_ document: CPDFDocument, callback: @escaping (_ result: Bool, _ errorAnno: CPDFAnnotation?) -> ()) {
+        for i in 0 ..< document.pageCount {
+            let page: CPDFPage = document.page(at: i)
+            for anno in page.annotations {
+                if ((anno.isKind(of: CPDFRedactAnnotation.self)) == false) {
+                    continue
+                }
+                
+                let result = page.erasureRedact(from: anno.bounds)
+                if (result == false) {
+                    callback(false, anno)
+                    return
+                }
+            }
+        }
+        
+        callback(true, nil)
+    }
+}

+ 4 - 83
PDF Office/PDF Office/Class/PDFWindowController/ViewController/KMMainViewController+Action.swift

@@ -512,92 +512,11 @@ extension KMMainViewController {
     }
     
     @objc func redact_menuItemClick_apply(sender: NSMenuItem?) {
-        let anno = self.listView.activeAnnotation
-        if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
-            return
-        }
-        
-        let windowController = KMRedactConfirmWindowController(.redactOne)
-        self.currentWindowController = windowController
-        self.view.window?.beginSheet(windowController.window!)
-        windowController.itemClick = { [weak self] index in
-            if (index == 2) { /// 取消
-                self?.view.window?.endSheet((self?.currentWindowController.window)!)
-                self?.currentWindowController = nil
-                return
-            }
-            
-            self?.view.window?.endSheet((self?.currentWindowController.window)!)
-            self?.currentWindowController = nil
-            
-            let panel = NSSavePanel()
-            panel.nameFieldStringValue = "[新文件]"+(self?.listView.document.documentURL.lastPathComponent)!
-            let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
-            button.state = .on
-            panel.accessoryView = button
-            panel.isExtensionHidden = true
-            panel.beginSheetModal(for: (self?.view.window!)!) { response in
-                if response != .OK {
-                    return
-                }
-                
-                (anno as! CPDFRedactAnnotation).applyRedaction()
-                self!.listView.document.write(to: panel.url)
-                
-                if (button.state == .on) {
-                    NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { document, alreadyOpen, error in
-                        
-                    }
-                } else {
-                    NSWorkspace.shared.activateFileViewerSelecting([panel.url!])
-                }
-            }
-        }
+        self.exeRedactConfirm(.redactOne) {}
     }
     
     @objc func redact_menuItemClick_clear(sender: NSMenuItem?) {
-        let anno = self.listView.activeAnnotation
-        if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
-            return
-        }
-        
-        let windowController = KMRedactConfirmWindowController(.eraserOne)
-        self.currentWindowController = windowController
-        self.view.window?.beginSheet(windowController.window!)
-        windowController.itemClick = { [weak self] index in
-            if (index == 2) { /// 取消
-                self?.view.window?.endSheet((self?.currentWindowController.window)!)
-                self?.currentWindowController = nil
-                return
-            }
-            
-            self?.view.window?.endSheet((self?.currentWindowController.window)!)
-            self?.currentWindowController = nil
-            
-            let panel = NSSavePanel()
-            panel.nameFieldStringValue = "[新文件]"+(self?.listView.document.documentURL.lastPathComponent)!
-            let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
-            button.state = .on
-            panel.accessoryView = button
-            panel.isExtensionHidden = true
-            panel.beginSheetModal(for: (self?.view.window!)!) { response in
-                if response != .OK {
-                    return
-                }
-                
-                let result = anno?.page.erasureRedact(from: anno!.bounds)
-                if (result == true) {
-                    self!.listView.document.write(to: panel.url)
-                }
-                if (button.state == .on) {
-                    NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { document, alreadyOpen, error in
-                        
-                    }
-                } else {
-                    NSWorkspace.shared.activateFileViewerSelecting([panel.url!])
-                }
-            }
-        }
+        self.exeRedactConfirm(.eraserOne) {}
     }
     
     @objc func redact_menuItemClick_paste(sender: NSMenuItem?) {
@@ -1114,9 +1033,11 @@ extension KMMainViewController : KMMainToolbarControllerDelegate {
                 return
             }
             if (index == 2) { /// 擦除
+                self.exeRedactConfirm(.eraserAll) {}
                 return
             }
             if (index == 3) { /// 应用
+                self.exeRedactConfirm(.redactAll) {}
                 return
             }
             if (index == 4) { /// 文本&图像

+ 68 - 0
PDF Office/PDF Office/Class/PDFWindowController/ViewController/KMMainViewController.swift

@@ -186,6 +186,74 @@ import Cocoa
         return editController
     }
     
+    // MARK: Redact 【标记密文】
+    func exeRedactConfirm(_ type: KMRedactConfirmType, callback: @escaping () -> ()?) {
+        let windowController = KMRedactConfirmWindowController(type)
+        self.currentWindowController = windowController
+        self.view.window?.beginSheet(windowController.window!)
+        windowController.itemClick = { [weak self] index in
+            if (index == 2) { /// 取消
+                self?.view.window?.endSheet((self?.currentWindowController.window)!)
+                self?.currentWindowController = nil
+                callback()
+                return
+            }
+            
+            self?.view.window?.endSheet((self?.currentWindowController.window)!)
+            self?.currentWindowController = nil
+            
+            let panel = NSSavePanel()
+            panel.nameFieldStringValue = "[新文件]"+(self?.listView.document.documentURL.lastPathComponent)!
+            let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
+            button.state = .on
+            panel.accessoryView = button
+            panel.isExtensionHidden = true
+            panel.beginSheetModal(for: (self?.view.window!)!) { response in
+                if response != .OK {
+                    callback()
+                    return
+                }
+                
+                if (type == .redactOne) {
+                    let anno = self!.listView.activeAnnotation
+                    if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
+                        callback()
+                        return
+                    }
+                    
+                    (anno as! CPDFRedactAnnotation).applyRedaction()
+                } else if (type == .redactAll) {
+                    self?.listView.document.applyRedactions()
+                } else if (type == .eraserOne) {
+                    let anno = self!.listView.activeAnnotation
+                    if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
+                        callback()
+                        return
+                    }
+                    
+                    anno?.page.erasureRedact(from: anno!.bounds)
+                } else if (type == .eraserAll) {
+                    KMRedactTools.eraserDocument((self?.listView.document)!) { result, errorAnno in
+                        if (result == false) {
+                            callback()
+                            return
+                        }
+                    }
+                }
+                
+                self!.listView.document.write(to: panel.url)
+                if (button.state == .on) {
+                    NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { document, alreadyOpen, error in
+                        
+                    }
+                } else {
+                    NSWorkspace.shared.activateFileViewerSelecting([panel.url!])
+                }
+                callback()
+            }
+        }
+    }
+    
     //MARK: - PDFListViewDelegate
     func pdfViewDocumentDidLoaded(_ pdfView: CPDFView!) {
         #if DEBUG