KMLeftSideViewController+Outline.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. //
  2. // KMLeftSideViewController+Outline.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/22.
  6. //
  7. import Foundation
  8. enum KMOutlineViewMenuItemTag: Int {
  9. case addEntry = 0
  10. case addChild
  11. case addAunt
  12. case remove
  13. case edit
  14. case setDestination
  15. case rename
  16. case promote
  17. case demote
  18. }
  19. extension KMLeftSideViewController {
  20. func outline_initSubViews() {
  21. self.outlineSearchField.delegate = self
  22. self.outlineAddButton.target = self
  23. self.outlineAddButton.action = #selector(outlineContextMenuItemClicked_AddEntry)
  24. self.outlineMoreButton.target = self
  25. self.outlineMoreButton.tag = 302
  26. self.outlineMoreButton.action = #selector(leftSideViewMoreButtonAction)
  27. self.outlineDoneButton.target = self
  28. self.outlineDoneButton.tag = 310
  29. self.outlineDoneButton.action = #selector(leftSideViewDoneButtonAction)
  30. let menuOutline = NSMenu()
  31. _ = menuOutline.addItem(title: KMLocalizedString("Ignore Case", "Menu item title"), action: #selector(toggleOutlineCaseInsensitiveSearch), target: self)
  32. (self.outlineSearchField.cell as? NSSearchFieldCell)?.searchMenuTemplate = menuOutline
  33. self.outlineSearchField.target = self
  34. self.tocOutlineView.menu = NSMenu()
  35. self.tocOutlineView.menu?.delegate = self
  36. self.tocOutlineView.delegate = self
  37. self.tocOutlineView.dataSource = self
  38. self.tocOutlineView.botaDelegate = self
  39. self.tocOutlineView.botaDataSource = self
  40. self.tocOutlineView.tocDelegate = self
  41. // kKMPDFViewOutlineDragDataType
  42. self.tocOutlineView.registerForDraggedTypes([.localDraggedTypes])
  43. self.tocOutlineView.target = self
  44. // self.tocOutlineView.doubleAction = #selector(goToSelectedOutlineItem)
  45. self.tocOutlineView.doubleAction = #selector(outlineContextMenuItemClicked_Rename)
  46. }
  47. func outline_initDefalutValue() {
  48. self.outlineView.wantsLayer = true
  49. self.outlineView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  50. self.outlineLabel.stringValue = KMLocalizedString("Outline", nil);
  51. self.outlineLabel.textColor = KMAppearance.Layout.h0Color()
  52. self.outlineAddButton.toolTip = KMLocalizedString("Add Item", nil)
  53. self.outlineSearchButton.toolTip = KMLocalizedString("Search", nil)
  54. self.outlineDoneButton.title = KMLocalizedString("Done", nil);
  55. self.outlineDoneButton.toolTip = KMLocalizedString("Done", nil);
  56. self.outlineDoneButton.setTitleColor(KMAppearance.Layout.w0Color())
  57. self.outlineDoneButton.wantsLayer = true
  58. self.outlineDoneButton.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  59. self.outlineDoneButton.layer?.cornerRadius = 4.0
  60. self.outlineSearchField.wantsLayer = true
  61. self.outlineSearchField.backgroundColor = KMAppearance.Layout.l_1Color()
  62. self.outlineSearchField.layer?.backgroundColor = KMAppearance.Layout.l_1Color().cgColor
  63. self.outlineSearchField.layer?.borderWidth = 1.0
  64. self.outlineSearchField.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  65. self.outlineDoneButton.isHidden = true
  66. self.outlineSearchField.isHidden = true
  67. (self.outlineSearchField.cell as? NSSearchFieldCell)?.placeholderString = KMLocalizedString("Search Outline", nil)
  68. self.tocOutlineView.backgroundColor = KMAppearance.Layout.l0Color()
  69. self.tocOutlineView.autoresizesOutlineColumn = false
  70. self.tocOutlineView.allowsMultipleSelection = true
  71. self.tocOutlineView.allowsEmptySelection = true
  72. }
  73. func updateOutlineSelection() {
  74. if self.listView.document.outlineRoot() == nil || self.updatingOutlineSelection {
  75. return
  76. }
  77. self.updatingOutlineSelection = true
  78. let numRows = self.tocOutlineView.numberOfRows
  79. var arr = NSMutableArray()
  80. for i in 0 ..< numRows {
  81. guard let tPDFOutline = self.tocOutlineView.item(atRow: i) as? CPDFOutline else {
  82. continue
  83. }
  84. let tPage = tPDFOutline.destination?.page()
  85. if (tPage == nil) {
  86. continue
  87. }
  88. let tDict = NSDictionary(object: tPage as Any, forKey: "\(i)" as NSCopying)
  89. arr.add(tDict)
  90. }
  91. let currentPage = self.listView.currentPage()
  92. var hasExistInOutlineView = false
  93. for dict in arr {
  94. guard let _dict = dict as? NSDictionary else {
  95. continue
  96. }
  97. let page = _dict.allValues.last as? CPDFPage
  98. // NSInteger index = [dict.allKeys.lastObject integerValue];
  99. let index = Int(_dict.allKeys.last as? String ?? "0") ?? 0
  100. if let data = page?.isEqual(to: currentPage), data {
  101. self.tocOutlineView.selectRowIndexes(IndexSet(integer: index), byExtendingSelection: false)
  102. self.tocOutlineView.scrollRowToVisible(index)
  103. hasExistInOutlineView = true
  104. break
  105. }
  106. }
  107. if (!hasExistInOutlineView) {
  108. self.tocOutlineView.deselectRow(self.tocOutlineView.selectedRow)
  109. }
  110. self.updatingOutlineSelection = false
  111. }
  112. func newAddOutlineEntryEditingMode(_ index: Int) {
  113. self.renamePDFOutline = self.tocOutlineView.item(atRow: self.tocOutlineView.selectedRow) as? CPDFOutline
  114. let row = self.tocOutlineView.selectedRow
  115. let viewS = self.tocOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  116. let targrtTextField = viewS?.subviews.first as? NSTextField
  117. self.renamePDFOutlineTextField = targrtTextField
  118. targrtTextField?.delegate = self
  119. targrtTextField?.isEditable = true
  120. targrtTextField?.becomeFirstResponder()
  121. }
  122. func loadUnfoldDate(_ foldType: KMFoldType) {
  123. // 清空所有折叠数组
  124. self.allFoldNotes.removeAll()
  125. var mutableArray: [CPDFAnnotation] = []
  126. if self.noteSearchMode {
  127. for note in self.noteSearchArray {
  128. if note is CPDFMarkupAnnotation {
  129. mutableArray.append(note)
  130. }
  131. }
  132. } else {
  133. for section in self.annotations {
  134. for item in section.annotations ?? [] {
  135. if let note = item.annotation, note is CPDFMarkupAnnotation {
  136. mutableArray.append(note)
  137. }
  138. }
  139. }
  140. }
  141. // for note in self.notes {
  142. // if note is CPDFMarkupAnnotation {
  143. // mutableArray.append(note)
  144. // }
  145. // }
  146. self.canFoldNotes = mutableArray
  147. self.allFoldNotes = []
  148. }
  149. func selectedRowIndexes() -> IndexSet {
  150. var selectedIndexes = self.tocOutlineView.selectedRowIndexes
  151. let clickedRow = self.tocOutlineView.clickedRow
  152. if clickedRow != -1 && selectedIndexes.contains(clickedRow) == false {
  153. var indexes = IndexSet(integer: clickedRow)
  154. selectedIndexes = indexes
  155. }
  156. return selectedIndexes
  157. }
  158. func editOutlineUI(_ editVC: KMOutlineEditViewController) {
  159. if editVC.pageButton.state == .on {
  160. let numberString = editVC.outlineTargetPageIndexTextField.stringValue
  161. let idx = Int(numberString) ?? 1
  162. let newPage = editVC.pdfView?.document.page(at: UInt(idx-1))
  163. let originalPage = editVC.originalDestination?.page()
  164. if let data = newPage?.pageIndex(), data != editVC.originalPageIndex {
  165. // if let data = newPage?.isEqual(to: originalPage), data {
  166. //新page不存在
  167. if (newPage == nil) {
  168. } else {
  169. if let outL = editVC.outline {
  170. let pageSize = newPage?.bounds(for: .cropBox).size ?? .zero
  171. if let destination = CPDFDestination(page: newPage, at: CGPointMake(pageSize.width, pageSize.height)) {
  172. self.changePDFOutlineDestination(destination, PDFoutline: outL)
  173. }
  174. }
  175. }
  176. }
  177. } else if editVC.urlButton.state == .on {
  178. var urlString = editVC.outlineURLTextField.stringValue
  179. let tLowerUrl = urlString.lowercased()
  180. if tLowerUrl.hasPrefix("https://") == false && tLowerUrl.hasPrefix("ftp://") == false && tLowerUrl.hasPrefix("http://") == false && urlString.isEmpty == false {
  181. urlString = "http://\(urlString)"
  182. }
  183. if let urlAction = CPDFURLAction(url: urlString), editVC.outline != nil {
  184. if editVC.originalURLString != editVC.outlineURLTextField.stringValue {
  185. self.changePDFAction(urlAction, PDFOutline: editVC.outline!)
  186. }
  187. }
  188. } else if editVC.mailButton.state == .on {
  189. var mailString = editVC.mailAddressTextField.stringValue
  190. let tLowerStr = mailString.lowercased()
  191. if tLowerStr.hasPrefix("mailto:") == false {
  192. mailString = "mailto:\(mailString)"
  193. }
  194. if var urlAction = CPDFURLAction(url: mailString) {
  195. if urlAction.url() == nil {
  196. urlAction = CPDFURLAction(url: "mailto:")
  197. }
  198. if mailString != editVC.originalURLString && editVC.outline != nil {
  199. self.changePDFAction(urlAction, PDFOutline: editVC.outline!)
  200. }
  201. }
  202. }
  203. if editVC.outlineNameTextView.string == editVC.originalLabel {
  204. return
  205. }
  206. if let outL = editVC.outline {
  207. self.renamePDFOutline(outL, label: editVC.outlineNameTextView.string)
  208. }
  209. }
  210. @objc func outlineContextMenuItemClicked_AddEntry(_ sender: AnyObject?) {
  211. let PDFOutlineArray = NSMutableArray()
  212. let rowSet = self.selectedRowIndexes()
  213. for idx in rowSet {
  214. PDFOutlineArray.add(self.tocOutlineView.item(atRow: idx))
  215. }
  216. if (PDFOutlineArray.count == 0) {
  217. var lastPDFLine = self.tocOutlineView.item(atRow: self.tocOutlineView.numberOfRows-1) as? CPDFOutline
  218. var rootPDFOutline: CPDFOutline?
  219. if (lastPDFLine != nil) {
  220. while (lastPDFLine!.parent != nil) {
  221. lastPDFLine = lastPDFLine?.parent
  222. }
  223. rootPDFOutline = lastPDFLine
  224. } else {
  225. rootPDFOutline = self.listView.document.outlineRoot()
  226. if ((rootPDFOutline == nil)) {
  227. // rootPDFOutline = CPDFOutline()
  228. // self.listView.document.setOutlineRoot(rootPDFOutline)
  229. rootPDFOutline = self.listView.document.setNewOutlineRoot()
  230. }
  231. }
  232. let addOutLine = CPDFOutline()
  233. addOutLine.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), self.listView.currentPageIndex+1)
  234. addOutLine.destination = self.listView.currentDestination
  235. self.addoutline(parent: rootPDFOutline, addOutline: addOutLine, index: Int(rootPDFOutline?.numberOfChildren ?? 0), needExpand: false)
  236. } else {
  237. let currentPDFline = PDFOutlineArray.lastObject as? CPDFOutline
  238. let currentIndex = currentPDFline?.index ?? 0
  239. var parent: CPDFOutline?
  240. parent = currentPDFline?.parent
  241. let addOutLine = CPDFOutline()
  242. addOutLine.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), self.listView.currentPageIndex+1)
  243. addOutLine.destination = self.listView.currentDestination
  244. self.addoutline(parent: parent, addOutline: addOutLine, index: Int(currentIndex) + 1, needExpand: false)
  245. self.tocOutlineView.scrollRowToVisible(Int(currentIndex) + 1)
  246. self.tocOutlineView.deselectRow(Int(currentIndex)+1)
  247. }
  248. }
  249. @IBAction func outlineNormalSearchButtonAction(_ sender: NSButton) {
  250. self.outlineSearchField.isHidden = false
  251. self.outlineDoneButton.isHidden = false
  252. self.outlineLabel.isHidden = true
  253. self.outlineSearchButton.isHidden = true
  254. self.outlineMoreButton.isHidden = true
  255. self.outlineAddButton.isHidden = true
  256. self.outlineSearchField.becomeFirstResponder()
  257. }
  258. @IBAction func toc_expandAllComments(_ sender: AnyObject?) {
  259. if (self.tocType == .unfold) {
  260. return
  261. }
  262. self.tocType = .unfold
  263. self.tocOutlineView.reloadData()
  264. self.tocOutlineView.expandItem(nil, expandChildren: true)
  265. }
  266. @IBAction func toc_foldAllComments(_ sender: AnyObject?) {
  267. if (self.tocType == .fold) {
  268. return
  269. }
  270. self.tocType = .fold
  271. self.tocOutlineView.reloadData()
  272. self.tocOutlineView.collapseItem(nil, collapseChildren: true)
  273. }
  274. @objc func leftSideEmptyAnnotationClick_DeleteOutline(_ sender: AnyObject?) {
  275. let alert = NSAlert()
  276. alert.alertStyle = .critical
  277. alert.messageText = ""
  278. alert.informativeText = KMLocalizedString("This will permanently remove all outlines. Are you sure to continue?", nil)
  279. alert.addButton(withTitle: KMLocalizedString("Yes", nil))
  280. alert.addButton(withTitle: KMLocalizedString("No", nil))
  281. let response = alert.runModal()
  282. if response == .alertFirstButtonReturn {
  283. if let item = self.listView.document.outlineRoot() {
  284. self.removeAllOutline(item)
  285. }
  286. }
  287. }
  288. func showSearchOutlineBlankState(_ toShowState: Bool) {
  289. if (toShowState) {
  290. self.leftSideEmptyVC.outlineSearchView.frame = CGRectMake((self.tocOutlineView.enclosingScrollView!.documentView!.frame.size.width - self.leftSideEmptyVC.outlineSearchView.bounds.size.width)/2.0, (self.tocOutlineView.enclosingScrollView!.documentView!.frame.size.height - self.leftSideEmptyVC.outlineSearchView.bounds.size.height)/2.0, self.leftSideEmptyVC.outlineSearchView.bounds.size.width, self.leftSideEmptyVC.outlineSearchView.bounds.size.height);
  291. self.tocOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  292. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  293. } else {
  294. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  295. }
  296. }
  297. func removeAllOutline(_ outline: CPDFOutline) {
  298. self.listView.document.setNewOutlineRoot()
  299. for i in 0 ..< self.listView.document.pageCount {
  300. self.listView.document.removeBookmark(forPageIndex: i)
  301. }
  302. self.listView.layoutDocumentView()
  303. DispatchQueue.main.async {
  304. self.tocOutlineView.reloadData()
  305. }
  306. }
  307. func updateSelectRowHeight() {
  308. var rowHeight: CGFloat = 0
  309. let outline: CPDFOutline? = self.tocOutlineView.km.selectedItem()
  310. if outline == nil {
  311. return
  312. }
  313. var attributedString = NSMutableAttributedString()
  314. var dictAttr1 = [NSAttributedString.Key.foregroundColor : KMAppearance.Layout.h0Color()]
  315. var attr1 = NSAttributedString(string: outline!.label, attributes: dictAttr1)
  316. attributedString.append(attr1)
  317. let row = self.tocOutlineView.selectedRow
  318. let viewS = self.tocOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  319. let tableColumn = self.tocOutlineView.tableColumn(withIdentifier: kLabelColumnId)
  320. // // id cell = [tableColumn dataCell];
  321. let cell = tableColumn?.dataCell(forRow: row)
  322. (cell as? NSCell)?.objectValue = attributedString
  323. // CGFloat w = leftSideController.view.frame.size.width - 86;//[tableColumn width] > 260 ? [tableColumn width] : 260;
  324. let w = self.view.frame.size.width-86
  325. let num = self.getNum(outline)
  326. let gap = self.tocOutlineView.indentationPerLevel
  327. rowHeight = ((cell as? NSCell)?.cellSize(forBounds: NSMakeRect(0, 0, w - (num > 0 ? 16 : 0) - gap * num.cgFloat, CGFLOAT_MAX)).height) ?? 0
  328. rowHeight = fmax(rowHeight, self.tocOutlineView.rowHeight) + 25
  329. // [rowHeights setFloat:rowHeight forKey:outline];
  330. var fram = viewS?.frame ?? .zero
  331. viewS?.frame = NSMakeRect(fram.origin.x, fram.origin.y, fram.size.width, rowHeight)
  332. self.tocOutlineView.reloadData()
  333. }
  334. func getNum(_ ol: CPDFOutline?) -> Int {
  335. var num = 0
  336. var outline = ol?.parent
  337. repeat {
  338. outline = outline?.parent
  339. if outline != nil {
  340. num += 1
  341. }
  342. } while outline != nil
  343. return num
  344. }
  345. }
  346. // MARK: - Undo & Redo
  347. extension KMLeftSideViewController {
  348. @objc dynamic func changePDFOutlineDestination(_ destination: CPDFDestination?, PDFoutline outline: CPDFOutline) {
  349. if let des = destination {
  350. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).changePDFOutlineDestination(outline.destination, PDFoutline: outline)
  351. let newDes = CPDFDestination(document: des.document, pageIndex: des.pageIndex, at: des.point, zoom: des.zoom)
  352. outline.destination = newDes
  353. self.tocOutlineView.reloadItem(outline)
  354. }
  355. }
  356. @objc dynamic func changePDFAction(_ action: CPDFAction, PDFOutline outline: CPDFOutline) {
  357. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).changePDFAction(outline.action, PDFOutline: outline)
  358. outline.action = action
  359. }
  360. @objc dynamic func renamePDFOutline(_ outline: CPDFOutline, label: String) {
  361. if (self.isRenameNoteOutline) {
  362. if outline is CPDFAnnotation {
  363. // if([outline isKindOfClass:[PDFAnnotation class]]) {
  364. // PDFAnnotation *annotation = (PDFAnnotation *)outline;
  365. // annotation.string = label;
  366. // [rightSideController.noteOutlineView reloadData];
  367. // [rightSideController.noteOutlineView selectRowIndexes:[[[NSIndexSet alloc] initWithIndex:[rightSideController.noteOutlineView rowForItem:outline]] autorelease] byExtendingSelection:NO];
  368. }
  369. } else {
  370. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).renamePDFOutline(outline, label: outline.label)
  371. outline.label = label
  372. self.tocOutlineView.reloadData()
  373. self.tocOutlineView.km_selectItem(outline, byExtendingSelection: false)
  374. }
  375. }
  376. @objc dynamic func demoteOutlineWithGrandParent(_ grandParentOutline: CPDFOutline, demoteOutline: CPDFOutline, index: Int) {
  377. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).promoteOutlineWithGrandParent(grandParentOutline, promoteOutline: demoteOutline, rowIndex: index)
  378. if grandParentOutline.isEqual(to: demoteOutline.parent) {
  379. let demoteIndex = demoteOutline.index
  380. let previousOutline = grandParentOutline.child(at: demoteIndex-1)
  381. demoteOutline.removeFromParent()
  382. previousOutline?.insertChild(demoteOutline, at: UInt(index))
  383. self.tocOutlineView.reloadData()
  384. self.tocOutlineView.expandItem(previousOutline)
  385. } else {
  386. demoteOutline.removeFromParent()
  387. grandParentOutline.insertChild(demoteOutline, at: grandParentOutline.numberOfChildren)
  388. self.tocOutlineView.reloadData()
  389. self.tocOutlineView.expandItem(grandParentOutline)
  390. }
  391. self.tocOutlineView.km_selectItem(demoteOutline, byExtendingSelection: false)
  392. }
  393. @objc dynamic func promoteOutlineWithGrandParent(_ grandParentOutline: CPDFOutline, promoteOutline: CPDFOutline, rowIndex: Int) {
  394. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).demoteOutlineWithGrandParent(grandParentOutline, demoteOutline: promoteOutline, index: rowIndex)
  395. let index = promoteOutline.parent?.index ?? 0
  396. if grandParentOutline.isEqual(to: promoteOutline.parent) {
  397. promoteOutline.removeFromParent()
  398. grandParentOutline.parent.insertChild(promoteOutline, at: index+1)
  399. } else {
  400. promoteOutline.removeFromParent()
  401. grandParentOutline.insertChild(promoteOutline, at: index+1)
  402. }
  403. self.tocOutlineView.reloadData()
  404. self.tocOutlineView.km_selectItem(promoteOutline, byExtendingSelection: false)
  405. }
  406. @objc dynamic func addoutline(parent parentOutline: CPDFOutline?, addOutline: CPDFOutline, index: Int, needExpand: Bool) {
  407. var tempO: CPDFOutline? = addOutline
  408. if addOutline.label != nil {
  409. parentOutline?.insertChild(addOutline, at: UInt(index))
  410. } else {
  411. let outline = parentOutline?.insertChild(at: UInt(index))
  412. outline?.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), self.listView.currentPageIndex+1)
  413. outline?.destination = self.listView.currentDestination
  414. tempO = outline
  415. }
  416. guard let outline = tempO else {
  417. return
  418. }
  419. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).removeOutline(parent: parentOutline, removeOutline: outline, index: index, needExpand: needExpand)
  420. self.view.window?.makeFirstResponder(nil)
  421. Task { @MainActor in
  422. self.tocOutlineView.reloadData()
  423. if (needExpand) {
  424. self.tocOutlineView.expandItem(parentOutline)
  425. }
  426. let idx = self.tocOutlineView.row(forItem: outline)
  427. self.tocOutlineView.selectRowIndexes(IndexSet(integer: idx), byExtendingSelection: false)
  428. self.newAddOutlineEntryEditingMode(index)
  429. }
  430. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  431. self.tocOutlineView.scrollRowToVisible(self.tocOutlineView.row(forItem: outline))
  432. }
  433. }
  434. @objc dynamic func removeOutline(parent parentOutline: CPDFOutline?, removeOutline: CPDFOutline, index: Int, needExpand: Bool) {
  435. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).addoutline(parent: parentOutline, addOutline: removeOutline, index: index, needExpand: true)
  436. removeOutline.removeFromParent()
  437. if (needExpand) {
  438. self.tocOutlineView.expandItem(parentOutline)
  439. }
  440. Task { @MainActor in
  441. self.tocOutlineView.reloadData()
  442. }
  443. }
  444. @objc dynamic func dragPDFOutline(_ outline: CPDFOutline?, toIndex index: Int, newParentOutline: CPDFOutline?) {
  445. guard let doc = self.view.window?.windowController?.document as? NSDocument else {
  446. NSSound.beep()
  447. return
  448. }
  449. if outline == nil {
  450. NSSound.beep()
  451. return
  452. }
  453. (doc.undoManager?.prepare(withInvocationTarget: self) as AnyObject).dragPDFOutline(outline, toIndex: Int(outline!.index), newParentOutline: outline?.parent)
  454. outline?.removeFromParent()
  455. newParentOutline?.insertChild(outline, at: UInt(index))
  456. self.tocOutlineView.reloadData()
  457. self.tocOutlineView.expandItem(newParentOutline)
  458. self.tocOutlineView.selectRowIndexes(IndexSet(integer: self.tocOutlineView.row(forItem: outline)), byExtendingSelection: false)
  459. }
  460. }
  461. // MARK: - Menu Actions
  462. extension KMLeftSideViewController {
  463. //添加子节点
  464. @objc func outlineContextMenuItemClicked_AddChildEntry(_ sender: AnyObject?) {
  465. let PDFOutlineArray = NSMutableArray()
  466. let rowSet = self.selectedRowIndexes()
  467. for idx in rowSet {
  468. PDFOutlineArray.add(self.tocOutlineView.item(atRow: idx))
  469. }
  470. let currentPDFline = PDFOutlineArray.lastObject as? CPDFOutline
  471. let addOutLine = CPDFOutline()
  472. addOutLine.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), "\(self.listView.currentPageIndex + 1)")
  473. addOutLine.destination = self.listView.currentDestination
  474. self.addoutline(parent: currentPDFline, addOutline: addOutLine, index: Int(currentPDFline?.numberOfChildren ?? 0), needExpand: true)
  475. }
  476. //添加上一级节点
  477. @objc func outlineContextMenuItemClicked_AddAuntEntry(_ sender: AnyObject?) {
  478. let PDFOutlineArray = NSMutableArray()
  479. let rowSet = self.selectedRowIndexes()
  480. for idx in rowSet {
  481. PDFOutlineArray.add(self.tocOutlineView.item(atRow: idx))
  482. }
  483. let clickedOutline = PDFOutlineArray.lastObject as? CPDFOutline
  484. let fatherOutLine = clickedOutline?.parent
  485. let grandfatherOutLine = fatherOutLine?.parent
  486. if (grandfatherOutLine != nil) {
  487. let addOutLine = CPDFOutline()
  488. addOutLine.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), "\(self.listView.currentPageIndex + 1)")
  489. addOutLine.destination = self.listView.currentDestination
  490. self.addoutline(parent: grandfatherOutLine, addOutline: addOutLine, index: Int(fatherOutLine?.index ?? 0) + 1, needExpand: false)
  491. }
  492. }
  493. //移除节点
  494. @objc func outlineContextMenuItemClicked_RemoveEntry(_ sender: AnyObject?) {
  495. let set = self.selectedRowIndexes()
  496. var selectedPDFOutlineArr = NSMutableArray()
  497. for idx in set {
  498. selectedPDFOutlineArr.add(self.tocOutlineView.item(atRow: idx))
  499. }
  500. //整体移除多选
  501. for tOutline in selectedPDFOutlineArr {
  502. guard let outL = tOutline as? CPDFOutline else {
  503. continue
  504. }
  505. self.removeOutline(parent: outL.parent , removeOutline: outL, index: Int(outL.index), needExpand: false)
  506. }
  507. }
  508. //重置目的
  509. @objc func outlineContextMenuItemClicked_SetDestination(_ sender: AnyObject?) {
  510. guard let setPDFOutline = self.tocOutlineView.item(atRow: self.tocOutlineView.clickedRow) as? CPDFOutline else {
  511. return
  512. }
  513. Task {
  514. let modalRes = await KMAlertTool.runModel(style: .informational, message: KMLocalizedString("Are you sure you want to set the destination as the current location?", nil), buttons: [KMLocalizedString("Yes", nil), KMLocalizedString("No", nil)])
  515. if (modalRes == .alertFirstButtonReturn) {
  516. self.changePDFOutlineDestination(self.listView.currentDestination, PDFoutline: setPDFOutline)
  517. self.tocOutlineView.reloadData()
  518. let idx = self.tocOutlineView.row(forItem: setPDFOutline)
  519. self.tocOutlineView.selectRowIndexes(IndexSet(integer: idx), byExtendingSelection: false)
  520. }
  521. }
  522. }
  523. //弹出菜单
  524. @objc func outlineContextMenuItemClicked_Edit(_ sender: AnyObject?) {
  525. let popover = NSPopover()
  526. popover.delegate = self
  527. let targetOutline: CPDFOutline? = self.tocOutlineView.km.clickedItem()
  528. let outlineEditViewController = KMOutlineEditViewController(outline: targetOutline, document: self.listView)
  529. let cell = self.tocOutlineView.rowView(atRow: self.tocOutlineView.clickedRow, makeIfNecessary: true)
  530. popover.contentViewController = outlineEditViewController
  531. popover.animates = true
  532. popover.behavior = .transient
  533. popover.show(relativeTo: cell?.bounds ?? .zero, of: cell!, preferredEdge: .minX)
  534. }
  535. //重命名
  536. @objc func outlineContextMenuItemClicked_Rename(_ sender: AnyObject?) {
  537. if (self.tocOutlineView.clickedRow < 0) {
  538. return
  539. }
  540. self.renamePDFOutline = self.tocOutlineView.km.clickedItem()
  541. let viewS = self.tocOutlineView.view(atColumn: 0, row: self.tocOutlineView.clickedRow, makeIfNecessary: true)
  542. let targrtTextField = viewS?.subviews.first as? NSTextField
  543. self.renamePDFOutlineTextField = targrtTextField
  544. targrtTextField?.delegate = self
  545. targrtTextField?.isEditable = true
  546. targrtTextField?.becomeFirstResponder()
  547. }
  548. //降级节点
  549. @objc func outlineContextMenuItemClicked_Demote(_ sender: AnyObject?) {
  550. guard let currentOutline: CPDFOutline = self.tocOutlineView.km.clickedItem() else {
  551. return
  552. }
  553. let parentOutLine = currentOutline.parent
  554. let newParentOutLine = parentOutLine?.child(at: currentOutline.index-1)
  555. var newIndex = 0
  556. let newParentOutLineExpandState = self.tocOutlineView.isItemExpanded(newParentOutLine)
  557. if (newParentOutLineExpandState) {
  558. newIndex = self.tocOutlineView.clickedRow
  559. } else {
  560. newIndex = self.tocOutlineView.clickedRow + Int(newParentOutLine?.numberOfChildren ?? 0)
  561. }
  562. let currentIndex = currentOutline.index
  563. currentOutline.removeFromParent()
  564. self.demoteOutlineWithGrandParent(newParentOutLine!, demoteOutline: currentOutline, index: Int(currentIndex))
  565. }
  566. //升级节点
  567. @objc func outlineContextMenuItemClicked_Promote(_ sender: AnyObject?) {
  568. guard let currentOutline: CPDFOutline = self.tocOutlineView.km.clickedItem() else {
  569. return
  570. }
  571. let parentOutLine = currentOutline.parent
  572. if let grandParentOutLine = parentOutLine?.parent {
  573. self.promoteOutlineWithGrandParent(grandParentOutLine, promoteOutline: currentOutline, rowIndex:Int(currentOutline.index))
  574. }
  575. }
  576. @objc func toggleOutlineCaseInsensitiveSearch(_ sender: NSMenuItem) {
  577. if (sender.state == .on) {
  578. self.outlineIgnoreCaseFlag = false
  579. } else {
  580. self.outlineIgnoreCaseFlag = true
  581. }
  582. if (self.outlineSearchField.stringValue.isEmpty == false) {
  583. self.tocOutlineView.reloadData()
  584. self.tocOutlineView.expandItem(nil, expandChildren: true)
  585. }
  586. }
  587. }
  588. // MARK: - KMTocOutlineViewDelegate
  589. extension KMLeftSideViewController: KMTocOutlineViewDelegate {
  590. func outlineView(_ anOutlineView: NSOutlineView, highlightLevelForRow row: Int) -> Int {
  591. if self.tocOutlineView.isEqual(to: anOutlineView) {
  592. let numRows = anOutlineView.numberOfRows
  593. if let outline = anOutlineView.item(atRow: row) as? CPDFOutline {
  594. let firstPage = outline.km_pageIndex
  595. var lastPage = self.listView.document.pageCount
  596. if row + 1 < numRows {
  597. lastPage = (anOutlineView.item(atRow: row + 1) as? CPDFOutline)?.km_pageIndex ?? UInt(NSNotFound)
  598. }
  599. // NSRange range = NSMakeRange(firstPage, MAX(1LU, lastPage - firstPage));
  600. // NSUInteger i, iMax = [lastViewedPages count];
  601. // for (i = 0; i < iMax; i++) {
  602. // if (NSLocationInRange((NSUInteger)[lastViewedPages pointerAtIndex:i], range))
  603. // return i;
  604. // }
  605. }
  606. }
  607. return NSNotFound
  608. }
  609. func outlineView(_ anOutlineView: NSOutlineView, imageContextForItem item: Any?) -> AnyObject? {
  610. if anOutlineView.isEqual(to: self.tocOutlineView) {
  611. if item == nil {
  612. return true as AnyObject
  613. }
  614. if item is CPDFOutline {
  615. return (item as! CPDFOutline).destination
  616. }
  617. }
  618. return nil
  619. }
  620. }
  621. extension KMLeftSideViewController: KMNoteOutlineViewDelegate {
  622. func outlineView(_ anOutlineView: NSOutlineView, canResizeRowByItem item: AnyObject?) -> Bool? {
  623. if anOutlineView.isEqual(to: self.noteOutlineView) {
  624. return true
  625. }
  626. return false
  627. }
  628. func outlineView(_ anOutlineView: NSOutlineView, setHeight newHeight: CGFloat, ofRowByItem item: AnyObject?) {
  629. // [rowHeights setFloat:newHeight forKey:item];
  630. }
  631. func outlineView(_ anOutlineView: NSOutlineView, didChangeHiddenOfTableColumn aTableColumn: NSTableColumn) {
  632. // if (mwcFlags.autoResizeNoteRows &&
  633. // [ov isEqual:rightSideController.noteOutlineView] &&
  634. // [[tableColumn identifier] isEqualToString:NOTE_COLUMNID]) {
  635. // [rowHeights removeAllFloats];
  636. // [rightSideController.noteOutlineView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [rightSideController.noteOutlineView numberOfRows])]];
  637. // }
  638. }
  639. func outlineViewCommandKeyPressedDuringNavigation(_ anOutlineView: NSOutlineView) {
  640. // PDFAnnotation *annotation = [[self selectedNotes] lastObject];
  641. // if (annotation) {
  642. // [pdfView scrollAnnotationToVisible:annotation];
  643. // [pdfView setActiveAnnotation:annotation];
  644. // }
  645. }
  646. }