KMLeftSideViewController+Outline.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. let documentViewFrame = self.tocOutlineView.enclosingScrollView?.documentView?.frame ?? .zero
  293. self.leftSideEmptyVC.outlineSearchView.frame = CGRectMake((documentViewFrame.size.width - self.leftSideEmptyVC.outlineSearchView.bounds.size.width)/2.0, (documentViewFrame.size.height - self.leftSideEmptyVC.outlineSearchView.bounds.size.height)/2.0, self.leftSideEmptyVC.outlineSearchView.bounds.size.width, self.leftSideEmptyVC.outlineSearchView.bounds.size.height);
  294. self.tocOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  295. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  296. } else {
  297. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  298. }
  299. }
  300. func removeAllOutline(_ outline: CPDFOutline) {
  301. self.listView.document.setNewOutlineRoot()
  302. for i in 0 ..< self.listView.document.pageCount {
  303. self.listView.document.removeBookmark(forPageIndex: i)
  304. }
  305. self.listView.layoutDocumentView()
  306. DispatchQueue.main.async {
  307. self.tocOutlineView.reloadData()
  308. }
  309. }
  310. func updateSelectRowHeight() {
  311. var rowHeight: CGFloat = 0
  312. let outline: CPDFOutline? = self.tocOutlineView.km.selectedItem()
  313. if outline == nil {
  314. return
  315. }
  316. var attributedString = NSMutableAttributedString()
  317. var dictAttr1 = [NSAttributedString.Key.foregroundColor : KMAppearance.Layout.h0Color()]
  318. var attr1 = NSAttributedString(string: outline?.label ?? "", attributes: dictAttr1)
  319. attributedString.append(attr1)
  320. let row = self.tocOutlineView.selectedRow
  321. let viewS = self.tocOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  322. let tableColumn = self.tocOutlineView.tableColumn(withIdentifier: kLabelColumnId)
  323. // // id cell = [tableColumn dataCell];
  324. let cell = tableColumn?.dataCell(forRow: row)
  325. (cell as? NSCell)?.objectValue = attributedString
  326. // CGFloat w = leftSideController.view.frame.size.width - 86;//[tableColumn width] > 260 ? [tableColumn width] : 260;
  327. let w = self.view.frame.size.width-86
  328. let num = self.getNum(outline)
  329. let gap = self.tocOutlineView.indentationPerLevel
  330. rowHeight = ((cell as? NSCell)?.cellSize(forBounds: NSMakeRect(0, 0, w - (num > 0 ? 16 : 0) - gap * num.cgFloat, CGFLOAT_MAX)).height) ?? 0
  331. rowHeight = fmax(rowHeight, self.tocOutlineView.rowHeight) + 25
  332. // [rowHeights setFloat:rowHeight forKey:outline];
  333. var fram = viewS?.frame ?? .zero
  334. viewS?.frame = NSMakeRect(fram.origin.x, fram.origin.y, fram.size.width, rowHeight)
  335. self.tocOutlineView.reloadData()
  336. }
  337. func getNum(_ ol: CPDFOutline?) -> Int {
  338. var num = 0
  339. var outline = ol?.parent
  340. repeat {
  341. outline = outline?.parent
  342. if outline != nil {
  343. num += 1
  344. }
  345. } while outline != nil
  346. return num
  347. }
  348. }
  349. // MARK: - Undo & Redo
  350. extension KMLeftSideViewController {
  351. @objc dynamic func changePDFOutlineDestination(_ destination: CPDFDestination?, PDFoutline outline: CPDFOutline) {
  352. if let des = destination {
  353. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).changePDFOutlineDestination(outline.destination, PDFoutline: outline)
  354. let newDes = CPDFDestination(document: des.document, pageIndex: des.pageIndex, at: des.point, zoom: des.zoom)
  355. outline.destination = newDes
  356. self.tocOutlineView.reloadItem(outline)
  357. }
  358. }
  359. @objc dynamic func changePDFAction(_ action: CPDFAction, PDFOutline outline: CPDFOutline) {
  360. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).changePDFAction(outline.action, PDFOutline: outline)
  361. outline.action = action
  362. }
  363. @objc dynamic func renamePDFOutline(_ outline: CPDFOutline, label: String) {
  364. if (self.isRenameNoteOutline) {
  365. if outline is CPDFAnnotation {
  366. // if([outline isKindOfClass:[PDFAnnotation class]]) {
  367. // PDFAnnotation *annotation = (PDFAnnotation *)outline;
  368. // annotation.string = label;
  369. // [rightSideController.noteOutlineView reloadData];
  370. // [rightSideController.noteOutlineView selectRowIndexes:[[[NSIndexSet alloc] initWithIndex:[rightSideController.noteOutlineView rowForItem:outline]] autorelease] byExtendingSelection:NO];
  371. }
  372. } else {
  373. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).renamePDFOutline(outline, label: outline.label)
  374. outline.label = label
  375. self.tocOutlineView.reloadData()
  376. self.tocOutlineView.km_selectItem(outline, byExtendingSelection: false)
  377. }
  378. }
  379. @objc dynamic func demoteOutlineWithGrandParent(_ grandParentOutline: CPDFOutline, demoteOutline: CPDFOutline, index: Int) {
  380. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).promoteOutlineWithGrandParent(grandParentOutline, promoteOutline: demoteOutline, rowIndex: index)
  381. if grandParentOutline.isEqual(to: demoteOutline.parent) {
  382. let demoteIndex = demoteOutline.index
  383. let previousOutline = grandParentOutline.child(at: demoteIndex-1)
  384. demoteOutline.removeFromParent()
  385. previousOutline?.insertChild(demoteOutline, at: UInt(index))
  386. self.tocOutlineView.reloadData()
  387. self.tocOutlineView.expandItem(previousOutline)
  388. } else {
  389. demoteOutline.removeFromParent()
  390. grandParentOutline.insertChild(demoteOutline, at: grandParentOutline.numberOfChildren)
  391. self.tocOutlineView.reloadData()
  392. self.tocOutlineView.expandItem(grandParentOutline)
  393. }
  394. self.tocOutlineView.km_selectItem(demoteOutline, byExtendingSelection: false)
  395. }
  396. @objc dynamic func promoteOutlineWithGrandParent(_ grandParentOutline: CPDFOutline, promoteOutline: CPDFOutline, rowIndex: Int) {
  397. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).demoteOutlineWithGrandParent(grandParentOutline, demoteOutline: promoteOutline, index: rowIndex)
  398. let index = promoteOutline.parent?.index ?? 0
  399. if grandParentOutline.isEqual(to: promoteOutline.parent) {
  400. promoteOutline.removeFromParent()
  401. grandParentOutline.parent.insertChild(promoteOutline, at: index+1)
  402. } else {
  403. promoteOutline.removeFromParent()
  404. grandParentOutline.insertChild(promoteOutline, at: index+1)
  405. }
  406. self.tocOutlineView.reloadData()
  407. self.tocOutlineView.km_selectItem(promoteOutline, byExtendingSelection: false)
  408. }
  409. @objc dynamic func addoutline(parent parentOutline: CPDFOutline?, addOutline: CPDFOutline, index: Int, needExpand: Bool) {
  410. var tempO: CPDFOutline? = addOutline
  411. if addOutline.label != nil {
  412. parentOutline?.insertChild(addOutline, at: UInt(index))
  413. } else {
  414. let outline = parentOutline?.insertChild(at: UInt(index))
  415. outline?.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), self.listView.currentPageIndex+1)
  416. outline?.destination = self.listView.currentDestination
  417. tempO = outline
  418. }
  419. guard let outline = tempO else {
  420. return
  421. }
  422. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).removeOutline(parent: parentOutline, removeOutline: outline, index: index, needExpand: needExpand)
  423. self.view.window?.makeFirstResponder(nil)
  424. Task { @MainActor in
  425. self.tocOutlineView.reloadData()
  426. if (needExpand) {
  427. self.tocOutlineView.expandItem(parentOutline)
  428. }
  429. let idx = self.tocOutlineView.row(forItem: outline)
  430. self.tocOutlineView.selectRowIndexes(IndexSet(integer: idx), byExtendingSelection: false)
  431. self.newAddOutlineEntryEditingMode(index)
  432. }
  433. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  434. self.tocOutlineView.scrollRowToVisible(self.tocOutlineView.row(forItem: outline))
  435. }
  436. }
  437. @objc dynamic func removeOutline(parent parentOutline: CPDFOutline?, removeOutline: CPDFOutline, index: Int, needExpand: Bool) {
  438. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).addoutline(parent: parentOutline, addOutline: removeOutline, index: index, needExpand: true)
  439. removeOutline.removeFromParent()
  440. if (needExpand) {
  441. self.tocOutlineView.expandItem(parentOutline)
  442. }
  443. Task { @MainActor in
  444. self.tocOutlineView.reloadData()
  445. }
  446. }
  447. @objc dynamic func dragPDFOutline(_ outline: CPDFOutline?, toIndex index: Int, newParentOutline: CPDFOutline?) {
  448. guard let doc = self.view.window?.windowController?.document as? NSDocument else {
  449. NSSound.beep()
  450. return
  451. }
  452. if outline == nil {
  453. NSSound.beep()
  454. return
  455. }
  456. (doc.undoManager?.prepare(withInvocationTarget: self) as AnyObject).dragPDFOutline(outline, toIndex: Int(outline!.index), newParentOutline: outline?.parent)
  457. outline?.removeFromParent()
  458. newParentOutline?.insertChild(outline, at: UInt(index))
  459. self.tocOutlineView.reloadData()
  460. self.tocOutlineView.expandItem(newParentOutline)
  461. self.tocOutlineView.selectRowIndexes(IndexSet(integer: self.tocOutlineView.row(forItem: outline)), byExtendingSelection: false)
  462. }
  463. }
  464. // MARK: - Menu Actions
  465. extension KMLeftSideViewController {
  466. //添加子节点
  467. @objc func outlineContextMenuItemClicked_AddChildEntry(_ sender: AnyObject?) {
  468. let PDFOutlineArray = NSMutableArray()
  469. let rowSet = self.selectedRowIndexes()
  470. for idx in rowSet {
  471. PDFOutlineArray.add(self.tocOutlineView.item(atRow: idx))
  472. }
  473. let currentPDFline = PDFOutlineArray.lastObject as? CPDFOutline
  474. let addOutLine = CPDFOutline()
  475. addOutLine.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), "\(self.listView.currentPageIndex + 1)")
  476. addOutLine.destination = self.listView.currentDestination
  477. self.addoutline(parent: currentPDFline, addOutline: addOutLine, index: Int(currentPDFline?.numberOfChildren ?? 0), needExpand: true)
  478. }
  479. //添加上一级节点
  480. @objc func outlineContextMenuItemClicked_AddAuntEntry(_ sender: AnyObject?) {
  481. let PDFOutlineArray = NSMutableArray()
  482. let rowSet = self.selectedRowIndexes()
  483. for idx in rowSet {
  484. PDFOutlineArray.add(self.tocOutlineView.item(atRow: idx))
  485. }
  486. let clickedOutline = PDFOutlineArray.lastObject as? CPDFOutline
  487. let fatherOutLine = clickedOutline?.parent
  488. let grandfatherOutLine = fatherOutLine?.parent
  489. if (grandfatherOutLine != nil) {
  490. let addOutLine = CPDFOutline()
  491. addOutLine.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), "\(self.listView.currentPageIndex + 1)")
  492. addOutLine.destination = self.listView.currentDestination
  493. self.addoutline(parent: grandfatherOutLine, addOutline: addOutLine, index: Int(fatherOutLine?.index ?? 0) + 1, needExpand: false)
  494. }
  495. }
  496. //移除节点
  497. @objc func outlineContextMenuItemClicked_RemoveEntry(_ sender: AnyObject?) {
  498. let set = self.selectedRowIndexes()
  499. var selectedPDFOutlineArr = NSMutableArray()
  500. for idx in set {
  501. selectedPDFOutlineArr.add(self.tocOutlineView.item(atRow: idx))
  502. }
  503. //整体移除多选
  504. for tOutline in selectedPDFOutlineArr {
  505. guard let outL = tOutline as? CPDFOutline else {
  506. continue
  507. }
  508. self.removeOutline(parent: outL.parent , removeOutline: outL, index: Int(outL.index), needExpand: false)
  509. }
  510. }
  511. //重置目的
  512. @objc func outlineContextMenuItemClicked_SetDestination(_ sender: AnyObject?) {
  513. guard let setPDFOutline = self.tocOutlineView.item(atRow: self.tocOutlineView.clickedRow) as? CPDFOutline else {
  514. return
  515. }
  516. Task {
  517. 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)])
  518. if (modalRes == .alertFirstButtonReturn) {
  519. self.changePDFOutlineDestination(self.listView.currentDestination, PDFoutline: setPDFOutline)
  520. self.tocOutlineView.reloadData()
  521. let idx = self.tocOutlineView.row(forItem: setPDFOutline)
  522. self.tocOutlineView.selectRowIndexes(IndexSet(integer: idx), byExtendingSelection: false)
  523. }
  524. }
  525. }
  526. //弹出菜单
  527. @objc func outlineContextMenuItemClicked_Edit(_ sender: AnyObject?) {
  528. let popover = NSPopover()
  529. popover.delegate = self
  530. let targetOutline: CPDFOutline? = self.tocOutlineView.km.clickedItem()
  531. let outlineEditViewController = KMOutlineEditViewController(outline: targetOutline, document: self.listView)
  532. if let cell = self.tocOutlineView.rowView(atRow: self.tocOutlineView.clickedRow, makeIfNecessary: true) {
  533. popover.contentViewController = outlineEditViewController
  534. popover.animates = true
  535. popover.behavior = .transient
  536. popover.show(relativeTo: cell.bounds, of: cell, preferredEdge: .minX)
  537. }
  538. }
  539. //重命名
  540. @objc func outlineContextMenuItemClicked_Rename(_ sender: AnyObject?) {
  541. if (self.tocOutlineView.clickedRow < 0) {
  542. return
  543. }
  544. self.renamePDFOutline = self.tocOutlineView.km.clickedItem()
  545. let viewS = self.tocOutlineView.view(atColumn: 0, row: self.tocOutlineView.clickedRow, makeIfNecessary: true)
  546. let targrtTextField = viewS?.subviews.first as? NSTextField
  547. self.renamePDFOutlineTextField = targrtTextField
  548. targrtTextField?.delegate = self
  549. targrtTextField?.isEditable = true
  550. targrtTextField?.becomeFirstResponder()
  551. }
  552. //降级节点
  553. @objc func outlineContextMenuItemClicked_Demote(_ sender: AnyObject?) {
  554. guard let currentOutline: CPDFOutline = self.tocOutlineView.km.clickedItem() else {
  555. return
  556. }
  557. let parentOutLine = currentOutline.parent
  558. let newParentOutLine = parentOutLine?.child(at: currentOutline.index-1)
  559. var newIndex = 0
  560. let newParentOutLineExpandState = self.tocOutlineView.isItemExpanded(newParentOutLine)
  561. if (newParentOutLineExpandState) {
  562. newIndex = self.tocOutlineView.clickedRow
  563. } else {
  564. newIndex = self.tocOutlineView.clickedRow + Int(newParentOutLine?.numberOfChildren ?? 0)
  565. }
  566. let currentIndex = currentOutline.index
  567. currentOutline.removeFromParent()
  568. if let data = newParentOutLine {
  569. self.demoteOutlineWithGrandParent(data, demoteOutline: currentOutline, index: Int(currentIndex))
  570. }
  571. }
  572. //升级节点
  573. @objc func outlineContextMenuItemClicked_Promote(_ sender: AnyObject?) {
  574. guard let currentOutline: CPDFOutline = self.tocOutlineView.km.clickedItem() else {
  575. return
  576. }
  577. let parentOutLine = currentOutline.parent
  578. if let grandParentOutLine = parentOutLine?.parent {
  579. self.promoteOutlineWithGrandParent(grandParentOutLine, promoteOutline: currentOutline, rowIndex:Int(currentOutline.index))
  580. }
  581. }
  582. @objc func toggleOutlineCaseInsensitiveSearch(_ sender: NSMenuItem) {
  583. if (sender.state == .on) {
  584. self.outlineIgnoreCaseFlag = false
  585. } else {
  586. self.outlineIgnoreCaseFlag = true
  587. }
  588. if (self.outlineSearchField.stringValue.isEmpty == false) {
  589. self.tocOutlineView.reloadData()
  590. self.tocOutlineView.expandItem(nil, expandChildren: true)
  591. }
  592. }
  593. }
  594. // MARK: - KMTocOutlineViewDelegate
  595. extension KMLeftSideViewController: KMTocOutlineViewDelegate {
  596. func outlineView(_ anOutlineView: NSOutlineView, highlightLevelForRow row: Int) -> Int {
  597. if self.tocOutlineView.isEqual(to: anOutlineView) {
  598. let numRows = anOutlineView.numberOfRows
  599. if let outline = anOutlineView.item(atRow: row) as? CPDFOutline {
  600. let firstPage = outline.km_pageIndex
  601. var lastPage = self.listView.document.pageCount
  602. if row + 1 < numRows {
  603. lastPage = (anOutlineView.item(atRow: row + 1) as? CPDFOutline)?.km_pageIndex ?? UInt(NSNotFound)
  604. }
  605. // NSRange range = NSMakeRange(firstPage, MAX(1LU, lastPage - firstPage));
  606. // NSUInteger i, iMax = [lastViewedPages count];
  607. // for (i = 0; i < iMax; i++) {
  608. // if (NSLocationInRange((NSUInteger)[lastViewedPages pointerAtIndex:i], range))
  609. // return i;
  610. // }
  611. }
  612. }
  613. return NSNotFound
  614. }
  615. func outlineView(_ anOutlineView: NSOutlineView, imageContextForItem item: Any?) -> AnyObject? {
  616. if anOutlineView.isEqual(to: self.tocOutlineView) {
  617. if item == nil {
  618. return true as AnyObject
  619. }
  620. if item is CPDFOutline {
  621. return (item as! CPDFOutline).destination
  622. }
  623. }
  624. return nil
  625. }
  626. }
  627. extension KMLeftSideViewController: KMNoteOutlineViewDelegate {
  628. func outlineView(_ anOutlineView: NSOutlineView, canResizeRowByItem item: AnyObject?) -> Bool? {
  629. if anOutlineView.isEqual(to: self.noteOutlineView) {
  630. return true
  631. }
  632. return false
  633. }
  634. func outlineView(_ anOutlineView: NSOutlineView, setHeight newHeight: CGFloat, ofRowByItem item: AnyObject?) {
  635. // [rowHeights setFloat:newHeight forKey:item];
  636. }
  637. func outlineView(_ anOutlineView: NSOutlineView, didChangeHiddenOfTableColumn aTableColumn: NSTableColumn) {
  638. // if (mwcFlags.autoResizeNoteRows &&
  639. // [ov isEqual:rightSideController.noteOutlineView] &&
  640. // [[tableColumn identifier] isEqualToString:NOTE_COLUMNID]) {
  641. // [rowHeights removeAllFloats];
  642. // [rightSideController.noteOutlineView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [rightSideController.noteOutlineView numberOfRows])]];
  643. // }
  644. }
  645. func outlineViewCommandKeyPressedDuringNavigation(_ anOutlineView: NSOutlineView) {
  646. // PDFAnnotation *annotation = [[self selectedNotes] lastObject];
  647. // if (annotation) {
  648. // [pdfView scrollAnnotationToVisible:annotation];
  649. // [pdfView setActiveAnnotation:annotation];
  650. // }
  651. }
  652. }