Browse Source

【2025】【Edit】编辑数据时, 点击工具栏按钮优先判断是否要保存修改逻辑处理。

niehaoyu 4 weeks ago
parent
commit
ae565629ee

+ 43 - 18
PDF Office/PDF Master/KMClass/KMPDFViewController/EditTool/Background/Views/KMBackgroundPropertyController.swift

@@ -343,6 +343,44 @@ class KMBackgroundPropertyController: NSViewController {
         
     }
     
+    func isOriginalDataDictChanged() -> Bool {
+        var isChanged = false
+        if let dict = self.originalDataDict {
+            isChanged = KMBackgroundManager.compareIsChangedModel(backgroundData, withDict: dict as NSDictionary)
+        }
+        return isChanged
+    }
+    
+    func showSaveEditChangeAlert(completion: @escaping (NSApplication.ModalResponse) -> Void) {
+        let alert = NSAlert()
+        alert.alertStyle = .critical
+        alert.messageText = KMLocalizedString("Save template changes?")
+        alert.informativeText = KMLocalizedString("Cancel and they will not be saved.")
+        alert.addButton(withTitle: KMLocalizedString("Save"))
+        alert.addButton(withTitle: KMLocalizedString("Cancel"))
+        if let window = self.view.window {
+            alert.beginSheetModal(for: window) { response in
+                completion(response)
+            }
+        }
+    }
+    
+    func cancelDataEdit() {
+        if let dict = self.originalDataDict {
+            KMBackgroundManager.defaultManager.updateModel(self.backgroundData, withDict: dict as NSDictionary)
+            
+            let _ = KMBackgroundManager.defaultManager.updateTemplate(model: self.backgroundData)
+        }
+        self.delegate?.backgroundPropertyControllerFinishTemplateEdit?(self)
+    }
+    
+    func saveDataEdit() {
+        let _ = KMBackgroundManager.defaultManager.updateTemplate(model: self.backgroundData)
+        
+        self.delegate?.backgroundPropertyControllerFinishTemplateEdit?(self)
+        
+    }
+     
     //MARK: - Action
     @objc func leftTopButtonClicked(_ sender: ComponentButton) {
         if sender == leftTopButton {
@@ -352,26 +390,13 @@ class KMBackgroundPropertyController: NSViewController {
             }
             
             if isChanged == true {
-                let alert = NSAlert()
-                alert.alertStyle = .critical
-                alert.messageText = KMLocalizedString("Save template changes?")
-                alert.informativeText = KMLocalizedString("Cancel and they will not be saved.")
-                alert.addButton(withTitle: KMLocalizedString("Save"))
-                alert.addButton(withTitle: KMLocalizedString("Cancel"))
-                alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
+                self.showSaveEditChangeAlert { response in
                     if response == NSApplication.ModalResponse.alertFirstButtonReturn {
-                        let _ = KMBackgroundManager.defaultManager.updateTemplate(model: self.backgroundData)
-                        
-                        self.delegate?.backgroundPropertyControllerFinishTemplateEdit?(self)
-                        
+                        //OK
+                        self.saveDataEdit()
                     } else {
-                        if let dict = self.originalDataDict {
-                            KMBackgroundManager.defaultManager.updateModel(self.backgroundData, withDict: dict as NSDictionary)
-                            
-                            let _ = KMBackgroundManager.defaultManager.updateTemplate(model: self.backgroundData)
-                        }
-                        self.delegate?.backgroundPropertyControllerFinishTemplateEdit?(self)
-                        
+                        //Cancel
+                        self.cancelDataEdit()
                     }
                 }
             } else {

+ 115 - 48
PDF Office/PDF Master/KMClass/KMPDFViewController/KMMainViewController.swift

@@ -4888,66 +4888,70 @@ extension KMMainViewController: KMEditToolbarViewDelegate {
             pageString = String(format: "%ld", listView.currentPageIndex)
         }
         if view.editType == .watermark {
-            if let model = watermarkViewController?.currentWatermarkData {
-                let watermark = KMWatermarkModel.returnWaterMarkWith(model, listView.document)
-                watermark.pageString = pageString
-                listView.document.addWatermark(watermark)
-                listView.layoutDocumentView()
+            if view.editSubType == .edit {
                 
-                self.recordIsPDFDocumentEdited()
-            }
-            exitEditToolbarView()
-        } else if view.editType == .background {
-            if let model = backgroundViewController?.backgroundModel {
-                if let background = listView.document.background() {
-                    KMBackgroundManager.defaultManager.updateBackground(background, withModel: model)
-                    background.pageString = pageString
-                    background.update()
-                    
-                    listView.document?.refreshPageData()
-                    listView.layoutDocumentView()
-                    
-                    self.recordIsPDFDocumentEdited()
-                }
+            } else {
+                self.applyWatermarkInfo(pageString)
+            }
+        } else if view.editType == .background, let propertyVC = backgroundViewController?.propertyController {
+            if view.editSubType == .edit && backgroundViewController?.propertyController.isOriginalDataDictChanged() == true {
+                propertyVC.showSaveEditChangeAlert(completion: { response in
+                    if response == NSApplication.ModalResponse.alertFirstButtonReturn {
+                        //OK
+                        propertyVC.saveDataEdit()
+                        
+                        self.applyBackgroundInfo(pageString)
+                    } else {
+                        //Cancel
+                        propertyVC.cancelDataEdit()
+                    }
+                })
+            } else {
+                self.applyBackgroundInfo(pageString)
             }
-            exitEditToolbarView()
-            
         } else if view.editType == .header_Footer {
-            if let model = headerFooterViewController?.headerFooterModel {
-                if let headerFooter = listView.document.headerFooter() {
-                    KMHeaderFooterManager.defaultManager.updateCPDFHeaderFooter(headerFooter, withModel: model, Int(listView.document.pageCount))
-                    headerFooter.pageString = pageString
-                    headerFooter.update()
-                    
-                    listView.document?.refreshPageData()
-                    listView.layoutDocumentView()
-                    
-                    self.recordIsPDFDocumentEdited()
-                }
+            if view.editSubType == .edit {
                 
+            } else {
+                self.applyHeaderFooterInfo(pageString)
             }
-            
-            exitEditToolbarView()
-            
         } else if view.editType == .bates {
-            if let model = batesViewController?.batesModel {
-                if let bates = listView.document.bates() {
-                    KMBatesManager.defaultManager.updateCPDFBates(bates, withModel: model, Int(listView.document.pageCount))
-                    bates.pageString = pageString
-                    bates.update()
-                    
-                    listView.document?.refreshPageData()
-                    listView.layoutDocumentView()
-                    
-                    self.recordIsPDFDocumentEdited()
-                }
+            if view.editSubType == .edit {
+                
+            } else {
+                self.applyBatesInfo(pageString)
             }
-            exitEditToolbarView() 
         }
         
     }
     
     func kmEditToolbarViewDidChooseExit(_ view: KMEditToolbarView) {
+        //在编辑数据时,先判断是否要保存数据。
+        
+        if view.editType == .watermark {
+
+        } else if view.editType == .background, view.editSubType == .edit, let propertyVC = backgroundViewController?.propertyController, propertyVC.isOriginalDataDictChanged() == true {
+            propertyVC.showSaveEditChangeAlert { response in
+                if response == NSApplication.ModalResponse.alertFirstButtonReturn {
+                    //OK
+                    propertyVC.saveDataEdit()
+                    
+                    self.kmEditToolbarViewDidChooseExit(view)
+                } else {
+                    //Cancel
+                    propertyVC.cancelDataEdit()
+                    
+                    self.kmEditToolbarViewDidChooseExit(view)
+                }
+            }
+            return
+        } else if view.editType == .header_Footer {
+             
+        } else if view.editType == .bates {
+             
+        }
+        
+        
         if view.applyEnable {
             let alert = NSAlert()
             if view.editType == .watermark {
@@ -4977,6 +4981,69 @@ extension KMMainViewController: KMEditToolbarViewDelegate {
             exitEditToolbarView()
         }
     }
+    
+    func applyWatermarkInfo(_ pageString: String) {
+        if let model = watermarkViewController?.currentWatermarkData {
+            let watermark = KMWatermarkModel.returnWaterMarkWith(model, listView.document)
+            watermark.pageString = pageString
+            listView.document.addWatermark(watermark)
+            listView.layoutDocumentView()
+            
+            self.recordIsPDFDocumentEdited()
+        }
+        exitEditToolbarView()
+    }
+    
+    func applyBackgroundInfo(_ pageString: String) {
+        if let model = backgroundViewController?.backgroundModel {
+            if let background = listView.document.background() {
+                KMBackgroundManager.defaultManager.updateBackground(background, withModel: model)
+                background.pageString = pageString
+                background.update()
+                
+                listView.document?.refreshPageData()
+                listView.layoutDocumentView()
+                
+                self.recordIsPDFDocumentEdited()
+            }
+        }
+        exitEditToolbarView()
+    }
+    
+    func applyHeaderFooterInfo(_ pageString: String) {
+        if let model = headerFooterViewController?.headerFooterModel {
+            if let headerFooter = listView.document.headerFooter() {
+                KMHeaderFooterManager.defaultManager.updateCPDFHeaderFooter(headerFooter, withModel: model, Int(listView.document.pageCount))
+                headerFooter.pageString = pageString
+                headerFooter.update()
+                
+                listView.document?.refreshPageData()
+                listView.layoutDocumentView()
+                
+                self.recordIsPDFDocumentEdited()
+            }
+            
+        }
+        
+        exitEditToolbarView()
+    }
+    
+    func applyBatesInfo(_ pageString: String) {
+        if let model = batesViewController?.batesModel {
+            if let bates = listView.document.bates() {
+                KMBatesManager.defaultManager.updateCPDFBates(bates, withModel: model, Int(listView.document.pageCount))
+                bates.pageString = pageString
+                bates.update()
+                
+                listView.document?.refreshPageData()
+                listView.layoutDocumentView()
+                
+                self.recordIsPDFDocumentEdited()
+            }
+        }
+        exitEditToolbarView()
+    }
+    
 }
 
 //MARK: - KMCropControllerDelegate 裁剪相关代理

+ 12 - 0
PDF Office/PDF Master/KMClass/Tools/Category/Foundation/NSAlert_Extensions.swift

@@ -0,0 +1,12 @@
+//
+//  NSAlert_Extensions.swift
+//  PDF Reader Pro
+//
+//  Created by kdanmobile on 2025/2/13.
+//
+
+import Foundation
+
+extension NSAlert {
+      
+}

+ 10 - 0
PDF Office/PDF Reader Pro.xcodeproj/project.pbxproj

@@ -3444,6 +3444,10 @@
 		BBB1A3A629F6B66400E54E47 /* NSPanel+KMExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB1A3A529F6B66400E54E47 /* NSPanel+KMExtension.swift */; };
 		BBB1A3A729F6B66400E54E47 /* NSPanel+KMExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB1A3A529F6B66400E54E47 /* NSPanel+KMExtension.swift */; };
 		BBB1A3A829F6B66400E54E47 /* NSPanel+KMExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB1A3A529F6B66400E54E47 /* NSPanel+KMExtension.swift */; };
+		BBB216A42D5E06AF00CAA8AD /* NSAlert_Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB216A32D5E06AF00CAA8AD /* NSAlert_Extensions.swift */; };
+		BBB216A52D5E06AF00CAA8AD /* NSAlert_Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB216A32D5E06AF00CAA8AD /* NSAlert_Extensions.swift */; };
+		BBB216A62D5E06AF00CAA8AD /* NSAlert_Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB216A32D5E06AF00CAA8AD /* NSAlert_Extensions.swift */; };
+		BBB216A72D5E06AF00CAA8AD /* NSAlert_Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB216A32D5E06AF00CAA8AD /* NSAlert_Extensions.swift */; };
 		BBB2A99D2CB65C8E0066560B /* KMURLCreatePDFWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB2A99B2CB65C8E0066560B /* KMURLCreatePDFWindowController.swift */; };
 		BBB2A99E2CB65C8E0066560B /* KMURLCreatePDFWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB2A99B2CB65C8E0066560B /* KMURLCreatePDFWindowController.swift */; };
 		BBB2A99F2CB65C8E0066560B /* KMURLCreatePDFWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB2A99B2CB65C8E0066560B /* KMURLCreatePDFWindowController.swift */; };
