123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- //
- // KMSearchReplaceHanddler.swift
- // PDF Reader Pro
- //
- // Created by User-Tangchao on 2024/8/8.
- //
- import Cocoa
- class KMSearchReplaceHanddler: NSObject {
- weak var pdfView: CPDFView?
-
- var searchResults: [KMSearchMode] = []
- var showIdx = 0
-
- func search(key: String, isCase: Bool, display: Bool, needShowAll: Bool) {
- guard let document = self.pdfView?.document else {
- NSSound.beep()
- return
- }
- if document.isFinding {
- document.cancelFindString()
- }
- if key.isEmpty {
- self.searchResults = []
- // self.leftSideViewController.searchResults = self.searchResults
- self.pdfView?.setHighlightedSelections([])
- self.pdfView?.setHighlightedSelection(nil, animated: false)
- self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
- } else {
- let wholeWordSearch = isCase == true ? 1 : 0
- var findArray : [[CPDFSelection]]
- if isCase {
- findArray = document.findString(key, with: .matchWholeWord) ?? []
- } else {
- findArray = document.findString(key, with: [.caseSensitive, .matchWholeWord]) ?? []
-
- }
- self.searchResults.removeAll()
- var _selections: [CPDFSelection] = []
- for selections in findArray {
- for selection in selections {
- let mode : KMSearchMode = KMSearchMode()
- mode.selection = selection
- mode.attributedString = KMOCToolClass.getAttributedString(selection: selection, keyword: key)
- mode.selectionPageIndex = document.index(for: selection.page)
- self.searchResults.insert(mode, at: self.searchResults.count)
- _selections.append(selection)
- selection.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
- // self.listView.setHighlight(selection, forBorderColor: .yellow, fill: .red, animated: false)
- }
- // self.listView.setHighlightedSelections(selections )
- }
-
- if needShowAll {
- self.pdfView?.setHighlightedSelections(_selections)
- }
- if _selections.isEmpty {
- self.pdfView?.setHighlightedSelection(nil, animated: false)
- }
- self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
- // self.leftSideViewController.searchResults = self.searchResults
- }
-
- // if display {
- // if self.leftSideViewController.findPaneState == .singular {
- // self.leftSideViewController.displayFindViewAnimating(true)
- // } else {
- // self.leftSideViewController.displayGroupedFindViewAnimating(true)
- // }
- // }
- }
-
- func search(keyword: String, isCase: Bool, isWholeWord: Bool, callback: @escaping (([KMSearchMode]?) -> Void)) {
- guard let document = self.pdfView?.document else {
- NSSound.beep()
- callback(nil)
- return
- }
- if document.isFinding {
- document.cancelFindString()
- }
- if keyword.isEmpty {
- callback(nil)
- return
- }
-
- let theKeyword = keyword.decomposedStringWithCompatibilityMapping
- var opt = CPDFSearchOptions()
- if isCase {
- opt.insert(.caseSensitive)
- }
- if isWholeWord {
- opt.insert(.matchWholeWord)
- }
- DispatchQueue.global().async {
- let result = document.findString(theKeyword, with: opt)
-
- self.searchResults.removeAll()
- for sels in result ?? [] {
- for sel in sels {
- let mode : KMSearchMode = KMSearchMode()
- mode.selection = sel
- mode.attributedString = KMOCToolClass.getAttributedString(selection: sel, keyword: theKeyword)
- mode.selectionPageIndex = document.index(for: sel.page)
- self.searchResults.insert(mode, at: self.searchResults.count)
- sel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
- }
- }
-
- DispatchQueue.main.async {
- callback(self.searchResults)
- }
- }
- }
-
- func newSearch(keyword: String, isCase: Bool, isWholeWord: Bool, callback: @escaping (([KMSearchMode]?) -> Void)) {
- guard let document = self.pdfView?.document else {
- NSSound.beep()
- callback(nil)
- return
- }
- if document.isFinding {
- document.cancelFindString()
- }
- if keyword.isEmpty {
- callback(nil)
- return
- }
-
- let theKeyword = keyword.decomposedStringWithCompatibilityMapping
- var opt = CPDFSearchOptions()
- if isCase {
- opt.insert(.caseSensitive)
- }
- if isWholeWord {
- opt.insert(.matchWholeWord)
- }
- DispatchQueue.global().async {
- let result = document.findString(theKeyword, with: opt)
-
- self.searchResults.removeAll()
- for sels in result ?? [] {
- for sel in sels {
- let mode : KMSearchMode = KMSearchMode()
- mode.selection = sel
- mode.attributedString = KMOCToolClass.getAttributedString(selection: sel, keyword: theKeyword)
- mode.selectionPageIndex = document.index(for: sel.page)
- self.searchResults.insert(mode, at: self.searchResults.count)
- sel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
- }
- }
-
- DispatchQueue.main.async {
- callback(self.searchResults)
- }
- }
- }
-
- // func findEdit
-
- func replace(searchS: String, replaceS: String?, sel: CPDFSelection, callback: @escaping ((CPDFSelection?)->Void)) -> Bool {
- return self.pdfView?.document.replace(with: sel, search: searchS, toReplace: replaceS, completionHandler: { newSel in
- callback(newSel)
- }) ?? false
- }
-
- func showSelection(_ sel: CPDFSelection?) {
- guard let theSel = sel else {
- let isEditing = self.pdfView?.isEditing() ?? false
- if isEditing {
- self.pdfView?.setHighlightedSelection(nil, animated: true)
- } else {
- self.pdfView?.setHighlightedSelections([])
- }
- self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
- return
- }
- guard let document = self.pdfView?.document else {
- return
- }
-
- theSel.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
-
- let pageIdx = document.index(for: theSel.page)
- self.pdfView?.go(toPageIndex: Int(pageIdx), animated: false)
- self.pdfView?.go(to: theSel.bounds, on: theSel.page)
- let isEditing = self.pdfView?.isEditing() ?? false
- if isEditing {
- self.pdfView?.setHighlightedSelection(theSel, animated: true)
- } else {
- self.pdfView?.setHighlightedSelections([theSel])
- }
- self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
- }
-
- func clearData() {
- let isEditing = self.pdfView?.isEditing() ?? false
- if isEditing {
- self.pdfView?.setHighlightedSelection(nil, animated: false)
- } else {
- self.pdfView?.setHighlightedSelections([])
- }
- self.pdfView?.setNeedsDisplayAnnotationViewForVisiblePages()
- }
- }
|