KMLinkViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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: KMNAlignmentController = KMNAlignmentController.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 = CGRectMake((CGRectGetWidth(infoContendView.frame) - 232)/2, CGRectGetHeight(infoContendView.frame)-112, 232, 112)
  94. multiController.view.autoresizingMask = [.width, .maxYMargin]
  95. infoContendView.addSubview(multiController.view)
  96. multiController.view.isHidden = false
  97. tabsBGView.isHidden = true
  98. typeContendView.isHidden = true
  99. if annotations.count > 2 {
  100. multiController.aligHorizontalXButton.properties.isDisabled = false
  101. multiController.aligVerticalYButton.properties.isDisabled = false
  102. } else {
  103. multiController.aligHorizontalXButton.properties.isDisabled = true
  104. multiController.aligVerticalYButton.properties.isDisabled = true
  105. }
  106. multiController.aligHorizontalXButton.reloadData()
  107. multiController.aligVerticalYButton.reloadData()
  108. } else {
  109. multiController.view.isHidden = true
  110. tabsBGView.isHidden = false
  111. typeContendView.isHidden = false
  112. linkPageView.isHidden = true
  113. linkEmailView.isHidden = true
  114. linkWebView.isHidden = true
  115. if pdfLinkType == .Page {
  116. linkPageView.isHidden = false
  117. linkPageView.pdfView = pdfView
  118. linkPageView.annotation = annotation
  119. linkPageView.reloadData()
  120. } else if pdfLinkType == .Web {
  121. linkWebView.isHidden = false
  122. } else if pdfLinkType == .Email {
  123. linkEmailView.isHidden = false
  124. }
  125. }
  126. }
  127. //MARK: - private
  128. func judgeWebURL(_ urlString: String) -> String {
  129. var modifiedURLString = urlString
  130. modifiedURLString = modifiedURLString.replacingOccurrences(of: "\n", with: "")
  131. modifiedURLString = modifiedURLString.replacingOccurrences(of: " ", with: "")
  132. if !modifiedURLString.hasPrefix("http://") && !modifiedURLString.hasPrefix("https://") {
  133. if modifiedURLString.hasPrefix("smb://") {
  134. } else {
  135. modifiedURLString = "https://" + modifiedURLString
  136. }
  137. }
  138. if modifiedURLString.hasSuffix(".com") == false {
  139. modifiedURLString = modifiedURLString + ".com"
  140. }
  141. if modifiedURLString == "https://" {
  142. modifiedURLString = ""
  143. }
  144. return modifiedURLString
  145. }
  146. func judgeEmailURL(_ urlString: String) -> String {
  147. var modifiedURLString = urlString
  148. if !modifiedURLString.hasPrefix("mailto:") {
  149. modifiedURLString = "mailto:" + modifiedURLString
  150. }
  151. if modifiedURLString == "mailto:" {
  152. modifiedURLString = ""
  153. }
  154. return modifiedURLString
  155. }
  156. func isValidateEmail(_ email: String) -> Bool {
  157. let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
  158. let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex)
  159. return emailTest.evaluate(with: email)
  160. }
  161. //MARK: - MouseEvent
  162. public override func mouseUp(with event: NSEvent) {
  163. super.mouseUp(with: event)
  164. }
  165. }
  166. //MARK: - ComponentTabsDelegate
  167. extension KMLinkViewController: ComponentTabsDelegate {
  168. public func componentTabsDidSelected(_ view: ComponentTabs, _ property: ComponentTabsProperty) {
  169. if property == pageProperty {
  170. pdfLinkType = .Page
  171. } else if property == webProperty {
  172. pdfLinkType = .Web
  173. } else if property == emailProperty {
  174. pdfLinkType = .Email
  175. }
  176. DispatchQueue.main.async {
  177. self.reloadData()
  178. }
  179. }
  180. }
  181. //MARK: - KMLinkPageViewDelegate
  182. extension KMLinkViewController: KMLinkPageViewDelegate {
  183. func kmLinkPageViewDidGoToPage(_ view: KMLinkPageView, _ pageIndex: Int) {
  184. if let page = pdfView?.document.page(at: UInt(pageIndex-1)) {
  185. pdfView?.go(toPageIndex: pageIndex-1, animated: false)
  186. }
  187. }
  188. func kmLinkPageViewDidChangeDestination(_ view: KMLinkPageView, _ pageIndex: Int) {
  189. guard let activiteAnno = annotation else {
  190. return
  191. }
  192. activiteAnno.setURL(nil)
  193. if let page = pdfView?.document.page(at: UInt(pageIndex-1)) {
  194. let bounds = page.bounds(for: .cropBox)
  195. let destination = CPDFDestination(page: page, at: NSPoint(x: 0, y: bounds.size.height))
  196. activiteAnno.setDestination(destination)
  197. pdfView?.needsDisplay = true
  198. }
  199. if let page = pdfView?.document.page(at: UInt(pageIndex-1)) {
  200. pdfView?.go(toPageIndex: pageIndex-1, animated: false)
  201. }
  202. }
  203. }
  204. //MARK: - KMLinkPageViewDelegate
  205. extension KMLinkViewController: KMLinkWebViewDelegate {
  206. func kmLinkWebViewDidGo(_ view: KMLinkWebView, _ webString: String) {
  207. guard let activeAnnotation = annotation else {
  208. return
  209. }
  210. activeAnnotation.setDestination(nil)
  211. let linkUrlPath = judgeWebURL(webString)
  212. activeAnnotation.setURL(linkUrlPath)
  213. pdfView!.needsDisplay = true
  214. if let url = activeAnnotation.url() {
  215. if let data = URL(string: url) {
  216. NSWorkspace.shared.open(data)
  217. }
  218. }
  219. }
  220. }
  221. //MARK: - KMLinkPageViewDelegate
  222. extension KMLinkViewController: KMLinkEmailViewDelegate {
  223. func kmLinkEmailViewDidGo(_ view: KMLinkEmailView, _ emailString: String) {
  224. guard let activeAnnotation = annotation else {
  225. return
  226. }
  227. activeAnnotation.setDestination(nil)
  228. if !isValidateEmail(emailString) {
  229. let alert = NSAlert()
  230. alert.alertStyle = .critical
  231. alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "")
  232. alert.runModal()
  233. return
  234. }
  235. let linkUrlPath = judgeEmailURL(emailString)
  236. activeAnnotation.setURL(linkUrlPath)
  237. pdfView!.needsDisplay = true
  238. if let url = activeAnnotation.url() {
  239. NSWorkspace.shared.open(URL(string: url)!)
  240. }
  241. }
  242. }
  243. //MARK: - KMNAlignmentControllerDelegate
  244. extension KMLinkViewController: KMNAlignmentControllerDelegate {
  245. func alignmentControllerDidClick(_ controller: KMNAlignmentController, _ alignmentType: KMPDFActiveFormsAlignType) {
  246. pdfView?.change(annotations, alignmentType: alignmentType)
  247. pdfView?.setNeedsDisplayForVisiblePages()
  248. }
  249. }