KMLinkViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. // KMLinkViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/18.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc public enum PDFLinkType: Int, CaseIterable{
  10. case Page = 0 //
  11. case Web //
  12. case Email //
  13. }
  14. @objc public enum PDFLinkPopupType: Int, CaseIterable{
  15. case None = 0 //默认类型
  16. case Page //
  17. case Web //
  18. case Email //
  19. case MultiSelect //Popup弹窗
  20. }
  21. /**
  22. 未选中注释:修改默认添加类型。
  23. 选中注释:
  24. - 切换类型:判断类型->修改显示信息。
  25. - 输入文字:修改跳转内容。
  26. */
  27. //MARK: - KMLinkViewController
  28. @objcMembers class KMLinkViewController: NSViewController {
  29. @IBOutlet var contendBox: NSBox!
  30. @IBOutlet var infoContendView: NSView!
  31. @IBOutlet var tabsBGView: NSView!
  32. @IBOutlet var tabsView: ComponentTabs!
  33. @IBOutlet var typeContendView: NSView!
  34. @IBOutlet var linkPageView: KMLinkPageView!
  35. @IBOutlet var linkEmailView: KMLinkEmailView!
  36. @IBOutlet var linkWebView: KMLinkWebView!
  37. @IBOutlet var emptyContendView: ComponentEmpty!
  38. private weak var _pdfView: CPDFListView? = nil
  39. private var annotations: [CPDFLinkAnnotation] = []
  40. private var current_Annotation: CPDFLinkAnnotation? = nil
  41. var multiController: KMNAlignmentController = KMNAlignmentController.init() //注释多选界面
  42. let pageProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: NSLocalizedString("Page", comment: ""))
  43. let webProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: NSLocalizedString("Web", comment: ""))
  44. let emailProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: NSLocalizedString("Email", comment: ""))
  45. public override func viewDidLoad() {
  46. super.viewDidLoad()
  47. // Do view setup here.
  48. setUpUI()
  49. }
  50. func setUpUI() {
  51. contendBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle")
  52. tabsView.updateItemProperty([pageProperty, webProperty, emailProperty])
  53. tabsView.delegate = self
  54. linkPageView.delegate = self
  55. linkWebView.delegate = self
  56. linkEmailView.delegate = self
  57. multiController.delegate = self
  58. emptyContendView.properties = ComponentEmptyProperty(emptyType: .noLink,
  59. text: KMLocalizedString("Add Link"),
  60. subText: KMLocalizedString("Select an area or text on the page to add a link."))
  61. }
  62. //MARK: - Setter
  63. var pdfLinkType: PDFLinkType = .Page {
  64. didSet {
  65. view.window?.makeFirstResponder(nil)
  66. }
  67. }
  68. weak var pdfView: CPDFListView? {
  69. get {
  70. return _pdfView
  71. }
  72. set {
  73. _pdfView = newValue
  74. }
  75. }
  76. //MARK: - func
  77. public func reloadData() {
  78. guard let pdfView = self.pdfView else {
  79. return
  80. }
  81. typeContendView.isHidden = true
  82. emptyContendView.isHidden = true
  83. multiController.view.isHidden = true
  84. tabsBGView.isHidden = false
  85. let pdfAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  86. var linkAnnotations: [CPDFLinkAnnotation] = []
  87. for annotation in pdfAnnotations {
  88. if annotation is CPDFLinkAnnotation {
  89. linkAnnotations.append((annotation as! CPDFLinkAnnotation))
  90. }
  91. }
  92. annotations = linkAnnotations
  93. current_Annotation = nil
  94. if annotations.count == 0 {
  95. emptyContendView.isHidden = false
  96. linkWebView.clearData()
  97. linkEmailView.clearData()
  98. } else if annotations.count == 1 {
  99. current_Annotation = annotations.first
  100. multiController.view.isHidden = true
  101. typeContendView.isHidden = false
  102. linkPageView.isHidden = true
  103. linkEmailView.isHidden = true
  104. linkWebView.isHidden = true
  105. if let url = current_Annotation?.url() {
  106. if url.hasPrefix("mailto:") {
  107. pdfLinkType = .Email
  108. } else {
  109. pdfLinkType = .Web
  110. }
  111. } else if let _ = current_Annotation?.destination() {
  112. pdfLinkType = .Page
  113. }
  114. if pdfLinkType == .Page {
  115. linkPageView.isHidden = false
  116. linkPageView.pdfView = pdfView
  117. linkPageView.annotation = current_Annotation
  118. linkPageView.reloadData()
  119. } else if pdfLinkType == .Web {
  120. linkWebView.isHidden = false
  121. linkWebView.annotation = current_Annotation
  122. } else if pdfLinkType == .Email {
  123. linkEmailView.isHidden = false
  124. linkEmailView.annotation = current_Annotation
  125. }
  126. } else {
  127. multiController.view.frame = CGRectMake((CGRectGetWidth(infoContendView.frame) - 232)/2, CGRectGetHeight(infoContendView.frame)-112, 232, 112)
  128. multiController.view.autoresizingMask = [.width, .maxYMargin]
  129. infoContendView.addSubview(multiController.view)
  130. multiController.view.isHidden = false
  131. tabsBGView.isHidden = true
  132. if annotations.count > 2 {
  133. multiController.aligHorizontalXButton.properties.isDisabled = false
  134. multiController.aligVerticalYButton.properties.isDisabled = false
  135. } else {
  136. multiController.aligHorizontalXButton.properties.isDisabled = true
  137. multiController.aligVerticalYButton.properties.isDisabled = true
  138. }
  139. multiController.aligHorizontalXButton.reloadData()
  140. multiController.aligVerticalYButton.reloadData()
  141. }
  142. pageProperty.state = .normal
  143. webProperty.state = .normal
  144. emailProperty.state = .normal
  145. if pdfLinkType == .Page {
  146. pageProperty.state = .pressed
  147. } else if pdfLinkType == .Web {
  148. webProperty.state = .pressed
  149. } else if pdfLinkType == .Email {
  150. emailProperty.state = .pressed
  151. }
  152. tabsView.refreshItems()
  153. }
  154. func pdfLinkTypeChanged() {
  155. guard let _ = current_Annotation else {
  156. return
  157. }
  158. linkPageView.isHidden = true
  159. linkEmailView.isHidden = true
  160. linkWebView.isHidden = true
  161. if pdfLinkType == .Page {
  162. linkPageView.isHidden = false
  163. linkPageView.reloadData()
  164. } else if pdfLinkType == .Web {
  165. linkWebView.isHidden = false
  166. } else if pdfLinkType == .Email {
  167. linkEmailView.isHidden = false
  168. }
  169. }
  170. //MARK: - MouseEvent
  171. public override func mouseUp(with event: NSEvent) {
  172. super.mouseUp(with: event)
  173. }
  174. }
  175. //MARK: - ComponentTabsDelegate
  176. extension KMLinkViewController: ComponentTabsDelegate {
  177. public func componentTabsDidSelected(_ view: ComponentTabs, _ property: ComponentTabsProperty) {
  178. if let annotation = current_Annotation {
  179. if property == pageProperty {
  180. pdfLinkType = .Page
  181. annotation.setURL(nil)
  182. if let page = pdfView?.document.page(at: UInt(linkPageView.choosedIndex-1)) {
  183. let bounds = page.bounds(for: .cropBox)
  184. let destination = CPDFDestination(page: page, at: NSPoint(x: 0, y: bounds.size.height))
  185. annotation.setDestination(destination)
  186. }
  187. } else if property == webProperty {
  188. pdfLinkType = .Web
  189. annotation.setDestination(nil)
  190. let linkUrlPath = KMNTools.judgeWebURL(linkWebView.inputTextarea.properties.text)
  191. annotation.setURL(linkUrlPath)
  192. } else if property == emailProperty {
  193. pdfLinkType = .Email
  194. annotation.setDestination(nil)
  195. let linkUrlPath = KMNTools.judgeEmailURL(linkEmailView.inputTextarea.properties.text)
  196. annotation.setURL(linkUrlPath)
  197. }
  198. pdfLinkTypeChanged()
  199. }
  200. CPDFLinkAnnotation.updateDefault_AddLinkType(pdfLinkType.rawValue)
  201. }
  202. }
  203. //MARK: - KMLinkPageViewDelegate
  204. extension KMLinkViewController: KMLinkPageViewDelegate {
  205. func kmLinkPageViewDidGoToPage(_ view: KMLinkPageView, _ pageIndex: Int) {
  206. if let _ = pdfView?.document.page(at: UInt(pageIndex-1)) {
  207. pdfView?.go(toPageIndex: pageIndex-1, animated: false)
  208. }
  209. }
  210. }
  211. //MARK: - KMLinkPageViewDelegate
  212. extension KMLinkViewController: KMLinkWebViewDelegate {
  213. func kmLinkWebViewDidGo(_ view: KMLinkWebView, _ webString: String) {
  214. guard let activeAnnotation = current_Annotation else {
  215. return
  216. }
  217. if let url = activeAnnotation.url() {
  218. if let data = URL(string: url) {
  219. NSWorkspace.shared.open(data)
  220. }
  221. }
  222. }
  223. }
  224. //MARK: - KMLinkEmailViewDelegate
  225. extension KMLinkViewController: KMLinkEmailViewDelegate {
  226. func kmLinkEmailViewDidGo(_ view: KMLinkEmailView, _ emailString: String) {
  227. guard let activeAnnotation = current_Annotation else {
  228. return
  229. }
  230. if !KMNTools.isValidateEmail(emailString) {
  231. let alert = NSAlert()
  232. alert.alertStyle = .critical
  233. alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "")
  234. alert.runModal()
  235. return
  236. }
  237. if let url = activeAnnotation.url() {
  238. NSWorkspace.shared.open(URL(string: url)!)
  239. }
  240. }
  241. }
  242. //MARK: - KMNAlignmentControllerDelegate
  243. extension KMLinkViewController: KMNAlignmentControllerDelegate {
  244. func alignmentControllerDidClick(_ controller: KMNAlignmentController, _ alignmentType: KMPDFActiveFormsAlignType) {
  245. pdfView?.change(annotations, alignmentType: alignmentType)
  246. pdfView?.setNeedsDisplayForVisiblePages()
  247. }
  248. }
  249. //MARK: - KMLinkViewController Class Method
  250. extension KMLinkViewController {
  251. }