KMLeftSideViewController+Note.swift 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. //
  2. // KMLeftSideViewController+Note.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/23.
  6. //
  7. import Foundation
  8. extension KMLeftSideViewController.Key {
  9. static let noteAscendSortKey = "KMLeftSideViewAscendSortBoolKey"
  10. static let noteSortTypeKey = "KMLeftSideViewNoteSortTypeKey"
  11. static let noteTableColumn = "KMNoteOutlineViewTableColumnKey"
  12. static let noteFilterPage = "kKMNoteFilterAnnotationPageKey"
  13. static let noteFilterTime = "kKMNoteFilterAnnotationTimeKey"
  14. static let noteFilterAuther = "kKMNoteFilterAnnotationAutherKey"
  15. }
  16. // MARK: - Action
  17. extension KMLeftSideViewController {
  18. func note_initSubViews() {
  19. self.noteSearchField.backgroundColor = KMAppearance.Layout.l_1Color()
  20. self.noteSearchField.wantsLayer = true
  21. self.noteSearchField.layer?.backgroundColor = KMAppearance.Layout.l_1Color().cgColor
  22. self.noteSearchField.layer?.borderWidth = 1.0
  23. self.noteMoreButton.target = self
  24. self.noteMoreButton.tag = 304
  25. self.noteMoreButton.action = #selector(leftSideViewMoreButtonAction)
  26. self.moreButtonLayer = KMButtonLayer()
  27. self.noteMoreButton.layer?.addSublayer(self.moreButtonLayer!)
  28. self.moreButtonLayer?.frame = NSMakeRect(0, 0, NSWidth(self.noteMoreButton.bounds), NSHeight(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 = NSMakeRect(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 = { [weak 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: NSMakePoint(-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. func annoListIsShowPage() -> Bool {
  141. return !(self.noteTypeDict[Self.Key.noteFilterPage] as? Bool ?? false)
  142. }
  143. func annoListIsShowTime() -> Bool {
  144. return !(self.noteTypeDict[Self.Key.noteFilterTime] as? Bool ?? false)
  145. }
  146. func annoListIsShowAnther() -> Bool {
  147. return !(self.noteTypeDict[Self.Key.noteFilterAuther] as? Bool ?? false)
  148. }
  149. }
  150. // MARK: - Menu
  151. extension KMLeftSideViewController {
  152. func annoListMenu(_ menu: NSMenu) {
  153. var item: NSMenuItem?
  154. var items: NSArray?
  155. var rowIndexes = self.noteOutlineView.selectedRowIndexes
  156. let row = self.noteOutlineView.clickedRow
  157. if row == -1 {
  158. _ = self._addExportPDFMenu(menu)
  159. _ = self._addDeleteAllAnnoItem(menu)
  160. return
  161. }
  162. if rowIndexes.contains(row) == false {
  163. rowIndexes = IndexSet(integer: row)
  164. }
  165. items = self.noteOutlineView.itemsAtRowIndexes(rowIndexes) as NSArray
  166. guard let model = self.fetchAnnoModel(for: row) else {
  167. return
  168. }
  169. let isFold = model.isFold()
  170. item = menu.addItem(title: KMLocalizedString("Expand", nil), action: #selector(unfoldNoteAction), target: self)
  171. item?.state = isFold ? .off : .on
  172. item?.representedObject = items
  173. item = menu.addItem(title: KMLocalizedString("Collapse", nil), action: #selector(foldNoteAction), target: self)
  174. item?.state = isFold ? .on : .off
  175. item?.representedObject = items
  176. menu.addItem(.separator())
  177. let hideNotes = self.hideNotes()
  178. if hideNotes == false && (items?.count ?? 0) == 1 {
  179. let annotation = self.noteItems(items!).lastObject as? CPDFAnnotation
  180. if let data = annotation?.isEditable(), data {
  181. if annotation?.type == nil {
  182. let isNote = annotation?.isNote() ?? false
  183. if isNote {
  184. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  185. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteTextFromTable), target: self)
  186. item?.representedObject = annotation
  187. }
  188. } else if let data = self.noteOutlineView.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier("note"))?.isHidden, data {
  189. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  190. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  191. item?.representedObject = annotation
  192. } else {
  193. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteFromTable), target: self)
  194. item?.representedObject = annotation
  195. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  196. item?.representedObject = annotation
  197. item?.keyEquivalentModifierMask = [.option]
  198. item?.isAlternate = true
  199. }
  200. }
  201. }
  202. if menu.numberOfItems > 0 {
  203. _ = self._addExportPDFMenu(menu)
  204. menu.addItem(.separator())
  205. if self.outlineView(self.noteOutlineView, canDeleteItems: items as? [Any] ?? []) {
  206. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteNotes), target: self)
  207. item?.representedObject = items
  208. }
  209. _ = self._addDeleteAllAnnoItem(menu)
  210. }
  211. }
  212. private func _addExportPDFMenu(_ menu: NSMenu) -> NSMenu {
  213. var item = menu.addItem(title: NSLocalizedString("Export Annotations…", tableName: "", comment: ""), action: nil, target: self)
  214. let subMenu = NSMenu()
  215. item?.submenu = subMenu
  216. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  217. item?.tag = 0
  218. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  219. item?.tag = 1
  220. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  221. item?.tag = 2
  222. item = subMenu.addItem(title: NSLocalizedString("Notes as Text", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  223. item?.tag = 3
  224. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  225. item?.tag = 4
  226. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  227. item?.tag = 5
  228. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  229. item?.tag = 6
  230. return menu
  231. }
  232. private func _addDeleteAllAnnoItem(_ menu: NSMenu) -> NSMenuItem? {
  233. return menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "", comment: ""), action: #selector(removeAllAnnotations), target: self)
  234. }
  235. private func _addDeleteAllReplyAnnoItem(_ menu: NSMenu) -> NSMenuItem? {
  236. return menu.addItem(title: NSLocalizedString("删除所有注释回复", tableName: "", comment: ""), action: #selector(removeAllReplyAnnotations), target: self)
  237. }
  238. func annoListMoreMenu(_ view: NSButton) {
  239. let menu = NSMenu()
  240. let object = KMPopupMenuObject()
  241. object.menuTag = 1001
  242. menu.delegate = object
  243. object.enterControllerCallback = { [weak self] isEnter in
  244. if (isEnter) {
  245. self?.moreButtonLayer?.isHidden = false
  246. } else {
  247. self?.moreButtonLayer?.isHidden = true
  248. }
  249. }
  250. let expandAllItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(note_expandAllComments), target: self)
  251. expandAllItem?.representedObject = self.noteOutlineView
  252. let foldAllItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(note_foldAllComments), target: self)
  253. foldAllItem?.representedObject = self.noteOutlineView
  254. let type = self.annoListModel?.foldType ?? .none
  255. expandAllItem?.state = type == .unfold ? .on : .off
  256. foldAllItem?.state = type == .fold ? .on : .off
  257. let showItem = menu.addItem(title: KMLocalizedString("Show Note", nil), action: nil, target: self)
  258. let subMenu = NSMenu()
  259. let pageItem = subMenu.addItem(title: KMLocalizedString("Page", nil), action: #selector(noteShowNoteAction), target: self)
  260. pageItem?.state = self.annoListIsShowPage() ? .on : .off
  261. pageItem?.representedObject = self.noteOutlineView
  262. pageItem?.tag = 101
  263. let timeItem = subMenu.addItem(title: KMLocalizedString("Time", nil) , action: #selector(noteShowNoteAction), target: self)
  264. timeItem?.state = self.annoListIsShowTime() ? .on : .off
  265. timeItem?.representedObject = self.noteOutlineView
  266. timeItem?.tag = 102
  267. let authorItem = subMenu.addItem(title: KMLocalizedString("Author", nil) , action: #selector(noteShowNoteAction), target: self)
  268. authorItem?.state = self.annoListIsShowAnther() ? .on : .off
  269. authorItem?.representedObject = self.noteOutlineView
  270. authorItem?.tag = 103
  271. showItem?.submenu = subMenu
  272. menu.addItem(.separator())
  273. _ = self._addExportPDFMenu(menu)
  274. menu.addItem(.separator())
  275. _ = self._addDeleteAllAnnoItem(menu)
  276. _ = self._addDeleteAllReplyAnnoItem(menu)
  277. menu.addItem(.separator())
  278. let importItem = NSMenuItem(title: NSLocalizedString("导入注释", comment: ""), action: #selector(importNotes), keyEquivalent: "")
  279. importItem.target = self
  280. menu.addItem(importItem)
  281. let exportItem = NSMenuItem(title: NSLocalizedString("导出注释", comment: ""), action: #selector(exportNotes), keyEquivalent: "")
  282. exportItem.target = self
  283. menu.addItem(exportItem)
  284. if let data = NSApp.currentEvent {
  285. NSMenu.popUpContextMenu(menu, with: data, for: view, with: nil)
  286. }
  287. }
  288. func annoListValidateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  289. let action = menuItem.action
  290. if (action == #selector(note_expandAllComments) ||
  291. action == #selector(note_foldAllComments) ||
  292. action == #selector(exportAnnotationNotes) ||
  293. action == #selector(removeAllAnnotations)) {
  294. let cnt = self.annoListModel?.datas.count ?? 0
  295. return cnt > 0
  296. } else if (action == #selector(unfoldNoteAction) ||
  297. action == #selector(foldNoteAction)) {
  298. let row = self.noteOutlineView.clickedRow
  299. let foldNote = self.fetchNote(for: row)
  300. // SKNPDFAnnotationNote
  301. if foldNote is CPDFMarkupAnnotation || foldNote is CPDFTextAnnotation {
  302. return true
  303. } else {
  304. return false
  305. }
  306. } else if (action == #selector(editNoteFromTable)) {
  307. let row = self.noteOutlineView.clickedRow
  308. let foldNote = self.fetchNote(for: row)
  309. // if (@available(macOS 10.13, *)) {
  310. // if ([foldNote.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  311. // return NO;
  312. // }
  313. // }
  314. if foldNote is CPDFStampAnnotation || foldNote is KMAnnotationStamp || foldNote is CPDFListStampAnnotation {
  315. return false
  316. } else {
  317. return true
  318. }
  319. }
  320. return true
  321. }
  322. @IBAction func note_expandAllComments(_ sender: AnyObject?) {
  323. guard let model = self.annoListModel else {
  324. return
  325. }
  326. if (model.foldType == .unfold) { // 已全部展开
  327. return
  328. }
  329. model.foldType = .unfold
  330. self.noteOutlineView.reloadData()
  331. }
  332. @IBAction func note_foldAllComments(_ sender: AnyObject?) {
  333. guard let model = self.annoListModel else {
  334. return
  335. }
  336. if (model.foldType == .fold) {
  337. return
  338. }
  339. model.foldType = .fold
  340. self.noteOutlineView.reloadData()
  341. }
  342. @IBAction func noteShowNoteAction(_ sender: AnyObject?) {
  343. let item = sender as? NSMenuItem
  344. let tag = item?.tag ?? 0
  345. if (tag == 100) {
  346. } else if (tag == 101) {
  347. let isPage = !self.annoListIsShowPage()
  348. self.noteTypeDict[Self.Key.noteFilterPage] = !isPage
  349. } else if (tag == 102) {
  350. let isTime = !self.annoListIsShowTime()
  351. self.noteTypeDict[Self.Key.noteFilterTime] = !isTime
  352. } else if (tag == 103) {
  353. let isAuther = !self.annoListIsShowAnther()
  354. self.noteTypeDict[Self.Key.noteFilterAuther] = !isAuther
  355. }
  356. UserDefaults.standard.sync_setValue(self.noteTypeDict, forKey: Self.Key.noteTableColumn)
  357. // 更新数据
  358. var models: [KMBotaAnnotationModel] = []
  359. if self.noteSearchMode {
  360. models = self.noteSearchArray
  361. } else {
  362. let selModels = self.annoListModel?.datas ?? []
  363. for selModel in selModels {
  364. for item in selModel.items {
  365. if let data = item as? KMBotaAnnotationModel {
  366. models.append(data)
  367. }
  368. }
  369. }
  370. }
  371. for model in models {
  372. model.showPage = self.annoListIsShowPage()
  373. model.showTime = self.annoListIsShowTime()
  374. model.showAuthor = self.annoListIsShowAnther()
  375. }
  376. let selectRow = self.noteOutlineView.selectedRow
  377. self.noteOutlineView.reloadData()
  378. self.noteOutlineView.selectRowIndexes(IndexSet(integer: selectRow), byExtendingSelection: false)
  379. }
  380. @objc func importNotes(_ sender: NSMenuItem) {
  381. let panel = NSOpenPanel()
  382. panel.allowedFileTypes = ["xfdf"]
  383. panel.allowsMultipleSelection = false
  384. panel.beginSheetModal(for: self.view.window!) { resp in
  385. if resp != .OK {
  386. return
  387. }
  388. if let result = self.pdfDocument()?.importAnnotation(fromXFDFPath: panel.url?.path), result {
  389. self.reloadAnnotation()
  390. self.listView?.setNeedsDisplayForVisiblePages()
  391. }
  392. }
  393. }
  394. @objc func exportNotes(_ sender: NSMenuItem) {
  395. guard let cnt = self.listView?.notes.count, cnt > 0 else {
  396. NSSound.beep()
  397. return
  398. }
  399. let fileName = "\(self.pdfDocument()?.documentURL.deletingPathExtension().lastPathComponent ?? "")" + "_xfdf"
  400. let panel = NSSavePanel()
  401. panel.directoryURL = self.pdfDocument()?.documentURL.deletingLastPathComponent()
  402. panel.allowedFileTypes = ["xfdf"]
  403. panel.nameFieldStringValue = fileName
  404. panel.beginSheetModal(for: self.view.window!) { resp in
  405. if resp != .OK {
  406. return
  407. }
  408. let filePath = panel.url?.path
  409. if let success = self.pdfDocument()?.exportAnnotation(toXFDFPath: filePath), success {
  410. NSWorkspace.shared.selectFile(filePath, inFileViewerRootedAtPath: "")
  411. } else {
  412. Task {
  413. _ = await KMAlertTool.runModel(message: NSLocalizedString("Export Failure!", comment: ""), buttons: ["OK"])
  414. }
  415. }
  416. }
  417. }
  418. @objc func exportAnnotationNotes(_ sender: AnyObject?) {
  419. let doc = self.view.window?.windowController?.document as? NSDocument
  420. doc?.saveTo(sender)
  421. }
  422. // 展开
  423. @objc func unfoldNoteAction(_ sender: NSMenuItem) {
  424. if sender.state == .on {
  425. return
  426. }
  427. let row = self.noteOutlineView.clickedRow
  428. guard let model = self.fetchAnnoModel(for: row) else {
  429. return
  430. }
  431. model.foldType = .unfold
  432. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  433. (viewS as? KMNoteTableViewCell)?.isFold = false
  434. }
  435. @objc func foldNoteAction(_ sender: NSMenuItem) {
  436. if sender.state == .on {
  437. return
  438. }
  439. let row = self.noteOutlineView.clickedRow
  440. guard let model = self.fetchAnnoModel(for: row) else {
  441. return
  442. }
  443. model.foldType = .fold
  444. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  445. (viewS as? KMNoteTableViewCell)?.isFold = true
  446. }
  447. @objc func deleteNotes(_ sender: NSMenuItem) {
  448. self.outlineView(self.noteOutlineView, deleteItems: sender.representedObject as? [Any] ?? [])
  449. }
  450. @objc func removeAllAnnotations(_ sender: AnyObject?) {
  451. guard let doc = self.pdfDocument() else {
  452. return
  453. }
  454. Task {
  455. 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)])
  456. if response == .alertFirstButtonReturn {
  457. // var annos: [CPDFAnnotation] = []
  458. // for i in 0 ..< doc.pageCount {
  459. // let page = self.pdfDocument()?.page(at: i)
  460. // for anno in page?.annotations ?? [] {
  461. // if anno is CPDFTextWidgetAnnotation || anno is CPDFButtonWidgetAnnotation || anno is CPDFChoiceWidgetAnnotation {
  462. // continue
  463. // }
  464. // // if ([annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  465. // // continue;
  466. // // }
  467. // if anno is CPDFLinkAnnotation {
  468. // continue
  469. // }
  470. // annos.append(anno)
  471. // }
  472. // }
  473. self.dataUpdating = true
  474. // for anno in annos {
  475. // self.listView?.remove(anno)
  476. // }
  477. for model in self.annoListModel?.datas ?? [] {
  478. for item in model.items {
  479. if let anno = item.anno {
  480. self.listView?.remove(anno)
  481. }
  482. }
  483. }
  484. self.annoListModel?.datas.removeAll()
  485. self.dataUpdating = false
  486. self.note_refrshUIIfNeed()
  487. }
  488. }
  489. }
  490. @objc func removeAllReplyAnnotations(_ sender: NSMenuItem?) {
  491. self.dataUpdating = true
  492. for model in self.annoListModel?.datas ?? [] {
  493. for item in model.items {
  494. // if let anno = item.anno {
  495. // self.listView?.remove(anno)
  496. // }
  497. guard let annoM = item as? KMBotaAnnotationModel else {
  498. continue
  499. }
  500. for replyM in annoM.replyAnnos {
  501. // self.listView?.remove(replyM.replyAnno)
  502. replyM.replyAnno?.page.removeAnnotation(replyM.replyAnno)
  503. }
  504. annoM.replyAnnos.removeAll()
  505. }
  506. }
  507. self.dataUpdating = false
  508. self.note_refrshUIIfNeed()
  509. }
  510. @objc func editNoteTextFromTable(_ sender: NSMenuItem) {
  511. // PDFAnnotation *annotation = [sender representedObject];
  512. guard let annotation = sender.representedObject as? CPDFAnnotation else {
  513. return
  514. }
  515. self.listView?.scrollAnnotationToVisible(annotation)
  516. // self.listView.activeAnnotation = annotation
  517. // [self showNote:annotation];
  518. // SKNoteWindowController *noteController = (SKNoteWindowController *)[self windowControllerForNote:annotation];
  519. // [[noteController window] makeFirstResponder:[noteController textView]];
  520. // [[noteController textView] selectAll:nil];
  521. }
  522. @objc func editThisAnnotation(_ sender: AnyObject?) {
  523. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  524. NSSound.beep()
  525. return
  526. }
  527. self.listView?.edit(annotation)
  528. }
  529. @objc func editNoteFromTable(_ sender: AnyObject?) {
  530. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  531. NSSound.beep()
  532. return
  533. }
  534. let model = fetchAnnoModel(for: annotation)
  535. let row = self.noteOutlineView.row(forItem: model)
  536. self.noteOutlineView.km_safe_selectRowIndexes(.init(integer: row), byExtendingSelection: false)
  537. let noteIndex = self.noteOutlineView.column(withIdentifier: .init("note"))
  538. if (noteIndex >= 0) {
  539. self.noteOutlineView.scrollColumnToVisible(noteIndex)
  540. //
  541. self.isRenameNoteOutline = true
  542. // self.renamePDFOutline = [rightSideController.noteOutlineView itemAtRow:rightSideController.noteOutlineView.clickedRow];
  543. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true) as? KMNoteTableViewCell
  544. viewS!.isFold = false
  545. let targrtTextField = viewS?.noteContentLabel
  546. self.editNoteTextField = targrtTextField
  547. self.editNote = annotation
  548. targrtTextField?.delegate = self
  549. targrtTextField?.isEditable = true
  550. targrtTextField?.becomeFirstResponder()
  551. }
  552. }
  553. @IBAction func noteSortAction(_ sender: AnyObject?) {
  554. if (self.isAscendSort) {
  555. self.isAscendSort = false
  556. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  557. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  558. } else {
  559. self.isAscendSort = true
  560. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  561. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  562. }
  563. KMDataManager.ud_set(self.isAscendSort, forKey: Self.Key.noteAscendSortKey)
  564. if self.noteSearchMode {
  565. self.reloadNoteForSearchMode()
  566. } else {
  567. self.reloadAnnotation()
  568. }
  569. }
  570. @IBAction func noteSearchAction(_ sender: NSButton) {
  571. self.noteSearchField.isHidden = false
  572. self.noteTitleLabel.isHidden = true
  573. self.noteSearchButton.isHidden = true
  574. self.noteDoneButton.isHidden = false
  575. self.noteFilterButton.isHidden = true
  576. self.noteMoreButton.isHidden = true
  577. self.noteSearchField.becomeFirstResponder()
  578. }
  579. @IBAction func noteFilterAction(_ sender: AnyObject?) {
  580. let button = sender as? NSButton
  581. let menu = NSMenu()
  582. let filterViewController = KMNoteOutlineFilterViewController()
  583. filterViewController.listView = self.listView
  584. filterViewController.view.layer?.backgroundColor = .clear
  585. var states: [CPDFAnnotationState] = [.none, .unMarked]
  586. for anno in self.allAnnotations {
  587. if let reviewS = self.noteReplyHanddler.fetchReviewState(anno) {
  588. if states.contains(reviewS) == false {
  589. states.append(reviewS)
  590. }
  591. }
  592. if let markS = self.noteReplyHanddler.fetchAnnoState(anno) {
  593. if states.contains(markS) == false {
  594. states.append(markS)
  595. }
  596. }
  597. }
  598. filterViewController.updateStates(states: states)
  599. filterViewController.setNotesArray(self.allAnnotations as NSArray)
  600. filterViewController.applyFilterCallback = { [weak self] typeArr, colorArr, authorArr, isEmpty in
  601. menu.cancelTracking()
  602. if (isEmpty) {
  603. self?.filterButtonLayer?.isHidden = true
  604. } else {
  605. self?.filterButtonLayer?.isHidden = false
  606. }
  607. self?.reloadAnnotation()
  608. }
  609. filterViewController.cancelCallback = { isCancel in
  610. if (isCancel) {
  611. menu.cancelTracking()
  612. }
  613. }
  614. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  615. item.target = self
  616. item.representedObject = filterViewController
  617. item.view = filterViewController.view
  618. menu.popUp(positioning: nil, at: NSMakePoint(-130, 30), in: button)
  619. // let win = KMNoteOutlineFilterViewController_window(contentRect: .zero, styleMask: .borderless, backing: .buffered, defer: false)
  620. // win.center()
  621. // win.contentViewController = filterViewController
  622. // win.orderFront(nil)
  623. }
  624. func fetchNote(for index: Int) -> CPDFAnnotation? {
  625. return self.fetchAnnoModel(for: index)?.anno
  626. }
  627. func fetchAnnoModel(for index: Int) -> KMBotaAnnotationModel? {
  628. if self.noteSearchMode { // 搜索模式
  629. return self.noteSearchArray.safe_element(for: index) as? KMBotaAnnotationModel
  630. } else { // 常规模式(非搜索)
  631. // let model = self.annoListModel?.datas.safe_element(for: index)
  632. let model = self.noteOutlineView.item(atRow: index)
  633. if let data = model as? KMBotaAnnotationSectionModel {
  634. return nil
  635. }
  636. if let data = model as? KMBotaAnnotationModel {
  637. return data
  638. }
  639. if let data = model as? KMBotaAnnotationFooterModel {
  640. // return data.annoModel
  641. }
  642. if let data = model as? KMBotaAnnotationReplyModel {
  643. // return data.annoModel
  644. }
  645. // return self.annoListModel?.datas.safe_element(for: index) as? KMBotaAnnotationModel
  646. return nil
  647. }
  648. }
  649. func fetchAnnoModel(for anno: CPDFAnnotation) -> KMBotaAnnotationModel? {
  650. if self.noteSearchMode { // 搜索模式
  651. for model in self.noteSearchArray {
  652. if anno.isEqual(to: model.anno) {
  653. return model
  654. }
  655. }
  656. } else { // 常规模式(非搜索)
  657. for model in self.annoListModel?.datas ?? [] {
  658. for item in model.items {
  659. if let data = item as? KMBotaAnnotationModel {
  660. if anno.isEqual(to: data.anno) {
  661. return data
  662. }
  663. }
  664. }
  665. }
  666. }
  667. return nil
  668. }
  669. @IBAction @objc func sortTypeAction(_ sender: NSMenuItem) {
  670. let item = sender
  671. let tag = item.tag
  672. if (item.state == .on) {
  673. item.state = .off
  674. } else {
  675. item.state = .on
  676. }
  677. if (tag == 0) {
  678. self.noteSortType = .page
  679. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  680. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  681. } else if (tag == 1) {
  682. self.noteSortType = .time
  683. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  684. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  685. }
  686. KMDataManager.ud_set(self.noteSortType.rawValue, forKey: Self.Key.noteSortTypeKey)
  687. if self.noteSearchMode {
  688. self.reloadNoteForSearchMode()
  689. } else {
  690. self.reloadAnnotation()
  691. }
  692. }
  693. func showNoteEmptyView() {
  694. let view = self.noteOutlineView.enclosingScrollView?.documentView
  695. let viewFrame = view?.frame ?? .zero
  696. let emptyVcSize = self.leftSideEmptyVC.emptyAnnotationView.frame.size
  697. self.leftSideEmptyVC.emptyAnnotationView.frame = NSMakeRect((viewFrame.size.width-emptyVcSize.width)/2.0,(viewFrame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  698. self.leftSideEmptyVC.emptyAnnotationView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  699. self.noteOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.emptyAnnotationView)
  700. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = false
  701. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = false
  702. if (self.leftView.segmentedControl.selectedSegment == KMSelectedSegmentType.annotation.rawValue) {
  703. self.noteHeaderView.isHidden = true
  704. self.toolButtonBoxLayoutConstraint.constant = 40.0
  705. }
  706. }
  707. func hideNoteEmptyView() {
  708. self.leftSideEmptyVC.emptyAnnotationView.removeFromSuperview()
  709. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = true
  710. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = true
  711. if (self.leftView.segmentedControl.selectedSegment == 3) {
  712. self.noteHeaderView.isHidden = false
  713. self.toolButtonBoxLayoutConstraint.constant = 64.0
  714. }
  715. }
  716. }
  717. // MARK: - Note
  718. extension KMLeftSideViewController {
  719. public func refreshUIForAddAnnotation(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  720. let need = self._annoList_needRefreshUI(annos: annos)
  721. if need == false {
  722. return
  723. }
  724. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  725. self.note_reloadDataIfNeed()
  726. }
  727. public func refreshUIForAnnoAttributeDidChange(_ anno: CPDFAnnotation?, attributes: [String : Any]?) {
  728. self.updateThumbnail(at: Int(anno?.page?.pageIndex() ?? 0))
  729. let need = self._annoList_needRefreshUI(annos: anno != nil ? [anno!] : [])
  730. if need == false {
  731. return
  732. }
  733. if let data = anno {
  734. self.note_reloadDataForAnnoIfNeed(anno: data)
  735. }
  736. }
  737. public func annoList_refreshUIForDeleteAnnotations(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  738. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  739. if self.type.methodType != .Annotation {
  740. return
  741. }
  742. let need = self._annoList_needRefreshUI(annos: annos)
  743. if need == false {
  744. return
  745. }
  746. for anno in annos ?? [] {
  747. if let model = self.fetchAnnoModel(for: anno) {
  748. self.noteSearchArray.removeObject(model)
  749. // self.annoListModel?.datas.removeObject(model)
  750. if self.allAnnotations.contains(anno) {
  751. self.allAnnotations.removeObject(anno)
  752. }
  753. }
  754. }
  755. if self.dataUpdating == false {
  756. self.note_refrshUIIfNeed()
  757. }
  758. }
  759. func note_refrshUIIfNeed() {
  760. if self.type.methodType != .Annotation {
  761. return
  762. }
  763. Task { @MainActor in
  764. self.noteOutlineView.reloadData()
  765. }
  766. }
  767. func note_reloadDataIfNeed() {
  768. if self.type.methodType != .Annotation {
  769. return
  770. }
  771. self.reloadAnnotation()
  772. }
  773. func note_reloadDataForAnnoIfNeed(anno: CPDFAnnotation) {
  774. if self.type.methodType != .Annotation {
  775. return
  776. }
  777. if anno is CPDFLineAnnotation || anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation || anno is CPDFInkAnnotation {
  778. // 形状注释 + Ink 需要显示框住的内容【刷新】
  779. for item in self.annoListModel?.datas ?? [] {
  780. for itemM in item.items {
  781. if anno.isEqual(to: itemM.anno) {
  782. self.noteOutlineView.reloadItem(itemM)
  783. break
  784. }
  785. }
  786. }
  787. } else {
  788. for item in self.annoListModel?.datas ?? [] {
  789. for itemM in item.items {
  790. if anno.isEqual(to: itemM.anno) {
  791. self.noteOutlineView.reloadItem(itemM)
  792. break
  793. }
  794. }
  795. }
  796. }
  797. }
  798. func reloadAnnotation() {
  799. if self.listView != nil {
  800. let filterKey = self.pdfDocument()?.documentURL.path ?? ""
  801. let typeArr: [String] = KMBotaTools.noteFilterAnnoTypes(key: filterKey)
  802. let colorArr: [Any] = KMBotaTools.noteFilterColors(key: filterKey)
  803. let authorArr: [Any] = KMBotaTools.noteFilterAuthors(key: filterKey)
  804. let stateArr: [NSNumber] = KMBotaTools.noteFilterStates(key: filterKey)
  805. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 && stateArr.count == 0 {
  806. // self.filtrateButton.image = NSImage(named: "KMImageNameAnnotationsFiltrate")
  807. self.filterButtonLayer?.isHidden = true
  808. } else {
  809. // self.filtrateButton.image = NSImage(named: "icon_annotation_screening_select")self.filterButtonLayer?.isHidden = true
  810. self.filterButtonLayer?.isHidden = false
  811. }
  812. var annotationArray: [CPDFAnnotation] = []
  813. var allAnnotation: [CPDFAnnotation] = []
  814. for i in 0 ..< self.pageCount() {
  815. let page = self.pdfDocument()?.page(at: UInt(i))
  816. var annos: [CPDFAnnotation] = []
  817. // 处理过滤
  818. let types = ["Highlight","Underline","Strikeout","Squiggly","Freehand","FreeText","Note","Square","Circle","Line","Stamp","Arrow","Image","Redact","Sign"/*, "table"*/,"Polyline","Polygon"]
  819. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 && stateArr.count == 0 {
  820. annos = KMOCToolClass.filterAnnotation(annotations: page?.annotations ?? [],types: types) as? [CPDFAnnotation] ?? []
  821. annotationArray += annos
  822. } else {
  823. var filterAnnos: [CPDFAnnotation] = page?.annotations ?? []
  824. let allAnnos = KMOCToolClass.filterAnnotation(annotations: filterAnnos,types: types) as? [CPDFAnnotation] ?? []
  825. annotationArray += allAnnos
  826. if typeArr.count > 0 {
  827. var theTypes = typeArr
  828. if typeArr.contains(CPDFAnnotation.kType.measureArrow) && typeArr.contains(CPDFAnnotation.kType.arrow) == false {
  829. theTypes.append(CPDFAnnotation.kType.arrow)
  830. }
  831. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos, types: theTypes) as? [CPDFAnnotation]) ?? []
  832. }
  833. if (colorArr.count > 0) {
  834. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,colors: colorArr) as? [CPDFAnnotation]) ?? []
  835. }
  836. if (authorArr.count > 0) {
  837. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,authors: authorArr) as? [CPDFAnnotation]) ?? []
  838. }
  839. if typeArr.contains(CPDFAnnotation.kType.measureArrow) {
  840. if typeArr.contains(CPDFAnnotation.kType.arrow) == false {
  841. for anno in filterAnnos {
  842. if let data = anno as? CPDFLineAnnotation, data.type == CPDFAnnotation.kType.arrow && data.isMeasure == false {
  843. filterAnnos.removeObject(anno)
  844. }
  845. }
  846. }
  847. } else {
  848. for anno in filterAnnos {
  849. if let data = anno as? CPDFLineAnnotation, data.isMeasure {
  850. filterAnnos.removeObject(anno)
  851. }
  852. }
  853. }
  854. if stateArr.count > 0 {
  855. for anno in filterAnnos {
  856. let markState = self.noteReplyHanddler.fetchAnnoState(anno) ?? .unMarked
  857. let reviewState = self.noteReplyHanddler.fetchReviewState(anno) ?? .none
  858. if stateArr.contains(NSNumber(value: markState.rawValue)) == false || stateArr.contains(NSNumber(value: reviewState.rawValue)) == false {
  859. filterAnnos.removeObject(anno)
  860. }
  861. }
  862. }
  863. annos = filterAnnos
  864. }
  865. //添加签名注释
  866. for annotation in page?.annotations ?? [] {
  867. if annotation.isKind(of: CPDFSignatureAnnotation.self) {
  868. annos.append(annotation)
  869. annotationArray.append(annotation)
  870. }
  871. }
  872. for annotation in annos {
  873. if annotation.isKind(of: KMTableAnnotation.self) {
  874. annos.removeObject(annotation)
  875. if annotationArray.contains(annotation) {
  876. annotationArray.removeObject(annotation)
  877. }
  878. } else if annotation.annotationShouldDisplay() == false {
  879. annos.removeObject(annotation)
  880. if annotationArray.contains(annotation) {
  881. annotationArray.removeObject(annotation)
  882. }
  883. } else if annotation.isKind(of: CPDFLinkAnnotation.self) {
  884. annos.removeObject(annotation)
  885. if annotationArray.contains(annotation) {
  886. annotationArray.removeObject(annotation)
  887. }
  888. }
  889. }
  890. // 添加刷选后的注释
  891. allAnnotation += annos
  892. //添加所有annotation 用于筛选
  893. // annotationArray += (page?.annotations ?? [])
  894. // annotationArray += annos
  895. }
  896. // 处理排序
  897. if self.noteSortType == .page {
  898. /// 排序(升序)
  899. if self.isAscendSort {
  900. allAnnotation.sort {
  901. let idx0 = $0.page?.pageIndex() ?? 0
  902. let idx1 = $1.page?.pageIndex() ?? 0
  903. return idx0 <= idx1
  904. }
  905. } else {
  906. allAnnotation.sort {
  907. let idx0 = $0.page?.pageIndex() ?? 0
  908. let idx1 = $1.page?.pageIndex() ?? 0
  909. return idx0 > idx1
  910. }
  911. }
  912. } else if self.noteSortType == .time {
  913. /// 排序(升序)
  914. if self.isAscendSort {
  915. allAnnotation.sort {
  916. if $0.modificationDate() == nil {
  917. return false
  918. }
  919. if $1.modificationDate() == nil {
  920. return false
  921. }
  922. return $0.modificationDate() <= $1.modificationDate()
  923. }
  924. } else {
  925. allAnnotation.sort {
  926. if $0.modificationDate() == nil {
  927. return false
  928. }
  929. if $1.modificationDate() == nil {
  930. return false
  931. }
  932. return $0.modificationDate() > $1.modificationDate()
  933. }
  934. }
  935. }
  936. // 数据模型\化
  937. let model = KMAnnotationListModel()
  938. var datas: [KMBotaAnnotationModel] = []
  939. var prePageIdx: Int = NSNotFound
  940. var preDate: Date?
  941. var secM: KMBotaAnnotationSectionModel?
  942. for anno in allAnnotation {
  943. let item = KMBotaAnnotationModel()
  944. item.anno = anno
  945. item.showPage = self.annoListIsShowPage()
  946. item.showTime = self.annoListIsShowTime()
  947. item.showAuthor = self.annoListIsShowAnther()
  948. // datas.append(item)
  949. if self.noteSortType == .page {
  950. let pageIdx = Int(anno.pageIndex())
  951. if pageIdx != prePageIdx { // 不是同一个页面
  952. secM = KMBotaAnnotationSectionModel()
  953. model.datas.append(secM!)
  954. }
  955. secM?.items.append(item)
  956. prePageIdx = Int(anno.pageIndex())
  957. } else { // time
  958. let date = anno.modificationDate()
  959. if let same = date?.isSameDay(other: preDate), same == false { // 不是同一天
  960. secM = KMBotaAnnotationSectionModel()
  961. model.datas.append(secM!)
  962. }
  963. secM?.items.append(item)
  964. preDate = date
  965. }
  966. item.sectionModel = secM
  967. let replyAnnos = self.noteReplyHanddler.fetchReplyAnnotations(anno) ?? []
  968. for replyAnno in replyAnnos {
  969. let replyM = KMBotaAnnotationReplyModel()
  970. replyM.anno = anno
  971. replyM.replyAnno = replyAnno
  972. // secM?.items.append(replyM)
  973. replyM.annoModel = item
  974. item.replyAnnos.append(replyM)
  975. }
  976. let footerI = KMBotaAnnotationFooterModel()
  977. footerI.anno = anno
  978. secM?.items.append(footerI)
  979. item.footerModel = footerI
  980. footerI.annoModel = item
  981. }
  982. // model.datas = datas
  983. self.annoListModel = model
  984. // 转换对象,用于数据显示
  985. self.allAnnotations = annotationArray
  986. self.noteFilterButton.isEnabled = self.allAnnotations.count >= 1
  987. }
  988. self.note_refrshUIIfNeed()
  989. }
  990. func reloadNoteForSearchMode() {
  991. if self.noteSearchMode == false {
  992. return
  993. }
  994. // 处理排序
  995. if self.noteSortType == .page {
  996. if self.isAscendSort { /// 排序(升序)
  997. self.noteSearchArray.sort {
  998. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  999. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  1000. return idx0 <= idx1
  1001. }
  1002. } else {
  1003. self.noteSearchArray.sort {
  1004. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  1005. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  1006. return idx0 > idx1
  1007. }
  1008. }
  1009. } else if self.noteSortType == .time {
  1010. if self.isAscendSort { /// 排序(升序)
  1011. self.noteSearchArray.sort {
  1012. if $0.anno?.modificationDate() == nil {
  1013. return false
  1014. }
  1015. if $1.anno?.modificationDate() == nil {
  1016. return false
  1017. }
  1018. return $0.anno!.modificationDate() <= $1.anno!.modificationDate()
  1019. }
  1020. } else {
  1021. self.noteSearchArray.sort {
  1022. if $0.anno?.modificationDate() == nil {
  1023. return false
  1024. }
  1025. if $1.anno?.modificationDate() == nil {
  1026. return false
  1027. }
  1028. return $0.anno!.modificationDate() > $1.anno!.modificationDate()
  1029. }
  1030. }
  1031. }
  1032. self.note_refrshUIIfNeed()
  1033. }
  1034. // 搜索 Action
  1035. func updateNoteFilterPredicate() {
  1036. var stringValue = self.noteSearchField.stringValue
  1037. // 清空数据
  1038. self.noteSearchArray.removeAll()
  1039. if stringValue.isEmpty {
  1040. for model in self.annoListModel?.datas ?? [] {
  1041. for item in model.items {
  1042. guard let _ = item.anno else {
  1043. continue
  1044. }
  1045. guard let data = item as? KMBotaAnnotationModel else {
  1046. continue
  1047. }
  1048. self.noteSearchArray.append(data)
  1049. }
  1050. }
  1051. } else {
  1052. // 忽略大小写
  1053. let caseInsensite = self.caseInsensitiveNoteSearch
  1054. if caseInsensite {
  1055. stringValue = stringValue.lowercased()
  1056. }
  1057. for model in self.annoListModel?.datas ?? [] {
  1058. for item in model.items {
  1059. guard let note = item.anno else {
  1060. continue
  1061. }
  1062. var noteString = ""
  1063. if let anno = note as? CPDFMarkupAnnotation {
  1064. noteString = anno.markupContent()
  1065. } else {
  1066. noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  1067. }
  1068. if caseInsensite {
  1069. noteString = noteString.lowercased()
  1070. }
  1071. guard let data = item as? KMBotaAnnotationModel else {
  1072. continue
  1073. }
  1074. if noteString.contains(stringValue) {
  1075. self.noteSearchArray.append(data)
  1076. }
  1077. }
  1078. }
  1079. }
  1080. self.note_refrshUIIfNeed()
  1081. }
  1082. @objc func selectSelectedNote(_ sender: AnyObject?) {
  1083. if self.hideNotes() == false {
  1084. let selectedNotes = self.selectedNotes()
  1085. if selectedNotes.count == 1 {
  1086. let annotation = selectedNotes.last!
  1087. self.listView?.go(to: annotation.bounds, on: annotation.page, animated: true)
  1088. // [pdfView scrollAnnotationToVisible:annotation];
  1089. // [pdfView setActiveAnnotation:annotation];
  1090. self.listView?.updateActiveAnnotations([annotation])
  1091. self.listView?.setNeedsDisplayAnnotationViewForVisiblePages()
  1092. if annotation is CPDFPolygonAnnotation || annotation is CPDFPolylineAnnotation {
  1093. self.listView?.pdfListViewDelegate.pdfListViewAnnotationMeasureInfoChange?(self.listView, with: annotation)
  1094. } else if let anno = annotation as? CPDFLineAnnotation {
  1095. if anno.isMeasure {
  1096. self.listView?.pdfListViewDelegate.pdfListViewAnnotationMeasureInfoChange?(self.listView, with: annotation)
  1097. }
  1098. }
  1099. // }
  1100. }
  1101. // NSInteger column = [sender clickedColumn];
  1102. // if (column != -1) {
  1103. // NSString *colID = [[[sender tableColumns] objectAtIndex:column] identifier];
  1104. //
  1105. // if ([colID isEqualToString:@"color"]){
  1106. // for (PDFAnnotation *annotation in self.pdfView.activeAnnotations) {
  1107. // if (![annotation isKindOfClass:[PDFAnnotationChoiceWidget class]] &&
  1108. // ![annotation isKindOfClass:[PDFAnnotationButtonWidget class]] &&
  1109. // ![annotation isKindOfClass:[PDFAnnotationTextWidget class]]) {
  1110. // [[NSColorPanel sharedColorPanel] orderFront:nil];
  1111. // break;
  1112. // }
  1113. //
  1114. // }
  1115. // }
  1116. // }
  1117. }
  1118. }
  1119. func selectedNotes() -> [CPDFAnnotation] {
  1120. var selectedNotes: [CPDFAnnotation] = []
  1121. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  1122. for row in rowIndexes {
  1123. let item = self.noteOutlineView.item(atRow: row)
  1124. if item is KMBotaAnnotationModel {
  1125. if let anno = (item as! KMBotaAnnotationModel).anno {
  1126. // if anno.type == nil {
  1127. // item = [(SKNoteText *)item note];
  1128. // }
  1129. if selectedNotes.contains(anno) == false {
  1130. selectedNotes.append(anno)
  1131. }
  1132. }
  1133. }
  1134. }
  1135. return selectedNotes
  1136. }
  1137. func clearAnnotationFilterData() {
  1138. if let _key = self.pdfDocument()?.documentURL?.path {
  1139. let userDefaults = UserDefaults.standard
  1140. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1141. userDefaults.set(typeData, forKey: NoteFilterVC.filterSelectTypeKey + _key)
  1142. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1143. userDefaults.set(colorData, forKey: NoteFilterVC.filterSelectColorKey + _key)
  1144. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1145. userDefaults.set(authorData, forKey: NoteFilterVC.filterSelectAuthorKey + _key)
  1146. userDefaults.synchronize()
  1147. }
  1148. }
  1149. private func _annoList_needRefreshUI(annos: [CPDFAnnotation]?) -> Bool {
  1150. guard let data = annos else {
  1151. return false
  1152. }
  1153. // for anno in data {
  1154. // if anno.isKind(of: KMTableAnnotation.self) == false {
  1155. // return true
  1156. // }
  1157. // }
  1158. return true
  1159. }
  1160. }