KMNSearchHanddler.swift 4.5 KB

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