KMOutlineViewController.swift 50 KB

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