KMAnnotationViewController.swift 33 KB

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