123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer.Helper;
- using System.Collections.Generic;
- namespace ComPDFKit.Tool.UndoManger
- {
- public class AnnotHistory : IHistory
- {
- internal CPDFAnnotation Annot { get; set; }
- public CPDFDocument PDFDoc { get; set; }
- public HistoryAction Action { get; set; }
- internal int HistoryIndex { get; set; } = 0;
- public AnnotParam CurrentParam { get; set; }
- public AnnotParam PreviousParam { get; set; }
- public virtual int GetPageIndex()
- {
- return -1;
- }
- public virtual int GetAnnotIndex()
- {
- return -1;
- }
- public virtual void SetAnnotIndex(int newIndex)
- {
- }
- public AnnotHistory()
- {
- }
- public bool Redo()
- {
- bool result=false;
- switch(Action)
- {
- case HistoryAction.Add:
- result = Add();
- break;
- case HistoryAction.Remove:
- result = Remove();
- break;
- case HistoryAction.Update:
- result = Update(false);
- break;
- default:
- break;
- }
- return result;
- }
- public bool Undo()
- {
- bool result = false;
- switch (Action)
- {
- case HistoryAction.Add:
- result = Remove();
- break;
- case HistoryAction.Remove:
- result = Add();
- break;
- case HistoryAction.Update:
- result = Update(true);
- break;
- default:
- break;
- }
- return result;
- }
- public void Check(IHistory checkItem, bool undo, bool redo, bool add)
- {
- List<AnnotHistory> checkAnnotList = new List<AnnotHistory>();
- if (checkItem is MultiAnnotHistory)
- {
- MultiAnnotHistory multiHistory = (MultiAnnotHistory)checkItem;
- foreach (AnnotHistory checkAnnot in multiHistory.Histories)
- {
- if (checkAnnot == null)
- {
- continue;
- }
- checkAnnotList.Add(checkAnnot);
- }
- }
- if (checkItem is AnnotHistory)
- {
- checkAnnotList.Add((AnnotHistory)checkItem);
- }
- foreach (AnnotHistory checkAnnot in checkAnnotList)
- {
- if (add && checkAnnot.Action == HistoryAction.Remove)
- {
- //remove
- SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
- }
- if (undo && checkAnnot.Action == HistoryAction.Add)
- {
- //remove
- SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
- }
- if (redo && checkAnnot.Action == HistoryAction.Remove)
- {
- //remove
- SubCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.GetAnnotIndex());
- }
- if (undo && checkAnnot.Action == HistoryAction.Remove)
- {
- //add
- UpdateCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.HistoryIndex, checkAnnot.GetAnnotIndex());
- }
- if (redo && checkAnnot.Action == HistoryAction.Add)
- {
- //add
- UpdateCurrentIndex(checkAnnot.GetPageIndex(), checkAnnot.HistoryIndex, checkAnnot.GetAnnotIndex());
- }
- }
- }
- internal void SubCurrentIndex(int pageIndex, int annotIndex)
- {
- if (GetPageIndex() == pageIndex)
- {
- int oldIndex = GetAnnotIndex();
- if (oldIndex > annotIndex || oldIndex<=-1)
- {
- SetAnnotIndex(oldIndex - 1);
- HistoryIndex = oldIndex - 1;
- }
-
- if(oldIndex==annotIndex)
- {
- SetAnnotIndex(-1);
- HistoryIndex = -1;
- }
- }
- }
- internal void UpdateCurrentIndex(int pageIndex, int prevIndex, int annotIndex)
- {
- if (GetPageIndex() == pageIndex && HistoryIndex == prevIndex)
- {
- SetAnnotIndex(annotIndex);
- HistoryIndex = annotIndex;
- }
- }
- internal virtual bool Add()
- {
- return false;
- }
- internal virtual bool Remove()
- {
- return false;
- }
- internal virtual bool Update(bool isUndo)
- {
- return false;
- }
- internal bool MakeAnnotValid(AnnotParam currentParam)
- {
- if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
- {
- return false;
- }
-
- CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
- if (pdfPage != null)
- {
- List<CPDFAnnotation> annotList = pdfPage.GetAnnotations();
- if (annotList != null && annotList.Count > currentParam.AnnotIndex)
- {
- Annot = annotList[currentParam.AnnotIndex];
- if (Annot != null && Annot.IsValid())
- {
- return true;
- }
- }
- }
- return false;
- }
- internal bool CheckArrayEqual<T>(T[] compareA, T[] compareB)
- {
- if (compareA == compareB)
- {
- return true;
- }
- if(compareA!=null && compareB!=null)
- {
- if(compareA.Length != compareB.Length)
- {
- return false;
- }
- for (int i = 0; i < compareA.Length; i++)
- {
- if (!compareA[i].Equals(compareB[i]))
- {
- return false;
- }
- }
- return true;
- }
- return false;
- }
- }
- }
|