KMLeftSideViewController+Outline.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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.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.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. let 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) as Any)
  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.outlineRoot()
  228. if ((rootPDFOutline == nil)) {
  229. // rootPDFOutline = CPDFOutline()
  230. // self.listView.document.setOutlineRoot(rootPDFOutline)
  231. rootPDFOutline = self.setNewOutlineRoot()
  232. }
  233. }
  234. let addOutLine = CPDFOutline()
  235. addOutLine.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), self.currentPageIndex()+1)
  236. addOutLine.destination = self.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.currentPageIndex()+1)
  245. addOutLine.destination = self.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.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.setNewOutlineRoot()
  302. for i in 0 ..< self.pageCount() {
  303. self.pdfDocument()?.removeBookmark(forPageIndex: UInt(i))
  304. }
  305. self.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. func addOutlineAfter(_ ol: CPDFOutline) {
  349. if self.leftView.segmentedControl.selectedSegment == 1 {
  350. } else {
  351. self.leftView.segmentedControl.selectedSegment = 1
  352. }
  353. Task { @MainActor in
  354. self.tocOutlineView.reloadData()
  355. let idx = self.tocOutlineView.row(forItem: ol)
  356. self.tocOutlineView.km_selectItem(ol, byExtendingSelection: false)
  357. self.newAddOutlineEntryEditingMode(idx)
  358. }
  359. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  360. self.tocOutlineView.scrollRowToVisible(self.tocOutlineView.row(forItem: ol))
  361. }
  362. }
  363. func removeOutlineAfter(_ ol: CPDFOutline) {
  364. Task { @MainActor in
  365. self.tocOutlineView.reloadData()
  366. }
  367. }
  368. func demoteOutlineAfter(_ ol: CPDFOutline) {
  369. Task { @MainActor in
  370. self.tocOutlineView.reloadData()
  371. self.tocOutlineView.expandItem(ol.parent)
  372. self.tocOutlineView.km_selectItem(ol, byExtendingSelection: false)
  373. }
  374. }
  375. func promoteOutlineAfter(_ ol: CPDFOutline) {
  376. Task { @MainActor in
  377. self.tocOutlineView.reloadData()
  378. self.tocOutlineView.km_selectItem(ol, byExtendingSelection: false)
  379. }
  380. }
  381. }
  382. // MARK: - Undo & Redo
  383. extension KMLeftSideViewController {
  384. @objc dynamic func changePDFOutlineDestination(_ destination: CPDFDestination?, PDFoutline outline: CPDFOutline) {
  385. if let des = destination {
  386. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).changePDFOutlineDestination(outline.destination, PDFoutline: outline)
  387. let newDes = CPDFDestination(document: des.document, pageIndex: des.pageIndex, at: des.point, zoom: des.zoom)
  388. outline.destination = newDes
  389. self.tocOutlineView.reloadItem(outline)
  390. }
  391. }
  392. @objc dynamic func changePDFAction(_ action: CPDFAction, PDFOutline outline: CPDFOutline) {
  393. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).changePDFAction(outline.action, PDFOutline: outline)
  394. outline.action = action
  395. }
  396. @objc dynamic func renamePDFOutline(_ outline: CPDFOutline, label: String) {
  397. if (self.isRenameNoteOutline) {
  398. if outline is CPDFAnnotation {
  399. // if([outline isKindOfClass:[PDFAnnotation class]]) {
  400. // PDFAnnotation *annotation = (PDFAnnotation *)outline;
  401. // annotation.string = label;
  402. // [rightSideController.noteOutlineView reloadData];
  403. // [rightSideController.noteOutlineView selectRowIndexes:[[[NSIndexSet alloc] initWithIndex:[rightSideController.noteOutlineView rowForItem:outline]] autorelease] byExtendingSelection:NO];
  404. }
  405. } else {
  406. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).renamePDFOutline(outline, label: outline.label)
  407. outline.label = label
  408. self.tocOutlineView.reloadData()
  409. self.tocOutlineView.km_selectItem(outline, byExtendingSelection: false)
  410. }
  411. }
  412. @objc dynamic func addoutline(parent parentOutline: CPDFOutline?, addOutline: CPDFOutline, index: Int, needExpand: Bool) {
  413. var tempO: CPDFOutline? = addOutline
  414. if addOutline.label != nil {
  415. parentOutline?.insertChild(addOutline, at: UInt(index))
  416. } else {
  417. let outline = parentOutline?.km_insertChild(label: String(format: "%@ %ld", KMLocalizedString("Page", nil), self.currentPageIndex()+1), dest: self.currentDestination(), at: index)
  418. tempO = outline
  419. }
  420. guard let outline = tempO else {
  421. return
  422. }
  423. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).removeOutline(parent: parentOutline, removeOutline: outline, index: index, needExpand: needExpand)
  424. self.view.window?.makeFirstResponder(nil)
  425. Task { @MainActor in
  426. self.tocOutlineView.reloadData()
  427. if (needExpand) {
  428. self.tocOutlineView.expandItem(parentOutline)
  429. }
  430. let idx = self.tocOutlineView.row(forItem: outline)
  431. self.tocOutlineView.selectRowIndexes(IndexSet(integer: idx), byExtendingSelection: false)
  432. self.newAddOutlineEntryEditingMode(index)
  433. }
  434. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  435. self.tocOutlineView.scrollRowToVisible(self.tocOutlineView.row(forItem: outline))
  436. }
  437. }
  438. @objc dynamic func removeOutline(parent parentOutline: CPDFOutline?, removeOutline: CPDFOutline, index: Int, needExpand: Bool) {
  439. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).addoutline(parent: parentOutline, addOutline: removeOutline, index: index, needExpand: true)
  440. removeOutline.removeFromParent()
  441. if (needExpand) {
  442. self.tocOutlineView.expandItem(parentOutline)
  443. }
  444. Task { @MainActor in
  445. self.tocOutlineView.reloadData()
  446. }
  447. }
  448. @objc dynamic func dragPDFOutline(_ outline: CPDFOutline?, toIndex index: Int, newParentOutline: CPDFOutline?) {
  449. guard let doc = self.view.window?.windowController?.document as? NSDocument else {
  450. NSSound.beep()
  451. return
  452. }
  453. if outline == nil {
  454. NSSound.beep()
  455. return
  456. }
  457. (doc.undoManager?.prepare(withInvocationTarget: self) as AnyObject).dragPDFOutline(outline, toIndex: Int(outline!.index), newParentOutline: outline?.parent)
  458. outline?.removeFromParent()
  459. newParentOutline?.insertChild(outline, at: UInt(index))
  460. self.tocOutlineView.reloadData()
  461. self.tocOutlineView.expandItem(newParentOutline)
  462. self.tocOutlineView.selectRowIndexes(IndexSet(integer: self.tocOutlineView.row(forItem: outline)), byExtendingSelection: false)
  463. }
  464. }
  465. // MARK: - Menu Actions
  466. extension KMLeftSideViewController {
  467. //添加子节点
  468. @objc func outlineContextMenuItemClicked_AddChildEntry(_ sender: AnyObject?) {
  469. let PDFOutlineArray = NSMutableArray()
  470. let rowSet = self.selectedRowIndexes()
  471. for idx in rowSet {
  472. PDFOutlineArray.add(self.tocOutlineView.item(atRow: idx) as Any)
  473. }
  474. let currentPDFline = PDFOutlineArray.lastObject as? CPDFOutline
  475. let addOutLine = CPDFOutline()
  476. addOutLine.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), "\(self.currentPageIndex() + 1)")
  477. addOutLine.destination = self.currentDestination()
  478. self.addoutline(parent: currentPDFline, addOutline: addOutLine, index: Int(currentPDFline?.numberOfChildren ?? 0), needExpand: true)
  479. }
  480. //添加上一级节点
  481. @objc func outlineContextMenuItemClicked_AddAuntEntry(_ sender: AnyObject?) {
  482. let PDFOutlineArray = NSMutableArray()
  483. let rowSet = self.selectedRowIndexes()
  484. for idx in rowSet {
  485. PDFOutlineArray.add(self.tocOutlineView.item(atRow: idx) as Any)
  486. }
  487. let clickedOutline = PDFOutlineArray.lastObject as? CPDFOutline
  488. let fatherOutLine = clickedOutline?.parent
  489. let grandfatherOutLine = fatherOutLine?.parent
  490. if (grandfatherOutLine != nil) {
  491. let addOutLine = CPDFOutline()
  492. addOutLine.label = String(format: "%@ %ld", KMLocalizedString("Page", nil), "\(self.currentPageIndex() + 1)")
  493. addOutLine.destination = self.currentDestination()
  494. self.addoutline(parent: grandfatherOutLine, addOutline: addOutLine, index: Int(fatherOutLine?.index ?? 0) + 1, needExpand: false)
  495. }
  496. }
  497. //移除节点
  498. @objc func outlineContextMenuItemClicked_RemoveEntry(_ sender: AnyObject?) {
  499. let set = self.selectedRowIndexes()
  500. var selectedPDFOutlineArr = NSMutableArray()
  501. for idx in set {
  502. selectedPDFOutlineArr.add(self.tocOutlineView.item(atRow: idx) as Any)
  503. }
  504. //整体移除多选
  505. for tOutline in selectedPDFOutlineArr {
  506. guard let outL = tOutline as? CPDFOutline else {
  507. continue
  508. }
  509. self.removeOutline(parent: outL.parent , removeOutline: outL, index: Int(outL.index), needExpand: false)
  510. }
  511. }
  512. //重置目的
  513. @objc func outlineContextMenuItemClicked_SetDestination(_ sender: AnyObject?) {
  514. guard let setPDFOutline = self.tocOutlineView.item(atRow: self.tocOutlineView.clickedRow) as? CPDFOutline else {
  515. return
  516. }
  517. Task {
  518. 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)])
  519. if (modalRes == .alertFirstButtonReturn) {
  520. self.changePDFOutlineDestination(self.currentDestination(), PDFoutline: setPDFOutline)
  521. self.tocOutlineView.reloadData()
  522. let idx = self.tocOutlineView.row(forItem: setPDFOutline)
  523. self.tocOutlineView.selectRowIndexes(IndexSet(integer: idx), byExtendingSelection: false)
  524. }
  525. }
  526. }
  527. //弹出菜单
  528. @objc func outlineContextMenuItemClicked_Edit(_ sender: AnyObject?) {
  529. let popover = NSPopover()
  530. popover.delegate = self
  531. let targetOutline: CPDFOutline? = self.tocOutlineView.km.clickedItem()
  532. let outlineEditViewController = KMOutlineEditViewController(outline: targetOutline, document: self.listView)
  533. if let cell = self.tocOutlineView.rowView(atRow: self.tocOutlineView.clickedRow, makeIfNecessary: true) {
  534. popover.contentViewController = outlineEditViewController
  535. popover.animates = true
  536. popover.behavior = .transient
  537. popover.show(relativeTo: cell.bounds, of: cell, preferredEdge: .minX)
  538. }
  539. }
  540. //重命名
  541. @objc func outlineContextMenuItemClicked_Rename(_ sender: AnyObject?) {
  542. if (self.tocOutlineView.clickedRow < 0) {
  543. return
  544. }
  545. self.renamePDFOutline = self.tocOutlineView.km.clickedItem()
  546. let viewS = self.tocOutlineView.view(atColumn: 0, row: self.tocOutlineView.clickedRow, makeIfNecessary: true)
  547. let targrtTextField = viewS?.subviews.first as? NSTextField
  548. self.renamePDFOutlineTextField = targrtTextField
  549. targrtTextField?.delegate = self
  550. targrtTextField?.isEditable = true
  551. targrtTextField?.becomeFirstResponder()
  552. }
  553. //降级节点
  554. @objc func outlineContextMenuItemClicked_Demote(_ sender: AnyObject?) {
  555. self.delegate?.controller?(controller: self, itemClick: sender, itemKey: .demote, params: nil)
  556. }
  557. //升级节点
  558. @objc func outlineContextMenuItemClicked_Promote(_ sender: AnyObject?) {
  559. self.delegate?.controller?(controller: self, itemClick: sender, itemKey: .promote, params: nil)
  560. }
  561. @objc func toggleOutlineCaseInsensitiveSearch(_ sender: NSMenuItem) {
  562. if (sender.state == .on) {
  563. self.outlineIgnoreCaseFlag = false
  564. } else {
  565. self.outlineIgnoreCaseFlag = true
  566. }
  567. if (self.outlineSearchField.stringValue.isEmpty == false) {
  568. self.tocOutlineView.reloadData()
  569. self.tocOutlineView.expandItem(nil, expandChildren: true)
  570. }
  571. }
  572. }
  573. // MARK: - KMTocOutlineViewDelegate
  574. extension KMLeftSideViewController: KMTocOutlineViewDelegate {
  575. func outlineView(_ anOutlineView: NSOutlineView, highlightLevelForRow row: Int) -> Int {
  576. if self.tocOutlineView.isEqual(to: anOutlineView) {
  577. let numRows = anOutlineView.numberOfRows
  578. if let outline = anOutlineView.item(atRow: row) as? CPDFOutline {
  579. let firstPage = outline.km_pageIndex
  580. var lastPage = self.pageCount()
  581. if row + 1 < numRows {
  582. lastPage = Int((anOutlineView.item(atRow: row + 1) as? CPDFOutline)?.km_pageIndex ?? UInt(NSNotFound))
  583. }
  584. // NSRange range = NSMakeRange(firstPage, MAX(1LU, lastPage - firstPage));
  585. // NSUInteger i, iMax = [lastViewedPages count];
  586. // for (i = 0; i < iMax; i++) {
  587. // if (NSLocationInRange((NSUInteger)[lastViewedPages pointerAtIndex:i], range))
  588. // return i;
  589. // }
  590. }
  591. }
  592. return NSNotFound
  593. }
  594. func outlineView(_ anOutlineView: NSOutlineView, imageContextForItem item: Any?) -> AnyObject? {
  595. if anOutlineView.isEqual(to: self.tocOutlineView) {
  596. if item == nil {
  597. return true as AnyObject
  598. }
  599. if item is CPDFOutline {
  600. return (item as! CPDFOutline).destination
  601. }
  602. }
  603. return nil
  604. }
  605. }
  606. extension KMLeftSideViewController: KMNoteOutlineViewDelegate {
  607. func outlineView(_ anOutlineView: NSOutlineView, canResizeRowByItem item: AnyObject?) -> Bool? {
  608. if anOutlineView.isEqual(to: self.noteOutlineView) {
  609. return true
  610. }
  611. return false
  612. }
  613. func outlineView(_ anOutlineView: NSOutlineView, setHeight newHeight: CGFloat, ofRowByItem item: AnyObject?) {
  614. // [rowHeights setFloat:newHeight forKey:item];
  615. }
  616. func outlineView(_ anOutlineView: NSOutlineView, didChangeHiddenOfTableColumn aTableColumn: NSTableColumn) {
  617. // if (mwcFlags.autoResizeNoteRows &&
  618. // [ov isEqual:rightSideController.noteOutlineView] &&
  619. // [[tableColumn identifier] isEqualToString:NOTE_COLUMNID]) {
  620. // [rowHeights removeAllFloats];
  621. // [rightSideController.noteOutlineView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [rightSideController.noteOutlineView numberOfRows])]];
  622. // }
  623. }
  624. func outlineViewCommandKeyPressedDuringNavigation(_ anOutlineView: NSOutlineView) {
  625. // PDFAnnotation *annotation = [[self selectedNotes] lastObject];
  626. // if (annotation) {
  627. // [pdfView scrollAnnotationToVisible:annotation];
  628. // [pdfView setActiveAnnotation:annotation];
  629. // }
  630. }
  631. }