Explorar o código

【综合】BOTA大纲列表数据整理

tangchao hai 11 meses
pai
achega
5417596e73

+ 136 - 0
PDF Office/PDF Master/Class/Common/Category/CPDFKit/CPDFOutline+KMExtension.swift

@@ -52,3 +52,139 @@ import Foundation
         }
     }
 }
+
+@objc extension CPDFOutline {
+    func km_insertChild(label: String?, dest: CPDFDestination?, at index: Int) -> CPDFOutline? {
+        let outline = self.insertChild(at: UInt(index))
+        outline?.label = label
+        outline?.destination = dest
+        return outline
+    }
+}
+
+// CPDFOutline
+
+@objc extension CPDFDocument {
+    // 升级大纲 [插入位置 (当前父节点后)]
+    
+    @objc func promoteNext(outline ol: CPDFOutline) {
+        //
+        let idx = (ol.parent?.index ?? 0) + 1
+        self.promote(outline: ol, at: idx)
+    }
+    
+    // 升级大纲 [指定位置]
+    
+    @objc func promote(outline ol: CPDFOutline, at index: UInt) {
+        if let newParentOL = Self._parentForPromote(outline: ol) {
+            self._move(outline: ol, to: newParentOL, at: index)
+        }
+    }
+    
+    // 降级大纲 [插入位置 (最后)]
+    
+    @objc func demoteLast(outline ol: CPDFOutline) {
+        if let newParentOL = Self._parentForDemote(outline: ol) {
+            self.demote(outline: ol, at: newParentOL.numberOfChildren)
+        }
+    }
+    
+    // 降级大纲 [指定位置]
+    
+    @objc func demote(outline ol: CPDFOutline, at index: UInt) {
+        if let newParentOL = Self._parentForDemote(outline: ol) {
+            self._move(outline: ol, to: newParentOL, at: index)
+        }
+    }
+    
+    // 移动节点
+    
+    private func _move(outline ol: CPDFOutline, to destOL: CPDFOutline, at index: UInt) {
+        if index < 0 || index > destOL.numberOfChildren {
+#if DEBUG
+            print("大纲移动失败;索引越界:\(index) [0 \(destOL.numberOfChildren)]")
+#endif
+            return
+        }
+        
+        ol.removeFromParent()
+        destOL.insertChild(ol, at: index)
+    }
+    
+    // 前一个节点
+    
+    fileprivate class func _previousOL(outline ol: CPDFOutline) -> CPDFOutline? {
+        guard let parentOL = ol.parent else {
+            return nil
+        }
+        let idx = ol.index-1
+        if idx < 0 || idx >= parentOL.numberOfChildren {
+            return nil
+        }
+        return ol.parent.child(at: idx)
+    }
+    
+    // 后一个节点
+    
+    fileprivate class func _nextOL(outline ol: CPDFOutline) -> CPDFOutline? {
+        guard let parentOL = ol.parent else {
+            return nil
+        }
+        let idx = ol.index+1
+        if idx < 0 || idx >= parentOL.numberOfChildren {
+            return nil
+        }
+        return ol.parent.child(at: idx)
+    }
+    
+    // 祖父节点
+    
+    fileprivate class func _parentForPromote(outline ol: CPDFOutline) -> CPDFOutline? {
+        return ol.parent?.parent
+    }
+    
+    // 前一个节点
+    
+    fileprivate class func _parentForDemote(outline ol: CPDFOutline) -> CPDFOutline? {
+        return self._previousOL(outline: ol)
+    }
+}
+
+@objc extension CPDFListView {
+    static let outlineKey =                 "outline"
+    static let outlineUndoKey =             "undo"
+    static let outlineDemoteKey =           "demote"
+    static let outlinePromoteKey =          "promote"
+    
+    @objc func demote(outline ol: CPDFOutline) {
+        (self.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_promote(outline: ol, at: ol.index)
+        
+        self.document?.demoteLast(outline: ol)
+        
+        self.pdfListViewDelegate.pdfListView?(self, documentDataDidChanged: ol, withInfo: [Self.outlineKey : true, Self.outlineDemoteKey : true])
+    }
+    
+    @objc func promote(outline ol: CPDFOutline) {
+        (self.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_demote(outline: ol, at: ol.index)
+        
+        self.document?.promoteNext(outline: ol)
+        
+        self.pdfListViewDelegate.pdfListView?(self, documentDataDidChanged: ol, withInfo: [Self.outlineKey : true, Self.outlinePromoteKey : true])
+    }
+    
+    private func _undo_demote(outline ol: CPDFOutline, at index: UInt) {
+        (self.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_promote(outline: ol, at: ol.index)
+        
+        self.document?.demote(outline: ol, at: index)
+        
+        self.pdfListViewDelegate.pdfListView?(self, documentDataDidChanged: ol, withInfo: [Self.outlineKey : true, Self.outlineDemoteKey : true, Self.outlineUndoKey : true])
+    }
+    
+    private func _undo_promote(outline ol: CPDFOutline, at index: UInt) {
+        (self.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_demote(outline: ol, at: ol.index)
+        
+        self.document?.promote(outline: ol, at: index)
+        
+        self.pdfListViewDelegate.pdfListView?(self, documentDataDidChanged: ol, withInfo: [Self.outlineKey : true, Self.outlinePromoteKey : true, Self.outlineUndoKey : true])
+    }
+}

+ 5 - 0
PDF Office/PDF Master/Class/Common/KMCommonEnum.swift

@@ -69,6 +69,11 @@ import Foundation
     case paste
     
     case pageEdit
+    
+    // 降级
+    case demote
+    // 升级
+    case promote
 }
 
 @objc enum KMSortMode: Int {

+ 2 - 0
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFListView.h

@@ -308,4 +308,6 @@ typedef NS_ENUM(NSInteger, KMPDFViewSplitMode) {
 
 - (void)PDFListView:(CPDFListView *)sender showSnapshotAtPageNumber:(NSInteger)pageNum forRect:(NSRect)rect scaleFactor:(CGFloat)scaleFactor autoFits:(BOOL)autoFits;
 
+- (void)PDFListView:(CPDFListView *)pdfView documentDataDidChanged:(id)docData withInfo:(NSDictionary *)info;
+
 @end

+ 18 - 70
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/KMLeftSideViewController+Outline.swift

@@ -391,6 +391,21 @@ extension KMLeftSideViewController {
         } while outline != nil
         return num
     }
+    
+    func demoteOutlineAfter(_ ol: CPDFOutline) {
+        Task { @MainActor in
+            self.tocOutlineView.reloadData()
+            self.tocOutlineView.expandItem(ol.parent)
+            self.tocOutlineView.km_selectItem(ol, byExtendingSelection: false)
+        }
+    }
+    
+    func promoteOutlineAfter(_ ol: CPDFOutline) {
+        Task { @MainActor in
+            self.tocOutlineView.reloadData()
+            self.tocOutlineView.km_selectItem(ol, byExtendingSelection: false)
+        }
+    }
 }
 
 // MARK: - Undo & Redo
@@ -428,51 +443,12 @@ extension KMLeftSideViewController {
         }
     }
     
-    @objc dynamic func demoteOutlineWithGrandParent(_ grandParentOutline: CPDFOutline, demoteOutline: CPDFOutline, index: Int) {
-        (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).promoteOutlineWithGrandParent(grandParentOutline, promoteOutline: demoteOutline, rowIndex: index)
-
-        if grandParentOutline.isEqual(to: demoteOutline.parent) {
-            let demoteIndex = demoteOutline.index
-            let previousOutline = grandParentOutline.child(at: demoteIndex-1)
-            demoteOutline.removeFromParent()
-            previousOutline?.insertChild(demoteOutline, at: UInt(index))
-            self.tocOutlineView.reloadData()
-            self.tocOutlineView.expandItem(previousOutline)
-        } else {
-            demoteOutline.removeFromParent()
-            grandParentOutline.insertChild(demoteOutline, at: grandParentOutline.numberOfChildren)
-            self.tocOutlineView.reloadData()
-            self.tocOutlineView.expandItem(grandParentOutline)
-        }
-
-        self.tocOutlineView.km_selectItem(demoteOutline, byExtendingSelection: false)
-    }
-    
-    @objc dynamic func promoteOutlineWithGrandParent(_ grandParentOutline: CPDFOutline, promoteOutline: CPDFOutline, rowIndex: Int) {
-        (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).demoteOutlineWithGrandParent(grandParentOutline, demoteOutline: promoteOutline, index: rowIndex)
-        let index = promoteOutline.parent?.index ?? 0
-        
-        if grandParentOutline.isEqual(to: promoteOutline.parent) {
-            promoteOutline.removeFromParent()
-            grandParentOutline.parent.insertChild(promoteOutline, at: index+1)
-        } else {
-            promoteOutline.removeFromParent()
-            grandParentOutline.insertChild(promoteOutline, at: index+1)
-        }
-
-        self.tocOutlineView.reloadData()
-        
-        self.tocOutlineView.km_selectItem(promoteOutline, byExtendingSelection: false)
-    }
-    
     @objc dynamic func addoutline(parent parentOutline: CPDFOutline?, addOutline: CPDFOutline, index: Int, needExpand: Bool) {
         var tempO: CPDFOutline? = addOutline
         if addOutline.label != nil {
             parentOutline?.insertChild(addOutline, at: UInt(index))
         } else {
-            let outline = parentOutline?.insertChild(at: UInt(index))
-            outline?.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), self.currentPageIndex()+1)
-            outline?.destination = self.currentDestination()
+            let outline = parentOutline?.km_insertChild(label: String(format: "%@ %ld", KMLocalizedString("Page", nil), self.currentPageIndex()+1), dest: self.currentDestination(), at: index)
             tempO = outline
         }
         
@@ -643,41 +619,13 @@ extension KMLeftSideViewController {
     //降级节点
     
     @objc func outlineContextMenuItemClicked_Demote(_ sender: AnyObject?) {
-        guard let currentOutline: CPDFOutline = self.tocOutlineView.km.clickedItem() else {
-            return
-        }
-        let parentOutLine = currentOutline.parent
-        let newParentOutLine = parentOutLine?.child(at: currentOutline.index-1)
-
-        var newIndex = 0
-        let newParentOutLineExpandState = self.tocOutlineView.isItemExpanded(newParentOutLine)
-
-        if (newParentOutLineExpandState) {
-            newIndex = self.tocOutlineView.clickedRow
-        } else {
-            newIndex = self.tocOutlineView.clickedRow + Int(newParentOutLine?.numberOfChildren ?? 0)
-        }
-
-        let currentIndex = currentOutline.index
-        currentOutline.removeFromParent()
-
-        if let data = newParentOutLine {
-            self.demoteOutlineWithGrandParent(data, demoteOutline: currentOutline, index: Int(currentIndex))
-        }
+        self.delegate?.controller?(controller: self, itemClick: sender, itemKey: .demote, params: nil)
     }
     
     //升级节点
     
     @objc func outlineContextMenuItemClicked_Promote(_ sender: AnyObject?) {
-        guard let currentOutline: CPDFOutline = self.tocOutlineView.km.clickedItem() else {
-            return
-        }
-
-        let parentOutLine = currentOutline.parent
-
-        if let grandParentOutLine = parentOutLine?.parent {
-            self.promoteOutlineWithGrandParent(grandParentOutLine, promoteOutline: currentOutline, rowIndex:Int(currentOutline.index))
-        }
+        self.delegate?.controller?(controller: self, itemClick: sender, itemKey: .promote, params: nil)
     }
     
     @objc func toggleOutlineCaseInsensitiveSearch(_ sender: NSMenuItem) {

+ 2 - 8
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/KMLeftSideViewController+Thumbnail.swift

@@ -127,11 +127,9 @@ extension KMLeftSideViewController {
         self.thumbnailTableView.thumbDelegate = self
         self.thumbnailTableView.botaDelegate = self
         self.thumbnailTableView.menu?.delegate = self
-        
-        //        [thumbnailTableView registerForDraggedTypes:@[KPDFThumbnailLocalForDraggedTypes,NSFilenamesPboardType]];
         self.thumbnailTableView.registerForDraggedTypes([.localDraggedTypes, .fileURL,.string,.pdf])
-        self.thumbnailTableView.registerForDraggedTypes(NSFilePromiseReceiver.readableDraggedTypes.map { NSPasteboard.PasteboardType($0) })
-        self.thumbnailTableView.setDraggingSourceOperationMask(.every, forLocal: false)
+//        self.thumbnailTableView.registerForDraggedTypes(NSFilePromiseReceiver.readableDraggedTypes.map { NSPasteboard.PasteboardType($0) })
+//        self.thumbnailTableView.setDraggingSourceOperationMask(.every, forLocal: false)
     }
     
     func thumb_initDefalutValue() {
@@ -575,13 +573,11 @@ extension KMLeftSideViewController {
         }
         let pdf = CPDFDocument()
         for page in pages {
-//            PDFPage *copyPage = [[page copy] autorelease];
             if let _page = page as? CPDFPage {
                 pdf?.insertPageObject(_page, at: pdf?.pageCount ?? 0)
             }
         }
         var fileName = ""
-//        NSString * = [self.pdfDocument.documentURL.lastPathComponent stringByDeletingPathExtension];
         var documentFileName = document.documentURL?.deletingPathExtension().lastPathComponent ?? ""
 
         var tName = self.fileNameWithSelectedPages(self.thumbnailTableView.selectedRowIndexes)
@@ -614,7 +610,6 @@ extension KMLeftSideViewController {
             let sucess = pdf?.write(toFile: cachesDir) ?? false
             if (sucess) {
                 DispatchQueue.main.async {
-//                    [[sender representedObject] performWithItems:array];
                     let represent = (sender as? NSMenuItem)?.representedObject as? NSSharingService
                     represent?.perform(withItems: [url])
                     
@@ -626,7 +621,6 @@ extension KMLeftSideViewController {
                 vc.close()
             }
         }
-//        [vc release];
     }
 }
 

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

@@ -861,31 +861,6 @@ extension KMLeftSideViewController: KMBotaTableViewDelegate {
         }
         return nil
     }
-    
-    /*
-     - (NSArray *)tableView:(NSTableView *)tv typeSelectHelperSelectionStrings:(SKTypeSelectHelper *)typeSelectHelper {
-         if ([tv isEqual:leftSideController.thumbnailTableView]) {
-             return pageLabels;
-         }
-         return nil;
-     }
-
-     - (void)tableView:(NSTableView *)tv typeSelectHelper:(SKTypeSelectHelper *)typeSelectHelper didFailToFindMatchForSearchString:(NSString *)searchString {
-         if ([tv isEqual:leftSideController.thumbnailTableView]) {
-             [statusBar setLeftStringValue:[NSString stringWithFormat:NSLocalizedString(@"No match: \"%@\"", @"Status message"), searchString]];
-         }
-     }
-
-     - (void)tableView:(NSTableView *)tv typeSelectHelper:(SKTypeSelectHelper *)typeSelectHelper updateSearchString:(NSString *)searchString {
-         if ([tv isEqual:leftSideController.thumbnailTableView]) {
-             if (searchString.length > 0)
-                 [statusBar setLeftStringValue:[NSString stringWithFormat:NSLocalizedString(@"Go to page: %@", @"Status message"), searchString]];
-             else
-                 [self updateLeftStatus];
-         }
-     }
-     */
-    
 }
 
 // MARK: - 扩展

+ 10 - 0
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+UI.swift

@@ -558,6 +558,16 @@ extension KMMainViewController: KMLeftSideViewControllerDelegate {
             self.recordIsPDFDocumentEdited(type: itemKey.toSubscribeWaterMarkType())
         } else if itemKey == .pageEdit {
             self.toolbarController.clickItem(KMDocumentPageToolbarItemIdentifier)
+        } else if itemKey == .demote { // 大纲降级
+            guard let currentOutline: CPDFOutline = controller.tocOutlineView.km.clickedItem() else {
+                return
+            }
+            self.listView?.demote(outline: currentOutline)
+        } else if itemKey == .promote { // 大纲升级
+            guard let currentOutline: CPDFOutline = controller.tocOutlineView.km.clickedItem() else {
+                return
+            }
+            self.listView?.promote(outline: currentOutline)
         }
     }
     

+ 16 - 0
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController.swift

@@ -3212,6 +3212,22 @@ let LOCKED_KEY  = "locked"
         self.myDocument?.addWindowController(swc)
     }
     
