KMOutlineViewController.swift 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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 Outlines"), subText: KMLocalizedString(""))
  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. self.searchButton.properties.isDisabled = true
  423. } else {
  424. self.emptyView.isHidden = true
  425. self.searchButton.properties.isDisabled = false
  426. }
  427. self.searchButton.reloadData()
  428. }
  429. }
  430. }
  431. //MARK: - Menu 右键菜单
  432. extension KMOutlineViewController {
  433. override func rightMouseDown(with event: NSEvent) {
  434. print("rightMouseDown")
  435. self.addOutlineRightMouseDown(event: event)
  436. }
  437. func addOutlineRightMouseDown(event: NSEvent) {
  438. let point = event.locationInWindow
  439. var viewHeight: CGFloat = 0
  440. let items: [String] = ["Add Outline", "Select All", "", "Delete"]
  441. var menuItemArr: [ComponentMenuitemProperty] = []
  442. for value in items {
  443. if value.count == 0 {
  444. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  445. menuItemArr.append(property)
  446. viewHeight += 8
  447. } else {
  448. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  449. itemSelected: false,
  450. isDisabled: false,
  451. keyEquivalent: nil,
  452. text: KMLocalizedString(value),
  453. identifier: value)
  454. menuItemArr.append(properties_Menuitem)
  455. viewHeight += 36
  456. }
  457. }
  458. for data in menuItemArr {
  459. if data.text == KMLocalizedString("Delete") {
  460. data.isDisabled = true
  461. }
  462. if data.text == KMLocalizedString("Select All") {
  463. data.keyEquivalent = "⌘ A"
  464. }
  465. }
  466. if menuGroupView_ == nil {
  467. menuGroupView_ = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  468. }
  469. if menuGroupView_ != nil {
  470. menuGroupView_?.groupDelegate = self
  471. menuGroupView_?.frame = CGRectMake(0, 0, 180, viewHeight)
  472. menuGroupView_?.updateGroupInfo(menuItemArr)
  473. menuGroupView_?.showWithPoint(CGPoint(x: point.x, y: point.y - viewHeight), relativeTo: nil)
  474. }
  475. }
  476. @objc func outlineViewDoubleAction() {
  477. guard let outlineView = outlineView_ else {
  478. return
  479. }
  480. let clickedRow = outlineView.clickedRow
  481. if clickedRow >= 0 && clickedRow < outlineView.numberOfRows {
  482. self.renameItemAction()
  483. }
  484. }
  485. @objc func addItemAction() {
  486. guard let outlineView = BOTAOutlineView.outlineView else {
  487. return
  488. }
  489. let selectRowIndexs = outlineView.selectedRowIndexes
  490. let dataCount = BOTAOutlineView.data?.children.count ?? 0
  491. var index: Int = 0
  492. var parent: KMBOTAOutlineItem?
  493. var outlineItem: KMBOTAOutlineItem?
  494. if selectRowIndexs.count == 0 {
  495. var lastOulineItem: KMBOTAOutlineItem?
  496. if dataCount == 0 {
  497. let item = KMBOTAOutlineItem()
  498. item.outline = self.handdler.document!.setNewOutlineRoot()
  499. item.parent = nil
  500. parent = item
  501. lastOulineItem = item
  502. } else {
  503. outlineItem = outlineView.item(atRow: outlineView.numberOfRows - 1) as? KMBOTAOutlineItem
  504. lastOulineItem = outlineItem
  505. while lastOulineItem?.parent != nil {
  506. lastOulineItem = lastOulineItem?.parent
  507. }
  508. parent = lastOulineItem
  509. }
  510. index = Int(lastOulineItem?.outline.numberOfChildren ?? 0)
  511. } else {
  512. outlineItem = outlineView.item(atRow: selectRowIndexs.last ?? 0) as? KMBOTAOutlineItem
  513. parent = outlineItem?.parent ?? KMBOTAOutlineItem()
  514. index = Int(((outlineItem?.outline.index) ?? 0) + 1)
  515. }
  516. self.addOutlineToIndex(index: index, parent: parent)
  517. }
  518. @objc func addChildItemAction() {
  519. let outlineView: KMOutlineView = self.BOTAOutlineView.outlineView
  520. let selectRowIndexs = outlineView.selectedRowIndexes
  521. if selectRowIndexs.count != 0 {
  522. let outlineItem: KMBOTAOutlineItem = outlineView.item(atRow: selectRowIndexs.last!) as! KMBOTAOutlineItem
  523. let index = outlineItem.outline.numberOfChildren
  524. self.addOutlineToIndex(index: NSInteger(index), parent: outlineItem)
  525. }
  526. }
  527. @objc func addHigherItemAction() {
  528. let outlineView: KMOutlineView = self.BOTAOutlineView.outlineView
  529. let selectRowIndexs = outlineView.selectedRowIndexes
  530. if selectRowIndexs.count != 0 {
  531. let outlineItem: KMBOTAOutlineItem = outlineView.item(atRow: selectRowIndexs.last!) as! KMBOTAOutlineItem
  532. var parent = outlineItem.parent
  533. let index = NSInteger(parent!.outline.index) + 1
  534. parent = parent?.parent
  535. if parent != nil {
  536. self.addOutlineToIndex(index: index, parent: parent!)
  537. }
  538. }
  539. }
  540. @objc func deleteItemAction() {
  541. let outlineView: KMOutlineView = self.BOTAOutlineView.outlineView
  542. let selectRowIndexs = outlineView.selectedRowIndexes
  543. if selectRowIndexs.count != 0 {
  544. var outlineItems: [KMBOTAOutlineItem] = []
  545. for index in selectRowIndexs {
  546. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: index) as! KMBOTAOutlineItem
  547. outlineItem.toIndex = index
  548. outlineItem.parent = outlineItem.parent ?? KMBOTAOutlineItem()
  549. outlineItems.append(outlineItem)
  550. }
  551. self.deleteOutline(outlineItems: outlineItems)
  552. }
  553. }
  554. @objc func editItemAction() {
  555. let clickedRow = BOTAOutlineView.outlineView.clickedRow
  556. if clickedRow < 0 {
  557. NSSound.beep()
  558. return
  559. }
  560. if let rowView = self.BOTAOutlineView.outlineView.rowView(atRow: clickedRow, makeIfNecessary: true) {
  561. let item = self.BOTAOutlineView.outlineView.item(atRow: clickedRow) as? KMBOTAOutlineItem
  562. let vc = KMOutlineEditViewController.init(outline: item?.outline, document: self.handdler.pdfView)
  563. vc.pageCount = handdler.pageCount()
  564. vc.itemClick = { [weak self] idx, params in
  565. if idx == 1 {
  566. self?.popover_?.close()
  567. } else if idx == 2 {
  568. self?.popover_?.close()
  569. if let viewC = params.first as? KMOutlineEditViewController {
  570. 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")])
  571. if resp == .alertFirstButtonReturn {
  572. self?.editOutlineUI(editVC: viewC)
  573. }
  574. }
  575. }
  576. }
  577. let popover = NSPopover()
  578. popover_ = popover
  579. popover.delegate = self
  580. popover.contentViewController = vc
  581. popover.animates = true
  582. popover.behavior = .transient
  583. popover.setValue(true, forKey: "shouldHideAnchor")
  584. popover.show(relativeTo: rowView.bounds, of: rowView, preferredEdge: .minX)
  585. }
  586. }
  587. @objc func renameItemAction() {
  588. if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
  589. self.renameOutlineWithRow(row: self.BOTAOutlineView.outlineView.clickedRow)
  590. } else {
  591. __NSBeep()
  592. }
  593. }
  594. @objc func changeItemAction() {
  595. guard let currentDest = handdler.currentDestination() else {
  596. NSSound.beep()
  597. return
  598. }
  599. guard let item = outlineView_?.clickedItem() as? KMBOTAOutlineItem else {
  600. NSSound.beep()
  601. return
  602. }
  603. 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")])
  604. if resp == .alertFirstButtonReturn {
  605. handdler.changeLocation(outlineItem: item, destination: currentDest)
  606. }
  607. }
  608. @objc func promoteItemAction() {
  609. if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
  610. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: self.BOTAOutlineView.outlineView.clickedRow) as! KMBOTAOutlineItem
  611. var parent = outlineItem.parent
  612. let index = NSInteger(parent!.outline.index) + 1
  613. parent = parent?.parent
  614. if parent != nil {
  615. self.moveOutline(outlineItem: outlineItem, index: index, parent: parent)
  616. }
  617. }
  618. }
  619. @objc func demoteItemAction() {
  620. if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
  621. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: self.BOTAOutlineView.outlineView.clickedRow) as! KMBOTAOutlineItem
  622. let parent = outlineItem.parent
  623. let newParent = parent?.children[Int(outlineItem.outline.index) - 1]
  624. let index = newParent?.children.count
  625. if (index != nil) {
  626. self.moveOutline(outlineItem: outlineItem, index: NSInteger(index ?? 0), parent: newParent)
  627. }
  628. }
  629. }
  630. @objc private func expandAllComments(item: NSMenuItem) {
  631. self.BOTAOutlineView.expandAllComments(item: item)
  632. }
  633. @objc private func collapseAllComments(item: NSMenuItem) {
  634. self.BOTAOutlineView.collapseAllComments(item: item)
  635. }
  636. @objc private func removeAllOutlineItem(item: NSMenuItem) {
  637. let alter = NSAlert()
  638. alter.alertStyle = .informational
  639. alter.messageText = KMLocalizedString("This will permanently remove all outlines. Are you sure to continue?")
  640. alter.addButton(withTitle: KMLocalizedString("Yes"))
  641. alter.addButton(withTitle: KMLocalizedString("No"))
  642. let modlres = alter.runModal()
  643. if modlres == .alertFirstButtonReturn {
  644. self.removeAllOutline()
  645. }
  646. }
  647. @objc private func _searchAction() {
  648. searchButton.properties.state = .normal
  649. searchButton.reloadData()
  650. showHeaderSearch()
  651. }
  652. func selectAll() {
  653. self.BOTAOutlineView.selectAll()
  654. }
  655. }
  656. //MARK: - Action
  657. extension KMOutlineViewController {
  658. @IBAction func addNewOutline(_ sender: Any) {
  659. self.addItemAction()
  660. }
  661. @IBAction func escButtonAction(_ sender: Any) {
  662. self.cancelSelect()
  663. }
  664. func cancelSelect() {
  665. self.BOTAOutlineView.cancelSelect()
  666. }
  667. func renameOutlineWithRow(row: NSInteger) {
  668. DispatchQueue.main.async {
  669. self.renamePDFOutline = self.BOTAOutlineView.outlineView.item(atRow: row) as? KMBOTAOutlineItem
  670. let cell : KMBOTAOutlineCellView = self.BOTAOutlineView.outlineView.view(atColumn: 0, row: row, makeIfNecessary: true) as! KMBOTAOutlineCellView
  671. self.renameCell = cell
  672. self.renameTextField = cell.titleLabel
  673. self.renameTextField.delegate = self
  674. self.renameTextField.isEditable = true
  675. self.renameTextField.becomeFirstResponder()
  676. }
  677. }
  678. func addOutlineToIndex(index: Int, parent: KMBOTAOutlineItem?) {
  679. var pageIndex = self.handdler.currentPageIndex
  680. var label = self.fetchCurrentLabel(pageIndex: pageIndex)
  681. var destination = self.handdler.currentDestination()
  682. if ((self.handdler.pdfView?.currentSelection) != nil) {
  683. pageIndex = Int(((self.handdler.pdfView?.currentSelection.page.pageIndex() ?? 0) + 1))
  684. label = self.handdler.pdfView?.currentSelection?.string() ?? ""
  685. destination = self.handdler.pdfView?.currentDestination
  686. }
  687. self.addOutlineToIndex(index: index, pageIndex: pageIndex, destination: destination, lable: label, parent: parent)
  688. }
  689. func addOutlineToIndex(index: Int, pageIndex: Int, destination: CPDFDestination?, lable: String, parent: KMBOTAOutlineItem?) {
  690. let outlineItem = KMBOTAOutlineItem()
  691. outlineItem.destination = destination
  692. outlineItem.label = lable
  693. outlineItem.parent = parent
  694. outlineItem.toIndex = index
  695. self.addOutline(outlineItems: [outlineItem])
  696. let tempOutlineView = self.BOTAOutlineView!
  697. var index = -1
  698. if tempOutlineView.outlineView.numberOfRows == 1 || tempOutlineView.data == nil {
  699. index = 0
  700. } else {
  701. index = tempOutlineView.outlineView.row(forItem: outlineItem)
  702. }
  703. tempOutlineView.selectIndex(index: index)
  704. //滑动到指定位置
  705. if(tempOutlineView.outlineView.selectedRow >= 0) {
  706. self.renameOutlineWithRow(row: tempOutlineView.outlineView.selectedRow)
  707. }
  708. let row = tempOutlineView.outlineView.row(forItem: outlineItem)
  709. if Thread.current.isMainThread {
  710. tempOutlineView.outlineView.scrollToVisible(tempOutlineView.outlineView.rect(ofRow: row))
  711. } else {
  712. DispatchQueue.main.async {
  713. tempOutlineView.outlineView.scrollToVisible(tempOutlineView.outlineView.rect(ofRow: row))
  714. }
  715. }
  716. }
  717. func updateOutlineSelection() {
  718. let currentPageIndex = self.handdler.currentPageIndex
  719. let numRows = self.BOTAOutlineView.outlineView.numberOfRows
  720. if numRows > 0 {
  721. for i in 0...numRows - 1 {
  722. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: i) as! KMBOTAOutlineItem
  723. if (outlineItem.outline.destination == nil) {
  724. continue
  725. }
  726. if outlineItem.outline.destination.pageIndex == currentPageIndex {
  727. self.BOTAOutlineView.selectIndex(index: currentPageIndex)
  728. break
  729. }
  730. }
  731. }
  732. }
  733. func fetchCurrentLabel(pageIndex: Int) -> String {
  734. var label = "\(KMLocalizedString("Page"))\(pageIndex + 1)"
  735. let currentSelection = self.handdler.currentSelection()
  736. if currentSelection != nil && currentSelection?.selectionsByLine != nil {
  737. for data in currentSelection?.selectionsByLine ?? [] {
  738. label = data.string() ?? ""
  739. }
  740. }
  741. return label
  742. }
  743. }
  744. //MARK: - KMBOTAOutlineViewDelegate
  745. extension KMOutlineViewController: KMBOTAOutlineViewDelegate {
  746. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, rightDidMoseDown: KMBOTAOutlineItem, event: NSEvent) {
  747. let row = outlineView.outlineView.row(forItem: rightDidMoseDown)
  748. if outlineView.outlineView.rowView(atRow: row, makeIfNecessary: false) != nil {
  749. let rowView = outlineView.outlineView.rowView(atRow: row, makeIfNecessary: false)
  750. self.addRightMenu(view: rowView!, event: event)
  751. }
  752. }
  753. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, didReloadData: KMBOTAOutlineItem) {
  754. self.updateExtempViewState()
  755. }
  756. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, didSelectItem: [KMBOTAOutlineItem]) {
  757. if outlineView_?.selectedRowIndexes.count == 1 {
  758. isLocalEvent = true
  759. guard let item = outlineView_?.selectedItem() as? KMBOTAOutlineItem else {
  760. return
  761. }
  762. handdler.selectOutline(item.outline)
  763. }
  764. }
  765. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool {
  766. if outlineView.outlineView.selectedRow == -1 {
  767. return false
  768. }
  769. self.dragPDFOutline = items.first as? KMBOTAOutlineItem
  770. self.dragPDFOutlines_ = items as? [KMBOTAOutlineItem] ?? []
  771. let indexSet = [outlineView.outlineView.clickedRow]
  772. let indexSetData: Data = try!NSKeyedArchiver.archivedData(withRootObject: indexSet, requiringSecureCoding: true)
  773. pasteboard.declareTypes([NSPasteboard.PasteboardType(rawValue: "kKMPDFViewOutlineDragDataType")], owner: self)
  774. pasteboard.setData(indexSetData, forType: NSPasteboard.PasteboardType(rawValue: NSPasteboard.PasteboardType.RawValue("kKMPDFViewOutlineDragDataType")))
  775. return true
  776. }
  777. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
  778. var dragOperation = NSDragOperation.init(rawValue: 0)
  779. if index >= 0 {
  780. dragOperation = NSDragOperation.move
  781. }
  782. return dragOperation
  783. }
  784. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
  785. guard let dragOutlineItem = self.dragPDFOutline else { return false }
  786. let outlineItem: KMBOTAOutlineItem = (item ?? KMBOTAOutlineItem()) as! KMBOTAOutlineItem
  787. if index < 0 {
  788. return false
  789. }
  790. for dragOutlineItem in dragPDFOutlines_ {
  791. if outlineItem.parent == nil {
  792. var root = dragOutlineItem.parent
  793. while root?.parent?.children != nil {
  794. root = root?.parent!
  795. }
  796. if dragOutlineItem.parent!.isEqual(root) {
  797. if dragOutlineItem.outline.index > index {
  798. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: root)
  799. } else {
  800. self.moveOutline(outlineItem: dragOutlineItem, index: index - 1, parent: root)
  801. }
  802. } else {
  803. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: root)
  804. }
  805. } else {
  806. if dragOutlineItem.parent!.isEqual(item) {
  807. // if dragOutlineItem.outline.index != 0 {
  808. if dragOutlineItem.outline.index > index {
  809. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: item as? KMBOTAOutlineItem)
  810. } else {
  811. self.moveOutline(outlineItem: dragOutlineItem, index: index - 1, parent: item as? KMBOTAOutlineItem)
  812. }
  813. // } else {
  814. // return false
  815. // }
  816. } else {
  817. var tOutline = outlineItem
  818. var isContains = false
  819. while (tOutline.parent != nil) {
  820. if tOutline.outline.isEqual(dragOutlineItem.outline) {
  821. isContains = true
  822. break
  823. }
  824. tOutline = tOutline.parent!
  825. }
  826. if isContains == false {
  827. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: item as? KMBOTAOutlineItem)
  828. }
  829. }
  830. }
  831. }
  832. self.BOTAOutlineView.selectItem(outlineItem: dragOutlineItem)
  833. return true
  834. }
  835. }
  836. //MARK: - NSTextFieldDelegate
  837. extension KMOutlineViewController: NSTextFieldDelegate {
  838. func controlTextDidEndEditing(_ obj: Notification) {
  839. if (self.renameTextField.isEqual(obj.object)) {
  840. let textField : NSTextField = obj.object as! NSTextField
  841. self.renamePDFOutline(outlineItem: self.renamePDFOutline, label: textField.stringValue)
  842. renameCell?.titleLabelBox.borderWidth = 0
  843. renameCell?.titleLabelBox.fillColor = .clear
  844. }
  845. }
  846. }
  847. //MARK: - NSPopoverDelegate
  848. extension KMOutlineViewController: NSPopoverDelegate {
  849. func popoverWillClose(_ notification: Notification) {
  850. let popover : NSPopover = notification.object as! NSPopover
  851. if popover.contentViewController!.isKind(of: KMOutlineEditViewController.self) {
  852. }
  853. }
  854. func popoverDidClose(_ notification: Notification) {
  855. if popover_ == (notification.object as? NSPopover) {
  856. popover_ = nil
  857. }
  858. }
  859. }
  860. //MARK: - NSMenuItemValidation
  861. extension KMOutlineViewController: NSMenuDelegate, NSMenuItemValidation {
  862. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  863. let action = menuItem.action
  864. if (action == #selector(undo)) {
  865. return handdler.canUndo()
  866. }
  867. if (action == #selector(redo)) {
  868. return handdler.canRedo()
  869. }
  870. return true
  871. }
  872. }
  873. // MARK: - KMNOutlineHanddlerDelegate
  874. extension KMOutlineViewController: KMNOutlineHanddlerDelegate {
  875. func handdler(_ handdler: KMNOutlineHanddler, didAdd info: [String : Any]?) {
  876. let tempOutlineItems = info?["data"] as? [KMBOTAOutlineItem] ?? []
  877. let tempOutlineView = self.BOTAOutlineView
  878. if tempOutlineView?.data?.children.count == 0 || tempOutlineView?.data == nil {
  879. tempOutlineView?.inputData = self.handdler.outlineRoot()
  880. } else {
  881. if BOTAOutlineView.isValidSearchMode() {
  882. BOTAOutlineView.reloadSearchChildren(item: BOTAOutlineView.data)
  883. }
  884. tempOutlineView?.outlineView.reloadData()
  885. }
  886. //展开
  887. // DispatchQueue.main.async {
  888. for outlineItem in tempOutlineItems {
  889. var tempParent = outlineItem
  890. while tempParent.parent != nil {
  891. tempParent.isItemExpanded = true
  892. tempParent = tempParent.parent!
  893. tempOutlineView?.outlineView.expandItem(tempParent)
  894. }
  895. tempOutlineView?.outlineView.expandItem(tempParent.parent)
  896. }
  897. // }
  898. self.updateExtempViewState()
  899. }
  900. func handdler(_ handdler: KMNOutlineHanddler, didRemove info: [String : Any]?) {
  901. let tempOutlineItems = info?["data"] as? [KMBOTAOutlineItem] ?? []
  902. let tempOutlineView = self.BOTAOutlineView
  903. //展开
  904. for outlineItem in tempOutlineItems {
  905. outlineItem.parent?.isItemExpanded = true
  906. tempOutlineView?.outlineView.expandItem(outlineItem.parent)
  907. }
  908. if BOTAOutlineView.isValidSearchMode() {
  909. BOTAOutlineView.reloadSearchChildren(item: BOTAOutlineView.data)
  910. }
  911. tempOutlineView?.outlineView.reloadData()
  912. //删除需要取消选中
  913. tempOutlineView?.cancelSelect()
  914. //刷新nil数据
  915. self.updateExtempViewState()
  916. }
  917. func handdler(_ handdler: KMNOutlineHanddler, didRename outline: CPDFOutline?, info: [String : Any]?) {
  918. let outlineItem = info?["data"] as? KMBOTAOutlineItem
  919. let tempOutlineView = self.BOTAOutlineView
  920. tempOutlineView?.outlineView.reloadItem(outlineItem)
  921. }
  922. func handdler(_ handdler: KMNOutlineHanddler, didChangeLocation outline: CPDFOutline?, info: [String : Any]?) {
  923. let outlineItem = info?["data"] as? KMBOTAOutlineItem
  924. let tempOutlineView = self.BOTAOutlineView
  925. tempOutlineView?.outlineView.reloadItem(outlineItem)
  926. }
  927. func handdler(_ handdler: KMNOutlineHanddler, didMove outline: CPDFOutline?, info: [String : Any]?) {
  928. guard let outlineItem = info?["data"] as? KMBOTAOutlineItem else {
  929. return
  930. }
  931. let parent = info?["parent"] as? KMBOTAOutlineItem
  932. let tempOutlineView = self.BOTAOutlineView
  933. let index = info?["index"] as? Int ?? 0
  934. if BOTAOutlineView.isValidSearchMode() {
  935. BOTAOutlineView.reloadSearchChildren(item: BOTAOutlineView.data)
  936. }
  937. //显示数据刷新
  938. outlineItem.parent?.children.removeObject(outlineItem)
  939. parent?.children.insert(outlineItem, at: index)
  940. outlineItem.parent = parent
  941. tempOutlineView?.outlineView.reloadData()
  942. tempOutlineView?.cancelSelect()
  943. //展开
  944. outlineItem.isItemExpanded = true
  945. outlineItem.parent?.isItemExpanded = true
  946. tempOutlineView?.outlineView.expandItem(outlineItem)
  947. tempOutlineView?.outlineView.expandItem(outlineItem.parent)
  948. }
  949. }
  950. //MARK: - 快捷键
  951. extension KMOutlineViewController {
  952. @IBAction func delete(_ sender: Any) {
  953. self.deleteItemAction()
  954. }
  955. }
  956. //MARK: - undoRedo
  957. extension KMOutlineViewController {
  958. func moveOutline(outlineItem: KMBOTAOutlineItem, index: NSInteger, parent: KMBOTAOutlineItem!) {
  959. handdler.moveOutline(outlineItem: outlineItem, index: index, parent: parent)
  960. }
  961. func changeLocation(outlineItem: KMBOTAOutlineItem, destination: CPDFDestination) {
  962. handdler.changeLocation(outlineItem: outlineItem, destination: destination)
  963. }
  964. func renamePDFOutline(outlineItem: KMBOTAOutlineItem!, label: String) {
  965. let tempOutlineView = self.BOTAOutlineView!
  966. self.view.window?.makeFirstResponder(tempOutlineView.outlineView)
  967. self.renameTextField.isEditable = false
  968. if outlineItem.outline.label == label {
  969. return
  970. }
  971. handdler.renamePDFOutline(outlineItem: outlineItem, label: label)
  972. }
  973. func deleteOutline(outlineItems: [KMBOTAOutlineItem]) {
  974. NSApp.mainWindow?.makeFirstResponder(self.BOTAOutlineView)
  975. let tempOutlineView = self.BOTAOutlineView!
  976. handdler.deleteOutline(outlineItems: outlineItems)
  977. }
  978. func addOutline(outlineItems: [KMBOTAOutlineItem]) {
  979. NSApp.mainWindow?.makeFirstResponder(self.BOTAOutlineView)
  980. let tempOutlineView = self.BOTAOutlineView!
  981. //先取消选中
  982. tempOutlineView.cancelSelect()
  983. var tempOutlineItems: [KMBOTAOutlineItem] = outlineItems
  984. tempOutlineItems.sort(){$0.toIndex < $1.toIndex}
  985. handdler.addOutline(outlineItems: tempOutlineItems)
  986. }
  987. @IBAction func undo(_ sender: Any) {
  988. handdler.undo()
  989. }
  990. @IBAction func redo(_ sender: Any) {
  991. handdler.redo()
  992. }
  993. }
  994. //MARK: - ComponentDropdownDelegate
  995. extension KMOutlineViewController: ComponentDropdownDelegate {
  996. func componentDropdownDidShowMenuItem(dropdown: ComponentDropdown) {
  997. showGroupView()
  998. }
  999. }
  1000. //MARK: - ComponentGroupDelegate
  1001. extension KMOutlineViewController: ComponentGroupDelegate {
  1002. func componentGroupDidDismiss(group: ComponentGroup?) {
  1003. if group == groupView_ {
  1004. moreDropdown_.properties.state = .normal
  1005. moreDropdown_.reloadData()
  1006. } else if group == menuGroupView_ {
  1007. } else if group == searchGroupView {
  1008. searchGroupTarget?.properties.state = .normal
  1009. searchGroupTarget?.reloadData()
  1010. }
  1011. }
  1012. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  1013. if group == groupView_ {
  1014. if let selItem = menuItemProperty {
  1015. let index = group?.menuItemArr.firstIndex(of: selItem)
  1016. if index == 0 {
  1017. expandAllComments(item: NSMenuItem())
  1018. } else if index == 1 {
  1019. collapseAllComments(item: NSMenuItem())
  1020. } else if index == 2 {
  1021. removeAllOutlineItem(item: NSMenuItem())
  1022. }
  1023. }
  1024. } else if group == menuGroupView_ {
  1025. if let selItem = menuItemProperty {
  1026. let index = group?.menuItemArr.firstIndex(of: selItem)
  1027. if selItem.text == KMLocalizedString("Add Item") {
  1028. addItemAction()
  1029. }
  1030. if selItem.text == KMLocalizedString("Select All") {
  1031. self.selectAll()
  1032. }
  1033. if selItem.text == KMLocalizedString("Add Sub-Item") {
  1034. self.addChildItemAction()
  1035. }
  1036. if selItem.text == KMLocalizedString("Add A Higher Level") {
  1037. self.addHigherItemAction()
  1038. }
  1039. if selItem.text == KMLocalizedString("Rename") {
  1040. self.renameItemAction()
  1041. }
  1042. if selItem.text == KMLocalizedString("Delete") {
  1043. self.deleteItemAction()
  1044. }
  1045. if selItem.text == KMLocalizedString("Edit") {
  1046. self.editItemAction()
  1047. }
  1048. if selItem.text == KMLocalizedString("Promote") {
  1049. self.promoteItemAction()
  1050. }
  1051. if selItem.text == KMLocalizedString("Demote") {
  1052. self.demoteItemAction()
  1053. }
  1054. if selItem.text == KMLocalizedString("Change Destination") {
  1055. self.changeItemAction()
  1056. }
  1057. }
  1058. } else if group == searchGroupView {
  1059. guard let menuI = menuItemProperty else {
  1060. return
  1061. }
  1062. let idx = group?.menuItemArr.firstIndex(of: menuI)
  1063. if idx == 0 {
  1064. let key = KMNSearchKey.wholeWords.botaSearch
  1065. let value = KMDataManager.ud_bool(forKey: key)
  1066. KMDataManager.ud_set(!value, forKey: key)
  1067. BOTAOutlineView.wholeWords = !value
  1068. } else if idx == 1 {
  1069. let key = KMNSearchKey.caseSensitive.botaSearch
  1070. let value = KMDataManager.ud_bool(forKey: key)
  1071. KMDataManager.ud_set(!value, forKey: key)
  1072. BOTAOutlineView.caseSensitive = !value
  1073. }
  1074. } else if group == outlineRightGroupView_ {
  1075. if let selItem = menuItemProperty {
  1076. let index = group?.menuItemArr.firstIndex(of: selItem)
  1077. if index == 0 {
  1078. addItemAction()
  1079. } else if index == 1 {
  1080. }
  1081. }
  1082. }
  1083. }
  1084. }