KMLeftSideViewController+Note.swift 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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
  709. if annotation.annotationShouldDisplay() == false {
  710. annos.removeObject(annotation)
  711. }
  712. }
  713. // 添加刷选后的注释
  714. allAnnotation += annos
  715. //添加所有annotation 用于筛选
  716. annotationArray += (page?.annotations ?? [])
  717. }
  718. // 处理排序
  719. if self.noteSortType == .page {
  720. /// 排序(升序)
  721. if self.isAscendSort {
  722. allAnnotation.sort {
  723. let idx0 = $0.page?.pageIndex() ?? 0
  724. let idx1 = $1.page?.pageIndex() ?? 0
  725. return idx0 <= idx1
  726. }
  727. } else {
  728. allAnnotation.sort {
  729. let idx0 = $0.page?.pageIndex() ?? 0
  730. let idx1 = $1.page?.pageIndex() ?? 0
  731. return idx0 > idx1
  732. }
  733. }
  734. } else if self.noteSortType == .time {
  735. /// 排序(升序)
  736. if self.isAscendSort {
  737. allAnnotation.sort {
  738. if $0.modificationDate() == nil {
  739. return false
  740. }
  741. if $1.modificationDate() == nil {
  742. return false
  743. }
  744. return $0.modificationDate() <= $1.modificationDate()
  745. }
  746. } else {
  747. allAnnotation.sort {
  748. if $0.modificationDate() == nil {
  749. return false
  750. }
  751. if $1.modificationDate() == nil {
  752. return false
  753. }
  754. return $0.modificationDate() > $1.modificationDate()
  755. }
  756. }
  757. }
  758. // 数据模型\化
  759. let model = KMAnnotationListModel()
  760. var datas: [KMBotaAnnotationModel] = []
  761. for anno in allAnnotation {
  762. let item = KMBotaAnnotationModel()
  763. item.anno = anno
  764. item.showPage = self.annoListIsShowPage()
  765. item.showTime = self.annoListIsShowTime()
  766. item.showAuthor = self.annoListIsShowAnther()
  767. datas.append(item)
  768. }
  769. model.datas = datas
  770. self.annoListModel = model
  771. // 转换对象,用于数据显示
  772. self.allAnnotations = annotationArray
  773. // if self.annotations.count < 1 {
  774. // self.filtrateButton.isEnabled = false
  775. // } else {
  776. // self.filtrateButton.isEnabled = true
  777. // }
  778. }
  779. self.note_refrshUIIfNeed()
  780. }
  781. func reloadNoteForSearchMode() {
  782. if self.noteSearchMode == false {
  783. return
  784. }
  785. // 处理排序
  786. if self.noteSortType == .page {
  787. if self.isAscendSort { /// 排序(升序)
  788. self.noteSearchArray.sort {
  789. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  790. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  791. return idx0 <= idx1
  792. }
  793. } else {
  794. self.noteSearchArray.sort {
  795. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  796. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  797. return idx0 > idx1
  798. }
  799. }
  800. } else if self.noteSortType == .time {
  801. if self.isAscendSort { /// 排序(升序)
  802. self.noteSearchArray.sort {
  803. if $0.anno?.modificationDate() == nil {
  804. return false
  805. }
  806. if $1.anno?.modificationDate() == nil {
  807. return false
  808. }
  809. return $0.anno!.modificationDate() <= $1.anno!.modificationDate()
  810. }
  811. } else {
  812. self.noteSearchArray.sort {
  813. if $0.anno?.modificationDate() == nil {
  814. return false
  815. }
  816. if $1.anno?.modificationDate() == nil {
  817. return false
  818. }
  819. return $0.anno!.modificationDate() > $1.anno!.modificationDate()
  820. }
  821. }
  822. }
  823. self.note_refrshUIIfNeed()
  824. }
  825. // 搜索 Action
  826. func updateNoteFilterPredicate() {
  827. var stringValue = self.noteSearchField.stringValue
  828. // 清空数据
  829. self.noteSearchArray.removeAll()
  830. if stringValue.isEmpty {
  831. for model in self.annoListModel?.datas ?? [] {
  832. guard let _ = model.anno else {
  833. continue
  834. }
  835. self.noteSearchArray.append(model)
  836. }
  837. } else {
  838. // 忽略大小写
  839. let caseInsensite = self.caseInsensitiveNoteSearch
  840. if caseInsensite {
  841. stringValue = stringValue.lowercased()
  842. }
  843. for model in self.annoListModel?.datas ?? [] {
  844. guard let note = model.anno else {
  845. continue
  846. }
  847. var noteString = ""
  848. if let anno = note as? CPDFMarkupAnnotation {
  849. noteString = anno.markupContent()
  850. } else {
  851. noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  852. }
  853. if caseInsensite {
  854. noteString = noteString.lowercased()
  855. }
  856. if noteString.contains(stringValue) {
  857. self.noteSearchArray.append(model)
  858. }
  859. }
  860. }
  861. self.note_refrshUIIfNeed()
  862. }
  863. @objc func selectSelectedNote(_ sender: AnyObject?) {
  864. if self.hideNotes() == false {
  865. let selectedNotes = self.selectedNotes()
  866. if selectedNotes.count == 1 {
  867. let annotation = selectedNotes.last!
  868. self.listView?.go(to: annotation.bounds, on: annotation.page, animated: true)
  869. // [pdfView scrollAnnotationToVisible:annotation];
  870. // [pdfView setActiveAnnotation:annotation];
  871. self.listView?.updateActiveAnnotations([annotation])
  872. self.listView?.setNeedsDisplayAnnotationViewForVisiblePages()
  873. // }
  874. }
  875. // NSInteger column = [sender clickedColumn];
  876. // if (column != -1) {
  877. // NSString *colID = [[[sender tableColumns] objectAtIndex:column] identifier];
  878. //
  879. // if ([colID isEqualToString:@"color"]){
  880. // for (PDFAnnotation *annotation in self.pdfView.activeAnnotations) {
  881. // if (![annotation isKindOfClass:[PDFAnnotationChoiceWidget class]] &&
  882. // ![annotation isKindOfClass:[PDFAnnotationButtonWidget class]] &&
  883. // ![annotation isKindOfClass:[PDFAnnotationTextWidget class]]) {
  884. // [[NSColorPanel sharedColorPanel] orderFront:nil];
  885. // break;
  886. // }
  887. //
  888. // }
  889. // }
  890. // }
  891. }
  892. }
  893. func selectedNotes() -> [CPDFAnnotation] {
  894. var selectedNotes: [CPDFAnnotation] = []
  895. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  896. for row in rowIndexes {
  897. let item = self.noteOutlineView.item(atRow: row)
  898. if item is KMBotaAnnotationModel {
  899. if let anno = (item as! KMBotaAnnotationModel).anno {
  900. // if anno.type == nil {
  901. // item = [(SKNoteText *)item note];
  902. // }
  903. if selectedNotes.contains(anno) == false {
  904. selectedNotes.append(anno)
  905. }
  906. }
  907. }
  908. }
  909. return selectedNotes
  910. }
  911. func clearAnnotationFilterData() {
  912. if let _key = self.pdfDocument()?.documentURL?.path {
  913. let userDefaults = UserDefaults.standard
  914. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  915. userDefaults.set(typeData, forKey: NoteFilterVC.filterSelectTypeKey + _key)
  916. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  917. userDefaults.set(colorData, forKey: NoteFilterVC.filterSelectColorKey + _key)
  918. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  919. userDefaults.set(authorData, forKey: NoteFilterVC.filterSelectAuthorKey + _key)
  920. userDefaults.synchronize()
  921. }
  922. }
  923. private func _annoList_needRefreshUI(annos: [CPDFAnnotation]?) -> Bool {
  924. guard let data = annos else {
  925. return false
  926. }
  927. // for anno in data {
  928. // if anno.isKind(of: KMTableAnnotation.self) == false {
  929. // return true
  930. // }
  931. // }
  932. return true
  933. }
  934. }