KMOutlineViewController.swift 50 KB

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