GroupHistory.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using ComPDFKitViewer.Helper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ComPDFKit.Tool.UndoManger
  8. {
  9. public class GroupHistory : IHistory
  10. {
  11. public List<IHistory> Histories { get; set; } = new List<IHistory>();
  12. public bool Redo()
  13. {
  14. if (Histories != null && Histories.Count > 0)
  15. {
  16. bool success = true;
  17. List<AnnotHistory> checkAnnotList = new List<AnnotHistory>();
  18. List<IHistory> OtherList = new List<IHistory>();
  19. foreach (IHistory history in Histories)
  20. {
  21. if (history is AnnotHistory)
  22. {
  23. checkAnnotList.Add(history as AnnotHistory);
  24. continue;
  25. }
  26. OtherList.Add(history);
  27. }
  28. List<AnnotHistory> loopList = new List<AnnotHistory>();
  29. if (checkAnnotList != null && checkAnnotList.Count > 0)
  30. {
  31. List<int> pageList = checkAnnotList.AsEnumerable().Select(x => x.GetPageIndex()).Distinct().ToList();
  32. pageList.Sort();
  33. foreach (int pageIndex in pageList)
  34. {
  35. List<AnnotHistory> groupList = checkAnnotList.AsEnumerable().Where(x => x.GetPageIndex() == pageIndex).OrderByDescending(x => x.GetAnnotIndex()).ToList();
  36. if (groupList != null && groupList.Count > 0)
  37. {
  38. loopList.AddRange(groupList);
  39. }
  40. }
  41. }
  42. foreach (AnnotHistory history in loopList)
  43. {
  44. success &= history.Redo();
  45. }
  46. foreach (IHistory history in OtherList)
  47. {
  48. success &= history.Redo();
  49. }
  50. return success;
  51. }
  52. return false;
  53. }
  54. public bool Undo()
  55. {
  56. if (Histories != null && Histories.Count > 0)
  57. {
  58. bool success = true;
  59. List<AnnotHistory> checkAnnotList = new List<AnnotHistory>();
  60. List<IHistory> OtherList = new List<IHistory>();
  61. foreach (IHistory history in Histories)
  62. {
  63. if (history is AnnotHistory)
  64. {
  65. checkAnnotList.Add(history as AnnotHistory);
  66. continue;
  67. }
  68. OtherList.Add(history);
  69. }
  70. List<AnnotHistory> loopList = new List<AnnotHistory>();
  71. if (checkAnnotList != null && checkAnnotList.Count > 0)
  72. {
  73. List<int> pageList = checkAnnotList.AsEnumerable().Select(x => x.GetPageIndex()).Distinct().ToList();
  74. pageList.Sort();
  75. foreach (int pageIndex in pageList)
  76. {
  77. List<AnnotHistory> groupList = checkAnnotList.AsEnumerable().Where(x => x.GetPageIndex() == pageIndex).OrderByDescending(x => x.GetAnnotIndex()).ToList();
  78. if (groupList != null && groupList.Count > 0)
  79. {
  80. loopList.AddRange(groupList);
  81. }
  82. }
  83. }
  84. foreach (AnnotHistory history in loopList)
  85. {
  86. success &= history.Undo();
  87. }
  88. foreach (IHistory history in OtherList)
  89. {
  90. success &= history.Undo();
  91. }
  92. return success;
  93. }
  94. return false;
  95. }
  96. public void Check(IHistory checkItem, bool undo, bool redo, bool add)
  97. {
  98. List<AnnotHistory> checkAnnotList = new List<AnnotHistory>();
  99. if (checkItem is MultiAnnotHistory)
  100. {
  101. MultiAnnotHistory multiHistory = (MultiAnnotHistory)checkItem;
  102. foreach (AnnotHistory checkAnnot in multiHistory.Histories)
  103. {
  104. if (checkAnnot == null || checkAnnotList.Contains(checkAnnot))
  105. {
  106. continue;
  107. }
  108. checkAnnotList.Add(checkAnnot);
  109. }
  110. }
  111. if (checkItem is GroupHistory)
  112. {
  113. GroupHistory groupHistory = (GroupHistory)checkItem;
  114. foreach(IHistory checkHistory in groupHistory.Histories)
  115. {
  116. AnnotHistory checkAnnot = checkHistory as AnnotHistory;
  117. if (checkAnnot == null || checkAnnotList.Contains(checkAnnot))
  118. {
  119. continue;
  120. }
  121. checkAnnotList.Add(checkAnnot);
  122. }
  123. }
  124. if (checkItem is AnnotHistory)
  125. {
  126. checkAnnotList.Add((AnnotHistory)checkItem);
  127. }
  128. List<AnnotHistory> loopList = new List<AnnotHistory>();
  129. if (checkAnnotList != null && checkAnnotList.Count > 0)
  130. {
  131. List<int> pageList = checkAnnotList.AsEnumerable().Select(x => x.GetPageIndex()).Distinct().ToList();
  132. pageList.Sort();
  133. foreach (int pageIndex in pageList)
  134. {
  135. List<AnnotHistory> groupList = checkAnnotList.AsEnumerable().Where(x => x.GetPageIndex() == pageIndex).OrderByDescending(x => x.GetAnnotIndex()).ToList();
  136. if (groupList != null && groupList.Count > 0)
  137. {
  138. loopList.AddRange(groupList);
  139. }
  140. }
  141. }
  142. foreach (AnnotHistory checkAnnot in loopList)
  143. {
  144. if (add && checkAnnot.Action == HistoryAction.Remove)
  145. {
  146. //remove
  147. SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
  148. }
  149. if (undo && checkAnnot.Action == HistoryAction.Add)
  150. {
  151. //remove
  152. SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
  153. }
  154. if (redo && checkAnnot.Action == HistoryAction.Remove)
  155. {
  156. //remove
  157. SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
  158. }
  159. if (undo && checkAnnot.Action == HistoryAction.Remove)
  160. {
  161. //add
  162. UpdateCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.HistoryIndex, checkAnnot.GetAnnotIndex());
  163. }
  164. if (redo && checkAnnot.Action == HistoryAction.Add)
  165. {
  166. //add
  167. UpdateCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.HistoryIndex, checkAnnot.GetAnnotIndex());
  168. }
  169. }
  170. }
  171. internal void SubCurrentIndex(int pageIndex, int annotIndex)
  172. {
  173. foreach (AnnotHistory annotHistory in Histories)
  174. {
  175. if (annotHistory.GetPageIndex() == pageIndex)
  176. {
  177. int oldIndex = annotHistory.GetAnnotIndex();
  178. if (oldIndex == annotIndex && oldIndex >= 0)
  179. {
  180. annotHistory.SetAnnotIndex(-1);
  181. annotHistory.HistoryIndex = -1;
  182. }
  183. else
  184. {
  185. if (oldIndex > annotIndex || oldIndex <= -1)
  186. {
  187. annotHistory.SetAnnotIndex(oldIndex - 1);
  188. annotHistory.HistoryIndex = oldIndex - 1;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. internal void UpdateCurrentIndex(int pageIndex, int prevIndex, int annotIndex)
  195. {
  196. foreach (AnnotHistory annotHistory in Histories)
  197. {
  198. if (annotHistory.GetPageIndex() == pageIndex && annotHistory.HistoryIndex == prevIndex)
  199. {
  200. annotHistory.SetAnnotIndex(annotIndex);
  201. annotHistory.HistoryIndex = annotIndex;
  202. }
  203. }
  204. }
  205. }
  206. }