KMNBookmarkHanddler.swift 5.5 KB

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