KMLinkViewController.swift 9.8 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. @objcMembers class KMLinkViewController: NSViewController {
  22. @IBOutlet var contendBox: NSBox!
  23. @IBOutlet var infoContendView: NSView!
  24. @IBOutlet var tabsBGView: NSView!
  25. @IBOutlet var tabsView: ComponentTabs!
  26. @IBOutlet var typeContendView: NSView!
  27. @IBOutlet var linkPageView: KMLinkPageView!
  28. @IBOutlet var linkEmailView: KMLinkEmailView!
  29. @IBOutlet var linkWebView: KMLinkWebView!
  30. private var _annotations: [CPDFLinkAnnotation] = []
  31. private weak var _pdfView: CPDFListView? = nil
  32. private var annotation: CPDFLinkAnnotation? = nil
  33. var multiController: KMNLinkMultiController = KMNLinkMultiController.init() //注释多选界面
  34. let pageProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: NSLocalizedString("Page", comment: ""))
  35. let webProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: NSLocalizedString("Web", comment: ""))
  36. let emailProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: NSLocalizedString("Email", comment: ""))
  37. public override func viewDidLoad() {
  38. super.viewDidLoad()
  39. // Do view setup here.
  40. pdfLinkType = .Page
  41. setUpUI()
  42. }
  43. func setUpUI() {
  44. contendBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle")
  45. if pdfLinkType == .Page {
  46. pageProperty.state = .pressed
  47. } else if pdfLinkType == .Web {
  48. webProperty.state = .pressed
  49. } else if pdfLinkType == .Email {
  50. emailProperty.state = .pressed
  51. }
  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. }
  59. //MARK: - Setter
  60. var pdfLinkType: PDFLinkType = .Page {
  61. didSet {
  62. view.window?.makeFirstResponder(nil)
  63. reloadData()
  64. }
  65. }
  66. weak var pdfView: CPDFListView? {
  67. get {
  68. return _pdfView
  69. }
  70. set {
  71. _pdfView = newValue
  72. }
  73. }
  74. var annotations: [CPDFLinkAnnotation] {
  75. get {
  76. return _annotations
  77. }
  78. set {
  79. _annotations = newValue
  80. if _annotations.isEmpty == false {
  81. annotation = _annotations.first
  82. } else {
  83. annotation = nil
  84. }
  85. }
  86. }
  87. //MARK: - func
  88. public func reloadData() {
  89. infoContendView.isHidden = false
  90. if annotations.count == 0 {
  91. infoContendView.isHidden = true
  92. } else if annotations.count > 1 {
  93. multiController.view.frame = infoContendView.bounds
  94. multiController.view.autoresizingMask = [.width, .height]
  95. infoContendView.addSubview(multiController.view)
  96. multiController.view.isHidden = false
  97. tabsBGView.isHidden = true
  98. typeContendView.isHidden = true
  99. } else {
  100. multiController.view.isHidden = true
  101. tabsBGView.isHidden = false
  102. typeContendView.isHidden = false
  103. linkPageView.isHidden = true
  104. linkEmailView.isHidden = true
  105. linkWebView.isHidden = true
  106. if pdfLinkType == .Page {
  107. linkPageView.isHidden = false
  108. linkPageView.pdfView = pdfView
  109. linkPageView.annotation = annotation
  110. linkPageView.reloadData()
  111. } else if pdfLinkType == .Web {
  112. linkWebView.isHidden = false
  113. } else if pdfLinkType == .Email {
  114. linkEmailView.isHidden = false
  115. }
  116. }
  117. }
  118. //MARK: - private
  119. func judgeWebURL(_ urlString: String) -> String {
  120. var modifiedURLString = urlString
  121. modifiedURLString = modifiedURLString.replacingOccurrences(of: "\n", with: "")
  122. modifiedURLString = modifiedURLString.replacingOccurrences(of: " ", with: "")
  123. if !modifiedURLString.hasPrefix("http://") && !modifiedURLString.hasPrefix("https://") {
  124. if modifiedURLString.hasPrefix("smb://") {
  125. } else {
  126. modifiedURLString = "https://" + modifiedURLString
  127. }
  128. }
  129. if modifiedURLString.hasSuffix(".com") == false {
  130. modifiedURLString = modifiedURLString + ".com"
  131. }
  132. if modifiedURLString == "https://" {
  133. modifiedURLString = ""
  134. }
  135. return modifiedURLString
  136. }
  137. func judgeEmailURL(_ urlString: String) -> String {
  138. var modifiedURLString = urlString
  139. if !modifiedURLString.hasPrefix("mailto:") {
  140. modifiedURLString = "mailto:" + modifiedURLString
  141. }
  142. if modifiedURLString == "mailto:" {
  143. modifiedURLString = ""
  144. }
  145. return modifiedURLString
  146. }
  147. func isValidateEmail(_ email: String) -> Bool {
  148. let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
  149. let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex)
  150. return emailTest.evaluate(with: email)
  151. }
  152. //MARK: - MouseEvent
  153. public override func mouseUp(with event: NSEvent) {
  154. super.mouseUp(with: event)
  155. }
  156. }
  157. //MARK: - ComponentTabsDelegate
  158. extension KMLinkViewController: ComponentTabsDelegate {
  159. public func componentTabsDidSelected(_ view: ComponentTabs, _ property: ComponentTabsProperty) {
  160. if property == pageProperty {
  161. pdfLinkType = .Page
  162. } else if property == webProperty {
  163. pdfLinkType = .Web
  164. } else if property == emailProperty {
  165. pdfLinkType = .Email
  166. }
  167. DispatchQueue.main.async {
  168. self.reloadData()
  169. }
  170. }
  171. }
  172. //MARK: - KMLinkPageViewDelegate
  173. extension KMLinkViewController: KMLinkPageViewDelegate {
  174. func kmLinkPageViewDidGoToPage(_ view: KMLinkPageView, _ pageIndex: Int) {
  175. if let page = pdfView?.document.page(at: UInt(pageIndex-1)) {
  176. pdfView?.go(toPageIndex: pageIndex-1, animated: false)
  177. }
  178. }
  179. func kmLinkPageViewDidChangeDestination(_ view: KMLinkPageView, _ pageIndex: Int) {
  180. guard let activiteAnno = annotation else {
  181. return
  182. }
  183. activiteAnno.setURL(nil)
  184. if let page = pdfView?.document.page(at: UInt(pageIndex-1)) {
  185. let bounds = page.bounds(for: .cropBox)
  186. let destination = CPDFDestination(page: page, at: NSPoint(x: 0, y: bounds.size.height))
  187. activiteAnno.setDestination(destination)
  188. pdfView?.needsDisplay = true
  189. }
  190. if let page = pdfView?.document.page(at: UInt(pageIndex-1)) {
  191. pdfView?.go(toPageIndex: pageIndex-1, animated: false)
  192. }
  193. }
  194. }
  195. //MARK: - KMLinkPageViewDelegate
  196. extension KMLinkViewController: KMLinkWebViewDelegate {
  197. func kmLinkWebViewDidGo(_ view: KMLinkWebView, _ webString: String) {
  198. guard let activeAnnotation = annotation else {
  199. return
  200. }
  201. activeAnnotation.setDestination(nil)
  202. let linkUrlPath = judgeWebURL(webString)
  203. activeAnnotation.setURL(linkUrlPath)
  204. pdfView!.needsDisplay = true
  205. if let url = activeAnnotation.url() {
  206. if let data = URL(string: url) {
  207. NSWorkspace.shared.open(data)
  208. }
  209. }
  210. }
  211. }
  212. //MARK: - KMLinkPageViewDelegate
  213. extension KMLinkViewController: KMLinkEmailViewDelegate {
  214. func kmLinkEmailViewDidGo(_ view: KMLinkEmailView, _ emailString: String) {
  215. guard let activeAnnotation = annotation else {
  216. return
  217. }
  218. activeAnnotation.setDestination(nil)
  219. if !isValidateEmail(emailString) {
  220. let alert = NSAlert()
  221. alert.alertStyle = .critical
  222. alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "")
  223. alert.runModal()
  224. return
  225. }
  226. let linkUrlPath = judgeEmailURL(emailString)
  227. activeAnnotation.setURL(linkUrlPath)
  228. pdfView!.needsDisplay = true
  229. if let url = activeAnnotation.url() {
  230. NSWorkspace.shared.open(URL(string: url)!)
  231. }
  232. }
  233. }
  234. //MARK: - KMNLinkMultiControllerDelegate
  235. extension KMLinkViewController: KMNLinkMultiControllerDelegate {
  236. func multiControllerDidClick(_ controller: KMNLinkMultiController, _ alignmentType: KMAnnotationsAlignmentType) {
  237. var alignType: KMPDFActiveFormsAlignType = .left
  238. if alignmentType == .Left {
  239. alignType = .left
  240. } else if alignmentType == .XCenter {
  241. alignType = .horizontally
  242. } else if alignmentType == .Right {
  243. alignType = .right
  244. } else if alignmentType == .Top {
  245. alignType = .top
  246. } else if alignmentType == .YCenter {
  247. alignType = .vertical
  248. } else if alignmentType == .Bottom {
  249. alignType = .bottom
  250. }
  251. pdfView?.change(annotations, alignmentType: alignType)
  252. }
  253. }