@@ -7326,6 +7330,7 @@
 		BBAFC849298519F600D0648E /* KMSavePanelAccessoryController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = KMSavePanelAccessoryController.xib; sourceTree = "<group>"; };
 		BBB14A522978DD5400936EDB /* KMRedactTools.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMRedactTools.swift; sourceTree = "<group>"; };
 		BBB1A3A529F6B66400E54E47 /* NSPanel+KMExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSPanel+KMExtension.swift"; sourceTree = "<group>"; };
+		BBB216A32D5E06AF00CAA8AD /* NSAlert_Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSAlert_Extensions.swift; sourceTree = "<group>"; };
 		BBB2A99B2CB65C8E0066560B /* KMURLCreatePDFWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMURLCreatePDFWindowController.swift; sourceTree = "<group>"; };
 		BBB2A99C2CB65C8E0066560B /* KMURLCreatePDFWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMURLCreatePDFWindowController.xib; sourceTree = "<group>"; };
 		BBB2ACDF2B5943F600098854 /* Quick Start Guide.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = "Quick Start Guide.pdf"; sourceTree = "<group>"; };
@@ -8155,6 +8160,7 @@
 				9FB2210D2B1AE35E00A5B208 /* NSBitmapImageRep_KMExtension.swift */,
 				AD4C71702B9ADFE0003A6286 /* NSError_Extensions.swift */,
 				BBB4819E2D5C8A8C00384CBD /* NSColorPanel_Extensions.swift */,
