KMOutlineViewController.swift 50 KB

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