KMNSearchHanddler.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 resultCount = 0
  15. var searchSectionResults: [KMBotaSearchSectionModel] = []
  16. var searchResults: [KMSearchMode] = []
  17. func hideNotes() -> Bool {
  18. let listView = pdfView as? CPDFListView
  19. return listView?.hideNotes ?? false
  20. }
  21. func allowsNotes() -> Bool {
  22. let listView = pdfView as? CPDFListView
  23. return listView?.allowsNotes() ?? false
  24. }
  25. func pdfDocument() -> CPDFDocument? {
  26. return pdfView?.document
  27. }
  28. func scaleFactor() -> CGFloat? {
  29. return pdfView?.scaleFactor
  30. }
  31. func isEditing() -> Bool {
  32. return pdfView?.isEditing() ?? false
  33. }
  34. func search(keyword: String, isCase: Bool, isWholeWord: Bool, callback: @escaping (([KMBotaSearchSectionModel]?) -> Void)) {
  35. guard let document = self.pdfView?.document else {
  36. NSSound.beep()
  37. callback(nil)
  38. return
  39. }
  40. if document.isFinding {
  41. document.cancelFindString()
  42. }
  43. if keyword.isEmpty {
  44. callback(nil)
  45. return
  46. }
  47. let theKeyword = keyword.decomposedStringWithCompatibilityMapping
  48. searchKey = theKeyword
  49. var opt = CPDFSearchOptions()
  50. if isCase {
  51. opt.insert(.caseSensitive)
  52. }
  53. if isWholeWord {
  54. opt.insert(.matchWholeWord)
  55. }
  56. DispatchQueue.global().async {
  57. let result = document.findString(theKeyword, with: opt)
  58. self.searchResults.removeAll()
  59. self.searchSectionResults.removeAll()
  60. for sels in result ?? [] {
  61. let sectionM = KMBotaSearchSectionModel()
  62. sectionM.pageIndex = Int(sels.first?.page.pageIndex() ?? 0)
  63. for sel in sels {
  64. let mode : KMSearchMode = KMSearchMode()
  65. mode.selection = sel
  66. mode.attributedString = KMOCToolClass.getAttributedString(selection: sel, keyword: theKeyword)
  67. mode.selectionPageIndex = document.index(for: sel.page)
  68. sectionM.items.append(mode)
  69. sel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
  70. self.searchResults.append(mode)
  71. }
  72. self.searchSectionResults.append(sectionM)
  73. }
  74. DispatchQueue.main.async {
  75. callback(self.searchSectionResults)
  76. }
  77. }
  78. }
  79. func replace(searchS: String, replaceS: String?, sel: CPDFSelection, callback: @escaping ((CPDFSelection?)->Void)) -> Bool {
  80. return self.pdfView?.document.replace(with: sel, search: searchS, toReplace: replaceS, completionHandler: { newSel in
  81. callback(newSel)
  82. }) ?? false
  83. }
  84. func showSelection(_ sel: CPDFSelection?) {
  85. guard let theSel = sel else {
  86. let isEditing = self.pdfView?.isEditing() ?? false
  87. if isEditing {
  88. self.pdfView?.setHighlightedSelection(nil, animated: true)
  89. } else {
  90. self.pdfView?.setHighlightedSelections([])
  91. }
  92. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  93. return
  94. }
  95. guard let document = self.pdfView?.document else {
  96. return
  97. }
  98. theSel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
  99. let pageIdx = document.index(for: theSel.page)
  100. self.pdfView?.go(toPageIndex: Int(pageIdx), animated: false)
  101. self.pdfView?.go(to: theSel.bounds, on: theSel.page)
  102. let isEditing = self.pdfView?.isEditing() ?? false
  103. if isEditing {
  104. self.pdfView?.setHighlightedSelection(theSel, animated: true)
  105. } else {
  106. self.pdfView?.setHighlightedSelections([theSel])
  107. }
  108. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  109. }
  110. func clearData() {
  111. let isEditing = self.pdfView?.isEditing() ?? false
  112. if isEditing {
  113. self.pdfView?.setHighlightedSelection(nil, animated: false)
  114. } else {
  115. self.pdfView?.setHighlightedSelections([])
  116. }
  117. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  118. }
  119. }