KMLeftSideViewController+Note.swift 52 KB

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