Преглед изворни кода

【BOTA】大纲列表补充拖拽

tangchao пре 1 година
родитељ
комит
30a5215a4f

+ 17 - 0
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/KMLeftSideViewController+Outline.swift

@@ -318,6 +318,23 @@ extension KMLeftSideViewController {
             self.tocOutlineView.reloadData()
         }
     }
+    
+    @objc dynamic func dragPDFOutline(_ outline: CPDFOutline?, toIndex index: Int, newParentOutline: CPDFOutline?) {
+        guard let doc = self.view.window?.windowController?.document as? NSDocument else {
+            NSSound.beep()
+            return
+        }
+        if outline == nil {
+            NSSound.beep()
+            return
+        }
+        (doc.undoManager?.prepare(withInvocationTarget: self) as AnyObject).dragPDFOutline(outline, toIndex: Int(outline!.index), newParentOutline: outline?.parent)
+        outline?.removeFromParent()
+        newParentOutline?.insertChild(outline, at: UInt(index))
+        self.tocOutlineView.reloadData()
+        self.tocOutlineView.expandItem(newParentOutline)
+        self.tocOutlineView.selectRowIndexes(IndexSet(integer: self.tocOutlineView.row(forItem: outline)), byExtendingSelection: false)
+    }
 }
 
 // MARK: - Menu Actions

+ 111 - 103
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/KMLeftSideViewController.swift

@@ -240,7 +240,7 @@ class KMLeftSideViewController: KMSideViewController {
         }
     }
     
-    private let kKMPDFViewOutlineDragDataType  = "kKMPDFViewOutlineDragDataType"
+    private let kKMPDFViewOutlineDragDataType  = NSPasteboard.PasteboardType("kKMPDFViewOutlineDragDataType")
     private let KPDFThumbnailDoucumentURLForDraggedTypes  = NSPasteboard.PasteboardType("KPDFThumbnailDoucumentURLForDraggedTypes")
     
     var renamePDFOutline: CPDFOutline?
