KMNOutlineHanddler.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // KMNOutlineHanddler.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/11/12.
  6. //
  7. import Cocoa
  8. @objc protocol KMNOutlineHanddlerDelegate: NSObjectProtocol {
  9. @objc optional func handdler(_ handdler: KMNOutlineHanddler, didAdd info: [String : Any]?)
  10. @objc optional func handdler(_ handdler: KMNOutlineHanddler, didRemove info: [String : Any]?)
  11. @objc optional func handdler(_ handdler: KMNOutlineHanddler, didRename outline: CPDFOutline?, info: [String : Any]?)
  12. @objc optional func handdler(_ handdler: KMNOutlineHanddler, didChangeLocation outline: CPDFOutline?, info: [String : Any]?)
  13. @objc optional func handdler(_ handdler: KMNOutlineHanddler, didMove outline: CPDFOutline?, info: [String : Any]?)
  14. }
  15. class KMNOutlineHanddler: NSObject {
  16. weak var pdfView: CPDFView?
  17. weak var delegate: KMNOutlineHanddlerDelegate?
  18. var document: CPDFDocument? {
  19. get {
  20. return pdfView?.document
  21. }
  22. }
  23. var currentPageIndex: Int {
  24. get {
  25. return pdfView?.currentPageIndex ?? 0
  26. }
  27. }
  28. // Private Methods
  29. private func _showAlert(style: NSAlert.Style = .critical, message: String, informative: String = "", buttons: [String] = []) -> NSApplication.ModalResponse {
  30. let alert = NSAlert()
  31. alert.alertStyle = style
  32. alert.messageText = message
  33. alert.informativeText = informative
  34. for title in buttons {
  35. alert.addButton(withTitle: title)
  36. }
  37. return alert.runModal()
  38. }
  39. // Pulick Methods
  40. func pageCount() -> Int {
  41. return Int(self.document?.pageCount ?? 0)
  42. }
  43. func isLocked() -> Bool {
  44. return self.document?.isLocked ?? true
  45. }
  46. func outlineRoot() -> CPDFOutline? {
  47. return self.document?.outlineRoot()
  48. }
  49. func setNewOutlineRoot() -> CPDFOutline? {
  50. return self.document?.setNewOutlineRoot()
  51. }
  52. func currentSelection() -> CPDFSelection? {
  53. return pdfView?.currentSelection
  54. }
  55. func currentDestination() -> CPDFDestination? {
  56. return pdfView?.currentDestination
  57. }
  58. func canUndo() -> Bool {
  59. return pdfView?.undoManager?.canUndo ?? false
  60. }
  61. func canRedo() -> Bool {
  62. return pdfView?.undoManager?.canRedo ?? false
  63. }
  64. func undo() {
  65. if canUndo() {
  66. pdfView?.undoManager?.undo()
  67. }
  68. }
  69. func redo() {
  70. if canRedo() {
  71. pdfView?.undoManager?.redo()
  72. }
  73. }
  74. func selectOutline(_ outline: CPDFOutline?) {
  75. guard let ol = outline else {
  76. return
  77. }
  78. if let dest = ol.destination {
  79. pdfView?.go(to: dest)
  80. } else if let act = ol.action {
  81. pdfView?.perform(act)
  82. } else {
  83. _ = _showAlert(style: .informational, message: KMLocalizedString("The target page is invalid, please relocate it."))
  84. }
  85. }
  86. func addOutline(outlineItems: [KMBOTAOutlineItem]) {
  87. var tempOutlineItems: [KMBOTAOutlineItem] = outlineItems
  88. tempOutlineItems.sort(){$0.toIndex < $1.toIndex}
  89. for outlineItem in tempOutlineItems {
  90. if outlineItem.outline.label != nil {
  91. outlineItem.parent?.outline.insertChild(outlineItem.outline, at: UInt(outlineItem.toIndex))
  92. } else {
  93. let outline = outlineItem.parent?.outline.insertChild(at: UInt(outlineItem.toIndex))
  94. outline?.label = outlineItem.label
  95. outline?.destination = outlineItem.destination
  96. outlineItem.outline = outline!
  97. }
  98. outlineItem.parent?.children.insert(outlineItem, at: outlineItem.toIndex)
  99. }
  100. delegate?.handdler?(self, didAdd: ["data": tempOutlineItems])
  101. pdfView?.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  102. self?.deleteOutline(outlineItems: tempOutlineItems)
  103. }
  104. }
  105. func deleteOutline(outlineItems: [KMBOTAOutlineItem]) {
  106. var tempOutlineItems: [KMBOTAOutlineItem] = outlineItems
  107. tempOutlineItems.sort(){$0.toIndex > $1.toIndex}
  108. for outlineItem in tempOutlineItems {
  109. outlineItem.outline.removeFromParent()
  110. let index = outlineItem.parent?.children.firstIndex(of: outlineItem)
  111. outlineItem.toIndex = index!
  112. outlineItem.parent?.children.removeObject(outlineItem)
  113. }
  114. delegate?.handdler?(self, didRemove: ["data" : tempOutlineItems])
  115. pdfView?.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  116. self?.addOutline(outlineItems: tempOutlineItems)
  117. }
  118. }
  119. func renamePDFOutline(outlineItem: KMBOTAOutlineItem!, label: String) {
  120. if outlineItem.outline.label == label {
  121. return
  122. }
  123. let temp: String = outlineItem.outline.label
  124. outlineItem.outline.label = label
  125. delegate?.handdler?(self, didRename: outlineItem.outline, info: ["data" : outlineItem])
  126. pdfView?.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  127. self?.renamePDFOutline(outlineItem: outlineItem, label: temp)
  128. }
  129. }
  130. func changeLocation(outlineItem: KMBOTAOutlineItem, destination: CPDFDestination) {
  131. let temp = outlineItem.outline.destination
  132. outlineItem.outline.destination = CPDFDestination(document: destination.document, pageIndex: destination.pageIndex, at: destination.point, zoom: destination.zoom)
  133. delegate?.handdler?(self, didChangeLocation: outlineItem.outline, info: ["data" : outlineItem])
  134. pdfView?.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  135. if let data = temp {
  136. self?.changeLocation(outlineItem: outlineItem, destination: data)
  137. }
  138. }
  139. }
  140. func moveOutline(outlineItem: KMBOTAOutlineItem, index: NSInteger, parent: KMBOTAOutlineItem!) {
  141. // let tempOutlineView = self.BOTAOutlineView!
  142. let indexTemp = outlineItem.outline.index
  143. let parentTemp = outlineItem.parent
  144. //元数据移除
  145. outlineItem.outline.removeFromParent()
  146. parent.outline.insertChild(outlineItem.outline, at: UInt(index))
  147. delegate?.handdler?(self, didMove: outlineItem.outline, info: ["data" : outlineItem, "parent" : parent, "index" : index])
  148. pdfView?.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  149. self?.moveOutline(outlineItem: outlineItem, index: NSInteger(indexTemp), parent: parentTemp)
  150. }
  151. }
  152. }