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 checkAnnotList = new List(); 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 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[] 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; } } }