Fix updating of page commands

This commit is contained in:
Christoph Hagen
2025-01-27 22:30:27 +01:00
parent e02bfd17d2
commit 89062f153c
7 changed files with 201 additions and 67 deletions

View File

@ -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 "![image](\(selectedImage.id))"
}
return "![image](\(selectedImage.id);\(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 "![image](\(selectedImage.id))"
}
return "![image](\(selectedImage.id);\(caption))"
}
}