KMLeftSideViewController+Note.swift 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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. self.listView?.remove(anno)
  564. }
  565. }
  566. }
  567. self.annoListModel?.datas.removeAll()
  568. self.dataUpdating = false
  569. self.note_refrshUIIfNeed()
  570. }
  571. }
  572. }
  573. @objc func removeAllReplyAnnotations(_ sender: NSMenuItem?) {
  574. Task {
  575. let response = await KMAlertTool.runModel(message: KMLocalizedString("Are you sure to delete all comment replies?", nil), buttons: [KMLocalizedString("Yes", nil), KMLocalizedString("No", nil)])
  576. if response == .alertFirstButtonReturn {
  577. self.dataUpdating = true
  578. var hasAnno = false
  579. for model in self.annoListModel?.datas ?? [] {
  580. for item in model.items {
  581. // if let anno = item.anno {
  582. // self.listView?.remove(anno)
  583. // }
  584. guard let annoM = item as? KMBotaAnnotationModel else {
  585. continue
  586. }
  587. for replyM in annoM.replyAnnos {
  588. // self.listView?.remove(replyM.replyAnno)
  589. replyM.replyAnno?.page.removeAnnotation(replyM.replyAnno)
  590. hasAnno = true
  591. }
  592. annoM.replyAnnos.removeAll()
  593. }
  594. }
  595. self.dataUpdating = false
  596. self.note_refrshUIIfNeed()
  597. if let data = self.view.window, hasAnno {
  598. KMTools.setDocumentEditedState(window: data)
  599. }
  600. }
  601. }
  602. }
  603. @objc func editNoteTextFromTable(_ sender: NSMenuItem) {
  604. // PDFAnnotation *annotation = [sender representedObject];
  605. guard let annotation = sender.representedObject as? CPDFAnnotation else {
  606. return
  607. }
  608. self.listView?.scrollAnnotationToVisible(annotation)
  609. // self.listView.activeAnnotation = annotation
  610. // [self showNote:annotation];
  611. // SKNoteWindowController *noteController = (SKNoteWindowController *)[self windowControllerForNote:annotation];
  612. // [[noteController window] makeFirstResponder:[noteController textView]];
  613. // [[noteController textView] selectAll:nil];
  614. }
  615. @objc func editThisAnnotation(_ sender: AnyObject?) {
  616. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  617. NSSound.beep()
  618. return
  619. }
  620. self.listView?.edit(annotation)
  621. }
  622. @objc func editNoteFromTable(_ sender: AnyObject?) {
  623. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  624. NSSound.beep()
  625. return
  626. }
  627. let model = fetchAnnoModel(for: annotation)
  628. let row = self.noteOutlineView.row(forItem: model)
  629. self.noteOutlineView.km_safe_selectRowIndexes(.init(integer: row), byExtendingSelection: false)
  630. let noteIndex = self.noteOutlineView.column(withIdentifier: .init("note"))
  631. if (noteIndex >= 0) {
  632. self.noteOutlineView.scrollColumnToVisible(noteIndex)
  633. //
  634. self.isRenameNoteOutline = true
  635. // self.renamePDFOutline = [rightSideController.noteOutlineView itemAtRow:rightSideController.noteOutlineView.clickedRow];
  636. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true) as? KMNoteTableViewCell
  637. viewS!.isFold = false
  638. let targrtTextField = viewS?.noteContentLabel
  639. self.editNoteTextField = targrtTextField
  640. self.editNote = annotation
  641. targrtTextField?.delegate = self
  642. targrtTextField?.isEditable = true
  643. targrtTextField?.becomeFirstResponder()
  644. }
  645. }
  646. @IBAction func noteSortAction(_ sender: AnyObject?) {
  647. if (self.isAscendSort) {
  648. self.isAscendSort = false
  649. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  650. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  651. } else {
  652. self.isAscendSort = true
  653. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  654. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  655. }
  656. KMDataManager.ud_set(self.isAscendSort, forKey: Self.Key.noteAscendSortKey)
  657. if self.noteSearchMode {
  658. self.reloadNoteForSearchMode()
  659. } else {
  660. self.reloadNoteForSortMode()
  661. }
  662. }
  663. @IBAction func noteSearchAction(_ sender: NSButton) {
  664. self.noteSearchField.isHidden = false
  665. self.noteTitleLabel.isHidden = true
  666. self.noteSearchButton.isHidden = true
  667. self.noteDoneButton.isHidden = false
  668. self.noteFilterButton.isHidden = true
  669. self.noteMoreButton.isHidden = true
  670. self.noteSearchField.becomeFirstResponder()
  671. }
  672. @IBAction func noteFilterAction(_ sender: AnyObject?) {
  673. let button = sender as? NSButton
  674. if self.noteFilterSelected == false {
  675. if KMAppearance.isDarkMode() {
  676. self.noteFilterButtonHoverView.layer?.backgroundColor = NSColor(white: 1, alpha: 0.05).cgColor
  677. } else {
  678. self.noteFilterButtonHoverView.layer?.backgroundColor = NSColor(white: 0, alpha: 0.05).cgColor
  679. }
  680. }
  681. let menu = NSMenu()
  682. let filterViewController = KMNoteOutlineFilterViewController()
  683. filterViewController.listView = self.listView
  684. filterViewController.view.layer?.backgroundColor = .clear
  685. filterViewController.hoverView = self.noteFilterButtonHoverView
  686. var states: [CPDFAnnotationState] = []
  687. for anno in self.allAnnotations {
  688. if let reviewS = self.noteReplyHanddler.fetchReviewState(anno) {
  689. if states.contains(reviewS) == false {
  690. states.append(reviewS)
  691. }
  692. } else {
  693. if states.contains(.none) == false {
  694. states.append(.none)
  695. }
  696. }
  697. if let markS = self.noteReplyHanddler.fetchAnnoState(anno) {
  698. if states.contains(markS) == false {
  699. states.append(markS)
  700. }
  701. } else {
  702. if states.contains(.unMarked) == false {
  703. states.append(.unMarked)
  704. }
  705. }
  706. }
  707. filterViewController.updateStates(states: states)
  708. filterViewController.setNotesArray(self.allAnnotations as NSArray)
  709. filterViewController.applyFilterCallback = { [weak self] typeArr, colorArr, authorArr, isEmpty in
  710. menu.cancelTracking()
  711. if (isEmpty) {
  712. self?.filterButtonLayer?.isHidden = true
  713. self?.noteFilterButtonHoverView.layer?.backgroundColor = .clear
  714. self?.noteFilterSelected = false
  715. } else {
  716. self?.filterButtonLayer?.isHidden = false
  717. self?.noteFilterButtonHoverView.layer?.backgroundColor = NSColor(hex: "#227AFF").withAlphaComponent(0.3).cgColor
  718. self?.noteFilterSelected = true
  719. }
  720. self?.reloadAnnotation()
  721. }
  722. filterViewController.cancelCallback = { isCancel in
  723. if (isCancel) {
  724. menu.cancelTracking()
  725. }
  726. }
  727. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  728. item.target = self
  729. item.representedObject = filterViewController
  730. item.view = filterViewController.view
  731. menu.popUp(positioning: nil, at: NSMakePoint(-130, 30), in: button)
  732. // let win = KMNoteOutlineFilterViewController_window(contentRect: .zero, styleMask: .borderless, backing: .buffered, defer: false)
  733. // win.center()
  734. // win.contentViewController = filterViewController
  735. // win.orderFront(nil)
  736. }
  737. func fetchNote(for index: Int) -> CPDFAnnotation? {
  738. return self.fetchAnnoModel(for: index)?.anno
  739. }
  740. func fetchAnnoModel(for index: Int) -> KMBotaAnnotationModel? {
  741. if self.noteSearchMode { // 搜索模式
  742. return self.noteSearchArray.safe_element(for: index) as? KMBotaAnnotationModel
  743. } else { // 常规模式(非搜索)
  744. // let model = self.annoListModel?.datas.safe_element(for: index)
  745. let model = self.noteOutlineView.item(atRow: index)
  746. if let data = model as? KMBotaAnnotationSectionModel {
  747. return nil
  748. }
  749. if let data = model as? KMBotaAnnotationModel {
  750. return data
  751. }
  752. if let data = model as? KMBotaAnnotationFooterModel {
  753. // return data.annoModel
  754. }
  755. if let data = model as? KMBotaAnnotationReplyModel {
  756. // return data.annoModel
  757. }
  758. // return self.annoListModel?.datas.safe_element(for: index) as? KMBotaAnnotationModel
  759. return nil
  760. }
  761. }
  762. func fetchAnnoModel(for anno: CPDFAnnotation) -> KMBotaAnnotationModel? {
  763. if self.noteSearchMode { // 搜索模式
  764. for model in self.noteSearchArray {
  765. guard let data = model as? KMBotaAnnotationModel else {
  766. continue
  767. }
  768. if anno.isEqual(to: data.anno) {
  769. return data
  770. }
  771. }
  772. } else { // 常规模式(非搜索)
  773. for model in self.annoListModel?.datas ?? [] {
  774. for item in model.items {
  775. if let data = item as? KMBotaAnnotationModel {
  776. if anno.isEqual(to: data.anno) {
  777. return data
  778. }
  779. }
  780. }
  781. }
  782. }
  783. return nil
  784. }
  785. @IBAction @objc func sortTypeAction(_ sender: NSMenuItem) {
  786. let item = sender
  787. let tag = item.tag
  788. if (item.state == .on) {
  789. item.state = .off
  790. } else {
  791. item.state = .on
  792. }
  793. if (tag == 0) {
  794. self.noteSortType = .page
  795. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  796. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  797. } else if (tag == 1) {
  798. self.noteSortType = .time
  799. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  800. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  801. }
  802. // KMDataManager.ud_set(self.noteSortType.rawValue, forKey: Self.Key.noteSortTypeKey)
  803. if let data = self.listView?.document?.documentURL.path {
  804. KMDataManager.udExtension_set(self.noteSortType.rawValue, forKey: Self.Key.noteSortTypeKey + data)
  805. }
  806. if self.noteSearchMode {
  807. self.reloadNoteForSearchMode()
  808. } else {
  809. self.reloadAnnotation()
  810. }
  811. }
  812. func showNoteEmptyView() {
  813. let view = self.noteOutlineView.enclosingScrollView?.documentView
  814. let viewFrame = view?.frame ?? .zero
  815. let emptyVcSize = self.leftSideEmptyVC.emptyAnnotationView.frame.size
  816. self.leftSideEmptyVC.emptyAnnotationView.frame = NSMakeRect((viewFrame.size.width-emptyVcSize.width)/2.0,(viewFrame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  817. self.leftSideEmptyVC.emptyAnnotationView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  818. self.noteOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.emptyAnnotationView)
  819. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = false
  820. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = false
  821. if (self.leftView.segmentedControl.selectedSegment == KMSelectedSegmentType.annotation.rawValue) {
  822. self.noteHeaderView.isHidden = true
  823. self.toolButtonBoxLayoutConstraint.constant = 40.0
  824. }
  825. }
  826. func hideNoteEmptyView() {
  827. self.leftSideEmptyVC.emptyAnnotationView.removeFromSuperview()
  828. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = true
  829. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = true
  830. if (self.leftView.segmentedControl.selectedSegment == 3) {
  831. self.noteHeaderView.isHidden = false
  832. self.toolButtonBoxLayoutConstraint.constant = 64.0
  833. }
  834. }
  835. }
  836. // MARK: - Note
  837. extension KMLeftSideViewController {
  838. public func refreshUIForAddAnnotation(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  839. let need = self._annoList_needRefreshUI(annos: annos)
  840. if need == false {
  841. return
  842. }
  843. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  844. self.note_reloadDataIfNeed()
  845. }
  846. public func refreshUIForAnnoAttributeDidChange(_ anno: CPDFAnnotation?, attributes: [String : Any]?) {
  847. self.updateThumbnail(at: Int(anno?.page?.pageIndex() ?? 0))
  848. let need = self._annoList_needRefreshUI(annos: anno != nil ? [anno!] : [])
  849. if need == false {
  850. return
  851. }
  852. if let data = anno {
  853. self.note_reloadDataForAnnoIfNeed(anno: data)
  854. }
  855. }
  856. public func annoList_refreshUIForDeleteAnnotations(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  857. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  858. if self.type.methodType != .Annotation {
  859. return
  860. }
  861. let need = self._annoList_needRefreshUI(annos: annos)
  862. if need == false {
  863. return
  864. }
  865. for anno in annos ?? [] {
  866. if let model = self.fetchAnnoModel(for: anno) {
  867. self.noteSearchArray.removeObject(model)
  868. // self.annoListModel?.datas.removeObject(model)
  869. model.sectionModel?.items.removeObject(model)
  870. if let footer = model.footerModel {
  871. model.sectionModel?.items.removeObject(footer)
  872. }
  873. if self.allAnnotations.contains(anno) {
  874. self.allAnnotations.removeObject(anno)
  875. }
  876. }
  877. }
  878. if self.dataUpdating == false {
  879. self.note_refrshUIIfNeed()
  880. }
  881. }
  882. func note_refrshUIIfNeed() {
  883. if self.type.methodType != .Annotation {
  884. return
  885. }
  886. Task { @MainActor in
  887. self.noteOutlineView.reloadData()
  888. }
  889. }
  890. func note_reloadDataIfNeed() {
  891. if self.type.methodType != .Annotation {
  892. return
  893. }
  894. self.reloadAnnotation()
  895. }
  896. func note_reloadDataForAnnoIfNeed(anno: CPDFAnnotation) {
  897. if self.type.methodType != .Annotation {
  898. return
  899. }
  900. if anno is CPDFLineAnnotation || anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation || anno is CPDFInkAnnotation {
  901. // 形状注释 + Ink 需要显示框住的内容【刷新】
  902. for item in self.annoListModel?.datas ?? [] {
  903. for itemM in item.items {
  904. if anno.isEqual(to: itemM.anno) {
  905. self.noteOutlineView.reloadItem(itemM)
  906. break
  907. }
  908. }
  909. }
  910. } else {
  911. for item in self.annoListModel?.datas ?? [] {
  912. for itemM in item.items {
  913. if anno.isEqual(to: itemM.anno) {
  914. self.noteOutlineView.reloadItem(itemM)
  915. break
  916. }
  917. }
  918. }
  919. }
  920. }
  921. func reloadAnnotation() {
  922. if self.listView != nil {
  923. let filterKey = self.pdfDocument()?.documentURL.path ?? ""
  924. let typeArr: [String] = KMBotaTools.noteFilterAnnoTypes(key: filterKey)
  925. let colorArr: [Any] = KMBotaTools.noteFilterColors(key: filterKey)
  926. let authorArr: [Any] = KMBotaTools.noteFilterAuthors(key: filterKey)
  927. var stateArr: [NSNumber] = KMBotaTools.noteFilterStates(key: filterKey)
  928. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 && stateArr.count == 0 {
  929. // self.filtrateButton.image = NSImage(named: "KMImageNameAnnotationsFiltrate")
  930. self.filterButtonLayer?.isHidden = true
  931. self.noteFilterButtonHoverView.layer?.backgroundColor = .clear
  932. self.noteFilterSelected = false
  933. } else {
  934. // self.filtrateButton.image = NSImage(named: "icon_annotation_screening_select")self.filterButtonLayer?.isHidden = true
  935. self.filterButtonLayer?.isHidden = false
  936. self.noteFilterButtonHoverView.layer?.backgroundColor = NSColor(hex: "#227AFF").withAlphaComponent(0.3).cgColor
  937. self.noteFilterSelected = true
  938. }
  939. var hasMark = false
  940. var hasReview = false
  941. for data in stateArr {
  942. let state = data.intValue
  943. if state == CPDFAnnotationState.marked.rawValue || state == CPDFAnnotationState.unMarked.rawValue {
  944. hasMark = true
  945. }
  946. if state == CPDFAnnotationState.none.rawValue || state == CPDFAnnotationState.accepted.rawValue || state == CPDFAnnotationState.rejected.rawValue || state == CPDFAnnotationState.canceled.rawValue || state == CPDFAnnotationState.completed.rawValue {
  947. hasReview = true
  948. }
  949. }
  950. if hasMark == false {
  951. stateArr.append(NSNumber(value: CPDFAnnotationState.marked.rawValue))
  952. stateArr.append(NSNumber(value: CPDFAnnotationState.unMarked.rawValue))
  953. }
  954. if hasReview == false {
  955. stateArr.append(NSNumber(value: CPDFAnnotationState.none.rawValue))
  956. stateArr.append(NSNumber(value: CPDFAnnotationState.accepted.rawValue))
  957. stateArr.append(NSNumber(value: CPDFAnnotationState.rejected.rawValue))
  958. stateArr.append(NSNumber(value: CPDFAnnotationState.canceled.rawValue))
  959. stateArr.append(NSNumber(value: CPDFAnnotationState.completed.rawValue))
  960. }
  961. var annotationArray: [CPDFAnnotation] = []
  962. var allAnnotation: [CPDFAnnotation] = []
  963. for i in 0 ..< self.pageCount() {
  964. let page = self.pdfDocument()?.page(at: UInt(i))
  965. var annos: [CPDFAnnotation] = []
  966. // 处理过滤
  967. let types = ["Highlight","Underline","Strikeout","Squiggly","Freehand","FreeText","Note","Square","Circle","Line","Stamp","Arrow","Image","Redact","Sign"/*, "table"*/,"Polyline","Polygon"]
  968. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 && stateArr.count == 0 {
  969. annos = KMOCToolClass.filterAnnotation(annotations: page?.annotations ?? [],types: types) as? [CPDFAnnotation] ?? []
  970. annotationArray += annos
  971. } else {
  972. var filterAnnos: [CPDFAnnotation] = page?.annotations ?? []
  973. let allAnnos = KMOCToolClass.filterAnnotation(annotations: filterAnnos,types: types) as? [CPDFAnnotation] ?? []
  974. annotationArray += allAnnos
  975. if typeArr.count > 0 {
  976. var theTypes = typeArr
  977. if typeArr.contains(CPDFAnnotation.kType.measureArrow) && typeArr.contains(CPDFAnnotation.kType.arrow) == false {
  978. theTypes.append(CPDFAnnotation.kType.arrow)
  979. }
  980. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos, types: theTypes) as? [CPDFAnnotation]) ?? []
  981. }
  982. if (colorArr.count > 0) {
  983. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,colors: colorArr) as? [CPDFAnnotation]) ?? []
  984. }
  985. if (authorArr.count > 0) {
  986. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,authors: authorArr) as? [CPDFAnnotation]) ?? []
  987. }
  988. if typeArr.contains(CPDFAnnotation.kType.measureArrow) {
  989. if typeArr.contains(CPDFAnnotation.kType.arrow) == false {
  990. for anno in filterAnnos {
  991. if let data = anno as? CPDFLineAnnotation, data.type == CPDFAnnotation.kType.arrow && data.isMeasure == false {
  992. filterAnnos.removeObject(anno)
  993. }
  994. }
  995. }
  996. } else {
  997. for anno in filterAnnos {
  998. if let data = anno as? CPDFLineAnnotation, data.isMeasure {
  999. filterAnnos.removeObject(anno)
  1000. }
  1001. }
  1002. }
  1003. if stateArr.count > 0 {
  1004. for anno in filterAnnos {
  1005. let markState = self.noteReplyHanddler.fetchAnnoState(anno) ?? .unMarked
  1006. let reviewState = self.noteReplyHanddler.fetchReviewState(anno) ?? .none
  1007. if stateArr.contains(NSNumber(value: markState.rawValue)) == false || stateArr.contains(NSNumber(value: reviewState.rawValue)) == false {
  1008. filterAnnos.removeObject(anno)
  1009. }
  1010. }
  1011. }
  1012. annos = filterAnnos
  1013. }
  1014. //添加签名注释
  1015. for annotation in page?.annotations ?? [] {
  1016. if annotation.isKind(of: CPDFSignatureAnnotation.self) {
  1017. annos.append(annotation)
  1018. annotationArray.append(annotation)
  1019. }
  1020. }
  1021. for annotation in annos {
  1022. if annotation.isKind(of: KMTableAnnotation.self) {
  1023. annos.removeObject(annotation)
  1024. if annotationArray.contains(annotation) {
  1025. annotationArray.removeObject(annotation)
  1026. }
  1027. } else if annotation.annotationShouldDisplay() == false {
  1028. annos.removeObject(annotation)
  1029. if annotationArray.contains(annotation) {
  1030. annotationArray.removeObject(annotation)
  1031. }
  1032. } else if annotation.isKind(of: CPDFLinkAnnotation.self) {
  1033. annos.removeObject(annotation)
  1034. if annotationArray.contains(annotation) {
  1035. annotationArray.removeObject(annotation)
  1036. }
  1037. } else if annotation.isForm() {
  1038. annos.removeObject(annotation)
  1039. if annotationArray.contains(annotation) {
  1040. annotationArray.removeObject(annotation)
  1041. }
  1042. }
  1043. }
  1044. // 添加刷选后的注释
  1045. allAnnotation += annos
  1046. //添加所有annotation 用于筛选
  1047. // annotationArray += (page?.annotations ?? [])
  1048. // annotationArray += annos
  1049. }
  1050. // 处理排序
  1051. if self.noteSortType == .page {
  1052. /// 排序(升序)
  1053. if self.isAscendSort {
  1054. allAnnotation.sort {
  1055. let idx0 = $0.page?.pageIndex() ?? 0
  1056. let idx1 = $1.page?.pageIndex() ?? 0
  1057. return idx0 <= idx1
  1058. }
  1059. } else {
  1060. allAnnotation.sort {
  1061. let idx0 = $0.page?.pageIndex() ?? 0
  1062. let idx1 = $1.page?.pageIndex() ?? 0
  1063. return idx0 > idx1
  1064. }
  1065. }
  1066. } else if self.noteSortType == .time {
  1067. /// 排序(升序)
  1068. if self.isAscendSort {
  1069. allAnnotation.sort {
  1070. if $0.modificationDate() == nil {
  1071. return false
  1072. }
  1073. if $1.modificationDate() == nil {
  1074. return false
  1075. }
  1076. return $0.modificationDate() <= $1.modificationDate()
  1077. }
  1078. } else {
  1079. allAnnotation.sort {
  1080. if $0.modificationDate() == nil {
  1081. return false
  1082. }
  1083. if $1.modificationDate() == nil {
  1084. return false
  1085. }
  1086. return $0.modificationDate() > $1.modificationDate()
  1087. }
  1088. }
  1089. }
  1090. // 数据模型化
  1091. let model = KMAnnotationListModel()
  1092. var datas: [KMBotaAnnotationModel] = []
  1093. var prePageIdx: Int = NSNotFound
  1094. var preDate: Date?
  1095. var secM: KMBotaAnnotationSectionModel?
  1096. for anno in allAnnotation {
  1097. let item = KMBotaAnnotationModel()
  1098. item.anno = anno
  1099. item.showPage = self.annoListIsShowPage()
  1100. item.showTime = self.annoListIsShowTime()
  1101. item.showAuthor = self.annoListIsShowAnther()
  1102. // datas.append(item)
  1103. if self.noteSortType == .page {
  1104. let pageIdx = Int(anno.pageIndex())
  1105. if pageIdx != prePageIdx { // 不是同一个页面
  1106. secM = KMBotaAnnotationSectionModel()
  1107. model.datas.append(secM!)
  1108. }
  1109. secM?.items.append(item)
  1110. prePageIdx = Int(anno.pageIndex())
  1111. } else { // time
  1112. let date = anno.modificationDate()
  1113. if let same = date?.isSameDay(other: preDate), same == false { // 不是同一天
  1114. secM = KMBotaAnnotationSectionModel()
  1115. model.datas.append(secM!)
  1116. }
  1117. secM?.items.append(item)
  1118. preDate = date
  1119. }
  1120. item.sectionModel = secM
  1121. let replyAnnos = self.noteReplyHanddler.fetchReplyAnnotations(anno) ?? []
  1122. for replyAnno in replyAnnos {
  1123. let replyM = KMBotaAnnotationReplyModel()
  1124. replyM.anno = anno
  1125. replyM.replyAnno = replyAnno
  1126. // secM?.items.append(replyM)
  1127. replyM.annoModel = item
  1128. item.replyAnnos.append(replyM)
  1129. }
  1130. let footerI = KMBotaAnnotationFooterModel()
  1131. footerI.anno = anno
  1132. secM?.items.append(footerI)
  1133. item.footerModel = footerI
  1134. footerI.annoModel = item
  1135. }
  1136. // model.datas = datas
  1137. self.annoListModel = model
  1138. // 转换对象,用于数据显示
  1139. self.allAnnotations = annotationArray
  1140. self.noteFilterButton.isEnabled = self.allAnnotations.count >= 1
  1141. }
  1142. self.note_refrshUIIfNeed()
  1143. }
  1144. func reloadNoteForSearchMode() {
  1145. if self.noteSearchMode == false {
  1146. return
  1147. }
  1148. // 处理排序
  1149. if self.noteSortType == .page {
  1150. if self.isAscendSort { /// 排序(升序)
  1151. self.noteSearchArray.sort {
  1152. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  1153. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  1154. return idx0 <= idx1
  1155. }
  1156. } else {
  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. }
  1163. } else if self.noteSortType == .time {
  1164. if self.isAscendSort { /// 排序(升序)
  1165. self.noteSearchArray.sort {
  1166. if $0.anno?.modificationDate() == nil {
  1167. return false
  1168. }
  1169. if $1.anno?.modificationDate() == nil {
  1170. return false
  1171. }
  1172. return $0.anno!.modificationDate() <= $1.anno!.modificationDate()
  1173. }
  1174. } else {
  1175. self.noteSearchArray.sort {
  1176. if $0.anno?.modificationDate() == nil {
  1177. return false
  1178. }
  1179. if $1.anno?.modificationDate() == nil {
  1180. return false
  1181. }
  1182. return $0.anno!.modificationDate() > $1.anno!.modificationDate()
  1183. }
  1184. }
  1185. }
  1186. self.note_refrshUIIfNeed()
  1187. }
  1188. func reloadNoteForSortMode() {
  1189. // 处理排序
  1190. var models: [KMBotaAnnotationModel] = []
  1191. for selM in self.annoListModel?.datas ?? [] {
  1192. for model in selM.items {
  1193. guard let annoModel = model as? KMBotaAnnotationModel else {
  1194. continue
  1195. }
  1196. models.append(annoModel)
  1197. }
  1198. }
  1199. if self.noteSortType == .page {
  1200. if self.isAscendSort { /// 排序(升序)
  1201. models.sort {
  1202. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  1203. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  1204. return idx0 <= idx1
  1205. }
  1206. } else {
  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. }
  1213. } else if self.noteSortType == .time {
  1214. if self.isAscendSort { /// 排序(升序)
  1215. models.sort {
  1216. if $0.anno?.modificationDate() == nil {
  1217. return false
  1218. }
  1219. if $1.anno?.modificationDate() == nil {
  1220. return false
  1221. }
  1222. return $0.anno!.modificationDate() <= $1.anno!.modificationDate()
  1223. }
  1224. } else {
  1225. models.sort {
  1226. if $0.anno?.modificationDate() == nil {
  1227. return false
  1228. }
  1229. if $1.anno?.modificationDate() == nil {
  1230. return false
  1231. }
  1232. return $0.anno!.modificationDate() > $1.anno!.modificationDate()
  1233. }
  1234. }
  1235. }
  1236. // 数据模型\化
  1237. let listModel = KMAnnotationListModel()
  1238. var prePageIdx: Int = NSNotFound
  1239. var preDate: Date?
  1240. var secM: KMBotaAnnotationSectionModel?
  1241. for itemM in models {
  1242. guard let anno = itemM.anno else {
  1243. continue
  1244. }
  1245. if self.noteSortType == .page {
  1246. let pageIdx = Int(anno.pageIndex())
  1247. if pageIdx != prePageIdx { // 不是同一个页面
  1248. secM = KMBotaAnnotationSectionModel()
  1249. listModel.datas.append(secM!)
  1250. secM?.isExpand = itemM.sectionModel?.isExpand ?? true
  1251. }
  1252. secM?.items.append(itemM)
  1253. prePageIdx = Int(anno.pageIndex())
  1254. } else { // time
  1255. let date = anno.modificationDate()
  1256. if let same = date?.isSameDay(other: preDate), same == false { // 不是同一天
  1257. secM = KMBotaAnnotationSectionModel()
  1258. listModel.datas.append(secM!)
  1259. secM?.isExpand = itemM.sectionModel?.isExpand ?? true
  1260. }
  1261. secM?.items.append(itemM)
  1262. preDate = date
  1263. }
  1264. var tmpSelM = itemM.sectionModel
  1265. itemM.sectionModel = secM
  1266. let footerI = KMBotaAnnotationFooterModel()
  1267. footerI.anno = anno
  1268. secM?.items.append(footerI)
  1269. footerI.isExpand = itemM.footerModel?.isExpand ?? false
  1270. itemM.footerModel = footerI
  1271. footerI.annoModel = itemM
  1272. }
  1273. self.annoListModel = listModel
  1274. self.note_refrshUIIfNeed()
  1275. }
  1276. // 搜索 Action
  1277. func updateNoteFilterPredicate() {
  1278. var stringValue = self.noteSearchField.stringValue
  1279. // 清空数据
  1280. self.noteSearchArray.removeAll()
  1281. if stringValue.isEmpty {
  1282. for model in self.annoListModel?.datas ?? [] {
  1283. for item in model.items {
  1284. guard let _ = item.anno else {
  1285. continue
  1286. }
  1287. guard let data = item as? KMBotaAnnotationModel else {
  1288. continue
  1289. }
  1290. self.noteSearchArray.append(item)
  1291. }
  1292. }
  1293. } else {
  1294. // 忽略大小写
  1295. let caseInsensite = self.caseInsensitiveNoteSearch
  1296. if caseInsensite {
  1297. stringValue = stringValue.lowercased()
  1298. }
  1299. for model in self.annoListModel?.datas ?? [] {
  1300. for item in model.items {
  1301. guard let note = item.anno else {
  1302. continue
  1303. }
  1304. var noteString = ""
  1305. if let anno = note as? CPDFMarkupAnnotation {
  1306. noteString = anno.markupContent()
  1307. } else {
  1308. noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  1309. }
  1310. if caseInsensite {
  1311. noteString = noteString.lowercased()
  1312. }
  1313. guard let data = item as? KMBotaAnnotationModel else {
  1314. continue
  1315. }
  1316. if noteString.contains(stringValue) {
  1317. self.noteSearchArray.append(item)
  1318. }
  1319. }
  1320. }
  1321. }
  1322. self.note_refrshUIIfNeed()
  1323. }
  1324. @objc func selectSelectedNote(_ sender: AnyObject?) {
  1325. if self.hideNotes() == false {
  1326. let selectedNotes = self.selectedNotes()
  1327. if selectedNotes.count == 1 {
  1328. let annotation = selectedNotes.last!
  1329. self.listView?.go(to: annotation.bounds, on: annotation.page, animated: true)
  1330. // [pdfView scrollAnnotationToVisible:annotation];
  1331. // [pdfView setActiveAnnotation:annotation];
  1332. self.listView?.updateActiveAnnotations([annotation])
  1333. self.listView?.setNeedsDisplayAnnotationViewForVisiblePages()
  1334. if annotation is CPDFPolygonAnnotation || annotation is CPDFPolylineAnnotation {
  1335. self.listView?.pdfListViewDelegate.pdfListViewAnnotationMeasureInfoChange?(self.listView, with: annotation)
  1336. } else if let anno = annotation as? CPDFLineAnnotation {
  1337. if anno.isMeasure {
  1338. self.listView?.pdfListViewDelegate.pdfListViewAnnotationMeasureInfoChange?(self.listView, with: annotation)
  1339. }
  1340. }
  1341. // }
  1342. }
  1343. // NSInteger column = [sender clickedColumn];
  1344. // if (column != -1) {
  1345. // NSString *colID = [[[sender tableColumns] objectAtIndex:column] identifier];
  1346. //
  1347. // if ([colID isEqualToString:@"color"]){
  1348. // for (PDFAnnotation *annotation in self.pdfView.activeAnnotations) {
  1349. // if (![annotation isKindOfClass:[PDFAnnotationChoiceWidget class]] &&
  1350. // ![annotation isKindOfClass:[PDFAnnotationButtonWidget class]] &&
  1351. // ![annotation isKindOfClass:[PDFAnnotationTextWidget class]]) {
  1352. // [[NSColorPanel sharedColorPanel] orderFront:nil];
  1353. // break;
  1354. // }
  1355. //
  1356. // }
  1357. // }
  1358. // }
  1359. }
  1360. }
  1361. func selectedNotes() -> [CPDFAnnotation] {
  1362. var selectedNotes: [CPDFAnnotation] = []
  1363. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  1364. for row in rowIndexes {
  1365. let item = self.noteOutlineView.item(atRow: row)
  1366. if item is KMBotaAnnotationModel {
  1367. if let anno = (item as! KMBotaAnnotationModel).anno {
  1368. // if anno.type == nil {
  1369. // item = [(SKNoteText *)item note];
  1370. // }
  1371. if selectedNotes.contains(anno) == false {
  1372. selectedNotes.append(anno)
  1373. }
  1374. }
  1375. }
  1376. }
  1377. return selectedNotes
  1378. }
  1379. func clearAnnotationFilterData() {
  1380. if let _key = self.pdfDocument()?.documentURL?.path {
  1381. let userDefaults = UserDefaults.standard
  1382. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1383. userDefaults.set(typeData, forKey: NoteFilterVC.filterSelectTypeKey + _key)
  1384. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1385. userDefaults.set(colorData, forKey: NoteFilterVC.filterSelectColorKey + _key)
  1386. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1387. userDefaults.set(authorData, forKey: NoteFilterVC.filterSelectAuthorKey + _key)
  1388. userDefaults.synchronize()
  1389. }
  1390. }
  1391. private func _annoList_needRefreshUI(annos: [CPDFAnnotation]?) -> Bool {
  1392. guard let data = annos else {
  1393. return false
  1394. }
  1395. var need = false
  1396. for anno in data {
  1397. // if anno.isKind(of: KMTableAnnotation.self) == false {
  1398. // return true
  1399. // }
  1400. if anno.isForm() {
  1401. continue
  1402. }
  1403. if anno is CPDFLinkAnnotation {
  1404. continue
  1405. }
  1406. need = true
  1407. }
  1408. return need
  1409. }
  1410. }