KMNBookmarkHanddler.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. if let theBookmark = bookmark {
  58. pdfView?.go(toPageIndex: theBookmark.pageIndex, animated: animated)
  59. }
  60. }
  61. func canAddBorkmark() -> Bool {
  62. for bk in bookmarks() {
  63. if bk.pageIndex == currentPageIndex {
  64. return false
  65. }
  66. }
  67. return true
  68. }
  69. func bookmarkIsEqual(bookmark: CPDFBookmark?, otherBookmark: CPDFBookmark?) -> Bool {
  70. guard let theBookmark = bookmark else {
  71. return false
  72. }
  73. if theBookmark.document.isEqual(to: otherBookmark?.document) == false {
  74. return false
  75. }
  76. if theBookmark.pageIndex == otherBookmark?.pageIndex {
  77. return true
  78. }
  79. return false
  80. }
  81. func addCurrentBookmark(callback: ((CPDFBookmark?)->Void)?) {
  82. let currentPageIndex = self.currentPageIndex
  83. if let _ = bookmark(for: currentPageIndex) {
  84. } else {
  85. let label = "\(NSLocalizedString("Page", comment:"")) \(currentPageIndex + 1)"
  86. let bookMark = KMBookmarkItem()
  87. bookMark.label = label
  88. bookMark.index = UInt(currentPageIndex)
  89. self._undo_add(label: label, index: currentPageIndex)
  90. callback?(self.bookmark(for: currentPageIndex))
  91. }
  92. }
  93. func removeBookmark(for index: Int) -> Bool {
  94. guard let bk = self.bookmark(for: index) else {
  95. return false
  96. }
  97. self._undo_remove(label: bk.label, index: bk.pageIndex)
  98. return true
  99. }
  100. func removeBookmarks(for indexs: IndexSet) -> Bool {
  101. var bks: [CPDFBookmark] = []
  102. for i in indexs {
  103. if let bk = self.bookmark(for: i) {
  104. bks.append(bk)
  105. }
  106. }
  107. if bks.isEmpty {
  108. return false
  109. }
  110. _undo_removeAll(bookmarks: bks)
  111. return true
  112. }
  113. func removeAllBookmarks() -> Bool {
  114. guard let bks = document?.bookmarks(), bks.isEmpty == false else {
  115. return false
  116. }
  117. _undo_removeAll(bookmarks: bks)
  118. return true
  119. }
  120. @objc func rename(bookmark: CPDFBookmark, label: String) {
  121. guard let theLabel = bookmark.label, theLabel != label else {
  122. return
  123. }
  124. _undo_rename(bookmark: bookmark, label: label)
  125. }
  126. func bookmarks() -> [CPDFBookmark] {
  127. return document?.bookmarks() ?? []
  128. }
  129. func bookmark(for pageIndex: Int) -> CPDFBookmark? {
  130. return document?.bookmark(forPageIndex: UInt(pageIndex))
  131. }
  132. // MARK: - Private Methods
  133. // MARK: - Undo & Redo
  134. @objc private func _undo_remove(label: String, index: Int) {
  135. let bookmark = self.bookmark(for: index)
  136. document?.removeBookmark(forPageIndex: UInt(index))
  137. pdfView?.setNeedsDisplayForVisiblePages()
  138. delegate?.handdler?(self, didRemove: bookmark, info: nil)
  139. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_add(label: label, index: index)
  140. }
  141. @objc private func _undo_add(label: String, index: Int) {
  142. document?.addBookmark(label, forPageIndex: UInt(index))
  143. pdfView?.setNeedsDisplayForVisiblePages()
  144. delegate?.handdler?(self, didAdd: self.bookmark(for: index), info: nil)
  145. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_remove(label: label, index: index)
  146. }
  147. @objc private func _undo_removeAll(bookmarks: [CPDFBookmark]) {
  148. for bookmark in bookmarks {
  149. document?.removeBookmark(forPageIndex: UInt(bookmark.pageIndex))
  150. }
  151. pdfView?.setNeedsDisplayForVisiblePages()
  152. delegate?.handdler?(self, didRemoveAll: nil)
  153. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_removeAll_back(bookmarks: bookmarks)
  154. }
  155. @objc private func _undo_removeAll_back(bookmarks: [CPDFBookmark]) {
  156. for bookmark in bookmarks {
  157. document?.addBookmark(bookmark.label, forPageIndex: UInt(bookmark.pageIndex))
  158. }
  159. pdfView?.setNeedsDisplayForVisiblePages()
  160. delegate?.handdler?(self, didRemoveAll: nil)
  161. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_removeAll(bookmarks: bookmarks)
  162. }
  163. @objc private func _undo_rename(bookmark: CPDFBookmark, label: String) {
  164. let theLabel = bookmark.label ?? ""
  165. bookmark.label = label
  166. delegate?.handdler?(self, didRename: bookmark, info: nil)
  167. (pdfView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_rename(bookmark: bookmark, label: theLabel)
  168. }
  169. }