KMLeftSideViewController+Note.swift 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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 = CGRectMake(0, 0, CGRectGetWidth(self.noteMoreButton.bounds), CGRectGetHeight(self.noteMoreButton.bounds))
  29. self.noteFilterButton.target = self
  30. self.noteFilterButton.action = #selector(noteFilterAction)
  31. self.filterButtonLayer = NSView()
  32. self.noteFilterButton.addSubview(self.filterButtonLayer!)
  33. self.filterButtonLayer?.frame = CGRectMake(14, 2, 8, 8)
  34. self.noteDoneButton.action = #selector(leftSideViewDoneButtonAction)
  35. self.noteDoneButton.target = self
  36. self.noteDoneButton.tag = 311
  37. self.noteDoneButton.isHidden = true
  38. self.noteSearchField.delegate = self
  39. self.noteSearchField.isHidden = true
  40. self.noteSearchField.endEditCallBack = { [unowned self] isEndEdit in
  41. // if (isEndEdit) {
  42. // self.noteSearchField.isHidden = true
  43. // self.noteSearchButton.isHidden = false
  44. // self.noteTitleLabel.isHidden = false
  45. // }
  46. }
  47. self.sortTypeBox.downCallback = { [unowned self] downEntered, mouseBox, _ in
  48. if (downEntered) {
  49. let menu = NSMenu()
  50. let timeItem = menu.addItem(title: KMLocalizedString("Time", nil), action: #selector(sortTypeAction), target: self)
  51. timeItem?.representedObject = self
  52. timeItem?.tag = 0
  53. let pageItem = menu.addItem(title: KMLocalizedString("Page", nil), action: #selector(sortTypeAction), target: self)
  54. pageItem?.representedObject = self
  55. timeItem?.tag = 1
  56. if (self.noteSortType == .time) {
  57. timeItem?.state = .on
  58. pageItem?.state = .off
  59. } else if (self.noteSortType == .page) {
  60. timeItem?.state = .off
  61. pageItem?.state = .on
  62. }
  63. menu.popUp(positioning: nil, at: CGPointMake(-10, 0), in: self.sortTypeBox)
  64. }
  65. }
  66. self.noteOutlineView.delegate = self
  67. self.noteOutlineView.dataSource = self
  68. self.noteOutlineView.botaDelegate = self
  69. self.noteOutlineView.botaDataSource = self
  70. self.noteOutlineView.noteDelegate = self
  71. self.noteOutlineView.menu = NSMenu()
  72. self.noteOutlineView.menu?.delegate = self
  73. self.noteOutlineView.typeSelectHelper = SKTypeSelectHelper(matchOption: .SKSubstringMatch)
  74. self.noteOutlineView.registerForDraggedTypes(NSColor.readableTypes(for: NSPasteboard(name: .drag)))
  75. self.noteOutlineView.target = self
  76. self.noteOutlineView.doubleAction = #selector(selectSelectedNote)
  77. }
  78. func note_initDefalutValue() {
  79. self.noteView.wantsLayer = true
  80. self.noteView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  81. let sud = UserDefaults.standard
  82. if let dict = sud.dictionary(forKey: Self.Key.noteTableColumn) {
  83. self.noteTypeDict = dict
  84. } else {
  85. self.noteTypeDict = [Self.Key.noteFilterPage : false,
  86. Self.Key.noteFilterTime : false,
  87. Self.Key.noteFilterAuther : false]
  88. sud.sync_setValue(self.noteTypeDict, forKey: Self.Key.noteTableColumn)
  89. }
  90. self.caseInsensitiveNoteSearch = sud.bool(forKey: SKCaseInsensitiveNoteSearchKey)
  91. self.isAscendSort = KMDataManager.ud_bool(forKey: Self.Key.noteAscendSortKey)
  92. self.noteTitleLabel.stringValue = KMLocalizedString("Notes", nil);
  93. self.noteTitleLabel.textColor = KMAppearance.Layout.h0Color()
  94. self.noteSearchField.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  95. self.noteMoreButton.wantsLayer = true
  96. self.moreButtonLayer?.layerType = .none
  97. self.moreButtonLayer?.isHidden = true
  98. self.noteFilterButton.toolTip = KMLocalizedString("Sort", nil)
  99. self.noteFilterButton.wantsLayer = true
  100. self.filterButtonLayer?.isHidden = true
  101. self.filterButtonLayer?.wantsLayer = true
  102. self.filterButtonLayer?.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  103. self.filterButtonLayer?.layer?.cornerRadius = 4.0
  104. if (self.isAscendSort) {
  105. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  106. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  107. } else {
  108. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  109. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  110. }
  111. self.noteSearchButton.toolTip = KMLocalizedString("Search", nil)
  112. self.noteDoneButton.title = KMLocalizedString("Done", nil)
  113. self.noteDoneButton.toolTip = KMLocalizedString("Done", nil)
  114. self.noteDoneButton.setTitleColor(KMAppearance.Layout.w0Color())
  115. self.noteDoneButton.wantsLayer = true
  116. self.noteDoneButton.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  117. self.noteDoneButton.layer?.cornerRadius = 4.0
  118. self.noteHeaderView.wantsLayer = true
  119. self.noteHeaderView.layer?.backgroundColor = KMAppearance.Else.textTagColor().cgColor
  120. self.noteHeaderView.layer?.cornerRadius = 1.0
  121. let sortType = KMDataManager.ud_integer(forKey: Self.Key.noteSortTypeKey)
  122. if (sortType == 1) {
  123. self.noteSortType = KMNoteSortType(rawValue: sortType) ?? .none
  124. if (self.noteSortType == .time) {
  125. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  126. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  127. } else if (self.noteSortType == .page) {
  128. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  129. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  130. }
  131. } else {
  132. self.noteSortType = .time
  133. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  134. }
  135. self.sortTypeLabel.textColor = KMAppearance.Layout.h1Color()
  136. self.noteOutlineView.backgroundColor = KMAppearance.Layout.l0Color()
  137. self.noteOutlineView.autoresizesOutlineColumn = false
  138. self.noteOutlineView.indentationPerLevel = 0
  139. }
  140. 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. if let data = NSApp.currentEvent {
  274. NSMenu.popUpContextMenu(menu, with: data, for: view, with: nil)
  275. }
  276. }
  277. func annoListValidateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  278. let action = menuItem.action
  279. if (action == #selector(note_expandAllComments) ||
  280. action == #selector(note_foldAllComments) ||
  281. action == #selector(exportAnnotationNotes) ||
  282. action == #selector(removeAllAnnotations)) {
  283. let cnt = self.annoListModel?.datas.count ?? 0
  284. return cnt > 0
  285. } else if (action == #selector(unfoldNoteAction) ||
  286. action == #selector(foldNoteAction)) {
  287. let row = self.noteOutlineView.clickedRow
  288. let foldNote = self.fetchNote(for: row)
  289. // SKNPDFAnnotationNote
  290. if foldNote is CPDFMarkupAnnotation || foldNote is CPDFTextAnnotation {
  291. return true
  292. } else {
  293. return false
  294. }
  295. } else if (action == #selector(editNoteFromTable)) {
  296. let row = self.noteOutlineView.clickedRow
  297. let foldNote = self.fetchNote(for: row)
  298. // if (@available(macOS 10.13, *)) {
  299. // if ([foldNote.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  300. // return NO;
  301. // }
  302. // }
  303. if foldNote is CPDFStampAnnotation || foldNote is KMAnnotationStamp || foldNote is CPDFListStampAnnotation {
  304. return false
  305. } else {
  306. return true
  307. }
  308. }
  309. return true
  310. }
  311. @IBAction func note_expandAllComments(_ sender: AnyObject?) {
  312. guard let model = self.annoListModel else {
  313. return
  314. }
  315. if (model.foldType == .unfold) { // 已全部展开
  316. return
  317. }
  318. model.foldType = .unfold
  319. self.noteOutlineView.reloadData()
  320. }
  321. @IBAction func note_foldAllComments(_ sender: AnyObject?) {
  322. guard let model = self.annoListModel else {
  323. return
  324. }
  325. if (model.foldType == .fold) {
  326. return
  327. }
  328. model.foldType = .fold
  329. self.noteOutlineView.reloadData()
  330. }
  331. @IBAction func noteShowNoteAction(_ sender: AnyObject?) {
  332. let item = sender as? NSMenuItem
  333. let tag = item?.tag ?? 0
  334. if (tag == 100) {
  335. } else if (tag == 101) {
  336. let isPage = !self.annoListIsShowPage()
  337. self.noteTypeDict[Self.Key.noteFilterPage] = !isPage
  338. } else if (tag == 102) {
  339. let isTime = !self.annoListIsShowTime()
  340. self.noteTypeDict[Self.Key.noteFilterTime] = !isTime
  341. } else if (tag == 103) {
  342. let isAuther = !self.annoListIsShowAnther()
  343. self.noteTypeDict[Self.Key.noteFilterAuther] = !isAuther
  344. }
  345. UserDefaults.standard.sync_setValue(self.noteTypeDict, forKey: Self.Key.noteTableColumn)
  346. // 更新数据
  347. var models: [KMBotaAnnotationModel] = []
  348. if self.noteSearchMode {
  349. models = self.noteSearchArray
  350. } else {
  351. models = self.annoListModel?.datas ?? []
  352. }
  353. for model in models {
  354. model.showPage = self.annoListIsShowPage()
  355. model.showTime = self.annoListIsShowTime()
  356. model.showAuthor = self.annoListIsShowAnther()
  357. }
  358. let selectRow = self.noteOutlineView.selectedRow
  359. self.noteOutlineView.reloadData()
  360. self.noteOutlineView.selectRowIndexes(IndexSet(integer: selectRow), byExtendingSelection: false)
  361. }
  362. @objc func exportAnnotationNotes(_ sender: AnyObject?) {
  363. let doc = self.view.window?.windowController?.document as? NSDocument
  364. doc?.saveTo(sender)
  365. }
  366. // 展开
  367. @objc func unfoldNoteAction(_ sender: NSMenuItem) {
  368. if sender.state == .on {
  369. return
  370. }
  371. let row = self.noteOutlineView.clickedRow
  372. guard let model = self.fetchAnnoModel(for: row) else {
  373. return
  374. }
  375. model.foldType = .unfold
  376. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  377. (viewS as? KMNoteTableViewCell)?.isFold = false
  378. }
  379. @objc func foldNoteAction(_ sender: NSMenuItem) {
  380. if sender.state == .on {
  381. return
  382. }
  383. let row = self.noteOutlineView.clickedRow
  384. guard let model = self.fetchAnnoModel(for: row) else {
  385. return
  386. }
  387. model.foldType = .fold
  388. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  389. (viewS as? KMNoteTableViewCell)?.isFold = true
  390. }
  391. @objc func deleteNotes(_ sender: NSMenuItem) {
  392. self.outlineView(self.noteOutlineView, deleteItems: sender.representedObject as? [Any] ?? [])
  393. }
  394. @objc func removeAllAnnotations(_ sender: AnyObject?) {
  395. guard let doc = self.pdfDocument() else {
  396. return
  397. }
  398. Task {
  399. 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)])
  400. if response == .alertFirstButtonReturn {
  401. var annos: [CPDFAnnotation] = []
  402. for i in 0 ..< doc.pageCount {
  403. let page = self.pdfDocument()?.page(at: i)
  404. for anno in page?.annotations ?? [] {
  405. if anno is CPDFTextWidgetAnnotation || anno is CPDFButtonWidgetAnnotation || anno is CPDFChoiceWidgetAnnotation {
  406. continue
  407. }
  408. // if ([annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  409. // continue;
  410. // }
  411. annos.append(anno)
  412. }
  413. }
  414. self.dataUpdating = true
  415. for anno in annos {
  416. self.listView?.remove(anno)
  417. }
  418. self.dataUpdating = false
  419. self.note_refrshUIIfNeed()
  420. }
  421. }
  422. }
  423. @objc func editNoteTextFromTable(_ sender: NSMenuItem) {
  424. // PDFAnnotation *annotation = [sender representedObject];
  425. guard let annotation = sender.representedObject as? CPDFAnnotation else {
  426. return
  427. }
  428. self.listView?.scrollAnnotationToVisible(annotation)
  429. // self.listView.activeAnnotation = annotation
  430. // [self showNote:annotation];
  431. // SKNoteWindowController *noteController = (SKNoteWindowController *)[self windowControllerForNote:annotation];
  432. // [[noteController window] makeFirstResponder:[noteController textView]];
  433. // [[noteController textView] selectAll:nil];
  434. }
  435. @objc func editThisAnnotation(_ sender: AnyObject?) {
  436. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  437. NSSound.beep()
  438. return
  439. }
  440. self.listView?.edit(annotation)
  441. }
  442. @objc func editNoteFromTable(_ sender: AnyObject?) {
  443. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  444. NSSound.beep()
  445. return
  446. }
  447. let model = fetchAnnoModel(for: annotation)
  448. let row = self.noteOutlineView.row(forItem: model)
  449. self.noteOutlineView.km_safe_selectRowIndexes(.init(integer: row), byExtendingSelection: false)
  450. let noteIndex = self.noteOutlineView.column(withIdentifier: .init("note"))
  451. if (noteIndex >= 0) {
  452. self.noteOutlineView.scrollColumnToVisible(noteIndex)
  453. //
  454. self.isRenameNoteOutline = true
  455. // self.renamePDFOutline = [rightSideController.noteOutlineView itemAtRow:rightSideController.noteOutlineView.clickedRow];
  456. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true) as? KMNoteTableViewCell
  457. let targrtTextField = viewS?.noteContentLabel
  458. self.editNoteTextField = targrtTextField
  459. self.editNote = annotation
  460. targrtTextField?.delegate = self
  461. targrtTextField?.isEditable = true
  462. targrtTextField?.becomeFirstResponder()
  463. }
  464. }
  465. @IBAction func noteSortAction(_ sender: AnyObject?) {
  466. if (self.isAscendSort) {
  467. self.isAscendSort = false
  468. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  469. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  470. } else {
  471. self.isAscendSort = true
  472. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  473. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  474. }
  475. KMDataManager.ud_set(self.isAscendSort, forKey: Self.Key.noteAscendSortKey)
  476. if self.noteSearchMode {
  477. self.reloadNoteForSearchMode()
  478. } else {
  479. self.reloadAnnotation()
  480. }
  481. }
  482. @IBAction func noteSearchAction(_ sender: NSButton) {
  483. self.noteSearchField.isHidden = false
  484. self.noteTitleLabel.isHidden = true
  485. self.noteSearchButton.isHidden = true
  486. self.noteDoneButton.isHidden = false
  487. self.noteFilterButton.isHidden = true
  488. self.noteMoreButton.isHidden = true
  489. self.noteSearchField.becomeFirstResponder()
  490. }
  491. @IBAction func noteFilterAction(_ sender: AnyObject?) {
  492. let button = sender as? NSButton
  493. let menu = NSMenu()
  494. let filterViewController = KMNoteOutlineFilterViewController()
  495. filterViewController.listView = self.listView
  496. filterViewController.view.layer?.backgroundColor = .clear
  497. filterViewController.setNotesArray(self.allAnnotations as NSArray)
  498. filterViewController.applyFilterCallback = { [weak self] typeArr, colorArr, authorArr, isEmpty in
  499. menu.cancelTracking()
  500. if (isEmpty) {
  501. self?.filterButtonLayer?.isHidden = true
  502. } else {
  503. self?.filterButtonLayer?.isHidden = false
  504. }
  505. self?.reloadAnnotation()
  506. }
  507. filterViewController.cancelCallback = { isCancel in
  508. if (isCancel) {
  509. menu.cancelTracking()
  510. }
  511. }
  512. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  513. item.target = self
  514. item.representedObject = filterViewController
  515. item.view = filterViewController.view
  516. menu.popUp(positioning: nil, at: CGPointMake(-130, 30), in: button)
  517. }
  518. func fetchNote(for index: Int) -> CPDFAnnotation? {
  519. return self.fetchAnnoModel(for: index)?.anno
  520. }
  521. func fetchAnnoModel(for index: Int) -> KMBotaAnnotationModel? {
  522. if self.noteSearchMode { // 搜索模式
  523. return self.noteSearchArray.safe_element(for: index) as? KMBotaAnnotationModel
  524. } else { // 常规模式(非搜索)
  525. return self.annoListModel?.datas.safe_element(for: index) as? KMBotaAnnotationModel
  526. }
  527. }
  528. func fetchAnnoModel(for anno: CPDFAnnotation) -> KMBotaAnnotationModel? {
  529. if self.noteSearchMode { // 搜索模式
  530. for model in self.noteSearchArray {
  531. if anno.isEqual(to: model.anno) {
  532. return model
  533. }
  534. }
  535. } else { // 常规模式(非搜索)
  536. for model in self.annoListModel?.datas ?? [] {
  537. if anno.isEqual(to: model.anno) {
  538. return model
  539. }
  540. }
  541. }
  542. return nil
  543. }
  544. @IBAction @objc func sortTypeAction(_ sender: NSMenuItem) {
  545. let item = sender
  546. let tag = item.tag
  547. if (item.state == .on) {
  548. item.state = .off
  549. } else {
  550. item.state = .on
  551. }
  552. if (tag == 0) {
  553. self.noteSortType = .page
  554. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  555. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  556. } else if (tag == 1) {
  557. self.noteSortType = .time
  558. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  559. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  560. }
  561. KMDataManager.ud_set(self.noteSortType.rawValue, forKey: Self.Key.noteSortTypeKey)
  562. if self.noteSearchMode {
  563. self.reloadNoteForSearchMode()
  564. } else {
  565. self.reloadAnnotation()
  566. }
  567. }
  568. func showNoteEmptyView() {
  569. let view = self.noteOutlineView.enclosingScrollView?.documentView
  570. let viewFrame = view?.frame ?? .zero
  571. let emptyVcSize = self.leftSideEmptyVC.emptyAnnotationView.frame.size
  572. self.leftSideEmptyVC.emptyAnnotationView.frame = NSMakeRect((viewFrame.size.width-emptyVcSize.width)/2.0,(viewFrame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  573. self.leftSideEmptyVC.emptyAnnotationView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  574. self.noteOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.emptyAnnotationView)
  575. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = false
  576. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = false
  577. if (self.leftView.segmentedControl.selectedSegment == KMSelectedSegmentType.annotation.rawValue) {
  578. self.noteHeaderView.isHidden = true
  579. self.toolButtonBoxLayoutConstraint.constant = 40.0
  580. }
  581. }
  582. func hideNoteEmptyView() {
  583. self.leftSideEmptyVC.emptyAnnotationView.removeFromSuperview()
  584. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = true
  585. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = true
  586. if (self.leftView.segmentedControl.selectedSegment == KMSelectedSegmentType.annotation.rawValue) {
  587. self.noteHeaderView.isHidden = false
  588. self.toolButtonBoxLayoutConstraint.constant = 64.0
  589. }
  590. }
  591. }
  592. // MARK: - Note
  593. extension KMLeftSideViewController {
  594. public func refreshUIForAddAnnotation(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  595. let need = self._annoList_needRefreshUI(annos: annos)
  596. if need == false {
  597. return
  598. }
  599. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  600. self.note_reloadDataIfNeed()
  601. }
  602. public func refreshUIForAnnoAttributeDidChange(_ anno: CPDFAnnotation?, attributes: [String : Any]?) {
  603. let need = self._annoList_needRefreshUI(annos: anno != nil ? [anno!] : [])
  604. if need == false {
  605. return
  606. }
  607. if let data = anno {
  608. self.note_reloadDataForAnnoIfNeed(anno: data)
  609. }
  610. }
  611. public func annoList_refreshUIForDeleteAnnotations(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  612. if self.type.methodType != .Annotation {
  613. return
  614. }
  615. let need = self._annoList_needRefreshUI(annos: annos)
  616. if need == false {
  617. return
  618. }
  619. for anno in annos ?? [] {
  620. if let model = self.fetchAnnoModel(for: anno) {
  621. self.noteSearchArray.removeObject(model)
  622. self.annoListModel?.datas.removeObject(model)
  623. }
  624. }
  625. if self.dataUpdating == false {
  626. self.note_refrshUIIfNeed()
  627. }
  628. }
  629. func note_refrshUIIfNeed() {
  630. if self.type.methodType != .Annotation {
  631. return
  632. }
  633. Task { @MainActor in
  634. self.noteOutlineView.reloadData()
  635. }
  636. }
  637. func note_reloadDataIfNeed() {
  638. if self.type.methodType != .Annotation {
  639. return
  640. }
  641. self.reloadAnnotation()
  642. }
  643. func note_reloadDataForAnnoIfNeed(anno: CPDFAnnotation) {
  644. if self.type.methodType != .Annotation {
  645. return
  646. }
  647. if anno is CPDFLineAnnotation || anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation || anno is CPDFInkAnnotation {
  648. // 形状注释 + Ink 需要显示框住的内容【刷新】
  649. for item in self.annoListModel?.datas ?? [] {
  650. if anno.isEqual(to: item.anno) {
  651. self.noteOutlineView.reloadItem(item)
  652. break
  653. }
  654. }
  655. } else {
  656. for item in self.annoListModel?.datas ?? [] {
  657. if anno.isEqual(to: item.anno) {
  658. self.noteOutlineView.reloadItem(item)
  659. break
  660. }
  661. }
  662. }
  663. }
  664. func reloadAnnotation() {
  665. if self.listView != nil {
  666. let filterKey = self.pdfDocument()?.documentURL.path ?? ""
  667. let typeArr: [Any] = KMBotaTools.noteFilterAnnoTypes(key: filterKey)
  668. let colorArr: [Any] = KMBotaTools.noteFilterColors(key: filterKey)
  669. let authorArr: [Any] = KMBotaTools.noteFilterAuthors(key: filterKey)
  670. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  671. // self.filtrateButton.image = NSImage(named: "KMImageNameAnnotationsFiltrate")
  672. self.filterButtonLayer?.isHidden = true
  673. } else {
  674. // self.filtrateButton.image = NSImage(named: "icon_annotation_screening_select")self.filterButtonLayer?.isHidden = true
  675. self.filterButtonLayer?.isHidden = false
  676. }
  677. var annotationArray: [CPDFAnnotation] = []
  678. var allAnnotation: [CPDFAnnotation] = []
  679. for i in 0 ..< self.pageCount() {
  680. let page = self.pdfDocument()?.page(at: UInt(i))
  681. var annos: [CPDFAnnotation] = []
  682. // 处理过滤
  683. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  684. let types = ["Highlight","Underline","Strikeout","Freehand","FreeText","Note","Square","Circle","Line","Stamp","Arrow","Image","Redact","Sign"]
  685. annos = KMOCToolClass.filterAnnotation(annotations: page?.annotations ?? [],types: types) as? [CPDFAnnotation] ?? []
  686. } else {
  687. var filterAnnos: [CPDFAnnotation] = page?.annotations ?? []
  688. if typeArr.count > 0 {
  689. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos, types: typeArr) as? [CPDFAnnotation]) ?? []
  690. }
  691. if (colorArr.count > 0) {
  692. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,colors: colorArr) as? [CPDFAnnotation]) ?? []
  693. }
  694. if (authorArr.count > 0) {
  695. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,authors: authorArr) as? [CPDFAnnotation]) ?? []
  696. }
  697. annos = filterAnnos
  698. }
  699. //添加签名注释
  700. for annotation in page?.annotations ?? [] {
  701. if annotation.isKind(of: CPDFSignatureAnnotation.self) {
  702. annos.append(annotation)
  703. }
  704. }
  705. for annotation in annos {
  706. if annotation.isKind(of: KMTableAnnotation.self) {
  707. annos.removeObject(annotation)
  708. } else if annotation.annotationShouldDisplay() == false {
  709. annos.removeObject(annotation)
  710. }
  711. }
  712. // 添加刷选后的注释
  713. allAnnotation += annos
  714. //添加所有annotation 用于筛选
  715. annotationArray += (page?.annotations ?? [])
  716. }
  717. // 处理排序
  718. if self.noteSortType == .page {
  719. /// 排序(升序)
  720. if self.isAscendSort {
  721. allAnnotation.sort {
  722. let idx0 = $0.page?.pageIndex() ?? 0
  723. let idx1 = $1.page?.pageIndex() ?? 0
  724. return idx0 <= idx1
  725. }
  726. } else {
  727. allAnnotation.sort {
  728. let idx0 = $0.page?.pageIndex() ?? 0
  729. let idx1 = $1.page?.pageIndex() ?? 0
  730. return idx0 > idx1
  731. }
  732. }
  733. } else if self.noteSortType == .time {
  734. /// 排序(升序)
  735. if self.isAscendSort {
  736. allAnnotation.sort {
  737. if $0.modificationDate() == nil {
  738. return false
  739. }
  740. if $1.modificationDate() == nil {
  741. return false
  742. }
  743. return $0.modificationDate() <= $1.modificationDate()
  744. }
  745. } else {
  746. allAnnotation.sort {
  747. if $0.modificationDate() == nil {
  748. return false
  749. }
  750. if $1.modificationDate() == nil {
  751. return false
  752. }
  753. return $0.modificationDate() > $1.modificationDate()
  754. }
  755. }
  756. }
  757. // 数据模型\化
  758. let model = KMAnnotationListModel()
  759. var datas: [KMBotaAnnotationModel] = []
  760. for anno in allAnnotation {
  761. let item = KMBotaAnnotationModel()
  762. item.anno = anno
  763. item.showPage = self.annoListIsShowPage()
  764. item.showTime = self.annoListIsShowTime()
  765. item.showAuthor = self.annoListIsShowAnther()
  766. datas.append(item)
  767. }
  768. model.datas = datas
  769. self.annoListModel = model
  770. // 转换对象,用于数据显示
  771. self.allAnnotations = annotationArray
  772. // if self.annotations.count < 1 {
  773. // self.filtrateButton.isEnabled = false
  774. // } else {
  775. // self.filtrateButton.isEnabled = true
  776. // }
  777. }
  778. self.note_refrshUIIfNeed()
  779. }
  780. func reloadNoteForSearchMode() {
  781. if self.noteSearchMode == false {
  782. return
  783. }
  784. // 处理排序
  785. if self.noteSortType == .page {
  786. if self.isAscendSort { /// 排序(升序)
  787. self.noteSearchArray.sort {
  788. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  789. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  790. return idx0 <= idx1
  791. }
  792. } else {
  793. self.noteSearchArray.sort {
  794. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  795. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  796. return idx0 > idx1
  797. }
  798. }
  799. } else if self.noteSortType == .time {
  800. if self.isAscendSort { /// 排序(升序)
  801. self.noteSearchArray.sort {
  802. if $0.anno?.modificationDate() == nil {
  803. return false
  804. }
  805. if $1.anno?.modificationDate() == nil {
  806. return false
  807. }
  808. return $0.anno!.modificationDate() <= $1.anno!.modificationDate()
  809. }
  810. } else {
  811. self.noteSearchArray.sort {
  812. if $0.anno?.modificationDate() == nil {
  813. return false
  814. }
  815. if $1.anno?.modificationDate() == nil {
  816. return false
  817. }
  818. return $0.anno!.modificationDate() > $1.anno!.modificationDate()
  819. }
  820. }
  821. }
  822. self.note_refrshUIIfNeed()
  823. }
  824. // 搜索 Action
  825. func updateNoteFilterPredicate() {
  826. var stringValue = self.noteSearchField.stringValue
  827. // 清空数据
  828. self.noteSearchArray.removeAll()
  829. if stringValue.isEmpty {
  830. for model in self.annoListModel?.datas ?? [] {
  831. guard let _ = model.anno else {
  832. continue
  833. }
  834. self.noteSearchArray.append(model)
  835. }
  836. } else {
  837. // 忽略大小写
  838. let caseInsensite = self.caseInsensitiveNoteSearch
  839. if caseInsensite {
  840. stringValue = stringValue.lowercased()
  841. }
  842. for model in self.annoListModel?.datas ?? [] {
  843. guard let note = model.anno else {
  844. continue
  845. }
  846. var noteString = ""
  847. if let anno = note as? CPDFMarkupAnnotation {
  848. noteString = anno.markupContent()
  849. } else {
  850. noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  851. }
  852. if caseInsensite {
  853. noteString = noteString.lowercased()
  854. }
  855. if noteString.contains(stringValue) {
  856. self.noteSearchArray.append(model)
  857. }
  858. }
  859. }
  860. self.note_refrshUIIfNeed()
  861. }
  862. @objc func selectSelectedNote(_ sender: AnyObject?) {
  863. if self.hideNotes() == false {
  864. let selectedNotes = self.selectedNotes()
  865. if selectedNotes.count == 1 {
  866. let annotation = selectedNotes.last!
  867. self.listView?.go(to: annotation.bounds, on: annotation.page, animated: true)
  868. // [pdfView scrollAnnotationToVisible:annotation];
  869. // [pdfView setActiveAnnotation:annotation];
  870. self.listView?.updateActiveAnnotations([annotation])
  871. self.listView?.setNeedsDisplayAnnotationViewForVisiblePages()
  872. // }
  873. }
  874. // NSInteger column = [sender clickedColumn];
  875. // if (column != -1) {
  876. // NSString *colID = [[[sender tableColumns] objectAtIndex:column] identifier];
  877. //
  878. // if ([colID isEqualToString:@"color"]){
  879. // for (PDFAnnotation *annotation in self.pdfView.activeAnnotations) {
  880. // if (![annotation isKindOfClass:[PDFAnnotationChoiceWidget class]] &&
  881. // ![annotation isKindOfClass:[PDFAnnotationButtonWidget class]] &&
  882. // ![annotation isKindOfClass:[PDFAnnotationTextWidget class]]) {
  883. // [[NSColorPanel sharedColorPanel] orderFront:nil];
  884. // break;
  885. // }
  886. //
  887. // }
  888. // }
  889. // }
  890. }
  891. }
  892. func selectedNotes() -> [CPDFAnnotation] {
  893. var selectedNotes: [CPDFAnnotation] = []
  894. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  895. for row in rowIndexes {
  896. let item = self.noteOutlineView.item(atRow: row)
  897. if item is KMBotaAnnotationModel {
  898. if let anno = (item as! KMBotaAnnotationModel).anno {
  899. // if anno.type == nil {
  900. // item = [(SKNoteText *)item note];
  901. // }
  902. if selectedNotes.contains(anno) == false {
  903. selectedNotes.append(anno)
  904. }
  905. }
  906. }
  907. }
  908. return selectedNotes
  909. }
  910. func clearAnnotationFilterData() {
  911. if let _key = self.pdfDocument()?.documentURL?.path {
  912. let userDefaults = UserDefaults.standard
  913. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  914. userDefaults.set(typeData, forKey: NoteFilterVC.filterSelectTypeKey + _key)
  915. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  916. userDefaults.set(colorData, forKey: NoteFilterVC.filterSelectColorKey + _key)
  917. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  918. userDefaults.set(authorData, forKey: NoteFilterVC.filterSelectAuthorKey + _key)
  919. userDefaults.synchronize()
  920. }
  921. }
  922. private func _annoList_needRefreshUI(annos: [CPDFAnnotation]?) -> Bool {
  923. guard let data = annos else {
  924. return false
  925. }
  926. for anno in data {
  927. if anno.isKind(of: KMTableAnnotation.self) == false {
  928. return true
  929. }
  930. }
  931. return false
  932. }
  933. }