KMLeftSideViewController+Note.swift 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. //
  2. // KMLeftSideViewController+Note.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/23.
  6. //
  7. import Foundation
  8. extension KMLeftSideViewController.Key {
  9. static let noteAscendSortKey = "KMLeftSideViewAscendSortBoolKey"
  10. static let noteSortTypeKey = "KMLeftSideViewNoteSortTypeKey"
  11. static let noteTableColumn = "KMNoteOutlineViewTableColumnKey"
  12. static let noteFilterPage = "kKMNoteFilterAnnotationPageKey"
  13. static let noteFilterTime = "kKMNoteFilterAnnotationTimeKey"
  14. static let noteFilterAuther = "kKMNoteFilterAnnotationAutherKey"
  15. }
  16. // MARK: - Action
  17. extension KMLeftSideViewController {
  18. func note_initSubViews() {
  19. self.noteSearchField.backgroundColor = KMAppearance.Layout.l_1Color()
  20. self.noteSearchField.wantsLayer = true
  21. self.noteSearchField.layer?.backgroundColor = KMAppearance.Layout.l_1Color().cgColor
  22. self.noteSearchField.layer?.borderWidth = 1.0
  23. self.noteMoreButton.target = self
  24. self.noteMoreButton.tag = 304
  25. self.noteMoreButton.action = #selector(leftSideViewMoreButtonAction)
  26. self.moreButtonLayer = KMButtonLayer()
  27. self.noteMoreButton.layer?.addSublayer(self.moreButtonLayer!)
  28. self.moreButtonLayer?.frame = NSMakeRect(0, 0, NSWidth(self.noteMoreButton.bounds), NSHeight(self.noteMoreButton.bounds))
  29. self.noteFilterButton.target = self
  30. self.noteFilterButton.action = #selector(noteFilterAction)
  31. self.filterButtonLayer = NSView()
  32. self.noteFilterButton.addSubview(self.filterButtonLayer!)
  33. self.filterButtonLayer?.frame = NSMakeRect(14, 2, 8, 8)
  34. self.noteDoneButton.action = #selector(leftSideViewDoneButtonAction)
  35. self.noteDoneButton.target = self
  36. self.noteDoneButton.tag = 311
  37. self.noteDoneButton.isHidden = true
  38. self.noteSearchField.delegate = self
  39. self.noteSearchField.isHidden = true
  40. self.noteSearchField.endEditCallBack = { [weak self] isEndEdit in
  41. // if (isEndEdit) {
  42. // self.noteSearchField.isHidden = true
  43. // self.noteSearchButton.isHidden = false
  44. // self.noteTitleLabel.isHidden = false
  45. // }
  46. }
  47. self.sortTypeBox.downCallback = { [unowned self] downEntered, mouseBox, _ in
  48. if (downEntered) {
  49. let menu = NSMenu()
  50. let timeItem = menu.addItem(title: KMLocalizedString("Time", nil), action: #selector(sortTypeAction), target: self)
  51. timeItem?.representedObject = self
  52. timeItem?.tag = 0
  53. let pageItem = menu.addItem(title: KMLocalizedString("Page", nil), action: #selector(sortTypeAction), target: self)
  54. pageItem?.representedObject = self
  55. timeItem?.tag = 1
  56. if (self.noteSortType == .time) {
  57. timeItem?.state = .on
  58. pageItem?.state = .off
  59. } else if (self.noteSortType == .page) {
  60. timeItem?.state = .off
  61. pageItem?.state = .on
  62. }
  63. menu.popUp(positioning: nil, at: NSMakePoint(-10, 0), in: self.sortTypeBox)
  64. }
  65. }
  66. self.noteOutlineView.delegate = self
  67. self.noteOutlineView.dataSource = self
  68. self.noteOutlineView.botaDelegate = self
  69. self.noteOutlineView.botaDataSource = self
  70. self.noteOutlineView.noteDelegate = self
  71. self.noteOutlineView.menu = NSMenu()
  72. self.noteOutlineView.menu?.delegate = self
  73. self.noteOutlineView.typeSelectHelper = SKTypeSelectHelper(matchOption: .SKSubstringMatch)
  74. self.noteOutlineView.enclosingScrollView?.scrollerStyle = .legacy
  75. self.noteOutlineView.enclosingScrollView?.autohidesScrollers = true
  76. self.noteOutlineView.registerForDraggedTypes(NSColor.readableTypes(for: NSPasteboard(name: .drag)))
  77. self.noteOutlineView.target = self
  78. self.noteOutlineView.doubleAction = #selector(selectSelectedNote)
  79. }
  80. func note_initDefalutValue() {
  81. self.noteView.wantsLayer = true
  82. self.noteView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  83. let sud = UserDefaults.standard
  84. if let dict = sud.dictionary(forKey: Self.Key.noteTableColumn) {
  85. self.noteTypeDict = dict
  86. } else {
  87. self.noteTypeDict = [Self.Key.noteFilterPage : false,
  88. Self.Key.noteFilterTime : false,
  89. Self.Key.noteFilterAuther : false]
  90. sud.sync_setValue(self.noteTypeDict, forKey: Self.Key.noteTableColumn)
  91. }
  92. self.caseInsensitiveNoteSearch = sud.bool(forKey: SKCaseInsensitiveNoteSearchKey)
  93. self.isAscendSort = KMDataManager.ud_bool(forKey: Self.Key.noteAscendSortKey)
  94. self.noteTitleLabel.stringValue = KMLocalizedString("Notes", nil);
  95. self.noteTitleLabel.textColor = KMAppearance.Layout.h0Color()
  96. self.noteSearchField.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  97. self.noteMoreButton.wantsLayer = true
  98. self.moreButtonLayer?.layerType = .none
  99. self.moreButtonLayer?.isHidden = true
  100. self.noteFilterButton.toolTip = KMLocalizedString("Sort", nil)
  101. self.noteFilterButton.wantsLayer = true
  102. self.filterButtonLayer?.isHidden = true
  103. self.filterButtonLayer?.wantsLayer = true
  104. self.filterButtonLayer?.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  105. self.filterButtonLayer?.layer?.cornerRadius = 4.0
  106. if (self.isAscendSort) {
  107. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  108. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  109. } else {
  110. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  111. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  112. }
  113. self.noteSearchButton.toolTip = KMLocalizedString("Search", nil)
  114. self.noteDoneButton.title = KMLocalizedString("Done", nil)
  115. self.noteDoneButton.toolTip = KMLocalizedString("Done", nil)
  116. self.noteDoneButton.setTitleColor(KMAppearance.Layout.w0Color())
  117. self.noteDoneButton.wantsLayer = true
  118. self.noteDoneButton.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  119. self.noteDoneButton.layer?.cornerRadius = 4.0
  120. // self.noteHeaderView.wantsLayer = true
  121. // self.noteHeaderView.layer?.backgroundColor = KMAppearance.Else.textTagColor().cgColor
  122. // self.noteHeaderView.layer?.cornerRadius = 1.0
  123. let sortType = KMDataManager.ud_integer(forKey: Self.Key.noteSortTypeKey)
  124. if (sortType == 1) {
  125. self.noteSortType = KMNoteSortType(rawValue: sortType) ?? .none
  126. if (self.noteSortType == .time) {
  127. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  128. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  129. } else if (self.noteSortType == .page) {
  130. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  131. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  132. }
  133. } else {
  134. self.noteSortType = .time
  135. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  136. }
  137. self.sortTypeLabel.textColor = KMAppearance.Layout.h1Color()
  138. self.noteOutlineView.backgroundColor = KMAppearance.Layout.l0Color()
  139. self.noteOutlineView.autoresizesOutlineColumn = false
  140. self.noteOutlineView.indentationPerLevel = 0
  141. }
  142. func annoListIsShowPage() -> Bool {
  143. return !(self.noteTypeDict[Self.Key.noteFilterPage] as? Bool ?? false)
  144. }
  145. func annoListIsShowTime() -> Bool {
  146. return !(self.noteTypeDict[Self.Key.noteFilterTime] as? Bool ?? false)
  147. }
  148. func annoListIsShowAnther() -> Bool {
  149. return !(self.noteTypeDict[Self.Key.noteFilterAuther] as? Bool ?? false)
  150. }
  151. }
  152. // MARK: - Menu
  153. extension KMLeftSideViewController {
  154. func annoListMenu(_ menu: NSMenu) {
  155. var item: NSMenuItem?
  156. var items: NSArray?
  157. var rowIndexes = self.noteOutlineView.selectedRowIndexes
  158. let row = self.noteOutlineView.clickedRow
  159. if row == -1 {
  160. // _ = self._addExportPDFMenu(menu)
  161. _ = self._addDeleteAllAnnoItem(menu)
  162. return
  163. }
  164. if rowIndexes.contains(row) == false {
  165. rowIndexes = IndexSet(integer: row)
  166. }
  167. items = self.noteOutlineView.itemsAtRowIndexes(rowIndexes) as NSArray
  168. guard let model = self.fetchAnnoModel(for: row) else {
  169. return
  170. }
  171. let isFold = model.isFold()
  172. item = menu.addItem(title: KMLocalizedString("Expand", nil), action: #selector(unfoldNoteAction), target: self)
  173. item?.state = isFold ? .off : .on
  174. item?.representedObject = items
  175. item = menu.addItem(title: KMLocalizedString("Collapse", nil), action: #selector(foldNoteAction), target: self)
  176. item?.state = isFold ? .on : .off
  177. item?.representedObject = items
  178. menu.addItem(.separator())
  179. let hideNotes = self.hideNotes()
  180. if hideNotes == false && (items?.count ?? 0) == 1 {
  181. let annotation = self.noteItems(items!).lastObject as? CPDFAnnotation
  182. if let data = annotation?.isEditable(), data {
  183. if annotation?.type == nil {
  184. let isNote = annotation?.isNote() ?? false
  185. if isNote {
  186. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  187. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteTextFromTable), target: self)
  188. item?.representedObject = annotation
  189. }
  190. } else if let data = self.noteOutlineView.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier("note"))?.isHidden, data {
  191. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  192. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  193. item?.representedObject = annotation
  194. } else {
  195. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteFromTable), target: self)
  196. item?.representedObject = annotation
  197. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  198. item?.representedObject = annotation
  199. item?.keyEquivalentModifierMask = [.option]
  200. item?.isAlternate = true
  201. }
  202. }
  203. }
  204. if menu.numberOfItems > 0 {
  205. // _ = self._addExportPDFMenu(menu)
  206. // menu.addItem(.separator())
  207. if self.outlineView(self.noteOutlineView, canDeleteItems: items as? [Any] ?? []) {
  208. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteNotes), target: self)
  209. item?.representedObject = items
  210. }
  211. _ = self._addDeleteAllAnnoItem(menu)
  212. }
  213. }
  214. private func _addExportPDFMenu(_ menu: NSMenu) -> NSMenu {
  215. var item = menu.addItem(title: NSLocalizedString("Export Annotations…", tableName: "", comment: ""), action: nil, target: self)
  216. let subMenu = NSMenu()
  217. item?.submenu = subMenu
  218. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  219. item?.tag = 0
  220. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  221. item?.tag = 1
  222. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  223. item?.tag = 2
  224. item = subMenu.addItem(title: NSLocalizedString("Notes as Text", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  225. item?.tag = 3
  226. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  227. item?.tag = 4
  228. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  229. item?.tag = 5
  230. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  231. item?.tag = 6
  232. return menu
  233. }
  234. private func _addDeleteAllAnnoItem(_ menu: NSMenu) -> NSMenuItem? {
  235. return menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "", comment: ""), action: #selector(removeAllAnnotations), target: self)
  236. }
  237. private func _addDeleteAllReplyAnnoItem(_ menu: NSMenu) -> NSMenuItem? {
  238. return menu.addItem(title: NSLocalizedString("Delete All Reply", tableName: "", comment: ""), action: #selector(removeAllReplyAnnotations), target: self)
  239. }
  240. func annoListMoreMenu(_ view: NSButton) {
  241. let menu = NSMenu()
  242. let object = KMPopupMenuObject()
  243. object.menuTag = 1001
  244. menu.delegate = object
  245. object.enterControllerCallback = { [weak self] isEnter in
  246. if (isEnter) {
  247. self?.moreButtonLayer?.isHidden = false
  248. } else {
  249. self?.moreButtonLayer?.isHidden = true
  250. }
  251. }
  252. let expandAllItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(note_expandAllComments), target: self)
  253. expandAllItem?.representedObject = self.noteOutlineView
  254. let foldAllItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(note_foldAllComments), target: self)
  255. foldAllItem?.representedObject = self.noteOutlineView
  256. let type = self.annoListModel?.foldType ?? .none
  257. expandAllItem?.state = type == .unfold ? .on : .off
  258. foldAllItem?.state = type == .fold ? .on : .off
  259. let showItem = menu.addItem(title: KMLocalizedString("Show Note", nil), action: nil, target: self)
  260. let subMenu = NSMenu()
  261. let pageItem = subMenu.addItem(title: KMLocalizedString("Page", nil), action: #selector(noteShowNoteAction), target: self)
  262. pageItem?.state = self.annoListIsShowPage() ? .on : .off
  263. pageItem?.representedObject = self.noteOutlineView
  264. pageItem?.tag = 101
  265. let timeItem = subMenu.addItem(title: KMLocalizedString("Time", nil) , action: #selector(noteShowNoteAction), target: self)
  266. timeItem?.state = self.annoListIsShowTime() ? .on : .off
  267. timeItem?.representedObject = self.noteOutlineView
  268. timeItem?.tag = 102
  269. let authorItem = subMenu.addItem(title: KMLocalizedString("Author", nil) , action: #selector(noteShowNoteAction), target: self)
  270. authorItem?.state = self.annoListIsShowAnther() ? .on : .off
  271. authorItem?.representedObject = self.noteOutlineView
  272. authorItem?.tag = 103
  273. showItem?.submenu = subMenu
  274. menu.addItem(.separator())
  275. // _ = self._addExportPDFMenu(menu)
  276. // menu.addItem(.separator())
  277. _ = self._addDeleteAllAnnoItem(menu)
  278. _ = self._addDeleteAllReplyAnnoItem(menu)
  279. menu.addItem(.separator())
  280. let importItem = NSMenuItem(title: NSLocalizedString("Import Annotations", comment: ""), action: #selector(importNotes), keyEquivalent: "")
  281. importItem.target = self
  282. menu.addItem(importItem)
  283. let exportItem = NSMenuItem(title: NSLocalizedString("Export Annotations", comment: ""), action: #selector(exportNotes), keyEquivalent: "")
  284. exportItem.target = self
  285. menu.addItem(exportItem)
  286. if let data = NSApp.currentEvent {
  287. NSMenu.popUpContextMenu(menu, with: data, for: view, with: nil)
  288. }
  289. }
  290. func annoListValidateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  291. let action = menuItem.action
  292. if (action == #selector(note_expandAllComments) ||
  293. action == #selector(note_foldAllComments) ||
  294. action == #selector(exportAnnotationNotes) ||
  295. action == #selector(removeAllAnnotations)) {
  296. let cnt = self.annoListModel?.datas.count ?? 0
  297. return cnt > 0
  298. } else if (action == #selector(unfoldNoteAction) ||
  299. action == #selector(foldNoteAction)) {
  300. let row = self.noteOutlineView.clickedRow
  301. let foldNote = self.fetchNote(for: row)
  302. // SKNPDFAnnotationNote
  303. if foldNote is CPDFMarkupAnnotation || foldNote is CPDFTextAnnotation {
  304. return true
  305. } else {
  306. return false
  307. }
  308. } else if (action == #selector(editNoteFromTable)) {
  309. let row = self.noteOutlineView.clickedRow
  310. let foldNote = self.fetchNote(for: row)
  311. // if (@available(macOS 10.13, *)) {
  312. // if ([foldNote.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  313. // return NO;
  314. // }
  315. // }
  316. if foldNote is CPDFStampAnnotation || foldNote is KMAnnotationStamp || foldNote is CPDFListStampAnnotation {
  317. return false
  318. } else {
  319. return true
  320. }
  321. }
  322. return true
  323. }
  324. @IBAction func note_expandAllComments(_ sender: AnyObject?) {
  325. guard let model = self.annoListModel else {
  326. return
  327. }
  328. if (model.foldType == .unfold) { // 已全部展开
  329. return
  330. }
  331. model.foldType = .unfold
  332. self.noteOutlineView.reloadData()
  333. }
  334. @IBAction func note_foldAllComments(_ sender: AnyObject?) {
  335. guard let model = self.annoListModel else {
  336. return
  337. }
  338. if (model.foldType == .fold) {
  339. return
  340. }
  341. model.foldType = .fold
  342. self.noteOutlineView.reloadData()
  343. }
  344. @IBAction func noteShowNoteAction(_ sender: AnyObject?) {
  345. let item = sender as? NSMenuItem
  346. let tag = item?.tag ?? 0
  347. if (tag == 100) {
  348. } else if (tag == 101) {
  349. let isPage = !self.annoListIsShowPage()
  350. self.noteTypeDict[Self.Key.noteFilterPage] = !isPage
  351. } else if (tag == 102) {
  352. let isTime = !self.annoListIsShowTime()
  353. self.noteTypeDict[Self.Key.noteFilterTime] = !isTime
  354. } else if (tag == 103) {
  355. let isAuther = !self.annoListIsShowAnther()
  356. self.noteTypeDict[Self.Key.noteFilterAuther] = !isAuther
  357. }
  358. UserDefaults.standard.sync_setValue(self.noteTypeDict, forKey: Self.Key.noteTableColumn)
  359. // 更新数据
  360. var models: [KMBotaAnnotationModel] = []
  361. if self.noteSearchMode {
  362. models = self.noteSearchArray
  363. } else {
  364. let selModels = self.annoListModel?.datas ?? []
  365. for selModel in selModels {
  366. for item in selModel.items {
  367. if let data = item as? KMBotaAnnotationModel {
  368. models.append(data)
  369. }
  370. }
  371. }
  372. }
  373. for model in models {
  374. model.showPage = self.annoListIsShowPage()
  375. model.showTime = self.annoListIsShowTime()
  376. model.showAuthor = self.annoListIsShowAnther()
  377. }
  378. let selectRow = self.noteOutlineView.selectedRow
  379. self.noteOutlineView.reloadData()
  380. self.noteOutlineView.selectRowIndexes(IndexSet(integer: selectRow), byExtendingSelection: false)
  381. }
  382. @objc func importNotes(_ sender: NSMenuItem) {
  383. let panel = NSOpenPanel()
  384. panel.allowedFileTypes = ["xfdf"]
  385. panel.allowsMultipleSelection = false
  386. panel.beginSheetModal(for: self.view.window!) { resp in
  387. if resp != .OK {
  388. return
  389. }
  390. if let result = self.pdfDocument()?.importAnnotation(fromXFDFPath: panel.url?.path), result {
  391. self.reloadAnnotation()
  392. self.listView?.setNeedsDisplayForVisiblePages()
  393. }
  394. }
  395. }
  396. @objc func exportNotes(_ sender: NSMenuItem?) {
  397. guard let cnt = self.listView?.notes.count, cnt > 0 else {
  398. NSSound.beep()
  399. return
  400. }
  401. let fileName = "\(self.pdfDocument()?.documentURL.deletingPathExtension().lastPathComponent ?? "")" + "_xfdf"
  402. let panel = NSSavePanel()
  403. panel.directoryURL = self.pdfDocument()?.documentURL.deletingLastPathComponent()
  404. panel.allowedFileTypes = ["xfdf"]
  405. panel.nameFieldStringValue = fileName
  406. panel.beginSheetModal(for: self.view.window!) { resp in
  407. if resp != .OK {
  408. return
  409. }
  410. let filePath = panel.url?.path
  411. if let success = self.pdfDocument()?.exportAnnotation(toXFDFPath: filePath), success {
  412. NSWorkspace.shared.selectFile(filePath, inFileViewerRootedAtPath: "")
  413. } else {
  414. Task {
  415. _ = await KMAlertTool.runModel(message: NSLocalizedString("Export Failure!", comment: ""), buttons: ["OK"])
  416. }
  417. }
  418. }
  419. }
  420. @objc func exportAnnotationNotes(_ sender: AnyObject?) {
  421. let doc = self.view.window?.windowController?.document as? NSDocument
  422. doc?.saveTo(sender)
  423. }
  424. // 展开
  425. @objc func unfoldNoteAction(_ sender: NSMenuItem) {
  426. if sender.state == .on {
  427. return
  428. }
  429. let row = self.noteOutlineView.clickedRow
  430. guard let model = self.fetchAnnoModel(for: row) else {
  431. return
  432. }
  433. model.foldType = .unfold
  434. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  435. (viewS as? KMNoteTableViewCell)?.isFold = false
  436. }
  437. @objc func foldNoteAction(_ sender: NSMenuItem) {
  438. if sender.state == .on {
  439. return
  440. }
  441. let row = self.noteOutlineView.clickedRow
  442. guard let model = self.fetchAnnoModel(for: row) else {
  443. return
  444. }
  445. model.foldType = .fold
  446. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  447. (viewS as? KMNoteTableViewCell)?.isFold = true
  448. }
  449. @objc func deleteNotes(_ sender: NSMenuItem) {
  450. self.outlineView(self.noteOutlineView, deleteItems: sender.representedObject as? [Any] ?? [])
  451. }
  452. @objc func removeAllAnnotations(_ sender: AnyObject?) {
  453. guard let doc = self.pdfDocument() else {
  454. return
  455. }
  456. Task {
  457. 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)])
  458. if response == .alertFirstButtonReturn {
  459. // var annos: [CPDFAnnotation] = []
  460. // for i in 0 ..< doc.pageCount {
  461. // let page = self.pdfDocument()?.page(at: i)
  462. // for anno in page?.annotations ?? [] {
  463. // if anno is CPDFTextWidgetAnnotation || anno is CPDFButtonWidgetAnnotation || anno is CPDFChoiceWidgetAnnotation {
  464. // continue
  465. // }
  466. // // if ([annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  467. // // continue;
  468. // // }
  469. // if anno is CPDFLinkAnnotation {
  470. // continue
  471. // }
  472. // annos.append(anno)
  473. // }
  474. // }
  475. self.dataUpdating = true
  476. // for anno in annos {
  477. // self.listView?.remove(anno)
  478. // }
  479. for model in self.annoListModel?.datas ?? [] {
  480. for item in model.items {
  481. if let anno = item.anno {
  482. self.listView?.remove(anno)
  483. }
  484. }
  485. }
  486. self.annoListModel?.datas.removeAll()
  487. self.dataUpdating = false
  488. self.note_refrshUIIfNeed()
  489. }
  490. }
  491. }
  492. @objc func removeAllReplyAnnotations(_ sender: NSMenuItem?) {
  493. self.dataUpdating = true
  494. for model in self.annoListModel?.datas ?? [] {
  495. for item in model.items {
  496. // if let anno = item.anno {
  497. // self.listView?.remove(anno)
  498. // }
  499. guard let annoM = item as? KMBotaAnnotationModel else {
  500. continue
  501. }
  502. for replyM in annoM.replyAnnos {
  503. // self.listView?.remove(replyM.replyAnno)
  504. replyM.replyAnno?.page.removeAnnotation(replyM.replyAnno)
  505. }
  506. annoM.replyAnnos.removeAll()
  507. }
  508. }
  509. self.dataUpdating = false
  510. self.note_refrshUIIfNeed()
  511. }
  512. @objc func editNoteTextFromTable(_ sender: NSMenuItem) {
  513. // PDFAnnotation *annotation = [sender representedObject];
  514. guard let annotation = sender.representedObject as? CPDFAnnotation else {
  515. return
  516. }
  517. self.listView?.scrollAnnotationToVisible(annotation)
  518. // self.listView.activeAnnotation = annotation
  519. // [self showNote:annotation];
  520. // SKNoteWindowController *noteController = (SKNoteWindowController *)[self windowControllerForNote:annotation];
  521. // [[noteController window] makeFirstResponder:[noteController textView]];
  522. // [[noteController textView] selectAll:nil];
  523. }
  524. @objc func editThisAnnotation(_ sender: AnyObject?) {
  525. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  526. NSSound.beep()
  527. return
  528. }
  529. self.listView?.edit(annotation)
  530. }
  531. @objc func editNoteFromTable(_ sender: AnyObject?) {
  532. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  533. NSSound.beep()
  534. return
  535. }
  536. let model = fetchAnnoModel(for: annotation)
  537. let row = self.noteOutlineView.row(forItem: model)
  538. self.noteOutlineView.km_safe_selectRowIndexes(.init(integer: row), byExtendingSelection: false)
  539. let noteIndex = self.noteOutlineView.column(withIdentifier: .init("note"))
  540. if (noteIndex >= 0) {
  541. self.noteOutlineView.scrollColumnToVisible(noteIndex)
  542. //
  543. self.isRenameNoteOutline = true
  544. // self.renamePDFOutline = [rightSideController.noteOutlineView itemAtRow:rightSideController.noteOutlineView.clickedRow];
  545. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true) as? KMNoteTableViewCell
  546. viewS!.isFold = false
  547. let targrtTextField = viewS?.noteContentLabel
  548. self.editNoteTextField = targrtTextField
  549. self.editNote = annotation
  550. targrtTextField?.delegate = self
  551. targrtTextField?.isEditable = true
  552. targrtTextField?.becomeFirstResponder()
  553. }
  554. }
  555. @IBAction func noteSortAction(_ sender: AnyObject?) {
  556. if (self.isAscendSort) {
  557. self.isAscendSort = false
  558. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  559. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  560. } else {
  561. self.isAscendSort = true
  562. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  563. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  564. }
  565. KMDataManager.ud_set(self.isAscendSort, forKey: Self.Key.noteAscendSortKey)
  566. if self.noteSearchMode {
  567. self.reloadNoteForSearchMode()
  568. } else {
  569. self.reloadAnnotation()
  570. }
  571. }
  572. @IBAction func noteSearchAction(_ sender: NSButton) {
  573. self.noteSearchField.isHidden = false
  574. self.noteTitleLabel.isHidden = true
  575. self.noteSearchButton.isHidden = true
  576. self.noteDoneButton.isHidden = false
  577. self.noteFilterButton.isHidden = true
  578. self.noteMoreButton.isHidden = true
  579. self.noteSearchField.becomeFirstResponder()
  580. }
  581. @IBAction func noteFilterAction(_ sender: AnyObject?) {
  582. let button = sender as? NSButton
  583. let menu = NSMenu()
  584. let filterViewController = KMNoteOutlineFilterViewController()
  585. filterViewController.listView = self.listView
  586. filterViewController.view.layer?.backgroundColor = .clear
  587. var states: [CPDFAnnotationState] = [.none, .unMarked]
  588. for anno in self.allAnnotations {
  589. if let reviewS = self.noteReplyHanddler.fetchReviewState(anno) {
  590. if states.contains(reviewS) == false {
  591. states.append(reviewS)
  592. }
  593. }
  594. if let markS = self.noteReplyHanddler.fetchAnnoState(anno) {
  595. if states.contains(markS) == false {
  596. states.append(markS)
  597. }
  598. }
  599. }
  600. filterViewController.updateStates(states: states)
  601. filterViewController.setNotesArray(self.allAnnotations as NSArray)
  602. filterViewController.applyFilterCallback = { [weak self] typeArr, colorArr, authorArr, isEmpty in
  603. menu.cancelTracking()
  604. if (isEmpty) {
  605. self?.filterButtonLayer?.isHidden = true
  606. } else {
  607. self?.filterButtonLayer?.isHidden = false
  608. }
  609. self?.reloadAnnotation()
  610. }
  611. filterViewController.cancelCallback = { isCancel in
  612. if (isCancel) {
  613. menu.cancelTracking()
  614. }
  615. }
  616. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  617. item.target = self
  618. item.representedObject = filterViewController
  619. item.view = filterViewController.view
  620. menu.popUp(positioning: nil, at: NSMakePoint(-130, 30), in: button)
  621. // let win = KMNoteOutlineFilterViewController_window(contentRect: .zero, styleMask: .borderless, backing: .buffered, defer: false)
  622. // win.center()
  623. // win.contentViewController = filterViewController
  624. // win.orderFront(nil)
  625. }
  626. func fetchNote(for index: Int) -> CPDFAnnotation? {
  627. return self.fetchAnnoModel(for: index)?.anno
  628. }
  629. func fetchAnnoModel(for index: Int) -> KMBotaAnnotationModel? {
  630. if self.noteSearchMode { // 搜索模式
  631. return self.noteSearchArray.safe_element(for: index) as? KMBotaAnnotationModel
  632. } else { // 常规模式(非搜索)
  633. // let model = self.annoListModel?.datas.safe_element(for: index)
  634. let model = self.noteOutlineView.item(atRow: index)
  635. if let data = model as? KMBotaAnnotationSectionModel {
  636. return nil
  637. }
  638. if let data = model as? KMBotaAnnotationModel {
  639. return data
  640. }
  641. if let data = model as? KMBotaAnnotationFooterModel {
  642. // return data.annoModel
  643. }
  644. if let data = model as? KMBotaAnnotationReplyModel {
  645. // return data.annoModel
  646. }
  647. // return self.annoListModel?.datas.safe_element(for: index) as? KMBotaAnnotationModel
  648. return nil
  649. }
  650. }
  651. func fetchAnnoModel(for anno: CPDFAnnotation) -> KMBotaAnnotationModel? {
  652. if self.noteSearchMode { // 搜索模式
  653. for model in self.noteSearchArray {
  654. if anno.isEqual(to: model.anno) {
  655. return model
  656. }
  657. }
  658. } else { // 常规模式(非搜索)
  659. for model in self.annoListModel?.datas ?? [] {
  660. for item in model.items {
  661. if let data = item as? KMBotaAnnotationModel {
  662. if anno.isEqual(to: data.anno) {
  663. return data
  664. }
  665. }
  666. }
  667. }
  668. }
  669. return nil
  670. }
  671. @IBAction @objc func sortTypeAction(_ sender: NSMenuItem) {
  672. let item = sender
  673. let tag = item.tag
  674. if (item.state == .on) {
  675. item.state = .off
  676. } else {
  677. item.state = .on
  678. }
  679. if (tag == 0) {
  680. self.noteSortType = .page
  681. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  682. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  683. } else if (tag == 1) {
  684. self.noteSortType = .time
  685. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  686. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  687. }
  688. KMDataManager.ud_set(self.noteSortType.rawValue, forKey: Self.Key.noteSortTypeKey)
  689. if self.noteSearchMode {
  690. self.reloadNoteForSearchMode()
  691. } else {
  692. self.reloadAnnotation()
  693. }
  694. }
  695. func showNoteEmptyView() {
  696. let view = self.noteOutlineView.enclosingScrollView?.documentView
  697. let viewFrame = view?.frame ?? .zero
  698. let emptyVcSize = self.leftSideEmptyVC.emptyAnnotationView.frame.size
  699. self.leftSideEmptyVC.emptyAnnotationView.frame = NSMakeRect((viewFrame.size.width-emptyVcSize.width)/2.0,(viewFrame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  700. self.leftSideEmptyVC.emptyAnnotationView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  701. self.noteOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.emptyAnnotationView)
  702. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = false
  703. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = false
  704. if (self.leftView.segmentedControl.selectedSegment == KMSelectedSegmentType.annotation.rawValue) {
  705. self.noteHeaderView.isHidden = true
  706. self.toolButtonBoxLayoutConstraint.constant = 40.0
  707. }
  708. }
  709. func hideNoteEmptyView() {
  710. self.leftSideEmptyVC.emptyAnnotationView.removeFromSuperview()
  711. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = true
  712. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = true
  713. if (self.leftView.segmentedControl.selectedSegment == 3) {
  714. self.noteHeaderView.isHidden = false
  715. self.toolButtonBoxLayoutConstraint.constant = 64.0
  716. }
  717. }
  718. }
  719. // MARK: - Note
  720. extension KMLeftSideViewController {
  721. public func refreshUIForAddAnnotation(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  722. let need = self._annoList_needRefreshUI(annos: annos)
  723. if need == false {
  724. return
  725. }
  726. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  727. self.note_reloadDataIfNeed()
  728. }
  729. public func refreshUIForAnnoAttributeDidChange(_ anno: CPDFAnnotation?, attributes: [String : Any]?) {
  730. self.updateThumbnail(at: Int(anno?.page?.pageIndex() ?? 0))
  731. let need = self._annoList_needRefreshUI(annos: anno != nil ? [anno!] : [])
  732. if need == false {
  733. return
  734. }
  735. if let data = anno {
  736. self.note_reloadDataForAnnoIfNeed(anno: data)
  737. }
  738. }
  739. public func annoList_refreshUIForDeleteAnnotations(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  740. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  741. if self.type.methodType != .Annotation {
  742. return
  743. }
  744. let need = self._annoList_needRefreshUI(annos: annos)
  745. if need == false {
  746. return
  747. }
  748. for anno in annos ?? [] {
  749. if let model = self.fetchAnnoModel(for: anno) {
  750. self.noteSearchArray.removeObject(model)
  751. // self.annoListModel?.datas.removeObject(model)
  752. if self.allAnnotations.contains(anno) {
  753. self.allAnnotations.removeObject(anno)
  754. }
  755. }
  756. }
  757. if self.dataUpdating == false {
  758. self.note_refrshUIIfNeed()
  759. }
  760. }
  761. func note_refrshUIIfNeed() {
  762. if self.type.methodType != .Annotation {
  763. return
  764. }
  765. Task { @MainActor in
  766. self.noteOutlineView.reloadData()
  767. }
  768. }
  769. func note_reloadDataIfNeed() {
  770. if self.type.methodType != .Annotation {
  771. return
  772. }
  773. self.reloadAnnotation()
  774. }
  775. func note_reloadDataForAnnoIfNeed(anno: CPDFAnnotation) {
  776. if self.type.methodType != .Annotation {
  777. return
  778. }
  779. if anno is CPDFLineAnnotation || anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation || anno is CPDFInkAnnotation {
  780. // 形状注释 + Ink 需要显示框住的内容【刷新】
  781. for item in self.annoListModel?.datas ?? [] {
  782. for itemM in item.items {
  783. if anno.isEqual(to: itemM.anno) {
  784. self.noteOutlineView.reloadItem(itemM)
  785. break
  786. }
  787. }
  788. }
  789. } else {
  790. for item in self.annoListModel?.datas ?? [] {
  791. for itemM in item.items {
  792. if anno.isEqual(to: itemM.anno) {
  793. self.noteOutlineView.reloadItem(itemM)
  794. break
  795. }
  796. }
  797. }
  798. }
  799. }
  800. func reloadAnnotation() {
  801. if self.listView != nil {
  802. let filterKey = self.pdfDocument()?.documentURL.path ?? ""
  803. let typeArr: [String] = KMBotaTools.noteFilterAnnoTypes(key: filterKey)
  804. let colorArr: [Any] = KMBotaTools.noteFilterColors(key: filterKey)
  805. let authorArr: [Any] = KMBotaTools.noteFilterAuthors(key: filterKey)
  806. var stateArr: [NSNumber] = KMBotaTools.noteFilterStates(key: filterKey)
  807. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 && stateArr.count == 0 {
  808. // self.filtrateButton.image = NSImage(named: "KMImageNameAnnotationsFiltrate")
  809. self.filterButtonLayer?.isHidden = true
  810. } else {
  811. // self.filtrateButton.image = NSImage(named: "icon_annotation_screening_select")self.filterButtonLayer?.isHidden = true
  812. self.filterButtonLayer?.isHidden = false
  813. }
  814. var hasMark = false
  815. var hasReview = false
  816. for data in stateArr {
  817. let state = data.intValue
  818. if state == CPDFAnnotationState.marked.rawValue || state == CPDFAnnotationState.unMarked.rawValue {
  819. hasMark = true
  820. }
  821. if state == CPDFAnnotationState.none.rawValue || state == CPDFAnnotationState.accepted.rawValue || state == CPDFAnnotationState.rejected.rawValue || state == CPDFAnnotationState.canceled.rawValue || state == CPDFAnnotationState.completed.rawValue {
  822. hasReview = true
  823. }
  824. }
  825. if hasMark == false {
  826. stateArr.append(NSNumber(value: CPDFAnnotationState.marked.rawValue))
  827. stateArr.append(NSNumber(value: CPDFAnnotationState.unMarked.rawValue))
  828. }
  829. if hasReview == false {
  830. stateArr.append(NSNumber(value: CPDFAnnotationState.none.rawValue))
  831. stateArr.append(NSNumber(value: CPDFAnnotationState.accepted.rawValue))
  832. stateArr.append(NSNumber(value: CPDFAnnotationState.rejected.rawValue))
  833. stateArr.append(NSNumber(value: CPDFAnnotationState.canceled.rawValue))
  834. stateArr.append(NSNumber(value: CPDFAnnotationState.completed.rawValue))
  835. }
  836. var annotationArray: [CPDFAnnotation] = []
  837. var allAnnotation: [CPDFAnnotation] = []
  838. for i in 0 ..< self.pageCount() {
  839. let page = self.pdfDocument()?.page(at: UInt(i))
  840. var annos: [CPDFAnnotation] = []
  841. // 处理过滤
  842. let types = ["Highlight","Underline","Strikeout","Squiggly","Freehand","FreeText","Note","Square","Circle","Line","Stamp","Arrow","Image","Redact","Sign"/*, "table"*/,"Polyline","Polygon"]
  843. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 && stateArr.count == 0 {
  844. annos = KMOCToolClass.filterAnnotation(annotations: page?.annotations ?? [],types: types) as? [CPDFAnnotation] ?? []
  845. annotationArray += annos
  846. } else {
  847. var filterAnnos: [CPDFAnnotation] = page?.annotations ?? []
  848. let allAnnos = KMOCToolClass.filterAnnotation(annotations: filterAnnos,types: types) as? [CPDFAnnotation] ?? []
  849. annotationArray += allAnnos
  850. if typeArr.count > 0 {
  851. var theTypes = typeArr
  852. if typeArr.contains(CPDFAnnotation.kType.measureArrow) && typeArr.contains(CPDFAnnotation.kType.arrow) == false {
  853. theTypes.append(CPDFAnnotation.kType.arrow)
  854. }
  855. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos, types: theTypes) as? [CPDFAnnotation]) ?? []
  856. }
  857. if (colorArr.count > 0) {
  858. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,colors: colorArr) as? [CPDFAnnotation]) ?? []
  859. }
  860. if (authorArr.count > 0) {
  861. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,authors: authorArr) as? [CPDFAnnotation]) ?? []
  862. }
  863. if typeArr.contains(CPDFAnnotation.kType.measureArrow) {
  864. if typeArr.contains(CPDFAnnotation.kType.arrow) == false {
  865. for anno in filterAnnos {
  866. if let data = anno as? CPDFLineAnnotation, data.type == CPDFAnnotation.kType.arrow && data.isMeasure == false {
  867. filterAnnos.removeObject(anno)
  868. }
  869. }
  870. }
  871. } else {
  872. for anno in filterAnnos {
  873. if let data = anno as? CPDFLineAnnotation, data.isMeasure {
  874. filterAnnos.removeObject(anno)
  875. }
  876. }
  877. }
  878. if stateArr.count > 0 {
  879. for anno in filterAnnos {
  880. let markState = self.noteReplyHanddler.fetchAnnoState(anno) ?? .unMarked
  881. let reviewState = self.noteReplyHanddler.fetchReviewState(anno) ?? .none
  882. if stateArr.contains(NSNumber(value: markState.rawValue)) == false || stateArr.contains(NSNumber(value: reviewState.rawValue)) == false {
  883. filterAnnos.removeObject(anno)
  884. }
  885. }
  886. }
  887. annos = filterAnnos
  888. }
  889. //添加签名注释
  890. for annotation in page?.annotations ?? [] {
  891. if annotation.isKind(of: CPDFSignatureAnnotation.self) {
  892. annos.append(annotation)
  893. annotationArray.append(annotation)
  894. }
  895. }
  896. for annotation in annos {
  897. if annotation.isKind(of: KMTableAnnotation.self) {
  898. annos.removeObject(annotation)
  899. if annotationArray.contains(annotation) {
  900. annotationArray.removeObject(annotation)
  901. }
  902. } else if annotation.annotationShouldDisplay() == false {
  903. annos.removeObject(annotation)
  904. if annotationArray.contains(annotation) {
  905. annotationArray.removeObject(annotation)
  906. }
  907. } else if annotation.isKind(of: CPDFLinkAnnotation.self) {
  908. annos.removeObject(annotation)
  909. if annotationArray.contains(annotation) {
  910. annotationArray.removeObject(annotation)
  911. }
  912. }
  913. }
  914. // 添加刷选后的注释
  915. allAnnotation += annos
  916. //添加所有annotation 用于筛选
  917. // annotationArray += (page?.annotations ?? [])
  918. // annotationArray += annos
  919. }
  920. // 处理排序
  921. if self.noteSortType == .page {
  922. /// 排序(升序)
  923. if self.isAscendSort {
  924. allAnnotation.sort {
  925. let idx0 = $0.page?.pageIndex() ?? 0
  926. let idx1 = $1.page?.pageIndex() ?? 0
  927. return idx0 <= idx1
  928. }
  929. } else {
  930. allAnnotation.sort {
  931. let idx0 = $0.page?.pageIndex() ?? 0
  932. let idx1 = $1.page?.pageIndex() ?? 0
  933. return idx0 > idx1
  934. }
  935. }
  936. } else if self.noteSortType == .time {
  937. /// 排序(升序)
  938. if self.isAscendSort {
  939. allAnnotation.sort {
  940. if $0.modificationDate() == nil {
  941. return false
  942. }
  943. if $1.modificationDate() == nil {
  944. return false
  945. }
  946. return $0.modificationDate() <= $1.modificationDate()
  947. }
  948. } else {
  949. allAnnotation.sort {
  950. if $0.modificationDate() == nil {
  951. return false
  952. }
  953. if $1.modificationDate() == nil {
  954. return false
  955. }
  956. return $0.modificationDate() > $1.modificationDate()
  957. }
  958. }
  959. }
  960. // 数据模型\化
  961. let model = KMAnnotationListModel()
  962. var datas: [KMBotaAnnotationModel] = []
  963. var prePageIdx: Int = NSNotFound
  964. var preDate: Date?
  965. var secM: KMBotaAnnotationSectionModel?
  966. for anno in allAnnotation {
  967. let item = KMBotaAnnotationModel()
  968. item.anno = anno
  969. item.showPage = self.annoListIsShowPage()
  970. item.showTime = self.annoListIsShowTime()
  971. item.showAuthor = self.annoListIsShowAnther()
  972. // datas.append(item)
  973. if self.noteSortType == .page {
  974. let pageIdx = Int(anno.pageIndex())
  975. if pageIdx != prePageIdx { // 不是同一个页面
  976. secM = KMBotaAnnotationSectionModel()
  977. model.datas.append(secM!)
  978. }
  979. secM?.items.append(item)
  980. prePageIdx = Int(anno.pageIndex())
  981. } else { // time
  982. let date = anno.modificationDate()
  983. if let same = date?.isSameDay(other: preDate), same == false { // 不是同一天
  984. secM = KMBotaAnnotationSectionModel()
  985. model.datas.append(secM!)
  986. }
  987. secM?.items.append(item)
  988. preDate = date
  989. }
  990. item.sectionModel = secM
  991. let replyAnnos = self.noteReplyHanddler.fetchReplyAnnotations(anno) ?? []
  992. for replyAnno in replyAnnos {
  993. let replyM = KMBotaAnnotationReplyModel()
  994. replyM.anno = anno
  995. replyM.replyAnno = replyAnno
  996. // secM?.items.append(replyM)
  997. replyM.annoModel = item
  998. item.replyAnnos.append(replyM)
  999. }
  1000. let footerI = KMBotaAnnotationFooterModel()
  1001. footerI.anno = anno
  1002. secM?.items.append(footerI)
  1003. item.footerModel = footerI
  1004. footerI.annoModel = item
  1005. }
  1006. // model.datas = datas
  1007. self.annoListModel = model
  1008. // 转换对象,用于数据显示
  1009. self.allAnnotations = annotationArray
  1010. self.noteFilterButton.isEnabled = self.allAnnotations.count >= 1
  1011. }
  1012. self.note_refrshUIIfNeed()
  1013. }
  1014. func reloadNoteForSearchMode() {
  1015. if self.noteSearchMode == false {
  1016. return
  1017. }
  1018. // 处理排序
  1019. if self.noteSortType == .page {
  1020. if self.isAscendSort { /// 排序(升序)
  1021. self.noteSearchArray.sort {
  1022. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  1023. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  1024. return idx0 <= idx1
  1025. }
  1026. } else {
  1027. self.noteSearchArray.sort {
  1028. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  1029. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  1030. return idx0 > idx1
  1031. }
  1032. }
  1033. } else if self.noteSortType == .time {
  1034. if self.isAscendSort { /// 排序(升序)
  1035. self.noteSearchArray.sort {
  1036. if $0.anno?.modificationDate() == nil {
  1037. return false
  1038. }
  1039. if $1.anno?.modificationDate() == nil {
  1040. return false
  1041. }
  1042. return $0.anno!.modificationDate() <= $1.anno!.modificationDate()
  1043. }
  1044. } else {
  1045. self.noteSearchArray.sort {
  1046. if $0.anno?.modificationDate() == nil {
  1047. return false
  1048. }
  1049. if $1.anno?.modificationDate() == nil {
  1050. return false
  1051. }
  1052. return $0.anno!.modificationDate() > $1.anno!.modificationDate()
  1053. }
  1054. }
  1055. }
  1056. self.note_refrshUIIfNeed()
  1057. }
  1058. // 搜索 Action
  1059. func updateNoteFilterPredicate() {
  1060. var stringValue = self.noteSearchField.stringValue
  1061. // 清空数据
  1062. self.noteSearchArray.removeAll()
  1063. if stringValue.isEmpty {
  1064. for model in self.annoListModel?.datas ?? [] {
  1065. for item in model.items {
  1066. guard let _ = item.anno else {
  1067. continue
  1068. }
  1069. guard let data = item as? KMBotaAnnotationModel else {
  1070. continue
  1071. }
  1072. self.noteSearchArray.append(data)
  1073. }
  1074. }
  1075. } else {
  1076. // 忽略大小写
  1077. let caseInsensite = self.caseInsensitiveNoteSearch
  1078. if caseInsensite {
  1079. stringValue = stringValue.lowercased()
  1080. }
  1081. for model in self.annoListModel?.datas ?? [] {
  1082. for item in model.items {
  1083. guard let note = item.anno else {
  1084. continue
  1085. }
  1086. var noteString = ""
  1087. if let anno = note as? CPDFMarkupAnnotation {
  1088. noteString = anno.markupContent()
  1089. } else {
  1090. noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  1091. }
  1092. if caseInsensite {
  1093. noteString = noteString.lowercased()
  1094. }
  1095. guard let data = item as? KMBotaAnnotationModel else {
  1096. continue
  1097. }
  1098. if noteString.contains(stringValue) {
  1099. self.noteSearchArray.append(data)
  1100. }
  1101. }
  1102. }
  1103. }
  1104. self.note_refrshUIIfNeed()
  1105. }
  1106. @objc func selectSelectedNote(_ sender: AnyObject?) {
  1107. if self.hideNotes() == false {
  1108. let selectedNotes = self.selectedNotes()
  1109. if selectedNotes.count == 1 {
  1110. let annotation = selectedNotes.last!
  1111. self.listView?.go(to: annotation.bounds, on: annotation.page, animated: true)
  1112. // [pdfView scrollAnnotationToVisible:annotation];
  1113. // [pdfView setActiveAnnotation:annotation];
  1114. self.listView?.updateActiveAnnotations([annotation])
  1115. self.listView?.setNeedsDisplayAnnotationViewForVisiblePages()
  1116. if annotation is CPDFPolygonAnnotation || annotation is CPDFPolylineAnnotation {
  1117. self.listView?.pdfListViewDelegate.pdfListViewAnnotationMeasureInfoChange?(self.listView, with: annotation)
  1118. } else if let anno = annotation as? CPDFLineAnnotation {
  1119. if anno.isMeasure {
  1120. self.listView?.pdfListViewDelegate.pdfListViewAnnotationMeasureInfoChange?(self.listView, with: annotation)
  1121. }
  1122. }
  1123. // }
  1124. }
  1125. // NSInteger column = [sender clickedColumn];
  1126. // if (column != -1) {
  1127. // NSString *colID = [[[sender tableColumns] objectAtIndex:column] identifier];
  1128. //
  1129. // if ([colID isEqualToString:@"color"]){
  1130. // for (PDFAnnotation *annotation in self.pdfView.activeAnnotations) {
  1131. // if (![annotation isKindOfClass:[PDFAnnotationChoiceWidget class]] &&
  1132. // ![annotation isKindOfClass:[PDFAnnotationButtonWidget class]] &&
  1133. // ![annotation isKindOfClass:[PDFAnnotationTextWidget class]]) {
  1134. // [[NSColorPanel sharedColorPanel] orderFront:nil];
  1135. // break;
  1136. // }
  1137. //
  1138. // }
  1139. // }
  1140. // }
  1141. }
  1142. }
  1143. func selectedNotes() -> [CPDFAnnotation] {
  1144. var selectedNotes: [CPDFAnnotation] = []
  1145. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  1146. for row in rowIndexes {
  1147. let item = self.noteOutlineView.item(atRow: row)
  1148. if item is KMBotaAnnotationModel {
  1149. if let anno = (item as! KMBotaAnnotationModel).anno {
  1150. // if anno.type == nil {
  1151. // item = [(SKNoteText *)item note];
  1152. // }
  1153. if selectedNotes.contains(anno) == false {
  1154. selectedNotes.append(anno)
  1155. }
  1156. }
  1157. }
  1158. }
  1159. return selectedNotes
  1160. }
  1161. func clearAnnotationFilterData() {
  1162. if let _key = self.pdfDocument()?.documentURL?.path {
  1163. let userDefaults = UserDefaults.standard
  1164. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1165. userDefaults.set(typeData, forKey: NoteFilterVC.filterSelectTypeKey + _key)
  1166. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1167. userDefaults.set(colorData, forKey: NoteFilterVC.filterSelectColorKey + _key)
  1168. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1169. userDefaults.set(authorData, forKey: NoteFilterVC.filterSelectAuthorKey + _key)
  1170. userDefaults.synchronize()
  1171. }
  1172. }
  1173. private func _annoList_needRefreshUI(annos: [CPDFAnnotation]?) -> Bool {
  1174. guard let data = annos else {
  1175. return false
  1176. }
  1177. // for anno in data {
  1178. // if anno.isKind(of: KMTableAnnotation.self) == false {
  1179. // return true
  1180. // }
  1181. // }
  1182. return true
  1183. }
  1184. }