KMPDFViewAnnotationOnceModeStore.swift 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // KMPDFViewAnnotationOnceModeStore.swift
  3. // PDF Office
  4. //
  5. // Created by tangchao on 2023/4/20.
  6. //
  7. import Cocoa
  8. class KMPDFViewRemoveAllAnnotationsStore: KMMemorandumPattern<CPDFListView> {
  9. internal var annotations: [CPDFAnnotation] = []
  10. internal var undoManager = UndoManager()
  11. override func store(t: CPDFListView) {
  12. // 清空
  13. self.annotations.removeAll()
  14. // 存储所有注释
  15. for pageIdx in 0 ..< t.document.pageCount {
  16. let page = t.document.page(at: pageIdx)
  17. if let annotations = page?.annotations {
  18. self.annotations.append(contentsOf: annotations)
  19. }
  20. page?.removeAllAnnotations()
  21. }
  22. // 刷新UI
  23. t.setNeedsDisplayForVisiblePages()
  24. // undo
  25. t.undoManager?.registerUndo(withTarget: self) { target in
  26. target.restore(t: t)
  27. }
  28. }
  29. override func restore(t: CPDFListView) {
  30. // 恢复所有注释
  31. for anno in self.annotations {
  32. if (anno.page.annotations.contains(anno)) {
  33. continue
  34. }
  35. anno.page.addAnnotation(anno)
  36. }
  37. // 刷新UI
  38. t.setNeedsDisplayForVisiblePages()
  39. // 清空
  40. self.annotations.removeAll()
  41. // undo
  42. t.undoManager?.registerUndo(withTarget: self) { target in
  43. target.store(t: t)
  44. }
  45. }
  46. }