KMNSearchHanddler.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // KMNSearchHanddler.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/12/1.
  6. //
  7. import Cocoa
  8. class KMNSearchHanddler: NSObject {
  9. weak var pdfView: CPDFView?
  10. var type: KMNBotaSearchType = .search
  11. var searchKey: String = ""
  12. var replaceKey: String = ""
  13. var showIdx = 0
  14. var searchSectionResults: [KMBotaSearchSectionModel] = []
  15. var searchResults: [KMSearchMode] = []
  16. func search(keyword: String, isCase: Bool, isWholeWord: Bool, callback: @escaping (([KMBotaSearchSectionModel]?) -> Void)) {
  17. guard let document = self.pdfView?.document else {
  18. NSSound.beep()
  19. callback(nil)
  20. return
  21. }
  22. if document.isFinding {
  23. document.cancelFindString()
  24. }
  25. if keyword.isEmpty {
  26. callback(nil)
  27. return
  28. }
  29. let theKeyword = keyword.decomposedStringWithCompatibilityMapping
  30. searchKey = theKeyword
  31. var opt = CPDFSearchOptions()
  32. if isCase {
  33. opt.insert(.caseSensitive)
  34. }
  35. if isWholeWord {
  36. opt.insert(.matchWholeWord)
  37. }
  38. DispatchQueue.global().async {
  39. let result = document.findString(theKeyword, with: opt)
  40. self.searchResults.removeAll()
  41. self.searchSectionResults.removeAll()
  42. for sels in result ?? [] {
  43. let sectionM = KMBotaSearchSectionModel()
  44. sectionM.pageIndex = Int(sels.first?.page.pageIndex() ?? 0)
  45. for sel in sels {
  46. let mode : KMSearchMode = KMSearchMode()
  47. mode.selection = sel
  48. mode.attributedString = KMOCToolClass.getAttributedString(selection: sel, keyword: theKeyword)
  49. mode.selectionPageIndex = document.index(for: sel.page)
  50. sectionM.items.append(mode)
  51. sel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
  52. self.searchResults.append(mode)
  53. }
  54. self.searchSectionResults.append(sectionM)
  55. }
  56. DispatchQueue.main.async {
  57. callback(self.searchSectionResults)
  58. }
  59. }
  60. }
  61. func replace(searchS: String, replaceS: String?, sel: CPDFSelection, callback: @escaping ((CPDFSelection?)->Void)) -> Bool {
  62. return self.pdfView?.document.replace(with: sel, search: searchS, toReplace: replaceS, completionHandler: { newSel in
  63. callback(newSel)
  64. }) ?? false
  65. }
  66. func showSelection(_ sel: CPDFSelection?) {
  67. guard let theSel = sel else {
  68. let isEditing = self.pdfView?.isEditing() ?? false
  69. if isEditing {
  70. self.pdfView?.setHighlightedSelection(nil, animated: true)
  71. } else {
  72. self.pdfView?.setHighlightedSelections([])
  73. }
  74. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  75. return
  76. }
  77. guard let document = self.pdfView?.document else {
  78. return
  79. }
  80. theSel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
  81. let pageIdx = document.index(for: theSel.page)
  82. self.pdfView?.go(toPageIndex: Int(pageIdx), animated: false)
  83. self.pdfView?.go(to: theSel.bounds, on: theSel.page)
  84. let isEditing = self.pdfView?.isEditing() ?? false
  85. if isEditing {
  86. self.pdfView?.setHighlightedSelection(theSel, animated: true)
  87. } else {
  88. self.pdfView?.setHighlightedSelections([theSel])
  89. }
  90. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  91. }
  92. func clearData() {
  93. let isEditing = self.pdfView?.isEditing() ?? false
  94. if isEditing {
  95. self.pdfView?.setHighlightedSelection(nil, animated: false)
  96. } else {
  97. self.pdfView?.setHighlightedSelections([])
  98. }
  99. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  100. }
  101. }