KMNOutlineHanddler.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. func pageCount() -> Int {
  29. return Int(self.document?.pageCount ?? 0)
  30. }
  31. func isLocked() -> Bool {
  32. return self.document?.isLocked ?? true
  33. }
  34. func outlineRoot() -> CPDFOutline? {
  35. return self.document?.outlineRoot()
  36. }
  37. func setNewOutlineRoot() -> CPDFOutline? {
  38. return self.document?.setNewOutlineRoot()
  39. }
  40. func currentSelection() -> CPDFSelection? {
  41. return pdfView?.currentSelection
  42. }
  43. func currentDestination() -> CPDFDestination? {
  44. return pdfView?.currentDestination
  45. }
  46. func canUndo() -> Bool {
  47. return pdfView?.undoManager?.canUndo ?? false
  48. }
  49. func canRedo() -> Bool {
  50. return pdfView?.undoManager?.canRedo ?? false
  51. }
  52. func undo() {
  53. if canUndo() {
  54. pdfView?.undoManager?.undo()
  55. }
  56. }
  57. func redo() {
  58. if canRedo() {
  59. pdfView?.undoManager?.redo()
  60. }
  61. }
  62. func addOutline(outlineItems: [KMBOTAOutlineItem]) {
  63. var tempOutlineItems: [KMBOTAOutlineItem] = outlineItems
  64. tempOutlineItems.sort(){$0.toIndex < $1.toIndex}
  65. for outlineItem in tempOutlineItems {
  66. if outlineItem.outline.label != nil {
  67. outlineItem.parent?.outline.insertChild(outlineItem.outline, at: UInt(outlineItem.toIndex))
  68. } else {
  69. let outline = outlineItem.parent?.outline.insertChild(at: UInt(outlineItem.toIndex))
  70. outline?.label = outlineItem.label
  71. outline?.destination = outlineItem.destination
  72. outlineItem.outline = outline!
  73. }
  74. outlineItem.parent?.children.insert(outlineItem, at: outlineItem.toIndex)
  75. }
  76. delegate?.handdler?(self, didAdd: ["data": tempOutlineItems])
  77. pdfView?.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  78. self?.deleteOutline(outlineItems: tempOutlineItems)
  79. }
  80. }
  81. func deleteOutline(outlineItems: [KMBOTAOutlineItem]) {
  82. var tempOutlineItems: [KMBOTAOutlineItem] = outlineItems
  83. tempOutlineItems.sort(){$0.toIndex > $1.toIndex}
  84. for outlineItem in tempOutlineItems {
  85. outlineItem.outline.removeFromParent()
  86. let index = outlineItem.parent?.children.firstIndex(of: outlineItem)
  87. outlineItem.toIndex = index!
  88. outlineItem.parent?.children.removeObject(outlineItem)
  89. }
  90. delegate?.handdler?(self, didRemove: ["data" : tempOutlineItems])
  91. pdfView?.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  92. self?.addOutline(outlineItems: tempOutlineItems)
  93. }
  94. }
  95. func renamePDFOutline(outlineItem: KMBOTAOutlineItem!, label: String) {
  96. if outlineItem.outline.label == label {
  97. return
  98. }
  99. let temp: String = outlineItem.outline.label
  100. outlineItem.outline.label = label
  101. delegate?.handdler?(self, didRename: outlineItem.outline, info: ["data" : outlineItem])
  102. pdfView?.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  103. self?.renamePDFOutline(outlineItem: outlineItem, label: temp)
  104. }
  105. }
  106. func changeLocation(outlineItem: KMBOTAOutlineItem, destination: CPDFDestination) {
  107. let temp = outlineItem.outline.destination
  108. outlineItem.outline.destination = CPDFDestination(document: destination.document, pageIndex: destination.pageIndex, at: destination.point, zoom: destination.zoom)
  109. delegate?.handdler?(self, didChangeLocation: outlineItem.outline, info: ["data" : outlineItem])
  110. pdfView?.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  111. if let data = temp {
  112. self?.changeLocation(outlineItem: outlineItem, destination: data)
  113. }
  114. }
  115. }
  116. func moveOutline(outlineItem: KMBOTAOutlineItem, index: NSInteger, parent: KMBOTAOutlineItem!) {
  117. // let tempOutlineView = self.BOTAOutlineView!
  118. let indexTemp = outlineItem.outline.index
  119. let parentTemp = outlineItem.parent
  120. //元数据移除
  121. outlineItem.outline.removeFromParent()
  122. parent.outline.insertChild(outlineItem.outline, at: UInt(index))
  123. delegate?.handdler?(self, didMove: outlineItem.outline, info: ["data" : outlineItem, "parent" : parent, "index" : index])
  124. pdfView?.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  125. self?.moveOutline(outlineItem: outlineItem, index: NSInteger(indexTemp), parent: parentTemp)
  126. }
  127. }
  128. }