105 lines
3.2 KiB
Swift
105 lines
3.2 KiB
Swift
import SwiftUI
|
|
|
|
struct PostLabelsView: View {
|
|
|
|
@ObservedObject
|
|
var post: LocalizedPost
|
|
|
|
@ObservedObject
|
|
var other: LocalizedPost
|
|
|
|
@Environment(\.colorScheme)
|
|
var colorScheme
|
|
|
|
@State
|
|
private var showLabelEditor: Bool = false
|
|
|
|
var body: some View {
|
|
ScrollView(.horizontal) {
|
|
HStack(spacing: 5) {
|
|
if post.labels.isEmpty {
|
|
Text("Labels")
|
|
.font(.headline)
|
|
}
|
|
ForEach(post.labels) { label in
|
|
HStack {
|
|
PageIconView(icon: label.icon)
|
|
.frame(maxWidth: 16, maxHeight: 16)
|
|
.scaleEffect(25/16)
|
|
Text(label.value)
|
|
}
|
|
.padding(.vertical, 2)
|
|
.padding(.horizontal, 8)
|
|
.background(colorScheme == .light ? Color.white : Color.black)
|
|
.cornerRadius(8)
|
|
}
|
|
Button(action: { showLabelEditor = true }) {
|
|
Image(systemSymbol: .squareAndPencilCircleFill)
|
|
.resizable()
|
|
.aspectRatio(1, contentMode: .fit)
|
|
.frame(height: 22)
|
|
.foregroundColor(Color.gray)
|
|
.background(Circle()
|
|
.fill(Color.white)
|
|
.padding(1))
|
|
}.buttonStyle(.plain)
|
|
if !other.labels.isEmpty {
|
|
Button("Transfer") {
|
|
post.labels = other.labels.map {
|
|
// Copy instead of reference
|
|
ContentLabel(icon: $0.icon, value: $0.value)
|
|
}
|
|
}
|
|
}
|
|
if !post.labels.isEmpty {
|
|
Button("Copy") {
|
|
var command = "```labels"
|
|
for label in post.labels {
|
|
command += "\n\(label.icon.rawValue): \(label.value)"
|
|
}
|
|
command += "\n```"
|
|
let pasteboard = NSPasteboard.general
|
|
pasteboard.clearContents()
|
|
pasteboard.setString(command, forType: .string)
|
|
}
|
|
}
|
|
}
|
|
.padding(.vertical, 2)
|
|
}
|
|
.sheet(isPresented: $showLabelEditor) {
|
|
LabelModificationView(labels: $post.labels)
|
|
}
|
|
}
|
|
|
|
func addLabel() {
|
|
post.labels.append(.init(icon: .clockFill, value: "Value"))
|
|
}
|
|
|
|
func remove(_ label: ContentLabel) {
|
|
guard let index = post.labels.firstIndex(of: label) else {
|
|
return
|
|
}
|
|
post.labels.remove(at: index)
|
|
}
|
|
}
|
|
|
|
private struct LabelModificationView: View {
|
|
|
|
@Environment(\.dismiss)
|
|
private var dismiss
|
|
|
|
@Binding
|
|
var labels: [ContentLabel]
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Text("Labels")
|
|
.font(.title)
|
|
LabelCreationView(labels: $labels)
|
|
Button("Save") {
|
|
dismiss()
|
|
}
|
|
}.padding()
|
|
}
|
|
}
|