@@ -256,6 +256,8 @@ class KMLeftSideViewController: KMSideViewController {
     
     var mwcFlags: MwcFlags = MwcFlags()
     
+    private var _dragPDFOutline: CPDFOutline?
+    
     override func loadView() {
         super.loadView()
         
@@ -659,7 +661,7 @@ class KMLeftSideViewController: KMSideViewController {
 //        self.thumbnailTableView.registerForDraggedTypes(NSFilePromiseReceiver.readableDraggedTypes.map { NSPasteboard.PasteboardType($0) })
 //        self.thumbnailTableView.setDraggingSourceOperationMask([.copy, .delete], forLocal: true)
 //        [tocOutlineView setTypeSelectHelper:[SKTypeSelectHelper typeSelectHelperWithMatchOption:SKSubstringMatch]];
-//        [tocOutlineView registerForDraggedTypes:[NSArray arrayWithObject:kKMPDFViewOutlineDragDataType]];
+        self.tocOutlineView.registerForDraggedTypes([kKMPDFViewOutlineDragDataType, .localDraggedTypes])
 //        [[[findTableView tableColumnWithIdentifier:PAGE_COLUMNID] headerCell] setTitle:NSLocalizedString(@"Page", @"Table header title")];
 //        [[[groupedFindTableView tableColumnWithIdentifier:PAGE_COLUMNID] headerCell] setTitle:NSLocalizedString(@"Page", @"Table header title")];
 //        [[[groupedFindTableView tableColumnWithIdentifier:RELEVANCE_COLUMNID] dataCell] setEnabled:NO];
@@ -4063,6 +4065,113 @@ extension KMLeftSideViewController: NSOutlineViewDelegate, NSOutlineViewDataSour
         }
     }
     
+    func outlineView(_ outlineView: NSOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
+        var dragOp = NSDragOperation(rawValue: 0)
+//        if outlineView.isEqual(to: self.noteOutlineView)
+//            NSPasteboard *pboard = [info draggingPasteboard];
+//            if ([pboard canReadObjectForClasses:[NSArray arrayWithObject:[NSColor class]] options:[NSDictionary dictionary]] &&
+//                anIndex == NSOutlineViewDropOnItemIndex && [(PDFAnnotation *)item type] != nil)
+//                dragOp = NSDragOperationEvery;
+//        } else
+        if outlineView.isEqual(to: self.tocOutlineView) {
+            if (index == -1) {
+                dragOp = NSDragOperation(rawValue: 0)
+            } else {
+                dragOp =  .move
+            }
+        }
+        return dragOp
+    }
+    
+    func outlineView(_ outlineView: NSOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool {
+        if outlineView.isEqual(to: self.tocOutlineView) {
+            if (self.tocOutlineView.selectedRowIndexes.count > 1) {
+                return false
+            }
+            let tIndex = IndexSet(integer: self.tocOutlineView.clickedRow)
+            self.tocOutlineView.deselectRow(tIndex.first ?? 0)
+            self._dragPDFOutline = items.first as? CPDFOutline
+
+            let set = IndexSet(integer: 0)
+            let zNSIndexSetData = NSKeyedArchiver.archivedData(withRootObject: set)
+//            [pasteboard declareTypes:[NSArray arrayWithObject:kKMPDFViewOutlineDragDataType] owner:self];
+            pasteboard.declareTypes([.localDraggedTypes], owner: self)
+            pasteboard.setData(zNSIndexSetData, forType: .localDraggedTypes)
+            return true
+        }
+        return false
+    }
+    
+    func outlineView(_ outlineView: NSOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
+        if outlineView.isEqual(to: self.noteOutlineView) {
+//            NSPasteboard *pboard = [info draggingPasteboard];
+//            if ([pboard canReadObjectForClasses:[NSArray arrayWithObject:[NSColor class]] options:[NSDictionary dictionary]]) {
+//                BOOL isShift = ([NSEvent standardModifierFlags] & NSEventModifierFlagShift) != 0;
+//                BOOL isAlt = ([NSEvent standardModifierFlags] & NSEventModifierFlagOption) != 0;
+//                [item setColor:[NSColor colorFromPasteboard:pboard] alternate:isAlt updateDefaults:isShift];
+//                return YES;
+//            }
+        } else if outlineView.isEqual(to: self.tocOutlineView) {
+            if (index < 0) {
+                return false
+            }
+//            guard let outline = item as? CPDFOutline else {
+//                return false
+//            }
+            let outline = item as? CPDFOutline
+            
+            guard let dragPDFOL = self._dragPDFOutline else {
+                return false
+            }
+
+            //root,drag item to root
+            if ((outline?.parent == nil)) {
+                //fetch root
+                var root = dragPDFOL
+                while (root.parent != nil) {
+                    root = root.parent
+                }
+                if dragPDFOL.parent.isEqual(to: root) {
+                    if dragPDFOL.index > index {
+                        self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: root)
+                    } else {
+                        self.dragPDFOutline(self._dragPDFOutline, toIndex: index-1, newParentOutline: root)
+                    }
+                } else {
+                    self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: root)
+                }
+            } else {
+                //在同一个层级内移动
+                if dragPDFOL.parent.isEqual(to: item) {
+                    if (dragPDFOL.index > 0) {
+                        if dragPDFOL.index > index {
+                            self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: outline)
+                        }else{
+                            self.dragPDFOutline(self._dragPDFOutline, toIndex: index-1, newParentOutline: outline)
+                        }
+                    } else {
+                        return false
+                    }
+                } else {
+                    var tOutlline: CPDFOutline? = outline
+                    var isContains = false
+                    while (tOutlline != nil) {
+                        if tOutlline!.isEqual(to: self._dragPDFOutline) {
+                            isContains = true
+                            break
+                        }
+                        tOutlline = tOutlline?.parent
+                    }
+                    if (!isContains) {
+                        self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: outline)
+                    }
+                }
+            }
+            return true
+        }
+        return false
+    }
+    
     /*
      #pragma mark NSOutlineView datasource protocol
 
@@ -4081,106 +4190,6 @@ extension KMLeftSideViewController: NSOutlineViewDelegate, NSOutlineViewDataSour
          }
      }
 
-     #pragma mark Drag
-     - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard{
-         
-         if ([outlineView isEqual:leftSideController.tocOutlineView]) {
-             if (leftSideController.tocOutlineView.selectedRowIndexes.count > 1) {
-                 return NO;
-             }
-             NSIndexSet *tIndex = [[[NSIndexSet alloc] initWithIndex:leftSideController.tocOutlineView.clickedRow] autorelease];
-             [leftSideController.tocOutlineView deselectRow:tIndex.firstIndex];
-             self.dragPDFOutline = items.firstObject;
-
-             NSIndexSet *set = [NSIndexSet indexSetWithIndex:0];
-             NSData *zNSIndexSetData = [NSKeyedArchiver archivedDataWithRootObject:set];
-             [pasteboard declareTypes:[NSArray arrayWithObject:kKMPDFViewOutlineDragDataType] owner:self];
-             [pasteboard setData:zNSIndexSetData forType:kKMPDFViewOutlineDragDataType];
-             return YES;
-         }
-         return NO;
-     }
-
-     - (NSDragOperation)outlineView:(NSOutlineView *)ov validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)anIndex {
-         NSDragOperation dragOp = NSDragOperationNone;
-         if ([ov isEqual:rightSideController.noteOutlineView]) {
-             NSPasteboard *pboard = [info draggingPasteboard];
-             if ([pboard canReadObjectForClasses:[NSArray arrayWithObject:[NSColor class]] options:[NSDictionary dictionary]] &&
-                 anIndex == NSOutlineViewDropOnItemIndex && [(PDFAnnotation *)item type] != nil)
-                 dragOp = NSDragOperationEvery;
-         } else if ([ov isEqual:leftSideController.tocOutlineView]) {
-             if (anIndex == -1) {
-                 dragOp = NSDragOperationNone;
-             } else {
-                 dragOp =  NSDragOperationMove;
-             }
-         }
-         return dragOp;
-     }
-
-     - (BOOL)outlineView:(NSOutlineView *)ov acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)anIndex {
-         if ([ov isEqual:rightSideController.noteOutlineView]) {
-             NSPasteboard *pboard = [info draggingPasteboard];
-             if ([pboard canReadObjectForClasses:[NSArray arrayWithObject:[NSColor class]] options:[NSDictionary dictionary]]) {
-                 BOOL isShift = ([NSEvent standardModifierFlags] & NSEventModifierFlagShift) != 0;
-                 BOOL isAlt = ([NSEvent standardModifierFlags] & NSEventModifierFlagOption) != 0;
-                 [item setColor:[NSColor colorFromPasteboard:pboard] alternate:isAlt updateDefaults:isShift];
-                 return YES;
-             }
-         } else if ([ov isEqual:leftSideController.tocOutlineView]) {
-             if (anIndex < 0) {
-                 return NO;
-             }
-             PDFOutline *outline = item;
-             
-             //root,drag item to root
-             if (!outline.parent) {
-                 //fetch root
-                 PDFOutline *root  = self.dragPDFOutline;
-                 while (root.parent) {
-                     root = root.parent;
-                 }
-                 if ([self.dragPDFOutline.parent isEqual:root]) {
-                     if (self.dragPDFOutline.index > (NSUInteger)anIndex) {
-                         [self dragPDFOutline:self.dragPDFOutline toIndex:anIndex newParentOutline:root];
-                     } else {
-                         [self dragPDFOutline:self.dragPDFOutline toIndex:anIndex - 1 newParentOutline:root];
-                     }
-                 } else {
-                     [self dragPDFOutline:self.dragPDFOutline toIndex:anIndex newParentOutline:root];
-                 }
-             } else {
-                 //在同一个层级内移动
-                 if ([self.dragPDFOutline.parent isEqual:item]) {
-                     if (self.dragPDFOutline.index) {
-                         if (self.dragPDFOutline.index > (NSUInteger)anIndex) {
-                             [self dragPDFOutline:self.dragPDFOutline toIndex:anIndex newParentOutline:outline];
-                         }else{
-                             [self dragPDFOutline:self.dragPDFOutline toIndex:anIndex - 1 newParentOutline:outline];
-                         }
-                     } else {
-                         return NO;
-                     }
-                 } else {
-                     PDFOutline *tOutlline = outline;
-                     BOOL isContains = NO;
-                     while (tOutlline) {
-                         if ([tOutlline isEqual:self.dragPDFOutline]) {
-                             isContains = YES;
-                             break;
-                         }
-                         tOutlline = tOutlline.parent;
-                     }
-                     if (!isContains) {
-                         [self dragPDFOutline:self.dragPDFOutline toIndex:anIndex newParentOutline:outline];
-                     }
-                 }
-             }
-             return YES;
-         }
-         return NO;
-     }
-
      #pragma mark NSOutlineView delegate protocol
 
      - (NSCell *)outlineView:(NSOutlineView *)ov dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
@@ -4852,7 +4861,6 @@ extension KMLeftSideViewController {
                 pdf?.insertPageObject(_page, at: pdf?.pageCount ?? 0)
             }
         }
-//        NSString * = nil;
         var fileName = ""
 //        NSString * = [self.pdfDocument.documentURL.lastPathComponent stringByDeletingPathExtension];
         var documentFileName = document.documentURL?.deletingPathExtension().lastPathComponent ?? ""