// // KMSearchMode.swift // PDF Reader Pro // // Created by lxy on 2022/12/9. // import Cocoa class KMBotaSearchSectionModel: NSObject { var items: [KMSearchMode] = [] var pageIndex: Int = 0 var itemCount: Int { get { return self.items.count } } var isExpand = false } class KMSearchMode: NSObject { var selection : CPDFSelection = CPDFSelection() var attributedString : NSMutableAttributedString = NSMutableAttributedString() var selectionPageIndex : UInt = 0 var datas : [KMSearchMode] = [] var select: Bool = false var hover: Bool = false func fetchSelection(document: CPDFDocument?) -> CPDFSelection? { guard let doc = document else { return nil } if self.selectionPageIndex < 0 || self.selectionPageIndex >= doc.pageCount { return nil } guard let page = document?.page(at: self.selectionPageIndex) else { return nil } return CPDFSelection(page: page, rect: self.selection.bounds) } class func sortSearchResult(results:[KMSearchMode]) ->[KMSearchMode] { var sortResult : [KMSearchMode] = [] var pageIndexs : [UInt] = [] for mode in results { pageIndexs.append(mode.selectionPageIndex) } if Set(pageIndexs).count != pageIndexs.count { pageIndexs = Array(Set(pageIndexs)) } pageIndexs = pageIndexs.sorted() for index in pageIndexs { var indexs : [KMSearchMode] = [] for result in results { if result.selectionPageIndex == index { indexs.append(result) } } var s : KMSearchMode = KMSearchMode() s.datas = indexs s.selectionPageIndex = index sortResult.append(s) } return sortResult } }