KMNPageEditViewController.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // KMNPageEditViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/21.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMNPageEditViewViewDelegate: AnyObject {
  10. @objc optional func changeBoundEditViewController(pageEditVC:KMNPageEditViewController)
  11. @objc optional func changeUndoManagerEditViewController(pageEditVC:KMNPageEditViewController)
  12. }
  13. class KMNPageEditViewController: KMNThumbnailBaseViewController {
  14. private var groupView: ComponentGroup? = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  15. weak open var pageEditViewController: KMNPageEditViewViewDelegate?
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. collectionView.menuClickedAction = {[weak self] point in
  19. let menuStruct = self?.clickMenu(point: point)
  20. if self?.groupView != nil {
  21. self?.groupView?.clickedAutoHide = false
  22. self?.groupView?.groupDelegate = self
  23. self?.groupView?.frame = CGRectMake(0, 0, 180, menuStruct?.viewHeight ?? 0)
  24. self?.groupView?.updateGroupInfo(menuStruct?.menuitems ?? [])
  25. self?.groupView?.showWithPoint(CGPoint(x: point.x, y: point.y - (menuStruct?.viewHeight ?? 0)), relativeTo: self?.collectionView)
  26. }
  27. return NSMenu()
  28. }
  29. }
  30. override func updateUIThemeColor() {
  31. super.updateUIThemeColor()
  32. backViewBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-pdfView")
  33. }
  34. override func updateUILanguage() {
  35. super.updateUILanguage()
  36. self.groupView?.reloadData()
  37. }
  38. private func insertFilePath(filePath:String,pdfPassword:String?) {
  39. let selectedIndexPaths = collectionView.selectionIndexPaths
  40. let insertPDF = KMNPDFInsertPDFWindowController(showDocument, filePath: filePath, password: pdfPassword, selectionIndexPaths: selectedIndexPaths)
  41. insertPDF.pdfCallback = { [weak self] fileAttribute, insertIdx in
  42. let doc = fileAttribute.pdfDocument
  43. var insertPages: [CPDFPage] = []
  44. for number in fileAttribute.fetchSelectPages() {
  45. if let page = doc?.page(at: UInt(number-1)) {
  46. insertPages.append(page)
  47. }
  48. }
  49. self?.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: doc)
  50. self?.insertFormPages(insertPages: insertPages, pageDex: insertIdx)
  51. }
  52. insertPDF.fileCallback = { [weak self] filePath, insertIdx in
  53. self?.insertFromFilePath(fileNames: [filePath], formDex: 0, indexDex: UInt(insertIdx), selectIndexs: [], completionBlock: { newSelectIndexs in
  54. })
  55. }
  56. insertPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  57. }
  58. // MARK: - public
  59. public func canZoomInPageSize()->Bool {
  60. if pageThumbnailSize.width > defaultItemSize.width {
  61. return false
  62. }
  63. return true
  64. }
  65. public func canZoomOutPageSize()->Bool {
  66. if pageThumbnailSize.width < defaultItemSize.width {
  67. return false
  68. }
  69. return true
  70. }
  71. public func zoomInPageAction() { //放大
  72. if (canZoomInPageSize() == false) {
  73. return
  74. }
  75. let indexpaths = collectionView.selectionIndexPaths
  76. if (pageThumbnailSize.width < defaultItemSize.width) {
  77. pageThumbnailSize = defaultItemSize
  78. } else if (pageThumbnailSize.width == defaultItemSize.width) {
  79. pageThumbnailSize = NSSize(width: defaultItemSize.width*1.5, height: defaultItemSize.height*1.5)
  80. }
  81. collectionView.reloadData()
  82. collectionView.selectionIndexPaths = indexpaths
  83. }
  84. public func zoomOutPageAction() { //缩小
  85. if (canZoomOutPageSize() == false) {
  86. return
  87. }
  88. let indexpaths = collectionView.selectionIndexPaths
  89. if (pageThumbnailSize.width > defaultItemSize.width) {
  90. pageThumbnailSize = defaultItemSize
  91. } else if (pageThumbnailSize.width == defaultItemSize.width) {
  92. pageThumbnailSize = NSSize(width: defaultItemSize.width*0.5, height: defaultItemSize.height*0.5)
  93. }
  94. collectionView.reloadData()
  95. collectionView.selectionIndexPaths = indexpaths
  96. }
  97. }
  98. //MARK: - ComponentSelectDelegate
  99. extension KMNPageEditViewController: ComponentGroupDelegate {
  100. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  101. if menuItemProperty?.identifier == ThumbnailMenuIdentifier_Copy {
  102. copyMenuItemAciton()
  103. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Cut) {
  104. cutMenuItemAciton()
  105. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Paste) {
  106. if(menuItemProperty != nil) {
  107. pastMenuItemAciton(menuitemProperty: menuItemProperty!)
  108. }
  109. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Delete) {
  110. cutMenuItemAciton()
  111. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_RotateRight) {
  112. rotatePageRightAction()
  113. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_RotateLeft) {
  114. rotatePageLeftAction()
  115. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_InsertFile) {
  116. insertFromPDFAction()
  117. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_InsertBlank) {
  118. insertFromBlankAction()
  119. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Export) {
  120. extractPDFAction()
  121. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Share) {
  122. let indexpaths = collectionView.selectionIndexPaths
  123. let doucument = showDocument
  124. let filename : String = doucument?.documentURL.lastPathComponent ?? ""
  125. let folderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(filename)) ?? ""
  126. try? FileManager.default.removeItem(atPath: folderPath)
  127. let pdfdocument = CPDFDocument()
  128. let ret = pdfdocument?.importPages(KMNTools.indexpathsToIndexs(indexpaths: indexpaths), from: doucument, at: 0) ?? false
  129. let url = URL(fileURLWithPath: folderPath)
  130. if ret {
  131. let _ = pdfdocument?.write(toFile: folderPath)
  132. let selects: [URL] = [url]
  133. let picker = NSSharingServicePicker.init(items: selects)
  134. if(groupView != nil) {
  135. let rect = CGRectMake(CGRectGetMaxX(groupView!.bounds), CGRectGetMinY(groupView!.bounds) + 35.0, 1, 1)
  136. picker.show(relativeTo: rect, of: groupView!, preferredEdge: NSRectEdge.maxX)
  137. }
  138. }
  139. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_FileShowSize) {
  140. displayPageSizeAction()
  141. }
  142. }
  143. func componentGroupDidDismiss(group: ComponentGroup?) {
  144. }
  145. }