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. //而且 checkAnnotList 要从大到小排序
  68. List<AnnotHistory> loopList = new List<AnnotHistory>();
  69. if (checkAnnotList != null && checkAnnotList.Count > 0)
  70. {
  71. List<int> pageList = checkAnnotList.AsEnumerable().Select(x => x.GetPageIndex()).Distinct().ToList();
  72. pageList.Sort();
  73. foreach (int pageIndex in pageList)
  74. {
  75. List<AnnotHistory> groupList = checkAnnotList.AsEnumerable().Where(x => x.GetPageIndex() == pageIndex).OrderByDescending(x => x.GetAnnotIndex()).ToList();
  76. if (groupList != null && groupList.Count > 0)
  77. {
  78. loopList.AddRange(groupList);
  79. }
  80. }
  81. }
  82. foreach (AnnotHistory checkAnnot in loopList)
  83. {
  84. if (add && checkAnnot.Action == HistoryAction.Remove)
  85. {
  86. //remove
  87. SubCurrentIndex(checkAnnot.GetPageIndex(),checkAnnot.GetAnnotIndex());
  88. }
  89. if (undo && checkAnnot.Action == HistoryAction.Add)
  90. {
  91. //remove
  92. SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
  93. }
  94. if (redo && checkAnnot.Action == HistoryAction.Remove)
  95. {
  96. //remove
  97. SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
  98. }
  99. if(undo && checkAnnot.Action == HistoryAction.Remove)
  100. {
  101. //add
  102. UpdateCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.HistoryIndex, checkAnnot.GetAnnotIndex());
  103. }
  104. if(redo && checkAnnot.Action == HistoryAction.Add)
  105. {
  106. //add
  107. UpdateCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.HistoryIndex, checkAnnot.GetAnnotIndex());
  108. }
  109. }
  110. }
  111. internal void SubCurrentIndex(int pageIndex, int annotIndex)
  112. {
  113. foreach (AnnotHistory annotHistory in Histories)
  114. {
  115. if (annotHistory.GetPageIndex() == pageIndex)
  116. {
  117. int oldIndex = annotHistory.GetAnnotIndex();
  118. if (oldIndex == annotIndex && oldIndex >= 0)
  119. {
  120. annotHistory.SetAnnotIndex(-1);
  121. annotHistory.HistoryIndex = -1;
  122. }
  123. else
  124. {
  125. if (oldIndex > annotIndex || oldIndex <= -1)
  126. {
  127. annotHistory.SetAnnotIndex(oldIndex - 1);
  128. annotHistory.HistoryIndex = oldIndex - 1;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. internal void UpdateCurrentIndex(int pageIndex,int prevIndex, int annotIndex)
  135. {
  136. foreach (AnnotHistory annotHistory in Histories)
  137. {
  138. if (annotHistory.GetPageIndex() == pageIndex && annotHistory.HistoryIndex==prevIndex)
  139. {
  140. annotHistory.SetAnnotIndex(annotIndex);
  141. annotHistory.HistoryIndex = annotIndex;
  142. }
  143. }
  144. }
  145. }
  146. }