KMPDFViewAnnotationOnceModeStore.swift 1.9 KB

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