KMLeftSideViewController+Note.swift 43 KB

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