Преглед на файлове

【BOTA】大纲编辑菜单功能补充

tangchao преди 1 година
родител
ревизия
da4fe66308

+ 31 - 0
PDF Office/PDF Master/Class/Common/Category/NSObject+KMExtension.swift

@@ -577,6 +577,37 @@ extension NSApplication {
     public static let interfaceThemeChangedNotification = NSNotification.Name("AppleInterfaceThemeChangedNotification")
 }
 
+
+// MARK: - NSOutlineView
+
+extension NSOutlineView {
+    func clickedItem() -> Any? {
+        let row = self.clickedRow
+        if row < 0 {
+            return nil
+        }
+        return self.item(atRow: row)
+    }
+    
+    func km_selectRow(_ row: Int, byExtendingSelection extend: Bool = true) {
+        self.selectRowIndexes(IndexSet(integer: row), byExtendingSelection: extend)
+    }
+    
+    func km_selectItem(_ item: Any, byExtendingSelection extend: Bool = true) {
+        let row = self.row(forItem: item)
+        if row == -1 || row == NSNotFound {
+            return
+        }
+        self.km_selectRow(row, byExtendingSelection: extend)
+    }
+}
+
+extension KM where Base: NSOutlineView {
+    public func clickedItem<T>() -> T {
+        return base.clickedItem() as! T
+    }
+}
+
 // MARK: - NSParagraphStyle
 
 extension NSParagraphStyle {

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

@@ -787,3 +787,17 @@ extension KMLeftSideViewController: NSMenuItemValidation {
         return true
     }
 }
+
+// MARK: - NSPopoverDelegate
+
+extension KMLeftSideViewController: NSPopoverDelegate {
+    func popoverWillClose(_ notification: Notification) {
+        guard let popover = notification.object as? NSPopover else {
+            return
+        }
+        if let vc = popover.contentViewController as? KMOutlineEditViewController {
+            self.editOutlineUI(vc)
+            self.tocOutlineView.reloadData()
+        }
+    }
+}

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

@@ -142,10 +142,89 @@ extension KMLeftSideViewController {
         }
     }
     
+    func editOutlineUI(_ editVC: KMOutlineEditViewController) {
+        if editVC.pageButton.state == .on {
+            let numberString = editVC.outlineTargetPageIndexTextField.stringValue
+            let idx = Int(numberString) ?? 1
+            let newPage = editVC.pdfView.document.page(at: UInt(idx-1))
+            let originalPage = editVC.originalDestination?.page()
+            if let data = newPage?.isEqual(to: originalPage), data {
+                //新page不存在
+                if (newPage == nil) {
+
+                } else {
+                    let pageSize = newPage?.bounds(for: .cropBox).size ?? .zero
+                    if let destination = CPDFDestination(page: newPage, at: CGPointMake(pageSize.width, pageSize.height)) {
+                        self.changePDFOutlineDestination(destination, PDFoutline: editVC.outline)
+                    }
+                }
+            }
+        } else if editVC.urlButton.state == .on {
+            var urlString = editVC.outlineURLTextField.stringValue
+            let tLowerUrl = urlString.lowercased()
+            if tLowerUrl.hasPrefix("https://") == false && tLowerUrl.hasPrefix("ftp://") == false && tLowerUrl.hasPrefix("http://") == false && urlString.isEmpty == false {
+                urlString = "http://\(urlString)"
+            }
+
+            if let urlAction = CPDFURLAction(url: urlString) {
+                if editVC.originalURLString != editVC.outlineURLTextField.stringValue {
+                    self.changePDFAction(urlAction, PDFOutline: editVC.outline)
+                }
+            }
+        } else if editVC.mailButton.state == .on {
+            var mailString = editVC.mailAddressTextField.stringValue
+            let tLowerStr = mailString.lowercased()
+            if tLowerStr.hasPrefix("mailto:") == false {
+                mailString = "mailto:\(mailString)"
+            }
+
+            if var urlAction = CPDFURLAction(url: mailString) {
+                if urlAction.url() == nil {
+                    urlAction = CPDFURLAction(url: "mailto:")
+                }
+                if mailString != editVC.originalURLString {
+                    self.changePDFAction(urlAction, PDFOutline: editVC.outline)
+                }
+            }
+        }
+        
+        if editVC.outlineNameTextView.string == editVC.originalLabel {
+            return
+        }
+
+        self.renamePDFOutline(editVC.outline, label: editVC.outlineNameTextView.string)
+    }
+}
+
+// MARK: - Undo & Redo
+
+extension KMLeftSideViewController {
     @objc dynamic func changePDFOutlineDestination(_ destination: CPDFDestination, PDFoutline outline: CPDFOutline) {
         (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).changePDFOutlineDestination(outline.destination, PDFoutline: outline)
         outline.destination = destination
     }
+    
+    @objc dynamic func changePDFAction(_ action: CPDFAction, PDFOutline outline: CPDFOutline) {
+        (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).changePDFAction(outline.action, PDFOutline: outline)
+        outline.action = action
+    }
+    
+    @objc dynamic func renamePDFOutline(_ outline: CPDFOutline, label: String) {
+        if (self.isRenameNoteOutline) {
+            if outline is CPDFAnnotation {
+//            if([outline isKindOfClass:[PDFAnnotation class]]) {
+//                PDFAnnotation *annotation = (PDFAnnotation *)outline;
+//                annotation.string = label;
+//                [rightSideController.noteOutlineView reloadData];
+//                [rightSideController.noteOutlineView selectRowIndexes:[[[NSIndexSet alloc] initWithIndex:[rightSideController.noteOutlineView rowForItem:outline]] autorelease] byExtendingSelection:NO];
+            }
+        } else {
+            (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).renamePDFOutline(outline, label: outline.label)
+            outline.label = label
+            self.tocOutlineView.reloadData()
+            self.tocOutlineView.km_selectItem(outline, byExtendingSelection: false)
+        }
+    }
 }
 
 // MARK: - Menu Actions
