KMLeftSideViewController+Note.swift 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. //
  2. // KMLeftSideViewController+Note.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/23.
  6. //
  7. import Foundation
  8. extension KMLeftSideViewController.Key {
  9. static let noteAscendSortKey = "KMLeftSideViewAscendSortBoolKey"
  10. static let noteSortTypeKey = "KMLeftSideViewNoteSortTypeKey"
  11. static let noteTableColumn = "KMNoteOutlineViewTableColumnKey"
  12. static let noteFilterPage = "kKMNoteFilterAnnotationPageKey"
  13. static let noteFilterTime = "kKMNoteFilterAnnotationTimeKey"
  14. static let noteFilterAuther = "kKMNoteFilterAnnotationAutherKey"
  15. }
  16. // MARK: - Action
  17. extension KMLeftSideViewController {
  18. func note_initSubViews() {
  19. self.noteSearchField.backgroundColor = KMAppearance.Layout.l_1Color()
  20. self.noteSearchField.wantsLayer = true
  21. self.noteSearchField.layer?.backgroundColor = KMAppearance.Layout.l_1Color().cgColor
  22. self.noteSearchField.layer?.borderWidth = 1.0
  23. self.noteMoreButton.target = self
  24. self.noteMoreButton.tag = 304
  25. self.noteMoreButton.action = #selector(leftSideViewMoreButtonAction)
  26. self.moreButtonLayer = KMButtonLayer()
  27. self.noteMoreButton.layer?.addSublayer(self.moreButtonLayer!)
  28. self.moreButtonLayer?.frame = NSMakeRect(0, 0, NSWidth(self.noteMoreButton.bounds), NSHeight(self.noteMoreButton.bounds))
  29. self.noteFilterButton.target = self
  30. self.noteFilterButton.action = #selector(noteFilterAction)
  31. self.filterButtonLayer = NSView()
  32. self.noteFilterButton.addSubview(self.filterButtonLayer!)
  33. self.filterButtonLayer?.frame = NSMakeRect(14, 2, 8, 8)
  34. self.noteDoneButton.action = #selector(leftSideViewDoneButtonAction)
  35. self.noteDoneButton.target = self
  36. self.noteDoneButton.tag = 311
  37. self.noteDoneButton.isHidden = true
  38. self.noteSearchField.delegate = self
  39. self.noteSearchField.isHidden = true
  40. self.noteSearchField.endEditCallBack = { [weak self] isEndEdit in
  41. // if (isEndEdit) {
  42. // self.noteSearchField.isHidden = true
  43. // self.noteSearchButton.isHidden = false
  44. // self.noteTitleLabel.isHidden = false
  45. // }
  46. }
  47. self.sortTypeBox.downCallback = { [unowned self] downEntered, mouseBox, _ in
  48. if (downEntered) {
  49. let menu = NSMenu()
  50. let timeItem = menu.addItem(title: KMLocalizedString("Time", nil), action: #selector(sortTypeAction), target: self)
  51. timeItem?.representedObject = self
  52. timeItem?.tag = 0
  53. let pageItem = menu.addItem(title: KMLocalizedString("Page", nil), action: #selector(sortTypeAction), target: self)
  54. pageItem?.representedObject = self
  55. timeItem?.tag = 1
  56. if (self.noteSortType == .time) {
  57. timeItem?.state = .on
  58. pageItem?.state = .off
  59. } else if (self.noteSortType == .page) {
  60. timeItem?.state = .off
  61. pageItem?.state = .on
  62. }
  63. menu.popUp(positioning: nil, at: NSMakePoint(-10, 0), in: self.sortTypeBox)
  64. }
  65. }
  66. self.noteOutlineView.delegate = self
  67. self.noteOutlineView.dataSource = self
  68. self.noteOutlineView.botaDelegate = self
  69. self.noteOutlineView.botaDataSource = self
  70. self.noteOutlineView.noteDelegate = self
  71. self.noteOutlineView.menu = NSMenu()
  72. self.noteOutlineView.menu?.delegate = self
  73. self.noteOutlineView.typeSelectHelper = SKTypeSelectHelper(matchOption: .SKSubstringMatch)
  74. self.noteOutlineView.registerForDraggedTypes(NSColor.readableTypes(for: NSPasteboard(name: .drag)))
  75. self.noteOutlineView.target = self
  76. self.noteOutlineView.doubleAction = #selector(selectSelectedNote)
  77. }
  78. func note_initDefalutValue() {
  79. self.noteView.wantsLayer = true
  80. self.noteView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  81. let sud = UserDefaults.standard
  82. if let dict = sud.dictionary(forKey: Self.Key.noteTableColumn) {
  83. self.noteTypeDict = dict
  84. } else {
  85. self.noteTypeDict = [Self.Key.noteFilterPage : false,
  86. Self.Key.noteFilterTime : false,
  87. Self.Key.noteFilterAuther : false]
  88. sud.sync_setValue(self.noteTypeDict, forKey: Self.Key.noteTableColumn)
  89. }
  90. self.caseInsensitiveNoteSearch = sud.bool(forKey: SKCaseInsensitiveNoteSearchKey)
  91. self.isAscendSort = KMDataManager.ud_bool(forKey: Self.Key.noteAscendSortKey)
  92. self.noteTitleLabel.stringValue = KMLocalizedString("Notes", nil);
  93. self.noteTitleLabel.textColor = KMAppearance.Layout.h0Color()
  94. self.noteSearchField.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  95. self.noteMoreButton.wantsLayer = true
  96. self.moreButtonLayer?.layerType = .none
  97. self.moreButtonLayer?.isHidden = true
  98. self.noteFilterButton.toolTip = KMLocalizedString("Sort", nil)
  99. self.noteFilterButton.wantsLayer = true
  100. self.filterButtonLayer?.isHidden = true
  101. self.filterButtonLayer?.wantsLayer = true
  102. self.filterButtonLayer?.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  103. self.filterButtonLayer?.layer?.cornerRadius = 4.0
  104. if (self.isAscendSort) {
  105. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  106. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  107. } else {
  108. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  109. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  110. }
  111. self.noteSearchButton.toolTip = KMLocalizedString("Search", nil)
  112. self.noteDoneButton.title = KMLocalizedString("Done", nil)
  113. self.noteDoneButton.toolTip = KMLocalizedString("Done", nil)
  114. self.noteDoneButton.setTitleColor(KMAppearance.Layout.w0Color())
  115. self.noteDoneButton.wantsLayer = true
  116. self.noteDoneButton.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  117. self.noteDoneButton.layer?.cornerRadius = 4.0
  118. self.noteHeaderView.wantsLayer = true
  119. self.noteHeaderView.layer?.backgroundColor = KMAppearance.Else.textTagColor().cgColor
  120. self.noteHeaderView.layer?.cornerRadius = 1.0
  121. let sortType = KMDataManager.ud_integer(forKey: Self.Key.noteSortTypeKey)
  122. if (sortType == 1) {
  123. self.noteSortType = KMNoteSortType(rawValue: sortType) ?? .none
  124. if (self.noteSortType == .time) {
  125. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  126. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  127. } else if (self.noteSortType == .page) {
  128. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  129. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  130. }
  131. } else {
  132. self.noteSortType = .time
  133. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  134. }
  135. self.sortTypeLabel.textColor = KMAppearance.Layout.h1Color()
  136. self.noteOutlineView.backgroundColor = KMAppearance.Layout.l0Color()
  137. self.noteOutlineView.autoresizesOutlineColumn = false
  138. self.noteOutlineView.indentationPerLevel = 0
  139. }
  140. func annoListIsShowPage() -> Bool {
  141. return !(self.noteTypeDict[Self.Key.noteFilterPage] as? Bool ?? false)
  142. }
  143. func annoListIsShowTime() -> Bool {
  144. return !(self.noteTypeDict[Self.Key.noteFilterTime] as? Bool ?? false)
  145. }
  146. func annoListIsShowAnther() -> Bool {
  147. return !(self.noteTypeDict[Self.Key.noteFilterAuther] as? Bool ?? false)
  148. }
  149. }
  150. // MARK: - Menu
  151. extension KMLeftSideViewController {
  152. func annoListMenu(_ menu: NSMenu) {
  153. var item: NSMenuItem?
  154. var items: NSArray?
  155. var rowIndexes = self.noteOutlineView.selectedRowIndexes
  156. let row = self.noteOutlineView.clickedRow
  157. if row == -1 {
  158. _ = self._addExportPDFMenu(menu)
  159. _ = self._addDeleteAllAnnoItem(menu)
  160. return
  161. }
  162. if rowIndexes.contains(row) == false {
  163. rowIndexes = IndexSet(integer: row)
  164. }
  165. items = self.noteOutlineView.itemsAtRowIndexes(rowIndexes) as NSArray
  166. guard let model = self.fetchAnnoModel(for: row) else {
  167. return
  168. }
  169. let isFold = model.isFold()
  170. item = menu.addItem(title: KMLocalizedString("Expand", nil), action: #selector(unfoldNoteAction), target: self)
  171. item?.state = isFold ? .off : .on
  172. item?.representedObject = items
  173. item = menu.addItem(title: KMLocalizedString("Collapse", nil), action: #selector(foldNoteAction), target: self)
  174. item?.state = isFold ? .on : .off
  175. item?.representedObject = items
  176. menu.addItem(.separator())
  177. let hideNotes = self.hideNotes()
  178. if hideNotes == false && (items?.count ?? 0) == 1 {
  179. let annotation = self.noteItems(items!).lastObject as? CPDFAnnotation
  180. if let data = annotation?.isEditable(), data {
  181. if annotation?.type == nil {
  182. let isNote = annotation?.isNote() ?? false
  183. if isNote {
  184. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  185. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteTextFromTable), target: self)
  186. item?.representedObject = annotation
  187. }
  188. } else if let data = self.noteOutlineView.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier("note"))?.isHidden, data {
  189. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  190. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  191. item?.representedObject = annotation
  192. } else {
  193. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteFromTable), target: self)
  194. item?.representedObject = annotation
  195. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  196. item?.representedObject = annotation
  197. item?.keyEquivalentModifierMask = [.option]
  198. item?.isAlternate = true
  199. }
  200. }
  201. }
  202. if menu.numberOfItems > 0 {
  203. _ = self._addExportPDFMenu(menu)
  204. menu.addItem(.separator())
  205. if self.outlineView(self.noteOutlineView, canDeleteItems: items as? [Any] ?? []) {
  206. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteNotes), target: self)
  207. item?.representedObject = items
  208. }
  209. _ = self._addDeleteAllAnnoItem(menu)
  210. }
  211. }
  212. private func _addExportPDFMenu(_ menu: NSMenu) -> NSMenu {
  213. var item = menu.addItem(title: NSLocalizedString("Export Annotations…", tableName: "", comment: ""), action: nil, target: self)
  214. let subMenu = NSMenu()
  215. item?.submenu = subMenu
  216. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  217. item?.tag = 0
  218. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  219. item?.tag = 1
  220. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  221. item?.tag = 2
  222. item = subMenu.addItem(title: NSLocalizedString("Notes as Text", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  223. item?.tag = 3
  224. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  225. item?.tag = 4
  226. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  227. item?.tag = 5
  228. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  229. item?.tag = 6
  230. return menu
  231. }
  232. private func _addDeleteAllAnnoItem(_ menu: NSMenu) -> NSMenuItem? {
  233. return menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "", comment: ""), action: #selector(removeAllAnnotations), target: self)
  234. }
  235. func annoListMoreMenu(_ view: NSButton) {
  236. let menu = NSMenu()
  237. let object = KMPopupMenuObject()
  238. object.menuTag = 1001
  239. menu.delegate = object
  240. object.enterControllerCallback = { [weak self] isEnter in
  241. if (isEnter) {
  242. self?.moreButtonLayer?.isHidden = false
  243. } else {
  244. self?.moreButtonLayer?.isHidden = true
  245. }
  246. }
  247. let expandAllItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(note_expandAllComments), target: self)
  248. expandAllItem?.representedObject = self.noteOutlineView
  249. let foldAllItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(note_foldAllComments), target: self)
  250. foldAllItem?.representedObject = self.noteOutlineView
  251. let type = self.annoListModel?.foldType ?? .none
  252. expandAllItem?.state = type == .unfold ? .on : .off
  253. foldAllItem?.state = type == .fold ? .on : .off
  254. let showItem = menu.addItem(title: KMLocalizedString("Show Note", nil), action: nil, target: self)
  255. let subMenu = NSMenu()
  256. let pageItem = subMenu.addItem(title: KMLocalizedString("Page", nil), action: #selector(noteShowNoteAction), target: self)
  257. pageItem?.state = self.annoListIsShowPage() ? .on : .off
  258. pageItem?.representedObject = self.noteOutlineView
  259. pageItem?.tag = 101
  260. let timeItem = subMenu.addItem(title: KMLocalizedString("Time", nil) , action: #selector(noteShowNoteAction), target: self)
  261. timeItem?.state = self.annoListIsShowTime() ? .on : .off
  262. timeItem?.representedObject = self.noteOutlineView
  263. timeItem?.tag = 102
  264. let authorItem = subMenu.addItem(title: KMLocalizedString("Author", nil) , action: #selector(noteShowNoteAction), target: self)
  265. authorItem?.state = self.annoListIsShowAnther() ? .on : .off
  266. authorItem?.representedObject = self.noteOutlineView
  267. authorItem?.tag = 103
  268. showItem?.submenu = subMenu
  269. menu.addItem(.separator())
  270. _ = self._addExportPDFMenu(menu)
  271. menu.addItem(.separator())
  272. _ = self._addDeleteAllAnnoItem(menu)
  273. menu.addItem(.separator())
  274. let importItem = NSMenuItem(title: NSLocalizedString("导入注释", comment: ""), action: #selector(importNotes), keyEquivalent: "")
  275. importItem.target = self
  276. menu.addItem(importItem)
  277. let exportItem = NSMenuItem(title: NSLocalizedString("导出注释", comment: ""), action: #selector(exportNotes), keyEquivalent: "")
  278. exportItem.target = self
  279. menu.addItem(exportItem)
  280. if let data = NSApp.currentEvent {
  281. NSMenu.popUpContextMenu(menu, with: data, for: view, with: nil)
  282. }
  283. }
  284. func annoListValidateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  285. let action = menuItem.action
  286. if (action == #selector(note_expandAllComments) ||
  287. action == #selector(note_foldAllComments) ||
  288. action == #selector(exportAnnotationNotes) ||
  289. action == #selector(removeAllAnnotations)) {
  290. let cnt = self.annoListModel?.datas.count ?? 0
  291. return cnt > 0
  292. } else if (action == #selector(unfoldNoteAction) ||
  293. action == #selector(foldNoteAction)) {
  294. let row = self.noteOutlineView.clickedRow
  295. let foldNote = self.fetchNote(for: row)
  296. // SKNPDFAnnotationNote
  297. if foldNote is CPDFMarkupAnnotation || foldNote is CPDFTextAnnotation {
  298. return true
  299. } else {
  300. return false
  301. }
  302. } else if (action == #selector(editNoteFromTable)) {
  303. let row = self.noteOutlineView.clickedRow
  304. let foldNote = self.fetchNote(for: row)
  305. // if (@available(macOS 10.13, *)) {
  306. // if ([foldNote.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  307. // return NO;
  308. // }
  309. // }
  310. if foldNote is CPDFStampAnnotation || foldNote is KMAnnotationStamp || foldNote is CPDFListStampAnnotation {
  311. return false
  312. } else {
  313. return true
  314. }
  315. }
  316. return true
  317. }
  318. @IBAction func note_expandAllComments(_ sender: AnyObject?) {
  319. guard let model = self.annoListModel else {
  320. return
  321. }
  322. if (model.foldType == .unfold) { // 已全部展开
  323. return
  324. }
  325. model.foldType = .unfold
  326. self.noteOutlineView.reloadData()
  327. }
  328. @IBAction func note_foldAllComments(_ sender: AnyObject?) {
  329. guard let model = self.annoListModel else {
  330. return
  331. }
  332. if (model.foldType == .fold) {
  333. return
  334. }
  335. model.foldType = .fold
  336. self.noteOutlineView.reloadData()
  337. }
  338. @IBAction func noteShowNoteAction(_ sender: AnyObject?) {
  339. let item = sender as? NSMenuItem
  340. let tag = item?.tag ?? 0
  341. if (tag == 100) {
  342. } else if (tag == 101) {
  343. let isPage = !self.annoListIsShowPage()
  344. self.noteTypeDict[Self.Key.noteFilterPage] = !isPage
  345. } else if (tag == 102) {
  346. let isTime = !self.annoListIsShowTime()
  347. self.noteTypeDict[Self.Key.noteFilterTime] = !isTime
  348. } else if (tag == 103) {
  349. let isAuther = !self.annoListIsShowAnther()
  350. self.noteTypeDict[Self.Key.noteFilterAuther] = !isAuther
  351. }
  352. UserDefaults.standard.sync_setValue(self.noteTypeDict, forKey: Self.Key.noteTableColumn)
  353. // 更新数据
  354. var models: [KMBotaAnnotationModel] = []
  355. if self.noteSearchMode {
  356. models = self.noteSearchArray
  357. } else {
  358. models = self.annoListModel?.datas ?? []
  359. }
  360. for model in models {
  361. model.showPage = self.annoListIsShowPage()
  362. model.showTime = self.annoListIsShowTime()
  363. model.showAuthor = self.annoListIsShowAnther()
  364. }
  365. let selectRow = self.noteOutlineView.selectedRow
  366. self.noteOutlineView.reloadData()
  367. self.noteOutlineView.selectRowIndexes(IndexSet(integer: selectRow), byExtendingSelection: false)
  368. }
  369. @objc func importNotes(_ sender: NSMenuItem) {
  370. let panel = NSOpenPanel()
  371. panel.allowedFileTypes = ["xfdf"]
  372. panel.allowsMultipleSelection = false
  373. panel.beginSheetModal(for: self.view.window!) { resp in
  374. if resp != .OK {
  375. return
  376. }
  377. if let result = self.pdfDocument()?.importAnnotation(fromXFDFPath: panel.url?.path), result {
  378. self.reloadAnnotation()
  379. self.listView?.setNeedsDisplayForVisiblePages()
  380. }
  381. }
  382. }
  383. @objc func exportNotes(_ sender: NSMenuItem) {
  384. guard let cnt = self.listView?.notes.count, cnt > 0 else {
  385. NSSound.beep()
  386. return
  387. }
  388. let fileName = "\(self.pdfDocument()?.documentURL.deletingPathExtension().lastPathComponent ?? "")" + "_xfdf"
  389. let panel = NSSavePanel()
  390. panel.directoryURL = self.pdfDocument()?.documentURL.deletingLastPathComponent()
  391. panel.allowedFileTypes = ["xfdf"]
  392. panel.nameFieldStringValue = fileName
  393. panel.beginSheetModal(for: self.view.window!) { resp in
  394. if resp != .OK {
  395. return
  396. }
  397. let filePath = panel.url?.path
  398. if let success = self.pdfDocument()?.exportAnnotation(toXFDFPath: filePath), success {
  399. NSWorkspace.shared.selectFile(filePath, inFileViewerRootedAtPath: "")
  400. } else {
  401. Task {
  402. _ = await KMAlertTool.runModel(message: NSLocalizedString("Export Failure!", comment: ""), buttons: ["OK"])
  403. }
  404. }
  405. }
  406. }
  407. @objc func exportAnnotationNotes(_ sender: AnyObject?) {
  408. let doc = self.view.window?.windowController?.document as? NSDocument
  409. doc?.saveTo(sender)
  410. }
  411. // 展开
  412. @objc func unfoldNoteAction(_ sender: NSMenuItem) {
  413. if sender.state == .on {
  414. return
  415. }
  416. let row = self.noteOutlineView.clickedRow
  417. guard let model = self.fetchAnnoModel(for: row) else {
  418. return
  419. }
  420. model.foldType = .unfold
  421. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  422. (viewS as? KMNoteTableViewCell)?.isFold = false
  423. }
  424. @objc func foldNoteAction(_ sender: NSMenuItem) {
  425. if sender.state == .on {
  426. return
  427. }
  428. let row = self.noteOutlineView.clickedRow
  429. guard let model = self.fetchAnnoModel(for: row) else {
  430. return
  431. }
  432. model.foldType = .fold
  433. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  434. (viewS as? KMNoteTableViewCell)?.isFold = true
  435. }
  436. @objc func deleteNotes(_ sender: NSMenuItem) {
  437. self.outlineView(self.noteOutlineView, deleteItems: sender.representedObject as? [Any] ?? [])
  438. }
  439. @objc func removeAllAnnotations(_ sender: AnyObject?) {
  440. guard let doc = self.pdfDocument() else {
  441. return
  442. }
  443. Task {
  444. let response = await KMAlertTool.runModel(message: KMLocalizedString("This will permanently remove all annotations. Are you sure to continue?", nil), buttons: [KMLocalizedString("Yes", nil), KMLocalizedString("No", nil)])
  445. if response == .alertFirstButtonReturn {
  446. // var annos: [CPDFAnnotation] = []
  447. // for i in 0 ..< doc.pageCount {
  448. // let page = self.pdfDocument()?.page(at: i)
  449. // for anno in page?.annotations ?? [] {
  450. // if anno is CPDFTextWidgetAnnotation || anno is CPDFButtonWidgetAnnotation || anno is CPDFChoiceWidgetAnnotation {
  451. // continue
  452. // }
  453. // // if ([annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  454. // // continue;
  455. // // }
  456. // if anno is CPDFLinkAnnotation {
  457. // continue
  458. // }
  459. // annos.append(anno)
  460. // }
  461. // }
  462. self.dataUpdating = true
  463. // for anno in annos {
  464. // self.listView?.remove(anno)
  465. // }
  466. for model in self.annoListModel?.datas ?? [] {
  467. if let anno = model.anno {
  468. self.listView?.remove(anno)
  469. }
  470. }
  471. self.dataUpdating = false
  472. self.note_refrshUIIfNeed()
  473. }
  474. }
  475. }
  476. @objc func editNoteTextFromTable(_ sender: NSMenuItem) {
  477. // PDFAnnotation *annotation = [sender representedObject];
  478. guard let annotation = sender.representedObject as? CPDFAnnotation else {
  479. return
  480. }
  481. self.listView?.scrollAnnotationToVisible(annotation)
  482. // self.listView.activeAnnotation = annotation
  483. // [self showNote:annotation];
  484. // SKNoteWindowController *noteController = (SKNoteWindowController *)[self windowControllerForNote:annotation];
  485. // [[noteController window] makeFirstResponder:[noteController textView]];
  486. // [[noteController textView] selectAll:nil];
  487. }
  488. @objc func editThisAnnotation(_ sender: AnyObject?) {
  489. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  490. NSSound.beep()
  491. return
  492. }
  493. self.listView?.edit(annotation)
  494. }
  495. @objc func editNoteFromTable(_ sender: AnyObject?) {
  496. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  497. NSSound.beep()
  498. return
  499. }
  500. let model = fetchAnnoModel(for: annotation)
  501. let row = self.noteOutlineView.row(forItem: model)
  502. self.noteOutlineView.km_safe_selectRowIndexes(.init(integer: row), byExtendingSelection: false)
  503. let noteIndex = self.noteOutlineView.column(withIdentifier: .init("note"))
  504. if (noteIndex >= 0) {
  505. self.noteOutlineView.scrollColumnToVisible(noteIndex)
  506. //
  507. self.isRenameNoteOutline = true
  508. // self.renamePDFOutline = [rightSideController.noteOutlineView itemAtRow:rightSideController.noteOutlineView.clickedRow];
  509. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true) as? KMNoteTableViewCell
  510. viewS!.isFold = false
  511. let targrtTextField = viewS?.noteContentLabel
  512. self.editNoteTextField = targrtTextField
  513. self.editNote = annotation
  514. targrtTextField?.delegate = self
  515. targrtTextField?.isEditable = true
  516. targrtTextField?.becomeFirstResponder()
  517. }
  518. }
  519. @IBAction func noteSortAction(_ sender: AnyObject?) {
  520. if (self.isAscendSort) {
  521. self.isAscendSort = false
  522. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  523. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  524. } else {
  525. self.isAscendSort = true
  526. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  527. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  528. }
  529. KMDataManager.ud_set(self.isAscendSort, forKey: Self.Key.noteAscendSortKey)
  530. if self.noteSearchMode {
  531. self.reloadNoteForSearchMode()
  532. } else {
  533. self.reloadAnnotation()
  534. }
  535. }
  536. @IBAction func noteSearchAction(_ sender: NSButton) {
  537. self.noteSearchField.isHidden = false
  538. self.noteTitleLabel.isHidden = true
  539. self.noteSearchButton.isHidden = true
  540. self.noteDoneButton.isHidden = false
  541. self.noteFilterButton.isHidden = true
  542. self.noteMoreButton.isHidden = true
  543. self.noteSearchField.becomeFirstResponder()
  544. }
  545. @IBAction func noteFilterAction(_ sender: AnyObject?) {
  546. let button = sender as? NSButton
  547. let menu = NSMenu()
  548. let filterViewController = KMNoteOutlineFilterViewController()
  549. filterViewController.listView = self.listView
  550. filterViewController.view.layer?.backgroundColor = .clear
  551. filterViewController.setNotesArray(self.allAnnotations as NSArray)
  552. filterViewController.applyFilterCallback = { [weak self] typeArr, colorArr, authorArr, isEmpty in
  553. menu.cancelTracking()
  554. if (isEmpty) {
  555. self?.filterButtonLayer?.isHidden = true
  556. } else {
  557. self?.filterButtonLayer?.isHidden = false
  558. }
  559. self?.reloadAnnotation()
  560. }
  561. filterViewController.cancelCallback = { isCancel in
  562. if (isCancel) {
  563. menu.cancelTracking()
  564. }
  565. }
  566. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  567. item.target = self
  568. item.representedObject = filterViewController
  569. item.view = filterViewController.view
  570. menu.popUp(positioning: nil, at: NSMakePoint(-130, 30), in: button)
  571. }
  572. func fetchNote(for index: Int) -> CPDFAnnotation? {
  573. return self.fetchAnnoModel(for: index)?.anno
  574. }
  575. func fetchAnnoModel(for index: Int) -> KMBotaAnnotationModel? {
  576. if self.noteSearchMode { // 搜索模式
  577. return self.noteSearchArray.safe_element(for: index) as? KMBotaAnnotationModel
  578. } else { // 常规模式(非搜索)
  579. return self.annoListModel?.datas.safe_element(for: index) as? KMBotaAnnotationModel
  580. }
  581. }
  582. func fetchAnnoModel(for anno: CPDFAnnotation) -> KMBotaAnnotationModel? {
  583. if self.noteSearchMode { // 搜索模式
  584. for model in self.noteSearchArray {
  585. if anno.isEqual(to: model.anno) {
  586. return model
  587. }
  588. }
  589. } else { // 常规模式(非搜索)
  590. for model in self.annoListModel?.datas ?? [] {
  591. if anno.isEqual(to: model.anno) {
  592. return model
  593. }
  594. }
  595. }
  596. return nil
  597. }
  598. @IBAction @objc func sortTypeAction(_ sender: NSMenuItem) {
  599. let item = sender
  600. let tag = item.tag
  601. if (item.state == .on) {
  602. item.state = .off
  603. } else {
  604. item.state = .on
  605. }
  606. if (tag == 0) {
  607. self.noteSortType = .page
  608. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  609. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  610. } else if (tag == 1) {
  611. self.noteSortType = .time
  612. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  613. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  614. }
  615. KMDataManager.ud_set(self.noteSortType.rawValue, forKey: Self.Key.noteSortTypeKey)
  616. if self.noteSearchMode {
  617. self.reloadNoteForSearchMode()
  618. } else {
  619. self.reloadAnnotation()
  620. }
  621. }
  622. func showNoteEmptyView() {
  623. let view = self.noteOutlineView.enclosingScrollView?.documentView
  624. let viewFrame = view?.frame ?? .zero
  625. let emptyVcSize = self.leftSideEmptyVC.emptyAnnotationView.frame.size
  626. self.leftSideEmptyVC.emptyAnnotationView.frame = NSMakeRect((viewFrame.size.width-emptyVcSize.width)/2.0,(viewFrame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  627. self.leftSideEmptyVC.emptyAnnotationView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  628. self.noteOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.emptyAnnotationView)
  629. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = false
  630. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = false
  631. if (self.leftView.segmentedControl.selectedSegment == KMSelectedSegmentType.annotation.rawValue) {
  632. self.noteHeaderView.isHidden = true
  633. self.toolButtonBoxLayoutConstraint.constant = 40.0
  634. }
  635. }
  636. func hideNoteEmptyView() {
  637. self.leftSideEmptyVC.emptyAnnotationView.removeFromSuperview()
  638. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = true
  639. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = true
  640. if (self.leftView.segmentedControl.selectedSegment == KMSelectedSegmentType.annotation.rawValue) {
  641. self.noteHeaderView.isHidden = false
  642. self.toolButtonBoxLayoutConstraint.constant = 64.0
  643. }
  644. }
  645. }
  646. // MARK: - Note
  647. extension KMLeftSideViewController {
  648. public func refreshUIForAddAnnotation(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  649. let need = self._annoList_needRefreshUI(annos: annos)
  650. if need == false {
  651. return
  652. }
  653. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  654. self.note_reloadDataIfNeed()
  655. }
  656. public func refreshUIForAnnoAttributeDidChange(_ anno: CPDFAnnotation?, attributes: [String : Any]?) {
  657. self.updateThumbnail(at: Int(anno?.page?.pageIndex() ?? 0))
  658. let need = self._annoList_needRefreshUI(annos: anno != nil ? [anno!] : [])
  659. if need == false {
  660. return
  661. }
  662. if let data = anno {
  663. self.note_reloadDataForAnnoIfNeed(anno: data)
  664. }
  665. }
  666. public func annoList_refreshUIForDeleteAnnotations(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  667. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  668. if self.type.methodType != .Annotation {
  669. return
  670. }
  671. let need = self._annoList_needRefreshUI(annos: annos)
  672. if need == false {
  673. return
  674. }
  675. for anno in annos ?? [] {
  676. if let model = self.fetchAnnoModel(for: anno) {
  677. self.noteSearchArray.removeObject(model)
  678. self.annoListModel?.datas.removeObject(model)
  679. if self.allAnnotations.contains(anno) {
  680. self.allAnnotations.removeObject(anno)
  681. }
  682. }
  683. }
  684. if self.dataUpdating == false {
  685. self.note_refrshUIIfNeed()
  686. }
  687. }
  688. func note_refrshUIIfNeed() {
  689. if self.type.methodType != .Annotation {
  690. return
  691. }
  692. Task { @MainActor in
  693. self.noteOutlineView.reloadData()
  694. }
  695. }
  696. func note_reloadDataIfNeed() {
  697. if self.type.methodType != .Annotation {
  698. return
  699. }
  700. self.reloadAnnotation()
  701. }
  702. func note_reloadDataForAnnoIfNeed(anno: CPDFAnnotation) {
  703. if self.type.methodType != .Annotation {
  704. return
  705. }
  706. if anno is CPDFLineAnnotation || anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation || anno is CPDFInkAnnotation {
  707. // 形状注释 + Ink 需要显示框住的内容【刷新】
  708. for item in self.annoListModel?.datas ?? [] {
  709. if anno.isEqual(to: item.anno) {
  710. self.noteOutlineView.reloadItem(item)
  711. break
  712. }
  713. }
  714. } else {
  715. for item in self.annoListModel?.datas ?? [] {
  716. if anno.isEqual(to: item.anno) {
  717. self.noteOutlineView.reloadItem(item)
  718. break
  719. }
  720. }
  721. }
  722. }
  723. func reloadAnnotation() {
  724. if self.listView != nil {
  725. let filterKey = self.pdfDocument()?.documentURL.path ?? ""
  726. let typeArr: [String] = KMBotaTools.noteFilterAnnoTypes(key: filterKey)
  727. let colorArr: [Any] = KMBotaTools.noteFilterColors(key: filterKey)
  728. let authorArr: [Any] = KMBotaTools.noteFilterAuthors(key: filterKey)
  729. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  730. // self.filtrateButton.image = NSImage(named: "KMImageNameAnnotationsFiltrate")
  731. self.filterButtonLayer?.isHidden = true
  732. } else {
  733. // self.filtrateButton.image = NSImage(named: "icon_annotation_screening_select")self.filterButtonLayer?.isHidden = true
  734. self.filterButtonLayer?.isHidden = false
  735. }
  736. var annotationArray: [CPDFAnnotation] = []
  737. var allAnnotation: [CPDFAnnotation] = []
  738. for i in 0 ..< self.pageCount() {
  739. let page = self.pdfDocument()?.page(at: UInt(i))
  740. var annos: [CPDFAnnotation] = []
  741. // 处理过滤
  742. let types = ["Highlight","Underline","Strikeout","Squiggly","Freehand","FreeText","Note","Square","Circle","Line","Stamp","Arrow","Image","Redact","Sign"/*, "table"*/,"Polyline","Polygon"]
  743. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  744. annos = KMOCToolClass.filterAnnotation(annotations: page?.annotations ?? [],types: types) as? [CPDFAnnotation] ?? []
  745. annotationArray += annos
  746. } else {
  747. var filterAnnos: [CPDFAnnotation] = page?.annotations ?? []
  748. let allAnnos = KMOCToolClass.filterAnnotation(annotations: filterAnnos,types: types) as? [CPDFAnnotation] ?? []
  749. annotationArray += allAnnos
  750. if typeArr.count > 0 {
  751. var theTypes = typeArr
  752. if typeArr.contains(CPDFAnnotation.kType.measureArrow) && typeArr.contains(CPDFAnnotation.kType.arrow) == false {
  753. theTypes.append(CPDFAnnotation.kType.arrow)
  754. }
  755. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos, types: theTypes) as? [CPDFAnnotation]) ?? []
  756. }
  757. if (colorArr.count > 0) {
  758. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,colors: colorArr) as? [CPDFAnnotation]) ?? []
  759. }
  760. if (authorArr.count > 0) {
  761. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,authors: authorArr) as? [CPDFAnnotation]) ?? []
  762. }
  763. if typeArr.contains(CPDFAnnotation.kType.measureArrow) {
  764. if typeArr.contains(CPDFAnnotation.kType.arrow) == false {
  765. for anno in filterAnnos {
  766. if let data = anno as? CPDFLineAnnotation, data.type == CPDFAnnotation.kType.arrow && data.isMeasure == false {
  767. filterAnnos.removeObject(anno)
  768. }
  769. }
  770. }
  771. } else {
  772. for anno in filterAnnos {
  773. if let data = anno as? CPDFLineAnnotation, data.isMeasure {
  774. filterAnnos.removeObject(anno)
  775. }
  776. }
  777. }
  778. annos = filterAnnos
  779. }
  780. //添加签名注释
  781. for annotation in page?.annotations ?? [] {
  782. if annotation.isKind(of: CPDFSignatureAnnotation.self) {
  783. annos.append(annotation)
  784. annotationArray.append(annotation)
  785. }
  786. }
  787. for annotation in annos {
  788. if annotation.isKind(of: KMTableAnnotation.self) {
  789. annos.removeObject(annotation)
  790. if annotationArray.contains(annotation) {
  791. annotationArray.removeObject(annotation)
  792. }
  793. } else if annotation.annotationShouldDisplay() == false {
  794. annos.removeObject(annotation)
  795. if annotationArray.contains(annotation) {
  796. annotationArray.removeObject(annotation)
  797. }
  798. } else if annotation.isKind(of: CPDFLinkAnnotation.self) {
  799. annos.removeObject(annotation)
  800. if annotationArray.contains(annotation) {
  801. annotationArray.removeObject(annotation)
  802. }
  803. }
  804. }
  805. // 添加刷选后的注释
  806. allAnnotation += annos
  807. //添加所有annotation 用于筛选
  808. // annotationArray += (page?.annotations ?? [])
  809. // annotationArray += annos
  810. }
  811. // 处理排序
  812. if self.noteSortType == .page {
  813. /// 排序(升序)
  814. if self.isAscendSort {
  815. allAnnotation.sort {
  816. let idx0 = $0.page?.pageIndex() ?? 0
  817. let idx1 = $1.page?.pageIndex() ?? 0
  818. return idx0 <= idx1
  819. }
  820. } else {
  821. allAnnotation.sort {
  822. let idx0 = $0.page?.pageIndex() ?? 0
  823. let idx1 = $1.page?.pageIndex() ?? 0
  824. return idx0 > idx1
  825. }
  826. }
  827. } else if self.noteSortType == .time {
  828. /// 排序(升序)
  829. if self.isAscendSort {
  830. allAnnotation.sort {
  831. if $0.modificationDate() == nil {
  832. return false
  833. }
  834. if $1.modificationDate() == nil {
  835. return false
  836. }
  837. return $0.modificationDate() <= $1.modificationDate()
  838. }
  839. } else {
  840. allAnnotation.sort {
  841. if $0.modificationDate() == nil {
  842. return false
  843. }
  844. if $1.modificationDate() == nil {
  845. return false
  846. }
  847. return $0.modificationDate() > $1.modificationDate()
  848. }
  849. }
  850. }
  851. // 数据模型\化
  852. let model = KMAnnotationListModel()
  853. var datas: [KMBotaAnnotationModel] = []
  854. for anno in allAnnotation {
  855. let item = KMBotaAnnotationModel()
  856. item.anno = anno
  857. item.showPage = self.annoListIsShowPage()
  858. item.showTime = self.annoListIsShowTime()
  859. item.showAuthor = self.annoListIsShowAnther()
  860. datas.append(item)
  861. }
  862. model.datas = datas
  863. self.annoListModel = model
  864. // 转换对象,用于数据显示
  865. self.allAnnotations = annotationArray
  866. self.noteFilterButton.isEnabled = self.allAnnotations.count >= 1
  867. }
  868. self.note_refrshUIIfNeed()
  869. }
  870. func reloadNoteForSearchMode() {
  871. if self.noteSearchMode == false {
  872. return
  873. }
  874. // 处理排序
  875. if self.noteSortType == .page {
  876. if self.isAscendSort { /// 排序(升序)
  877. self.noteSearchArray.sort {
  878. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  879. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  880. return idx0 <= idx1
  881. }
  882. } else {
  883. self.noteSearchArray.sort {
  884. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  885. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  886. return idx0 > idx1
  887. }
  888. }
  889. } else if self.noteSortType == .time {
  890. if self.isAscendSort { /// 排序(升序)
  891. self.noteSearchArray.sort {
  892. if $0.anno?.modificationDate() == nil {
  893. return false
  894. }
  895. if $1.anno?.modificationDate() == nil {
  896. return false
  897. }
  898. return $0.anno!.modificationDate() <= $1.anno!.modificationDate()
  899. }
  900. } else {
  901. self.noteSearchArray.sort {
  902. if $0.anno?.modificationDate() == nil {
  903. return false
  904. }
  905. if $1.anno?.modificationDate() == nil {
  906. return false
  907. }
  908. return $0.anno!.modificationDate() > $1.anno!.modificationDate()
  909. }
  910. }
  911. }
  912. self.note_refrshUIIfNeed()
  913. }
  914. // 搜索 Action
  915. func updateNoteFilterPredicate() {
  916. var stringValue = self.noteSearchField.stringValue
  917. // 清空数据
  918. self.noteSearchArray.removeAll()
  919. if stringValue.isEmpty {
  920. for model in self.annoListModel?.datas ?? [] {
  921. guard let _ = model.anno else {
  922. continue
  923. }
  924. self.noteSearchArray.append(model)
  925. }
  926. } else {
  927. // 忽略大小写
  928. let caseInsensite = self.caseInsensitiveNoteSearch
  929. if caseInsensite {
  930. stringValue = stringValue.lowercased()
  931. }
  932. for model in self.annoListModel?.datas ?? [] {
  933. guard let note = model.anno else {
  934. continue
  935. }
  936. var noteString = ""
  937. if let anno = note as? CPDFMarkupAnnotation {
  938. noteString = anno.markupContent()
  939. } else {
  940. noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  941. }
  942. if caseInsensite {
  943. noteString = noteString.lowercased()
  944. }
  945. if noteString.contains(stringValue) {
  946. self.noteSearchArray.append(model)
  947. }
  948. }
  949. }
  950. self.note_refrshUIIfNeed()
  951. }
  952. @objc func selectSelectedNote(_ sender: AnyObject?) {
  953. if self.hideNotes() == false {
  954. let selectedNotes = self.selectedNotes()
  955. if selectedNotes.count == 1 {
  956. let annotation = selectedNotes.last!
  957. self.listView?.go(to: annotation.bounds, on: annotation.page, animated: true)
  958. // [pdfView scrollAnnotationToVisible:annotation];
  959. // [pdfView setActiveAnnotation:annotation];
  960. self.listView?.updateActiveAnnotations([annotation])
  961. self.listView?.setNeedsDisplayAnnotationViewForVisiblePages()
  962. if annotation is CPDFPolygonAnnotation || annotation is CPDFPolylineAnnotation {
  963. self.listView?.pdfListViewDelegate.pdfListViewAnnotationMeasureInfoChange?(self.listView, with: annotation)
  964. } else if let anno = annotation as? CPDFLineAnnotation {
  965. if anno.isMeasure {
  966. self.listView?.pdfListViewDelegate.pdfListViewAnnotationMeasureInfoChange?(self.listView, with: annotation)
  967. }
  968. }
  969. // }
  970. }
  971. // NSInteger column = [sender clickedColumn];
  972. // if (column != -1) {
  973. // NSString *colID = [[[sender tableColumns] objectAtIndex:column] identifier];
  974. //
  975. // if ([colID isEqualToString:@"color"]){
  976. // for (PDFAnnotation *annotation in self.pdfView.activeAnnotations) {
  977. // if (![annotation isKindOfClass:[PDFAnnotationChoiceWidget class]] &&
  978. // ![annotation isKindOfClass:[PDFAnnotationButtonWidget class]] &&
  979. // ![annotation isKindOfClass:[PDFAnnotationTextWidget class]]) {
  980. // [[NSColorPanel sharedColorPanel] orderFront:nil];
  981. // break;
  982. // }
  983. //
  984. // }
  985. // }
  986. // }
  987. }
  988. }
  989. func selectedNotes() -> [CPDFAnnotation] {
  990. var selectedNotes: [CPDFAnnotation] = []
  991. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  992. for row in rowIndexes {
  993. let item = self.noteOutlineView.item(atRow: row)
  994. if item is KMBotaAnnotationModel {
  995. if let anno = (item as! KMBotaAnnotationModel).anno {
  996. // if anno.type == nil {
  997. // item = [(SKNoteText *)item note];
  998. // }
  999. if selectedNotes.contains(anno) == false {
  1000. selectedNotes.append(anno)
  1001. }
  1002. }
  1003. }
  1004. }
  1005. return selectedNotes
  1006. }
  1007. func clearAnnotationFilterData() {
  1008. if let _key = self.pdfDocument()?.documentURL?.path {
  1009. let userDefaults = UserDefaults.standard
  1010. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1011. userDefaults.set(typeData, forKey: NoteFilterVC.filterSelectTypeKey + _key)
  1012. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1013. userDefaults.set(colorData, forKey: NoteFilterVC.filterSelectColorKey + _key)
  1014. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1015. userDefaults.set(authorData, forKey: NoteFilterVC.filterSelectAuthorKey + _key)
  1016. userDefaults.synchronize()
  1017. }
  1018. }
  1019. private func _annoList_needRefreshUI(annos: [CPDFAnnotation]?) -> Bool {
  1020. guard let data = annos else {
  1021. return false
  1022. }
  1023. // for anno in data {
  1024. // if anno.isKind(of: KMTableAnnotation.self) == false {
  1025. // return true
  1026. // }
  1027. // }
  1028. return true
  1029. }
  1030. }