GroupHistory.cs 8.6 KB

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