KMBookMarkViewController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. } else {
  288. cell.dateLabel.stringValue = ""
  289. }
  290. if let page = self.listView.document.page(at: UInt(item.bookMark.pageIndex)) {
  291. if let data = page.PDFListViewTIFFData(for: page.bounds) {
  292. let image = NSImage(data: data)
  293. cell.iconIv.image = image
  294. }
  295. }
  296. cell.textFieldDidEndEditingCallback = { [weak self] textF in
  297. self?.renameCellView = cell
  298. self?.renamePDFBook(bookmark: item, label: textF.stringValue)
  299. }
  300. return cell
  301. }
  302. return nil
  303. }
  304. func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
  305. let rowView = KMBookMarkTableRowView()
  306. rowView.selectionHighlightStyle = .none
  307. if row < self.dataSource.count {
  308. rowView.model = self.dataSource[row]
  309. rowView.mouseDownAction = { [unowned self] (view, event) in
  310. self.didSelectItem(row: row, event: event)
  311. }
  312. rowView.rightMouseDownAction = { [unowned self] (view, event) in
  313. if !KMOCToolClass.arrayContains(array: self.selectItems, annotation: rowView.model) ||
  314. self.selectItems.count == 1 {
  315. self.selectIndex(index: row)
  316. }
  317. if row < 0 || row >= self.dataSource.count {
  318. return
  319. }
  320. if self.bookTableView.rowView(atRow: row, makeIfNecessary: false) != nil {
  321. let tempView = self.bookTableView.rowView(atRow: row, makeIfNecessary: false)
  322. self.addMenuTitle(view: tempView!, event: event)
  323. }
  324. }
  325. rowView.hoverCallback = { [unowned self] (mouseEntered, mouseBox) in
  326. self.bookTableView.enumerateAvailableRowViews { view, row in
  327. if view is KMBookMarkTableRowView {
  328. (view as? KMBookMarkTableRowView)?.model.hover = false
  329. (view as? KMBookMarkTableRowView)?.reloadData()
  330. }
  331. }
  332. if mouseEntered {
  333. rowView.model.hover = true
  334. } else {
  335. rowView.model.hover = false
  336. }
  337. }
  338. }
  339. return rowView
  340. }
  341. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  342. self.isLocalEvent = true
  343. return true
  344. }
  345. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  346. self.isLocalEvent = true
  347. return true
  348. }
  349. func tableViewSelectionDidChange(_ notification: Notification) {
  350. // if self.bookTableView.selectedRow == -1 {
  351. // self.cancelSelect()
  352. // }
  353. }
  354. func selectIndex(index: Int) {
  355. if index < 0 || index >= self.dataSource.count {
  356. return
  357. }
  358. self.bookTableView.selectRowIndexes(IndexSet(integer: IndexSet.Element(index)), byExtendingSelection: false)
  359. self.didSelectItem(row: index, event: NSEvent(), needJump: false)
  360. }
  361. func didSelectItem(row: Int, event: NSEvent?, needJump: Bool = true) {
  362. //当选中一个时
  363. if self.bookTableView.selectedRowIndexes.count == 1 || (event != nil &&
  364. (!event!.modifierFlags.contains(NSEvent.ModifierFlags.command) &&
  365. !event!.modifierFlags.contains(NSEvent.ModifierFlags.shift))) {
  366. self.bookTableView.selectRowIndexes(IndexSet(integer: IndexSet.Element(row)), byExtendingSelection: false)
  367. }
  368. //原始数据置空
  369. for model in self.selectItems {
  370. self.dataSource.contains { KMBookMarkItem in
  371. if KMBookMarkItem.bookMark == model.bookMark {
  372. let index = KMOCToolClass.arrayIndexOf(array: self.dataSource, item: KMBookMarkItem) ?? 0
  373. if index != nil {
  374. KMBookMarkItem.select = false
  375. if self.bookTableView.rowView(atRow: index, makeIfNecessary: false) != nil {
  376. let rowView: KMBookMarkTableRowView = self.bookTableView.rowView(atRow: index, makeIfNecessary: false) as! KMBookMarkTableRowView
  377. rowView.reloadData()
  378. }
  379. }
  380. return true
  381. }
  382. return false
  383. }
  384. }
  385. //获取最新数据
  386. var items: [KMBookMarkItem] = []
  387. for index in self.bookTableView.selectedRowIndexes {
  388. if index < self.dataSource.count {
  389. let model: KMBookMarkItem = self.dataSource[index]
  390. model.select = true
  391. if self.bookTableView.rowView(atRow: index, makeIfNecessary: false) != nil {
  392. let rowView: KMBookMarkTableRowView = self.bookTableView.rowView(atRow: index, makeIfNecessary: false) as! KMBookMarkTableRowView
  393. rowView.reloadData()
  394. }
  395. items.append(model)
  396. }
  397. }
  398. self.selectItems = items
  399. //刷新数据
  400. if needJump {
  401. self.updateListViewData()
  402. }
  403. }
  404. func updateListViewData() {
  405. if self.bookTableView.selectedRowIndexes.count == 1 &&
  406. self.bookTableView.selectedRowIndexes.first! < self.dataSource.count {
  407. let index = self.bookTableView.selectedRowIndexes.first
  408. let selectBookMark = self.dataSource[index!]
  409. self.listView.go(toPageIndex: selectBookMark.bookMark.pageIndex, animated: true)
  410. }
  411. }
  412. func cancelSelect() {
  413. self.bookTableView.deselectAll(nil)
  414. for model in self.selectItems {
  415. model.select = false
  416. let index = self.dataSource.firstIndex(of: model)
  417. if index != nil {
  418. if (self.bookTableView.rowView(atRow: index!, makeIfNecessary: false) != nil) {
  419. let rowView: KMBookMarkTableRowView = self.bookTableView.rowView(atRow: index!, makeIfNecessary: false) as! KMBookMarkTableRowView
  420. rowView.reloadData()
  421. }
  422. }
  423. }
  424. }
  425. func updateAddBookMarkState() {
  426. // self.addBookButton.isEnabled = self.canAddBorkMark()
  427. }
  428. func canAddBorkMark() -> Bool {
  429. if self.listView.document.bookmarks() != nil && self.listView.document.bookmarks()?.count != 0 {
  430. for bookMark in self.listView.document.bookmarks() {
  431. if bookMark.pageIndex == self.listView.currentPageIndex {
  432. return false
  433. }
  434. }
  435. }
  436. return true
  437. }
  438. @objc func removeBookmark(_ sender: NSMenuItem) {
  439. let row = self.bookTableView.selectedRow
  440. if let item = self.dataSource.safe_element(for: row) as? KMBookMarkItem {
  441. // self.listView.document.removeBookmark(forPageIndex: UInt(item.bookMark.pageIndex))
  442. self.deleteBookMark(bookMarks: [item])
  443. }
  444. // DispatchQueue.main.async {
  445. // self.reloadData()
  446. // }
  447. }
  448. @objc func removeAllBookmark(_ sender: NSMenuItem) {
  449. // for data in self.dataSource {
  450. // self.listView.document.removeBookmark(forPageIndex: UInt(data.bookMark.pageIndex))
  451. // }
  452. // DispatchQueue.main.async {
  453. // self.reloadData()
  454. // }
  455. self.deleteBookMark(bookMarks: self.dataSource)
  456. }
  457. }
  458. //MARK: undoRedo
  459. extension KMBookMarkViewController {
  460. func changeLocation(oldBookMark: KMBookMarkItem, newBookMark: KMBookMarkItem) {
  461. self.listView.document.removeBookmark(forPageIndex: oldBookMark.index)
  462. self.listView.document.addBookmark(newBookMark.label, forPageIndex: newBookMark.index)
  463. self.reloadData()
  464. self.listView.setNeedsDisplayForVisiblePages()
  465. self.listView.undoManager?.registerUndo(withTarget: self) { [unowned self] targetType in
  466. self.changeLocation(oldBookMark: newBookMark, newBookMark: oldBookMark)
  467. }
  468. }
  469. func renamePDFBook(bookmark : KMBookMarkItem! , label:String) {
  470. if bookmark.bookMark.label == label {
  471. return
  472. }
  473. let temp = bookmark.bookMark.label
  474. bookmark.bookMark.label = label
  475. self.reloadData()
  476. var indexSet = IndexSet()
  477. indexSet.insert(self.bookTableView.row(for: self.renameCellView))
  478. self.bookTableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  479. self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  480. bookmark.bookMark.label = label
  481. self?.renamePDFBook(bookmark: bookmark, label: temp ?? bookmark.label)
  482. }
  483. }
  484. func deleteBookMark(bookMarks: [KMBookMarkItem]) {
  485. for bookMark in bookMarks {
  486. if self.listView.document.removeBookmark(forPageIndex: bookMark.index) {
  487. KMPrint("删除标签成功")
  488. }
  489. }
  490. self.listView.setNeedsDisplayForVisiblePages()
  491. self.reloadData()
  492. //undo redo
  493. var saveBooks:[KMBookMarkItem] = bookMarks
  494. self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  495. saveBooks.sort(){$0.index > $1.index}
  496. self?.addBookMark(bookMarks: bookMarks)
  497. }
  498. guard let callBack = bookMarkDidChange else { return }
  499. callBack(self, bookMarks)
  500. }
  501. func addBookMark(bookMarks: [KMBookMarkItem]) {
  502. for bookMark in bookMarks {
  503. self.listView.document.addBookmark(bookMark.label, forPageIndex: UInt(bookMark.index))
  504. }
  505. self.listView.setNeedsDisplayForVisiblePages()
  506. self.reloadData()
  507. if bookMarks.count == 1 {
  508. DispatchQueue.main.async {
  509. if self.listView.document.bookmark(forPageIndex: UInt(bookMarks.first!.index)) != nil {
  510. let item = KMBookMarkItem()
  511. item.bookMark = self.listView.document.bookmark(forPageIndex: UInt(bookMarks.first!.index))
  512. item.label = item.bookMark.label
  513. item.index = UInt(item.bookMark.pageIndex)
  514. self.addBookMarkAndEdit(newBookMark: item)
  515. }
  516. }
  517. }
  518. //undo redo
  519. var saveBooks:[KMBookMarkItem] = bookMarks
  520. self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  521. saveBooks.sort(){$0.index > $1.index}
  522. self?.deleteBookMark(bookMarks: saveBooks)
  523. }
  524. guard let callBack = bookMarkDidChange else { return }
  525. callBack(self, bookMarks)
  526. }
  527. @IBAction func undo(_ sender: Any) {
  528. if (self.listView.undoManager?.canUndo ?? false) {
  529. self.listView.undoManager?.undo()
  530. }
  531. }
  532. @IBAction func redo(_ sender: Any) {
  533. if (self.listView.undoManager?.canRedo ?? false) {
  534. self.listView.undoManager?.redo()
  535. }
  536. }
  537. }
  538. extension KMBookMarkViewController: NSMenuDelegate, NSMenuItemValidation {
  539. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  540. let action = menuItem.action
  541. if (action == #selector(undo)) {
  542. return self.listView.undoManager?.canUndo ?? false
  543. }
  544. if (action == #selector(redo)) {
  545. return self.listView.undoManager?.canRedo ?? false
  546. }
  547. if action == #selector(renameBookAction) ||
  548. action == #selector(changeLocationAction) ||
  549. action == #selector(deleteBookAction) {
  550. if self.bookTableView.selectedRowIndexes.count > 1 {
  551. if action == #selector(changeLocationAction) {
  552. return false
  553. } else if action == #selector(renameBookAction) {
  554. return false
  555. }
  556. } else if self.bookTableView.selectedRowIndexes.count == 1 {
  557. return true
  558. } else {
  559. if self.bookTableView.selectedRowIndexes.count == 0 {
  560. if action == #selector(changeLocationAction) {
  561. // if self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) != nil {
  562. // return false
  563. // }
  564. }
  565. } else {
  566. return false
  567. }
  568. }
  569. }
  570. if action == #selector(removeBookmark) {
  571. let row = self.bookTableView.selectedRow
  572. return row >= 0 && row < self.dataSource.count
  573. }
  574. if action == #selector(removeAllBookmark) {
  575. return self.dataSource.count > 0
  576. }
  577. return true
  578. }
  579. }
  580. class KMBookMarkItem: NSObject {
  581. var label: String = ""
  582. var index: UInt = 0
  583. var bookMark: CPDFBookmark = CPDFBookmark()
  584. var select: Bool = false
  585. var hover: Bool = false
  586. }