MultiAnnotHistory.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using ComPDFKitViewer.Helper;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace ComPDFKit.Tool.UndoManger
  5. {
  6. public class MultiAnnotHistory : IHistory
  7. {
  8. public List<AnnotHistory> Histories { get; set; } = new List<AnnotHistory>();
  9. public bool Redo()
  10. {
  11. if (Histories != null && Histories.Count > 0)
  12. {
  13. bool success=true;
  14. foreach (AnnotHistory history in Histories)
  15. {
  16. success &= history.Redo();
  17. }
  18. return success;
  19. }
  20. return false;
  21. }
  22. public bool Undo()
  23. {
  24. if (Histories != null && Histories.Count > 0)
  25. {
  26. bool success = true;
  27. foreach (AnnotHistory history in Histories)
  28. {
  29. success &= history.Undo();
  30. }
  31. return success;
  32. }
  33. return false;
  34. }
  35. public void Check(IHistory checkItem, bool undo, bool redo, bool add)
  36. {
  37. List<AnnotHistory> checkAnnotList= new List<AnnotHistory>();
  38. if (checkItem is MultiAnnotHistory)
  39. {
  40. MultiAnnotHistory multiHistory = (MultiAnnotHistory)checkItem;
  41. foreach (AnnotHistory checkAnnot in multiHistory.Histories)
  42. {
  43. if (checkAnnot == null || checkAnnotList.Contains(checkAnnot))
  44. {
  45. continue;
  46. }
  47. checkAnnotList.Add(checkAnnot);
  48. }
  49. }
  50. if (checkItem is GroupHistory)
  51. {
  52. GroupHistory groupHistory = (GroupHistory)checkItem;
  53. foreach (IHistory checkHistory in groupHistory.Histories)
  54. {
  55. AnnotHistory checkAnnot = checkHistory as AnnotHistory;
  56. if (checkAnnot == null || checkAnnotList.Contains(checkAnnot))
  57. {
  58. continue;
  59. }
  60. checkAnnotList.Add(checkAnnot);
  61. }
  62. }
  63. if (checkItem is AnnotHistory)
  64. {
  65. checkAnnotList.Add((AnnotHistory)checkItem);
  66. }
  67. List<AnnotHistory> loopList = new List<AnnotHistory>();
  68. if (checkAnnotList != null && checkAnnotList.Count > 0)
  69. {
  70. List<int> pageList = checkAnnotList.AsEnumerable().Select(x => x.GetPageIndex()).Distinct().ToList();
  71. pageList.Sort();
  72. foreach (int pageIndex in pageList)
  73. {
  74. List<AnnotHistory> groupList = checkAnnotList.AsEnumerable().Where(x => x.GetPageIndex() == pageIndex).OrderByDescending(x => x.GetAnnotIndex()).ToList();
  75. if (groupList != null && groupList.Count > 0)
  76. {
  77. loopList.AddRange(groupList);
  78. }
  79. }
  80. }
  81. foreach (AnnotHistory checkAnnot in loopList)
  82. {
  83. if (add && checkAnnot.Action == HistoryAction.Remove)
  84. {
  85. //remove
  86. SubCurrentIndex(checkAnnot.GetPageIndex(),checkAnnot.GetAnnotIndex());
  87. }
  88. if (undo && checkAnnot.Action == HistoryAction.Add)
  89. {
  90. //remove
  91. SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
  92. }
  93. if (redo && checkAnnot.Action == HistoryAction.Remove)
  94. {
  95. //remove
  96. SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
  97. }
  98. if(undo && checkAnnot.Action == HistoryAction.Remove)
  99. {
  100. //add
  101. UpdateCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.HistoryIndex, checkAnnot.GetAnnotIndex());
  102. }
  103. if(redo && checkAnnot.Action == HistoryAction.Add)
  104. {
  105. //add
  106. UpdateCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.HistoryIndex, checkAnnot.GetAnnotIndex());
  107. }
  108. }
  109. }
  110. internal void SubCurrentIndex(int pageIndex, int annotIndex)
  111. {
  112. foreach (AnnotHistory annotHistory in Histories)
  113. {
  114. if (annotHistory.GetPageIndex() == pageIndex)
  115. {
  116. int oldIndex = annotHistory.GetAnnotIndex();
  117. if (oldIndex == annotIndex && oldIndex >= 0)
  118. {
  119. annotHistory.SetAnnotIndex(-1);
  120. annotHistory.HistoryIndex = -1;
  121. }
  122. else
  123. {
  124. if (oldIndex > annotIndex || oldIndex <= -1)
  125. {
  126. annotHistory.SetAnnotIndex(oldIndex - 1);
  127. annotHistory.HistoryIndex = oldIndex - 1;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. internal void UpdateCurrentIndex(int pageIndex,int prevIndex, int annotIndex)
  134. {
  135. foreach (AnnotHistory annotHistory in Histories)
  136. {
  137. if (annotHistory.GetPageIndex() == pageIndex && annotHistory.HistoryIndex==prevIndex)
  138. {
  139. annotHistory.SetAnnotIndex(annotIndex);
  140. annotHistory.HistoryIndex = annotIndex;
  141. }
  142. }
  143. }
  144. }
  145. }