KMLeftSideViewController+Note.swift 67 KB

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