KMLeftSideViewController+Note.swift 67 KB

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