KMSearchMode.swift 1.9 KB

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