Browse Source

ComPDFKit.Tool(win)-修改批量删除Undo Redo 问题,和修复删除中间注释后Undo Redo 异常问题

liyuxuan 7 months ago
parent
commit
331de55823

+ 40 - 8
Demo/Examples/ComPDFKit.Tool/UndoManger/AnnotHistory.cs

@@ -4,6 +4,7 @@ using ComPDFKit.PDFDocument;
 using ComPDFKit.PDFPage;
 using ComPDFKitViewer.Helper;
 using System.Collections.Generic;
+using System.Linq;
 
 namespace ComPDFKit.Tool.UndoManger
 {
@@ -87,7 +88,20 @@ namespace ComPDFKit.Tool.UndoManger
                 MultiAnnotHistory multiHistory = (MultiAnnotHistory)checkItem;
                 foreach (AnnotHistory checkAnnot in multiHistory.Histories)
                 {
-                    if (checkAnnot == null)
+                    if (checkAnnot == null || checkAnnotList.Contains(checkAnnot))
+                    {
+                        continue;
+                    }
+                    checkAnnotList.Add(checkAnnot);
+                }
+            }
+
+            if (checkItem is GroupHistory)
+            {
+                GroupHistory groupHistory = (GroupHistory)checkItem;
+                foreach (AnnotHistory checkAnnot in groupHistory.Histories)
+                {
+                    if (checkAnnot == null || checkAnnotList.Contains(checkAnnot))
                     {
                         continue;
                     }
@@ -100,6 +114,22 @@ namespace ComPDFKit.Tool.UndoManger
                 checkAnnotList.Add((AnnotHistory)checkItem);
             }
 
+            //而且 checkAnnotList 要从大到小排序
+            List<AnnotHistory> loopList = new List<AnnotHistory>();
+            if (checkAnnotList != null && checkAnnotList.Count > 0)
+            {
+                List<int> pageList = checkAnnotList.AsEnumerable().Select(x => x.GetPageIndex()).Distinct().ToList();
+                pageList.Sort();
+                foreach (int pageIndex in pageList)
+                {
+                    List<AnnotHistory> groupList = checkAnnotList.AsEnumerable().Where(x => x.GetPageIndex() == pageIndex).OrderByDescending(x => x.GetAnnotIndex()).ToList();
+                    if (groupList != null && groupList.Count > 0)
+                    {
+                        loopList.AddRange(groupList);
+                    }
+                }
+            }
+
             foreach (AnnotHistory checkAnnot in checkAnnotList)
             {
                 if (add && checkAnnot.Action == HistoryAction.Remove)
@@ -139,17 +169,19 @@ namespace ComPDFKit.Tool.UndoManger
             if (GetPageIndex() == pageIndex)
             {
                 int oldIndex = GetAnnotIndex();
-                if (oldIndex > annotIndex || oldIndex<=-1)
-                {
-                    SetAnnotIndex(oldIndex - 1);
-                    HistoryIndex = oldIndex - 1;
-                }
-               
-                if(oldIndex==annotIndex)
+                if (oldIndex == annotIndex && oldIndex >= 0)
                 {
                     SetAnnotIndex(-1);
                     HistoryIndex = -1;
                 }
+                else
+                {
+                    if (oldIndex > annotIndex || oldIndex <= -1)
+                    {
+                        SetAnnotIndex(oldIndex - 1);
+                        HistoryIndex = oldIndex - 1;
+                    }
+                }
             }
         }
 

+ 189 - 1
Demo/Examples/ComPDFKit.Tool/UndoManger/GroupHistory.cs

@@ -15,7 +15,44 @@ namespace ComPDFKit.Tool.UndoManger
             if (Histories != null && Histories.Count > 0)
             {
                 bool success = true;
+                //需要排序
+
+                List<AnnotHistory> checkAnnotList = new List<AnnotHistory>();
+                List<IHistory> OtherList = new List<IHistory>();
+
                 foreach (IHistory history in Histories)
+                {
+                    if (history is AnnotHistory)
+                    {
+                        checkAnnotList.Add(history as AnnotHistory);
+                        continue;
+                    }
+
+                    OtherList.Add(history);
+                }
+
+                //而且 checkAnnotList 要从大到小排序
+                List<AnnotHistory> loopList = new List<AnnotHistory>();
+                if (checkAnnotList != null && checkAnnotList.Count > 0)
+                {
+                    List<int> pageList = checkAnnotList.AsEnumerable().Select(x => x.GetPageIndex()).Distinct().ToList();
+                    pageList.Sort();
+                    foreach (int pageIndex in pageList)
+                    {
+                        List<AnnotHistory> groupList = checkAnnotList.AsEnumerable().Where(x => x.GetPageIndex() == pageIndex).OrderByDescending(x => x.GetAnnotIndex()).ToList();
+                        if (groupList != null && groupList.Count > 0)
+                        {
+                            loopList.AddRange(groupList);
+                        }
+                    }
+                }
+
+                foreach (AnnotHistory history in loopList)
+                {
+                    success &= history.Redo();
+                }
+
+                foreach (IHistory history in OtherList)
                 {
                     success &= history.Redo();
                 }
@@ -29,7 +66,44 @@ namespace ComPDFKit.Tool.UndoManger
             if (Histories != null && Histories.Count > 0)
             {
                 bool success = true;
+                //需要排序
+
+                List<AnnotHistory> checkAnnotList = new List<AnnotHistory>();
+                List<IHistory> OtherList = new List<IHistory>();
+
                 foreach (IHistory history in Histories)
+                {
+                    if (history is AnnotHistory)
+                    {
+                        checkAnnotList.Add(history as AnnotHistory);
+                        continue;
+                    }
+
+                    OtherList.Add(history);
+                }
+
+                //而且 checkAnnotList 要从大到小排序
+                List<AnnotHistory> loopList = new List<AnnotHistory>();
+                if (checkAnnotList != null && checkAnnotList.Count > 0)
+                {
+                    List<int> pageList = checkAnnotList.AsEnumerable().Select(x => x.GetPageIndex()).Distinct().ToList();
+                    pageList.Sort();
+                    foreach (int pageIndex in pageList)
+                    {
+                        List<AnnotHistory> groupList = checkAnnotList.AsEnumerable().Where(x => x.GetPageIndex() == pageIndex).OrderByDescending(x => x.GetAnnotIndex()).ToList();
+                        if (groupList != null && groupList.Count > 0)
+                        {
+                            loopList.AddRange(groupList);
+                        }
+                    }
+                }
+
+                foreach (AnnotHistory history in loopList)
+                {
+                    success &= history.Undo();
+                }
+
+                foreach (IHistory history in OtherList)
                 {
                     success &= history.Undo();
                 }
@@ -40,7 +114,121 @@ namespace ComPDFKit.Tool.UndoManger
 
         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 || checkAnnotList.Contains(checkAnnot))
+                    {
+                        continue;
+                    }
+                    checkAnnotList.Add(checkAnnot);
+                }
+            }
+
+            if (checkItem is GroupHistory)
+            {
+                GroupHistory groupHistory = (GroupHistory)checkItem;
+                foreach (AnnotHistory checkAnnot in groupHistory.Histories)
+                {
+                    if (checkAnnot == null || checkAnnotList.Contains(checkAnnot))
+                    {
+                        continue;
+                    }
+                    checkAnnotList.Add(checkAnnot);
+                }
+            }
+
+            if (checkItem is AnnotHistory)
+            {
+                checkAnnotList.Add((AnnotHistory)checkItem);
+            }
+            //而且 checkAnnotList 要从大到小排序
+            List<AnnotHistory> loopList = new List<AnnotHistory>();
+            if (checkAnnotList != null && checkAnnotList.Count > 0)
+            {
+                List<int> pageList = checkAnnotList.AsEnumerable().Select(x => x.GetPageIndex()).Distinct().ToList();
+                pageList.Sort();
+                foreach (int pageIndex in pageList)
+                {
+                    List<AnnotHistory> groupList = checkAnnotList.AsEnumerable().Where(x => x.GetPageIndex() == pageIndex).OrderByDescending(x => x.GetAnnotIndex()).ToList();
+                    if (groupList != null && groupList.Count > 0)
+                    {
+                        loopList.AddRange(groupList);
+                    }
+                }
+            }
+
+            foreach (AnnotHistory checkAnnot in loopList)
+            {
+                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)
+        {
+            foreach (AnnotHistory annotHistory in Histories)
+            {
+                if (annotHistory.GetPageIndex() == pageIndex)
+                {
+                    int oldIndex = annotHistory.GetAnnotIndex();
+                    if (oldIndex == annotIndex && oldIndex >= 0)
+                    {
+                        annotHistory.SetAnnotIndex(-1);
+                        annotHistory.HistoryIndex = -1;
+                    }
+                    else
+                    {
+                        if (oldIndex > annotIndex || oldIndex <= -1)
+                        {
+                            annotHistory.SetAnnotIndex(oldIndex - 1);
+                            annotHistory.HistoryIndex = oldIndex - 1;
+                        }
+                    }
+                }
+            }
+        }
+
+        internal void UpdateCurrentIndex(int pageIndex, int prevIndex, int annotIndex)
+        {
+            foreach (AnnotHistory annotHistory in Histories)
+            {
+                if (annotHistory.GetPageIndex() == pageIndex && annotHistory.HistoryIndex == prevIndex)
+                {
+                    annotHistory.SetAnnotIndex(annotIndex);
+                    annotHistory.HistoryIndex = annotIndex;
+                }
+            }
         }
     }
 }

