KMLeftSideViewController+Note.swift 51 KB

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