+				BBB216A32D5E06AF00CAA8AD /* NSAlert_Extensions.swift */,
 			);
 			path = Foundation;
 			sourceTree = "<group>";
@@ -16545,6 +16551,7 @@
 				BBF71D4E2D5A06E60043FFA2 /* KMNTools.swift in Sources */,
 				BBF71D4F2D5A06E60043FFA2 /* KMBotaSearchViewController.swift in Sources */,
 				BBF71D502D5A06E60043FFA2 /* NSPointerArray+PDFListView.m in Sources */,
+				BBB216A72D5E06AF00CAA8AD /* NSAlert_Extensions.swift in Sources */,
 				BBF71D512D5A06E60043FFA2 /* CPDFAnnotationConfig.swift in Sources */,
 				BBF71D522D5A06E60043FFA2 /* ViewController.swift in Sources */,
 				BBF71D532D5A06E60043FFA2 /* CPDFRedactAnnotation+PDFListView.swift in Sources */,
@@ -17554,6 +17561,7 @@
 				BB1C97012D01704600F1EFAD /* KMStampSettingWindowController.swift in Sources */,
 				BB4A94912B04926700940F8B /* KMGOCROperation.swift in Sources */,
 				BB135C2A29B6CD9A00FD5965 /* KMTools.swift in Sources */,
