KMLeftSideViewController+Note.swift 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 = CGRectMake(0, 0, CGRectGetWidth(self.noteMoreButton.bounds), CGRectGetHeight(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 = CGRectMake(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 = { [unowned 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: CGPointMake(-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. }
  141. // MARK: - Menu
  142. extension KMLeftSideViewController {
  143. func annoListMenu(_ menu: NSMenu) {
  144. var item: NSMenuItem?
  145. var items: NSArray?
  146. var rowIndexes = self.noteOutlineView.selectedRowIndexes
  147. let row = self.noteOutlineView.clickedRow
  148. if row == -1 {
  149. self._addExportPDFMenu(menu)
  150. item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
  151. return
  152. }
  153. if rowIndexes.contains(row) == false {
  154. rowIndexes = IndexSet(integer: row)
  155. }
  156. items = self.noteOutlineView.itemsAtRowIndexes(rowIndexes) as NSArray
  157. guard let foldNote = self.fetchNote(for: row) else {
  158. return
  159. }
  160. var isFold = true
  161. if self.allFoldNotes.contains(foldNote) {
  162. isFold = false
  163. }
  164. item = menu.addItem(title: KMLocalizedString("Expand", nil), action: #selector(unfoldNoteAction), target: self)
  165. if (isFold) {
  166. item?.state = .off
  167. } else {
  168. item?.state = .on
  169. }
  170. item?.representedObject = items
  171. item = menu.addItem(title: KMLocalizedString("Collapse", nil), action: #selector(foldNoteAction), target: self)
  172. if (isFold) {
  173. item?.state = .on
  174. } else {
  175. item?.state = .off
  176. }
  177. item?.representedObject = items
  178. menu.addItem(.separator())
  179. let hideNotes = self.hideNotes()
  180. if hideNotes == false && (items?.count ?? 0) == 1 {
  181. let annotation = self.noteItems(items!).lastObject as? CPDFAnnotation
  182. if let data = annotation?.isEditable(), data {
  183. if annotation?.type == nil {
  184. let isNote = annotation?.isNote() ?? false
  185. if isNote {
  186. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  187. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteTextFromTable), target: self)
  188. item?.representedObject = annotation
  189. }
  190. } else if let data = self.noteOutlineView.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier("note"))?.isHidden, data {
  191. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  192. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  193. item?.representedObject = annotation
  194. } else {
  195. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteFromTable), target: self)
  196. item?.representedObject = annotation
  197. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  198. item?.representedObject = annotation
  199. item?.keyEquivalentModifierMask = [.option]
  200. item?.isAlternate = true
  201. }
  202. }
  203. }
  204. if menu.numberOfItems > 0 {
  205. self._addExportPDFMenu(menu)
  206. menu.addItem(.separator())
  207. if self.outlineView(self.noteOutlineView, canDeleteItems: items as? [Any] ?? []) {
  208. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteNotes), target: self)
  209. item?.representedObject = items
  210. }
  211. item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
  212. }
  213. }
  214. private func _addExportPDFMenu(_ menu: NSMenu) -> NSMenu {
  215. var item = menu.addItem(title: NSLocalizedString("Export Annotations…", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  216. let subMenu = NSMenu()
  217. item?.submenu = subMenu
  218. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  219. item?.tag = 0
  220. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  221. item?.tag = 1
  222. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  223. item?.tag = 2
  224. item = subMenu.addItem(title: NSLocalizedString("Notes as Texts", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  225. item?.tag = 3
  226. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  227. item?.tag = 4
  228. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  229. item?.tag = 5
  230. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  231. item?.tag = 6
  232. return menu
  233. }
  234. @IBAction func note_expandAllComments(_ sender: AnyObject?) {
  235. if (self.foldType == .unfold) { // 已全部展开
  236. return
  237. }
  238. // 设置全部展开的标识
  239. self.foldType = .unfold
  240. // 加载数据
  241. self.loadUnfoldDate(.none)
  242. // 刷新UI
  243. self.noteOutlineView.reloadData()
  244. }
  245. @IBAction func note_foldAllComments(_ sender: AnyObject?) {
  246. if (self.foldType == .fold) {
  247. return
  248. }
  249. self.foldType = .fold
  250. self.loadUnfoldDate(.none)
  251. self.noteOutlineView.reloadData()
  252. }
  253. @IBAction func noteShowNoteAction(_ sender: AnyObject?) {
  254. let item = sender as? NSMenuItem
  255. let tag = item?.tag ?? 0
  256. if (tag == 100) {
  257. } else if (tag == 101) {
  258. let isPage = self.noteTypeDict[Self.Key.noteFilterPage] as? Bool ?? false
  259. self.noteTypeDict[Self.Key.noteFilterPage] = !isPage
  260. } else if (tag == 102) {
  261. let isTime = self.noteTypeDict[Self.Key.noteFilterTime] as? Bool ?? false
  262. self.noteTypeDict[Self.Key.noteFilterTime] = !isTime
  263. } else if (tag == 103) {
  264. let isAuther = self.noteTypeDict[Self.Key.noteFilterAuther] as? Bool ?? false
  265. self.noteTypeDict[Self.Key.noteFilterAuther] = !isAuther
  266. }
  267. UserDefaults.standard.sync_setValue(self.noteTypeDict, forKey: Self.Key.noteTableColumn)
  268. let selectRow = self.noteOutlineView.selectedRow
  269. self.noteOutlineView.reloadData()
  270. self.noteOutlineView.selectRowIndexes(IndexSet(integer: selectRow), byExtendingSelection: false)
  271. }
  272. @objc func exportAnnotationNotes(_ sender: AnyObject?) {
  273. let doc = self.view.window?.windowController?.document as? NSDocument
  274. doc?.saveTo(sender)
  275. }
  276. @objc func leftSideEmptyAnnotationClick_DeleteAnnotation(_ sender: AnyObject?) {
  277. guard let doc = self.pdfDocument() else {
  278. return
  279. }
  280. Task {
  281. 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)])
  282. if response == .alertFirstButtonReturn {
  283. var annos: [CPDFAnnotation] = []
  284. for i in 0 ..< doc.pageCount {
  285. let page = self.pdfDocument()?.page(at: i)
  286. for anno in page?.annotations ?? [] {
  287. if anno is CPDFTextWidgetAnnotation || anno is CPDFButtonWidgetAnnotation || anno is CPDFChoiceWidgetAnnotation {
  288. continue
  289. }
  290. // if ([annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  291. // continue;
  292. // }
  293. annos.append(anno)
  294. }
  295. }
  296. for anno in annos {
  297. self.listView?.remove(anno)
  298. }
  299. self.reloadAnnotation()
  300. }
  301. }
  302. }
  303. @objc func unfoldNoteAction(_ sender: NSMenuItem) {
  304. if sender.state == .on {
  305. return
  306. }
  307. let row = self.noteOutlineView.clickedRow
  308. guard let foldNote = self.fetchNote(for: row) else {
  309. return
  310. }
  311. if self.allFoldNotes.contains(foldNote) == false {
  312. self.allFoldNotes.append(foldNote)
  313. }
  314. if self.allFoldNotes.count == self.canFoldNotes.count {
  315. self.foldType = .unfold
  316. } else {
  317. self.foldType = .none
  318. }
  319. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  320. (viewS as? KMNoteTableViewCell)?.isFold = false
  321. }
  322. @objc func foldNoteAction(_ sender: NSMenuItem) {
  323. // if sender.state == .on {
  324. // return
  325. // }
  326. let row = self.noteOutlineView.clickedRow
  327. guard let foldNote = self.fetchNote(for: row) else {
  328. return
  329. }
  330. if self.allFoldNotes.contains(foldNote) == false {
  331. self.allFoldNotes.append(foldNote)
  332. }
  333. if (self.allFoldNotes.count == 0) {
  334. self.foldType = .fold
  335. } else {
  336. self.foldType = .none
  337. }
  338. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  339. (viewS as? KMNoteTableViewCell)?.isFold = true
  340. }
  341. @objc func deleteNotes(_ sender: NSMenuItem) {
  342. self.outlineView(self.noteOutlineView, deleteItems: sender.representedObject as? [Any] ?? [])
  343. }
  344. @objc func removeAllAnnotations(_ sender: AnyObject?) {
  345. self.leftSideEmptyAnnotationClick_DeleteAnnotation(nil)
  346. }
  347. @objc func editNoteTextFromTable(_ sender: NSMenuItem) {
  348. // PDFAnnotation *annotation = [sender representedObject];
  349. guard let annotation = sender.representedObject as? CPDFAnnotation else {
  350. return
  351. }
  352. self.listView?.scrollAnnotationToVisible(annotation)
  353. // self.listView.activeAnnotation = annotation
  354. // [self showNote:annotation];
  355. // SKNoteWindowController *noteController = (SKNoteWindowController *)[self windowControllerForNote:annotation];
  356. // [[noteController window] makeFirstResponder:[noteController textView]];
  357. // [[noteController textView] selectAll:nil];
  358. }
  359. @objc func editThisAnnotation(_ sender: AnyObject?) {
  360. KMPrint("editThisAnnotation ...")
  361. }
  362. @objc func editNoteFromTable(_ sender: AnyObject?) {
  363. KMPrint("editNoteFromTable ...")
  364. }
  365. @IBAction func noteSortAction(_ sender: AnyObject?) {
  366. if (self.isAscendSort) {
  367. self.isAscendSort = false
  368. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  369. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  370. } else {
  371. self.isAscendSort = true
  372. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  373. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  374. }
  375. KMDataManager.ud_set(self.isAscendSort, forKey: Self.Key.noteAscendSortKey)
  376. // self.annotationSort(sortArray: [])
  377. self.reloadAnnotation()
  378. }
  379. @IBAction func noteSearchAction(_ sender: NSButton) {
  380. self.noteSearchField.isHidden = false
  381. self.noteTitleLabel.isHidden = true
  382. self.noteSearchButton.isHidden = true
  383. self.noteDoneButton.isHidden = false
  384. self.noteFilterButton.isHidden = true
  385. self.noteMoreButton.isHidden = true
  386. self.noteSearchField.becomeFirstResponder()
  387. }
  388. @IBAction func noteFilterAction(_ sender: AnyObject?) {
  389. let button = sender as? NSButton
  390. let menu = NSMenu()
  391. let filterViewController = KMNoteOutlineFilterViewController()
  392. filterViewController.listView = self.listView
  393. filterViewController.view.layer?.backgroundColor = .clear
  394. var notes = NSMutableArray()
  395. // for section in self._annotations {
  396. // if section.annotations?.count != 0 {
  397. // for item in section.annotations! {
  398. // notes.add(item.annotation!)
  399. // }
  400. // }
  401. // }
  402. filterViewController.setNotesArray(self.allAnnotations as NSArray)
  403. filterViewController.applyFilterCallback = { [weak self] typeArr, colorArr, authorArr, isEmpty in
  404. menu.cancelTracking()
  405. if (isEmpty) {
  406. self?.filterButtonLayer?.isHidden = true
  407. } else {
  408. self?.filterButtonLayer?.isHidden = false
  409. }
  410. self?.reloadAnnotation()
  411. }
  412. filterViewController.cancelCallback = { isCancel in
  413. if (isCancel) {
  414. menu.cancelTracking()
  415. }
  416. }
  417. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  418. item.target = self
  419. item.representedObject = filterViewController
  420. item.view = filterViewController.view
  421. menu.popUp(positioning: nil, at: CGPointMake(-130, 30), in: button)
  422. }
  423. func fetchNote(for index: Int) -> CPDFAnnotation? {
  424. if self.noteSearchMode { // 搜索模式
  425. return self.noteSearchArray.safe_element(for: index) as? CPDFAnnotation
  426. } else { // 常规模式(非搜索)
  427. let section = self.annotations.safe_element(for: index) as? KMBOTAAnnotationSection
  428. return section?.annotations?.first?.annotation
  429. }
  430. }
  431. @IBAction @objc func sortTypeAction(_ sender: NSMenuItem) {
  432. let item = sender
  433. let tag = item.tag
  434. if (item.state == .on) {
  435. item.state = .off
  436. } else {
  437. item.state = .on
  438. }
  439. if (tag == 0) {
  440. self.noteSortType = .page
  441. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  442. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  443. } else if (tag == 1) {
  444. self.noteSortType = .time
  445. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  446. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  447. }
  448. KMDataManager.ud_set(self.noteSortType.rawValue, forKey: Self.Key.noteSortTypeKey)
  449. // self.annotationSort(sortArray: [])
  450. self.reloadAnnotation()
  451. }
  452. }
  453. // MARK: - Note
  454. extension KMLeftSideViewController {
  455. func note_refrshUIIfNeed() {
  456. if self.type.methodType != .Annotation {
  457. return
  458. }
  459. Task { @MainActor in
  460. self.noteOutlineView.reloadData()
  461. }
  462. }
  463. func note_reloadDataIfNeed() {
  464. if self.type.methodType != .Annotation {
  465. return
  466. }
  467. self.reloadAnnotation()
  468. }
  469. func note_reloadDataForAnnoIfNeed(anno: CPDFAnnotation) {
  470. if self.type.methodType != .Annotation {
  471. return
  472. }
  473. if anno is CPDFLineAnnotation || anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation || anno is CPDFInkAnnotation {
  474. // 形状注释 + Ink 需要显示框住的内容【刷新】
  475. for item in self.annotations {
  476. if let data = item.annotations?.first?.annotation, data.isEqual(to: anno) {
  477. self.noteOutlineView.reloadItem(item.annotations?.first)
  478. break
  479. }
  480. }
  481. }
  482. }
  483. func reloadAnnotation() {
  484. if self.listView != nil {
  485. let filterKey = self.pdfDocument()?.documentURL.path ?? ""
  486. var typeArr: [Any] = KMBotaTools.noteFilterAnnoTypes(key: filterKey)
  487. var colorArr: [Any] = KMBotaTools.noteFilterColors(key: filterKey)
  488. var authorArr: [Any] = KMBotaTools.noteFilterAuthors(key: filterKey)
  489. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  490. // self.filtrateButton.image = NSImage(named: "KMImageNameAnnotationsFiltrate")
  491. self.filterButtonLayer?.isHidden = true
  492. } else {
  493. // self.filtrateButton.image = NSImage(named: "icon_annotation_screening_select")self.filterButtonLayer?.isHidden = true
  494. self.filterButtonLayer?.isHidden = false
  495. }
  496. var dataArray: [KMBOTAAnnotationSection] = []
  497. var annotationArray: [CPDFAnnotation] = []
  498. var allAnnotation: [CPDFAnnotation] = []
  499. for i in 0 ..< self.pageCount() {
  500. var items: [KMBOTAAnnotationItem] = []
  501. let page = self.pdfDocument()?.page(at: UInt(i))
  502. var annos: [CPDFAnnotation] = []
  503. // 处理过滤
  504. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  505. let types = ["Highlight","Underline","Strikeout","Freehand","FreeText","Note","Square","Circle","Line","Stamp","Arrow","Image","Redact","Sign"]
  506. annos = KMOCToolClass.filterAnnotation(annotations: page?.annotations ?? [],types: types) as? [CPDFAnnotation] ?? []
  507. } else {
  508. var filterAnnos: [CPDFAnnotation] = page?.annotations ?? []
  509. if typeArr.count > 0 {
  510. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos, types: typeArr) as? [CPDFAnnotation]) ?? []
  511. }
  512. if (colorArr.count > 0) {
  513. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,colors: colorArr) as? [CPDFAnnotation]) ?? []
  514. }
  515. if (authorArr.count > 0) {
  516. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,authors: authorArr) as? [CPDFAnnotation]) ?? []
  517. }
  518. annos = filterAnnos
  519. }
  520. //添加签名注释
  521. for annotation in page?.annotations ?? [] {
  522. if annotation.isKind(of: CPDFSignatureAnnotation.self) {
  523. annos.append(annotation)
  524. }
  525. }
  526. for annotation in annos {
  527. if annotation.annotationShouldDisplay() == false {
  528. annos.removeObject(annotation)
  529. }
  530. }
  531. //转换所有annotation类型
  532. let section = KMBOTAAnnotationSection()
  533. for anno in annos {
  534. let item = KMBOTAAnnotationItem()
  535. item.section = section
  536. item.annotation = anno
  537. item.index = Int(anno.page.pageIndex())
  538. items.append(item)
  539. allAnnotation.append(anno)
  540. }
  541. if items.count != 0 {
  542. section.annotations = items
  543. section.page = page
  544. section.isItemExpanded = true
  545. dataArray.append(section)
  546. }
  547. //添加所有annotation 用于筛选
  548. annotationArray += (page?.annotations ?? [])
  549. }
  550. // 处理排序
  551. if self.noteSortType == .page {
  552. var datas: [KMBOTAAnnotationSection] = []
  553. /// 排序(升序)
  554. if self.isAscendSort {
  555. allAnnotation.sort {
  556. let idx0 = $0.page?.pageIndex() ?? 0
  557. let idx1 = $1.page?.pageIndex() ?? 0
  558. return idx0 <= idx1
  559. }
  560. } else {
  561. allAnnotation.sort {
  562. let idx0 = $0.page?.pageIndex() ?? 0
  563. let idx1 = $1.page?.pageIndex() ?? 0
  564. return idx0 > idx1
  565. }
  566. }
  567. for anno in allAnnotation {
  568. let section = KMBOTAAnnotationSection()
  569. let item = KMBOTAAnnotationItem()
  570. item.section = section
  571. item.annotation = anno
  572. item.index = Int(anno.pageIndex())
  573. section.annotations = [item]
  574. section.page = anno.page
  575. section.isItemExpanded = true
  576. datas.append(section)
  577. }
  578. self.annotations = datas
  579. } else if self.noteSortType == .time {
  580. var datas: [KMBOTAAnnotationSection] = []
  581. /// 排序(升序)
  582. if self.isAscendSort {
  583. allAnnotation.sort {
  584. if $0.modificationDate() == nil {
  585. return false
  586. }
  587. if $1.modificationDate() == nil {
  588. return false
  589. }
  590. return $0.modificationDate() <= $1.modificationDate()
  591. }
  592. } else {
  593. allAnnotation.sort {
  594. if $0.modificationDate() == nil {
  595. return false
  596. }
  597. if $1.modificationDate() == nil {
  598. return false
  599. }
  600. return $0.modificationDate() > $1.modificationDate()
  601. }
  602. }
  603. for anno in allAnnotation {
  604. let section = KMBOTAAnnotationSection()
  605. let item = KMBOTAAnnotationItem()
  606. item.section = section
  607. item.annotation = anno
  608. item.index = Int(anno.pageIndex())
  609. section.annotations = [item]
  610. section.page = anno.page
  611. section.isItemExpanded = true
  612. datas.append(section)
  613. }
  614. self.annotations = datas
  615. }
  616. // 转换对象,用于数据显示
  617. self.allAnnotations = annotationArray
  618. // if self.annotations.count < 1 {
  619. // self.filtrateButton.isEnabled = false
  620. // } else {
  621. // self.filtrateButton.isEnabled = true
  622. // }
  623. }
  624. Task { @MainActor in
  625. self.noteOutlineView.reloadData()
  626. }
  627. }
  628. // 搜索 Action
  629. func updateNoteFilterPredicate() {
  630. var stringValue = self.noteSearchField.stringValue
  631. if self.caseInsensitiveNoteSearch { // 忽略大小写
  632. stringValue = stringValue.lowercased()
  633. }
  634. // 清空数据
  635. self.noteSearchArray.removeAll()
  636. if stringValue.isEmpty {
  637. for section in self.annotations {
  638. guard let note = section.annotations?.first?.annotation else {
  639. continue
  640. }
  641. self.noteSearchArray.append(note)
  642. }
  643. } else {
  644. for section in self.annotations {
  645. guard let note = section.annotations?.first?.annotation else {
  646. continue
  647. }
  648. var noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  649. if let anno = note as? CPDFMarkupAnnotation {
  650. noteString = anno.markupContent()
  651. }
  652. if self.caseInsensitiveNoteSearch {
  653. noteString = noteString.lowercased()
  654. }
  655. if noteString.contains(stringValue) {
  656. self.noteSearchArray.append(note)
  657. }
  658. }
  659. }
  660. // 刷新 UI
  661. Task { @MainActor in
  662. self.noteOutlineView.reloadData()
  663. }
  664. }
  665. @objc func selectSelectedNote(_ sender: AnyObject?) {
  666. if self.hideNotes() == false {
  667. let selectedNotes = self.selectedNotes()
  668. if selectedNotes.count == 1 {
  669. let annotation = selectedNotes.last!
  670. self.listView?.go(to: annotation.bounds, on: annotation.page, animated: true)
  671. // [pdfView scrollAnnotationToVisible:annotation];
  672. // [pdfView setActiveAnnotation:annotation];
  673. self.listView?.updateActiveAnnotations([annotation])
  674. self.listView?.setNeedsDisplayAnnotationViewForVisiblePages()
  675. // }
  676. }
  677. // NSInteger column = [sender clickedColumn];
  678. // if (column != -1) {
  679. // NSString *colID = [[[sender tableColumns] objectAtIndex:column] identifier];
  680. //
  681. // if ([colID isEqualToString:@"color"]){
  682. // for (PDFAnnotation *annotation in self.pdfView.activeAnnotations) {
  683. // if (![annotation isKindOfClass:[PDFAnnotationChoiceWidget class]] &&
  684. // ![annotation isKindOfClass:[PDFAnnotationButtonWidget class]] &&
  685. // ![annotation isKindOfClass:[PDFAnnotationTextWidget class]]) {
  686. // [[NSColorPanel sharedColorPanel] orderFront:nil];
  687. // break;
  688. // }
  689. //
  690. // }
  691. // }
  692. // }
  693. }
  694. }
  695. func selectedNotes() -> [CPDFAnnotation] {
  696. var selectedNotes: [CPDFAnnotation] = []
  697. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  698. for row in rowIndexes {
  699. let item = self.noteOutlineView.item(atRow: row)
  700. if item is KMBOTAAnnotationItem {
  701. if let anno = (item as! KMBOTAAnnotationItem).annotation {
  702. // if anno.type == nil {
  703. // item = [(SKNoteText *)item note];
  704. // }
  705. if selectedNotes.contains(anno) == false {
  706. selectedNotes.append(anno)
  707. }
  708. }
  709. }
  710. }
  711. return selectedNotes
  712. }
  713. func clearAnnotationFilterData() {
  714. if let _key = self.pdfDocument()?.documentURL?.path {
  715. let userDefaults = UserDefaults.standard
  716. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  717. userDefaults.set(typeData, forKey: NoteFilterVC.filterSelectTypeKey + _key)
  718. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  719. userDefaults.set(colorData, forKey: NoteFilterVC.filterSelectColorKey + _key)
  720. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  721. userDefaults.set(authorData, forKey: NoteFilterVC.filterSelectAuthorKey + _key)
  722. userDefaults.synchronize()
  723. }
  724. }
  725. func loadUnfoldDate(_ foldType: KMFoldType) {
  726. // 清空所有折叠数组
  727. self.allFoldNotes.removeAll()
  728. var mutableArray: [CPDFAnnotation] = []
  729. if self.noteSearchMode {
  730. for note in self.noteSearchArray {
  731. if note is CPDFMarkupAnnotation {
  732. mutableArray.append(note)
  733. }
  734. }
  735. } else {
  736. for section in self.annotations {
  737. for item in section.annotations ?? [] {
  738. if let note = item.annotation, note is CPDFMarkupAnnotation {
  739. mutableArray.append(note)
  740. }
  741. }
  742. }
  743. }
  744. // for note in self.notes {
  745. // if note is CPDFMarkupAnnotation {
  746. // mutableArray.append(note)
  747. // }
  748. // }
  749. self.canFoldNotes = mutableArray
  750. self.allFoldNotes = []
  751. }
  752. }