KMLeftSideViewController+Note.swift 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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) ?? .none
  124. if (self.noteSortType == .time) {
  125. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  126. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  127. } else if (self.noteSortType == .page) {
  128. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  129. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  130. }
  131. } else {
  132. self.noteSortType = .time
  133. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  134. }
  135. self.sortTypeLabel.textColor = KMAppearance.Layout.h1Color()
  136. self.noteOutlineView.backgroundColor = KMAppearance.Layout.l0Color()
  137. self.noteOutlineView.autoresizesOutlineColumn = false
  138. self.noteOutlineView.indentationPerLevel = 0
  139. }
  140. @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 {
  535. let idx0 = $0.page?.pageIndex() ?? 0
  536. let idx1 = $1.page?.pageIndex() ?? 0
  537. return idx0 > idx1
  538. }
  539. } else {
  540. dataArray.sort {
  541. let idx0 = $0.page?.pageIndex() ?? 0
  542. let idx1 = $1.page?.pageIndex() ?? 0
  543. return idx0 <= idx1
  544. }
  545. }
  546. self.annotations = dataArray
  547. } else if self.noteSortType == .time {
  548. var datas: [KMBOTAAnnotationSection] = []
  549. /// 根据id进行排序(升序)
  550. if self.isAscendSort {
  551. allAnnotation.sort {
  552. if $0.modificationDate() == nil {
  553. return false
  554. }
  555. if $1.modificationDate() == nil {
  556. return false
  557. }
  558. return $0.modificationDate() > $1.modificationDate()
  559. }
  560. } else {
  561. allAnnotation.sort {
  562. if $0.modificationDate() == nil {
  563. return false
  564. }
  565. if $1.modificationDate() == nil {
  566. return false
  567. }
  568. return $0.modificationDate() <= $1.modificationDate()
  569. }
  570. }
  571. for anno in allAnnotation {
  572. let section = KMBOTAAnnotationSection()
  573. let item = KMBOTAAnnotationItem()
  574. item.section = section
  575. item.annotation = anno
  576. item.index = Int(anno.pageIndex())
  577. section.annotations = [item]
  578. section.page = anno.page
  579. section.isItemExpanded = true
  580. datas.append(section)
  581. }
  582. self.annotations = datas
  583. }
  584. //转换对象,用于数据显示
  585. // self._annotations = dataArray
  586. self.allAnnotations = annotationArray
  587. // if self.annotations.count < 1 {
  588. // self.filtrateButton.isEnabled = false
  589. // } else {
  590. // self.filtrateButton.isEnabled = true
  591. // }
  592. }
  593. }
  594. func annotationSort(sortArray:[[Any]]) {
  595. if self.listView != nil {
  596. var typeArr: [Any] = []
  597. var colorArr: [Any] = []
  598. var authorArr: [Any] = []
  599. let sud = UserDefaults.standard
  600. let typeData = KMDataManager.ud_object(forKey: NoteFilterVC.filterSelectTypeKey + self.listView.document.documentURL.path) as? Data
  601. if typeData != nil {
  602. typeArr = NSKeyedUnarchiver.unarchiveObject(with: typeData!) as? [Any] ?? []
  603. }
  604. let colorData = sud.object(forKey: NoteFilterVC.filterSelectColorKey + self.listView.document.documentURL.path) as? Data
  605. if colorData != nil {
  606. colorArr = NSKeyedUnarchiver.unarchiveObject(with: colorData!) as? [Any] ?? []
  607. }
  608. let authorData = sud.object(forKey: NoteFilterVC.filterSelectAuthorKey + self.listView.document.documentURL.path) as? Data
  609. if authorData != nil {
  610. authorArr = NSKeyedUnarchiver.unarchiveObject(with: authorData!) as? [Any] ?? []
  611. }
  612. var colorMutableArray = NSMutableArray()
  613. var typeMutableArray = NSMutableArray()
  614. var authorMutableArray = NSMutableArray()
  615. for annotation in self.allAnnotations {
  616. if annotation.isKind(of: CPDFInkAnnotation.self) == false &&
  617. annotation.isKind(of: CPDFTextWidgetAnnotation.self) == false &&
  618. annotation.isKind(of: CPDFButtonWidgetAnnotation.self) == false &&
  619. annotation.isKind(of: CPDFChoiceWidgetAnnotation.self) == false
  620. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature] &&
  621. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeText] &&
  622. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeButton] &&
  623. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeChoice])
  624. {
  625. let noteColor = annotation.color ?? .clear
  626. let noteType = annotation.type ?? ""
  627. let authorString = annotation.userName() ?? ""
  628. if (noteColor != nil) {
  629. if (colorMutableArray.count > 0) {
  630. if colorMutableArray.contains(noteColor) == false {
  631. colorMutableArray.add(noteColor)
  632. }
  633. } else {
  634. colorMutableArray.add(noteColor)
  635. }
  636. }
  637. if noteType.isEmpty == false {
  638. if (typeMutableArray.count > 0) {
  639. if typeMutableArray.contains(noteType) == false {
  640. typeMutableArray.add(noteType)
  641. }
  642. } else {
  643. typeMutableArray.add(noteType)
  644. }
  645. }
  646. if authorString.isEmpty == false {
  647. if (authorMutableArray.count > 0) {
  648. if authorMutableArray.contains(authorString) == false {
  649. authorMutableArray.add(authorString)
  650. }
  651. } else {
  652. authorMutableArray.add(authorString)
  653. }
  654. }
  655. }
  656. }
  657. // var temporaryArr1 = NSMutableArray()
  658. // var temporaryArr2 = NSMutableArray()
  659. // var temporaryArr3 = NSMutableArray()
  660. // if typeArr.isEmpty == false {
  661. // for type in typeArr {
  662. // if typeMutableArray.contains(type) == false {
  663. // temporaryArr1.add(type)
  664. // }
  665. // }
  666. // }
  667. // if colorArr.isEmpty == false {
  668. // for color in colorArr {
  669. // if colorMutableArray.contains(color) == false {
  670. // temporaryArr2.add(type)
  671. // }
  672. // }
  673. // }
  674. // if authorArr.isEmpty == false {
  675. // for author in authorArr {
  676. // if authorMutableArray.contains(author) == false {
  677. // temporaryArr3.add(type)
  678. // }
  679. // }
  680. // }
  681. // if (temporaryArr1.count > 0) {
  682. // for type in temporaryArr1 {
  683. // guard let typeS = type as? String else {
  684. // continue
  685. // }
  686. //// [typeArr removeObject:type];
  687. //
  688. // var flag = -1
  689. // for (i, data) in typeArr.enumerated() {
  690. // if data as! String == typeS {
  691. // flag = i
  692. // }
  693. // }
  694. // if flag != -1 {
  695. // typeArr.remove(at: flag)
  696. // }
  697. // }
  698. // }
  699. // if (temporaryArr2.count > 0) {
  700. // for (NSColor *color in temporaryArr2) {
  701. // [colorArr removeObject:color];
  702. // }
  703. // }
  704. // for color in temporaryArr2 {
  705. // guard let colorT = color as? NSColor else {
  706. // continue
  707. // }
  708. //// [typeArr removeObject:type];
  709. //
  710. // var flag = -1
  711. // for (i, data) in colorArr.enumerated() {
  712. // if data as! NSColor == colorT {
  713. // flag = i
  714. // }
  715. // }
  716. // if flag != -1 {
  717. // colorArr.remove(at: flag)
  718. // }
  719. // }
  720. // if (temporaryArr3.count > 0) {
  721. // for (NSColor *author in temporaryArr3) {
  722. // [authorArr removeObject:author];
  723. // }
  724. // }
  725. // for author in temporaryArr3 {
  726. // guard let authorS = author as? String else {
  727. // continue
  728. // }
  729. //// [typeArr removeObject:type];
  730. //
  731. // var flag = -1
  732. // for (i, data) in authorArr.enumerated() {
  733. // if data as! String == authorS {
  734. // flag = i
  735. // }
  736. // }
  737. // if flag != -1 {
  738. // authorArr.remove(at: flag)
  739. // }
  740. // }
  741. // let data1 = NSKeyedArchiver.archivedData(withRootObject: NSArray(array: typeArr))
  742. // let data2 = NSKeyedArchiver.archivedData(withRootObject: NSArray(array: colorArr))
  743. // let data3 = NSKeyedArchiver.archivedData(withRootObject: NSArray(array: authorArr))
  744. // let sud = UserDefaults.standard
  745. // sud.set(data1, forKey: "KMNoteOutlineFilterSelectArray_Type")
  746. // sud.set(data2, forKey: "KMNoteOutlineFilterSelectArray_Color")
  747. // sud.set(data3, forKey: "KMNoteOutlineFilterSelectArray_Author")
  748. // sud.synchronize()
  749. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  750. // self.filtrateButton.image = NSImage(named: "KMImageNameAnnotationsFiltrate")
  751. self.reloadAnnotation()
  752. } else {
  753. // self.filtrateButton.image = NSImage(named: "icon_annotation_screening_select")
  754. var dataArray: [KMBOTAAnnotationSection] = []
  755. var allAnnotation: [CPDFAnnotation] = []
  756. for i in 0 ..< self.listView.document.pageCount {
  757. var annotationItemArray: [KMBOTAAnnotationItem] = []
  758. guard let page = self.listView.document.page(at: i) else {
  759. continue
  760. }
  761. if page.annotations.count > 0 {
  762. var filterAnnotations: [CPDFAnnotation] = page.annotations
  763. if typeArr.count > 0 {
  764. filterAnnotations = (KMOCToolClass.filterAnnotation(annotations: filterAnnotations, types: typeArr) as? [CPDFAnnotation]) ?? []
  765. }
  766. if (colorArr.count > 0) {
  767. filterAnnotations = (KMOCToolClass.filterAnnotation(annotations: filterAnnotations,colors: colorArr) as? [CPDFAnnotation]) ?? []
  768. }
  769. if (authorArr.count > 0) {
  770. filterAnnotations = (KMOCToolClass.filterAnnotation(annotations: filterAnnotations,authors: authorArr) as? [CPDFAnnotation]) ?? []
  771. }
  772. let section = KMBOTAAnnotationSection()
  773. for annotation in filterAnnotations {
  774. let item = KMBOTAAnnotationItem()
  775. item.section = section
  776. item.annotation = annotation
  777. item.index = Int(page.pageIndex())
  778. annotationItemArray.append(item)
  779. allAnnotation.append(annotation)
  780. }
  781. if annotationItemArray.count != 0 {
  782. section.annotations = annotationItemArray
  783. section.page = page
  784. section.isItemExpanded = true
  785. dataArray.append(section)
  786. }
  787. }
  788. }
  789. if self.noteSortType == .page {
  790. /// 根据id进行排序(升序)
  791. if self.isAscendSort {
  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. } else {
  802. dataArray.sort {
  803. if $0.page == nil {
  804. return false
  805. }
  806. if $1.page == nil {
  807. return false
  808. }
  809. return $0.page!.pageIndex() <= $1.page!.pageIndex()
  810. }
  811. }
  812. self.annotations = dataArray
  813. } else if self.noteSortType == .time {
  814. var datas: [KMBOTAAnnotationSection] = []
  815. /// 根据id进行排序(升序)
  816. if self.isAscendSort {
  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. } else {
  827. allAnnotation.sort {
  828. if $0.modificationDate() == nil {
  829. return false
  830. }
  831. if $1.modificationDate() == nil {
  832. return false
  833. }
  834. return $0.modificationDate() <= $1.modificationDate()
  835. }
  836. }
  837. for anno in allAnnotation {
  838. let section = KMBOTAAnnotationSection()
  839. let item = KMBOTAAnnotationItem()
  840. item.section = section
  841. item.annotation = anno
  842. item.index = Int(anno.pageIndex())
  843. section.annotations = [item]
  844. section.page = anno.page
  845. section.isItemExpanded = true
  846. datas.append(section)
  847. }
  848. self.annotations = datas
  849. }
  850. }
  851. Task { @MainActor in
  852. self.noteOutlineView.reloadData()
  853. }
  854. }
  855. }
  856. func updateNoteFilterPredicate() {
  857. //注释筛选
  858. // [rightSideController.noteArrayController setFilterPredicate:[noteTypeSheetController filterPredicateForSearchString:[rightSideController.searchField stringValue] caseInsensitive:mwcFlags.caseInsensitiveNoteSearch]];
  859. var stringValue = self.noteSearchField.stringValue
  860. if self.caseInsensitiveNoteSearch {
  861. stringValue = stringValue.lowercased()
  862. }
  863. // NSPredicate *predicate = [noteTypeSheetController filterPredicateForSearchString:[rightSideController.searchField stringValue] caseInsensitive:mwcFlags.caseInsensitiveNoteSearch];
  864. // [self loadAnnotationSortData:[NSArray arrayWithObjects:predicate, nil]];
  865. self.noteSearchArray.removeAll()
  866. if stringValue.isEmpty {
  867. self.noteSearchArray.removeAll()
  868. for section in self.annotations {
  869. guard let note = section.annotations?.first?.annotation else {
  870. continue
  871. }
  872. self.noteSearchArray.append(note)
  873. }
  874. } else {
  875. for section in self.annotations {
  876. guard let note = section.annotations?.first?.annotation else {
  877. continue
  878. }
  879. var noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  880. if self.caseInsensitiveNoteSearch {
  881. noteString = noteString.lowercased()
  882. }
  883. if let anno = note as? CPDFMarkupAnnotation {
  884. noteString = anno.markupContent()
  885. }
  886. if noteString.contains(stringValue) {
  887. self.noteSearchArray.append(note)
  888. }
  889. }
  890. }
  891. Task { @MainActor in
  892. self.noteOutlineView.reloadData()
  893. }
  894. }
  895. @objc func selectSelectedNote(_ sender: AnyObject?) {
  896. if self.listView.hideNotes == false {
  897. let selectedNotes = self.selectedNotes()
  898. if selectedNotes.count == 1 {
  899. let annotation = selectedNotes.last!
  900. self.listView.go(to: annotation.bounds, on: annotation.page, animated: true)
  901. // [pdfView scrollAnnotationToVisible:annotation];
  902. // [pdfView setActiveAnnotation:annotation];
  903. self.listView.updateActiveAnnotations([annotation])
  904. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  905. // }
  906. }
  907. // NSInteger column = [sender clickedColumn];
  908. // if (column != -1) {
  909. // NSString *colID = [[[sender tableColumns] objectAtIndex:column] identifier];
  910. //
  911. // if ([colID isEqualToString:@"color"]){
  912. // for (PDFAnnotation *annotation in self.pdfView.activeAnnotations) {
  913. // if (![annotation isKindOfClass:[PDFAnnotationChoiceWidget class]] &&
  914. // ![annotation isKindOfClass:[PDFAnnotationButtonWidget class]] &&
  915. // ![annotation isKindOfClass:[PDFAnnotationTextWidget class]]) {
  916. // [[NSColorPanel sharedColorPanel] orderFront:nil];
  917. // break;
  918. // }
  919. //
  920. // }
  921. // }
  922. // }
  923. }
  924. }
  925. func selectedNotes() -> [CPDFAnnotation] {
  926. var selectedNotes: [CPDFAnnotation] = []
  927. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  928. for row in rowIndexes {
  929. let item = self.noteOutlineView.item(atRow: row)
  930. if item is KMBOTAAnnotationItem {
  931. if let anno = (item as! KMBOTAAnnotationItem).annotation {
  932. // if anno.type == nil {
  933. // item = [(SKNoteText *)item note];
  934. // }
  935. if selectedNotes.contains(anno) == false {
  936. selectedNotes.append(anno)
  937. }
  938. }
  939. }
  940. }
  941. return selectedNotes
  942. }
  943. func clearAnnotationFilterData() {
  944. if let _key = self.listView?.document?.documentURL?.path {
  945. let userDefaults = UserDefaults.standard
  946. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  947. userDefaults.set(typeData, forKey: NoteFilterVC.filterSelectTypeKey + _key)
  948. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  949. userDefaults.set(colorData, forKey: NoteFilterVC.filterSelectColorKey + _key)
  950. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  951. userDefaults.set(authorData, forKey: NoteFilterVC.filterSelectAuthorKey + _key)
  952. userDefaults.synchronize()
  953. }
  954. }
  955. }