KMLeftSideViewController+Outline.swift 33 KB

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