KMSearchReplaceHanddler.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // mwcFlags.wholeWordSearch = isCase == true ? 1 : 0
  28. let wholeWordSearch = isCase == true ? 1 : 0
  29. var findArray : [[CPDFSelection]]
  30. if isCase {
  31. // if self.leftSideViewController.mwcFlags.wholeWordSearch == 1 {
  32. findArray = document.findString(key, with: .matchWholeWord) ?? []
  33. // } else {
  34. // findArray = self.listView.document.findString(searchString) ?? []
  35. // }
  36. } else {
  37. // if self.leftSideViewController.mwcFlags.wholeWordSearch == 1 {
  38. findArray = document.findString(key, with: [.caseSensitive, .matchWholeWord]) ?? []
  39. // } else {
  40. // findArray = self.listView.document.findString(searchString, with: .caseSensitive) ?? []
  41. // }
  42. }
  43. self.searchResults.removeAll()
  44. var _selections: [CPDFSelection] = []
  45. for selections in findArray {
  46. for selection in selections {
  47. let mode : KMSearchMode = KMSearchMode()
  48. mode.selection = selection
  49. mode.attributedString = KMOCToolClass.getAttributedString(selection: selection, keyword: key)
  50. mode.selectionPageIndex = document.index(for: selection.page)
  51. self.searchResults.insert(mode, at: self.searchResults.count)
  52. _selections.append(selection)
  53. selection.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
  54. // self.listView.setHighlight(selection, forBorderColor: .yellow, fill: .red, animated: false)
  55. }
  56. // self.listView.setHighlightedSelections(selections )
  57. }
  58. if needShowAll {
  59. self.pdfView?.setHighlightedSelections(_selections)
  60. }
  61. if _selections.isEmpty {
  62. self.pdfView?.setHighlightedSelection(nil, animated: false)
  63. }
  64. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  65. // self.leftSideViewController.searchResults = self.searchResults
  66. }
  67. // if display {
  68. // if self.leftSideViewController.findPaneState == .singular {
  69. // self.leftSideViewController.displayFindViewAnimating(true)
  70. // } else {
  71. // self.leftSideViewController.displayGroupedFindViewAnimating(true)
  72. // }
  73. // }
  74. }
  75. func search(keyword: String, isCase: Bool, isWholeWord: Bool, callback: @escaping (([KMSearchMode]?) -> Void)) {
  76. guard let document = self.pdfView?.document else {
  77. NSSound.beep()
  78. callback(nil)
  79. return
  80. }
  81. if document.isFinding {
  82. document.cancelFindString()
  83. }
  84. if keyword.isEmpty {
  85. callback(nil)
  86. return
  87. }
  88. let theKeyword = keyword.decomposedStringWithCompatibilityMapping
  89. var opt = CPDFSearchOptions()
  90. if isCase {
  91. opt.insert(.caseSensitive)
  92. }
  93. if isWholeWord {
  94. opt.insert(.matchWholeWord)
  95. }
  96. DispatchQueue.global().async {
  97. let result = document.findString(theKeyword, with: opt)
  98. self.searchResults.removeAll()
  99. for sels in result ?? [] {
  100. for sel in sels {
  101. let mode : KMSearchMode = KMSearchMode()
  102. mode.selection = sel
  103. mode.attributedString = KMOCToolClass.getAttributedString(selection: sel, keyword: theKeyword)
  104. mode.selectionPageIndex = document.index(for: sel.page)
  105. self.searchResults.insert(mode, at: self.searchResults.count)
  106. sel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
  107. }
  108. }
  109. DispatchQueue.main.async {
  110. callback(self.searchResults)
  111. }
  112. }
  113. }
  114. // func findEdit
  115. func replace(searchS: String, replaceS: String?, sel: CPDFSelection, callback: @escaping ((CPDFSelection?)->Void)) -> Bool {
  116. return self.pdfView?.document.replace(with: sel, search: searchS, toReplace: replaceS, completionHandler: { newSel in
  117. callback(newSel)
  118. }) ?? false
  119. }
  120. func showSelection(_ sel: CPDFSelection?) {
  121. guard let theSel = sel else {
  122. return
  123. }
  124. guard let document = self.pdfView?.document else {
  125. return
  126. }
  127. theSel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
  128. let pageIdx = document.index(for: theSel.page)
  129. self.pdfView?.go(toPageIndex: Int(pageIdx), animated: false)
  130. self.pdfView?.go(to: theSel.bounds, on: theSel.page)
  131. let isEditing = self.pdfView?.isEditing() ?? false
  132. if isEditing {
  133. self.pdfView?.setHighlightedSelection(theSel, animated: true)
  134. } else {
  135. self.pdfView?.setHighlightedSelections([theSel])
  136. }
  137. self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
  138. }
  139. }