KMLinkViewController.swift 10 KB

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