From a542afa405b9a39628e3571efd2c3265a18f9734 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Fri, 23 Oct 2020 16:01:37 -0400 Subject: [PATCH] Refactor text-editing views out of PostEditorView [Mac] --- Shared/PostEditor/PostEditorModel.swift | 6 + .../project.pbxproj | 4 + iOS/PostEditor/PostTextEditingView.swift | 12 +- macOS/PostEditor/PostEditorView.swift | 129 +----------------- macOS/PostEditor/PostTextEditingView.swift | 66 +++++++++ 5 files changed, 87 insertions(+), 130 deletions(-) create mode 100644 macOS/PostEditor/PostTextEditingView.swift diff --git a/Shared/PostEditor/PostEditorModel.swift b/Shared/PostEditor/PostEditorModel.swift index d080927..ca8959f 100644 --- a/Shared/PostEditor/PostEditorModel.swift +++ b/Shared/PostEditor/PostEditorModel.swift @@ -1,6 +1,12 @@ import Foundation import CoreData +enum PostAppearance: String { + case sans = "OpenSans-Regular" + case mono = "Hack" + case serif = "Lora" +} + struct PostEditorModel { let lastDraftObjectURLKey = "lastDraftObjectURLKey" private(set) var lastDraft: WFAPost? diff --git a/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj b/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj index 846e2fd..4bc5d99 100644 --- a/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj +++ b/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj @@ -91,6 +91,7 @@ 17DFDE8A251D309400A25F31 /* Lora-Cyrillic-OFL.txt in Resources */ = {isa = PBXBuildFile; fileRef = 17DFDE85251D309400A25F31 /* Lora-Cyrillic-OFL.txt */; }; 17DFDE8B251D309400A25F31 /* OpenSans-License.txt in Resources */ = {isa = PBXBuildFile; fileRef = 17DFDE86251D309400A25F31 /* OpenSans-License.txt */; }; 17DFDE8C251D309400A25F31 /* OpenSans-License.txt in Resources */ = {isa = PBXBuildFile; fileRef = 17DFDE86251D309400A25F31 /* OpenSans-License.txt */; }; + 17E5DF8A2543610700DCDC9B /* PostTextEditingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17E5DF892543610700DCDC9B /* PostTextEditingView.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -174,6 +175,7 @@ 17DFDE84251D309400A25F31 /* Hack-License.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "Hack-License.txt"; sourceTree = ""; }; 17DFDE85251D309400A25F31 /* Lora-Cyrillic-OFL.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "Lora-Cyrillic-OFL.txt"; sourceTree = ""; }; 17DFDE86251D309400A25F31 /* OpenSans-License.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "OpenSans-License.txt"; sourceTree = ""; }; + 17E5DF892543610700DCDC9B /* PostTextEditingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostTextEditingView.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -300,6 +302,7 @@ isa = PBXGroup; children = ( 17A67CAE251A5DD7002F163D /* PostEditorView.swift */, + 17E5DF892543610700DCDC9B /* PostTextEditingView.swift */, ); path = PostEditor; sourceTree = ""; @@ -733,6 +736,7 @@ 17480CA6251272EE00EB7765 /* Bundle+AppVersion.swift in Sources */, 17C42E662509237800072984 /* PostListFilteredView.swift in Sources */, 17120DAD24E1B99F002B9F6C /* AccountLoginView.swift in Sources */, + 17E5DF8A2543610700DCDC9B /* PostTextEditingView.swift in Sources */, 17C42E71250AAFD500072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift in Sources */, 1756AE7B24CB65DF00FD7257 /* PostListView.swift in Sources */, 1753F6AC24E431CC00309365 /* MacPreferencesView.swift in Sources */, diff --git a/iOS/PostEditor/PostTextEditingView.swift b/iOS/PostEditor/PostTextEditingView.swift index 94a519a..c24530e 100644 --- a/iOS/PostEditor/PostTextEditingView.swift +++ b/iOS/PostEditor/PostTextEditingView.swift @@ -1,11 +1,5 @@ import SwiftUI -enum PostAppearance: String { - case sans = "OpenSans-Regular" - case mono = "Hack" - case serif = "Lora" -} - struct PostTextEditingView: View { @Environment(\.horizontalSizeClass) var horizontalSizeClass @ObservedObject var post: WFAPost @@ -45,7 +39,11 @@ struct PostTextEditingView: View { } TextEditor(text: $post.body) .font(.custom(appearance.rawValue, size: 17, relativeTo: .body)) - .lineSpacing(17 * (horizontalSizeClass == .compact ? 0.25 : bodyLineSpacingMultiplier)) + .lineSpacing( + 17 * ( + horizontalSizeClass == .compact ? bodyLineSpacingMultiplier / 2 : bodyLineSpacingMultiplier + ) + ) .onChange(of: post.body) { _ in if post.status == PostStatus.published.rawValue && !updatingBodyFromServer { post.status = PostStatus.edited.rawValue diff --git a/macOS/PostEditor/PostEditorView.swift b/macOS/PostEditor/PostEditorView.swift index 4e4b01e..ba7cb53 100644 --- a/macOS/PostEditor/PostEditorView.swift +++ b/macOS/PostEditor/PostEditorView.swift @@ -10,129 +10,12 @@ struct PostEditorView: View { @State private var updatingBodyFromServer: Bool = false var body: some View { - VStack { - switch post.appearance { - case "sans": - TextField("Title (optional)", text: $post.title) - .textFieldStyle(PlainTextFieldStyle()) - .padding(.horizontal, 4) - .padding(.bottom) - .font(.custom("OpenSans-Regular", size: 26, relativeTo: Font.TextStyle.largeTitle)) - .onChange(of: post.title) { _ in - if post.status == PostStatus.published.rawValue && !updatingTitleFromServer { - post.status = PostStatus.edited.rawValue - } - if updatingTitleFromServer { - updatingTitleFromServer = false - } - } - ZStack(alignment: .topLeading) { - if post.body.count == 0 { - Text("Write...") - .foregroundColor(Color(NSColor.placeholderTextColor)) - .padding(.horizontal, 4) - .padding(.vertical, 2) - .font(.custom("OpenSans-Regular", size: 17, relativeTo: Font.TextStyle.body)) - } - TextEditor(text: $post.body) - .font(.custom("OpenSans-Regular", size: 17, relativeTo: Font.TextStyle.body)) - .lineSpacing(bodyLineSpacing) - .opacity(post.body.count == 0 && !isHovering ? 0.0 : 1.0) - .onChange(of: post.body) { _ in - if post.status == PostStatus.published.rawValue && !updatingBodyFromServer { - post.status = PostStatus.edited.rawValue - } - if updatingBodyFromServer { - updatingBodyFromServer = false - } - } - .onHover(perform: { hovering in - self.isHovering = hovering - }) - } - .background(Color(NSColor.controlBackgroundColor)) - case "wrap", "mono", "code": - TextField("Title (optional)", text: $post.title) - .textFieldStyle(PlainTextFieldStyle()) - .padding(.horizontal, 4) - .padding(.bottom) - .font(.custom("Hack", size: 26, relativeTo: Font.TextStyle.largeTitle)) - .onChange(of: post.title) { _ in - if post.status == PostStatus.published.rawValue && !updatingTitleFromServer { - post.status = PostStatus.edited.rawValue - } - if updatingTitleFromServer { - updatingTitleFromServer = false - } - } - ZStack(alignment: .topLeading) { - if post.body.count == 0 { - Text("Write...") - .foregroundColor(Color(NSColor.placeholderTextColor)) - .padding(.horizontal, 4) - .padding(.vertical, 2) - .font(.custom("Hack", size: 17, relativeTo: Font.TextStyle.body)) - } - TextEditor(text: $post.body) - .font(.custom("Hack", size: 17, relativeTo: Font.TextStyle.body)) - .lineSpacing(bodyLineSpacing) - .opacity(post.body.count == 0 && !isHovering ? 0.0 : 1.0) - .onChange(of: post.body) { _ in - if post.status == PostStatus.published.rawValue && !updatingBodyFromServer { - post.status = PostStatus.edited.rawValue - } - if updatingBodyFromServer { - updatingBodyFromServer = false - } - } - .onHover(perform: { hovering in - self.isHovering = hovering - }) - } - .background(Color(NSColor.controlBackgroundColor)) - default: - TextField("Title (optional)", text: $post.title) - .textFieldStyle(PlainTextFieldStyle()) - .padding(.horizontal, 4) - .padding(.bottom) - .font(.custom("Lora", size: 26, relativeTo: Font.TextStyle.largeTitle)) - .onChange(of: post.title) { _ in - if post.status == PostStatus.published.rawValue && !updatingTitleFromServer { - post.status = PostStatus.edited.rawValue - } - if updatingTitleFromServer { - updatingTitleFromServer = false - } - } - ZStack(alignment: .topLeading) { - if post.body.count == 0 { - Text("Write...") - .foregroundColor(Color(NSColor.placeholderTextColor)) - .padding(.horizontal, 4) - .padding(.vertical, 2) - .font(.custom("Lora", size: 17, relativeTo: Font.TextStyle.body)) - } - TextEditor(text: $post.body) - .font(.custom("Lora", size: 17, relativeTo: Font.TextStyle.body)) - .lineSpacing(bodyLineSpacing) - .opacity(post.body.count == 0 && !isHovering ? 0.0 : 1.0) - .onChange(of: post.body) { _ in - if post.status == PostStatus.published.rawValue && !updatingBodyFromServer { - post.status = PostStatus.edited.rawValue - } - if updatingBodyFromServer { - updatingBodyFromServer = false - } - } - .onHover(perform: { hovering in - self.isHovering = hovering - }) - } - .background(Color(NSColor.controlBackgroundColor)) - } - } + PostTextEditingView( + post: post, + updatingTitleFromServer: $updatingTitleFromServer, + updatingBodyFromServer: $updatingBodyFromServer + ) .padding() - .background(Color.white) .toolbar { ToolbarItem(placement: .status) { PostEditorStatusToolbarView(post: post) @@ -150,7 +33,7 @@ struct PostEditorView: View { }, label: { Image(systemName: "paperplane") }) - .disabled(post.status == PostStatus.published.rawValue || || post.body.count == 0) + .disabled(post.status == PostStatus.published.rawValue || post.body.count == 0) } } .onChange(of: post.hasNewerRemoteCopy, perform: { _ in diff --git a/macOS/PostEditor/PostTextEditingView.swift b/macOS/PostEditor/PostTextEditingView.swift new file mode 100644 index 0000000..512b673 --- /dev/null +++ b/macOS/PostEditor/PostTextEditingView.swift @@ -0,0 +1,66 @@ +import SwiftUI + +struct PostTextEditingView: View { + @ObservedObject var post: WFAPost + @Binding var updatingTitleFromServer: Bool + @Binding var updatingBodyFromServer: Bool + @State private var isHovering: Bool = false + @State private var appearance: PostAppearance = .serif + private let bodyLineSpacingMultiplier: CGFloat = 0.5 + + var body: some View { + VStack { + TextField("Title (optional)", text: $post.title) + .textFieldStyle(PlainTextFieldStyle()) + .padding(.horizontal, 4) + .font(.custom(appearance.rawValue, size: 26, relativeTo: .largeTitle)) + .onChange(of: post.title) { _ in + if post.status == PostStatus.published.rawValue && !updatingTitleFromServer { + post.status = PostStatus.edited.rawValue + } + if updatingTitleFromServer { + updatingTitleFromServer = false + } + } + .padding(4) + .background(Color(NSColor.controlBackgroundColor)) + .padding(.bottom) + ZStack(alignment: .topLeading) { + if post.body.count == 0 { + Text("Write…") + .foregroundColor(Color(NSColor.placeholderTextColor)) + .padding(.horizontal, 4) + .padding(.vertical, 2) + .font(.custom(appearance.rawValue, size: 17, relativeTo: .body)) + } + TextEditor(text: $post.body) + .font(.custom(appearance.rawValue, size: 17, relativeTo: .body)) + .lineSpacing(17 * bodyLineSpacingMultiplier) + .opacity(post.body.count == 0 && !isHovering ? 0.0 : 1.0) + .onChange(of: post.body) { _ in + if post.status == PostStatus.published.rawValue && !updatingBodyFromServer { + post.status = PostStatus.edited.rawValue + } + if updatingBodyFromServer { + updatingBodyFromServer = false + } + } + .onHover(perform: { hovering in + self.isHovering = hovering + }) + } + .padding(4) + .background(Color(NSColor.controlBackgroundColor)) + } + .onAppear(perform: { + switch post.appearance { + case "sans": + self.appearance = .sans + case "wrap", "mono", "code": + self.appearance = .mono + default: + self.appearance = .serif + } + }) + } +}