KMOutlineViewController.swift 46 KB

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