Fix updating of page commands
This commit is contained in:
@ -1,7 +1,34 @@
|
||||
import SwiftUI
|
||||
import SFSafeSymbols
|
||||
|
||||
struct InsertableImage: View, InsertableCommand {
|
||||
struct InsertableImage: View, InsertableCommandView {
|
||||
|
||||
final class Model: ObservableObject, InsertableCommandModel {
|
||||
|
||||
@Published
|
||||
var caption: String?
|
||||
|
||||
@Published
|
||||
var selectedImage: FileResource?
|
||||
|
||||
var isReady: Bool {
|
||||
selectedImage != nil
|
||||
}
|
||||
|
||||
init() {
|
||||
|
||||
}
|
||||
|
||||
var command: String? {
|
||||
guard let selectedImage else {
|
||||
return nil
|
||||
}
|
||||
guard let caption else {
|
||||
return ")"
|
||||
}
|
||||
return ";\(caption))"
|
||||
}
|
||||
}
|
||||
|
||||
static let title = "Image"
|
||||
|
||||
@ -9,17 +36,11 @@ struct InsertableImage: View, InsertableCommand {
|
||||
|
||||
static let icon: SFSymbol = .photo
|
||||
|
||||
@State
|
||||
private var selectedImage: FileResource?
|
||||
@ObservedObject
|
||||
private var model: Model
|
||||
|
||||
@State
|
||||
private var caption: String? = ""
|
||||
|
||||
@Binding
|
||||
private var command: String?
|
||||
|
||||
init(command: Binding<String?>) {
|
||||
self._command = command
|
||||
init(model: Model) {
|
||||
self.model = model
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
@ -27,29 +48,13 @@ struct InsertableImage: View, InsertableCommand {
|
||||
FilePropertyView(
|
||||
title: "Image",
|
||||
footer: "Select the image to insert",
|
||||
selectedFile: $selectedImage,
|
||||
selectedFile: $model.selectedImage,
|
||||
allowedType: .image)
|
||||
OptionalStringPropertyView(
|
||||
title: "Caption",
|
||||
text: $caption,
|
||||
text: $model.caption,
|
||||
prompt: "Image Caption",
|
||||
footer: "The caption to show on the fullscreen image")
|
||||
}
|
||||
.onChange(of: caption) { updateCommand() }
|
||||
.onChange(of: selectedImage) { updateCommand() }
|
||||
}
|
||||
|
||||
func updateCommand() {
|
||||
command = currentCommand
|
||||
}
|
||||
|
||||
private var currentCommand: String? {
|
||||
guard let selectedImage else {
|
||||
return nil
|
||||
}
|
||||
guard let caption else {
|
||||
return ")"
|
||||
}
|
||||
return ";\(caption))"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user