KMLeftSideViewController+Note.swift 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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 Texts", 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.renamePDFOutlineTextField = targrtTextField
  459. targrtTextField?.delegate = self
  460. targrtTextField?.isEditable = true
  461. targrtTextField?.becomeFirstResponder()
  462. }
  463. }
  464. @IBAction func noteSortAction(_ sender: AnyObject?) {
  465. if (self.isAscendSort) {
  466. self.isAscendSort = false
  467. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  468. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  469. } else {
  470. self.isAscendSort = true
  471. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  472. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  473. }
  474. KMDataManager.ud_set(self.isAscendSort, forKey: Self.Key.noteAscendSortKey)
  475. if self.noteSearchMode {
  476. self.reloadNoteForSearchMode()
  477. } else {
  478. self.reloadAnnotation()
  479. }
  480. }
  481. @IBAction func noteSearchAction(_ sender: NSButton) {
  482. self.noteSearchField.isHidden = false
  483. self.noteTitleLabel.isHidden = true
  484. self.noteSearchButton.isHidden = true
  485. self.noteDoneButton.isHidden = false
  486. self.noteFilterButton.isHidden = true
  487. self.noteMoreButton.isHidden = true
  488. self.noteSearchField.becomeFirstResponder()
  489. }
  490. @IBAction func noteFilterAction(_ sender: AnyObject?) {
  491. let button = sender as? NSButton
  492. let menu = NSMenu()
  493. let filterViewController = KMNoteOutlineFilterViewController()
  494. filterViewController.listView = self.listView
  495. filterViewController.view.layer?.backgroundColor = .clear
  496. filterViewController.setNotesArray(self.allAnnotations as NSArray)
  497. filterViewController.applyFilterCallback = { [weak self] typeArr, colorArr, authorArr, isEmpty in
  498. menu.cancelTracking()
  499. if (isEmpty) {
  500. self?.filterButtonLayer?.isHidden = true
  501. } else {
  502. self?.filterButtonLayer?.isHidden = false
  503. }
  504. self?.reloadAnnotation()
  505. }
  506. filterViewController.cancelCallback = { isCancel in
  507. if (isCancel) {
  508. menu.cancelTracking()
  509. }
  510. }
  511. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  512. item.target = self
  513. item.representedObject = filterViewController
  514. item.view = filterViewController.view
  515. menu.popUp(positioning: nil, at: CGPointMake(-130, 30), in: button)
  516. }
  517. func fetchNote(for index: Int) -> CPDFAnnotation? {
  518. return self.fetchAnnoModel(for: index)?.anno
  519. }
  520. func fetchAnnoModel(for index: Int) -> KMBotaAnnotationModel? {
  521. if self.noteSearchMode { // 搜索模式
  522. return self.noteSearchArray.safe_element(for: index) as? KMBotaAnnotationModel
  523. } else { // 常规模式(非搜索)
  524. return self.annoListModel?.datas.safe_element(for: index) as? KMBotaAnnotationModel
  525. }
  526. }
  527. func fetchAnnoModel(for anno: CPDFAnnotation) -> KMBotaAnnotationModel? {
  528. if self.noteSearchMode { // 搜索模式
  529. for model in self.noteSearchArray {
  530. if anno.isEqual(to: model.anno) {
  531. return model
  532. }
  533. }
  534. } else { // 常规模式(非搜索)
  535. for model in self.annoListModel?.datas ?? [] {
  536. if anno.isEqual(to: model.anno) {
  537. return model
  538. }
  539. }
  540. }
  541. return nil
  542. }
  543. @IBAction @objc func sortTypeAction(_ sender: NSMenuItem) {
  544. let item = sender
  545. let tag = item.tag
  546. if (item.state == .on) {
  547. item.state = .off
  548. } else {
  549. item.state = .on
  550. }
  551. if (tag == 0) {
  552. self.noteSortType = .page
  553. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  554. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  555. } else if (tag == 1) {
  556. self.noteSortType = .time
  557. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  558. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  559. }
  560. KMDataManager.ud_set(self.noteSortType.rawValue, forKey: Self.Key.noteSortTypeKey)
  561. if self.noteSearchMode {
  562. self.reloadNoteForSearchMode()
  563. } else {
  564. self.reloadAnnotation()
  565. }
  566. }
  567. func showNoteEmptyView() {
  568. let view = self.noteOutlineView.enclosingScrollView?.documentView
  569. let viewFrame = view?.frame ?? .zero
  570. let emptyVcSize = self.leftSideEmptyVC.emptyAnnotationView.frame.size
  571. self.leftSideEmptyVC.emptyAnnotationView.frame = NSMakeRect((viewFrame.size.width-emptyVcSize.width)/2.0,(viewFrame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  572. self.leftSideEmptyVC.emptyAnnotationView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  573. self.noteOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.emptyAnnotationView)
  574. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = false
  575. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = false
  576. if (self.leftView.segmentedControl.selectedSegment == KMSelectedSegmentType.annotation.rawValue) {
  577. self.noteHeaderView.isHidden = true
  578. self.toolButtonBoxLayoutConstraint.constant = 40.0
  579. }
  580. }
  581. func hideNoteEmptyView() {
  582. self.leftSideEmptyVC.emptyAnnotationView.removeFromSuperview()
  583. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = true
  584. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = true
  585. if (self.leftView.segmentedControl.selectedSegment == KMSelectedSegmentType.annotation.rawValue) {
  586. self.noteHeaderView.isHidden = false
  587. self.toolButtonBoxLayoutConstraint.constant = 64.0
  588. }
  589. }
  590. }
  591. // MARK: - Note
  592. extension KMLeftSideViewController {
  593. func note_refrshUIIfNeed() {
  594. if self.type.methodType != .Annotation {
  595. return
  596. }
  597. Task { @MainActor in
  598. self.noteOutlineView.reloadData()
  599. }
  600. }
  601. func note_reloadDataIfNeed() {
  602. if self.type.methodType != .Annotation {
  603. return
  604. }
  605. self.reloadAnnotation()
  606. }
  607. func note_reloadDataForAnnoIfNeed(anno: CPDFAnnotation) {
  608. if self.type.methodType != .Annotation {
  609. return
  610. }
  611. if anno is CPDFLineAnnotation || anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation || anno is CPDFInkAnnotation {
  612. // 形状注释 + Ink 需要显示框住的内容【刷新】
  613. for item in self.annoListModel?.datas ?? [] {
  614. if anno.isEqual(to: item.anno) {
  615. self.noteOutlineView.reloadItem(item)
  616. break
  617. }
  618. }
  619. } else {
  620. for item in self.annoListModel?.datas ?? [] {
  621. if anno.isEqual(to: item.anno) {
  622. self.noteOutlineView.reloadItem(item)
  623. break
  624. }
  625. }
  626. }
  627. }
  628. func reloadAnnotation() {
  629. if self.listView != nil {
  630. let filterKey = self.pdfDocument()?.documentURL.path ?? ""
  631. let typeArr: [Any] = KMBotaTools.noteFilterAnnoTypes(key: filterKey)
  632. let colorArr: [Any] = KMBotaTools.noteFilterColors(key: filterKey)
  633. let authorArr: [Any] = KMBotaTools.noteFilterAuthors(key: filterKey)
  634. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  635. // self.filtrateButton.image = NSImage(named: "KMImageNameAnnotationsFiltrate")
  636. self.filterButtonLayer?.isHidden = true
  637. } else {
  638. // self.filtrateButton.image = NSImage(named: "icon_annotation_screening_select")self.filterButtonLayer?.isHidden = true
  639. self.filterButtonLayer?.isHidden = false
  640. }
  641. var annotationArray: [CPDFAnnotation] = []
  642. var allAnnotation: [CPDFAnnotation] = []
  643. for i in 0 ..< self.pageCount() {
  644. let page = self.pdfDocument()?.page(at: UInt(i))
  645. var annos: [CPDFAnnotation] = []
  646. // 处理过滤
  647. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  648. let types = ["Highlight","Underline","Strikeout","Freehand","FreeText","Note","Square","Circle","Line","Stamp","Arrow","Image","Redact","Sign"]
  649. annos = KMOCToolClass.filterAnnotation(annotations: page?.annotations ?? [],types: types) as? [CPDFAnnotation] ?? []
  650. } else {
  651. var filterAnnos: [CPDFAnnotation] = page?.annotations ?? []
  652. if typeArr.count > 0 {
  653. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos, types: typeArr) as? [CPDFAnnotation]) ?? []
  654. }
  655. if (colorArr.count > 0) {
  656. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,colors: colorArr) as? [CPDFAnnotation]) ?? []
  657. }
  658. if (authorArr.count > 0) {
  659. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,authors: authorArr) as? [CPDFAnnotation]) ?? []
  660. }
  661. annos = filterAnnos
  662. }
  663. //添加签名注释
  664. for annotation in page?.annotations ?? [] {
  665. if annotation.isKind(of: CPDFSignatureAnnotation.self) {
  666. annos.append(annotation)
  667. }
  668. }
  669. for annotation in annos {
  670. if annotation.annotationShouldDisplay() == false {
  671. annos.removeObject(annotation)
  672. }
  673. }
  674. // 添加刷选后的注释
  675. allAnnotation += annos
  676. //添加所有annotation 用于筛选
  677. annotationArray += (page?.annotations ?? [])
  678. }
  679. // 处理排序
  680. if self.noteSortType == .page {
  681. /// 排序(升序)
  682. if self.isAscendSort {
  683. allAnnotation.sort {
  684. let idx0 = $0.page?.pageIndex() ?? 0
  685. let idx1 = $1.page?.pageIndex() ?? 0
  686. return idx0 <= idx1
  687. }
  688. } else {
  689. allAnnotation.sort {
  690. let idx0 = $0.page?.pageIndex() ?? 0
  691. let idx1 = $1.page?.pageIndex() ?? 0
  692. return idx0 > idx1
  693. }
  694. }
  695. } else if self.noteSortType == .time {
  696. /// 排序(升序)
  697. if self.isAscendSort {
  698. allAnnotation.sort {
  699. if $0.modificationDate() == nil {
  700. return false
  701. }
  702. if $1.modificationDate() == nil {
  703. return false
  704. }
  705. return $0.modificationDate() <= $1.modificationDate()
  706. }
  707. } else {
  708. allAnnotation.sort {
  709. if $0.modificationDate() == nil {
  710. return false
  711. }
  712. if $1.modificationDate() == nil {
  713. return false
  714. }
  715. return $0.modificationDate() > $1.modificationDate()
  716. }
  717. }
  718. }
  719. // 数据模型\化
  720. let model = KMAnnotationListModel()
  721. var datas: [KMBotaAnnotationModel] = []
  722. for anno in allAnnotation {
  723. let item = KMBotaAnnotationModel()
  724. item.anno = anno
  725. item.showPage = self.annoListIsShowPage()
  726. item.showTime = self.annoListIsShowTime()
  727. item.showAuthor = self.annoListIsShowAnther()
  728. datas.append(item)
  729. }
  730. model.datas = datas
  731. self.annoListModel = model
  732. // 转换对象,用于数据显示
  733. self.allAnnotations = annotationArray
  734. // if self.annotations.count < 1 {
  735. // self.filtrateButton.isEnabled = false
  736. // } else {
  737. // self.filtrateButton.isEnabled = true
  738. // }
  739. }
  740. self.note_refrshUIIfNeed()
  741. }
  742. func reloadNoteForSearchMode() {
  743. if self.noteSearchMode == false {
  744. return
  745. }
  746. // 处理排序
  747. if self.noteSortType == .page {
  748. if self.isAscendSort { /// 排序(升序)
  749. self.noteSearchArray.sort {
  750. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  751. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  752. return idx0 <= idx1
  753. }
  754. } else {
  755. self.noteSearchArray.sort {
  756. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  757. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  758. return idx0 > idx1
  759. }
  760. }
  761. } else if self.noteSortType == .time {
  762. if self.isAscendSort { /// 排序(升序)
  763. self.noteSearchArray.sort {
  764. if $0.anno?.modificationDate() == nil {
  765. return false
  766. }
  767. if $1.anno?.modificationDate() == nil {
  768. return false
  769. }
  770. return $0.anno!.modificationDate() <= $1.anno!.modificationDate()
  771. }
  772. } else {
  773. self.noteSearchArray.sort {
  774. if $0.anno?.modificationDate() == nil {
  775. return false
  776. }
  777. if $1.anno?.modificationDate() == nil {
  778. return false
  779. }
  780. return $0.anno!.modificationDate() > $1.anno!.modificationDate()
  781. }
  782. }
  783. }
  784. self.note_refrshUIIfNeed()
  785. }
  786. // 搜索 Action
  787. func updateNoteFilterPredicate() {
  788. var stringValue = self.noteSearchField.stringValue
  789. // 清空数据
  790. self.noteSearchArray.removeAll()
  791. if stringValue.isEmpty {
  792. for model in self.annoListModel?.datas ?? [] {
  793. guard let _ = model.anno else {
  794. continue
  795. }
  796. self.noteSearchArray.append(model)
  797. }
  798. } else {
  799. // 忽略大小写
  800. let caseInsensite = self.caseInsensitiveNoteSearch
  801. if caseInsensite {
  802. stringValue = stringValue.lowercased()
  803. }
  804. for model in self.annoListModel?.datas ?? [] {
  805. guard let note = model.anno else {
  806. continue
  807. }
  808. var noteString = ""
  809. if let anno = note as? CPDFMarkupAnnotation {
  810. noteString = anno.markupContent()
  811. } else {
  812. noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  813. }
  814. if caseInsensite {
  815. noteString = noteString.lowercased()
  816. }
  817. if noteString.contains(stringValue) {
  818. self.noteSearchArray.append(model)
  819. }
  820. }
  821. }
  822. self.note_refrshUIIfNeed()
  823. }
  824. @objc func selectSelectedNote(_ sender: AnyObject?) {
  825. if self.hideNotes() == false {
  826. let selectedNotes = self.selectedNotes()
  827. if selectedNotes.count == 1 {
  828. let annotation = selectedNotes.last!
  829. self.listView?.go(to: annotation.bounds, on: annotation.page, animated: true)
  830. // [pdfView scrollAnnotationToVisible:annotation];
  831. // [pdfView setActiveAnnotation:annotation];
  832. self.listView?.updateActiveAnnotations([annotation])
  833. self.listView?.setNeedsDisplayAnnotationViewForVisiblePages()
  834. // }
  835. }
  836. // NSInteger column = [sender clickedColumn];
  837. // if (column != -1) {
  838. // NSString *colID = [[[sender tableColumns] objectAtIndex:column] identifier];
  839. //
  840. // if ([colID isEqualToString:@"color"]){
  841. // for (PDFAnnotation *annotation in self.pdfView.activeAnnotations) {
  842. // if (![annotation isKindOfClass:[PDFAnnotationChoiceWidget class]] &&
  843. // ![annotation isKindOfClass:[PDFAnnotationButtonWidget class]] &&
  844. // ![annotation isKindOfClass:[PDFAnnotationTextWidget class]]) {
  845. // [[NSColorPanel sharedColorPanel] orderFront:nil];
  846. // break;
  847. // }
  848. //
  849. // }
  850. // }
  851. // }
  852. }
  853. }
  854. func selectedNotes() -> [CPDFAnnotation] {
  855. var selectedNotes: [CPDFAnnotation] = []
  856. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  857. for row in rowIndexes {
  858. let item = self.noteOutlineView.item(atRow: row)
  859. if item is KMBotaAnnotationModel {
  860. if let anno = (item as! KMBotaAnnotationModel).anno {
  861. // if anno.type == nil {
  862. // item = [(SKNoteText *)item note];
  863. // }
  864. if selectedNotes.contains(anno) == false {
  865. selectedNotes.append(anno)
  866. }
  867. }
  868. }
  869. }
  870. return selectedNotes
  871. }
  872. func clearAnnotationFilterData() {
  873. if let _key = self.pdfDocument()?.documentURL?.path {
  874. let userDefaults = UserDefaults.standard
  875. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  876. userDefaults.set(typeData, forKey: NoteFilterVC.filterSelectTypeKey + _key)
  877. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  878. userDefaults.set(colorData, forKey: NoteFilterVC.filterSelectColorKey + _key)
  879. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  880. userDefaults.set(authorData, forKey: NoteFilterVC.filterSelectAuthorKey + _key)
  881. userDefaults.synchronize()
  882. }
  883. }
  884. }