KMNBookmarkHanddler.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // KMNBookmarkHanddler.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/11/8.
  6. //
  7. import Cocoa
  8. @objc protocol KMNBookmarkHanddlerDelegate: NSObjectProtocol {
  9. @objc optional func handdler(_ handdler: KMNBookmarkHanddler, didAdd bookmark: CPDFBookmark?, info: [String : Any]?)
  10. @objc optional func handdler(_ handdler: KMNBookmarkHanddler, didRemove bookmark: CPDFBookmark?, info: [String : Any]?)
  11. @objc optional func handdler(_ handdler: KMNBookmarkHanddler, didRemoveAll info: [String : Any]?)
  12. @objc optional func handdler(_ handdler: KMNBookmarkHanddler, didRename bookmark: CPDFBookmark?, info: [String : Any]?)
  13. }
  14. class KMNBookmarkHanddler: NSObject {
  15. weak var pdfView: CPDFView?
  16. weak var delegate: KMNBookmarkHanddlerDelegate?
  17. var document: CPDFDocument? {
  18. get {
  19. return pdfView?.document
  20. }
  21. }
  22. var currentPageIndex: Int {
  23. get {
  24. return pdfView?.currentPageIndex ?? 0
  25. }
  26. }
  27. func canUndo() -> Bool {
  28. return pdfView?.undoManager?.canUndo ?? false
  29. }
  30. func canRedo() -> Bool {
  31. return pdfView?.undoManager?.canRedo ?? false
  32. }
  33. func undo() {
  34. if canUndo() {
  35. pdfView?.undoManager?.undo()
  36. }
  37. }
  38. func redo() {
  39. if canRedo() {
  40. pdfView?.undoManager?.redo()
  41. }
  42. }
  43. func addCurrentBookmark(callback: ((CPDFBookmark?)->Void)?) {
  44. let currentPageIndex = self.currentPageIndex
  45. if let _ = bookmark(for: currentPageIndex) {
  46. } else {
  47. let label = "\(NSLocalizedString("Page", comment:"")) \(currentPageIndex + 1)"
  48. let bookMark = KMBookMarkItem()
  49. bookMark.label = label
  50. bookMark.index = UInt(currentPageIndex)
  51. self._undo_add(label: label, index: currentPageIndex)
  52. callback?(self.bookmark(for: currentPageIndex))
  53. }
  54. }
  55. func removeBookmark(for index: Int) -> Bool {
  56. guard let bk = self.bookmark(for: index) else {
  57. return false
  58. }
  59. self._undo_remove(label: bk.label, index: bk.pageIndex)
  60. return true
  61. }
  62. func removeBookmarks(for indexs: IndexSet) -> Bool {
  63. var bks: [CPDFBookmark] = []
  64. for i in indexs {
  65. if let bk = self.bookmark(for: i) {
  66. bks.append(bk)
  67. }
  68. }
  69. if bks.isEmpty {
  70. return false
  71. }
  72. _undo_removeAll(bookmarks: bks)
  73. return true
  74. }
  75. func removeAllBookmarks() -> Bool {
  76. guard let bks = document?.bookmarks(), bks.isEmpty == false else {
  77. return false
  78. }
  79. _undo_removeAll(bookmarks: bks)
  80. return true
  81. }
  82. @objc func rename(bookmark: CPDFBookmark, label: String) {
  83. guard let theLabel = bookmark.label, theLabel != label else {
  84. return
  85. }
  86. _undo_rename(bookmark: bookmark, label: label)
  87. }
  88. func bookmark(for pageIndex: Int) -> CPDFBookmark? {
  89. return document?.bookmark(forPageIndex: UInt(pageIndex))
  90. }
  91. // MARK: - Private Methods
  92. // MARK: - Undo & Redo
  93. @objc private func _undo_remove(label: String, index: Int) {
  94. let bookmark = self.bookmark(for: index)
  95. document?.removeBookmark(forPageIndex: UInt(index))
  96. pdfView?.setNeedsDisplayForVisiblePages()
  97. delegate?.handdler?(self, didRemove: bookmark, info: nil)
  98. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_add(label: label, index: index)
  99. }
  100. @objc private func _undo_add(label: String, index: Int) {
  101. document?.addBookmark(label, forPageIndex: UInt(index))
  102. pdfView?.setNeedsDisplayForVisiblePages()
  103. delegate?.handdler?(self, didAdd: self.bookmark(for: index), info: nil)
  104. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_remove(label: label, index: index)
  105. }
  106. @objc private func _undo_removeAll(bookmarks: [CPDFBookmark]) {
  107. for bookmark in bookmarks {
  108. document?.removeBookmark(forPageIndex: UInt(bookmark.pageIndex))
  109. }
  110. pdfView?.setNeedsDisplayForVisiblePages()
  111. delegate?.handdler?(self, didRemoveAll: nil)
  112. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_removeAll_back(bookmarks: bookmarks)
  113. }
  114. @objc private func _undo_removeAll_back(bookmarks: [CPDFBookmark]) {
  115. for bookmark in bookmarks {
  116. document?.addBookmark(bookmark.label, forPageIndex: UInt(bookmark.pageIndex))
  117. }
  118. pdfView?.setNeedsDisplayForVisiblePages()
  119. delegate?.handdler?(self, didRemoveAll: nil)
  120. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_removeAll(bookmarks: bookmarks)
  121. }
  122. @objc private func _undo_rename(bookmark: CPDFBookmark, label: String) {
  123. let theLabel = bookmark.label ?? ""
  124. bookmark.label = label
  125. delegate?.handdler?(self, didRename: bookmark, info: nil)
  126. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_rename(bookmark: bookmark, label: theLabel)
  127. }
  128. }