+    func pdfListView(_ pdfView: CPDFListView!, documentDataDidChanged docData: Any!, withInfo info: [AnyHashable : Any]!) {
+        if let data = info?[CPDFListView.outlineKey] as? Bool, data { // 大纲改变
+            guard let ol = docData as? CPDFOutline else {
+                return
+            }
+            let demote = info?[CPDFListView.outlineDemoteKey] as? Bool ?? false
+            let promote = info?[CPDFListView.outlinePromoteKey] as? Bool ?? false
+            if demote {
+                self.leftSideViewController.demoteOutlineAfter(ol)
+            }
+            if promote {
+                self.leftSideViewController.promoteOutlineAfter(ol)
+            }
+        }
+    }
+    
     // MARK: Split View
     
     func changePDFDocument(isChange: Bool, replaceBlock: @escaping (String) -> Void) {

+ 16 - 0
PDF Office/PDF Reader Pro.xcodeproj/xcuserdata/kdanmobile.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -596,5 +596,21 @@
             landmarkType = "7">
          </BreakpointContent>
       </BreakpointProxy>
+      <BreakpointProxy
+         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
+         <BreakpointContent
+            uuid = "1A7283C5-76BA-4504-9A88-46A4A08FD00E"
+            shouldBeEnabled = "Yes"
+            ignoreCount = "0"
+            continueAfterRunningActions = "No"
+            filePath = "PDF Master/Class/PDFWindowController/Side/LeftSide/KMLeftSideViewController.swift"
+            startingColumnNumber = "9223372036854775807"
+            endingColumnNumber = "9223372036854775807"
+            startingLineNumber = "2567"
+            endingLineNumber = "2567"
+            landmarkName = "outlineView(_:acceptDrop:item:childIndex:)"
+            landmarkType = "7">
+         </BreakpointContent>
+      </BreakpointProxy>
    </Breakpoints>
 </Bucket>