KMAnnotationViewController.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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. let pdfdocument : CPDFDocument = notification.object as! CPDFDocument
  244. if pdfdocument.isEqual(self.listView.document) {
  245. if !localEvent {
  246. self.reloadData()
  247. }
  248. // localEvent = false
  249. }
  250. }
  251. @objc func CPDFListViewActiveAnnotationsChangeNotification(notification: NSNotification) {
  252. let listView : CPDFListView = notification.object as! CPDFListView
  253. if listView.isEqual(self.listView) {
  254. if self.listView.activeAnnotations.count == 0 {
  255. self.escButtonAction(Any.self)
  256. } else {
  257. if !localEvent {
  258. let tempAnnotations : [CPDFAnnotation] = self.listView.activeAnnotations as! [CPDFAnnotation]
  259. var indexset = IndexSet()
  260. for annotation in tempAnnotations {
  261. if self.annotations.count > 0 {
  262. for section in self.annotations {
  263. for item in section.annotations! {
  264. if item.annotation == annotation {
  265. let index = self.annotationOutlineView.outlineView.row(forItem: item)
  266. indexset.insert(index)
  267. }
  268. }
  269. }
  270. }
  271. }
  272. if indexset.count != 0 && indexset.first != -1 {
  273. self.annotationOutlineView.outlineView.selectRowIndexes(indexset, byExtendingSelection: false)
  274. self.annotationOutlineView.didSelectItem(view: nil, event: NSEvent(), isNeedDelegate: false)
  275. }
  276. }
  277. // localEvent = false
  278. }
  279. }
  280. }
  281. @objc func CPDFListViewAnnotationsAttributeHasChangeNotification(notification: NSNotification) {
  282. let annotation : CPDFAnnotation = notification.object as? CPDFAnnotation ?? CPDFAnnotation()
  283. for section in self.annotations {
  284. for (_, item) in section.annotations!.enumerated() {
  285. if item.annotation == annotation {
  286. let row = self.annotationOutlineView.outlineView.row(forItem: item)
  287. let indexSet = IndexSet.init(integer: row)
  288. self.annotationOutlineView.outlineView.noteHeightOfRows(withIndexesChanged: indexSet)
  289. self.annotationOutlineView.outlineView.reloadItem(item)
  290. break
  291. }
  292. }
  293. }
  294. }
  295. }
  296. //MARK: Action
  297. extension KMAnnotationViewController {
  298. @IBAction func tableViewDoubleAction(_ sender: Any) {
  299. if self.annotationOutlineView.selectItems.count > 1 {
  300. return
  301. }
  302. let selectedRow = self.annotationOutlineView.outlineView.selectedRow
  303. if selectedRow >= 0 {
  304. let annotationItem = self.annotationOutlineView.outlineView.item(atRow: selectedRow)
  305. if (annotationItem is KMBOTAAnnotationItem) {
  306. let annotation = (annotationItem as! KMBOTAAnnotationItem).annotation
  307. self.listView.go(to: annotation!.bounds, on: annotation!.page, animated: true)
  308. }
  309. }
  310. }
  311. @IBAction func moreButtonAction(_ sender: NSButton) {
  312. self.addMoreMenu(sender: sender)
  313. }
  314. @IBAction func filtrateButtonAction(_ sender: NSButton) {
  315. let menu = NSMenu()
  316. let filterVC = KMNoteOutlineFilterViewController()
  317. filterVC.path = self.listView.document.documentURL.path
  318. filterVC.notesArray = self.allAnnotations
  319. filterVC.applyFilterCallback = { [unowned self] (typeArr, colorArr,authArr,isEmpty) in
  320. menu.cancelTracking()
  321. self.annotationSort(sortArray: [typeArr!,colorArr!,authArr!])
  322. }
  323. filterVC.cancelCallback = { isCancel in
  324. if isCancel {
  325. menu.cancelTracking()
  326. }
  327. }
  328. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  329. item.target = self
  330. item.representedObject = filterVC
  331. item.view = filterVC.view
  332. menu.popUp(positioning: nil, at: CGPoint(x: -130, y: 30), in: sender)
  333. }
  334. //
  335. //
  336. @IBAction func deleteButtonAction(_ sender: Any) {
  337. let alert = NSAlert()
  338. alert.alertStyle = .critical
  339. alert.messageText = NSLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
  340. alert.informativeText = NSLocalizedString("You cannot undo this operation.", comment: "")
  341. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  342. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  343. alert.beginSheetModal(for: self.view.window!, completionHandler: { result in
  344. if result == .OK {
  345. for i in 0 ..< self.listView.document.pageCount {
  346. let page = self.listView.document.page(at: i)
  347. page?.removeAllAnnotations()
  348. }
  349. // let undoManager : UndoManager = self.mainWindowController.document?.undoManager ?? UndoManager()
  350. // undoManager.setActionName("")
  351. self.listView.updateActiveAnnotations([CPDFAnnotation()])
  352. self.listView.setNeedsDisplayForVisiblePages()
  353. self.reloadData()
  354. }
  355. })
  356. }
  357. //
  358. @IBAction func flattenButtonAction(_ sender: NSMenuItem) {
  359. let selects = sender.representedObject as! NSIndexSet
  360. var indexs : [Int] = []
  361. for index in selects {
  362. indexs.append(index)
  363. }
  364. if selects.count == 1 {
  365. let index = selects.firstIndex
  366. let annotationItem: KMBOTAAnnotationItem = self.annotationOutlineView.outlineView.item(atRow: index) as! KMBOTAAnnotationItem
  367. if annotationItem.annotation != nil {
  368. if annotationItem.annotation!.contents?.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) ?? 0 > 0 {
  369. var content: String = annotationItem.annotation!.contents!
  370. let item: CPDFMarkupAnnotation = annotationItem.annotation! as? CPDFMarkupAnnotation ?? CPDFMarkupAnnotation()
  371. if item.markupText() != nil {
  372. print(item.markupText() as Any)
  373. content = content + "\n" + item.markupText()
  374. }
  375. let pasteBoard = NSPasteboard.general
  376. pasteBoard.clearContents()
  377. pasteBoard.setString(content, forType: .string)
  378. }
  379. }
  380. }
  381. }
  382. //
  383. @IBAction func exportItemAction(_ sender: Any) {
  384. let panel = NSSavePanel()
  385. panel.nameFieldStringValue = "\(NSLocalizedString("Untitled", comment: "")).xfdf"
  386. panel.isExtensionHidden = true
  387. let response = panel.runModal()
  388. if response == .OK {
  389. let url = panel.url
  390. let result = self.listView.document.exportAnnotation(toXFDFPath: url!.path)
  391. if result {
  392. // NSWorkspace.shared.openFile(url!.path.deletingLastPathComponent)
  393. // NSWorkspace.shared.open(url!)
  394. let filePath = url!.path // 要打开的文件路径
  395. let fileURL = URL(fileURLWithPath: filePath)
  396. let fileDirectoryURL = fileURL.deletingLastPathComponent() // 获取文件所在的文件夹路径
  397. NSWorkspace.shared.activateFileViewerSelecting([fileDirectoryURL])
  398. }
  399. }
  400. }
  401. //
  402. @IBAction func importItemAction(_ sender: Any) {
  403. let panel = NSOpenPanel()
  404. panel.allowsMultipleSelection = false
  405. panel.allowedFileTypes = ["xfdf"]
  406. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  407. if response == .OK {
  408. let openPath = panel.url?.path
  409. let result = self.listView.document.importAnnotation(fromXFDFPath: openPath!)
  410. if result {
  411. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  412. self.reloadData()
  413. }
  414. }
  415. }
  416. }
  417. //
  418. @IBAction func deleteItemAction(_ sender: NSMenuItem) {
  419. let selects = sender.representedObject as! NSIndexSet
  420. var indexs : [KMBOTAAnnotationItem] = []
  421. for index in selects {
  422. if self.annotationOutlineView.outlineView.item(atRow: index) is KMBOTAAnnotationItem {
  423. let annotationItem: KMBOTAAnnotationItem = self.annotationOutlineView.outlineView.item(atRow: index) as! KMBOTAAnnotationItem
  424. annotationItem.index = index
  425. indexs.append(annotationItem)
  426. }
  427. }
  428. indexs.sort(){$0.index! > $1.index!}
  429. self.deleteAnnotations(annotationItems: indexs)
  430. }
  431. //
  432. @IBAction func deleteAllAnonationAction(_ sender: NSMenuItem) {
  433. let alter = NSAlert()
  434. alter.alertStyle = NSAlert.Style.informational
  435. alter.messageText = NSLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
  436. alter.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  437. alter.addButton(withTitle: NSLocalizedString("No", comment:""))
  438. let modlres = alter.runModal()
  439. if modlres == NSApplication.ModalResponse.alertFirstButtonReturn {
  440. // for i in 0 ..< self.listView.document.pageCount {
  441. // let page = self.listView.document.page(at: i)
  442. // for annotation in page!.annotations {
  443. // page?.removeAnnotation(annotation)
  444. //
  445. // }
  446. // }
  447. // self.listView.setNeedsDisplayForVisiblePages()
  448. // self.reloadData()
  449. var indexs : [KMBOTAAnnotationItem] = []
  450. for section in self.annotations {
  451. indexs.append(contentsOf: section.annotations!)
  452. }
  453. indexs.sort(){$0.index! > $1.index!}
  454. self.deleteAnnotations(annotationItems: indexs)
  455. }
  456. }
  457. //
  458. @IBAction func escButtonAction(_ sender: Any) {
  459. self.cancelSelect()
  460. }
  461. //
  462. func cancelSelect() {
  463. self.annotationOutlineView.cancelSelect()
  464. }
  465. //
  466. func selectItem(index: Int) {
  467. self.annotationOutlineView.outlineView.selectRowIndexes(IndexSet(integer: index), byExtendingSelection: false)
  468. self.annotationOutlineView.didSelectItem(view: nil, event: NSEvent(), isNeedDelegate: false)
  469. }
  470. func updateListViewData(annotationItems: [KMBOTAAnnotationItem]) {
  471. if annotationItems.count > 0 {
  472. if annotationItems.count == 1 {
  473. let annotationItem = annotationItems.first!
  474. if annotationItem.annotation != nil {
  475. self.listView.go(to: annotationItem.annotation!.bounds, on: annotationItem.annotation!.page, animated: true)
  476. }
  477. }
  478. var annotations: [CPDFAnnotation] = []
  479. for item in annotationItems {
  480. if item.annotation != nil {
  481. annotations.append(item.annotation!)
  482. }
  483. }
  484. self.listView.updateActiveAnnotations(annotations)
  485. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  486. }
  487. }
  488. }
  489. extension KMAnnotationViewController: KMAnnotationOutlineViewDelegate {
  490. func annotationOutlineView(_ outlineView: KMAnnotationOutlineView, rightMouseDownDidSelectView: NSView, evnet: NSEvent) {
  491. self.addRightMenuItem(view: rightMouseDownDidSelectView, event: evnet)
  492. }
  493. func annotationOutlineView(_ outlineView: KMAnnotationOutlineView, didReloadData: KMBOTAOutlineItem) {
  494. }
  495. func annotationOutlineView(_ outlineView: KMAnnotationOutlineView, didSelectItem: [KMBOTAAnnotationItem]) {
  496. self.localEvent = true
  497. self.updateListViewData(annotationItems: didSelectItem)
  498. self.localEvent = false
  499. }
  500. }
  501. //MARK: Menu
  502. extension KMAnnotationViewController : NSMenuDelegate, NSMenuItemValidation {
  503. @objc private func expandAllComments(sender:NSMenuItem) {
  504. self.annotationOutlineView.expandAllComments(item: sender)
  505. }
  506. @objc private func collapseAllComments(sender:NSMenuItem) {
  507. self.annotationOutlineView.collapseAllComments(item: sender)
  508. }
  509. // @objc private func expandAllComments(sender:NSMenuItem) {
  510. // if sender.tag == 0 {
  511. // self.annotationOutlineView.expandAllComments(item: sender)
  512. // } else if sender.tag == 1 {
  513. // self.annotationOutlineView.collapseAllComments(item: sender)
  514. // } else if sender.tag == 2 {
  515. // let alter = NSAlert()
  516. // alter.alertStyle = NSAlert.Style.informational
  517. // alter.messageText = NSLocalizedString("This will permanently remove all outlines. Are you sure to continue?", comment: "")
  518. // alter.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  519. // alter.addButton(withTitle: NSLocalizedString("No", comment:""))
  520. // let modlres = alter.runModal()
  521. // if modlres == NSApplication.ModalResponse.alertFirstButtonReturn {
  522. // self.deleteAllAnonationAction(sender)
  523. // }
  524. // }
  525. // }
  526. func addRightMenuItem(view: NSView, event: NSEvent) {
  527. let menu = NSMenu()
  528. menu.delegate = self
  529. let selectedRowIndexes = self.annotationOutlineView.outlineView.selectedRowIndexes
  530. var menuItem = NSMenuItem()
  531. if selectedRowIndexes.count == 1 {
  532. let item: KMBOTAAnnotationItem = self.annotationOutlineView.outlineView.item(atRow: selectedRowIndexes.first!) as! KMBOTAAnnotationItem
  533. if item.annotation != nil {
  534. if item.annotation!.contents != nil {
  535. if item.annotation!.contents.count > 0 {
  536. menuItem = menu.addItem(withTitle: NSLocalizedString("Copy Text", comment: ""), action: #selector(flattenButtonAction), target: self)
  537. menuItem.representedObject = selectedRowIndexes
  538. menu.addItem(NSMenuItem.separator())
  539. }
  540. }
  541. }
  542. }
  543. menuItem = menu.addItem(withTitle: NSLocalizedString("Export Annotation", comment: ""), action: #selector(exportItemAction), target: self)
  544. if self.annotationOutlineView.selectItems.count == 1 {
  545. menuItem = menu.addItem(withTitle: NSLocalizedString("Import Annotation", comment: ""), action: #selector(importItemAction), target: self)
  546. }
  547. menu.addItem(NSMenuItem.separator())
  548. menuItem = menu.addItem(withTitle: NSLocalizedString("Delete", comment: ""), action: #selector(deleteItemAction), target: self)
  549. menuItem.representedObject = selectedRowIndexes
  550. menu.addItem(NSMenuItem.separator())
  551. let point = view.convert(event.locationInWindow, from: nil)
  552. menu.popUp(positioning: nil, at: point, in: view)
  553. }
  554. func addMoreMenu(sender: NSView) {
  555. let moreMenu = NSMenu()
  556. moreMenu.addItem(withTitle: NSLocalizedString("Expand All", comment: ""), action: #selector(expandAllComments), target: self, tag: 0)
  557. moreMenu.addItem(withTitle: NSLocalizedString("Collapse All", comment: ""), action: #selector(collapseAllComments), target: self, tag: 1)
  558. // let soreItem = moreMenu.addItem(withTitle: NSLocalizedString("Sort", comment: ""), action: nil, target: self)
  559. // let soreMenu = NSMenu()
  560. // soreMenu.addItem(withTitle: NSLocalizedString("Page", comment: ""), action: #selector(expandAllComments), target: self, tag: 0)
  561. // soreMenu.addItem(withTitle: NSLocalizedString("Chronologically - ascending", comment: ""), action: #selector(expandAllComments), target: self, tag: 1)
  562. // soreMenu.addItem(withTitle: NSLocalizedString("Chronologically - reverse", comment: ""), action: #selector(expandAllComments), target: self, tag: 0)
  563. // soreItem?.submenu = soreMenu
  564. moreMenu.addItem(withTitle: NSLocalizedString("Import Annotations", comment: ""), action: #selector(importItemAction), target: self)
  565. moreMenu.addItem(withTitle: NSLocalizedString("Export Annotations to XFDF", comment: ""), action: #selector(exportItemAction), target: self)
  566. moreMenu.addItem(withTitle: NSLocalizedString("Remove All Annotations", comment: ""), action: #selector(deleteAllAnonationAction), target: self)
  567. let rect = sender.convert(sender.bounds, to: self.view)
  568. moreMenu.popUp(positioning: nil, at: NSPoint(x: rect.origin.x, y: rect.origin.y-10), in: self.view)
  569. }
  570. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  571. let action = menuItem.action
  572. if (action == #selector(undo)) {
  573. return self.listView.undoManager?.canUndo ?? false
  574. }
  575. if (action == #selector(redo)) {
  576. return self.listView.undoManager?.canRedo ?? false
  577. }
  578. if (action == #selector(flattenButtonAction)) {
  579. if self.annotationOutlineView.selectItems.count != 1 {
  580. return false
  581. }
  582. }
  583. if (action == #selector(deleteAllAnonationAction)) {
  584. if self.annotations.count == 0 {
  585. return false
  586. }
  587. }
  588. if action == #selector(exportItemAction) {
  589. if self.annotations.count == 0 {
  590. return false
  591. }
  592. }
  593. if action == #selector(exportItemAction) {
  594. if self.annotations.count == 0 {
  595. return false
  596. }
  597. }
  598. if (action == #selector(expandAllComments)) {
  599. var canExpand = false
  600. for row in 0..<self.annotationOutlineView.outlineView.numberOfRows {
  601. // 检查当前项目是否可以展开
  602. let item = self.annotationOutlineView.outlineView.item(atRow: row)
  603. if self.annotationOutlineView.outlineView.isExpandable(item) {
  604. if !self.annotationOutlineView.outlineView.isItemExpanded(item) {
  605. canExpand = true
  606. break
  607. }
  608. }
  609. }
  610. return canExpand
  611. }
  612. if (action == #selector(collapseAllComments)) {
  613. var canCollapse = false
  614. for row in 0..<self.annotationOutlineView.outlineView.numberOfRows {
  615. let item = self.annotationOutlineView.outlineView.item(atRow: row)
  616. if self.annotationOutlineView.outlineView.isExpandable(item) {
  617. if self.annotationOutlineView.outlineView.isItemExpanded(item) {
  618. canCollapse = true
  619. break
  620. }
  621. }
  622. }
  623. return canCollapse
  624. }
  625. return true
  626. }
  627. }
  628. //MARK: undo Redo
  629. extension KMAnnotationViewController {
  630. func deleteAnnotations(annotationItems: [KMBOTAAnnotationItem]) {
  631. self.removeNotification()
  632. self.localEvent = true
  633. var removeAnnotations: [Any] = []
  634. var tempAnnotations: [KMBOTAAnnotationItem] = []
  635. for annotationItem in annotationItems {
  636. let annotation = annotationItem.annotation
  637. annotationItem.index = annotationItem.section?.annotations?.firstIndex(of: annotationItem)
  638. let page = annotation?.page
  639. if ((page?.annotations.contains(annotation!)) != nil) {
  640. page?.removeAnnotation(annotation)
  641. annotationItem.section?.annotations?.removeObject(annotationItem)
  642. } else {
  643. print("不存在")
  644. }
  645. if self.listView.activeAnnotations.contains(annotation!) {
  646. removeAnnotations.append(annotation!)
  647. }
  648. if annotation != nil {
  649. tempAnnotations.append(annotationItem)
  650. }
  651. }
  652. if removeAnnotations.count != 0 {
  653. self.listView.activeAnnotations.remove(removeAnnotations)
  654. }
  655. self.listView.setNeedsDisplayForVisiblePages()
  656. self.annotationOutlineView.reloadData(expandItemType: .none)
  657. self.updateExtempViewState()
  658. self.listView.undoManager?.registerUndo(withTarget: self) { [unowned self] targetType in
  659. self.addAnnotations(annotationItems: tempAnnotations)
  660. }
  661. self.addNotification()
  662. self.localEvent = false
  663. }
  664. func addAnnotations(annotationItems: [KMBOTAAnnotationItem]) {
  665. self.removeNotification()
  666. self.localEvent = true
  667. var tempAnnotationItems: [KMBOTAAnnotationItem] = annotationItems
  668. tempAnnotationItems.sort(){$0.index ?? 0 < $1.index ?? 0}
  669. for annotationItem in tempAnnotationItems {
  670. if !annotationItem.annotation!.page.annotations.contains(annotationItem.annotation!) {
  671. annotationItem.annotation?.page.addAnnotation(annotationItem.annotation!)
  672. annotationItem.section?.annotations?.insert(annotationItem, at: annotationItem.index ?? 0)
  673. }
  674. }
  675. self.listView.setNeedsDisplayForVisiblePages()
  676. self.annotationOutlineView.reloadData(expandItemType: .none)
  677. if tempAnnotationItems.count != 0 {
  678. let row = self.annotationOutlineView.outlineView.row(forItem: tempAnnotationItems.last)
  679. self.annotationOutlineView.outlineView.scrollRowToVisible(row)
  680. }
  681. self.updateExtempViewState()
  682. let tempAnnotations: [KMBOTAAnnotationItem] = tempAnnotationItems
  683. self.listView.undoManager?.registerUndo(withTarget: self) { [unowned self] targetType in
  684. self.deleteAnnotations(annotationItems: tempAnnotations)
  685. }
  686. self.addNotification()
  687. self.localEvent = false
  688. }
  689. @IBAction func undo(_ sender: Any) {
  690. if (self.listView.undoManager?.canUndo ?? false) {
  691. self.listView.undoManager?.undo()
  692. }
  693. }
  694. @IBAction func redo(_ sender: Any) {
  695. if (self.listView.undoManager?.canRedo ?? false) {
  696. self.listView.undoManager?.redo()
  697. }
  698. }
  699. }