119 lines
3.2 KiB
Swift
119 lines
3.2 KiB
Swift
import SwiftUI
|
|
import SFSafeSymbols
|
|
|
|
struct InsertableLink: View, InsertableCommandView {
|
|
|
|
final class Model: ObservableObject, InsertableCommandModel {
|
|
|
|
@Published
|
|
var selectedType: ItemType = .page
|
|
|
|
@Published
|
|
var selectedFile: FileResource?
|
|
|
|
@Published
|
|
var selectedPage: Page?
|
|
|
|
@Published
|
|
var selectedTag: Tag?
|
|
|
|
@Published
|
|
var isInlineLink = false
|
|
|
|
@Published
|
|
var inlineText = ""
|
|
|
|
var isReady: Bool {
|
|
switch selectedType {
|
|
case .post, .tagOverview:
|
|
return false
|
|
case .page:
|
|
return selectedPage != nil
|
|
case .tag:
|
|
return selectedTag != nil
|
|
case .file:
|
|
return selectedFile != nil
|
|
}
|
|
}
|
|
|
|
init() {
|
|
|
|
}
|
|
|
|
private var linkContent: String? {
|
|
switch selectedType {
|
|
case .post, .tagOverview:
|
|
return nil
|
|
case .page:
|
|
return selectedPage?.id
|
|
case .tag:
|
|
return selectedTag?.id
|
|
case .file:
|
|
return selectedFile?.id
|
|
}
|
|
}
|
|
|
|
var command: String? {
|
|
guard let linkContent else {
|
|
return nil
|
|
}
|
|
guard isInlineLink else {
|
|
return ")"
|
|
}
|
|
return "[\(inlineText)](\(selectedType.rawValue):\(linkContent))"
|
|
}
|
|
}
|
|
|
|
static let title = "Link"
|
|
|
|
static let sheetTitle = "Insert a link to a tag, page or file"
|
|
|
|
static let icon: SFSymbol = .photo
|
|
|
|
@ObservedObject
|
|
private var model: Model
|
|
|
|
init(model: Model) {
|
|
self.model = model
|
|
}
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Picker("", selection: $model.selectedType) {
|
|
Text("Page").tag(ItemType.page)
|
|
Text("Tag").tag(ItemType.tag)
|
|
Text("File").tag(ItemType.file)
|
|
}
|
|
.pickerStyle(.segmented)
|
|
Toggle("Inline Link", isOn: $model.isInlineLink)
|
|
if model.isInlineLink {
|
|
StringPropertyView(
|
|
title: "Link text",
|
|
text: $model.inlineText,
|
|
footer: "The text to show")
|
|
}
|
|
switch model.selectedType {
|
|
case .post:
|
|
Text("Linking posts is not supported")
|
|
case .page:
|
|
PagePropertyView(
|
|
title: "Linked page",
|
|
selectedPage: $model.selectedPage,
|
|
footer: "Select the page to link to")
|
|
case .tag:
|
|
TagPropertyView(
|
|
title: "Linked tag",
|
|
selectedTag: $model.selectedTag,
|
|
footer: "Select the tag to link to")
|
|
case .file:
|
|
FilePropertyView(
|
|
title: "File",
|
|
footer: "Select the image to insert",
|
|
selectedFile: $model.selectedFile)
|
|
case .tagOverview:
|
|
Text("Linking tag overview is not supported")
|
|
}
|
|
}
|
|
}
|
|
}
|