MultiAnnotHistory.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using ComPDFKitViewer.Helper;
  2. using System.Collections.Generic;
  3. namespace ComPDFKit.Tool.UndoManger
  4. {
  5. public class MultiAnnotHistory : IHistory
  6. {
  7. public List<AnnotHistory> Histories { get; set; } = new List<AnnotHistory>();
  8. public bool Redo()
  9. {
  10. if (Histories != null && Histories.Count > 0)
  11. {
  12. bool success=true;
  13. foreach (AnnotHistory history in Histories)
  14. {
  15. success &= history.Redo();
  16. }
  17. return success;
  18. }
  19. return false;
  20. }
  21. public bool Undo()
  22. {
  23. if (Histories != null && Histories.Count > 0)
  24. {
  25. bool success = true;
  26. foreach (AnnotHistory history in Histories)
  27. {
  28. success &= history.Undo();
  29. }
  30. return success;
  31. }
  32. return false;
  33. }
  34. public void Check(IHistory checkItem, bool undo, bool redo, bool add)
  35. {
  36. List<AnnotHistory> checkAnnotList= new List<AnnotHistory>();
  37. if (checkItem is MultiAnnotHistory)
  38. {
  39. MultiAnnotHistory multiHistory = (MultiAnnotHistory)checkItem;
  40. foreach (AnnotHistory checkAnnot in multiHistory.Histories)
  41. {
  42. if (checkAnnot == null)
  43. {
  44. continue;
  45. }
  46. checkAnnotList.Add(checkAnnot);
  47. }
  48. }
  49. if (checkItem is AnnotHistory)
  50. {
  51. checkAnnotList.Add((AnnotHistory)checkItem);
  52. }
  53. foreach (AnnotHistory checkAnnot in checkAnnotList)
  54. {
  55. if (add && checkAnnot.Action == HistoryAction.Remove)
  56. {
  57. //remove
  58. SubCurrentIndex(checkAnnot.GetPageIndex(),checkAnnot.GetAnnotIndex());
  59. }
  60. if (undo && checkAnnot.Action == HistoryAction.Add)
  61. {
  62. //remove
  63. SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
  64. }
  65. if (redo && checkAnnot.Action == HistoryAction.Remove)
  66. {
  67. //remove
  68. SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
  69. }
  70. if(undo && checkAnnot.Action == HistoryAction.Remove)
  71. {
  72. //add
  73. UpdateCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.HistoryIndex, checkAnnot.GetAnnotIndex());
  74. }
  75. if(redo && checkAnnot.Action == HistoryAction.Add)
  76. {
  77. //add
  78. UpdateCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.HistoryIndex, checkAnnot.GetAnnotIndex());
  79. }
  80. }
  81. }
  82. internal void SubCurrentIndex(int pageIndex, int annotIndex)
  83. {
  84. foreach (AnnotHistory annotHistory in Histories)
  85. {
  86. if (annotHistory.GetPageIndex() == pageIndex)
  87. {
  88. int oldIndex = annotHistory.GetAnnotIndex();
  89. if (oldIndex > annotIndex || oldIndex<=-1)
  90. {
  91. annotHistory.SetAnnotIndex(oldIndex-1);
  92. }
  93. if (oldIndex == annotIndex)
  94. {
  95. annotHistory.SetAnnotIndex(-1);
  96. }
  97. }
  98. }
  99. }
  100. internal void UpdateCurrentIndex(int pageIndex,int prevIndex, int annotIndex)
  101. {
  102. foreach (AnnotHistory annotHistory in Histories)
  103. {
  104. if (annotHistory.GetPageIndex() == pageIndex && annotHistory.HistoryIndex==prevIndex)
  105. {
  106. annotHistory.SetAnnotIndex(annotIndex);
  107. }
  108. }
  109. }
  110. }
  111. }