@@ -227,4 +306,18 @@ extension KMLeftSideViewController {
             }
         }
     }
+    
+    //弹出菜单
+    
+    @objc func outlineContextMenuItemClicked_Edit(_ sender: AnyObject?) {
+        let popover = NSPopover()
+        popover.delegate = self
+        let targetOutline: CPDFOutline? = self.tocOutlineView.km.clickedItem()
+        let outlineEditViewController = KMOutlineEditViewController(outline: targetOutline, document: self.listView)
+        let cell = self.tocOutlineView.rowView(atRow: self.tocOutlineView.clickedRow, makeIfNecessary: true)
+        popover.contentViewController = outlineEditViewController
+        popover.animates = true
+        popover.behavior = .transient
+        popover.show(relativeTo: cell?.bounds ?? .zero, of: cell!, preferredEdge: .minX)
+    }
 }

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

@@ -239,6 +239,8 @@ class KMLeftSideViewController: KMSideViewController {
     var notes: [CPDFAnnotation] = []
     var canFoldNotes: [CPDFAnnotation] = []
     
+    var isRenameNoteOutline = false
+    
     override func loadView() {
         super.loadView()
         
@@ -4749,10 +4751,6 @@ extension KMLeftSideViewController {
         }
     }
     
-    @objc func outlineContextMenuItemClicked_Edit(_ sender: AnyObject?) {
-        KMPrint("KMLeftSideViewController-outlineContextMenuItemClicked_Edit...")
-    }
-    
     @objc func outlineContextMenuItemClicked_Rename(_ sender: AnyObject?) {
         KMPrint("KMLeftSideViewController-outlineContextMenuItemClicked_Rename...")
     }

+ 4 - 4
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/Outline/KMOutlineEditViewController.swift

@@ -20,10 +20,10 @@ class KMOutlineEditViewController: NSViewController {
     @IBOutlet weak var mailButton: NSButton!
     var outline : CPDFOutline!
     var pdfView : CPDFListView!
-    var originalURLString : String!
-    var originalDestination : CPDFDestination!
-    var originalLabel : String!
-    var currentPageIndex : Int!
+    var originalURLString : String = ""
+    var originalDestination : CPDFDestination?
+    var originalLabel : String = ""
+    var currentPageIndex : Int = 0
     convenience init(outline:CPDFOutline!,document:CPDFListView!) {
         self.init()
         self.outline = outline

+ 1 - 1
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/Outline/KMOutlineViewController.swift

@@ -150,7 +150,7 @@ class KMOutlineViewController: NSViewController {
     func editOutlineUI(editVC : KMOutlineEditViewController!) {
         if editVC.pageButton.state == NSControl.StateValue.on {
             let index = Int(editVC.outlineTargetPageIndexTextField.stringValue)!
-            if editVC.originalDestination.pageIndex != index {
+            if editVC.originalDestination?.pageIndex != index {
                 let page = editVC.pdfView.document.page(at: UInt(index))
                 if page != nil {
                     let destination = CPDFDestination.init(document: editVC.pdfView.document, pageIndex: index)