AnnotHistory.cs 7.8 KB

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