+ 41 - 7
Demo/Examples/ComPDFKit.Tool/UndoManger/MultiAnnotHistory.cs

@@ -1,5 +1,6 @@
 using ComPDFKitViewer.Helper;
 using System.Collections.Generic;
+using System.Linq;
 
 namespace ComPDFKit.Tool.UndoManger
 {
@@ -42,7 +43,20 @@ namespace ComPDFKit.Tool.UndoManger
                 MultiAnnotHistory multiHistory = (MultiAnnotHistory)checkItem;
                 foreach (AnnotHistory checkAnnot in multiHistory.Histories)
                 {
-                    if (checkAnnot == null)
+                    if (checkAnnot == null || checkAnnotList.Contains(checkAnnot))
+                    {
+                        continue;
+                    }
+                    checkAnnotList.Add(checkAnnot);
+                }
+            }
+
+            if (checkItem is GroupHistory)
+            {
+                GroupHistory groupHistory = (GroupHistory)checkItem;
+                foreach (AnnotHistory checkAnnot in groupHistory.Histories)
+                {
+                    if (checkAnnot == null || checkAnnotList.Contains(checkAnnot))
                     {
                         continue;
                     }
@@ -54,8 +68,23 @@ namespace ComPDFKit.Tool.UndoManger
             {
                 checkAnnotList.Add((AnnotHistory)checkItem);
             }
+            //而且 checkAnnotList 要从大到小排序
+            List<AnnotHistory> loopList = new List<AnnotHistory>();
+            if (checkAnnotList != null && checkAnnotList.Count > 0)
+            {
+                List<int> pageList = checkAnnotList.AsEnumerable().Select(x => x.GetPageIndex()).Distinct().ToList();
+                pageList.Sort();
+                foreach (int pageIndex in pageList)
+                {
+                    List<AnnotHistory> groupList = checkAnnotList.AsEnumerable().Where(x => x.GetPageIndex() == pageIndex).OrderByDescending(x => x.GetAnnotIndex()).ToList();
+                    if (groupList != null && groupList.Count > 0)
+                    {
+                        loopList.AddRange(groupList);
+                    }
+                }
+            }
 
-            foreach (AnnotHistory checkAnnot in checkAnnotList)
+            foreach (AnnotHistory checkAnnot in loopList)
             {
                 if (add && checkAnnot.Action == HistoryAction.Remove)
                 {
@@ -96,14 +125,18 @@ namespace ComPDFKit.Tool.UndoManger
                 if (annotHistory.GetPageIndex() == pageIndex)
                 {
                     int oldIndex = annotHistory.GetAnnotIndex();
-                    if (oldIndex > annotIndex || oldIndex<=-1)
+                    if (oldIndex == annotIndex && oldIndex >= 0)
                     {
-                        annotHistory.SetAnnotIndex(oldIndex-1);
+                        annotHistory.SetAnnotIndex(-1);
+                        annotHistory.HistoryIndex = -1;
                     }
-
-                    if (oldIndex == annotIndex)
+                    else
                     {
-                        annotHistory.SetAnnotIndex(-1);
+                        if (oldIndex > annotIndex || oldIndex <= -1)
+                        {
+                            annotHistory.SetAnnotIndex(oldIndex - 1);
+                            annotHistory.HistoryIndex = oldIndex - 1;
+                        }
                     }
                 }
             }
@@ -116,6 +149,7 @@ namespace ComPDFKit.Tool.UndoManger
                 if (annotHistory.GetPageIndex() == pageIndex && annotHistory.HistoryIndex==prevIndex)
                 {
                     annotHistory.SetAnnotIndex(annotIndex);
+                    annotHistory.HistoryIndex = annotIndex;
                 }
             }
         }