Просмотр исходного кода

【BOTA】undo/redo 缩略图删除裁剪粘贴

lizhe 1 год назад
Родитель
Сommit
a02d4e6c6a

+ 2 - 2
PDF Office/PDF Office/Class/PDFWindowController/Side/LeftSide/BookMarks/KMBookMarkViewController.swift

@@ -8,7 +8,7 @@
 import Cocoa
 import PDFKit
 
-class KMBookMarkViewController: NSViewController, NSTextFieldDelegate,NSMenuItemValidation {
+class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
 
     @IBOutlet weak var addBookButton: NSButton!
     @IBOutlet weak var titleTextField: NSTextField!
@@ -398,7 +398,7 @@ extension KMBookMarkViewController {
     }
 }
 
-extension KMBookMarkViewController : NSMenuDelegate {
+extension KMBookMarkViewController: NSMenuDelegate, NSMenuItemValidation {
     func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
         let action = menuItem.action
         if (action == #selector(undo)) {

+ 72 - 63
PDF Office/PDF Office/Class/PDFWindowController/Side/LeftSide/Outline/KMOutlineViewController.swift

@@ -7,7 +7,7 @@
 
 import Cocoa
 
-class KMOutlineViewController: NSViewController,NSMenuItemValidation {
+class KMOutlineViewController: NSViewController {
   
     @IBOutlet var contendView: NSView!
     @IBOutlet weak var outlineView: NSOutlineView!
@@ -509,68 +509,6 @@ class KMOutlineViewController: NSViewController,NSMenuItemValidation {
         }
     }
     
-    //MARK: NSMenuItemValidation
-    
-    func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
-        let action = menuItem.action
-        if action == #selector(addItemAction) ||
-            action == #selector(addChildItemAction) ||
-            action == #selector(addHigherItemAction) ||
-            action == #selector(deleteItemAction) ||
-            action == #selector(editItemAction) ||
-            action == #selector(changeItemAction) ||
-            action == #selector(renameItemAction) ||
-            action == #selector(promoteItemAction) ||
-            action == #selector(demoteItemAction) {
-            if self.outlineView.selectedRowIndexes.count > 1 {
-                if action == #selector(deleteItemAction) {
-                    return true
-                }
-                return false
-            } else if self.outlineView.selectedRowIndexes.count > 0 {
-                if action == #selector(addChildItemAction) || action == #selector(changeItemAction) {
-                    return true
-                }
-            }
-            if self.outlineView.clickedRow == -1 {
-                if action == #selector(addItemAction) {
-                    return true
-                } else {
-                    return false
-                }
-            } else {
-                let outline : CPDFOutline = self.outlineView.item(atRow: self.outlineView.clickedRow) as! CPDFOutline
-                if outline.index > 0 {
-                    if action == #selector(demoteItemAction) {
-                        return true
-                    }
-                } else {
-                    if action == #selector(demoteItemAction) {
-                        return false
-                    }
-                }
-                
-                let parentOutline = outline.parent
-                let grandparentOutline = parentOutline?.parent
-                if grandparentOutline != nil {
-                    if action == #selector(addHigherItemAction) {
-                        return true
-                    } else if action == #selector(promoteItemAction) {
-                        return true
-                    }
-                } else {
-                    if action == #selector(addHigherItemAction) {
-                        return false
-                    } else if action == #selector(promoteItemAction) {
-                        return false
-                    }
-                }
-            }
-            return true
-        }
-        return true
-    }
-    
     
     //MARK: Noti
     @objc func KMPDFViewCurrentPageDidChangedNotification(notification: NSNotification) {
@@ -767,6 +705,77 @@ extension KMOutlineViewController : NSPopoverDelegate {
     }
 }
 
