|
@@ -29,6 +29,9 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate,NSMenuItem
|
|
|
|
|
|
var selectedRowIndexs : IndexSet = []
|
|
|
|
|
|
+ //undo redo
|
|
|
+ private var undoRedoManager: UndoManager = UndoManager()
|
|
|
+
|
|
|
func dealloc() {
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
}
|
|
@@ -142,13 +145,9 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate,NSMenuItem
|
|
|
}
|
|
|
|
|
|
@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)"
|
|
|
- self.listView.document.addBookmark(label, forPageIndex: UInt(self.listView.currentPageIndex))
|
|
|
- self.listView.setNeedsDisplayForVisiblePages()
|
|
|
- self.addBookMarkAndEdit(newBookMark: self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)))
|
|
|
+ self.addBookMark(bookMarks: [KMBookMark(label: label, index: UInt(self.listView.currentPageIndex))])
|
|
|
} else {
|
|
|
let alter = NSAlert()
|
|
|
alter.alertStyle = NSAlert.Style.informational
|
|
@@ -180,11 +179,8 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate,NSMenuItem
|
|
|
alter.addButton(withTitle: NSLocalizedString("NO", comment:""))
|
|
|
let modlres = alter.runModal()
|
|
|
if modlres == NSApplication.ModalResponse.alertFirstButtonReturn {
|
|
|
- self.listView.document.removeBookmark(forPageIndex:UInt(item.pageIndex))
|
|
|
- self.listView.document.addBookmark(item.label, forPageIndex: UInt(self.listView.currentPageIndex))
|
|
|
-
|
|
|
- self.reloadData()
|
|
|
- self.listView.setNeedsDisplayForVisiblePages()
|
|
|
+ self.changeLocation(oldBookMark: KMBookMark(label: item.label, index: UInt(item.pageIndex)),
|
|
|
+ newBookMark: KMBookMark(label: item.label, index: UInt(self.listView.currentPageIndex)))
|
|
|
}
|
|
|
} else {
|
|
|
__NSBeep()
|
|
@@ -193,14 +189,12 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate,NSMenuItem
|
|
|
|
|
|
@objc func deleteBookAction() {
|
|
|
if self.bookTableView.clickedRow >= 0 {
|
|
|
+ var bookMarks:[KMBookMark] = []
|
|
|
for index in self.bookTableView.selectedRowIndexes {
|
|
|
let item = self.dataSource[index]
|
|
|
- if self.listView.document.removeBookmark(forPageIndex:UInt(item.pageIndex)) {
|
|
|
- print("删除标签成功")
|
|
|
- }
|
|
|
+ bookMarks.append(KMBookMark(label: item.label, index: UInt(item.pageIndex)))
|
|
|
}
|
|
|
- self.listView.setNeedsDisplayForVisiblePages()
|
|
|
- self.reloadData()
|
|
|
+ self.deleteBookMark(bookMarks: bookMarks)
|
|
|
} else {
|
|
|
__NSBeep()
|
|
|
}
|
|
@@ -215,18 +209,6 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate,NSMenuItem
|
|
|
self.renameTextField.becomeFirstResponder()
|
|
|
}
|
|
|
|
|
|
- func renamePDFBook(bookmark : CPDFBookmark! , label:String) {
|
|
|
- if bookmark.label == label {
|
|
|
- return
|
|
|
- }
|
|
|
- bookmark.label = label
|
|
|
- self.reloadData()
|
|
|
-
|
|
|
- var indexSet = IndexSet()
|
|
|
- indexSet.insert(self.bookTableView.row(for: self.renameCellView))
|
|
|
- self.bookTableView.selectRowIndexes(indexSet, byExtendingSelection: false)
|
|
|
- }
|
|
|
-
|
|
|
//MARK: Noti
|
|
|
@objc func KMPDFViewCurrentPageDidChangedNotification(notification: NSNotification) {
|
|
|
// let pdfdocument : CPDFDocument = notification.object as! CPDFDocument
|
|
@@ -257,31 +239,31 @@ class KMBookMarkViewController: NSViewController, NSTextFieldDelegate,NSMenuItem
|
|
|
|
|
|
//MARK: NSMenuItemValidation
|
|
|
|
|
|
- func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
|
|
|
- let action = menuItem.action
|
|
|
- if action == #selector(renameBookAction) ||
|
|
|
- action == #selector(changeLocationAction) ||
|
|
|
- action == #selector(deleteBookAction) {
|
|
|
- if self.bookTableView.selectedRowIndexes.count > 1 {
|
|
|
- if action == #selector(changeLocationAction) {
|
|
|
- return false
|
|
|
- } else if action == #selector(renameBookAction) {
|
|
|
- return false
|
|
|
- }
|
|
|
- } else {
|
|
|
- if self.bookTableView.clickedRow > -1 {
|
|
|
- if action == #selector(changeLocationAction) {
|
|
|
- // if self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) != nil {
|
|
|
- // return false
|
|
|
- // }
|
|
|
- }
|
|
|
- } else {
|
|
|
- return false
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return true
|
|
|
- }
|
|
|
+// func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
|
|
|
+// let action = menuItem.action
|
|
|
+// if action == #selector(renameBookAction) ||
|
|
|
+// action == #selector(changeLocationAction) ||
|
|
|
+// action == #selector(deleteBookAction) {
|
|
|
+// if self.bookTableView.selectedRowIndexes.count > 1 {
|
|
|
+// if action == #selector(changeLocationAction) {
|
|
|
+// return false
|
|
|
+// } else if action == #selector(renameBookAction) {
|
|
|
+// return false
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// if self.bookTableView.clickedRow > -1 {
|
|
|
+// if action == #selector(changeLocationAction) {
|
|
|
+// // if self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) != nil {
|
|
|
+// // return false
|
|
|
+// // }
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// return false
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return true
|
|
|
+// }
|
|
|
|
|
|
}
|
|
|
|
|
@@ -312,14 +294,16 @@ extension KMBookMarkViewController : NSTableViewDelegate,NSTableViewDataSource {
|
|
|
|
|
|
func tableViewSelectionDidChange(_ notification: Notification) {
|
|
|
for index in self.selectedRowIndexs {
|
|
|
- if self.bookTableView.selectedRowIndexes.contains(index) {
|
|
|
- guard let rowView: KMBookMarkTableRowView = self.bookTableView.rowView(atRow: index, makeIfNecessary: false) as? KMBookMarkTableRowView else { return }
|
|
|
-
|
|
|
- rowView.itemSelect = true
|
|
|
- } else {
|
|
|
- guard let rowView: KMBookMarkTableRowView = self.bookTableView.rowView(atRow: index, makeIfNecessary: false) as? KMBookMarkTableRowView else { return }
|
|
|
-
|
|
|
- rowView.itemSelect = false
|
|
|
+ if index < self.bookTableView.numberOfRows {
|
|
|
+ if self.bookTableView.selectedRowIndexes.contains(index) {
|
|
|
+ guard let rowView: KMBookMarkTableRowView = self.bookTableView.rowView(atRow: index, makeIfNecessary: false) as? KMBookMarkTableRowView else { return }
|
|
|
+
|
|
|
+ rowView.itemSelect = true
|
|
|
+ } else {
|
|
|
+ guard let rowView: KMBookMarkTableRowView = self.bookTableView.rowView(atRow: index, makeIfNecessary: false) as? KMBookMarkTableRowView else { return }
|
|
|
+
|
|
|
+ rowView.itemSelect = false
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -332,4 +316,125 @@ extension KMBookMarkViewController : NSTableViewDelegate,NSTableViewDataSource {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//MARK: undoRedo
|
|
|
+extension KMBookMarkViewController {
|
|
|
+ func changeLocation(oldBookMark: KMBookMark, newBookMark: KMBookMark) {
|
|
|
+ self.listView.document.removeBookmark(forPageIndex: oldBookMark.index)
|
|
|
+ self.listView.document.addBookmark(newBookMark.label, forPageIndex: newBookMark.index)
|
|
|
+
|
|
|
+ self.reloadData()
|
|
|
+ self.listView.setNeedsDisplayForVisiblePages()
|
|
|
+
|
|
|
+ self.undoRedoManager.registerUndo(withTarget: self) { [unowned self] targetType in
|
|
|
+ self.changeLocation(oldBookMark: newBookMark, newBookMark: oldBookMark)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func renamePDFBook(bookmark : CPDFBookmark! , label:String) {
|
|
|
+ if bookmark.label == label {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ let temp = bookmark.label
|
|
|
+
|
|
|
+ bookmark.label = label
|
|
|
+ self.reloadData()
|
|
|
+
|
|
|
+ var indexSet = IndexSet()
|
|
|
+ indexSet.insert(self.bookTableView.row(for: self.renameCellView))
|
|
|
+ self.bookTableView.selectRowIndexes(indexSet, byExtendingSelection: false)
|
|
|
+
|
|
|
+ self.undoRedoManager.registerUndo(withTarget: self) { [unowned self] targetType in
|
|
|
+ bookmark.label = label
|
|
|
+ self.renamePDFBook(bookmark: bookmark, label: temp ?? bookmark.label)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func deleteBookMark(bookMarks: [KMBookMark]) {
|
|
|
+ for bookMark in bookMarks {
|
|
|
+ if self.listView.document.removeBookmark(forPageIndex: bookMark.index) {
|
|
|
+ print("删除标签成功")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ self.listView.setNeedsDisplayForVisiblePages()
|
|
|
+ self.reloadData()
|
|
|
+
|
|
|
+ //undo redo
|
|
|
+ var saveBooks:[KMBookMark] = bookMarks
|
|
|
+ self.undoRedoManager.registerUndo(withTarget: self) { [unowned self] targetType in
|
|
|
+ saveBooks.sort(){$0.index > $1.index}
|
|
|
+ self.addBookMark(bookMarks: bookMarks)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func addBookMark(bookMarks: [KMBookMark]) {
|
|
|
+ for bookMark in bookMarks {
|
|
|
+ self.listView.document.addBookmark(bookMark.label, forPageIndex: UInt(bookMark.index))
|
|
|
+ }
|
|
|
+ self.listView.setNeedsDisplayForVisiblePages()
|
|
|
+ self.reloadData()
|
|
|
+
|
|
|
+ if bookMarks.count == 1 {
|
|
|
+ self.addBookMarkAndEdit(newBookMark: self.listView.document.bookmark(forPageIndex: UInt(bookMarks.first!.index)))
|
|
|
+ }
|
|
|
+
|
|
|
+ //undo redo
|
|
|
+ var saveBooks:[KMBookMark] = bookMarks
|
|
|
+ self.undoRedoManager.registerUndo(withTarget: self) { [unowned self] targetType in
|
|
|
+ saveBooks.sort(){$0.index > $1.index}
|
|
|
+ self.deleteBookMark(bookMarks: saveBooks)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func undo(_ sender: Any) {
|
|
|
+ if (self.undoRedoManager.canUndo) {
|
|
|
+ self.undoRedoManager.undo()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func redo(_ sender: Any) {
|
|
|
+ if (self.undoRedoManager.canRedo) {
|
|
|
+ self.undoRedoManager.redo()
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
+extension KMBookMarkViewController : NSMenuDelegate {
|
|
|
+ func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
|
|
|
+ let action = menuItem.action
|
|
|
+ if (action == #selector(undo)) {
|
|
|
+ return self.undoRedoManager.canUndo
|
|
|
+ }
|
|
|
+ if (action == #selector(redo)) {
|
|
|
+ return self.undoRedoManager.canRedo
|
|
|
+ }
|
|
|
+
|
|
|
+ if action == #selector(renameBookAction) ||
|
|
|
+ action == #selector(changeLocationAction) ||
|
|
|
+ action == #selector(deleteBookAction) {
|
|
|
+ if self.bookTableView.selectedRowIndexes.count > 1 {
|
|
|
+ if action == #selector(changeLocationAction) {
|
|
|
+ return false
|
|
|
+ } else if action == #selector(renameBookAction) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if self.bookTableView.clickedRow > -1 {
|
|
|
+ if action == #selector(changeLocationAction) {
|
|
|
+ // if self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) != nil {
|
|
|
+ // return false
|
|
|
+ // }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+struct KMBookMark {
|
|
|
+ var label: String
|
|
|
+ var index: UInt
|
|
|
+}
|