2020-10-23 20:01:37 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct PostTextEditingView: View {
|
2023-07-23 11:19:52 +00:00
|
|
|
@EnvironmentObject var model: WriteFreelyModel
|
2020-10-23 20:01:37 +00:00
|
|
|
@ObservedObject var post: WFAPost
|
2020-11-23 19:13:15 +00:00
|
|
|
@Binding var updatingFromServer: Bool
|
2020-10-23 20:01:37 +00:00
|
|
|
@State private var appearance: PostAppearance = .serif
|
2020-11-23 16:18:03 +00:00
|
|
|
@State private var combinedText = ""
|
2021-02-10 20:16:34 +00:00
|
|
|
@State private var hasBeenEdited: Bool = false
|
|
|
|
|
|
|
|
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
|
2020-10-23 20:01:37 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2020-11-23 16:59:01 +00:00
|
|
|
ZStack(alignment: .topLeading) {
|
|
|
|
if combinedText.count == 0 {
|
|
|
|
Text("Write…")
|
|
|
|
.foregroundColor(Color(NSColor.placeholderTextColor))
|
2023-10-23 21:15:41 +00:00
|
|
|
.padding(.horizontal, 16)
|
|
|
|
.padding(.vertical, 16)
|
2020-11-23 16:59:01 +00:00
|
|
|
.font(.custom(appearance.rawValue, size: 17, relativeTo: .body))
|
|
|
|
}
|
2020-11-23 19:13:15 +00:00
|
|
|
if post.appearance == "sans" {
|
|
|
|
MacEditorTextView(
|
|
|
|
text: $combinedText,
|
|
|
|
isFirstResponder: combinedText.isEmpty,
|
|
|
|
isEditable: true,
|
2020-11-23 19:58:51 +00:00
|
|
|
font: NSFont(name: PostAppearance.sans.rawValue, size: 17),
|
2020-11-23 19:13:15 +00:00
|
|
|
onEditingChanged: onEditingChanged,
|
|
|
|
onCommit: onCommit,
|
|
|
|
onTextChange: onTextChange
|
|
|
|
)
|
|
|
|
} else if post.appearance == "wrap" || post.appearance == "mono" || post.appearance == "code" {
|
|
|
|
MacEditorTextView(
|
|
|
|
text: $combinedText,
|
|
|
|
isFirstResponder: combinedText.isEmpty,
|
|
|
|
isEditable: true,
|
2020-11-23 19:58:51 +00:00
|
|
|
font: NSFont(name: PostAppearance.mono.rawValue, size: 17),
|
2020-11-23 19:13:15 +00:00
|
|
|
onEditingChanged: onEditingChanged,
|
|
|
|
onCommit: onCommit,
|
|
|
|
onTextChange: onTextChange
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
MacEditorTextView(
|
|
|
|
text: $combinedText,
|
|
|
|
isFirstResponder: combinedText.isEmpty,
|
|
|
|
isEditable: true,
|
2020-11-23 19:58:51 +00:00
|
|
|
font: NSFont(name: PostAppearance.serif.rawValue, size: 17),
|
2020-11-23 19:13:15 +00:00
|
|
|
onEditingChanged: onEditingChanged,
|
|
|
|
onCommit: onCommit,
|
|
|
|
onTextChange: onTextChange
|
|
|
|
)
|
|
|
|
}
|
2020-11-23 16:59:01 +00:00
|
|
|
}
|
|
|
|
.background(Color(NSColor.controlBackgroundColor))
|
2020-10-23 20:01:37 +00:00
|
|
|
.onAppear(perform: {
|
2020-11-23 16:18:03 +00:00
|
|
|
if post.title.isEmpty {
|
|
|
|
self.combinedText = post.body
|
|
|
|
} else {
|
|
|
|
self.combinedText = "# \(post.title)\n\n\(post.body)"
|
|
|
|
}
|
2020-10-23 20:01:37 +00:00
|
|
|
})
|
2021-02-10 20:16:34 +00:00
|
|
|
.onReceive(timer) { _ in
|
|
|
|
if !post.body.isEmpty && hasBeenEdited {
|
|
|
|
DispatchQueue.main.async {
|
2021-10-08 21:07:06 +00:00
|
|
|
LocalStorageManager.standard.saveContext()
|
2021-02-10 20:16:34 +00:00
|
|
|
hasBeenEdited = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 20:01:37 +00:00
|
|
|
}
|
2020-11-23 16:18:03 +00:00
|
|
|
|
|
|
|
private func onEditingChanged() {
|
2021-02-10 20:16:34 +00:00
|
|
|
hasBeenEdited = true
|
2020-11-23 16:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func onTextChange(_ text: String) {
|
|
|
|
extractTitle(text)
|
2020-11-23 19:37:13 +00:00
|
|
|
|
2023-07-23 11:19:52 +00:00
|
|
|
if !updatingFromServer {
|
|
|
|
if post.status == PostStatus.published.rawValue {
|
|
|
|
post.status = PostStatus.edited.rawValue
|
|
|
|
}
|
|
|
|
if post.status == PostStatus.edited.rawValue,
|
|
|
|
post.title == model.editor.initialPostTitle,
|
|
|
|
post.body == model.editor.initialPostBody {
|
|
|
|
post.status = PostStatus.published.rawValue
|
|
|
|
}
|
2020-11-23 19:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if updatingFromServer {
|
|
|
|
self.updatingFromServer = false
|
|
|
|
}
|
2021-02-10 20:16:34 +00:00
|
|
|
hasBeenEdited = true
|
2020-11-23 16:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func onCommit() {
|
2021-02-10 20:16:34 +00:00
|
|
|
if !post.body.isEmpty && hasBeenEdited {
|
|
|
|
DispatchQueue.main.async {
|
2021-10-08 21:07:06 +00:00
|
|
|
LocalStorageManager.standard.saveContext()
|
2021-02-10 20:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hasBeenEdited = false
|
2020-11-23 16:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func extractTitle(_ text: String) {
|
|
|
|
var detectedTitle: String
|
|
|
|
|
|
|
|
if text.hasPrefix("# ") {
|
|
|
|
let endOfTitleIndex = text.firstIndex(of: "\n") ?? text.endIndex
|
|
|
|
detectedTitle = String(text[..<endOfTitleIndex])
|
|
|
|
|
|
|
|
self.post.title = String(detectedTitle.dropFirst("# ".count))
|
|
|
|
let remainingText = String(text.dropFirst(detectedTitle.count).dropFirst(1))
|
|
|
|
if remainingText.hasPrefix("\n") {
|
|
|
|
self.post.body = String(remainingText.dropFirst(1))
|
|
|
|
} else {
|
|
|
|
self.post.body = remainingText
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.post.title = ""
|
|
|
|
self.post.body = text
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 20:01:37 +00:00
|
|
|
}
|