KMLinkViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. @objc protocol KMLinkViewControllerDelegate: AnyObject {
  28. @objc optional func kmLinkViewControllerDidUpdateMode(_ controller: KMLinkViewController)
  29. }
  30. //MARK: - KMLinkViewController
  31. @objcMembers class KMLinkViewController: NSViewController {
  32. @IBOutlet var contendBox: NSBox!
  33. @IBOutlet var infoContendView: NSView!
  34. @IBOutlet var tabsBGView: NSView!
  35. @IBOutlet var tabsView: ComponentTabs!
  36. @IBOutlet var typeContendView: NSView!
  37. @IBOutlet var linkPageView: KMLinkPageView!
  38. @IBOutlet var linkEmailView: KMLinkEmailView!
  39. @IBOutlet var linkWebView: KMLinkWebView!
  40. @IBOutlet var emptyContendView: ComponentEmpty!
  41. private weak var _pdfView: CPDFListView? = nil
  42. private var annotations: [CPDFLinkAnnotation] = []
  43. private var current_Annotation: CPDFLinkAnnotation? = nil
  44. var multiController: KMNAlignmentController = KMNAlignmentController.init() //注释多选界面
  45. let pageProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: NSLocalizedString("Page", comment: ""))
  46. let webProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: NSLocalizedString("Web", comment: ""))
  47. let emailProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: NSLocalizedString("Email", comment: ""))
  48. weak open var delegate: KMLinkViewControllerDelegate?
  49. public override func viewDidLoad() {
  50. super.viewDidLoad()
  51. // Do view setup here.
  52. setUpUI()
  53. }
  54. func setUpUI() {
  55. contendBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle")
  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. emptyContendView.properties = ComponentEmptyProperty(emptyType: .noLink,
  63. text: KMLocalizedString("Add Link"),
  64. subText: KMLocalizedString("Select an area or text on the page to add a link."))
  65. }
  66. //MARK: - Setter
  67. var pdfLinkType: PDFLinkType = .Page {
  68. didSet {
  69. view.window?.makeFirstResponder(nil)
  70. }
  71. }
  72. weak var pdfView: CPDFListView? {
  73. get {
  74. return _pdfView
  75. }
  76. set {
  77. _pdfView = newValue
  78. }
  79. }
  80. //MARK: - func
  81. public func reloadData() {
  82. guard let pdfView = self.pdfView else {
  83. return
  84. }
  85. typeContendView.isHidden = true
  86. emptyContendView.isHidden = true
  87. multiController.view.isHidden = true
  88. tabsBGView.isHidden = false
  89. let pdfAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  90. var linkAnnotations: [CPDFLinkAnnotation] = []
  91. for annotation in pdfAnnotations {
  92. if annotation is CPDFLinkAnnotation {
  93. linkAnnotations.append((annotation as! CPDFLinkAnnotation))
  94. }
  95. }
  96. annotations = linkAnnotations
  97. if annotations.count == 0 {
  98. emptyContendView.isHidden = false
  99. current_Annotation = nil
  100. linkPageView.clearData()
  101. linkWebView.clearData()
  102. linkEmailView.clearData()
  103. } else if annotations.count == 1 {
  104. if let annotation = annotations.first {
  105. if current_Annotation != annotation {
  106. current_Annotation = annotation
  107. linkPageView.clearData()
  108. linkWebView.clearData()
  109. linkEmailView.clearData()
  110. }
  111. }
  112. multiController.view.isHidden = true
  113. typeContendView.isHidden = false
  114. linkPageView.isHidden = true
  115. linkEmailView.isHidden = true
  116. linkWebView.isHidden = true
  117. if let url = current_Annotation?.url() {
  118. if url.hasPrefix("mailto:") {
  119. pdfLinkType = .Email
  120. } else {
  121. pdfLinkType = .Web
  122. }
  123. } else if let _ = current_Annotation?.destination() {
  124. pdfLinkType = .Page
  125. }
  126. if pdfLinkType == .Page {
  127. linkPageView.isHidden = false
  128. linkPageView.pdfView = pdfView
  129. linkPageView.annotation = current_Annotation
  130. linkPageView.reloadData()
  131. } else if pdfLinkType == .Web {
  132. linkWebView.isHidden = false
  133. linkWebView.annotation = current_Annotation
  134. } else if pdfLinkType == .Email {
  135. linkEmailView.isHidden = false
  136. linkEmailView.annotation = current_Annotation
  137. }
  138. } else {
  139. multiController.view.frame = CGRectMake((CGRectGetWidth(infoContendView.frame) - 232)/2, CGRectGetHeight(infoContendView.frame)-112, 232, 112)
  140. multiController.view.autoresizingMask = [.width, .minYMargin]
  141. infoContendView.addSubview(multiController.view)
  142. multiController.view.isHidden = false
  143. tabsBGView.isHidden = true
  144. if annotations.count > 2 {
  145. multiController.aligHorizontalXButton.properties.isDisabled = false
  146. multiController.aligVerticalYButton.properties.isDisabled = false
  147. } else {
  148. multiController.aligHorizontalXButton.properties.isDisabled = true
  149. multiController.aligVerticalYButton.properties.isDisabled = true
  150. }
  151. multiController.aligHorizontalXButton.reloadData()
  152. multiController.aligVerticalYButton.reloadData()
  153. }
  154. pageProperty.state = .normal
  155. webProperty.state = .normal
  156. emailProperty.state = .normal
  157. if pdfLinkType == .Page {
  158. pageProperty.state = .pressed
  159. } else if pdfLinkType == .Web {
  160. webProperty.state = .pressed
  161. } else if pdfLinkType == .Email {
  162. emailProperty.state = .pressed
  163. }
  164. tabsView.refreshItems()
  165. }
  166. func pdfLinkTypeChanged() {
  167. guard let _ = current_Annotation else {
  168. return
  169. }
  170. linkPageView.isHidden = true
  171. linkEmailView.isHidden = true
  172. linkWebView.isHidden = true
  173. if pdfLinkType == .Page {
  174. linkPageView.isHidden = false
  175. linkPageView.pdfView = pdfView
  176. linkPageView.annotation = current_Annotation
  177. linkPageView.reloadData()
  178. } else if pdfLinkType == .Web {
  179. linkWebView.isHidden = false
  180. linkWebView.annotation = current_Annotation
  181. } else if pdfLinkType == .Email {
  182. linkEmailView.isHidden = false
  183. linkEmailView.annotation = current_Annotation
  184. }
  185. }
  186. //MARK: - MouseEvent
  187. public override func mouseUp(with event: NSEvent) {
  188. super.mouseUp(with: event)
  189. }
  190. }
  191. //MARK: - ComponentTabsDelegate
  192. extension KMLinkViewController: ComponentTabsDelegate {
  193. public func componentTabsDidSelected(_ view: ComponentTabs, _ property: ComponentTabsProperty) {
  194. if let annotation = current_Annotation {
  195. if property == pageProperty {
  196. pdfLinkType = .Page
  197. annotation.setURL(nil)
  198. if linkPageView.choosedIndex > 0, let page = pdfView?.document.page(at: UInt(linkPageView.choosedIndex-1)) {
  199. let bounds = page.bounds(for: .cropBox)
  200. let destination = CPDFDestination(page: page, at: NSPoint(x: 0, y: bounds.size.height))
  201. annotation.setDestination(destination)
  202. }
  203. } else if property == webProperty {
  204. pdfLinkType = .Web
  205. annotation.setDestination(nil)
  206. let linkUrlPath = KMNTools.judgeWebURL(linkWebView.inputTextarea.properties.text)
  207. annotation.setURL(linkUrlPath)
  208. } else if property == emailProperty {
  209. pdfLinkType = .Email
  210. annotation.setDestination(nil)
  211. let linkUrlPath = KMNTools.judgeEmailURL(linkEmailView.inputTextarea.properties.text)
  212. annotation.setURL(linkUrlPath)
  213. }
  214. pdfLinkTypeChanged()
  215. }
  216. CPDFLinkAnnotation.updateDefault_AddLinkType(pdfLinkType.rawValue)
  217. delegate?.kmLinkViewControllerDidUpdateMode?(self)
  218. }
  219. }
  220. //MARK: - KMLinkPageViewDelegate
  221. extension KMLinkViewController: KMLinkPageViewDelegate {
  222. func kmLinkPageViewDidGoToPage(_ view: KMLinkPageView, _ pageIndex: Int) {
  223. if let _ = pdfView?.document.page(at: UInt(pageIndex-1)) {
  224. pdfView?.go(toPageIndex: pageIndex-1, animated: false)
  225. }
  226. }
  227. }
  228. //MARK: - KMLinkPageViewDelegate
  229. extension KMLinkViewController: KMLinkWebViewDelegate {
  230. func kmLinkWebViewDidGo(_ view: KMLinkWebView, _ webString: String) {
  231. guard let activeAnnotation = current_Annotation else {
  232. return
  233. }
  234. let linkUrlPath = KMNTools.judgeWebURL(webString)
  235. activeAnnotation.setURL(linkUrlPath)
  236. if let url = activeAnnotation.url() {
  237. if let data = URL(string: url) {
  238. NSWorkspace.shared.open(data)
  239. } else {
  240. let alert = NSAlert()
  241. alert.alertStyle = .critical
  242. alert.messageText = NSLocalizedString("Invalid URL. Please try again.", comment: "")
  243. alert.runModal()
  244. }
  245. } else {
  246. let alert = NSAlert()
  247. alert.alertStyle = .critical
  248. alert.messageText = NSLocalizedString("Invalid URL. Please try again.", comment: "")
  249. alert.runModal()
  250. }
  251. }
  252. }
  253. //MARK: - KMLinkEmailViewDelegate
  254. extension KMLinkViewController: KMLinkEmailViewDelegate {
  255. func kmLinkEmailViewDidGo(_ view: KMLinkEmailView, _ emailString: String) {
  256. guard let activeAnnotation = current_Annotation else {
  257. return
  258. }
  259. if !KMNTools.isValidateEmail(emailString) {
  260. let alert = NSAlert()
  261. alert.alertStyle = .critical
  262. alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "")
  263. alert.runModal()
  264. return
  265. }
  266. if let url = activeAnnotation.url() {
  267. NSWorkspace.shared.open(URL(string: url)!)
  268. }
  269. }
  270. }
  271. //MARK: - KMNAlignmentControllerDelegate
  272. extension KMLinkViewController: KMNAlignmentControllerDelegate {
  273. func alignmentControllerDidClick(_ controller: KMNAlignmentController, _ alignmentType: KMPDFActiveFormsAlignType) {
  274. pdfView?.change(annotations, alignmentType: alignmentType)
  275. pdfView?.setNeedsDisplayForVisiblePages()
  276. }
  277. }
  278. //MARK: - KMLinkViewController Class Method
  279. extension KMLinkViewController {
  280. }