KMAnnotationViewController.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. //
  2. // KMAnnotationViewController.swift
  3. // PDF Master
  4. //
  5. // Created by lxy on 2022/10/10.
  6. //
  7. import Cocoa
  8. enum KMAnnotationViewShowType: Int {
  9. case none
  10. case hidden
  11. }
  12. class KMAnnotationViewController: KMSideViewController {
  13. @IBOutlet weak var topView: NSView!
  14. @IBOutlet weak var filtrateButton: NSButton!
  15. @IBOutlet weak var moreButton: NSButton!
  16. @IBOutlet weak var markupTitleLabel: NSTextField!
  17. @IBOutlet weak var emptyView: NSView!
  18. @IBOutlet weak var bigTipLabel: NSTextField!
  19. @IBOutlet weak var tipLabel: NSTextField!
  20. @IBOutlet weak var annotationOutlineView: KMAnnotationOutlineView!
  21. var annotations: [KMBOTAAnnotationSection] = [] {
  22. didSet {
  23. self.annotationOutlineView.inputData = annotations
  24. self.updateExtempViewState()
  25. }
  26. }
  27. var screenAnnotations: [KMBOTAAnnotationSection] = [] {
  28. didSet {
  29. self.annotations = screenAnnotations
  30. }
  31. }
  32. //注释状态
  33. var annotationShowState: KMAnnotationViewShowType = .none {
  34. didSet {
  35. self.reloadData()
  36. }
  37. }
  38. var allAnnotations: [CPDFAnnotation] = []
  39. //localEvent
  40. var localEvent: Bool = false
  41. deinit {
  42. print("KMAnnotationViewController")
  43. self.removeNotification()
  44. }
  45. //MARK: View
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48. self.setup()
  49. self.updateUI()
  50. self.updateLanguage()
  51. self.addNotification()
  52. self.reloadData()
  53. }
  54. func setup() {
  55. self.view.wantsLayer = true
  56. self.view.layer?.backgroundColor = NSColor(hex: "#F7F8FA").cgColor
  57. self.emptyView.backgroundColor(NSColor(hex: "#F7F8FA"))
  58. self.topView.wantsLayer = true
  59. self.topView.layer?.backgroundColor = NSColor.clear.cgColor
  60. self.annotationOutlineView.outlineView.doubleAction = #selector(tableViewDoubleAction)
  61. self.annotationOutlineView.delegate = self
  62. }
  63. func updateUI() {
  64. self.markupTitleLabel.font = NSFont.SFProTextSemibold(14.0)
  65. self.markupTitleLabel.textColor = NSColor(hex: "#252629")
  66. self.bigTipLabel.font = NSFont.SFProTextRegular(14.0)
  67. self.bigTipLabel.textColor = NSColor(hex: "#616469")
  68. if self.annotationShowState == .none {
  69. } else {
  70. self.bigTipLabel.font = NSFont.SFProTextRegular(12.0)
  71. self.bigTipLabel.textColor = NSColor(hex: "#94989C")
  72. }
  73. }
  74. func updateLanguage() {
  75. self.markupTitleLabel.stringValue = NSLocalizedString("Annotation", comment: "")
  76. self.filtrateButton.toolTip = NSLocalizedString("Sort", comment: "")
  77. self.moreButton.toolTip = NSLocalizedString("More", comment: "")
  78. if self.annotationShowState == .none {
  79. self.bigTipLabel.stringValue = NSLocalizedString("No Annotations", comment: "")
  80. let title = NSLocalizedString("All annotations will be displayed here.", comment: "")
  81. let paragraphStyle = NSMutableParagraphStyle()
  82. paragraphStyle.lineHeightMultiple = 1.32
  83. paragraphStyle.alignment = .center
  84. self.tipLabel.attributedStringValue = NSMutableAttributedString(string: title, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle, .foregroundColor : NSColor(hex: "#94989C"), NSAttributedString.Key.font: NSFont.SFProTextRegular(12)])
  85. } else {
  86. self.bigTipLabel.stringValue = NSLocalizedString("The Annotations are hidden", comment: "")
  87. self.tipLabel.stringValue = ""
  88. }
  89. }
  90. func addNotification() {
  91. NotificationCenter.default.addObserver(self, selector: #selector(documentPageCountChangedNotification), name: NSNotification.Name.init(rawValue: "CPDFDocumentPageCountChangedNotification"), object: nil)
  92. NotificationCenter.default.addObserver(self, selector: #selector(CPDFListViewActiveAnnotationsChangeNotification), name: NSNotification.Name.init(rawValue: "CPDFListViewActiveAnnotationsChangeNotification"), object: nil)
  93. NotificationCenter.default.addObserver(self, selector: #selector(CPDFListViewAnnotationsAttributeHasChangeNotification), name: NSNotification.Name.init(rawValue: "CPDFListViewAnnotationsAttributeHasChangeNotification"), object: nil)
  94. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataAfter), name: NSNotification.Name.init(rawValue: "CPDFPageDidAddAnnotationNotification"), object: nil)
  95. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataAfter), name: NSNotification.Name.init(rawValue: "CPDFPageDidRemoveAnnotationNotification"), object: nil)
  96. }
  97. func removeNotification() {
  98. NotificationCenter.default.removeObserver(self)
  99. }
  100. public func clear() {
  101. self.annotations.removeAll()
  102. self.annotationOutlineView.reloadData()
  103. }
  104. }
  105. //MARK: Data
  106. extension KMAnnotationViewController {
  107. @objc public func reloadData() {
  108. self.reloadAnnotation()
  109. self.annotationSort(sortArray: [])
  110. }
  111. func reloadAnnotation() {
  112. if self.listView != nil {
  113. var dataArray: [KMBOTAAnnotationSection] = []
  114. var annotationArray: [CPDFAnnotation] = []
  115. for i in 0 ..< self.listView.document.pageCount {
  116. var annotationItemArray: [KMBOTAAnnotationItem] = []
  117. let page = self.listView.document.page(at: i)
  118. let types = ["Highlight","Underline","Strikeout","Freehand","FreeText","Note","Square","Circle","Line","Stamp","Arrow","Image","Redact","Sign"]
  119. var pageAnnotations: [CPDFAnnotation] = KMOCToolClass.filterAnnotation(page!.annotations,types: types) as! [CPDFAnnotation]
  120. //添加签名注释
  121. for annotation in page!.annotations {
  122. if annotation.isKind(of: CPDFSignatureAnnotation.self) {
  123. pageAnnotations.append(annotation)
  124. }
  125. }
  126. for annotation in pageAnnotations {
  127. if annotation.annotationShouldDisplay() == false {
  128. pageAnnotations.removeObject(annotation)
  129. }
  130. }
  131. //转换所有annotation类型
  132. let section = KMBOTAAnnotationSection()
  133. for annotation in pageAnnotations {
  134. let item = KMBOTAAnnotationItem()
  135. item.section = section
  136. item.annotation = annotation
  137. item.index = Int(annotation.page.pageIndex())
  138. annotationItemArray.append(item)
  139. }
  140. if annotationItemArray.count != 0 {
  141. section.annotations = annotationItemArray
  142. section.page = page
  143. section.isItemExpanded = true
  144. dataArray.append(section)
  145. }
  146. //添加所有annotation 用于筛选
  147. annotationArray += pageAnnotations
  148. }
  149. //转换对象,用于数据显示
  150. self.annotations = dataArray
  151. self.allAnnotations = annotationArray
  152. if self.annotations.count < 1 {
  153. self.filtrateButton.isEnabled = false
  154. } else {
  155. self.filtrateButton.isEnabled = true
  156. }
  157. }
  158. }
  159. func annotationSort(sortArray:[[Any]]) {
  160. if self.listView != nil {
  161. var typeArr: [Any] = []
  162. var colorArr: [Any] = []
  163. var authorArr: [Any] = []
  164. let sud = UserDefaults.standard
  165. let typeData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Type" + self.listView.document.documentURL.path) as? Data
  166. if typeData != nil {
  167. typeArr = NSKeyedUnarchiver.unarchiveObject(with: typeData!) as! [Any]
  168. }
  169. let colorData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Color" + self.listView.document.documentURL.path) as? Data
  170. if colorData != nil {
  171. colorArr = NSKeyedUnarchiver.unarchiveObject(with: colorData!) as! [Any]
  172. }
  173. let authorData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Author" + self.listView.document.documentURL.path) as? Data
  174. if authorData != nil {
  175. authorArr = NSKeyedUnarchiver.unarchiveObject(with: authorData!) as! [Any]
  176. }
  177. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  178. self.filtrateButton.image = NSImage(named: "KMImageNameAnnotationsFiltrate")
  179. self.reloadAnnotation()
  180. } else {
  181. self.filtrateButton.image = NSImage(named: "icon_annotation_screening_select")
  182. var dataArray: [KMBOTAAnnotationSection] = []
  183. for i in 0 ..< self.listView.document.pageCount {
  184. var annotationItemArray: [KMBOTAAnnotationItem] = []
  185. let page = self.listView.document.page(at: i)
  186. if page!.annotations.count > 0 {
  187. var filterAnnotations: [CPDFAnnotation] = page!.annotations
  188. if typeArr.count > 0 {
  189. filterAnnotations = (KMOCToolClass.filterAnnotation(filterAnnotations, types: typeArr) as! [CPDFAnnotation])
  190. }
  191. if (colorArr.count > 0) {
  192. filterAnnotations = (KMOCToolClass.filterAnnotation(filterAnnotations,colors: colorArr) as! [CPDFAnnotation])
  193. }
  194. if (authorArr.count > 0) {
  195. filterAnnotations = (KMOCToolClass.filterAnnotation(filterAnnotations,authors: authorArr) as! [CPDFAnnotation])
  196. }
  197. let section = KMBOTAAnnotationSection()
  198. for annotation in filterAnnotations {
  199. let item = KMBOTAAnnotationItem()
  200. item.section = section
  201. item.annotation = annotation
  202. item.index = Int(page!.pageIndex())
  203. annotationItemArray.append(item)
  204. }
  205. if annotationItemArray.count != 0 {
  206. section.annotations = annotationItemArray
  207. section.page = page
  208. section.isItemExpanded = true
  209. dataArray.append(section)
  210. }
  211. }
  212. }
  213. self.annotations = dataArray
  214. }
  215. }
  216. }
  217. }
  218. //MARK: reloadData
  219. extension KMAnnotationViewController {
  220. func updateExtempViewState() {
  221. if self.emptyView != nil {
  222. var hidden = false
  223. if self.annotationOutlineView.outlineView.numberOfRows != 0 {
  224. hidden = true
  225. }
  226. self.emptyView.isHidden = hidden
  227. self.annotationOutlineView.isHidden = !hidden
  228. //刷新
  229. self.updateUI()
  230. self.updateLanguage()
  231. }
  232. }
  233. }
  234. //MARK: Notification
  235. extension KMAnnotationViewController {
  236. @objc public func reloadDataAfter() {
  237. if !localEvent {
  238. self.reloadData()
  239. }
  240. // localEvent = false
  241. }
  242. @objc func documentPageCountChangedNotification(notification: NSNotification) {
  243. if notification.object is CPDFDocument {
  244. let pdfdocument : CPDFDocument = notification.object as! CPDFDocument
  245. if pdfdocument.isEqual(self.listView.document) {
  246. if !localEvent {
  247. self.reloadData()
  248. }
  249. // localEvent = false
  250. }
  251. }
  252. }
  253. @objc func CPDFListViewActiveAnnotationsChangeNotification(notification: NSNotification) {
  254. if notification.object is CPDFListView {
  255. let listView : CPDFListView = notification.object as! CPDFListView
  256. if listView.isEqual(self.listView) {
  257. if self.listView.activeAnnotations.count == 0 {
  258. self.escButtonAction(Any.self)
  259. } else {
  260. if !localEvent {
  261. let tempAnnotations : [CPDFAnnotation] = self.listView.activeAnnotations as! [CPDFAnnotation]
  262. var indexset = IndexSet()
  263. for annotation in tempAnnotations {
  264. if self.annotations.count > 0 {
  265. for section in self.annotations {
  266. for item in section.annotations! {
  267. if item.annotation == annotation {
  268. let index = self.annotationOutlineView.outlineView.row(forItem: item)
  269. indexset.insert(index)
  270. }
  271. }
  272. }
  273. }
  274. }
  275. if indexset.count != 0 && indexset.first != -1 {
  276. self.annotationOutlineView.outlineView.selectRowIndexes(indexset, byExtendingSelection: false)
  277. self.annotationOutlineView.didSelectItem(view: nil, event: NSEvent(), isNeedDelegate: false)
  278. }
  279. }
  280. // localEvent = false
  281. }
  282. }
  283. }
  284. }
  285. @objc func CPDFListViewAnnotationsAttributeHasChangeNotification(notification: NSNotification) {
  286. if notification.object is CPDFAnnotation {
  287. let annotation : CPDFAnnotation = notification.object as? CPDFAnnotation ?? CPDFAnnotation()
  288. for section in self.annotations {
  289. for (_, item) in section.annotations!.enumerated() {
  290. if item.annotation == annotation {
  291. let row = self.annotationOutlineView.outlineView.row(forItem: item)
  292. let indexSet = IndexSet.init(integer: row)
  293. self.annotationOutlineView.outlineView.noteHeightOfRows(withIndexesChanged: indexSet)
  294. self.annotationOutlineView.outlineView.reloadItem(item)
  295. break
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. //MARK: Action
  303. extension KMAnnotationViewController {
  304. @IBAction func tableViewDoubleAction(_ sender: Any) {
  305. if self.annotationOutlineView.selectItems.count > 1 {
  306. return
  307. }
  308. let selectedRow = self.annotationOutlineView.outlineView.selectedRow
  309. if selectedRow >= 0 {
  310. let annotationItem = self.annotationOutlineView.outlineView.item(atRow: selectedRow)
  311. if (annotationItem is KMBOTAAnnotationItem) {
  312. let annotation = (annotationItem as! KMBOTAAnnotationItem).annotation
  313. self.listView.go(to: annotation!.bounds, on: annotation!.page, animated: true)
  314. }
  315. }
  316. }
  317. @IBAction func moreButtonAction(_ sender: NSButton) {
  318. self.addMoreMenu(sender: sender)
  319. }
  320. @IBAction func filtrateButtonAction(_ sender: NSButton) {
  321. let menu = NSMenu()
  322. let filterVC = KMNoteOutlineFilterViewController()
  323. filterVC.path = self.listView.document.documentURL.path
  324. filterVC.notesArray = self.allAnnotations
  325. filterVC.applyFilterCallback = { [unowned self] (typeArr, colorArr,authArr,isEmpty) in
  326. menu.cancelTracking()
  327. self.annotationSort(sortArray: [typeArr!,colorArr!,authArr!])
  328. }
  329. filterVC.cancelCallback = { isCancel in
  330. if isCancel {
  331. menu.cancelTracking()
  332. }
  333. }
  334. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  335. item.target = self
  336. item.representedObject = filterVC
  337. item.view = filterVC.view
  338. menu.popUp(positioning: nil, at: CGPoint(x: -130, y: 30), in: sender)
  339. }
  340. //
  341. //
  342. @IBAction func deleteButtonAction(_ sender: Any) {
  343. let alert = NSAlert()
  344. alert.alertStyle = .critical
  345. alert.messageText = NSLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
  346. alert.informativeText = NSLocalizedString("You cannot undo this operation.", comment: "")
  347. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  348. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  349. alert.beginSheetModal(for: self.view.window!, completionHandler: { result in
  350. if result == .OK {
  351. for i in 0 ..< self.listView.document.pageCount {
  352. let page = self.listView.document.page(at: i)
  353. page?.removeAllAnnotations()
  354. }
  355. // let undoManager : UndoManager = self.mainWindowController.document?.undoManager ?? UndoManager()
  356. // undoManager.setActionName("")
  357. self.listView.updateActiveAnnotations([CPDFAnnotation()])
  358. self.listView.setNeedsDisplayForVisiblePages()
  359. self.reloadData()
  360. }
  361. })
  362. }
  363. //
  364. @IBAction func flattenButtonAction(_ sender: NSMenuItem) {
  365. let selects = sender.representedObject as! NSIndexSet
  366. var indexs : [Int] = []
  367. for index in selects {
  368. indexs.append(index)
  369. }
  370. if selects.count == 1 {
  371. let index = selects.firstIndex
  372. let annotationItem: KMBOTAAnnotationItem = self.annotationOutlineView.outlineView.item(atRow: index) as! KMBOTAAnnotationItem
  373. if annotationItem.annotation != nil {
  374. if annotationItem.annotation!.contents?.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) ?? 0 > 0 {
  375. var content: String = annotationItem.annotation!.contents!
  376. let item: CPDFMarkupAnnotation = annotationItem.annotation! as? CPDFMarkupAnnotation ?? CPDFMarkupAnnotation()
  377. if item.markupText() != nil {
  378. print(item.markupText() as Any)
  379. content = content + "\n" + item.markupText()
  380. }
  381. let pasteBoard = NSPasteboard.general
  382. pasteBoard.clearContents()
  383. pasteBoard.setString(content, forType: .string)
  384. }
  385. }
  386. }
  387. }
  388. //
  389. @IBAction func exportItemAction(_ sender: Any) {
  390. let panel = NSSavePanel()
  391. panel.nameFieldStringValue = "\(NSLocalizedString("Untitled", comment: "")).xfdf"
  392. panel.isExtensionHidden = true
  393. let response = panel.runModal()
  394. if response == .OK {
  395. let url = panel.url
  396. let result = self.listView.document.exportAnnotation(toXFDFPath: url!.path)
  397. if result {
  398. // NSWorkspace.shared.openFile(url!.path.deletingLastPathComponent)
  399. // NSWorkspace.shared.open(url!)
  400. let filePath = url!.path // 要打开的文件路径
  401. let fileURL = URL(fileURLWithPath: filePath)
  402. let fileDirectoryURL = fileURL.deletingLastPathComponent() // 获取文件所在的文件夹路径
  403. NSWorkspace.shared.activateFileViewerSelecting([fileDirectoryURL])
  404. }
  405. }
  406. }
  407. //
  408. @IBAction func importItemAction(_ sender: Any) {
  409. let panel = NSOpenPanel()
  410. panel.allowsMultipleSelection = false
  411. panel.allowedFileTypes = ["xfdf"]
  412. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  413. if response == .OK {
  414. let openPath = panel.url?.path
  415. let result = self.listView.document.importAnnotation(fromXFDFPath: openPath!)
  416. if result {
  417. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  418. self.reloadData()
  419. }
  420. }
  421. }
  422. }
  423. //
  424. @IBAction func deleteItemAction(_ sender: NSMenuItem) {
  425. let selects = sender.representedObject as! NSIndexSet
  426. var indexs : [KMBOTAAnnotationItem] = []
  427. for index in selects {
  428. if self.annotationOutlineView.outlineView.item(atRow: index) is KMBOTAAnnotationItem {
  429. let annotationItem: KMBOTAAnnotationItem = self.annotationOutlineView.outlineView.item(atRow: index) as! KMBOTAAnnotationItem
  430. annotationItem.index = index
  431. indexs.append(annotationItem)
  432. }
  433. }
  434. indexs.sort(){$0.index! > $1.index!}
  435. self.deleteAnnotations(annotationItems: indexs)
  436. }
  437. //
  438. @IBAction func deleteAllAnonationAction(_ sender: NSMenuItem) {
  439. let alter = NSAlert()
  440. alter.alertStyle = NSAlert.Style.informational
  441. alter.messageText = NSLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
  442. alter.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  443. alter.addButton(withTitle: NSLocalizedString("No", comment:""))
  444. let modlres = alter.runModal()
  445. if modlres == NSApplication.ModalResponse.alertFirstButtonReturn {
  446. // for i in 0 ..< self.listView.document.pageCount {
  447. // let page = self.listView.document.page(at: i)
  448. // for annotation in page!.annotations {
  449. // page?.removeAnnotation(annotation)
  450. //
  451. // }
  452. // }
  453. // self.listView.setNeedsDisplayForVisiblePages()
  454. // self.reloadData()
  455. var indexs : [KMBOTAAnnotationItem] = []
  456. for section in self.annotations {
  457. indexs.append(contentsOf: section.annotations!)
  458. }
  459. indexs.sort(){$0.index! > $1.index!}
  460. self.deleteAnnotations(annotationItems: indexs)
  461. }
  462. }
  463. //
  464. @IBAction func escButtonAction(_ sender: Any) {
  465. self.cancelSelect()
  466. }
  467. //
  468. func cancelSelect() {
  469. self.annotationOutlineView.cancelSelect()
  470. }
  471. //
  472. func selectItem(index: Int) {
  473. self.annotationOutlineView.outlineView.selectRowIndexes(IndexSet(integer: index), byExtendingSelection: false)
  474. self.annotationOutlineView.didSelectItem(view: nil, event: NSEvent(), isNeedDelegate: false)
  475. }
  476. func updateListViewData(annotationItems: [KMBOTAAnnotationItem]) {
  477. if annotationItems.count > 0 {
  478. if annotationItems.count == 1 {
  479. let annotationItem = annotationItems.first!
  480. if annotationItem.annotation != nil {
  481. self.listView.go(to: annotationItem.annotation!.bounds, on: annotationItem.annotation!.page, animated: true)
  482. }
  483. }
  484. var annotations: [CPDFAnnotation] = []
  485. for item in annotationItems {
  486. if item.annotation != nil {
  487. annotations.append(item.annotation!)
  488. }
  489. }
  490. self.listView.updateActiveAnnotations(annotations)
  491. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  492. }
  493. }
  494. }
  495. extension KMAnnotationViewController: KMAnnotationOutlineViewDelegate {
  496. func annotationOutlineView(_ outlineView: KMAnnotationOutlineView, rightMouseDownDidSelectView: NSView, evnet: NSEvent) {
  497. self.addRightMenuItem(view: rightMouseDownDidSelectView, event: evnet)
  498. }
  499. func annotationOutlineView(_ outlineView: KMAnnotationOutlineView, didReloadData: KMBOTAOutlineItem) {
  500. }
  501. func annotationOutlineView(_ outlineView: KMAnnotationOutlineView, didSelectItem: [KMBOTAAnnotationItem]) {
  502. self.localEvent = true
  503. self.updateListViewData(annotationItems: didSelectItem)
  504. self.localEvent = false
  505. }
  506. }
  507. //MARK: Menu
  508. extension KMAnnotationViewController : NSMenuDelegate, NSMenuItemValidation {
  509. @objc private func expandAllComments(sender:NSMenuItem) {
  510. self.annotationOutlineView.expandAllComments(item: sender)
  511. }
  512. @objc private func collapseAllComments(sender:NSMenuItem) {
  513. self.annotationOutlineView.collapseAllComments(item: sender)
  514. }
  515. // @objc private func expandAllComments(sender:NSMenuItem) {
  516. // if sender.tag == 0 {
  517. // self.annotationOutlineView.expandAllComments(item: sender)
  518. // } else if sender.tag == 1 {
  519. // self.annotationOutlineView.collapseAllComments(item: sender)
  520. // } else if sender.tag == 2 {
  521. // let alter = NSAlert()
  522. // alter.alertStyle = NSAlert.Style.informational
  523. // alter.messageText = NSLocalizedString("This will permanently remove all outlines. Are you sure to continue?", comment: "")
  524. // alter.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  525. // alter.addButton(withTitle: NSLocalizedString("No", comment:""))
  526. // let modlres = alter.runModal()
  527. // if modlres == NSApplication.ModalResponse.alertFirstButtonReturn {
  528. // self.deleteAllAnonationAction(sender)
  529. // }
  530. // }
  531. // }
  532. func addRightMenuItem(view: NSView, event: NSEvent) {
  533. let menu = NSMenu()
  534. menu.delegate = self
  535. let selectedRowIndexes = self.annotationOutlineView.outlineView.selectedRowIndexes
  536. var menuItem = NSMenuItem()
  537. if selectedRowIndexes.count == 1 {
  538. let item: KMBOTAAnnotationItem = self.annotationOutlineView.outlineView.item(atRow: selectedRowIndexes.first!) as! KMBOTAAnnotationItem
  539. if item.annotation != nil {
  540. if item.annotation!.contents != nil {
  541. if item.annotation!.contents.count > 0 {
  542. menuItem = menu.addItem(withTitle: NSLocalizedString("Copy Text", comment: ""), action: #selector(flattenButtonAction), target: self)
  543. menuItem.representedObject = selectedRowIndexes
  544. menu.addItem(NSMenuItem.separator())
  545. }
  546. }
  547. }
  548. }
  549. menuItem = menu.addItem(withTitle: NSLocalizedString("Export Annotation", comment: ""), action: #selector(exportItemAction), target: self)
  550. if self.annotationOutlineView.selectItems.count == 1 {
  551. menuItem = menu.addItem(withTitle: NSLocalizedString("Import Annotation", comment: ""), action: #selector(importItemAction), target: self)
  552. }
  553. menu.addItem(NSMenuItem.separator())
  554. menuItem = menu.addItem(withTitle: NSLocalizedString("Delete", comment: ""), action: #selector(deleteItemAction), target: self)
  555. menuItem.representedObject = selectedRowIndexes
  556. menu.addItem(NSMenuItem.separator())
  557. let point = view.convert(event.locationInWindow, from: nil)
  558. menu.popUp(positioning: nil, at: point, in: view)
  559. }
  560. func addMoreMenu(sender: NSView) {
  561. let moreMenu = NSMenu()
  562. moreMenu.addItem(withTitle: NSLocalizedString("Expand All", comment: ""), action: #selector(expandAllComments), target: self, tag: 0)
  563. moreMenu.addItem(withTitle: NSLocalizedString("Collapse All", comment: ""), action: #selector(collapseAllComments), target: self, tag: 1)
  564. // let soreItem = moreMenu.addItem(withTitle: NSLocalizedString("Sort", comment: ""), action: nil, target: self)
  565. // let soreMenu = NSMenu()
  566. // soreMenu.addItem(withTitle: NSLocalizedString("Page", comment: ""), action: #selector(expandAllComments), target: self, tag: 0)
  567. // soreMenu.addItem(withTitle: NSLocalizedString("Chronologically - ascending", comment: ""), action: #selector(expandAllComments), target: self, tag: 1)
  568. // soreMenu.addItem(withTitle: NSLocalizedString("Chronologically - reverse", comment: ""), action: #selector(expandAllComments), target: self, tag: 0)
  569. // soreItem?.submenu = soreMenu
  570. moreMenu.addItem(withTitle: NSLocalizedString("Import Annotations", comment: ""), action: #selector(importItemAction), target: self)
  571. moreMenu.addItem(withTitle: NSLocalizedString("Export Annotations to XFDF", comment: ""), action: #selector(exportItemAction), target: self)
  572. moreMenu.addItem(withTitle: NSLocalizedString("Remove All Annotations", comment: ""), action: #selector(deleteAllAnonationAction), target: self)
  573. let rect = sender.convert(sender.bounds, to: self.view)
  574. moreMenu.popUp(positioning: nil, at: NSPoint(x: rect.origin.x, y: rect.origin.y-10), in: self.view)
  575. }
  576. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  577. let action = menuItem.action
  578. if (action == #selector(undo)) {
  579. return self.listView.undoManager?.canUndo ?? false
  580. }
  581. if (action == #selector(redo)) {
  582. return self.listView.undoManager?.canRedo ?? false
  583. }
  584. if (action == #selector(flattenButtonAction)) {
  585. if self.annotationOutlineView.selectItems.count != 1 {
  586. return false
  587. }
  588. }
  589. if (action == #selector(deleteAllAnonationAction)) {
  590. if self.annotations.count == 0 {
  591. return false
  592. }
  593. }
  594. if action == #selector(exportItemAction) {
  595. if self.annotations.count == 0 {
  596. return false
  597. }
  598. }
  599. if action == #selector(exportItemAction) {
  600. if self.annotations.count == 0 {
  601. return false
  602. }
  603. }
  604. if (action == #selector(expandAllComments)) {
  605. var canExpand = false
  606. for row in 0..<self.annotationOutlineView.outlineView.numberOfRows {
  607. // 检查当前项目是否可以展开
  608. let item = self.annotationOutlineView.outlineView.item(atRow: row)
  609. if self.annotationOutlineView.outlineView.isExpandable(item) {
  610. if !self.annotationOutlineView.outlineView.isItemExpanded(item) {
  611. canExpand = true
  612. break
  613. }
  614. }
  615. }
  616. return canExpand
  617. }
  618. if (action == #selector(collapseAllComments)) {
  619. var canCollapse = false
  620. for row in 0..<self.annotationOutlineView.outlineView.numberOfRows {
  621. let item = self.annotationOutlineView.outlineView.item(atRow: row)
  622. if self.annotationOutlineView.outlineView.isExpandable(item) {
  623. if self.annotationOutlineView.outlineView.isItemExpanded(item) {
  624. canCollapse = true
  625. break
  626. }
  627. }
  628. }
  629. return canCollapse
  630. }
  631. return true
  632. }
  633. }
  634. //MARK: undo Redo
  635. extension KMAnnotationViewController {
  636. func deleteAnnotations(annotationItems: [KMBOTAAnnotationItem]) {
  637. self.removeNotification()
  638. self.localEvent = true
  639. var removeAnnotations: [Any] = []
  640. var tempAnnotations: [KMBOTAAnnotationItem] = []
  641. for annotationItem in annotationItems {
  642. let annotation = annotationItem.annotation
  643. annotationItem.index = annotationItem.section?.annotations?.firstIndex(of: annotationItem)
  644. let page = annotation?.page
  645. if ((page?.annotations.contains(annotation!)) != nil) {
  646. page?.removeAnnotation(annotation)
  647. annotationItem.section?.annotations?.removeObject(annotationItem)
  648. } else {
  649. print("不存在")
  650. }
  651. if self.listView.activeAnnotations.contains(annotation!) {
  652. removeAnnotations.append(annotation!)
  653. }
  654. if annotation != nil {
  655. tempAnnotations.append(annotationItem)
  656. }
  657. }
  658. if removeAnnotations.count != 0 {
  659. self.listView.activeAnnotations.remove(removeAnnotations)
  660. }
  661. self.listView.setNeedsDisplayForVisiblePages()
  662. self.annotationOutlineView.reloadData(expandItemType: .none)
  663. self.updateExtempViewState()
  664. self.listView.undoManager?.registerUndo(withTarget: self) { [unowned self] targetType in
  665. self.addAnnotations(annotationItems: tempAnnotations)
  666. }
  667. self.addNotification()
  668. self.localEvent = false
  669. }
  670. func addAnnotations(annotationItems: [KMBOTAAnnotationItem]) {
  671. self.removeNotification()
  672. self.localEvent = true
  673. var tempAnnotationItems: [KMBOTAAnnotationItem] = annotationItems
  674. tempAnnotationItems.sort(){$0.index ?? 0 < $1.index ?? 0}
  675. for annotationItem in tempAnnotationItems {
  676. if !annotationItem.annotation!.page.annotations.contains(annotationItem.annotation!) {
  677. annotationItem.annotation?.page.addAnnotation(annotationItem.annotation!)
  678. annotationItem.section?.annotations?.insert(annotationItem, at: annotationItem.index ?? 0)
  679. }
  680. }
  681. self.listView.setNeedsDisplayForVisiblePages()
  682. self.annotationOutlineView.reloadData(expandItemType: .none)
  683. if tempAnnotationItems.count != 0 {
  684. let row = self.annotationOutlineView.outlineView.row(forItem: tempAnnotationItems.last)
  685. self.annotationOutlineView.outlineView.scrollRowToVisible(row)
  686. }
  687. self.updateExtempViewState()
  688. let tempAnnotations: [KMBOTAAnnotationItem] = tempAnnotationItems
  689. self.listView.undoManager?.registerUndo(withTarget: self) { [unowned self] targetType in
  690. self.deleteAnnotations(annotationItems: tempAnnotations)
  691. }
  692. self.addNotification()
  693. self.localEvent = false
  694. }
  695. @IBAction func undo(_ sender: Any) {
  696. if (self.listView.undoManager?.canUndo ?? false) {
  697. self.listView.undoManager?.undo()
  698. }
  699. }
  700. @IBAction func redo(_ sender: Any) {
  701. if (self.listView.undoManager?.canRedo ?? false) {
  702. self.listView.undoManager?.redo()
  703. }
  704. }
  705. }