KMNBookmarkHanddler.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 isSearchMode = false
  18. var wholeWords = false
  19. var caseSensitive = false
  20. var searchKey = ""
  21. func isValidSearchMode() -> Bool {
  22. if isSearchMode == false {
  23. return false
  24. }
  25. if searchKey.isEmpty {
  26. return false
  27. }
  28. return true
  29. }
  30. var document: CPDFDocument? {
  31. get {
  32. return pdfView?.document
  33. }
  34. }
  35. var currentPageIndex: Int {
  36. get {
  37. return pdfView?.currentPageIndex ?? 0
  38. }
  39. }
  40. func canUndo() -> Bool {
  41. return pdfView?.undoManager?.canUndo ?? false
  42. }
  43. func canRedo() -> Bool {
  44. return pdfView?.undoManager?.canRedo ?? false
  45. }
  46. func undo() {
  47. if canUndo() {
  48. pdfView?.undoManager?.undo()
  49. }
  50. }
  51. func redo() {
  52. if canRedo() {
  53. pdfView?.undoManager?.redo()
  54. }
  55. }
  56. func goBookmark(_ bookmark: CPDFBookmark, animated: Bool) {
  57. pdfView?.go(toPageIndex: bookmark.pageIndex, animated: animated)
  58. }
  59. func canAddBorkmark() -> Bool {
  60. for bk in bookmarks() {
  61. if bk.pageIndex == currentPageIndex {
  62. return false
  63. }
  64. }
  65. return true
  66. }
  67. func bookmarkIsEqual(bookmark: CPDFBookmark?, otherBookmark: CPDFBookmark?) -> Bool {
  68. guard let theBookmark = bookmark else {
  69. return false
  70. }
  71. if theBookmark.document.isEqual(to: otherBookmark?.document) == false {
  72. return false
  73. }
  74. if theBookmark.pageIndex == otherBookmark?.pageIndex {
  75. return true
  76. }
  77. return false
  78. }
  79. func addCurrentBookmark(callback: ((CPDFBookmark?)->Void)?) {
  80. let currentPageIndex = self.currentPageIndex
  81. if let _ = bookmark(for: currentPageIndex) {
  82. } else {
  83. let label = "\(NSLocalizedString("Page", comment:"")) \(currentPageIndex + 1)"
  84. let bookMark = KMBookMarkItem()
  85. bookMark.label = label
  86. bookMark.index = UInt(currentPageIndex)
  87. self._undo_add(label: label, index: currentPageIndex)
  88. callback?(self.bookmark(for: currentPageIndex))
  89. }
  90. }
  91. func removeBookmark(for index: Int) -> Bool {
  92. guard let bk = self.bookmark(for: index) else {
  93. return false
  94. }
  95. self._undo_remove(label: bk.label, index: bk.pageIndex)
  96. return true
  97. }
  98. func removeBookmarks(for indexs: IndexSet) -> Bool {
  99. var bks: [CPDFBookmark] = []
  100. for i in indexs {
  101. if let bk = self.bookmark(for: i) {
  102. bks.append(bk)
  103. }
  104. }
  105. if bks.isEmpty {
  106. return false
  107. }
  108. _undo_removeAll(bookmarks: bks)
  109. return true
  110. }
  111. func removeAllBookmarks() -> Bool {
  112. guard let bks = document?.bookmarks(), bks.isEmpty == false else {
  113. return false
  114. }
  115. _undo_removeAll(bookmarks: bks)
  116. return true
  117. }
  118. @objc func rename(bookmark: CPDFBookmark, label: String) {
  119. guard let theLabel = bookmark.label, theLabel != label else {
  120. return
  121. }
  122. _undo_rename(bookmark: bookmark, label: label)
  123. }
  124. func bookmarks() -> [CPDFBookmark] {
  125. return document?.bookmarks() ?? []
  126. }
  127. func bookmark(for pageIndex: Int) -> CPDFBookmark? {
  128. return document?.bookmark(forPageIndex: UInt(pageIndex))
  129. }
  130. // MARK: - Private Methods
  131. // MARK: - Undo & Redo
  132. @objc private func _undo_remove(label: String, index: Int) {
  133. let bookmark = self.bookmark(for: index)
  134. document?.removeBookmark(forPageIndex: UInt(index))
  135. pdfView?.setNeedsDisplayForVisiblePages()
  136. delegate?.handdler?(self, didRemove: bookmark, info: nil)
  137. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_add(label: label, index: index)
  138. }
  139. @objc private func _undo_add(label: String, index: Int) {
  140. document?.addBookmark(label, forPageIndex: UInt(index))
  141. pdfView?.setNeedsDisplayForVisiblePages()
  142. delegate?.handdler?(self, didAdd: self.bookmark(for: index), info: nil)
  143. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_remove(label: label, index: index)
  144. }
  145. @objc private func _undo_removeAll(bookmarks: [CPDFBookmark]) {
  146. for bookmark in bookmarks {
  147. document?.removeBookmark(forPageIndex: UInt(bookmark.pageIndex))
  148. }
  149. pdfView?.setNeedsDisplayForVisiblePages()
  150. delegate?.handdler?(self, didRemoveAll: nil)
  151. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_removeAll_back(bookmarks: bookmarks)
  152. }
  153. @objc private func _undo_removeAll_back(bookmarks: [CPDFBookmark]) {
  154. for bookmark in bookmarks {
  155. document?.addBookmark(bookmark.label, forPageIndex: UInt(bookmark.pageIndex))
  156. }
  157. pdfView?.setNeedsDisplayForVisiblePages()
  158. delegate?.handdler?(self, didRemoveAll: nil)
  159. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_removeAll(bookmarks: bookmarks)
  160. }
  161. @objc private func _undo_rename(bookmark: CPDFBookmark, label: String) {
  162. let theLabel = bookmark.label ?? ""
  163. bookmark.label = label
  164. delegate?.handdler?(self, didRename: bookmark, info: nil)
  165. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_rename(bookmark: bookmark, label: theLabel)
  166. }
  167. }