|
@@ -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 {
|
|
|
+
|
|
|
+}
|