KMLeftSideViewController+Note.swift 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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: - Menu
  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)!
  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. @IBAction func note_expandAllComments(_ sender: AnyObject?) {
  141. if (self.foldType == .unfold) { // 已全部展开
  142. return
  143. }
  144. // 设置全部展开的标识
  145. self.foldType = .unfold
  146. // 加载数据
  147. self.loadUnfoldDate(.none)
  148. // 刷新UI
  149. self.noteOutlineView.reloadData()
  150. }
  151. @IBAction func note_foldAllComments(_ sender: AnyObject?) {
  152. if (self.foldType == .fold) {
  153. return
  154. }
  155. self.foldType = .fold
  156. self.loadUnfoldDate(.none)
  157. self.noteOutlineView.reloadData()
  158. }
  159. @IBAction func noteShowNoteAction(_ sender: AnyObject?) {
  160. let item = sender as? NSMenuItem
  161. let tag = item?.tag ?? 0
  162. if (tag == 100) {
  163. } else if (tag == 101) {
  164. let isPage = self.noteTypeDict[Self.Key.noteFilterPage] as? Bool ?? false
  165. self.noteTypeDict[Self.Key.noteFilterPage] = !isPage
  166. } else if (tag == 102) {
  167. let isTime = self.noteTypeDict[Self.Key.noteFilterTime] as? Bool ?? false
  168. self.noteTypeDict[Self.Key.noteFilterTime] = !isTime
  169. } else if (tag == 103) {
  170. let isAuther = self.noteTypeDict[Self.Key.noteFilterAuther] as? Bool ?? false
  171. self.noteTypeDict[Self.Key.noteFilterAuther] = !isAuther
  172. }
  173. UserDefaults.standard.sync_setValue(self.noteTypeDict, forKey: Self.Key.noteTableColumn)
  174. let selectRow = self.noteOutlineView.selectedRow
  175. self.noteOutlineView.reloadData()
  176. self.noteOutlineView.selectRowIndexes(IndexSet(integer: selectRow), byExtendingSelection: false)
  177. }
  178. @objc func exportAnnotationNotes(_ sender: AnyObject?) {
  179. let doc = self.view.window?.windowController?.document as? NSDocument
  180. doc?.saveTo(sender)
  181. }
  182. @objc func leftSideEmptyAnnotationClick_DeleteAnnotation(_ sender: AnyObject?) {
  183. guard let doc = self.listView.document else {
  184. return
  185. }
  186. Task {
  187. 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)])
  188. if response == .alertFirstButtonReturn {
  189. var annotations = NSMutableArray()
  190. for i in 0 ..< doc.pageCount {
  191. let page = self.listView.document.page(at: i)
  192. for anno in page?.annotations ?? [] {
  193. if anno is CPDFTextWidgetAnnotation || anno is CPDFButtonWidgetAnnotation || anno is CPDFChoiceWidgetAnnotation {
  194. continue
  195. }
  196. // if (@available(macOS 10.13, *)) {
  197. // if ([annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  198. // continue;
  199. // }
  200. // }
  201. // if anno is CPDFInkAnnotation {
  202. // continue
  203. // }
  204. annotations.add(anno)
  205. }
  206. }
  207. for anno in annotations {
  208. if let data = anno as? CPDFAnnotation {
  209. self.listView.remove(data)
  210. }
  211. }
  212. self.reloadAnnotation()
  213. self.noteOutlineView.reloadData()
  214. }
  215. }
  216. }
  217. @objc func unfoldNoteAction(_ sender: NSMenuItem) {
  218. if sender.state == .on {
  219. return
  220. }
  221. let row = self.noteOutlineView.clickedRow
  222. guard let foldNote = self.fetchNote(for: row) else {
  223. return
  224. }
  225. if self.allFoldNotes.contains(foldNote) == false {
  226. self.allFoldNotes.append(foldNote)
  227. }
  228. if self.allFoldNotes.count == self.canFoldNotes.count {
  229. self.foldType = .unfold
  230. } else {
  231. self.foldType = .none
  232. }
  233. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  234. (viewS as? KMNoteTableViewCell)?.isFold = false
  235. }
  236. @objc func foldNoteAction(_ sender: NSMenuItem) {
  237. // if sender.state == .on {
  238. // return
  239. // }
  240. let row = self.noteOutlineView.clickedRow
  241. guard let foldNote = self.fetchNote(for: row) else {
  242. return
  243. }
  244. if self.allFoldNotes.contains(foldNote) == false {
  245. self.allFoldNotes.append(foldNote)
  246. }
  247. if (self.allFoldNotes.count == 0) {
  248. self.foldType = .fold
  249. } else {
  250. self.foldType = .none
  251. }
  252. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  253. (viewS as? KMNoteTableViewCell)?.isFold = true
  254. }
  255. @objc func deleteNotes(_ sender: NSMenuItem) {
  256. self.outlineView(self.noteOutlineView, deleteItems: sender.representedObject as! [Any])
  257. }
  258. @objc func removeAllAnnotations(_ sender: AnyObject?) {
  259. self.leftSideEmptyAnnotationClick_DeleteAnnotation(nil)
  260. }
  261. @objc func editNoteTextFromTable(_ sender: NSMenuItem) {
  262. // PDFAnnotation *annotation = [sender representedObject];
  263. guard let annotation = sender.representedObject as? CPDFAnnotation else {
  264. return
  265. }
  266. self.listView.scrollAnnotationToVisible(annotation)
  267. // self.listView.activeAnnotation = annotation
  268. // [self showNote:annotation];
  269. // SKNoteWindowController *noteController = (SKNoteWindowController *)[self windowControllerForNote:annotation];
  270. // [[noteController window] makeFirstResponder:[noteController textView]];
  271. // [[noteController textView] selectAll:nil];
  272. }
  273. @objc func editThisAnnotation(_ sender: AnyObject?) {
  274. KMPrint("editThisAnnotation ...")
  275. }
  276. @objc func editNoteFromTable(_ sender: AnyObject?) {
  277. KMPrint("editNoteFromTable ...")
  278. }
  279. @IBAction func noteSortAction(_ sender: AnyObject?) {
  280. if (self.isAscendSort) {
  281. self.isAscendSort = false
  282. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  283. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  284. } else {
  285. self.isAscendSort = true
  286. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  287. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  288. }
  289. KMDataManager.ud_set(self.isAscendSort, forKey: Self.Key.noteAscendSortKey)
  290. self.annotationSort(sortArray: [])
  291. }
  292. @IBAction func noteSearchAction(_ sender: NSButton) {
  293. self.noteSearchField.isHidden = false
  294. self.noteTitleLabel.isHidden = true
  295. self.noteSearchButton.isHidden = true
  296. self.noteDoneButton.isHidden = false
  297. self.noteFilterButton.isHidden = true
  298. self.noteMoreButton.isHidden = true
  299. self.noteSearchField.becomeFirstResponder()
  300. }
  301. @IBAction func noteFilterAction(_ sender: AnyObject?) {
  302. let button = sender as? NSButton
  303. let menu = NSMenu()
  304. let filterViewController = KMNoteOutlineFilterViewController()
  305. filterViewController.listView = self.listView
  306. filterViewController.view.layer?.backgroundColor = .clear
  307. var notes = NSMutableArray()
  308. // for section in self._annotations {
  309. // if section.annotations?.count != 0 {
  310. // for item in section.annotations! {
  311. // notes.add(item.annotation!)
  312. // }
  313. // }
  314. // }
  315. filterViewController.setNotesArray(self.allAnnotations as NSArray)
  316. filterViewController.applyFilterCallback = { [weak self] typeArr, colorArr, authorArr, isEmpty in
  317. menu.cancelTracking()
  318. if (isEmpty) {
  319. self?.filterButtonLayer?.isHidden = true
  320. } else {
  321. self?.filterButtonLayer?.isHidden = false
  322. }
  323. self?.annotationSort(sortArray: [])
  324. }
  325. filterViewController.cancelCallback = { isCancel in
  326. if (isCancel) {
  327. menu.cancelTracking()
  328. }
  329. }
  330. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  331. item.target = self
  332. item.representedObject = filterViewController
  333. item.view = filterViewController.view
  334. menu.popUp(positioning: nil, at: CGPointMake(-130, 30), in: button)
  335. }
  336. func fetchNote(for index: Int) -> CPDFAnnotation? {
  337. if self.noteSearchMode { // 搜索模式
  338. return self.noteSearchArray.safe_element(for: index) as? CPDFAnnotation
  339. } else { // 常规模式(非搜索)
  340. let section = self.annotations.safe_element(for: index) as? KMBOTAAnnotationSection
  341. return section?.annotations?.first?.annotation
  342. }
  343. }
  344. @IBAction @objc func sortTypeAction(_ sender: NSMenuItem) {
  345. let item = sender
  346. let tag = item.tag
  347. if (item.state == .on) {
  348. item.state = .off
  349. } else {
  350. item.state = .on
  351. }
  352. if (tag == 0) {
  353. self.noteSortType = .page
  354. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  355. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  356. } else if (tag == 1) {
  357. self.noteSortType = .time
  358. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  359. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  360. }
  361. KMDataManager.ud_set(self.noteSortType.rawValue, forKey: Self.Key.noteSortTypeKey)
  362. self.annotationSort(sortArray: [])
  363. }
  364. }
  365. // MARK: - Note
  366. extension KMLeftSideViewController {
  367. func loadAnnotationSortData(_ morePredicates: NSArray) {
  368. var isLink = false
  369. var typeMutableArr = NSMutableArray()
  370. if self.allAnnotations.count > 0 {
  371. for annotation in self.allAnnotations {
  372. if annotation is CPDFLinkAnnotation || annotation is CPDFTextWidgetAnnotation || annotation is CPDFButtonWidgetAnnotation || annotation is CPDFChoiceWidgetAnnotation {
  373. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature] ||
  374. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeText] ||
  375. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeButton] ||
  376. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeChoice]) {
  377. isLink = true
  378. } else {
  379. if typeMutableArr.contains(annotation.type) == false {
  380. typeMutableArr.add(annotation.type)
  381. }
  382. }
  383. }
  384. }
  385. var colorMutableArray = NSMutableArray()
  386. var typeMutableArray = NSMutableArray()
  387. var authorMutableArray = NSMutableArray()
  388. for annotation in self.allAnnotations {
  389. if annotation is CPDFLinkAnnotation || annotation is CPDFTextWidgetAnnotation || annotation is CPDFButtonWidgetAnnotation || annotation is CPDFChoiceWidgetAnnotation {
  390. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature] ||
  391. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeText] ||
  392. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeButton] ||
  393. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeChoice]) {
  394. let noteColor = annotation.color ?? .clear
  395. let noteType = annotation.type ?? ""
  396. let authorString = annotation.userName() ?? ""
  397. if (noteColor != nil) {
  398. if (colorMutableArray.count > 0) {
  399. if colorMutableArray.contains(noteColor) == false {
  400. colorMutableArray.add(noteColor)
  401. }
  402. } else {
  403. colorMutableArray.add(noteColor)
  404. }
  405. }
  406. if noteType.isEmpty == false {
  407. if typeMutableArray.count > 0 {
  408. if typeMutableArray.contains(noteType) == false {
  409. typeMutableArray.add(noteType)
  410. }
  411. } else {
  412. typeMutableArray.add(noteType)
  413. }
  414. }
  415. if authorString.isEmpty == false {
  416. if authorString.count > 0 {
  417. if authorMutableArray.contains(authorString) == false {
  418. authorMutableArray.add(authorString)
  419. }
  420. } else {
  421. authorMutableArray.add(authorString)
  422. }
  423. }
  424. } else {
  425. if typeMutableArr.contains(annotation.type) == false {
  426. typeMutableArr.add(annotation.type)
  427. }
  428. }
  429. }
  430. let sud = UserDefaults.standard
  431. var typeArr = NSMutableArray()
  432. if let typeData = KMDataManager.ud_object(forKey: NoteFilterVC.filterSelectTypeKey) as? Data {
  433. if let data = NSKeyedUnarchiver.unarchiveObject(with: typeData) as? NSArray {
  434. typeArr = NSMutableArray(array: data)
  435. }
  436. }
  437. var colorArr = NSMutableArray()
  438. if let colorData = sud.object(forKey: NoteFilterVC.filterSelectColorKey) as? Data {
  439. if let data = NSKeyedUnarchiver.unarchiveObject(with: colorData) as? NSArray {
  440. colorArr = NSMutableArray(array: data)
  441. }
  442. }
  443. var authorArr = NSMutableArray()
  444. if let authorData = sud.object(forKey: NoteFilterVC.filterSelectAuthorKey) as? Data {
  445. if let data = NSKeyedUnarchiver.unarchiveObject(with: authorData) as? NSArray {
  446. authorArr = NSMutableArray(array: data)
  447. }
  448. }
  449. var temporaryArr1 = NSMutableArray()
  450. var temporaryArr2 = NSMutableArray()
  451. var temporaryArr3 = NSMutableArray()
  452. if (typeArr.count > 0) {
  453. for type in typeArr {
  454. if typeMutableArray.contains(type) == false {
  455. temporaryArr1.add(type)
  456. }
  457. }
  458. }
  459. if (colorArr.count > 0) {
  460. for color in colorArr {
  461. if colorMutableArray.contains(color) == false {
  462. temporaryArr2.add(color)
  463. }
  464. }
  465. }
  466. if (authorArr.count > 0) {
  467. for author in authorArr {
  468. if authorMutableArray.contains(author) == false {
  469. temporaryArr3.add(author)
  470. }
  471. }
  472. }
  473. if (temporaryArr1.count > 0) {
  474. for type in temporaryArr1 {
  475. typeArr.remove(type)
  476. }
  477. }
  478. if (temporaryArr2.count > 0) {
  479. for color in temporaryArr2 {
  480. colorArr.remove(color)
  481. }
  482. }
  483. if (temporaryArr3.count > 0) {
  484. for author in temporaryArr3 {
  485. authorArr.remove(author)
  486. }
  487. }
  488. var predicateMutableArr = NSMutableArray()
  489. // predicateMutableArr.add
  490. }
  491. func reloadAnnotation() {
  492. if self.listView != nil {
  493. var dataArray: [KMBOTAAnnotationSection] = []
  494. var annotationArray: [CPDFAnnotation] = []
  495. var allAnnotation: [CPDFAnnotation] = []
  496. for i in 0 ..< self.listView.document.pageCount {
  497. var annotationItemArray: [KMBOTAAnnotationItem] = []
  498. let page = self.listView.document.page(at: i)
  499. let types = ["Highlight","Underline","Strikeout","Freehand","FreeText","Note","Square","Circle","Line","Stamp","Arrow","Image","Redact","Sign"]
  500. var pageAnnotations: [CPDFAnnotation] = KMOCToolClass.filterAnnotation(annotations: page!.annotations,types: types) as! [CPDFAnnotation]
  501. //添加签名注释
  502. for annotation in page!.annotations {
  503. if annotation.isKind(of: CPDFSignatureAnnotation.self) {
  504. pageAnnotations.append(annotation)
  505. }
  506. }
  507. for annotation in pageAnnotations {
  508. if annotation.annotationShouldDisplay() == false {
  509. pageAnnotations.removeObject(annotation)
  510. }
  511. }
  512. //转换所有annotation类型
  513. let section = KMBOTAAnnotationSection()
  514. for annotation in pageAnnotations {
  515. let item = KMBOTAAnnotationItem()
  516. item.section = section
  517. item.annotation = annotation
  518. item.index = Int(annotation.page.pageIndex())
  519. annotationItemArray.append(item)
  520. allAnnotation.append(annotation)
  521. }
  522. if annotationItemArray.count != 0 {
  523. section.annotations = annotationItemArray
  524. section.page = page
  525. section.isItemExpanded = true
  526. dataArray.append(section)
  527. }
  528. //添加所有annotation 用于筛选
  529. annotationArray += pageAnnotations
  530. }
  531. if self.noteSortType == .page {
  532. /// 根据id进行排序(升序)
  533. if self.isAscendSort {
  534. dataArray.sort { $0.page!.pageIndex() > $1.page!.pageIndex()}
  535. } else {
  536. dataArray.sort { $0.page!.pageIndex() <= $1.page!.pageIndex()}
  537. }
  538. self.annotations = dataArray
  539. } else if self.noteSortType == .time {
  540. var datas: [KMBOTAAnnotationSection] = []
  541. /// 根据id进行排序(升序)
  542. if self.isAscendSort {
  543. allAnnotation.sort {
  544. if $0.modificationDate() == nil {
  545. return false
  546. }
  547. if $1.modificationDate() == nil {
  548. return false
  549. }
  550. return $0.modificationDate() > $1.modificationDate()
  551. }
  552. } else {
  553. allAnnotation.sort {
  554. if $0.modificationDate() == nil {
  555. return false
  556. }
  557. if $1.modificationDate() == nil {
  558. return false
  559. }
  560. return $0.modificationDate() <= $1.modificationDate()
  561. }
  562. }
  563. for anno in allAnnotation {
  564. let section = KMBOTAAnnotationSection()
  565. let item = KMBOTAAnnotationItem()
  566. item.section = section
  567. item.annotation = anno
  568. item.index = Int(anno.pageIndex())
  569. section.annotations = [item]
  570. section.page = anno.page
  571. section.isItemExpanded = true
  572. datas.append(section)
  573. }
  574. self.annotations = datas
  575. }
  576. //转换对象,用于数据显示
  577. // self._annotations = dataArray
  578. self.allAnnotations = annotationArray
  579. // if self.annotations.count < 1 {
  580. // self.filtrateButton.isEnabled = false
  581. // } else {
  582. // self.filtrateButton.isEnabled = true
  583. // }
  584. }
  585. }
  586. func annotationSort(sortArray:[[Any]]) {
  587. if self.listView != nil {
  588. var typeArr: [Any] = []
  589. var colorArr: [Any] = []
  590. var authorArr: [Any] = []
  591. let sud = UserDefaults.standard
  592. let typeData = KMDataManager.ud_object(forKey: NoteFilterVC.filterSelectTypeKey + self.listView.document.documentURL.path) as? Data
  593. if typeData != nil {
  594. typeArr = NSKeyedUnarchiver.unarchiveObject(with: typeData!) as! [Any]
  595. }
  596. let colorData = sud.object(forKey: NoteFilterVC.filterSelectColorKey + self.listView.document.documentURL.path) as? Data
  597. if colorData != nil {
  598. colorArr = NSKeyedUnarchiver.unarchiveObject(with: colorData!) as! [Any]
  599. }
  600. let authorData = sud.object(forKey: NoteFilterVC.filterSelectAuthorKey + self.listView.document.documentURL.path) as? Data
  601. if authorData != nil {
  602. authorArr = NSKeyedUnarchiver.unarchiveObject(with: authorData!) as! [Any]
  603. }
  604. var colorMutableArray = NSMutableArray()
  605. var typeMutableArray = NSMutableArray()
  606. var authorMutableArray = NSMutableArray()
  607. for annotation in self.allAnnotations {
  608. if annotation.isKind(of: CPDFInkAnnotation.self) == false &&
  609. annotation.isKind(of: CPDFTextWidgetAnnotation.self) == false &&
  610. annotation.isKind(of: CPDFButtonWidgetAnnotation.self) == false &&
  611. annotation.isKind(of: CPDFChoiceWidgetAnnotation.self) == false
  612. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature] &&
  613. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeText] &&
  614. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeButton] &&
  615. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeChoice])
  616. {
  617. let noteColor = annotation.color ?? .clear
  618. let noteType = annotation.type ?? ""
  619. let authorString = annotation.userName() ?? ""
  620. if (noteColor != nil) {
  621. if (colorMutableArray.count > 0) {
  622. if colorMutableArray.contains(noteColor) == false {
  623. colorMutableArray.add(noteColor)
  624. }
  625. } else {
  626. colorMutableArray.add(noteColor)
  627. }
  628. }
  629. if noteType.isEmpty == false {
  630. if (typeMutableArray.count > 0) {
  631. if typeMutableArray.contains(noteType) == false {
  632. typeMutableArray.add(noteType)
  633. }
  634. } else {
  635. typeMutableArray.add(noteType)
  636. }
  637. }
  638. if authorString.isEmpty == false {
  639. if (authorMutableArray.count > 0) {
  640. if authorMutableArray.contains(authorString) == false {
  641. authorMutableArray.add(authorString)
  642. }
  643. } else {
  644. authorMutableArray.add(authorString)
  645. }
  646. }
  647. }
  648. }
  649. // var temporaryArr1 = NSMutableArray()
  650. // var temporaryArr2 = NSMutableArray()
  651. // var temporaryArr3 = NSMutableArray()
  652. // if typeArr.isEmpty == false {
  653. // for type in typeArr {
  654. // if typeMutableArray.contains(type) == false {
  655. // temporaryArr1.add(type)
  656. // }
  657. // }
  658. // }
  659. // if colorArr.isEmpty == false {
  660. // for color in colorArr {
  661. // if colorMutableArray.contains(color) == false {
  662. // temporaryArr2.add(type)
  663. // }
  664. // }
  665. // }
  666. // if authorArr.isEmpty == false {
  667. // for author in authorArr {
  668. // if authorMutableArray.contains(author) == false {
  669. // temporaryArr3.add(type)
  670. // }
  671. // }
  672. // }
  673. // if (temporaryArr1.count > 0) {
  674. // for type in temporaryArr1 {
  675. // guard let typeS = type as? String else {
  676. // continue
  677. // }
  678. //// [typeArr removeObject:type];
  679. //
  680. // var flag = -1
  681. // for (i, data) in typeArr.enumerated() {
  682. // if data as! String == typeS {
  683. // flag = i
  684. // }
  685. // }
  686. // if flag != -1 {
  687. // typeArr.remove(at: flag)
  688. // }
  689. // }
  690. // }
  691. // if (temporaryArr2.count > 0) {
  692. // for (NSColor *color in temporaryArr2) {
  693. // [colorArr removeObject:color];
  694. // }
  695. // }
  696. // for color in temporaryArr2 {
  697. // guard let colorT = color as? NSColor else {
  698. // continue
  699. // }
  700. //// [typeArr removeObject:type];
  701. //
  702. // var flag = -1
  703. // for (i, data) in colorArr.enumerated() {
  704. // if data as! NSColor == colorT {
  705. // flag = i
  706. // }
  707. // }
  708. // if flag != -1 {
  709. // colorArr.remove(at: flag)
  710. // }
  711. // }
  712. // if (temporaryArr3.count > 0) {
  713. // for (NSColor *author in temporaryArr3) {
  714. // [authorArr removeObject:author];
  715. // }
  716. // }
  717. // for author in temporaryArr3 {
  718. // guard let authorS = author as? String else {
  719. // continue
  720. // }
  721. //// [typeArr removeObject:type];
  722. //
  723. // var flag = -1
  724. // for (i, data) in authorArr.enumerated() {
  725. // if data as! String == authorS {
  726. // flag = i
  727. // }
  728. // }
  729. // if flag != -1 {
  730. // authorArr.remove(at: flag)
  731. // }
  732. // }
  733. // let data1 = NSKeyedArchiver.archivedData(withRootObject: NSArray(array: typeArr))
  734. // let data2 = NSKeyedArchiver.archivedData(withRootObject: NSArray(array: colorArr))
  735. // let data3 = NSKeyedArchiver.archivedData(withRootObject: NSArray(array: authorArr))
  736. // let sud = UserDefaults.standard
  737. // sud.set(data1, forKey: "KMNoteOutlineFilterSelectArray_Type")
  738. // sud.set(data2, forKey: "KMNoteOutlineFilterSelectArray_Color")
  739. // sud.set(data3, forKey: "KMNoteOutlineFilterSelectArray_Author")
  740. // sud.synchronize()
  741. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  742. // self.filtrateButton.image = NSImage(named: "KMImageNameAnnotationsFiltrate")
  743. self.reloadAnnotation()
  744. } else {
  745. // self.filtrateButton.image = NSImage(named: "icon_annotation_screening_select")
  746. var dataArray: [KMBOTAAnnotationSection] = []
  747. var allAnnotation: [CPDFAnnotation] = []
  748. for i in 0 ..< self.listView.document.pageCount {
  749. var annotationItemArray: [KMBOTAAnnotationItem] = []
  750. let page = self.listView.document.page(at: i)
  751. if page!.annotations.count > 0 {
  752. var filterAnnotations: [CPDFAnnotation] = page!.annotations
  753. if typeArr.count > 0 {
  754. filterAnnotations = (KMOCToolClass.filterAnnotation(annotations: filterAnnotations, types: typeArr) as! [CPDFAnnotation])
  755. }
  756. if (colorArr.count > 0) {
  757. filterAnnotations = (KMOCToolClass.filterAnnotation(annotations: filterAnnotations,colors: colorArr) as! [CPDFAnnotation])
  758. }
  759. if (authorArr.count > 0) {
  760. filterAnnotations = (KMOCToolClass.filterAnnotation(annotations: filterAnnotations,authors: authorArr) as! [CPDFAnnotation])
  761. }
  762. let section = KMBOTAAnnotationSection()
  763. for annotation in filterAnnotations {
  764. let item = KMBOTAAnnotationItem()
  765. item.section = section
  766. item.annotation = annotation
  767. item.index = Int(page!.pageIndex())
  768. annotationItemArray.append(item)
  769. allAnnotation.append(annotation)
  770. }
  771. if annotationItemArray.count != 0 {
  772. section.annotations = annotationItemArray
  773. section.page = page
  774. section.isItemExpanded = true
  775. dataArray.append(section)
  776. }
  777. }
  778. }
  779. if self.noteSortType == .page {
  780. /// 根据id进行排序(升序)
  781. if self.isAscendSort {
  782. dataArray.sort {
  783. if $0.page == nil {
  784. return false
  785. }
  786. if $1.page == nil {
  787. return false
  788. }
  789. return $0.page!.pageIndex() > $1.page!.pageIndex()
  790. }
  791. } else {
  792. dataArray.sort {
  793. if $0.page == nil {
  794. return false
  795. }
  796. if $1.page == nil {
  797. return false
  798. }
  799. return $0.page!.pageIndex() <= $1.page!.pageIndex()
  800. }
  801. }
  802. self.annotations = dataArray
  803. } else if self.noteSortType == .time {
  804. var datas: [KMBOTAAnnotationSection] = []
  805. /// 根据id进行排序(升序)
  806. if self.isAscendSort {
  807. allAnnotation.sort {
  808. if $0.modificationDate() == nil {
  809. return false
  810. }
  811. if $1.modificationDate() == nil {
  812. return false
  813. }
  814. return $0.modificationDate() > $1.modificationDate()
  815. }
  816. } else {
  817. allAnnotation.sort {
  818. if $0.modificationDate() == nil {
  819. return false
  820. }
  821. if $1.modificationDate() == nil {
  822. return false
  823. }
  824. return $0.modificationDate() <= $1.modificationDate()
  825. }
  826. }
  827. for anno in allAnnotation {
  828. let section = KMBOTAAnnotationSection()
  829. let item = KMBOTAAnnotationItem()
  830. item.section = section
  831. item.annotation = anno
  832. item.index = Int(anno.pageIndex())
  833. section.annotations = [item]
  834. section.page = anno.page
  835. section.isItemExpanded = true
  836. datas.append(section)
  837. }
  838. self.annotations = datas
  839. }
  840. }
  841. Task { @MainActor in
  842. self.noteOutlineView.reloadData()
  843. }
  844. }
  845. }
  846. func updateNoteFilterPredicate() {
  847. //注释筛选
  848. // [rightSideController.noteArrayController setFilterPredicate:[noteTypeSheetController filterPredicateForSearchString:[rightSideController.searchField stringValue] caseInsensitive:mwcFlags.caseInsensitiveNoteSearch]];
  849. var stringValue = self.noteSearchField.stringValue
  850. if self.caseInsensitiveNoteSearch {
  851. stringValue = stringValue.lowercased()
  852. }
  853. // NSPredicate *predicate = [noteTypeSheetController filterPredicateForSearchString:[rightSideController.searchField stringValue] caseInsensitive:mwcFlags.caseInsensitiveNoteSearch];
  854. // [self loadAnnotationSortData:[NSArray arrayWithObjects:predicate, nil]];
  855. self.noteSearchArray.removeAll()
  856. if stringValue.isEmpty {
  857. self.noteSearchArray.removeAll()
  858. for section in self.annotations {
  859. guard let note = section.annotations?.first?.annotation else {
  860. continue
  861. }
  862. self.noteSearchArray.append(note)
  863. }
  864. } else {
  865. for section in self.annotations {
  866. guard let note = section.annotations?.first?.annotation else {
  867. continue
  868. }
  869. var noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  870. if self.caseInsensitiveNoteSearch {
  871. noteString = noteString.lowercased()
  872. }
  873. if let anno = note as? CPDFMarkupAnnotation {
  874. noteString = anno.markupContent()
  875. }
  876. if noteString.contains(stringValue) {
  877. self.noteSearchArray.append(note)
  878. }
  879. }
  880. }
  881. Task { @MainActor in
  882. self.noteOutlineView.reloadData()
  883. }
  884. }
  885. @objc func selectSelectedNote(_ sender: AnyObject?) {
  886. if self.listView.hideNotes == false {
  887. let selectedNotes = self.selectedNotes()
  888. if selectedNotes.count == 1 {
  889. let annotation = selectedNotes.last!
  890. self.listView.go(to: annotation.bounds, on: annotation.page, animated: true)
  891. // [pdfView scrollAnnotationToVisible:annotation];
  892. // [pdfView setActiveAnnotation:annotation];
  893. self.listView.updateActiveAnnotations([annotation])
  894. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  895. // }
  896. }
  897. // NSInteger column = [sender clickedColumn];
  898. // if (column != -1) {
  899. // NSString *colID = [[[sender tableColumns] objectAtIndex:column] identifier];
  900. //
  901. // if ([colID isEqualToString:@"color"]){
  902. // for (PDFAnnotation *annotation in self.pdfView.activeAnnotations) {
  903. // if (![annotation isKindOfClass:[PDFAnnotationChoiceWidget class]] &&
  904. // ![annotation isKindOfClass:[PDFAnnotationButtonWidget class]] &&
  905. // ![annotation isKindOfClass:[PDFAnnotationTextWidget class]]) {
  906. // [[NSColorPanel sharedColorPanel] orderFront:nil];
  907. // break;
  908. // }
  909. //
  910. // }
  911. // }
  912. // }
  913. }
  914. }
  915. func selectedNotes() -> [CPDFAnnotation] {
  916. var selectedNotes: [CPDFAnnotation] = []
  917. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  918. for row in rowIndexes {
  919. let item = self.noteOutlineView.item(atRow: row)
  920. if item is KMBOTAAnnotationItem {
  921. if let anno = (item as! KMBOTAAnnotationItem).annotation {
  922. // if anno.type == nil {
  923. // item = [(SKNoteText *)item note];
  924. // }
  925. if selectedNotes.contains(anno) == false {
  926. selectedNotes.append(anno)
  927. }
  928. }
  929. }
  930. }
  931. return selectedNotes
  932. }
  933. func clearAnnotationFilterData() {
  934. if let _key = self.listView?.document?.documentURL?.path {
  935. let userDefaults = UserDefaults.standard
  936. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  937. userDefaults.set(typeData, forKey: NoteFilterVC.filterSelectTypeKey + _key)
  938. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  939. userDefaults.set(colorData, forKey: NoteFilterVC.filterSelectColorKey + _key)
  940. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  941. userDefaults.set(authorData, forKey: NoteFilterVC.filterSelectAuthorKey + _key)
  942. userDefaults.synchronize()
  943. }
  944. }
  945. }