KMLinkViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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, .maxYMargin]
  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.reloadData()
  176. } else if pdfLinkType == .Web {
  177. linkWebView.isHidden = false
  178. } else if pdfLinkType == .Email {
  179. linkEmailView.isHidden = false
  180. }
  181. }
  182. //MARK: - MouseEvent
  183. public override func mouseUp(with event: NSEvent) {
  184. super.mouseUp(with: event)
  185. }
  186. }
  187. //MARK: - ComponentTabsDelegate
  188. extension KMLinkViewController: ComponentTabsDelegate {
  189. public func componentTabsDidSelected(_ view: ComponentTabs, _ property: ComponentTabsProperty) {
  190. if let annotation = current_Annotation {
  191. if property == pageProperty {
  192. pdfLinkType = .Page
  193. annotation.setURL(nil)
  194. if let page = pdfView?.document.page(at: UInt(linkPageView.choosedIndex-1)) {
  195. let bounds = page.bounds(for: .cropBox)
  196. let destination = CPDFDestination(page: page, at: NSPoint(x: 0, y: bounds.size.height))
  197. annotation.setDestination(destination)
  198. }
  199. } else if property == webProperty {
  200. pdfLinkType = .Web
  201. annotation.setDestination(nil)
  202. let linkUrlPath = KMNTools.judgeWebURL(linkWebView.inputTextarea.properties.text)
  203. annotation.setURL(linkUrlPath)
  204. } else if property == emailProperty {
  205. pdfLinkType = .Email
  206. annotation.setDestination(nil)
  207. let linkUrlPath = KMNTools.judgeEmailURL(linkEmailView.inputTextarea.properties.text)
  208. annotation.setURL(linkUrlPath)
  209. }
  210. pdfLinkTypeChanged()
  211. }
  212. CPDFLinkAnnotation.updateDefault_AddLinkType(pdfLinkType.rawValue)
  213. delegate?.kmLinkViewControllerDidUpdateMode?(self)
  214. }
  215. }
  216. //MARK: - KMLinkPageViewDelegate
  217. extension KMLinkViewController: KMLinkPageViewDelegate {
  218. func kmLinkPageViewDidGoToPage(_ view: KMLinkPageView, _ pageIndex: Int) {
  219. if let _ = pdfView?.document.page(at: UInt(pageIndex-1)) {
  220. pdfView?.go(toPageIndex: pageIndex-1, animated: false)
  221. }
  222. }
  223. }
  224. //MARK: - KMLinkPageViewDelegate
  225. extension KMLinkViewController: KMLinkWebViewDelegate {
  226. func kmLinkWebViewDidGo(_ view: KMLinkWebView, _ webString: String) {
  227. guard let activeAnnotation = current_Annotation else {
  228. return
  229. }
  230. let linkUrlPath = KMNTools.judgeWebURL(webString)
  231. activeAnnotation.setURL(linkUrlPath)
  232. if let url = activeAnnotation.url() {
  233. if let data = URL(string: url) {
  234. NSWorkspace.shared.open(data)
  235. } else {
  236. let alert = NSAlert()
  237. alert.alertStyle = .critical
  238. alert.messageText = NSLocalizedString("Invalid URL. Please try again.", comment: "")
  239. alert.runModal()
  240. }
  241. } else {
  242. let alert = NSAlert()
  243. alert.alertStyle = .critical
  244. alert.messageText = NSLocalizedString("Invalid URL. Please try again.", comment: "")
  245. alert.runModal()
  246. }
  247. }
  248. }
  249. //MARK: - KMLinkEmailViewDelegate
  250. extension KMLinkViewController: KMLinkEmailViewDelegate {
  251. func kmLinkEmailViewDidGo(_ view: KMLinkEmailView, _ emailString: String) {
  252. guard let activeAnnotation = current_Annotation else {
  253. return
  254. }
  255. if !KMNTools.isValidateEmail(emailString) {
  256. let alert = NSAlert()
  257. alert.alertStyle = .critical
  258. alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "")
  259. alert.runModal()
  260. return
  261. }
  262. if let url = activeAnnotation.url() {
  263. NSWorkspace.shared.open(URL(string: url)!)
  264. }
  265. }
  266. }
  267. //MARK: - KMNAlignmentControllerDelegate
  268. extension KMLinkViewController: KMNAlignmentControllerDelegate {
  269. func alignmentControllerDidClick(_ controller: KMNAlignmentController, _ alignmentType: KMPDFActiveFormsAlignType) {
  270. pdfView?.change(annotations, alignmentType: alignmentType)
  271. pdfView?.setNeedsDisplayForVisiblePages()
  272. }
  273. }
  274. //MARK: - KMLinkViewController Class Method
  275. extension KMLinkViewController {
  276. }