KMLeftSideViewController+Note.swift 45 KB

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