KMBookMarkViewController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. //
  2. // KMBookMarkViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/10/10.
  6. //
  7. import Cocoa
  8. import PDFKit
  9. typealias KMBookMarkViewControllerBookMarkDidChange = (_ controller: KMBookMarkViewController, _ bookMarks: [KMBookMarkItem]) -> Void
  10. class KMBookMarkViewController: NSViewController, NSTextFieldDelegate {
  11. @IBOutlet weak var addBookButton: NSButton!
  12. @IBOutlet weak var titleTextField: NSTextField!
  13. @IBOutlet weak var sortButton: NSButton!
  14. @IBOutlet var topSeplineView: NSView!
  15. @IBOutlet weak var bookTableView: NSTableView!
  16. @IBOutlet weak var emptyView: NSView!
  17. @IBOutlet weak var bigTipLabel: NSTextField!
  18. @IBOutlet weak var tipLabel: NSTextField!
  19. var dataSource: [KMBookMarkItem]!
  20. var renameTextField: NSTextField!
  21. var renamePDFBook: KMBookMarkItem!
  22. var renameCellView: KMBookCellView!
  23. var listView: CPDFListView!
  24. let pdfView = PDFView.init()
  25. var isLocalEvent: Bool = false //区分外部点击还是内部点击
  26. var selectItems: [KMBookMarkItem] = []
  27. var bookMarkDidChange: KMBookMarkViewControllerBookMarkDidChange?
  28. // 升序
  29. private var sortType_: KMSortMode = .ascending
  30. deinit {
  31. NotificationCenter.default.removeObserver(self)
  32. }
  33. override func viewWillAppear() {
  34. super.viewWillAppear()
  35. self.interfaceThemeDidChanged(NSApp.appearance?.name ?? .aqua)
  36. }
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. self.topSeplineView.wantsLayer = true
  40. self.topSeplineView.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.1).cgColor
  41. if #available(macOS 11, *) {
  42. self.bookTableView.style = NSTableView.Style.plain
  43. }
  44. self.bookTableView.allowsMultipleSelection = true
  45. self.bookTableView.doubleAction = #selector(renameBookAction)
  46. // self.bookTableView.selectionHighlightStyle = NSTableView.SelectionHighlightStyle.none;
  47. self.sortButton.target = self
  48. self.sortButton.action = #selector(sortAction)
  49. self.refreshUI()
  50. self.reloadData()
  51. self.initNotification()
  52. }
  53. func refreshUI() {
  54. self.titleTextField.font = NSFont.SFProTextSemiboldFont(14.0)
  55. // self.titleTextField.textColor = NSColor.km_init(hex: "#252629")
  56. self.titleTextField.stringValue = NSLocalizedString("Bookmarks", comment: "")
  57. // self.addBookButton.toolTip = NSLocalizedString("Add Bookmark", comment: "")
  58. self.addBookButton.image = NSImage(named: "KMImageNameBotaBookmarkMoreIcon")
  59. self.sortButton.image = NSImage(named: "KMImageNameBotaBookmarkSortIcon")
  60. self.bigTipLabel.font = NSFont.SFProTextRegularFont(14.0)
  61. self.bigTipLabel.textColor = NSColor.km_init(hex: "#616469")
  62. self.bigTipLabel.stringValue = NSLocalizedString("No Bookmarks", comment: "")
  63. let title = NSLocalizedString("To create a bookmark, please right-click on the selected page and choose \"Add Bookmark\", or click \"Add\" button in the upper right corner.", comment: "")
  64. let paragraphStyle = NSMutableParagraphStyle()
  65. paragraphStyle.lineHeightMultiple = 1.32
  66. paragraphStyle.alignment = .center
  67. self.tipLabel.attributedStringValue = NSMutableAttributedString(string: title, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle, .foregroundColor : NSColor.km_init(hex: "#94989C")])
  68. self.tipLabel.isHidden = true
  69. }
  70. func reloadData() {
  71. let array = self.listView.document.bookmarks() ?? [CPDFBookmark]()
  72. var bookMarks: [KMBookMarkItem] = []
  73. for bookMark in array {
  74. let item = KMBookMarkItem()
  75. item.bookMark = bookMark
  76. item.index = UInt(bookMark.pageIndex)
  77. item.label = bookMark.label
  78. bookMarks.append(item)
  79. }
  80. self.dataSource = bookMarks
  81. if self.sortType_ == .descending {
  82. self.dataSource.sort(){$0.bookMark.pageIndex >= $1.bookMark.pageIndex}
  83. } else {
  84. self.dataSource.sort(){$0.bookMark.pageIndex < $1.bookMark.pageIndex}
  85. }
  86. self.bookTableView.reloadData()
  87. self.updateAddBookMarkState()
  88. }
  89. func addBookMarkAndEdit(newBookMark: KMBookMarkItem) {
  90. _ = self.dataSource.contains { KMBookMarkItem in
  91. if KMBookMarkItem.bookMark == newBookMark.bookMark {
  92. let index = KMOCToolClass.arrayIndexOf(array: self.dataSource, item: KMBookMarkItem) ?? 0
  93. self.didSelectItem(row: index, event: NSEvent())
  94. self.renameBookWithRow(row: index)
  95. return true
  96. }
  97. return false
  98. }
  99. }
  100. func initNotification() {
  101. NotificationCenter.default.addObserver(self, selector: #selector(KMPDFViewCurrentPageDidChangedNotification), name: NSNotification.Name.init(rawValue: "KMPDFViewCurrentPageDidChanged"), object: nil)
  102. NotificationCenter.default.addObserver(self, selector: #selector(documentPageCountChangedNotification), name: NSNotification.Name.init(rawValue: "CPDFDocumentPageCountChangedNotification"), object: nil)
  103. }
  104. func removeNotification() {
  105. NotificationCenter.default.removeObserver(self)
  106. }
  107. private func addMenuTitle(view: NSView, event: NSEvent) {
  108. let menus : NSMenu = NSMenu(title: "")
  109. menus.delegate = self
  110. let addItem = self.menuItemWithTitle(title: NSLocalizedString("Rename", comment: ""), action: #selector(renameBookAction))
  111. let addChildItem = self.menuItemWithTitle(title: NSLocalizedString("Change Destination", comment: ""), action: #selector(changeLocationAction))
  112. let addHigherItem = self.menuItemWithTitle(title: NSLocalizedString("Delete", comment: ""), action: #selector(deleteBookAction))
  113. menus.addItem(addItem)
  114. menus.addItem(addChildItem)
  115. menus.addItem(addHigherItem)
  116. let point = view.convert(event.locationInWindow, from: nil)
  117. menus.popUp(positioning: nil, at: point, in: view)
  118. // self.bookTableView.menu = menus
  119. }
  120. func menuItemWithTitle(title:String, action:Selector?) -> NSMenuItem {
  121. let menuItem = NSMenuItem.init(title: title as String, action: action, keyEquivalent: "")
  122. menuItem.target = self
  123. return menuItem
  124. }
  125. //MARK: Menu Action
  126. @objc func renameBookAction() {
  127. if self.bookTableView.selectedRowIndexes.count == 1 {
  128. self.renameBookWithRow(row: self.bookTableView.selectedRowIndexes.first!)
  129. } else {
  130. __NSBeep()
  131. }
  132. }
  133. @IBAction func escButtonAction(_ sender: Any) {
  134. self.bookTableView.deselectAll(nil)
  135. }
  136. @IBAction func addBookmarkAction(_ sender: Any) {
  137. // if self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) == nil {
  138. // let label = "\(NSLocalizedString("Page", comment:"")) \(self.listView.currentPageIndex + 1)"
  139. // let bookMark = KMBookMarkItem()
  140. // bookMark.label = label
  141. // bookMark.index = UInt(self.listView.currentPageIndex)
  142. // self.addBookMark(bookMarks: [bookMark])
  143. // } else {
  144. // let bookMark = self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex))
  145. // self.dataSource.contains { KMBookMarkItem in
  146. // if KMBookMarkItem.bookMark == bookMark {
  147. // let index = Int(KMBookMarkItem.index)
  148. // self.bookTableView.selectRowIndexes(IndexSet(integer: index), byExtendingSelection: false)
  149. // self.didSelectItem(row: index, event: NSEvent())
  150. // return true
  151. // }
  152. // return false
  153. // }
  154. // }
  155. let menu = NSMenu()
  156. let removeItem = NSMenuItem(title: NSLocalizedString("Remove Bookmark", comment: ""), action: #selector(removeBookmark), keyEquivalent: "")
  157. removeItem.target = self
  158. menu.addItem(removeItem)
  159. let removeAllItem = NSMenuItem(title: NSLocalizedString("Remove All Bookmarks", comment: ""), action: #selector(removeAllBookmark), keyEquivalent: "")
  160. menu.addItem(removeAllItem)
  161. removeAllItem.target = self
  162. menu.popUp(positioning: removeItem, at: .init(x: 0, y: 24), in: sender as? NSView)
  163. }
  164. @objc func changeLocationAction() {
  165. if self.bookTableView.selectedRowIndexes.count == 1 {
  166. let item = self.dataSource[self.bookTableView.selectedRowIndexes.first!]
  167. let alter = NSAlert()
  168. alter.alertStyle = NSAlert.Style.informational
  169. alter.messageText = NSLocalizedString("Are you sure you want to set the selected page as the bookmark location?", comment: "")
  170. alter.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  171. alter.addButton(withTitle: NSLocalizedString("No", comment:""))
  172. let modlres = alter.runModal()
  173. if modlres == NSApplication.ModalResponse.alertFirstButtonReturn {
  174. let bookMark = KMBookMarkItem()
  175. bookMark.bookMark = item.bookMark
  176. bookMark.label = item.label
  177. bookMark.index = UInt(self.listView.currentPageIndex)
  178. self.changeLocation(oldBookMark: item,
  179. newBookMark: bookMark)
  180. }
  181. } else {
  182. __NSBeep()
  183. }
  184. }
  185. @objc func deleteBookAction() {
  186. if self.bookTableView.selectedRowIndexes.count != 0 {
  187. var bookMarks:[KMBookMarkItem] = []
  188. for index in self.bookTableView.selectedRowIndexes {
  189. let item = self.dataSource[index]
  190. bookMarks.append(item)
  191. }
  192. self.deleteBookMark(bookMarks: bookMarks)
  193. } else {
  194. __NSBeep()
  195. }
  196. }
  197. private func renameBookWithRow(row: Int) {
  198. self.renamePDFBook = self.dataSource[row]
  199. self.renameCellView = self.bookTableView.view(atColumn: 0, row: row, makeIfNecessary: true) as! KMBookCellView
  200. self.renameTextField = self.renameCellView.inputTF
  201. // self.renameTextField.delegate = self
  202. self.renameTextField.isEditable = true
  203. self.renameTextField.becomeFirstResponder()
  204. }
  205. @objc func sortAction(_ sender: NSButton) {
  206. let type = self.sortType_
  207. if type == .ascending {
  208. self.sortType_ = .descending
  209. } else {
  210. self.sortType_ = .ascending
  211. }
  212. self.reloadData()
  213. }
  214. //MARK: Noti
  215. @objc func KMPDFViewCurrentPageDidChangedNotification(notification: NSNotification) {
  216. if notification.object is CPDFDocument {
  217. let pdfdocument : CPDFDocument = notification.object as! CPDFDocument
  218. if pdfdocument.isEqual(self.listView.document) {
  219. if !isLocalEvent {
  220. var containSelIndex:Bool = false
  221. for (index, value) in self.dataSource.enumerated() {
  222. if value.bookMark == self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) {
  223. containSelIndex = true
  224. self.didSelectItem(row: index, event: NSEvent())
  225. break
  226. }
  227. }
  228. if !containSelIndex {
  229. self.cancelSelect()
  230. }
  231. }
  232. isLocalEvent = false
  233. }
  234. self.updateAddBookMarkState()
  235. }
  236. }
  237. @objc func documentPageCountChangedNotification(notification: NSNotification) {
  238. if notification.object is CPDFDocument {
  239. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) { [weak self] in
  240. let pdfdocument : CPDFDocument = notification.object as! CPDFDocument
  241. if pdfdocument.isEqual(self?.listView?.document) {
  242. self?.reloadData()
  243. }
  244. }
  245. }
  246. }
  247. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  248. super.interfaceThemeDidChanged(appearance)
  249. let isDark = appearance == .darkAqua
  250. self.view.wantsLayer = true
  251. if isDark {
  252. self.view.layer?.backgroundColor = .clear
  253. self.bookTableView.backgroundColor = .clear
  254. } else {
  255. self.view.layer?.backgroundColor = NSColor(red: 247.0/255.0, green: 248.0/255.0, blue: 250.0/255.0, alpha: 1).cgColor
  256. self.bookTableView.backgroundColor = NSColor.km_init(hex: "#F7F8FA")
  257. }
  258. }
  259. //MARK: - NSTextFieldDelegate
  260. func controlTextDidEndEditing(_ obj: Notification) {
  261. if (self.renameTextField.isEqual(obj.object)) {
  262. let textField : NSTextField = obj.object as! NSTextField
  263. self.renamePDFBook(bookmark: self.renamePDFBook, label: textField.stringValue)
  264. // self.renameTextField.isEditable = false
  265. }
  266. }
  267. }
  268. extension KMBookMarkViewController : NSTableViewDelegate,NSTableViewDataSource {
  269. func numberOfRows(in tableView: NSTableView) -> Int {
  270. let count = self.dataSource?.count ?? 0
  271. if count == 0 {
  272. self.emptyView.isHidden = false
  273. } else {
  274. self.emptyView.isHidden = true
  275. }
  276. return count
  277. }
  278. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  279. if row < self.dataSource.count {
  280. let item: KMBookMarkItem = self.dataSource[row]
  281. let cell : KMBookCellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBookCellView"), owner: self) as! KMBookCellView
  282. cell.inputTF.stringValue = item.bookMark.label
  283. cell.bookTitle.stringValue = "\(item.bookMark.pageIndex + 1)"
  284. cell.inputBox.borderWidth = 0
  285. if let data = item.bookMark.date {
  286. cell.dateLabel.stringValue = KMTools.timeString(timeDate: data)
  287. }
  288. if let page = self.listView.document.page(at: UInt(item.bookMark.pageIndex)) {
  289. if let data = page.PDFListViewTIFFData(for: page.bounds) {
  290. let image = NSImage(data: data)
  291. cell.iconIv.image = image
  292. }
  293. }
  294. cell.textFieldDidEndEditingCallback = { [weak self] textF in
  295. self?.renameCellView = cell
  296. self?.renamePDFBook(bookmark: item, label: textF.stringValue)
  297. }
  298. return cell
  299. }
  300. return nil
  301. }
  302. func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
  303. let rowView = KMBookMarkTableRowView()
  304. rowView.selectionHighlightStyle = .none
  305. if row < self.dataSource.count {
  306. rowView.model = self.dataSource[row]
  307. rowView.mouseDownAction = { [unowned self] (view, event) in
  308. self.didSelectItem(row: row, event: event)
  309. }
  310. rowView.rightMouseDownAction = { [unowned self] (view, event) in
  311. if !KMOCToolClass.arrayContains(array: self.selectItems, annotation: rowView.model) ||
  312. self.selectItems.count == 1 {
  313. self.selectIndex(index: row)
  314. }
  315. if row < 0 || row >= self.dataSource.count {
  316. return
  317. }
  318. if self.bookTableView.rowView(atRow: row, makeIfNecessary: false) != nil {
  319. let tempView = self.bookTableView.rowView(atRow: row, makeIfNecessary: false)
  320. self.addMenuTitle(view: tempView!, event: event)
  321. }
  322. }
  323. rowView.hoverCallback = { [unowned self] (mouseEntered, mouseBox) in
  324. self.bookTableView.enumerateAvailableRowViews { view, row in
  325. if view is KMBookMarkTableRowView {
  326. (view as? KMBookMarkTableRowView)?.model.hover = false
  327. (view as? KMBookMarkTableRowView)?.reloadData()
  328. }
  329. }
  330. if mouseEntered {
  331. rowView.model.hover = true
  332. } else {
  333. rowView.model.hover = false
  334. }
  335. }
  336. }
  337. return rowView
  338. }
  339. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  340. self.isLocalEvent = true
  341. return true
  342. }
  343. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  344. self.isLocalEvent = true
  345. return true
  346. }
  347. func tableViewSelectionDidChange(_ notification: Notification) {
  348. // if self.bookTableView.selectedRow == -1 {
  349. // self.cancelSelect()
  350. // }
  351. }
  352. func selectIndex(index: Int) {
  353. if index < 0 || index >= self.dataSource.count {
  354. return
  355. }
  356. self.bookTableView.selectRowIndexes(IndexSet(integer: IndexSet.Element(index)), byExtendingSelection: false)
  357. self.didSelectItem(row: index, event: NSEvent(), needJump: false)
  358. }
  359. func didSelectItem(row: Int, event: NSEvent?, needJump: Bool = true) {
  360. //当选中一个时
  361. if self.bookTableView.selectedRowIndexes.count == 1 || (event != nil &&
  362. (!event!.modifierFlags.contains(NSEvent.ModifierFlags.command) &&
  363. !event!.modifierFlags.contains(NSEvent.ModifierFlags.shift))) {
  364. self.bookTableView.selectRowIndexes(IndexSet(integer: IndexSet.Element(row)), byExtendingSelection: false)
  365. }
  366. //原始数据置空
  367. for model in self.selectItems {
  368. self.dataSource.contains { KMBookMarkItem in
  369. if KMBookMarkItem.bookMark == model.bookMark {
  370. let index = KMOCToolClass.arrayIndexOf(array: self.dataSource, item: KMBookMarkItem) ?? 0
  371. if index != nil {
  372. KMBookMarkItem.select = false
  373. if self.bookTableView.rowView(atRow: index, makeIfNecessary: false) != nil {
  374. let rowView: KMBookMarkTableRowView = self.bookTableView.rowView(atRow: index, makeIfNecessary: false) as! KMBookMarkTableRowView
  375. rowView.reloadData()
  376. }
  377. }
  378. return true
  379. }
  380. return false
  381. }
  382. }
  383. //获取最新数据
  384. var items: [KMBookMarkItem] = []
  385. for index in self.bookTableView.selectedRowIndexes {
  386. if index < self.dataSource.count {
  387. let model: KMBookMarkItem = self.dataSource[index]
  388. model.select = true
  389. if self.bookTableView.rowView(atRow: index, makeIfNecessary: false) != nil {
  390. let rowView: KMBookMarkTableRowView = self.bookTableView.rowView(atRow: index, makeIfNecessary: false) as! KMBookMarkTableRowView
  391. rowView.reloadData()
  392. }
  393. items.append(model)
  394. }
  395. }
  396. self.selectItems = items
  397. //刷新数据
  398. if needJump {
  399. self.updateListViewData()
  400. }
  401. }
  402. func updateListViewData() {
  403. if self.bookTableView.selectedRowIndexes.count == 1 &&
  404. self.bookTableView.selectedRowIndexes.first! < self.dataSource.count {
  405. let index = self.bookTableView.selectedRowIndexes.first
  406. let selectBookMark = self.dataSource[index!]
  407. self.listView.go(toPageIndex: selectBookMark.bookMark.pageIndex, animated: true)
  408. }
  409. }
  410. func cancelSelect() {
  411. self.bookTableView.deselectAll(nil)
  412. for model in self.selectItems {
  413. model.select = false
  414. let index = self.dataSource.firstIndex(of: model)
  415. if index != nil {
  416. if (self.bookTableView.rowView(atRow: index!, makeIfNecessary: false) != nil) {
  417. let rowView: KMBookMarkTableRowView = self.bookTableView.rowView(atRow: index!, makeIfNecessary: false) as! KMBookMarkTableRowView
  418. rowView.reloadData()
  419. }
  420. }
  421. }
  422. }
  423. func updateAddBookMarkState() {
  424. // self.addBookButton.isEnabled = self.canAddBorkMark()
  425. }
  426. func canAddBorkMark() -> Bool {
  427. if self.listView.document.bookmarks() != nil && self.listView.document.bookmarks()?.count != 0 {
  428. for bookMark in self.listView.document.bookmarks() {
  429. if bookMark.pageIndex == self.listView.currentPageIndex {
  430. return false
  431. }
  432. }
  433. }
  434. return true
  435. }
  436. @objc func removeBookmark(_ sender: NSMenuItem) {
  437. let row = self.bookTableView.selectedRow
  438. if let item = self.dataSource.safe_element(for: row) as? KMBookMarkItem {
  439. // self.listView.document.removeBookmark(forPageIndex: UInt(item.bookMark.pageIndex))
  440. self.deleteBookMark(bookMarks: [item])
  441. }
  442. // DispatchQueue.main.async {
  443. // self.reloadData()
  444. // }
  445. }
  446. @objc func removeAllBookmark(_ sender: NSMenuItem) {
  447. // for data in self.dataSource {
  448. // self.listView.document.removeBookmark(forPageIndex: UInt(data.bookMark.pageIndex))
  449. // }
  450. // DispatchQueue.main.async {
  451. // self.reloadData()
  452. // }
  453. self.deleteBookMark(bookMarks: self.dataSource)
  454. }
  455. }
  456. //MARK: undoRedo
  457. extension KMBookMarkViewController {
  458. func changeLocation(oldBookMark: KMBookMarkItem, newBookMark: KMBookMarkItem) {
  459. self.listView.document.removeBookmark(forPageIndex: oldBookMark.index)
  460. self.listView.document.addBookmark(newBookMark.label, forPageIndex: newBookMark.index)
  461. self.reloadData()
  462. self.listView.setNeedsDisplayForVisiblePages()
  463. self.listView.undoManager?.registerUndo(withTarget: self) { [unowned self] targetType in
  464. self.changeLocation(oldBookMark: newBookMark, newBookMark: oldBookMark)
  465. }
  466. }
  467. func renamePDFBook(bookmark : KMBookMarkItem! , label:String) {
  468. if bookmark.bookMark.label == label {
  469. return
  470. }
  471. let temp = bookmark.bookMark.label
  472. bookmark.bookMark.label = label
  473. self.reloadData()
  474. var indexSet = IndexSet()
  475. indexSet.insert(self.bookTableView.row(for: self.renameCellView))
  476. self.bookTableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  477. self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  478. bookmark.bookMark.label = label
  479. self?.renamePDFBook(bookmark: bookmark, label: temp ?? bookmark.label)
  480. }
  481. }
  482. func deleteBookMark(bookMarks: [KMBookMarkItem]) {
  483. for bookMark in bookMarks {
  484. if self.listView.document.removeBookmark(forPageIndex: bookMark.index) {
  485. KMPrint("删除标签成功")
  486. }
  487. }
  488. self.listView.setNeedsDisplayForVisiblePages()
  489. self.reloadData()
  490. //undo redo
  491. var saveBooks:[KMBookMarkItem] = bookMarks
  492. self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  493. saveBooks.sort(){$0.index > $1.index}
  494. self?.addBookMark(bookMarks: bookMarks)
  495. }
  496. guard let callBack = bookMarkDidChange else { return }
  497. callBack(self, bookMarks)
  498. }
  499. func addBookMark(bookMarks: [KMBookMarkItem]) {
  500. for bookMark in bookMarks {
  501. self.listView.document.addBookmark(bookMark.label, forPageIndex: UInt(bookMark.index))
  502. }
  503. self.listView.setNeedsDisplayForVisiblePages()
  504. self.reloadData()
  505. if bookMarks.count == 1 {
  506. DispatchQueue.main.async {
  507. if self.listView.document.bookmark(forPageIndex: UInt(bookMarks.first!.index)) != nil {
  508. let item = KMBookMarkItem()
  509. item.bookMark = self.listView.document.bookmark(forPageIndex: UInt(bookMarks.first!.index))
  510. item.label = item.bookMark.label
  511. item.index = UInt(item.bookMark.pageIndex)
  512. self.addBookMarkAndEdit(newBookMark: item)
  513. }
  514. }
  515. }
  516. //undo redo
  517. var saveBooks:[KMBookMarkItem] = bookMarks
  518. self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  519. saveBooks.sort(){$0.index > $1.index}
  520. self?.deleteBookMark(bookMarks: saveBooks)
  521. }
  522. guard let callBack = bookMarkDidChange else { return }
  523. callBack(self, bookMarks)
  524. }
  525. @IBAction func undo(_ sender: Any) {
  526. if (self.listView.undoManager?.canUndo ?? false) {
  527. self.listView.undoManager?.undo()
  528. }
  529. }
  530. @IBAction func redo(_ sender: Any) {
  531. if (self.listView.undoManager?.canRedo ?? false) {
  532. self.listView.undoManager?.redo()
  533. }
  534. }
  535. }
  536. extension KMBookMarkViewController: NSMenuDelegate, NSMenuItemValidation {
  537. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  538. let action = menuItem.action
  539. if (action == #selector(undo)) {
  540. return self.listView.undoManager?.canUndo ?? false
  541. }
  542. if (action == #selector(redo)) {
  543. return self.listView.undoManager?.canRedo ?? false
  544. }
  545. if action == #selector(renameBookAction) ||
  546. action == #selector(changeLocationAction) ||
  547. action == #selector(deleteBookAction) {
  548. if self.bookTableView.selectedRowIndexes.count > 1 {
  549. if action == #selector(changeLocationAction) {
  550. return false
  551. } else if action == #selector(renameBookAction) {
  552. return false
  553. }
  554. } else if self.bookTableView.selectedRowIndexes.count == 1 {
  555. return true
  556. } else {
  557. if self.bookTableView.selectedRowIndexes.count == 0 {
  558. if action == #selector(changeLocationAction) {
  559. // if self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) != nil {
  560. // return false
  561. // }
  562. }
  563. } else {
  564. return false
  565. }
  566. }
  567. }
  568. if action == #selector(removeBookmark) {
  569. let row = self.bookTableView.selectedRow
  570. return row >= 0 && row < self.dataSource.count
  571. }
  572. return true
  573. }
  574. }
  575. class KMBookMarkItem: NSObject {
  576. var label: String = ""
  577. var index: UInt = 0
  578. var bookMark: CPDFBookmark = CPDFBookmark()
  579. var select: Bool = false
  580. var hover: Bool = false
  581. }