KMLeftSideViewController+Note.swift 67 KB

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