KMAnnotationModel.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // KMAnnotationModel.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/4/3.
  6. //
  7. import Cocoa
  8. class KMAnnotationModel: NSObject {
  9. weak var anno: CPDFAnnotation?
  10. }
  11. class KMBotaAnnotationModel: KMAnnotationModel {
  12. var showPage = true
  13. var showTime = true
  14. var showAuthor = true
  15. var foldType: KMFoldType = .none
  16. var foldH: CGFloat = 30
  17. func isFold() -> Bool {
  18. if self.foldType == .unfold {
  19. return false
  20. }
  21. return true
  22. }
  23. func canFold() -> Bool {
  24. guard let data = self.anno else {
  25. return false
  26. }
  27. if data is CPDFMarkupAnnotation {
  28. return true
  29. }
  30. if data is CPDFTextAnnotation {
  31. return true
  32. }
  33. return false
  34. }
  35. }
  36. class KMAnnotationListModel: NSObject {
  37. var datas: [KMBotaAnnotationModel] = []
  38. // 默认全部折叠
  39. var foldType: KMFoldType {
  40. set {
  41. for model in self.canFoldModel() {
  42. if newValue != .none {
  43. model.foldType = newValue
  44. }
  45. }
  46. }
  47. get {
  48. let canFolds = self.canFoldModel()
  49. if canFolds.isEmpty {
  50. return .none
  51. }
  52. // 是否全部折叠
  53. var isAll = true
  54. for model in canFolds {
  55. if model.isFold() == false {
  56. isAll = false
  57. break
  58. }
  59. }
  60. if isAll {
  61. return .fold
  62. }
  63. // 是否全部展开
  64. for model in canFolds {
  65. if model.isFold() {
  66. isAll = false
  67. break
  68. }
  69. }
  70. return isAll ? .unfold : .none
  71. }
  72. }
  73. func canFoldModel() -> [KMBotaAnnotationModel] {
  74. var models: [KMBotaAnnotationModel] = []
  75. for model in self.datas {
  76. if model.canFold() {
  77. models.append(model)
  78. }
  79. }
  80. return models
  81. }
  82. func isAllFold() -> Bool {
  83. for model in self.canFoldModel() {
  84. if model.isFold() == false {
  85. return false
  86. }
  87. }
  88. return true
  89. }
  90. }