// // 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() } }