Browse Source

【编辑工具】合并支持拖拽

lizhe 1 year ago
parent
commit
070168b4bf

+ 57 - 45
PDF Office/PDF Master/Class/PDFTools/Merge/MergeNew/View/KMMergeView.swift

@@ -208,10 +208,7 @@ extension KMMergeView: NSTableViewDelegate {
 
         if isCanDrag {
             result = .copy
-        } else {
-            result = []
         }
-
         return result
     }
     
@@ -253,49 +250,64 @@ extension KMMergeView: NSTableViewDelegate {
             addFilePaths(urls: array)
             result = true
        } else if pboard.availableType(from: [MyTableCellViewDataType]) != nil {
-           if let rowData = pboard.data(forType: MyTableCellViewDataType),
-              let rowIndexes = NSKeyedUnarchiver.unarchiveObject(with: rowData) as? IndexSet {
-
-               var moveArray = [KMFileAttribute]()
-               var allPhoto: [KMFileAttribute] = []
-               var index = 0
-               
-               for file in files {
-                   allPhoto.append(file)
-               }
-
-               rowIndexes.enumerated().forEach { (idx, shouldStop) in
-                   moveArray.append(allPhoto[idx])
-                   files.remove(at: idx)
-               }
-
-               var newRow = row
-               if newRow > 0 {
-                   var indexFile: KMFileAttribute?
-
-                   repeat {
-                       newRow = newRow - 1
-
-                       guard newRow >= 0 else {
-                           indexFile = nil
-                           break
-                       }
-
-                       indexFile = allPhoto[newRow]
-                   } while moveArray.contains(indexFile!)
-
-                   if let indexFile = indexFile {
-                       index = files.firstIndex(of: indexFile)! + 1
-                   }
-               }
-
-               moveArray.enumerated().forEach { (i, file) in
-                   files.insert(file, at: (index + i))
-               }
-
-               tableView.reloadData()
-               result = true
+           guard let data = info.draggingPasteboard.data(forType: MyTableCellViewDataType),
+                 let rowIndexes = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? IndexSet else {
+               return false
+           }
+           
+           // 移动数据
+           var draggedItems: [KMFileAttribute] = []
+           for index in rowIndexes {
+               draggedItems.append(files[index])
+           }
+           
+           for index in rowIndexes.reversed() {
+               files.remove(at: index)
            }
+           
+           let insertionIndex = row > rowIndexes.first! ? row - rowIndexes.count : row
+           
+           for (index, item) in draggedItems.enumerated() {
+               files.insert(item, at: insertionIndex + index)
+           }
+           
+           tableView.reloadData()
+           
+//           let rowData = pboard.data(forType: MyTableCellViewDataType)
+//           let rowIndexes = NSKeyedUnarchiver.unarchiveObject(with: rowData!) as? IndexSet
+//           let backUpArray = self.files
+//           var referenceIndex = -1
+//           rowIndexes?.enumerated().forEach { (idx, shouldStop) in
+//               let sortFile = backUpArray[idx]
+//               if referenceIndex < 0 {
+//                   if idx > row {
+//                       self.files.remove(at: idx)
+//                       self.files.insert(sortFile, at: row)
+//                   } else if idx < row {
+//                       if row > self.files.count {
+//                           self.files.remove(at: idx)
+//                           self.files.append(sortFile)
+//                       } else if row < self.files.count {
+//                           self.files.remove(at: idx)
+//                           self.files.insert(sortFile, at: row - 1)
+//                       }
+//                   }
+//                   referenceIndex = self.files.firstIndex(of: sortFile) ?? -1
+//               } else {
+//                   let currentIndex = self.files.firstIndex(of: sortFile) ?? -1
+//                   if currentIndex > referenceIndex {
+//                       self.files.remove(at: currentIndex)
+//                       self.files.insert(sortFile, at: referenceIndex)
+//                   } else {
+//                       referenceIndex -= 1
+//                       self.files.remove(at: currentIndex)
+//                       self.files.insert(sortFile, at: referenceIndex)
+//                   }
+//               }
+//           }
+//           self.tableview.reloadData()
+           return true
+           
        } else {
            result = false
        }

+ 30 - 32
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController.swift

@@ -1392,43 +1392,41 @@ let LOCKED_KEY  = "locked"
     
     public func showMergeWindow(url: URL? = nil, _ password: String?) {
         DispatchQueue.main.async {
-            if let _url = url {
-                let document = PDFDocument(url: _url)
-                let windowController = KMMergeWindowController(document: document!, password: password ?? "")
-                windowController.oriDucumentUrl = self.listView.document.documentURL
-                windowController.pageIndex = self.listView.currentPageIndex
-                self.currentWindowController = windowController
+            var documentURL = url
+            if documentURL == nil {
+                documentURL = self.listView.document.documentURL
+            }
+            
+            guard let _url = documentURL else { return }
+            
+            let document = PDFDocument(url: _url)
+            let windowController = KMMergeWindowController(document: document!, password: password ?? "")
+            windowController.oriDucumentUrl = self.listView.document.documentURL
+            windowController.pageIndex = self.listView.currentPageIndex
+            self.currentWindowController = windowController
+            
+            windowController.cancelAction = { [unowned self] controller in
+                self.view.window?.endSheet((self.currentWindowController.window)!)
+                self.currentWindowController = nil
+            }
+            
+            windowController.mergeAction = { [unowned self] controller, filePath in
+                self.view.window?.endSheet((self.currentWindowController.window)!)
+                self.currentWindowController = nil
                 
-                windowController.cancelAction = { [unowned self] controller in
-                    self.view.window?.endSheet((self.currentWindowController.window)!)
-                    self.currentWindowController = nil
+                let newDocument = CPDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
+                if let data = newDocument?.isLocked, data {
+                    newDocument?.unlock(withPassword: self.listView.document.password ?? "")
                 }
                 
-                windowController.mergeAction = { [unowned self] controller, filePath in
-                    self.view.window?.endSheet((self.currentWindowController.window)!)
-                    self.currentWindowController = nil
-                    
-                    let newDocument = CPDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
-                    if let data = newDocument?.isLocked, data {
-                        newDocument?.unlock(withPassword: self.listView.document.password ?? "")
-                    }
-                    
-                    self.setDocument = newDocument
-                    if self.leftSideViewController.type.methodType == .Thumbnail {
-                        self.leftSideViewController.resetThumbnails()
-                    }
+                self.setDocument = newDocument
+                if self.leftSideViewController.type.methodType == .Thumbnail {
+                    self.leftSideViewController.resetThumbnails()
                 }
-                self.toolbarController.cancelSelected(KMToolbarToolMergeItemIdentifier)
-                
-                self.view.window?.beginSheet(windowController.window!)
-            } else {
-                let document = PDFDocument(url: self.listView.document.documentURL)
-                let windowController = KMMergeWindowController(document: document!, password: password ?? "")
-                
-                self.toolbarController.cancelSelected(KMToolbarToolMergeItemIdentifier)
-                
-                self.view.window!.km_beginSheet(windowC: windowController)
             }
+            self.toolbarController.cancelSelected(KMToolbarToolMergeItemIdentifier)
+            
+            self.view.window?.beginSheet(windowController.window!)
         }
     }