AnnotHistory.cs 6.4 KB

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