+extension KMOutlineViewController: NSMenuDelegate, NSMenuItemValidation {
+    //MARK: NSMenuItemValidation
+    
+    func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
+        let action = menuItem.action
+        if action == #selector(addItemAction) ||
+            action == #selector(addChildItemAction) ||
+            action == #selector(addHigherItemAction) ||
+            action == #selector(deleteItemAction) ||
+            action == #selector(editItemAction) ||
+            action == #selector(changeItemAction) ||
+            action == #selector(renameItemAction) ||
+            action == #selector(promoteItemAction) ||
+            action == #selector(demoteItemAction) {
+            if self.outlineView.selectedRowIndexes.count > 1 {
+                if action == #selector(deleteItemAction) {
+                    return true
+                }
+                return false
+            } else if self.outlineView.selectedRowIndexes.count > 0 {
+                if action == #selector(addChildItemAction) || action == #selector(changeItemAction) {
+                    return true
+                }
+            }
+            if self.outlineView.clickedRow == -1 {
+                if action == #selector(addItemAction) {
+                    return true
+                } else {
+                    return false
+                }
+            } else {
+                let outline : CPDFOutline = self.outlineView.item(atRow: self.outlineView.clickedRow) as! CPDFOutline
+                if outline.index > 0 {
+                    if action == #selector(demoteItemAction) {
+                        return true
+                    }
+                } else {
+                    if action == #selector(demoteItemAction) {
+                        return false
+                    }
+                }
+                
+                let parentOutline = outline.parent
+                let grandparentOutline = parentOutline?.parent
+                if grandparentOutline != nil {
+                    if action == #selector(addHigherItemAction) {
+                        return true
+                    } else if action == #selector(promoteItemAction) {
+                        return true
+                    }
+                } else {
+                    if action == #selector(addHigherItemAction) {
+                        return false
+                    } else if action == #selector(promoteItemAction) {
+                        return false
+                    }
+                }
+            }
+            return true
+        }
+        
+        if (action == #selector(undo)) {
+            return self.undoRedoManager.canUndo
+        }
+        if (action == #selector(redo)) {
+            return self.undoRedoManager.canRedo
+        }
+        return true
+    }
+}
+
 //MARK: undoRedo
 extension KMOutlineViewController {
     func movePDFOutline(outline : CPDFOutline! , toIndex index : NSInteger , atParent parent : CPDFOutline!) {

+ 67 - 44
PDF Office/PDF Office/Class/PDFWindowController/Side/LeftSide/Thumbnail/KMThumbnailViewController.swift

@@ -145,35 +145,6 @@ class KMThumbnailViewController: NSViewController {
             return
         }
         
-//        // 插入位置偏移量
-//        var toIndexOffset: Int = 0
-//        // 需要移动的页面index数组
-//        var newPages: [CPDFPage] = []
-//        for index in indexs {
-//            let newPage: CPDFPage = self.listView?.document.page(at: UInt(index)).copy() as! CPDFPage
-//            newPages.append(newPage)
-//            self.listView?.document.removePage(at: UInt(index))
-//            if (index < toIndex) { /// 删除了插入位置前面的页面,需要改变 toIndex
-//                toIndexOffset += 1
-//            }
-//        }
-//
-//        let insertIndex = max(0, toIndex-toIndexOffset)
-//        for (index, page) in newPages.enumerated() {
-//            self.listView?.document.insertPageObject(page, at: UInt(insertIndex+index))
-//        }
-        
-        // 新增 undo
-//        self.undoRedoManager.registerUndo(withTarget: self) { target in
-//            target.dragPages_reversed(indexs, toIndex, newPages)
-//        }
-        
-        // 刷新UI
-//        self.listView?.layoutDocumentView()
-//        self.thumbnailView.refreshUI()
-        
-//        self.thumbnailView.reloadData()
-        
         //获取原有page的下标 和 开始添加的下标
         var indexTemp: Int = toIndex
         var pageItems: [KMThumbnailPageItem] = []
@@ -517,10 +488,7 @@ class KMThumbnailViewController: NSViewController {
     @IBAction func deletePageItemAction(menu:NSMenuItem) {
         let indexs = self.thumbnailView.collectionView.selectionIndexes
         if indexs.count > 0 {
-            self.listView.document.removePage(at: indexs)
-            self.listView.layoutDocumentView()
-            self.thumbnailView.reloadData()
-            self.pdfCurrentPageChange()
+            self.deletePageItemWithIndex(indexs: indexs)
         }
     }
     
@@ -568,23 +536,21 @@ class KMThumbnailViewController: NSViewController {
                 let page = self.thumbnailView.document.page(at: UInt(i))
                 self.copysPages.append(page ?? CPDFPage())
             }
-            self.listView.document.removePage(at: indexs)
-            self.listView.layoutDocumentView()
-            self.thumbnailView.reloadData()
+            
+            self.deletePageItemWithIndex(indexs: indexs)
         }
     }
     
     @IBAction func pastePageItemAction(menu:NSMenuItem) {
         var index = (self.thumbnailView.collectionView.selectionIndexes).last ?? -1
         if Int(index) >= 0 && self.copysPages.count > 0 {
+            var pageItems: [KMThumbnailPageItem] = []
             for page in self.copysPages {
-               let result = self.thumbnailView.document.insertPageObject(page, at: UInt(index))
-                if result {
-                    index = index + 1
-                }
+                let pageItem = KMThumbnailPageItem(page: page, index: index, oldIndex: index)
+                index = index + 1
+                pageItems.append(pageItem)
             }
-            self.listView.layoutDocumentView()
-            self.thumbnailView.reloadData()
+            self.addPageItems(pageItems: pageItems)
         }
     }
     
@@ -668,6 +634,13 @@ extension KMThumbnailViewController : NSMenuDelegate,NSMenuItemValidation {
             }
             return false
         }
