KMSearchMode.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // KMSearchMode.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/12/9.
  6. //
  7. import Cocoa
  8. class KMSearchMode: NSObject {
  9. var selection : CPDFSelection = CPDFSelection()
  10. var attributedString : NSMutableAttributedString = NSMutableAttributedString()
  11. var selectionPageIndex : UInt = 0
  12. var datas : [KMSearchMode] = []
  13. var select: Bool = false
  14. var hover: Bool = false
  15. func fetchSelection(document: CPDFDocument?) -> CPDFSelection? {
  16. guard let doc = document else {
  17. return nil
  18. }
  19. if self.selectionPageIndex < 0 || self.selectionPageIndex >= doc.pageCount {
  20. return nil
  21. }
  22. guard let page = document?.page(at: self.selectionPageIndex) else {
  23. return nil
  24. }
  25. return CPDFSelection(page: page, rect: self.selection.bounds)
  26. }
  27. class func sortSearchResult(results:[KMSearchMode]) ->[KMSearchMode] {
  28. var sortResult : [KMSearchMode] = []
  29. var pageIndexs : [UInt] = []
  30. for mode in results {
  31. pageIndexs.append(mode.selectionPageIndex)
  32. }
  33. if Set(pageIndexs).count != pageIndexs.count {
  34. pageIndexs = Array(Set(pageIndexs))
  35. }
  36. pageIndexs = pageIndexs.sorted()
  37. for index in pageIndexs {
  38. var indexs : [KMSearchMode] = []
  39. for result in results {
  40. if result.selectionPageIndex == index {
  41. indexs.append(result)
  42. }
  43. }
  44. var s : KMSearchMode = KMSearchMode()
  45. s.datas = indexs
  46. s.selectionPageIndex = index
  47. sortResult.append(s)
  48. }
  49. return sortResult
  50. }
  51. }