KMOutlineViewController.swift 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. //
  2. // KMOutlineViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/10/10.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMOutlineViewController: KMNBaseViewController {
  10. @IBOutlet var contendView: NSView!
  11. @IBOutlet weak var topView: NSView!
  12. @IBOutlet weak var titleLabel: NSTextField!
  13. @IBOutlet weak var lineView: NSView!
  14. @IBOutlet weak var addButton: NSButton!
  15. @IBOutlet weak var moreButton: NSButton!
  16. @IBOutlet var topSepline: NSView!
  17. @IBOutlet weak var emptyView: NSView!
  18. @IBOutlet weak var bigTipLabel: NSTextField!
  19. @IBOutlet weak var tipLabel: NSTextField!
  20. @IBOutlet weak var BOTAOutlineView: KMBOTAOutlineView!
  21. var dragPDFOutline : KMBOTAOutlineItem!
  22. var renameTextField : NSTextField!
  23. // var listView : CPDFListView!
  24. var renamePDFOutline : KMBOTAOutlineItem!
  25. let moreMenu = NSMenu()
  26. var isLocalEvent = false
  27. var handdler = KMNOutlineHanddler()
  28. private weak var popover_: NSPopover?
  29. private lazy var searchButton_: ComponentButton = {
  30. let view = ComponentButton()
  31. view.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, state: .normal, isDisable: false, onlyIcon: true)
  32. return view
  33. }()
  34. private lazy var addButton_: ComponentButton = {
  35. let view = ComponentButton()
  36. view.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, state: .normal, isDisable: false, onlyIcon: true, keepPressState: false)
  37. return view
  38. }()
  39. private lazy var moreDropdown_: ComponentDropdown = {
  40. let view = ComponentDropdown()
  41. view.properties = ComponentDropdownProperty(type: .split_primary, size: .xxs, state: .normal, split: false, disabled: false)
  42. return view
  43. }()
  44. private lazy var headerSearchView_: KMNBotaHeaderSearchView? = {
  45. let view = KMNBotaHeaderSearchView.createFromNib()
  46. return view
  47. }()
  48. private var emptyView_: ComponentEmpty = {
  49. let view = ComponentEmpty()
  50. view.properties = ComponentEmptyProperty(emptyType: .noOutline, state: .normal, image: NSImage(named: "KMImageNameOutlineEmpty"), text: KMLocalizedString("No Outline"), subText: KMLocalizedString("Here is the description."))
  51. return view
  52. }()
  53. private var groupView_: ComponentGroup?
  54. private var menuGroupView_: ComponentGroup?
  55. deinit {
  56. self.BOTAOutlineView.delegate = nil
  57. }
  58. override func viewWillDisappear() {
  59. super.viewWillDisappear()
  60. self.cancelSelect()
  61. }
  62. override func viewDidLoad() {
  63. super.viewDidLoad()
  64. handdler.delegate = self
  65. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  66. self.topView.wantsLayer = true
  67. addButton.image = nil
  68. topView.addSubview(searchButton_)
  69. searchButton_.km_add_size_constraint(size: NSMakeSize(24, 24))
  70. searchButton_.km_add_centerY_constraint(constant: 1)
  71. searchButton_.km_add_trailing_constraint(equalTo: addButton, attribute: .leading, constant: -4)
  72. searchButton_.setTarget(self, action: #selector(_searchAction))
  73. addButton.addSubview(addButton_)
  74. addButton_.km_add_size_constraint(size: NSMakeSize(24, 24))
  75. addButton_.km_add_centerX_constraint()
  76. addButton_.km_add_centerY_constraint()
  77. addButton_.setTarget(self, action: #selector(addNewOutline))
  78. moreButton.image = nil
  79. moreButton.addSubview(moreDropdown_)
  80. moreDropdown_.km_add_size_constraint(size: NSMakeSize(24, 24))
  81. moreDropdown_.km_add_centerX_constraint()
  82. moreDropdown_.km_add_centerY_constraint()
  83. moreDropdown_.componentDelegate = self
  84. if let data = headerSearchView_ {
  85. topView.addSubview(data)
  86. headerSearchView_?.frame = topView.bounds
  87. headerSearchView_?.autoresizingMask = [.width, .height]
  88. }
  89. _hideHeaderSearch()
  90. headerSearchView_?.itemClick = { [weak self] idx, _ in
  91. if idx == 1 { // 忽略大小写
  92. } else if idx == 2 { // 关闭搜索
  93. self?._hideHeaderSearch()
  94. }
  95. }
  96. emptyView.wantsLayer = true
  97. bigTipLabel.stringValue = ""
  98. tipLabel.stringValue = ""
  99. emptyView.addSubview(emptyView_)
  100. emptyView_.km_add_top_constraint(constant: 232)
  101. emptyView_.km_add_bottom_constraint()
  102. emptyView_.km_add_leading_constraint()
  103. emptyView_.km_add_trailing_constraint()
  104. self.BOTAOutlineView.delegate = self
  105. self.BOTAOutlineView.inputData = self.handdler.outlineRoot()
  106. self.BOTAOutlineView.outlineView.doubleAction = #selector(outlineViewDoubleAction)
  107. }
  108. // To create an outline, please right-click on the selected page and choose \"Add Outline\", or click \"Add\" in the upper right corner.
  109. override func updateUILanguage() {
  110. super.updateUILanguage()
  111. KMMainThreadExecute {
  112. self.titleLabel.stringValue = KMLocalizedString("Outline")
  113. self.moreButton.toolTip = KMLocalizedString("More")
  114. self.addButton.toolTip = KMLocalizedString("Add Outline")
  115. }
  116. }
  117. override func updateUIThemeColor() {
  118. super.updateUIThemeColor()
  119. KMMainThreadExecute {
  120. self.contendView.wantsLayer = true
  121. let color = KMNColorTools.colorBg_layoutMiddle()
  122. self.contendView.layer?.backgroundColor = color.cgColor
  123. self.headerSearchView_?.wantsLayer = true
  124. self.headerSearchView_?.layer?.backgroundColor = color.cgColor
  125. self.headerSearchView_?.bottomLine.wantsLayer = true
  126. self.headerSearchView_?.bottomLine.layer?.backgroundColor = KMNColorTools.colorPrimary_border1().cgColor
  127. self.titleLabel.textColor = KMNColorTools.colorText_2()
  128. self.addButton_.properties.icon = NSImage(named: "KMBookmarkAdd")
  129. self.addButton_.reloadData()
  130. self.searchButton_.properties.icon = NSImage(named: "KMImageNameOutlineSearch")
  131. self.searchButton_.reloadData()
  132. let dividerColor = KMNColorTools.colorBorder_divider()
  133. self.topSepline.wantsLayer = true
  134. self.topSepline.layer?.backgroundColor = dividerColor.cgColor
  135. self.lineView.backgroundColor(dividerColor)
  136. }
  137. }
  138. override func addNotifations() {
  139. super.addNotifations()
  140. NotificationCenter.default.addObserver(self, selector: #selector(KMPDFViewCurrentPageDidChangedNotification), name: NSNotification.Name.init(rawValue: "KMPDFViewCurrentPageDidChanged"), object: nil)
  141. }
  142. func addRightMenu(view: NSView, event: NSEvent) {
  143. let point = event.locationInWindow
  144. let tempView = view
  145. var viewHeight: CGFloat = 0
  146. let items: [String] = ["Add Item", "Add Sub-Item", "Add A Higher Level","", "Delete","", "Edit", "Rename", "Change Destination","", "Promote", "Demote"]
  147. var menuItemArr: [ComponentMenuitemProperty] = []
  148. for value in items {
  149. if value.count == 0 {
  150. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  151. menuItemArr.append(property)
  152. viewHeight += 8
  153. } else {
  154. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  155. itemSelected: false,
  156. isDisabled: false,
  157. keyEquivalent: nil,
  158. text: KMLocalizedString(value),
  159. identifier: value)
  160. menuItemArr.append(properties_Menuitem)
  161. viewHeight += 36
  162. }
  163. }
  164. if self.BOTAOutlineView.outlineView.selectedRowIndexes.count > 1 {
  165. for data in menuItemArr {
  166. if data.text == KMLocalizedString("Delete") {
  167. data.isDisabled = false
  168. } else {
  169. data.isDisabled = true
  170. }
  171. }
  172. if menuGroupView_ == nil {
  173. menuGroupView_ = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  174. }
  175. if menuGroupView_ != nil {
  176. menuGroupView_?.clickedAutoHide = false
  177. menuGroupView_?.groupDelegate = self
  178. menuGroupView_?.frame = CGRectMake(0, 0, 180, viewHeight)
  179. menuGroupView_?.updateGroupInfo(menuItemArr)
  180. menuGroupView_?.showWithPoint(CGPoint(x: point.x, y: point.y - viewHeight), relativeTo: tempView)
  181. }
  182. return
  183. } else if self.BOTAOutlineView.outlineView.selectedRowIndexes.count > 0 {
  184. for data in menuItemArr {
  185. if data.text == KMLocalizedString("Add Sub-Item") || data.text == KMLocalizedString("Change Destination") {
  186. data.isDisabled = false
  187. } else {
  188. // data.isDisabled = true
  189. }
  190. }
  191. }
  192. if self.BOTAOutlineView.outlineView.clickedRow == -1 {
  193. for data in menuItemArr {
  194. if data.text == KMLocalizedString("Add Item") {
  195. data.isDisabled = false
  196. } else {
  197. data.isDisabled = true
  198. }
  199. }
  200. } else {
  201. let outlineItem : KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: self.BOTAOutlineView.outlineView.clickedRow) as! KMBOTAOutlineItem
  202. if outlineItem.outline.index > 0 {
  203. for data in menuItemArr {
  204. if data.text == KMLocalizedString("Demote") {
  205. data.isDisabled = false
  206. } else {
  207. // data.isDisabled = true
  208. }
  209. }
  210. } else {
  211. for data in menuItemArr {
  212. if data.text == KMLocalizedString("Demote") {
  213. data.isDisabled = true
  214. } else {
  215. // data.isDisabled = true
  216. }
  217. }
  218. }
  219. let parentOutline = outlineItem.outline.parent
  220. let grandparentOutline = parentOutline?.parent
  221. if grandparentOutline != nil {
  222. for data in menuItemArr {
  223. if data.text == KMLocalizedString("Add A Higher Level") || data.text == KMLocalizedString("Promote") {
  224. data.isDisabled = false
  225. } else {
  226. // data.isDisabled = true
  227. }
  228. }
  229. } else {
  230. for data in menuItemArr {
  231. if data.text == KMLocalizedString("Add A Higher Level") || data.text == KMLocalizedString("Promote") {
  232. data.isDisabled = true
  233. } else {
  234. // data.isDisabled = true
  235. }
  236. }
  237. }
  238. }
  239. if menuGroupView_ == nil {
  240. menuGroupView_ = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  241. }
  242. if menuGroupView_ != nil {
  243. menuGroupView_?.clickedAutoHide = false
  244. menuGroupView_?.groupDelegate = self
  245. menuGroupView_?.frame = CGRectMake(0, 0, 180, viewHeight)
  246. menuGroupView_?.updateGroupInfo(menuItemArr)
  247. menuGroupView_?.showWithPoint(CGPoint(x: point.x, y: point.y - viewHeight), relativeTo: tempView)
  248. }
  249. }
  250. func reloadData() {
  251. self.BOTAOutlineView.reloadData(expandItemType: .none)
  252. }
  253. func editOutlineUI(editVC : KMOutlineEditViewController!) {
  254. if editVC.pageRadio.properties.checkboxType == .selected {
  255. let index = Int(editVC.pageInput.properties.text) ?? 0
  256. let pageIndex = max(0, index-1)
  257. if editVC.originalDestination?.pageIndex != pageIndex {
  258. let page = editVC.pdfView?.document.page(at: UInt(pageIndex))
  259. if page != nil {
  260. let destination = CPDFDestination.init(document: editVC.pdfView!.document, pageIndex: pageIndex)
  261. editVC.outline?.destination = destination
  262. } else {
  263. __NSBeep()
  264. }
  265. }
  266. } else if editVC.webRaido.properties.checkboxType == .selected {
  267. if editVC.originalURLString != editVC.webInput.properties.text {
  268. var urlString = editVC.webInput.properties.text
  269. let tLowerUrl = urlString.lowercased()
  270. if !tLowerUrl.hasPrefix("https://") && !tLowerUrl.hasPrefix("pf]://") && !urlString.hasPrefix("https://") &&
  271. urlString.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) > 0 {
  272. urlString = "http://\(urlString)"
  273. }
  274. let action = CPDFURLAction.init(url: urlString)
  275. editVC.outline?.action = action
  276. }
  277. } else if editVC.emailRadio.properties.checkboxType == .selected {
  278. var mailString = editVC.emailInput.properties.text
  279. let tLowerStr = mailString.lowercased()
  280. if !tLowerStr.hasPrefix("mailto:") {
  281. mailString = "mailto:\(mailString)"
  282. }
  283. if mailString != editVC.originalURLString {
  284. var action = CPDFURLAction.init(url: mailString)
  285. if action?.url == nil {
  286. action = CPDFURLAction.init(url: "mailto:")
  287. }
  288. editVC.outline?.action = action
  289. }
  290. }
  291. }
  292. // MARK: - Private Methods
  293. private func _showAlert(style: NSAlert.Style, message: String, info: String, buttons: [String]) -> NSApplication.ModalResponse {
  294. let alert = NSAlert()
  295. alert.alertStyle = style
  296. alert.messageText = message
  297. alert.informativeText = info
  298. for button in buttons {
  299. alert.addButton(withTitle: button)
  300. }
  301. return alert.runModal()
  302. }
  303. private func _showHeaderSearch() {
  304. headerSearchView_?.isHidden = false
  305. }
  306. private func _hideHeaderSearch() {
  307. headerSearchView_?.isHidden = true
  308. }
  309. //MARK: - GroupView
  310. func showGroupView() {
  311. var viewHeight: CGFloat = 8
  312. var menuItemArr: [ComponentMenuitemProperty] = []
  313. for i in ["Expand All", "Collapse All", "Remove All Outlines"] {
  314. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  315. itemSelected: false,
  316. isDisabled: false,
  317. keyEquivalent: nil,
  318. text: KMLocalizedString(i))
  319. menuItemArr.append(properties_Menuitem)
  320. viewHeight += 36
  321. }
  322. if let data = menuItemArr.first {
  323. var canExpand = false
  324. for row in 0..<self.BOTAOutlineView.outlineView.numberOfRows {
  325. // 检查当前项目是否可以展开
  326. let item = self.BOTAOutlineView.outlineView.item(atRow: row)
  327. if self.BOTAOutlineView.outlineView.isExpandable(item) {
  328. if !self.BOTAOutlineView.outlineView.isItemExpanded(item) {
  329. canExpand = true
  330. break
  331. }
  332. }
  333. }
  334. data.isDisabled = !canExpand
  335. }
  336. if let data = menuItemArr.safe_element(for: 1) as? ComponentMenuitemProperty {
  337. var canCollapse = false
  338. for row in 0..<self.BOTAOutlineView.outlineView.numberOfRows {
  339. let item = self.BOTAOutlineView.outlineView.item(atRow: row)
  340. if self.BOTAOutlineView.outlineView.isExpandable(item) {
  341. if self.BOTAOutlineView.outlineView.isItemExpanded(item) {
  342. canCollapse = true
  343. break
  344. }
  345. }
  346. }
  347. data.isDisabled = !canCollapse
  348. }
  349. if let data = menuItemArr.last {
  350. if self.BOTAOutlineView.outlineView.item(atRow: 0) != nil {
  351. data.isDisabled = false
  352. } else {
  353. data.isDisabled = true
  354. }
  355. }
  356. if groupView_ == nil {
  357. groupView_ = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  358. }
  359. groupView_?.groupDelegate = self
  360. groupView_?.frame = CGRectMake(310, 0, 200, viewHeight)
  361. groupView_?.updateGroupInfo(menuItemArr)
  362. var point = moreDropdown_.convert(moreDropdown_.frame.origin, to: nil)
  363. point.y -= viewHeight
  364. groupView_?.showWithPoint(point, relativeTo: moreDropdown_)
  365. moreDropdown_.properties.state = .pressed
  366. moreDropdown_.reloadData()
  367. }
  368. func removeGroupView() {
  369. if groupView_ != nil {
  370. groupView_?.removeFromSuperview()
  371. }
  372. moreDropdown_.properties.state = .normal
  373. moreDropdown_.reloadData()
  374. }
  375. func updateExtempViewState() {
  376. if(self.handdler.outlineRoot() == nil || self.handdler.outlineRoot()?.numberOfChildren == 0) { //无数据时的图
  377. self.emptyView.isHidden = false
  378. } else {
  379. self.emptyView.isHidden = true
  380. }
  381. }
  382. }
  383. //MARK: - Notification
  384. extension KMOutlineViewController {
  385. @objc func KMPDFViewCurrentPageDidChangedNotification(notification: NSNotification) {
  386. if notification.object is CPDFDocument {
  387. let pdfdocument : CPDFDocument = notification.object as! CPDFDocument
  388. if pdfdocument.isEqual(self.handdler.document) {
  389. if !isLocalEvent {
  390. self.updateOutlineSelection()
  391. }
  392. self.isLocalEvent = false
  393. }
  394. }
  395. }
  396. }
  397. //MARK: - Menu 右键菜单
  398. extension KMOutlineViewController {
  399. @objc func outlineViewDoubleAction() {
  400. if(self.BOTAOutlineView.outlineView.clickedRow >= 0) {
  401. self.renameItemAction()
  402. }
  403. }
  404. @objc func addItemAction() {
  405. guard let outlineView = BOTAOutlineView.outlineView else {
  406. return
  407. }
  408. let selectRowIndexs = outlineView.selectedRowIndexes
  409. let dataCount = BOTAOutlineView.data?.children.count ?? 0
  410. var index: Int = 0
  411. var parent: KMBOTAOutlineItem?
  412. var outlineItem: KMBOTAOutlineItem?
  413. if selectRowIndexs.count == 0 {
  414. var lastOulineItem: KMBOTAOutlineItem?
  415. if dataCount == 0 {
  416. let item = KMBOTAOutlineItem()
  417. item.outline = self.handdler.document!.setNewOutlineRoot()
  418. item.parent = nil
  419. parent = item
  420. lastOulineItem = item
  421. } else {
  422. outlineItem = outlineView.item(atRow: outlineView.numberOfRows - 1) as? KMBOTAOutlineItem
  423. lastOulineItem = outlineItem
  424. while lastOulineItem?.parent != nil {
  425. lastOulineItem = lastOulineItem?.parent
  426. }
  427. parent = lastOulineItem
  428. }
  429. index = Int(lastOulineItem?.outline.numberOfChildren ?? 0)
  430. } else {
  431. outlineItem = outlineView.item(atRow: selectRowIndexs.last ?? 0) as? KMBOTAOutlineItem
  432. parent = outlineItem?.parent ?? KMBOTAOutlineItem()
  433. index = Int((outlineItem?.outline.index) ?? 0 + 1)
  434. }
  435. self.addOutlineToIndex(index: index, parent: parent)
  436. }
  437. @objc func addChildItemAction() {
  438. let outlineView: KMOutlineView = self.BOTAOutlineView.outlineView
  439. let selectRowIndexs = outlineView.selectedRowIndexes
  440. if selectRowIndexs.count != 0 {
  441. let outlineItem: KMBOTAOutlineItem = outlineView.item(atRow: selectRowIndexs.last!) as! KMBOTAOutlineItem
  442. let index = outlineItem.outline.numberOfChildren
  443. self.addOutlineToIndex(index: NSInteger(index), parent: outlineItem)
  444. }
  445. }
  446. @objc func addHigherItemAction() {
  447. let outlineView: KMOutlineView = self.BOTAOutlineView.outlineView
  448. let selectRowIndexs = outlineView.selectedRowIndexes
  449. if selectRowIndexs.count != 0 {
  450. let outlineItem: KMBOTAOutlineItem = outlineView.item(atRow: selectRowIndexs.last!) as! KMBOTAOutlineItem
  451. var parent = outlineItem.parent
  452. let index = NSInteger(parent!.outline.index) + 1
  453. parent = parent?.parent
  454. if parent != nil {
  455. self.addOutlineToIndex(index: index, parent: parent!)
  456. }
  457. }
  458. }
  459. @objc func deleteItemAction() {
  460. let outlineView: KMOutlineView = self.BOTAOutlineView.outlineView
  461. let selectRowIndexs = outlineView.selectedRowIndexes
  462. if selectRowIndexs.count != 0 {
  463. var outlineItems: [KMBOTAOutlineItem] = []
  464. for index in selectRowIndexs {
  465. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: index) as! KMBOTAOutlineItem
  466. outlineItem.toIndex = index
  467. outlineItem.parent = outlineItem.parent ?? KMBOTAOutlineItem()
  468. outlineItems.append(outlineItem)
  469. }
  470. self.deleteOutline(outlineItems: outlineItems)
  471. }
  472. }
  473. @objc func editItemAction() {
  474. let clickedRow = BOTAOutlineView.outlineView.clickedRow
  475. if clickedRow < 0 {
  476. NSSound.beep()
  477. return
  478. }
  479. if let rowView = self.BOTAOutlineView.outlineView.rowView(atRow: clickedRow, makeIfNecessary: true) {
  480. let item = self.BOTAOutlineView.outlineView.item(atRow: clickedRow) as? KMBOTAOutlineItem
  481. let vc = KMOutlineEditViewController.init(outline: item?.outline, document: self.handdler.pdfView)
  482. vc.pageCount = handdler.pageCount()
  483. vc.itemClick = { [weak self] idx, params in
  484. if idx == 1 {
  485. self?.popover_?.close()
  486. } else if idx == 2 {
  487. self?.popover_?.close()
  488. if let viewC = params.first as? KMOutlineEditViewController {
  489. let resp = self?._showAlert(style: .informational, message: KMLocalizedString("Are you sure you want to apply edits to this outline?"), info: "", buttons: [KMLocalizedString("Apply"), KMLocalizedString("Cancel")])
  490. if resp == .alertFirstButtonReturn {
  491. self?.editOutlineUI(editVC: viewC)
  492. }
  493. }
  494. }
  495. }
  496. let popover = NSPopover()
  497. popover_ = popover
  498. popover.delegate = self
  499. popover.contentViewController = vc
  500. popover.animates = true
  501. popover.behavior = .transient
  502. popover.setValue(true, forKey: "shouldHideAnchor")
  503. popover.show(relativeTo: rowView.bounds, of: rowView, preferredEdge: .minX)
  504. }
  505. }
  506. @objc func renameItemAction() {
  507. if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
  508. self.renameOutlineWithRow(row: self.BOTAOutlineView.outlineView.clickedRow)
  509. } else {
  510. __NSBeep()
  511. }
  512. }
  513. @objc func changeItemAction() {
  514. if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
  515. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: self.BOTAOutlineView.outlineView.clickedRow) as! KMBOTAOutlineItem
  516. let resp = _showAlert(style: .informational, message: NSLocalizedString("Are you sure you want to set the destination as the current location?", comment: ""), info: "", buttons: [NSLocalizedString("Yes", comment:""), NSLocalizedString("No", comment:"")])
  517. if resp == .alertFirstButtonReturn {
  518. // self.changeLocation(outlineItem: outlineItem, destination: self.fetchCurrentDestination())
  519. }
  520. } else {
  521. __NSBeep()
  522. }
  523. }
  524. @objc func promoteItemAction() {
  525. if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
  526. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: self.BOTAOutlineView.outlineView.clickedRow) as! KMBOTAOutlineItem
  527. var parent = outlineItem.parent
  528. let index = NSInteger(parent!.outline.index) + 1
  529. parent = parent?.parent
  530. if parent != nil {
  531. self.moveOutline(outlineItem: outlineItem, index: index, parent: parent)
  532. }
  533. }
  534. }
  535. @objc func demoteItemAction() {
  536. if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
  537. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: self.BOTAOutlineView.outlineView.clickedRow) as! KMBOTAOutlineItem
  538. let parent = outlineItem.parent
  539. let newParent = parent?.children[Int(outlineItem.outline.index) - 1]
  540. let index = newParent?.children.count
  541. if (index != nil) {
  542. self.moveOutline(outlineItem: outlineItem, index: NSInteger(index ?? 0), parent: newParent)
  543. }
  544. }
  545. }
  546. @objc private func expandAllComments(item: NSMenuItem) {
  547. self.BOTAOutlineView.expandAllComments(item: item)
  548. }
  549. @objc private func collapseAllComments(item: NSMenuItem) {
  550. self.BOTAOutlineView.collapseAllComments(item: item)
  551. }
  552. @objc private func removeAllOutlineItem(item: NSMenuItem) {
  553. let alter = NSAlert()
  554. alter.alertStyle = NSAlert.Style.informational
  555. alter.messageText = NSLocalizedString("This will permanently remove all outlines. Are you sure to continue?", comment: "")
  556. alter.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  557. alter.addButton(withTitle: NSLocalizedString("No", comment:""))
  558. let modlres = alter.runModal()
  559. if modlres == NSApplication.ModalResponse.alertFirstButtonReturn {
  560. self.removeAllOutline()
  561. }
  562. }
  563. @objc private func removeAllOutline() {
  564. guard let data = self.BOTAOutlineView.data else { return }
  565. for item in data.children {
  566. item.toIndex = Int(item.outline.index)
  567. }
  568. self.deleteOutline(outlineItems: data.children)
  569. self.BOTAOutlineView.reloadData(expandItemType: .none)
  570. }
  571. @objc private func _searchAction() {
  572. _showHeaderSearch()
  573. }
  574. }
  575. //MARK: - Action
  576. extension KMOutlineViewController {
  577. @IBAction func addNewOutline(_ sender: Any) {
  578. self.addItemAction()
  579. }
  580. @IBAction func escButtonAction(_ sender: Any) {
  581. self.cancelSelect()
  582. }
  583. func cancelSelect() {
  584. self.BOTAOutlineView.cancelSelect()
  585. }
  586. func renameOutlineWithRow(row: NSInteger) {
  587. DispatchQueue.main.async {
  588. self.renamePDFOutline = self.BOTAOutlineView.outlineView.item(atRow: row) as? KMBOTAOutlineItem
  589. let cell : KMBOTAOutlineCellView = self.BOTAOutlineView.outlineView.view(atColumn: 0, row: row, makeIfNecessary: true) as! KMBOTAOutlineCellView
  590. self.renameTextField = cell.titleLabel
  591. self.renameTextField.delegate = self
  592. self.renameTextField.isEditable = true
  593. self.renameTextField.becomeFirstResponder()
  594. }
  595. }
  596. func addOutlineToIndex(index: Int, parent: KMBOTAOutlineItem?) {
  597. let pageIndex = self.handdler.currentPageIndex
  598. let label = self.fetchCurrentLabel(pageIndex: pageIndex)
  599. let destination = self.handdler.currentDestination()
  600. self.addOutlineToIndex(index: index, pageIndex: pageIndex, destination: destination, lable: label, parent: parent)
  601. }
  602. func addOutlineToIndex(index: Int, pageIndex: Int, destination: CPDFDestination?, lable: String, parent: KMBOTAOutlineItem?) {
  603. let outlineItem = KMBOTAOutlineItem()
  604. outlineItem.destination = destination
  605. outlineItem.label = lable
  606. outlineItem.parent = parent
  607. outlineItem.toIndex = index
  608. self.addOutline(outlineItems: [outlineItem])
  609. let tempOutlineView = self.BOTAOutlineView!
  610. var index = -1
  611. if tempOutlineView.outlineView.numberOfRows == 1 || tempOutlineView.data == nil {
  612. index = 0
  613. } else {
  614. index = tempOutlineView.outlineView.row(forItem: outlineItem)
  615. }
  616. tempOutlineView.selectIndex(index: index)
  617. //滑动到指定位置
  618. if(tempOutlineView.outlineView.selectedRow >= 0) {
  619. self.renameOutlineWithRow(row: tempOutlineView.outlineView.selectedRow)
  620. }
  621. let row = tempOutlineView.outlineView.row(forItem: outlineItem)
  622. if Thread.current.isMainThread {
  623. tempOutlineView.outlineView.scrollToVisible(tempOutlineView.outlineView.rect(ofRow: row))
  624. } else {
  625. DispatchQueue.main.async {
  626. tempOutlineView.outlineView.scrollToVisible(tempOutlineView.outlineView.rect(ofRow: row))
  627. }
  628. }
  629. }
  630. func updateOutlineSelection() {
  631. let currentPageIndex = self.handdler.currentPageIndex
  632. let numRows = self.BOTAOutlineView.outlineView.numberOfRows
  633. if numRows > 0 {
  634. for i in 0...numRows - 1 {
  635. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: i) as! KMBOTAOutlineItem
  636. if (outlineItem.outline.destination == nil) {
  637. continue
  638. }
  639. if outlineItem.outline.destination.pageIndex == currentPageIndex {
  640. self.BOTAOutlineView.selectIndex(index: currentPageIndex)
  641. break
  642. }
  643. }
  644. }
  645. }
  646. // func fetchCurrentDestination() -> CPDFDestination {
  647. // //destination
  648. // var destination: CPDFDestination
  649. // let pageIndex: Int
  650. // if self.listView.currentSelection != nil {
  651. // let des :CPDFDestination = CPDFDestination.init(document: self.listView.document, pageIndex: Int(self.listView.currentSelection.page.pageIndex()), at: CGPoint(x: self.listView.currentSelection.bounds.origin.x, y: self.listView.currentSelection.bounds.origin.y + self.listView.currentSelection.bounds.size.height), zoom: self.listView.scaleFactor)
  652. // destination = des
  653. // pageIndex = Int(self.listView.currentSelection.page.pageIndex())
  654. // } else {
  655. // let des :CPDFDestination = CPDFDestination.init(document: self.listView.document, pageIndex: Int(self.listView.currentPageIndex), at: CGPoint(x: 0, y: self.listView.currentPage().size.height), zoom: self.listView.scaleFactor)
  656. // destination = des
  657. // pageIndex = Int(self.listView.currentPageIndex)
  658. // }
  659. //
  660. // if "\(destination.point.x )" == "nan" {
  661. // destination = CPDFDestination(document: self.listView.document, pageIndex: pageIndex, at: CGPoint(x: 0, y: 0), zoom: self.listView.scaleFactor)
  662. // }
  663. // return destination
  664. // }
  665. func fetchCurrentLabel(pageIndex: Int) -> String {
  666. var label = "\(NSLocalizedString("Page", comment: ""))\(pageIndex + 1)"
  667. let currentSelection = self.handdler.currentSelection()
  668. if currentSelection != nil && currentSelection?.selectionsByLine != nil {
  669. for data in currentSelection?.selectionsByLine ?? [] {
  670. label = data.string() ?? ""
  671. }
  672. }
  673. return label
  674. }
  675. }
  676. //MARK: - KMBOTAOutlineViewDelegate
  677. extension KMOutlineViewController: KMBOTAOutlineViewDelegate {
  678. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, rightDidMoseDown: KMBOTAOutlineItem, event: NSEvent) {
  679. let row = outlineView.outlineView.row(forItem: rightDidMoseDown)
  680. if outlineView.outlineView.rowView(atRow: row, makeIfNecessary: false) != nil {
  681. let rowView = outlineView.outlineView.rowView(atRow: row, makeIfNecessary: false)
  682. self.addRightMenu(view: rowView!, event: event)
  683. }
  684. }
  685. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, didReloadData: KMBOTAOutlineItem) {
  686. self.updateExtempViewState()
  687. }
  688. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, didSelectItem: [KMBOTAOutlineItem]) {
  689. if self.BOTAOutlineView.outlineView.selectedRowIndexes.count == 1 {
  690. self.isLocalEvent = true
  691. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow:self.BOTAOutlineView.outlineView.selectedRow) as! KMBOTAOutlineItem
  692. if outlineItem.outline.destination != nil {
  693. if outlineItem.outline.destination.page() != nil {
  694. // self.listView.go(toTargetPoint: outlineItem.outline.destination.point, on: outlineItem.outline.destination.page() , at: .top)
  695. } else {
  696. let alter = NSAlert()
  697. alter.alertStyle = NSAlert.Style.informational
  698. alter.messageText = NSLocalizedString("The target page is invalid, please relocate it.", comment: "")
  699. alter.addButton(withTitle: NSLocalizedString("OK", comment:""))
  700. alter.beginSheetModal(for: self.view.window ?? NSWindow())
  701. }
  702. } else if outlineItem.outline.action != nil {
  703. let alter = NSAlert()
  704. alter.alertStyle = NSAlert.Style.informational
  705. alter.messageText = NSLocalizedString("The target page is invalid, please relocate it.", comment: "")
  706. alter.addButton(withTitle: NSLocalizedString("OK", comment:""))
  707. alter.beginSheetModal(for: self.view.window ?? NSWindow())
  708. // self.listView.perform(outlineItem.outline.action)
  709. }
  710. }
  711. }
  712. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool {
  713. if outlineView.outlineView.selectedRowIndexes.count > 1 || outlineView.outlineView.selectedRow == -1 {
  714. return false
  715. }
  716. self.dragPDFOutline = items.first as? KMBOTAOutlineItem
  717. let indexSet = [outlineView.outlineView.clickedRow]
  718. let indexSetData: Data = try!NSKeyedArchiver.archivedData(withRootObject: indexSet, requiringSecureCoding: true)
  719. pasteboard.declareTypes([NSPasteboard.PasteboardType(rawValue: "kKMPDFViewOutlineDragDataType")], owner: self)
  720. pasteboard.setData(indexSetData, forType: NSPasteboard.PasteboardType(rawValue: NSPasteboard.PasteboardType.RawValue("kKMPDFViewOutlineDragDataType")))
  721. return true
  722. }
  723. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
  724. var dragOperation = NSDragOperation.init(rawValue: 0)
  725. if index >= 0 {
  726. dragOperation = NSDragOperation.move
  727. }
  728. return dragOperation
  729. }
  730. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
  731. guard let dragOutlineItem = self.dragPDFOutline else { return false }
  732. let outlineItem: KMBOTAOutlineItem = (item ?? KMBOTAOutlineItem()) as! KMBOTAOutlineItem
  733. if index < 0 {
  734. return false
  735. }
  736. if outlineItem.parent == nil {
  737. var root = dragOutlineItem.parent
  738. while root?.parent?.children != nil {
  739. root = root?.parent!
  740. }
  741. if dragOutlineItem.parent!.isEqual(root) {
  742. if dragOutlineItem.outline.index > index {
  743. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: root)
  744. } else {
  745. self.moveOutline(outlineItem: dragOutlineItem, index: index - 1, parent: root)
  746. }
  747. } else {
  748. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: root)
  749. }
  750. } else {
  751. if dragOutlineItem.parent!.isEqual(item) {
  752. // if dragOutlineItem.outline.index != 0 {
  753. if dragOutlineItem.outline.index > index {
  754. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: item as? KMBOTAOutlineItem)
  755. } else {
  756. self.moveOutline(outlineItem: dragOutlineItem, index: index - 1, parent: item as? KMBOTAOutlineItem)
  757. }
  758. // } else {
  759. // return false
  760. // }
  761. } else {
  762. var tOutline = outlineItem
  763. var isContains = false
  764. while (tOutline.parent != nil) {
  765. if tOutline.outline.isEqual(dragOutlineItem.outline) {
  766. isContains = true
  767. break
  768. }
  769. tOutline = tOutline.parent!
  770. }
  771. if isContains == false {
  772. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: item as? KMBOTAOutlineItem)
  773. }
  774. }
  775. }
  776. self.BOTAOutlineView.selectItem(outlineItem: dragOutlineItem)
  777. return true
  778. }
  779. }
  780. //MARK: - NSTextFieldDelegate
  781. extension KMOutlineViewController: NSTextFieldDelegate {
  782. func controlTextDidEndEditing(_ obj: Notification) {
  783. if (self.renameTextField.isEqual(obj.object)) {
  784. let textField : NSTextField = obj.object as! NSTextField
  785. self.renamePDFOutline(outlineItem: self.renamePDFOutline, label: textField.stringValue)
  786. }
  787. }
  788. }
  789. //MARK: - NSPopoverDelegate
  790. extension KMOutlineViewController: NSPopoverDelegate {
  791. func popoverWillClose(_ notification: Notification) {
  792. let popover : NSPopover = notification.object as! NSPopover
  793. if popover.contentViewController!.isKind(of: KMOutlineEditViewController.self) {
  794. }
  795. }
  796. func popoverDidClose(_ notification: Notification) {
  797. if popover_ == (notification.object as? NSPopover) {
  798. popover_ = nil
  799. }
  800. }
  801. }
  802. //MARK: - NSMenuItemValidation
  803. extension KMOutlineViewController: NSMenuDelegate, NSMenuItemValidation {
  804. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  805. let action = menuItem.action
  806. if (action == #selector(undo)) {
  807. return handdler.canUndo()
  808. }
  809. if (action == #selector(redo)) {
  810. return handdler.canRedo()
  811. }
  812. return true
  813. }
  814. }
  815. // MARK: - KMNOutlineHanddlerDelegate
  816. extension KMOutlineViewController: KMNOutlineHanddlerDelegate {
  817. func handdler(_ handdler: KMNOutlineHanddler, didAdd info: [String : Any]?) {
  818. let tempOutlineItems = info?["data"] as? [KMBOTAOutlineItem] ?? []
  819. let tempOutlineView = self.BOTAOutlineView
  820. if tempOutlineView?.data?.children.count == 0 || tempOutlineView?.data == nil {
  821. tempOutlineView?.inputData = self.handdler.outlineRoot()
  822. } else {
  823. tempOutlineView?.outlineView.reloadData()
  824. }
  825. //展开
  826. // DispatchQueue.main.async {
  827. for outlineItem in tempOutlineItems {
  828. var tempParent = outlineItem
  829. while tempParent.parent != nil {
  830. tempParent.isItemExpanded = true
  831. tempParent = tempParent.parent!
  832. tempOutlineView?.outlineView.expandItem(tempParent)
  833. }
  834. tempOutlineView?.outlineView.expandItem(tempParent.parent)
  835. }
  836. // }
  837. self.updateExtempViewState()
  838. }
  839. func handdler(_ handdler: KMNOutlineHanddler, didRemove info: [String : Any]?) {
  840. let tempOutlineItems = info?["data"] as? [KMBOTAOutlineItem] ?? []
  841. let tempOutlineView = self.BOTAOutlineView
  842. //展开
  843. for outlineItem in tempOutlineItems {
  844. outlineItem.parent?.isItemExpanded = true
  845. tempOutlineView?.outlineView.expandItem(outlineItem.parent)
  846. }
  847. tempOutlineView?.outlineView.reloadData()
  848. //删除需要取消选中
  849. tempOutlineView?.cancelSelect()
  850. //刷新nil数据
  851. self.updateExtempViewState()
  852. }
  853. func handdler(_ handdler: KMNOutlineHanddler, didRename outline: CPDFOutline?, info: [String : Any]?) {
  854. let outlineItem = info?["data"] as? KMBOTAOutlineItem
  855. let tempOutlineView = self.BOTAOutlineView
  856. tempOutlineView?.outlineView.reloadItem(outlineItem)
  857. }
  858. func handdler(_ handdler: KMNOutlineHanddler, didChangeLocation outline: CPDFOutline?, info: [String : Any]?) {
  859. let outlineItem = info?["data"] as? KMBOTAOutlineItem
  860. let tempOutlineView = self.BOTAOutlineView
  861. tempOutlineView?.outlineView.reloadItem(outlineItem)
  862. }
  863. func handdler(_ handdler: KMNOutlineHanddler, didMove outline: CPDFOutline?, info: [String : Any]?) {
  864. guard let outlineItem = info?["data"] as? KMBOTAOutlineItem else {
  865. return
  866. }
  867. let parent = info?["parent"] as? KMBOTAOutlineItem
  868. let tempOutlineView = self.BOTAOutlineView
  869. let index = info?["index"] as? Int ?? 0
  870. //显示数据刷新
  871. outlineItem.parent?.children.removeObject(outlineItem)
  872. parent?.children.insert(outlineItem, at: index)
  873. outlineItem.parent = parent
  874. tempOutlineView?.outlineView.reloadData()
  875. tempOutlineView?.cancelSelect()
  876. //展开
  877. outlineItem.isItemExpanded = true
  878. outlineItem.parent?.isItemExpanded = true
  879. tempOutlineView?.outlineView.expandItem(outlineItem)
  880. tempOutlineView?.outlineView.expandItem(outlineItem.parent)
  881. }
  882. }
  883. //MARK: - 快捷键
  884. extension KMOutlineViewController {
  885. @IBAction func delete(_ sender: Any) {
  886. self.deleteItemAction()
  887. }
  888. }
  889. //MARK: - undoRedo
  890. extension KMOutlineViewController {
  891. func moveOutline(outlineItem: KMBOTAOutlineItem, index: NSInteger, parent: KMBOTAOutlineItem!) {
  892. handdler.moveOutline(outlineItem: outlineItem, index: index, parent: parent)
  893. }
  894. func changeLocation(outlineItem: KMBOTAOutlineItem, destination: CPDFDestination) {
  895. handdler.changeLocation(outlineItem: outlineItem, destination: destination)
  896. }
  897. func renamePDFOutline(outlineItem: KMBOTAOutlineItem!, label: String) {
  898. let tempOutlineView = self.BOTAOutlineView!
  899. self.view.window?.makeFirstResponder(tempOutlineView.outlineView)
  900. self.renameTextField.isEditable = false
  901. if outlineItem.outline.label == label {
  902. return
  903. }
  904. handdler.renamePDFOutline(outlineItem: outlineItem, label: label)
  905. }
  906. func deleteOutline(outlineItems: [KMBOTAOutlineItem]) {
  907. NSApp.mainWindow?.makeFirstResponder(self.BOTAOutlineView)
  908. let tempOutlineView = self.BOTAOutlineView!
  909. handdler.deleteOutline(outlineItems: outlineItems)
  910. }
  911. func addOutline(outlineItems: [KMBOTAOutlineItem]) {
  912. NSApp.mainWindow?.makeFirstResponder(self.BOTAOutlineView)
  913. let tempOutlineView = self.BOTAOutlineView!
  914. //先取消选中
  915. tempOutlineView.cancelSelect()
  916. var tempOutlineItems: [KMBOTAOutlineItem] = outlineItems
  917. tempOutlineItems.sort(){$0.toIndex < $1.toIndex}
  918. handdler.addOutline(outlineItems: tempOutlineItems)
  919. }
  920. @IBAction func undo(_ sender: Any) {
  921. handdler.undo()
  922. }
  923. @IBAction func redo(_ sender: Any) {
  924. handdler.redo()
  925. }
  926. }
  927. //MARK: - ComponentDropdownDelegate
  928. extension KMOutlineViewController: ComponentDropdownDelegate {
  929. func componentDropdownDidShowMenuItem(dropdown: ComponentDropdown) {
  930. showGroupView()
  931. }
  932. }
  933. //MARK: - ComponentGroupDelegate
  934. extension KMOutlineViewController: ComponentGroupDelegate {
  935. func componentGroupDidDismiss(group: ComponentGroup?) {
  936. if group == groupView_ {
  937. removeGroupView()
  938. } else if group == menuGroupView_ {
  939. group?.removeFromSuperview()
  940. menuGroupView_ = nil
  941. }
  942. }
  943. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  944. if group == groupView_ {
  945. if let selItem = menuItemProperty {
  946. let index = group?.menuItemArr.firstIndex(of: selItem)
  947. if index == 0 {
  948. expandAllComments(item: NSMenuItem())
  949. } else if index == 1 {
  950. collapseAllComments(item: NSMenuItem())
  951. } else if index == 2 {
  952. removeAllOutlineItem(item: NSMenuItem())
  953. }
  954. }
  955. } else if group == menuGroupView_ {
  956. if let selItem = menuItemProperty {
  957. let index = group?.menuItemArr.firstIndex(of: selItem)
  958. if index == 0 {
  959. addItemAction()
  960. } else if index == 1 {
  961. addChildItemAction()
  962. } else if index == 2 {
  963. addHigherItemAction()
  964. } else if index == 4 {
  965. deleteItemAction()
  966. } else if index == 6 {
  967. group?.removeFromSuperview()
  968. editItemAction()
  969. } else if index == 7 {
  970. renameItemAction()
  971. } else if index == 8 {
  972. changeItemAction()
  973. } else if index == 10 {
  974. promoteItemAction()
  975. } else if index == 11 {
  976. demoteItemAction()
  977. }
  978. group?.removeFromSuperview()
  979. }
  980. }
  981. }
  982. }