+        
+        if (action == #selector(undo)) {
+            return self.undoRedoManager.canUndo
+        }
+        if (action == #selector(redo)) {
+            return self.undoRedoManager.canRedo
+        }
         return true
     }
     
@@ -759,8 +732,56 @@ extension KMThumbnailViewController : NSMenuDelegate,NSMenuItemValidation {
 
 //MARK: undoRedo
 extension KMThumbnailViewController {
+    func rotatePageItemWithIndex(indexs: IndexSet) {
+        var pageItems: [KMThumbnailPageItem] = []
+        for index in indexs {
+            let page: CPDFPage = self.listView.document.page(at: UInt(index))
+            let pageItem = KMThumbnailPageItem(page: page, index: index, oldIndex: index)
+            pageItems.append(pageItem)
+        }
+        self.deletePageItems(pageItems: pageItems)
+    }
     
+    func deletePageItemWithIndex(indexs: IndexSet) {
+        var pageItems: [KMThumbnailPageItem] = []
+        for index in indexs {
+            let page: CPDFPage = self.listView.document.page(at: UInt(index))
+            let pageItem = KMThumbnailPageItem(page: page, index: index, oldIndex: index)
+            pageItems.append(pageItem)
+        }
+        self.deletePageItems(pageItems: pageItems)
+    }
     
+    func deletePageItems(pageItems: [KMThumbnailPageItem]) {
+        //移除现有
+        var tempPageItems = pageItems
+        tempPageItems.sort(){$0.oldIndex > $1.oldIndex}
+        for pageItem in tempPageItems {
+            self.listView?.document.removePage(at: UInt(pageItem.oldIndex))
+        }
+        self.listView.layoutDocumentView()
+        self.thumbnailView.reloadData()
+        self.pdfCurrentPageChange()
+        
+        self.undoRedoManager.registerUndo(withTarget: self) { targetType in
+            self.addPageItems(pageItems: tempPageItems)
+        }
+    }
+    
+    func addPageItems(pageItems: [KMThumbnailPageItem]) {
+        var tempPageItems = pageItems
+        tempPageItems.sort(){$0.oldIndex < $1.oldIndex}
+        for pageItem in tempPageItems {
+            self.listView?.document.insertPageObject(pageItem.page, at: UInt(pageItem.oldIndex))
+        }
+        self.listView.layoutDocumentView()
+        self.thumbnailView.reloadData()
+        self.pdfCurrentPageChange()
+        
+        self.undoRedoManager.registerUndo(withTarget: self) { targetType in
+            self.deletePageItems(pageItems: tempPageItems)
+        }
+    }
     
     
     func dragPageItems(pageItems: [KMThumbnailPageItem]) {
@@ -806,7 +827,9 @@ extension KMThumbnailViewController {
     
     struct KMThumbnailPageItem {
         var page: CPDFPage
-        var index: Int
-        var oldIndex: Int
+        var index: Int = 0
+        var oldIndex: Int = 0
+        var rotate: Int = 0
+        var oldRotate: Int = 0
     }
 }