+				BBB216A42D5E06AF00CAA8AD /* NSAlert_Extensions.swift in Sources */,
 				65AD98892CB615F000927779 /* KMNoteFilterStateViewModel.swift in Sources */,
 				9F1FE49629406E4700E952CA /* common.c in Sources */,
 				BBD1F79C296FF7A600343885 /* KMPageEditSplitSettingModel.swift in Sources */,
@@ -18211,6 +18219,7 @@
 				AD8DD2A42A9C35B2007CC9D0 /* KMThumbnailManager.swift in Sources */,
 				BB6B4C0D292F62B20071CA06 /* KMPDFThumbnialPageView.swift in Sources */,
 				9F8539D72943180000DF644E /* KMTabAppearance.swift in Sources */,
+				BBB216A52D5E06AF00CAA8AD /* NSAlert_Extensions.swift in Sources */,
 				BB451AAB2CF59F68003E1565 /* KMNoteController.swift in Sources */,
 				BB3AD6FA29935483004FC1AE /* Reachability.m in Sources */,
 				F3DB85F32CCA209600D0AFDE /* KMNBaseWindowController.swift in Sources */,
@@ -19393,6 +19402,7 @@
 				F3A0F86F2CCA118D00E7373F /* KMNTools.swift in Sources */,
 				657851642CFEA3C40023D640 /* KMBotaSearchViewController.swift in Sources */,
 				F35B484D29A4903300756255 /* NSPointerArray+PDFListView.m in Sources */,
+				BBB216A62D5E06AF00CAA8AD /* NSAlert_Extensions.swift in Sources */,
 				BB6F46CD2CF5E605003FCC3C /* CPDFAnnotationConfig.swift in Sources */,
 				BBFBE74928DD7DB8008B2335 /* ViewController.swift in Sources */,
 				BB6719F72AD2C949003D44D5 /* CPDFRedactAnnotation+PDFListView.swift in Sources */,