Browse Source

【UI替换】编辑工具 - 文件对比 文件添加相关逻辑串接

lizhe 1 year ago
parent
commit
67dbd8ccdb

+ 17 - 1
PDF Office/PDF Master/Class/PDFTools/Compare/KMCompareWindowController.swift

@@ -10,12 +10,28 @@ import Cocoa
 class KMCompareWindowController: KMBaseWindowController {
 
     @IBOutlet weak var compareView: KMCompareView!
+    var filePath: String = "" {
+        didSet {
+            if compareView != nil {
+                compareView.filePath = filePath
+            }
+        }
+    }
+    
+    var fileType: KMCompareFilesType = .content {
+        didSet {
+            if compareView != nil {
+                compareView.fileType = fileType
+            }
+        }
+    }
     
     override func windowDidLoad() {
         super.windowDidLoad()
 
         // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
-        
+        compareView.filePath = filePath
+        compareView.fileType = fileType
         compareView.cancelAction = { [unowned self] view in
             cancelAction?(self)
         }

+ 167 - 27
PDF Office/PDF Master/Class/PDFTools/Compare/View/KMCompareView.swift

@@ -65,23 +65,35 @@ class KMCompareView: KMBaseXibView {
     
     var pdfCompareContent: CPDFCompareContent?
     
-    
-    var pdfOldDocument: PDFDocument?
+    var filePath: String? {
+        didSet {
+            if filePath?.count != 0 {
+                self.addFilePath(filePath: filePath!)
+            }
+        }
+    }
+    private var pdfOldDocument: PDFDocument?
     var oldFileQKSelectedPaths: [String] = []
     
-    var pdfNewDocument: PDFDocument?
+    private var pdfNewDocument: PDFDocument?
     var fileQKNewSelectedPaths: [String] = []
     
-    var fileType: KMCompareFilesType = .content
+    var fileType: KMCompareFilesType = .content {
+        didSet {
+            self.updateFileCompareType(fileType: fileType)
+        }
+    }
+    
     var cancelAction: KMCompareViewCancelAction?
     
     convenience init(pdfDocument: PDFDocument) {
         self.init()
         self.pdfOldDocument = pdfDocument
-        let document: CPDFDocument = CPDFDocument.init(url: pdfDocument.documentURL)
+        let document: PDFDocument = PDFDocument.init(url: pdfDocument.documentURL!)!
+        let cDocument: CPDFDocument = CPDFDocument.init(url: pdfDocument.documentURL!)
         
         let file: KMFileAttribute = KMFileAttribute()
-        file.pdfDocument = document
+        file.pdfDocument = cDocument
         
         let config: KMCompareFilesConfig = KMCompareFilesConfig.init()
         config.fileOldAttribute = file
@@ -90,7 +102,8 @@ class KMCompareView: KMBaseXibView {
     convenience init(filePath: String, password: String) {
         self.init()
         self.pdfOldDocument = PDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)
-        let pdfDocument = CPDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)
+        let pdfDocument = PDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)
+        let cDocument = CPDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)
         if pdfDocument!.isLocked {
             pdfDocument!.unlock(withPassword: password)
         }
@@ -100,12 +113,11 @@ class KMCompareView: KMBaseXibView {
         }
         
         let file: KMFileAttribute = KMFileAttribute()
-        file.pdfDocument = pdfDocument
+        file.pdfDocument = cDocument
         
         let config = KMCompareFilesConfig.defaultConfig
         config.fileOldAttribute = file
         config.fileOldAttribute.password = password
-        
     }
     
     override func setup() {
@@ -139,7 +151,6 @@ class KMCompareView: KMBaseXibView {
         compareTypeSegment.layer?.cornerRadius = 5.0
         compareTypeSegment.layer?.masksToBounds = true
         compareTypeSegment.layer?.backgroundColor = NSColor.clear.cgColor
-        addFileContentView.wantsLayer = true
         
         addFileAddImageFramView.wantsLayer = true
         addFileAddImageFramView.layer?.backgroundColor = NSColor.clear.cgColor
@@ -261,23 +272,68 @@ class KMCompareView: KMBaseXibView {
 }
 
 extension KMCompareView {
-    func addFileQKSelectPath(filePath: String, isNewFile: Bool = false) {
-        let pdfdocument = CPDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
+    func addFilePath(filePath: String, isNew: Bool = false) {
+        DispatchQueue.main.async {
+            let document: PDFDocument = PDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)!
+            let cDocument: CPDFDocument = CPDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)!
+            let file: KMFileAttribute = KMFileAttribute()
+            file.pdfDocument = cDocument
+            
+            if isNew {
+                KMCompareFilesConfig.defaultConfig.fileNewAttribute = file
+                self.pdfNewDocument = document
+                
+                self.pdfNewView.document = document
+                self.pdfNewView.autoScales = true
+                self.pdfNewView.delegate = self
+            } else {
+                
+                KMCompareFilesConfig.defaultConfig.fileOldAttribute = file
+                self.pdfOldDocument = document
+                
+                self.oldPDFView.document = document
+                self.oldPDFView.autoScales = true
+                self.oldPDFView.delegate = self
+            }
+            
+            self.addFileQKSelectPath(filePath: filePath, isNew: isNew)
+            self.reloadData()
+        }
+    }
+    
+    func addFileQKSelectPath(filePath: String, isNew: Bool = false) {
+        let pdfdocument = PDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
         guard let pdfdocument = pdfdocument else { return }
         
         var key = ""
-        if isNewFile {
+        if isNew {
             key = CPDFNewFileQKSelectedPathsKey
         } else {
             key = CPDFOldFileQKSelectedPathsKey
         }
         
+        //取出
         var filePaths: [String] = UserDefaults.standard.object(forKey: key) as? [String] ?? []
         if filePaths.count > CPDFMaxQKSelectedPathsCount {
             filePaths.removeLast()
             
         }
-        filePaths.insert(filePath, at: 0)
+        if !filePaths.contains(filePath) {
+            filePaths.insert(filePath, at: 0)
+        }
+        
+        //存储
+        UserDefaults.standard.setValue(filePaths, forKey: key)
+        UserDefaults.standard.synchronize()
+        
+        self.updateSelectBoxData()
+        if isNew {
+            self.fileQKNewSelectedBox.selectItem(withObjectValue: filePath)
+            self.fileQKNewSelectedBox.toolTip = filePath.lastPathComponent
+        } else {
+            self.oldFileQKSelectedBox.selectItem(withObjectValue: filePath)
+            self.oldFileQKSelectedBox.toolTip = filePath.lastPathComponent
+        }
     }
     
     func updateSelectBoxData() {
@@ -294,15 +350,21 @@ extension KMCompareView {
         } else {
             self.fileQKNewSelectedBox.isEnabled = false
         }
+        
+        if self.oldFileQKSelectedPaths.count > 0 {
+            self.oldFileQKSelectedBox.isEnabled = true
+        } else {
+            self.oldFileQKSelectedBox.isEnabled = false
+        }
     }
     
     func updateSelectBoxItemData(isNew: Bool = false) -> [String] {
         let defaults = UserDefaults.standard
         var fileSelectedCachePaths: [String] = []
         if isNew {
-            fileSelectedCachePaths = defaults.value(forKey: CPDFOldFileQKSelectedPathsKey) as? [String] ?? []
-        } else {
             fileSelectedCachePaths = defaults.value(forKey: CPDFNewFileQKSelectedPathsKey) as? [String] ?? []
+        } else {
+            fileSelectedCachePaths = defaults.value(forKey: CPDFOldFileQKSelectedPathsKey) as? [String] ?? []
         }
         
         var fileSelectedPaths: [String] = []
@@ -339,17 +401,18 @@ extension KMCompareView {
         var currentPageLabel = NSTextField()
         var totalPageLabel = NSTextField()
         
-        guard let pdfNewDocument = pdfNewDocument else { return }
-        guard let pdfOldDocument = pdfOldDocument else { return }
-        
+        addFileContentView.isHidden = false
         if isNew {
-            pageDocument = pdfNewDocument
+            guard let pdfNewDocument = KMCompareFilesConfig.defaultConfig.fileNewAttribute.pdfDocument else { return }
             pdfView = pdfNewView
+            pageDocument = pdfView.document!
             currentPageLabel = currentNewPageLabel
             totalPageLabel = totalPaNewgeLabel
+            addFileContentView.isHidden = true
         } else {
-            pageDocument = pdfOldDocument
+            guard let pdfOldDocument = KMCompareFilesConfig.defaultConfig.fileOldAttribute.pdfDocument else { return }
             pdfView = oldPDFView
+            pageDocument = pdfView.document!
             currentPageLabel = currentOldPageLabel
             totalPageLabel = totalPaOldgeLabel
         }
@@ -359,7 +422,7 @@ extension KMCompareView {
         let pageCount = pageDocument.pageCount
         var currentPageIndex = 0
         if(pdfView.currentPage != nil) {
-            currentPageIndex = pageDocument.index(for: pdfView.currentPage!)
+            currentPageIndex = Int(pageDocument.index(for: pdfView.currentPage!))
         }
         
         if(pageCount > 0) {
@@ -373,9 +436,9 @@ extension KMCompareView {
     func updateDocument(filePath: String, isNew: Bool = false, completion: @escaping (_ fileAttitude: KMFileAttribute?) -> Void) {
         var pdfDocument = CPDFDocument()
         if isNew {
-            pdfDocument = KMCompareFilesConfig.defaultConfig.fileOldAttribute.pdfDocument ?? CPDFDocument()
+            pdfDocument = KMCompareFilesConfig.defaultConfig.fileOldAttribute.pdfDocument
         } else {
-            pdfDocument = KMCompareFilesConfig.defaultConfig.fileNewAttribute.pdfDocument ?? CPDFDocument()
+            pdfDocument = KMCompareFilesConfig.defaultConfig.fileNewAttribute.pdfDocument
         }
         
         guard let pdfDocument = pdfDocument else {
@@ -442,6 +505,8 @@ extension KMCompareView {
             settingBtnTopLayout.constant = 20
         }
     }
+    
+    
 }
 
 extension KMCompareView {
@@ -455,10 +520,81 @@ extension KMCompareView {
         
     }
     
-    @objc func segmentedControlClicked(sender: NSSegmentedControl) {
+    @IBAction func oldFilesSelectBoxAction(_ sender: Any) {
+        let selectIndex = self.oldFileQKSelectedBox.indexOfSelectedItem
+        let selectItem = self.oldFileQKSelectedBox.itemObjectValue(at: selectIndex)
+        self.updateDocument(filePath: selectItem as! String) { fileAttitude in
+            
+        }
+    }
+    
+    @IBAction func newFilesSelectBoxAction(_ sender: Any) {
+        let selectIndex = self.fileQKNewSelectedBox.indexOfSelectedItem
+        let selectItem = self.fileQKNewSelectedBox.itemObjectValue(at: selectIndex)
+        self.updateDocument(filePath: selectItem as! String) { fileAttitude in
+            
+        }
+    }
+    
+    @IBAction func oldChooseButtonAction(_ sender: Any) {
+        self.chooseFileAction()
+    }
+    
+    @IBAction func newChooseButtonAction(_ sender: Any) {
+        self.chooseFileAction(isNew: true)
+    }
+    
+    @IBAction func oldNextButtonAction(_ sender: Any) {
+        self.oldPDFView.goToNextPage(nil)
+        self.updatePageState()
+    }
+    
+    @IBAction func oldPreviousButtonAction(_ sender: Any) {
+        self.oldPDFView.goToPreviousPage(nil)
+        self.updatePageState()
+    }
+    
+    @IBAction func newNextButtonAction(_ sender: Any) {
+        self.pdfNewView.goToNextPage(nil)
+        self.updatePageState(isNew: true)
+    }
+    
+    @IBAction func newPreviousButtonAction(_ sender: Any) {
+        self.pdfNewView.goToPreviousPage(nil)
+        self.updatePageState(isNew: true)
+    }
+    
+    @IBAction func oldPageRangeBoxAction(_ sender: Any) {
         
     }
     
+    @IBAction func newPageRangeBoxAction(_ sender: Any) {
+        
+    }
+    
+    @IBAction func compareTextButtonAction(_ sender: Any) {
+        
+    }
+    
+    @IBAction func compareImageButtonAction(_ sender: Any) {
+        
+    }
+    
+    @IBAction func settingButtonAction(_ sender: Any) {
+        
+    }
+    
+    @objc func segmentedControlClicked(sender: NSSegmentedControl) {
+        let selectedType = self.compareTypeSegment.selectedSegment
+        var type: KMCompareFilesType = .content
+        if (selectedType == 0) {
+            type = .content;
+        } else {
+            type = .coverting;
+        }
+        self.updateFileCompareType(fileType: type)
+    }
+    
     func chooseFileAction(isNew: Bool = false) {
 //        fileQKNewSelectedBox.resignFirstResponder()
         self.window?.makeFirstResponder(nil)
@@ -484,8 +620,8 @@ extension KMCompareView {
                 #endif
 
                 if let filePath = openPanel.url?.path {
-                    self.updateDocument(filePath: filePath, isNew: isNew) { file in
-                        
+                    self.updateDocument(filePath: filePath, isNew: isNew) { [unowned self] file in
+                        self.addFilePath(filePath: filePath, isNew: isNew)
                     }
                 }
             }
@@ -498,3 +634,7 @@ extension KMCompareView {
 extension KMCompareView: NSComboBoxDelegate {
     
 }
+
+extension KMCompareView: PDFViewDelegate {
+    
+}

+ 39 - 0
PDF Office/PDF Master/Class/PDFTools/Compare/View/KMCompareView.xib

@@ -101,6 +101,9 @@
                                                     <color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
                                                     <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                                 </comboBoxCell>
+                                                <connections>
+                                                    <action selector="oldFilesSelectBoxAction:" target="-2" id="sBh-xi-XCX"/>
+                                                </connections>
                                             </comboBox>
                                             <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="8h1-sv-nB8">
                                                 <rect key="frame" x="127" y="292" width="82" height="32"/>
@@ -111,6 +114,9 @@
                                                 <constraints>
                                                     <constraint firstAttribute="height" constant="20" id="Kgo-0T-WOf"/>
                                                 </constraints>
+                                                <connections>
+                                                    <action selector="oldChooseButtonAction:" target="-2" id="Qqe-NQ-y9J"/>
+                                                </connections>
                                             </button>
                                             <pdfView autoresizesSubviews="NO" wantsLayer="YES" displayMode="singlePage" displaysPageBreaks="NO" translatesAutoresizingMaskIntoConstraints="NO" id="jw7-gN-Ept">
                                                 <rect key="frame" x="5" y="35" width="194" height="254"/>
@@ -129,6 +135,9 @@
                                                 <constraints>
                                                     <constraint firstAttribute="width" constant="20" id="E7s-aR-02e"/>
                                                 </constraints>
+                                                <connections>
+                                                    <action selector="oldPreviousButtonAction:" target="-2" id="76G-k0-VgS"/>
+                                                </connections>
                                             </button>
                                             <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Dme-Ry-3Kb">
                                                 <rect key="frame" x="178" y="0.0" width="26" height="26"/>
@@ -140,6 +149,9 @@
                                                     <constraint firstAttribute="height" constant="20" id="jxD-Tg-ABf"/>
                                                     <constraint firstAttribute="width" constant="20" id="ldl-sL-ddE"/>
                                                 </constraints>
+                                                <connections>
+                                                    <action selector="oldNextButtonAction:" target="-2" id="dKJ-Oi-cnk"/>
+                                                </connections>
                                             </button>
                                             <customView translatesAutoresizingMaskIntoConstraints="NO" id="0CC-Jo-reV">
                                                 <rect key="frame" x="38" y="3" width="128" height="20"/>
@@ -237,6 +249,9 @@
                                                     <color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
                                                     <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                                 </comboBoxCell>
+                                                <connections>
+                                                    <action selector="newFilesSelectBoxAction:" target="-2" id="y7e-yN-DUb"/>
+                                                </connections>
                                             </comboBox>
                                             <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="WSb-Q2-D3m">
                                                 <rect key="frame" x="127" y="292" width="82" height="32"/>
@@ -247,6 +262,9 @@
                                                 <constraints>
                                                     <constraint firstAttribute="height" constant="20" id="ejZ-97-bSB"/>
                                                 </constraints>
+                                                <connections>
+                                                    <action selector="newChooseButtonAction:" target="-2" id="4dh-xR-lba"/>
+                                                </connections>
                                             </button>
                                             <pdfView autoresizesSubviews="NO" wantsLayer="YES" displayMode="singlePage" displaysPageBreaks="NO" translatesAutoresizingMaskIntoConstraints="NO" id="O7G-En-YRx">
                                                 <rect key="frame" x="5" y="35" width="194" height="254"/>
@@ -323,6 +341,9 @@
                                                 <constraints>
                                                     <constraint firstAttribute="width" constant="20" id="ezm-me-nt7"/>
                                                 </constraints>
+                                                <connections>
+                                                    <action selector="newPreviousButtonAction:" target="-2" id="U1a-Br-o2u"/>
+                                                </connections>
                                             </button>
                                             <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="6F2-Zi-Bmp">
                                                 <rect key="frame" x="178" y="0.0" width="26" height="26"/>
@@ -334,6 +355,9 @@
                                                     <constraint firstAttribute="width" constant="20" id="4Gd-DT-BsG"/>
                                                     <constraint firstAttribute="height" constant="20" id="PDL-Xa-71e"/>
                                                 </constraints>
+                                                <connections>
+                                                    <action selector="newNextButtonAction:" target="-2" id="Mix-T9-hlA"/>
+                                                </connections>
                                             </button>
                                             <customView translatesAutoresizingMaskIntoConstraints="NO" id="xJZ-jv-zpg">
                                                 <rect key="frame" x="38" y="3" width="128" height="20"/>
@@ -470,6 +494,9 @@
                                                 <string>Item 3</string>
                                             </objectValues>
                                         </comboBoxCell>
+                                        <connections>
+                                            <action selector="oldPageRangeBoxAction:" target="-2" id="VFS-AI-v3o"/>
+                                        </connections>
                                     </comboBox>
                                     <textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="H4A-gF-NRQ">
                                         <rect key="frame" x="7" y="8" width="67" height="16"/>
@@ -490,6 +517,9 @@
                                             <color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
                                             <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                         </comboBoxCell>
+                                        <connections>
+                                            <action selector="newPageRangeBoxAction:" target="-2" id="NdN-Fz-TGt"/>
+                                        </connections>
                                     </comboBox>
                                 </subviews>
                                 <constraints>
@@ -532,6 +562,9 @@
                                 <constraint firstAttribute="height" constant="16" id="1HM-az-b2F"/>
                                 <constraint firstAttribute="width" constant="340" id="Cpp-2p-NH5"/>
                             </constraints>
+                            <connections>
+                                <action selector="compareTextButtonAction:" target="-2" id="Hvn-GG-1mw"/>
+                            </connections>
                         </button>
                         <button translatesAutoresizingMaskIntoConstraints="NO" id="Ry6-fx-Lsg">
                             <rect key="frame" x="20" y="154" width="342" height="18"/>
@@ -543,6 +576,9 @@
                                 <constraint firstAttribute="height" constant="16" id="1Ys-if-veD"/>
                                 <constraint firstAttribute="width" constant="340" id="pXd-yt-QAh"/>
                             </constraints>
+                            <connections>
+                                <action selector="compareImageButtonAction:" target="-2" id="rdg-na-uz5"/>
+                            </connections>
                         </button>
                         <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="ZWD-eI-OAU" userLabel="Settings">
                             <rect key="frame" x="22" y="119" width="72" height="16"/>
@@ -550,6 +586,9 @@
                                 <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
                                 <font key="font" metaFont="system"/>
                             </buttonCell>
+                            <connections>
+                                <action selector="settingButtonAction:" target="-2" id="ajg-fR-SE8"/>
+                            </connections>
                         </button>
                         <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="VOf-35-kbQ">
                             <rect key="frame" x="363" y="13" width="76" height="32"/>

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

@@ -3030,15 +3030,17 @@ extension KMMainViewController : KMMainToolbarControllerDelegate {
                 let controller = KMCompareWindowController(windowNibName: "KMCompareWindowController")
                 self.currentWindowController = controller
                 
+                controller.filePath = (self.document?.documentURL.path)!
+                
                 controller.cancelAction = { [unowned self] controller in
                     self.view.window?.endSheet((self.currentWindowController.window)!)
                     self.currentWindowController = nil
                 }
             
                 if index == 1 {
-                    
+                    controller.fileType = .content
                 } else {
-                    
+                    controller.fileType = .coverting
                 }
             
                 await NSWindow.currentWindow().beginSheet(controller.window!)