KMSearchReplaceHanddler.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // KMSearchReplaceHanddler.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/8/8.
  6. //
  7. import Cocoa
  8. class KMSearchReplaceHanddler: NSObject {
  9. weak var pdfView: CPDFView?
  10. var searchResults: [KMSearchMode] = []
  11. var showIdx = 0
  12. func search(key: String, isCase: Bool, display: Bool, needShowAll: Bool) {
  13. guard let document = self.pdfView?.document else {
  14. NSSound.beep()
  15. return
  16. }
  17. if document.isFinding {
  18. document.cancelFindString()
  19. }
  20. if key.isEmpty {
  21. self.searchResults = []
  22. // self.leftSideViewController.searchResults = self.searchResults
  23. self.pdfView?.setHighlightedSelections([])
  24. self.pdfView?.setHighlightedSelection(nil, animated: false)
  25. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  26. } else {
  27. let wholeWordSearch = isCase == true ? 1 : 0
  28. var findArray : [[CPDFSelection]]
  29. if isCase {
  30. findArray = document.findString(key, with: .matchWholeWord) ?? []
  31. } else {
  32. findArray = document.findString(key, with: [.caseSensitive, .matchWholeWord]) ?? []
  33. }
  34. self.searchResults.removeAll()
  35. var _selections: [CPDFSelection] = []
  36. for selections in findArray {
  37. for selection in selections {
  38. let mode : KMSearchMode = KMSearchMode()
  39. mode.selection = selection
  40. mode.attributedString = KMOCToolClass.getAttributedString(selection: selection, keyword: key)
  41. mode.selectionPageIndex = document.index(for: selection.page)
  42. self.searchResults.insert(mode, at: self.searchResults.count)
  43. _selections.append(selection)
  44. selection.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
  45. // self.listView.setHighlight(selection, forBorderColor: .yellow, fill: .red, animated: false)
  46. }
  47. // self.listView.setHighlightedSelections(selections )
  48. }
  49. if needShowAll {
  50. self.pdfView?.setHighlightedSelections(_selections)
  51. }
  52. if _selections.isEmpty {
  53. self.pdfView?.setHighlightedSelection(nil, animated: false)
  54. }
  55. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  56. // self.leftSideViewController.searchResults = self.searchResults
  57. }
  58. // if display {
  59. // if self.leftSideViewController.findPaneState == .singular {
  60. // self.leftSideViewController.displayFindViewAnimating(true)
  61. // } else {
  62. // self.leftSideViewController.displayGroupedFindViewAnimating(true)
  63. // }
  64. // }
  65. }
  66. func search(keyword: String, isCase: Bool, isWholeWord: Bool, callback: @escaping (([KMSearchMode]?) -> Void)) {
  67. guard let document = self.pdfView?.document else {
  68. NSSound.beep()
  69. callback(nil)
  70. return
  71. }
  72. if document.isFinding {
  73. document.cancelFindString()
  74. }
  75. if keyword.isEmpty {
  76. callback(nil)
  77. return
  78. }
  79. let theKeyword = keyword.decomposedStringWithCompatibilityMapping
  80. var opt = CPDFSearchOptions()
  81. if isCase {
  82. opt.insert(.caseSensitive)
  83. }
  84. if isWholeWord {
  85. opt.insert(.matchWholeWord)
  86. }
  87. DispatchQueue.global().async {
  88. let result = document.findString(theKeyword, with: opt)
  89. self.searchResults.removeAll()
  90. for sels in result ?? [] {
  91. for sel in sels {
  92. let mode : KMSearchMode = KMSearchMode()
  93. mode.selection = sel
  94. mode.attributedString = KMOCToolClass.getAttributedString(selection: sel, keyword: theKeyword)
  95. mode.selectionPageIndex = document.index(for: sel.page)
  96. self.searchResults.insert(mode, at: self.searchResults.count)
  97. sel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
  98. }
  99. }
  100. DispatchQueue.main.async {
  101. callback(self.searchResults)
  102. }
  103. }
  104. }
  105. func newSearch(keyword: String, isCase: Bool, isWholeWord: Bool, callback: @escaping (([KMSearchMode]?) -> Void)) {
  106. guard let document = self.pdfView?.document else {
  107. NSSound.beep()
  108. callback(nil)
  109. return
  110. }
  111. if document.isFinding {
  112. document.cancelFindString()
  113. }
  114. if keyword.isEmpty {
  115. callback(nil)
  116. return
  117. }
  118. let theKeyword = keyword.decomposedStringWithCompatibilityMapping
  119. var opt = CPDFSearchOptions()
  120. if isCase {
  121. opt.insert(.caseSensitive)
  122. }
  123. if isWholeWord {
  124. opt.insert(.matchWholeWord)
  125. }
  126. DispatchQueue.global().async {
  127. let result = document.findString(theKeyword, with: opt)
  128. self.searchResults.removeAll()
  129. for sels in result ?? [] {
  130. for sel in sels {
  131. let mode : KMSearchMode = KMSearchMode()
  132. mode.selection = sel
  133. mode.attributedString = KMOCToolClass.getAttributedString(selection: sel, keyword: theKeyword)
  134. mode.selectionPageIndex = document.index(for: sel.page)
  135. self.searchResults.insert(mode, at: self.searchResults.count)
  136. sel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
  137. }
  138. }
  139. DispatchQueue.main.async {
  140. callback(self.searchResults)
  141. }
  142. }
  143. }
  144. // func findEdit
  145. func replace(searchS: String, replaceS: String?, sel: CPDFSelection, callback: @escaping ((CPDFSelection?)->Void)) -> Bool {
  146. return self.pdfView?.document.replace(with: sel, search: searchS, toReplace: replaceS, completionHandler: { newSel in
  147. callback(newSel)
  148. }) ?? false
  149. }
  150. func showSelection(_ sel: CPDFSelection?) {
  151. guard let theSel = sel else {
  152. let isEditing = self.pdfView?.isEditing() ?? false
  153. if isEditing {
  154. self.pdfView?.setHighlightedSelection(nil, animated: true)
  155. } else {
  156. self.pdfView?.setHighlightedSelections([])
  157. }
  158. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  159. return
  160. }
  161. guard let document = self.pdfView?.document else {
  162. return
  163. }
  164. theSel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
  165. let pageIdx = document.index(for: theSel.page)
  166. self.pdfView?.go(toPageIndex: Int(pageIdx), animated: false)
  167. self.pdfView?.go(to: theSel.bounds, on: theSel.page)
  168. let isEditing = self.pdfView?.isEditing() ?? false
  169. if isEditing {
  170. self.pdfView?.setHighlightedSelection(theSel, animated: true)
  171. } else {
  172. self.pdfView?.setHighlightedSelections([theSel])
  173. }
  174. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  175. }
  176. func clearData() {
  177. let isEditing = self.pdfView?.isEditing() ?? false
  178. if isEditing {
  179. self.pdfView?.setHighlightedSelection(nil, animated: false)
  180. } else {
  181. self.pdfView?.setHighlightedSelections([])
  182. }
  183. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  184. }
  185. }