NCRichWorkspaceCommon.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // NCRichWorkspaceCommon.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 17/01/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import NCCommunication
  25. import SwiftRichString
  26. @objc class NCRichWorkspaceCommon: NSObject {
  27. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  28. @objc func createViewerNextcloudText(serverUrl: String,viewController: UIViewController) {
  29. if !appDelegate.reachability.isReachable() {
  30. NCContentPresenter.shared.messageNotification("_error_", description: "_go_online_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: 0)
  31. return;
  32. }
  33. NCUtility.sharedInstance.startActivityIndicator(view: viewController.view, bottom: 0)
  34. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: k_fileNameRichWorkspace, serverUrl: serverUrl, activeUrl: appDelegate.activeUrl)!
  35. NCCommunication.sharedInstance.NCTextCreateFile(urlString: appDelegate.activeUrl, fileNamePath: fileNamePath, editor: "text", templateId: "", account: appDelegate.activeAccount) { (account, url, errorCode, errorMessage) in
  36. NCUtility.sharedInstance.stopActivityIndicator()
  37. if errorCode == 0 && account == self.appDelegate.activeAccount {
  38. if let viewerRichWorkspaceWebView = UIStoryboard.init(name: "NCViewerRichWorkspace", bundle: nil).instantiateViewController(withIdentifier: "NCViewerRichWorkspaceWebView") as? NCViewerRichWorkspaceWebView {
  39. viewerRichWorkspaceWebView.url = url!
  40. viewerRichWorkspaceWebView.presentationController?.delegate = viewController as? UIAdaptivePresentationControllerDelegate
  41. viewController.present(viewerRichWorkspaceWebView, animated: true, completion: nil)
  42. }
  43. } else if errorCode != 0 {
  44. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: errorCode)
  45. }
  46. }
  47. }
  48. @objc func openViewerNextcloudText(serverUrl: String, viewController: UIViewController) {
  49. if !appDelegate.reachability.isReachable() {
  50. NCContentPresenter.shared.messageNotification("_error_", description: "_go_online_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: 0)
  51. return;
  52. }
  53. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", appDelegate.activeAccount, serverUrl, k_fileNameRichWorkspace.lowercased())) {
  54. if metadata.url == "" {
  55. NCUtility.sharedInstance.startActivityIndicator(view: viewController.view, bottom: 0)
  56. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
  57. NCCommunication.sharedInstance.NCTextOpenFile(urlString: appDelegate.activeUrl, fileNamePath: fileNamePath, editor: "text", account: appDelegate.activeAccount) { (account, url, errorCode, errorMessage) in
  58. NCUtility.sharedInstance.stopActivityIndicator()
  59. if errorCode == 0 && account == self.appDelegate.activeAccount {
  60. if let viewerRichWorkspaceWebView = UIStoryboard.init(name: "NCViewerRichWorkspace", bundle: nil).instantiateViewController(withIdentifier: "NCViewerRichWorkspaceWebView") as? NCViewerRichWorkspaceWebView {
  61. viewerRichWorkspaceWebView.url = url!
  62. viewerRichWorkspaceWebView.metadata = metadata
  63. viewerRichWorkspaceWebView.presentationController?.delegate = viewController as? UIAdaptivePresentationControllerDelegate
  64. viewController.present(viewerRichWorkspaceWebView, animated: true, completion: nil)
  65. }
  66. } else if errorCode != 0 {
  67. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: errorCode)
  68. }
  69. }
  70. } else {
  71. if let viewerRichWorkspaceWebView = UIStoryboard.init(name: "NCViewerRichWorkspace", bundle: nil).instantiateViewController(withIdentifier: "NCViewerRichWorkspaceWebView") as? NCViewerRichWorkspaceWebView {
  72. viewerRichWorkspaceWebView.url = metadata.url
  73. viewerRichWorkspaceWebView.metadata = metadata
  74. viewerRichWorkspaceWebView.presentationController?.delegate = viewController as? UIAdaptivePresentationControllerDelegate
  75. viewController.present(viewerRichWorkspaceWebView, animated: true, completion: nil)
  76. }
  77. }
  78. }
  79. }
  80. @objc func setRichWorkspaceText(_ richWorkspaceText: String, textView: UITextView) {
  81. let h1 = Style {
  82. $0.font = UIFont.systemFont(ofSize: 25, weight: .bold)
  83. $0.color = NCBrandColor.sharedInstance.textView
  84. }
  85. let h2 = Style {
  86. $0.font = UIFont.systemFont(ofSize: 23, weight: .bold)
  87. $0.color = NCBrandColor.sharedInstance.textView
  88. }
  89. let h3 = Style {
  90. $0.font = UIFont.systemFont(ofSize: 21, weight: .bold)
  91. $0.color = NCBrandColor.sharedInstance.textView
  92. }
  93. let h4 = Style {
  94. $0.font = UIFont.systemFont(ofSize: 19, weight: .bold)
  95. $0.color = NCBrandColor.sharedInstance.textView
  96. }
  97. let h5 = Style {
  98. $0.font = UIFont.systemFont(ofSize: 17, weight: .bold)
  99. $0.color = NCBrandColor.sharedInstance.textView
  100. }
  101. let h6 = Style {
  102. $0.font = UIFont.systemFont(ofSize: 15, weight: .bold)
  103. $0.color = NCBrandColor.sharedInstance.textView
  104. }
  105. let normal = Style {
  106. $0.font = UIFont.systemFont(ofSize: 15)
  107. $0.color = NCBrandColor.sharedInstance.textView
  108. }
  109. var richWorkspaceStyling = ""
  110. let richWorkspaceArray = richWorkspaceText.components(separatedBy: "\n")
  111. for string in richWorkspaceArray {
  112. if string.hasPrefix("# ") {
  113. richWorkspaceStyling = richWorkspaceStyling + "<h1>" + string.replacingOccurrences(of: "# ", with: "") + "</h1>\r\n"
  114. } else if string.hasPrefix("## ") {
  115. richWorkspaceStyling = richWorkspaceStyling + "<h2>" + string.replacingOccurrences(of: "## ", with: "") + "</h2>\r\n"
  116. } else if string.hasPrefix("### ") {
  117. richWorkspaceStyling = richWorkspaceStyling + "<h3>" + string.replacingOccurrences(of: "### ", with: "") + "</h3>\r\n"
  118. } else if string.hasPrefix("#### ") {
  119. richWorkspaceStyling = richWorkspaceStyling + "<h4>" + string.replacingOccurrences(of: "#### ", with: "") + "</h4>\r\n"
  120. } else if string.hasPrefix("##### ") {
  121. richWorkspaceStyling = richWorkspaceStyling + "<h5>" + string.replacingOccurrences(of: "##### ", with: "") + "</h5>\r\n"
  122. } else if string.hasPrefix("###### ") {
  123. richWorkspaceStyling = richWorkspaceStyling + "<h6>" + string.replacingOccurrences(of: "###### ", with: "") + "</h6>\r\n"
  124. } else {
  125. richWorkspaceStyling = richWorkspaceStyling + string + "\r\n"
  126. }
  127. }
  128. textView.attributedText = richWorkspaceStyling.set(style: StyleGroup(base: normal, ["h1": h1, "h2": h2, "h3": h3, "h4": h4, "h5": h5, "h6": h6]))
  129. }
  130. }