|
@@ -9,8 +9,12 @@ import Cocoa
|
|
|
import PDFKit
|
|
|
|
|
|
typealias KMBookMarkViewControllerBookMarkDidChange = (_ controller: KMBookMarkViewController, _ bookMarks: [KMBookMarkItem]) -> Void
|
|
|
-class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
|
|
|
+@objc protocol KMBookMarkViewControllerDelegate: NSObjectProtocol {
|
|
|
+ @objc optional func bkControllerAddAction(controller: KMBookMarkViewController)
|
|
|
+}
|
|
|
+
|
|
|
+class KMBookMarkViewController: KMNBaseViewController {
|
|
|
@IBOutlet weak var addBookButton: NSButton!
|
|
|
@IBOutlet weak var titleTextField: NSTextField!
|
|
|
@IBOutlet weak var sortButton: NSButton!
|
|
@@ -26,19 +30,23 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
var renameTextField: NSTextField!
|
|
|
var renamePDFBook: KMBookMarkItem!
|
|
|
var renameCellView: KMBookCellView!
|
|
|
- var listView: CPDFListView!
|
|
|
- let pdfView = PDFView.init()
|
|
|
+// var listView: CPDFListView!
|
|
|
+ weak var document: CPDFDocument?
|
|
|
|
|
|
var isLocalEvent: Bool = false //区分外部点击还是内部点击
|
|
|
|
|
|
var selectItems: [KMBookMarkItem] = []
|
|
|
|
|
|
var bookMarkDidChange: KMBookMarkViewControllerBookMarkDidChange?
|
|
|
+
|
|
|
+ weak var delegate: KMBookMarkViewControllerDelegate?
|
|
|
+
|
|
|
+ var handdler: KMNBookmarkHanddler?
|
|
|
// 升序
|
|
|
private var sortType_: KMSortMode = .ascending
|
|
|
|
|
|
- deinit {
|
|
|
- NotificationCenter.default.removeObserver(self)
|
|
|
+ convenience init() {
|
|
|
+ self.init(nibName: "KMBookMarkViewController", bundle: nil)
|
|
|
}
|
|
|
|
|
|
override func viewWillAppear() {
|
|
@@ -52,10 +60,8 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
|
|
|
self.topSeplineView.wantsLayer = true
|
|
|
self.topSeplineView.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.1).cgColor
|
|
|
-
|
|
|
- if #available(macOS 11, *) {
|
|
|
- self.bookTableView.style = NSTableView.Style.plain
|
|
|
- }
|
|
|
+
|
|
|
+ self.bookTableView.style = NSTableView.Style.plain
|
|
|
self.bookTableView.allowsMultipleSelection = true
|
|
|
self.bookTableView.doubleAction = #selector(renameBookAction)
|
|
|
// self.bookTableView.selectionHighlightStyle = NSTableView.SelectionHighlightStyle.none;
|
|
@@ -65,8 +71,6 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
|
|
|
self.refreshUI()
|
|
|
self.reloadData()
|
|
|
-
|
|
|
- self.initNotification()
|
|
|
}
|
|
|
|
|
|
func refreshUI() {
|
|
@@ -90,7 +94,7 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
}
|
|
|
|
|
|
func reloadData() {
|
|
|
- let array = self.listView.document.bookmarks() ?? [CPDFBookmark]()
|
|
|
+ let array = document?.bookmarks() ?? [CPDFBookmark]()
|
|
|
var bookMarks: [KMBookMarkItem] = []
|
|
|
for bookMark in array {
|
|
|
let item = KMBookMarkItem()
|
|
@@ -124,15 +128,13 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- func initNotification() {
|
|
|
+ override func addNotifations() {
|
|
|
+ super.addNotifations()
|
|
|
+
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(KMPDFViewCurrentPageDidChangedNotification), name: NSNotification.Name.init(rawValue: "KMPDFViewCurrentPageDidChanged"), object: nil)
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(documentPageCountChangedNotification), name: NSNotification.Name.init(rawValue: "CPDFDocumentPageCountChangedNotification"), object: nil)
|
|
|
}
|
|
|
|
|
|
- func removeNotification() {
|
|
|
- NotificationCenter.default.removeObserver(self)
|
|
|
- }
|
|
|
-
|
|
|
private func addMenuTitle(view: NSView, event: NSEvent) {
|
|
|
let menus : NSMenu = NSMenu(title: "")
|
|
|
menus.delegate = self
|
|
@@ -171,33 +173,17 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
}
|
|
|
|
|
|
@IBAction func addBookmarkAction(_ sender: Any) {
|
|
|
-// if self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) == nil {
|
|
|
-// let label = "\(NSLocalizedString("Page", comment:"")) \(self.listView.currentPageIndex + 1)"
|
|
|
-// let bookMark = KMBookMarkItem()
|
|
|
-// bookMark.label = label
|
|
|
-// bookMark.index = UInt(self.listView.currentPageIndex)
|
|
|
-// self.addBookMark(bookMarks: [bookMark])
|
|
|
-// } else {
|
|
|
-// let bookMark = self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex))
|
|
|
-// self.dataSource.contains { KMBookMarkItem in
|
|
|
-// if KMBookMarkItem.bookMark == bookMark {
|
|
|
-// let index = Int(KMBookMarkItem.index)
|
|
|
-// self.bookTableView.selectRowIndexes(IndexSet(integer: index), byExtendingSelection: false)
|
|
|
-// self.didSelectItem(row: index, event: NSEvent())
|
|
|
-// return true
|
|
|
-// }
|
|
|
-// return false
|
|
|
-// }
|
|
|
-// }
|
|
|
+ delegate?.bkControllerAddAction?(controller: self)
|
|
|
+ handdler?.addCurrentBookmark()
|
|
|
|
|
|
- let menu = NSMenu()
|
|
|
- let removeItem = NSMenuItem(title: NSLocalizedString("Remove Bookmark", comment: ""), action: #selector(removeBookmark), keyEquivalent: "")
|
|
|
- removeItem.target = self
|
|
|
- menu.addItem(removeItem)
|
|
|
- let removeAllItem = NSMenuItem(title: NSLocalizedString("Remove All Bookmarks", comment: ""), action: #selector(removeAllBookmark), keyEquivalent: "")
|
|
|
- menu.addItem(removeAllItem)
|
|
|
- removeAllItem.target = self
|
|
|
- menu.popUp(positioning: removeItem, at: .init(x: 0, y: 24), in: sender as? NSView)
|
|
|
+// let menu = NSMenu()
|
|
|
+// let removeItem = NSMenuItem(title: NSLocalizedString("Remove Bookmark", comment: ""), action: #selector(removeBookmark), keyEquivalent: "")
|
|
|
+// removeItem.target = self
|
|
|
+// menu.addItem(removeItem)
|
|
|
+// let removeAllItem = NSMenuItem(title: NSLocalizedString("Remove All Bookmarks", comment: ""), action: #selector(removeAllBookmark), keyEquivalent: "")
|
|
|
+// menu.addItem(removeAllItem)
|
|
|
+// removeAllItem.target = self
|
|
|
+// menu.popUp(positioning: removeItem, at: .init(x: 0, y: 24), in: sender as? NSView)
|
|
|
}
|
|
|
|
|
|
@objc func changeLocationAction() {
|
|
@@ -213,7 +199,7 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
let bookMark = KMBookMarkItem()
|
|
|
bookMark.bookMark = item.bookMark
|
|
|
bookMark.label = item.label
|
|
|
- bookMark.index = UInt(self.listView.currentPageIndex)
|
|
|
+// bookMark.index = UInt(self.listView.currentPageIndex)
|
|
|
self.changeLocation(oldBookMark: item,
|
|
|
newBookMark: bookMark)
|
|
|
}
|
|
@@ -259,15 +245,15 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
@objc func KMPDFViewCurrentPageDidChangedNotification(notification: NSNotification) {
|
|
|
if notification.object is CPDFDocument {
|
|
|
let pdfdocument : CPDFDocument = notification.object as! CPDFDocument
|
|
|
- if pdfdocument.isEqual(self.listView.document) {
|
|
|
+ if pdfdocument.isEqual(document) {
|
|
|
if !isLocalEvent {
|
|
|
var containSelIndex:Bool = false
|
|
|
for (index, value) in self.dataSource.enumerated() {
|
|
|
- if value.bookMark == self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) {
|
|
|
+// if value.bookMark == document?.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) {
|
|
|
containSelIndex = true
|
|
|
self.didSelectItem(row: index, event: NSEvent())
|
|
|
break
|
|
|
- }
|
|
|
+// }
|
|
|
}
|
|
|
if !containSelIndex {
|
|
|
self.cancelSelect()
|
|
@@ -283,7 +269,7 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
if notification.object is CPDFDocument {
|
|
|
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) { [weak self] in
|
|
|
let pdfdocument : CPDFDocument = notification.object as! CPDFDocument
|
|
|
- if pdfdocument.isEqual(self?.listView?.document) {
|
|
|
+ if pdfdocument.isEqual(self?.document) {
|
|
|
self?.reloadData()
|
|
|
}
|
|
|
}
|
|
@@ -303,8 +289,11 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
self.bookTableView.backgroundColor = NSColor.km_init(hex: "#F7F8FA")
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- //MARK: - NSTextFieldDelegate
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - NSTextFieldDelegate
|
|
|
+
|
|
|
+extension KMBookMarkViewController: NSTextFieldDelegate {
|
|
|
func controlTextDidEndEditing(_ obj: Notification) {
|
|
|
if (self.renameTextField.isEqual(obj.object)) {
|
|
|
let textField : NSTextField = obj.object as! NSTextField
|
|
@@ -314,6 +303,8 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// MARK: - NSTableViewDelegate,NSTableViewDataSource
|
|
|
+
|
|
|
extension KMBookMarkViewController : NSTableViewDelegate,NSTableViewDataSource {
|
|
|
func numberOfRows(in tableView: NSTableView) -> Int {
|
|
|
let count = self.dataSource?.count ?? 0
|
|
@@ -337,7 +328,7 @@ extension KMBookMarkViewController : NSTableViewDelegate,NSTableViewDataSource {
|
|
|
} else {
|
|
|
cell.dateLabel.stringValue = ""
|
|
|
}
|
|
|
- if let page = self.listView.document.page(at: UInt(item.bookMark.pageIndex)) {
|
|
|
+ if let page = document?.page(at: UInt(item.bookMark.pageIndex)) {
|
|
|
if let data = page.PDFListViewTIFFData(for: page.bounds) {
|
|
|
let image = NSImage(data: data)
|
|
|
cell.iconIv.image = image
|
|
@@ -471,7 +462,7 @@ extension KMBookMarkViewController : NSTableViewDelegate,NSTableViewDataSource {
|
|
|
self.bookTableView.selectedRowIndexes.first! < self.dataSource.count {
|
|
|
let index = self.bookTableView.selectedRowIndexes.first
|
|
|
let selectBookMark = self.dataSource[index!]
|
|
|
- self.listView.go(toPageIndex: selectBookMark.bookMark.pageIndex, animated: true)
|
|
|
+// self.listView.go(toPageIndex: selectBookMark.bookMark.pageIndex, animated: true)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -494,11 +485,11 @@ extension KMBookMarkViewController : NSTableViewDelegate,NSTableViewDataSource {
|
|
|
}
|
|
|
|
|
|
func canAddBorkMark() -> Bool {
|
|
|
- if self.listView.document.bookmarks() != nil && self.listView.document.bookmarks()?.count != 0 {
|
|
|
- for bookMark in self.listView.document.bookmarks() {
|
|
|
- if bookMark.pageIndex == self.listView.currentPageIndex {
|
|
|
- return false
|
|
|
- }
|
|
|
+ if document?.bookmarks() != nil && document?.bookmarks()?.count != 0 {
|
|
|
+ for bookMark in document?.bookmarks() ?? [] {
|
|
|
+// if bookMark.pageIndex == self.listView.currentPageIndex {
|
|
|
+// return false
|
|
|
+// }
|
|
|
}
|
|
|
}
|
|
|
return true
|
|
@@ -526,18 +517,19 @@ extension KMBookMarkViewController : NSTableViewDelegate,NSTableViewDataSource {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-//MARK: undoRedo
|
|
|
+//MARK: - undoRedo
|
|
|
+
|
|
|
extension KMBookMarkViewController {
|
|
|
func changeLocation(oldBookMark: KMBookMarkItem, newBookMark: KMBookMarkItem) {
|
|
|
- self.listView.document.removeBookmark(forPageIndex: oldBookMark.index)
|
|
|
- self.listView.document.addBookmark(newBookMark.label, forPageIndex: newBookMark.index)
|
|
|
+ document?.removeBookmark(forPageIndex: oldBookMark.index)
|
|
|
+ document?.addBookmark(newBookMark.label, forPageIndex: newBookMark.index)
|
|
|
|
|
|
- self.reloadData()
|
|
|
- self.listView.setNeedsDisplayForVisiblePages()
|
|
|
+ reloadData()
|
|
|
+// self.listView.setNeedsDisplayForVisiblePages()
|
|
|
|
|
|
- self.listView.undoManager?.registerUndo(withTarget: self) { [unowned self] targetType in
|
|
|
- self.changeLocation(oldBookMark: newBookMark, newBookMark: oldBookMark)
|
|
|
- }
|
|
|
+// self.listView.undoManager?.registerUndo(withTarget: self) { [unowned self] targetType in
|
|
|
+// self.changeLocation(oldBookMark: newBookMark, newBookMark: oldBookMark)
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
func renamePDFBook(bookmark : KMBookMarkItem! , label:String) {
|
|
@@ -553,27 +545,27 @@ extension KMBookMarkViewController {
|
|
|
indexSet.insert(self.bookTableView.row(for: self.renameCellView))
|
|
|
self.bookTableView.selectRowIndexes(indexSet, byExtendingSelection: false)
|
|
|
|
|
|
- self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
|
|
|
- bookmark.bookMark.label = label
|
|
|
- self?.renamePDFBook(bookmark: bookmark, label: temp ?? bookmark.label)
|
|
|
- }
|
|
|
+// self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
|
|
|
+// bookmark.bookMark.label = label
|
|
|
+// self?.renamePDFBook(bookmark: bookmark, label: temp ?? bookmark.label)
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
func deleteBookMark(bookMarks: [KMBookMarkItem]) {
|
|
|
for bookMark in bookMarks {
|
|
|
- if self.listView.document.removeBookmark(forPageIndex: bookMark.index) {
|
|
|
+ if ((document?.removeBookmark(forPageIndex: bookMark.index)) != nil) {
|
|
|
KMPrint("删除标签成功")
|
|
|
}
|
|
|
}
|
|
|
- self.listView.setNeedsDisplayForVisiblePages()
|
|
|
+// self.listView.setNeedsDisplayForVisiblePages()
|
|
|
self.reloadData()
|
|
|
|
|
|
//undo redo
|
|
|
var saveBooks:[KMBookMarkItem] = bookMarks
|
|
|
- self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
|
|
|
- saveBooks.sort(){$0.index > $1.index}
|
|
|
- self?.addBookMark(bookMarks: bookMarks)
|
|
|
- }
|
|
|
+// self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
|
|
|
+// saveBooks.sort(){$0.index > $1.index}
|
|
|
+// self?.addBookMark(bookMarks: bookMarks)
|
|
|
+// }
|
|
|
|
|
|
guard let callBack = bookMarkDidChange else { return }
|
|
|
|
|
@@ -582,16 +574,16 @@ extension KMBookMarkViewController {
|
|
|
|
|
|
func addBookMark(bookMarks: [KMBookMarkItem]) {
|
|
|
for bookMark in bookMarks {
|
|
|
- self.listView.document.addBookmark(bookMark.label, forPageIndex: UInt(bookMark.index))
|
|
|
+ document?.addBookmark(bookMark.label, forPageIndex: UInt(bookMark.index))
|
|
|
}
|
|
|
- self.listView.setNeedsDisplayForVisiblePages()
|
|
|
+// self.listView.setNeedsDisplayForVisiblePages()
|
|
|
self.reloadData()
|
|
|
|
|
|
if bookMarks.count == 1 {
|
|
|
- DispatchQueue.main.async {
|
|
|
- if self.listView.document.bookmark(forPageIndex: UInt(bookMarks.first!.index)) != nil {
|
|
|
+ DispatchQueue.main.async { [self] in
|
|
|
+ if document?.bookmark(forPageIndex: UInt(bookMarks.first!.index)) != nil {
|
|
|
let item = KMBookMarkItem()
|
|
|
- item.bookMark = self.listView.document.bookmark(forPageIndex: UInt(bookMarks.first!.index))
|
|
|
+ item.bookMark = (document?.bookmark(forPageIndex: UInt(bookMarks.first!.index)))!
|
|
|
item.label = item.bookMark.label
|
|
|
item.index = UInt(item.bookMark.pageIndex)
|
|
|
self.addBookMarkAndEdit(newBookMark: item)
|
|
@@ -601,10 +593,10 @@ extension KMBookMarkViewController {
|
|
|
|
|
|
//undo redo
|
|
|
var saveBooks:[KMBookMarkItem] = bookMarks
|
|
|
- self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
|
|
|
- saveBooks.sort(){$0.index > $1.index}
|
|
|
- self?.deleteBookMark(bookMarks: saveBooks)
|
|
|
- }
|
|
|
+// self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
|
|
|
+// saveBooks.sort(){$0.index > $1.index}
|
|
|
+// self?.deleteBookMark(bookMarks: saveBooks)
|
|
|
+// }
|
|
|
|
|
|
guard let callBack = bookMarkDidChange else { return }
|
|
|
|
|
@@ -612,26 +604,28 @@ extension KMBookMarkViewController {
|
|
|
}
|
|
|
|
|
|
@IBAction func undo(_ sender: Any) {
|
|
|
- if (self.listView.undoManager?.canUndo ?? false) {
|
|
|
- self.listView.undoManager?.undo()
|
|
|
- }
|
|
|
+// if (self.listView.undoManager?.canUndo ?? false) {
|
|
|
+// self.listView.undoManager?.undo()
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
@IBAction func redo(_ sender: Any) {
|
|
|
- if (self.listView.undoManager?.canRedo ?? false) {
|
|
|
- self.listView.undoManager?.redo()
|
|
|
- }
|
|
|
+// if (self.listView.undoManager?.canRedo ?? false) {
|
|
|
+// self.listView.undoManager?.redo()
|
|
|
+// }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// MARK: - NSMenuDelegate, NSMenuItemValidation
|
|
|
+
|
|
|
extension KMBookMarkViewController: NSMenuDelegate, NSMenuItemValidation {
|
|
|
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
|
|
|
let action = menuItem.action
|
|
|
if (action == #selector(undo)) {
|
|
|
- return self.listView.undoManager?.canUndo ?? false
|
|
|
+// return self.listView.undoManager?.canUndo ?? false
|
|
|
}
|
|
|
if (action == #selector(redo)) {
|
|
|
- return self.listView.undoManager?.canRedo ?? false
|
|
|
+// return self.listView.undoManager?.canRedo ?? false
|
|
|
}
|
|
|
|
|
|
if action == #selector(renameBookAction) ||
|