Source code for the WriteFreely SwiftUI app for iOS, iPadOS, and macOS
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

95 рядки
3.7 KiB

  1. import SwiftUI
  2. struct PostTextEditingView: View {
  3. @Environment(\.horizontalSizeClass) var horizontalSizeClass
  4. @EnvironmentObject var model: WriteFreelyModel
  5. @ObservedObject var post: WFAPost
  6. @Binding var updatingTitleFromServer: Bool
  7. @Binding var updatingBodyFromServer: Bool
  8. @State private var appearance: PostAppearance = .serif
  9. @State private var titleTextStyle: UIFont = UIFont(name: "Lora-Regular", size: 26)!
  10. @State private var titleIsFirstResponder: Bool = true
  11. @State private var bodyTextStyle: UIFont = UIFont(name: "Lora-Regular", size: 17)!
  12. @State private var bodyIsFirstResponder: Bool = false
  13. private let lineSpacingMultiplier: CGFloat = 0.5
  14. private let textEditorHeight: CGFloat = 50
  15. init(
  16. post: ObservedObject<WFAPost>,
  17. updatingTitleFromServer: Binding<Bool>,
  18. updatingBodyFromServer: Binding<Bool>
  19. ) {
  20. self._post = post
  21. self._updatingTitleFromServer = updatingTitleFromServer
  22. self._updatingBodyFromServer = updatingBodyFromServer
  23. UITextView.appearance().backgroundColor = .clear
  24. }
  25. var body: some View {
  26. ScrollView(.vertical) {
  27. MultilineTextField(
  28. "Title (optional)",
  29. text: $post.title,
  30. font: titleTextStyle,
  31. isFirstResponder: $titleIsFirstResponder,
  32. onCommit: didFinishEditingTitle
  33. )
  34. .accessibilityLabel(Text("Title (optional)"))
  35. .accessibilityHint(Text("Add or edit the title for your post; use the Return key to skip to the body"))
  36. .onChange(of: post.title) { value in
  37. if post.status == PostStatus.published.rawValue && !updatingTitleFromServer {
  38. post.status = PostStatus.edited.rawValue
  39. }
  40. if updatingTitleFromServer {
  41. updatingTitleFromServer = false
  42. }
  43. if post.status == PostStatus.edited.rawValue && value == model.editor.initialPostTitle {
  44. post.status = PostStatus.published.rawValue
  45. }
  46. }
  47. MultilineTextField(
  48. "Write...",
  49. text: $post.body,
  50. font: bodyTextStyle,
  51. isFirstResponder: $bodyIsFirstResponder
  52. )
  53. .accessibilityLabel(Text("Body"))
  54. .accessibilityHint(Text("Add or edit the body of your post"))
  55. .onChange(of: post.body) { value in
  56. if post.status == PostStatus.published.rawValue && !updatingBodyFromServer {
  57. post.status = PostStatus.edited.rawValue
  58. }
  59. if updatingBodyFromServer {
  60. updatingBodyFromServer = false
  61. }
  62. if post.status == PostStatus.edited.rawValue && value == model.editor.initialPostBody {
  63. post.status = PostStatus.published.rawValue
  64. }
  65. }
  66. }
  67. .onChange(of: titleIsFirstResponder, perform: { value in
  68. self.bodyIsFirstResponder = !value
  69. })
  70. .onAppear(perform: {
  71. switch post.appearance {
  72. case "sans":
  73. self.appearance = .sans
  74. case "wrap", "mono", "code":
  75. self.appearance = .mono
  76. default:
  77. self.appearance = .serif
  78. }
  79. self.titleTextStyle = UIFont(name: appearance.rawValue, size: 26)!
  80. self.bodyTextStyle = UIFont(name: appearance.rawValue, size: 17)!
  81. })
  82. .onDisappear {
  83. hideKeyboard()
  84. }
  85. }
  86. private func didFinishEditingTitle() {
  87. self.titleIsFirstResponder = false
  88. self.bodyIsFirstResponder = true
  89. }
  90. }