Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
210 lines
8.2 KiB
Swift
210 lines
8.2 KiB
Swift
import SwiftUI
|
||
import LichtkastenCore
|
||
|
||
/// Editor texts for all fields (lists are kept as raw "a, b, c" text while typing).
|
||
typealias FieldTexts = [String: String]
|
||
|
||
extension IPTCValues {
|
||
func texts() -> FieldTexts {
|
||
var t: FieldTexts = [:]
|
||
for f in IPTCField.all { t[f.id] = self[f] }
|
||
return t
|
||
}
|
||
|
||
init(texts: FieldTexts) {
|
||
self.init()
|
||
for f in IPTCField.all { self[f] = (texts[f.id] ?? "").trimmingCharacters(in: .whitespaces) }
|
||
}
|
||
}
|
||
|
||
/// The field layout used everywhere (inspector + template editor) – PM Stationery Pad order.
|
||
struct IPTCFieldsForm: View {
|
||
@Binding var texts: FieldTexts
|
||
var mixed: Set<String> = []
|
||
var dirty: Set<String> = []
|
||
var onEdit: (String) -> Void = { _ in }
|
||
@AppStorage("collapsedIPTCSections") private var collapsedRaw = ""
|
||
|
||
var body: some View {
|
||
VStack(alignment: .leading, spacing: 10) {
|
||
ForEach(IPTCSection.allCases, id: \.self) { section in
|
||
let fields = IPTCField.all.filter { $0.section == section }
|
||
DisclosureGroup(isExpanded: expandedBinding(section)) {
|
||
VStack(alignment: .leading, spacing: 6) {
|
||
ForEach(fields) { field in
|
||
FieldRow(field: field, text: binding(field), isMixed: mixed.contains(field.id) && !dirty.contains(field.id),
|
||
isDirty: dirty.contains(field.id))
|
||
}
|
||
}
|
||
.padding(.top, 4)
|
||
} label: {
|
||
Text(section.rawValue).font(.headline)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private func expandedBinding(_ s: IPTCSection) -> Binding<Bool> {
|
||
Binding(get: { !collapsedRaw.components(separatedBy: "|").contains(s.rawValue) },
|
||
set: { expanded in
|
||
var set = Set(collapsedRaw.components(separatedBy: "|").filter { !$0.isEmpty })
|
||
if expanded { set.remove(s.rawValue) } else { set.insert(s.rawValue) }
|
||
collapsedRaw = set.sorted().joined(separator: "|")
|
||
})
|
||
}
|
||
|
||
private func binding(_ f: IPTCField) -> Binding<String> {
|
||
Binding(get: { texts[f.id] ?? "" },
|
||
set: { texts[f.id] = $0; onEdit(f.id) })
|
||
}
|
||
}
|
||
|
||
struct FieldRow: View {
|
||
let field: IPTCField
|
||
@Binding var text: String
|
||
let isMixed: Bool
|
||
let isDirty: Bool
|
||
|
||
private static let variableFields: Set<String> = ["caption", "headline", "objectName", "instructions", "event",
|
||
"transmissionReference", "keywords"]
|
||
|
||
var body: some View {
|
||
VStack(alignment: .leading, spacing: 2) {
|
||
HStack(spacing: 4) {
|
||
Text(field.label).font(.caption).foregroundStyle(isDirty ? Color.accentColor : .secondary)
|
||
if field.kind == .list { Text("(Komma-getrennt)").font(.caption2).foregroundStyle(.tertiary) }
|
||
Spacer()
|
||
if let max = field.iimMaxBytes {
|
||
let used = text.utf8.count
|
||
if used > 0 {
|
||
Text("\(used)/\(max)")
|
||
.font(.caption2.monospacedDigit())
|
||
.foregroundStyle(used > max && !text.contains("{") ? .red : .secondary)
|
||
.help("IPTC-IIM-Limit in Bytes. Agenturen kürzen oder lehnen längere Werte ab.")
|
||
}
|
||
}
|
||
if Self.variableFields.contains(field.id) {
|
||
Menu {
|
||
ForEach(VariableExpander.documented, id: \.0) { v in
|
||
Button("\(v.0) – \(v.1)") { text += v.0 }
|
||
}
|
||
} label: { Image(systemName: "curlybraces") }
|
||
.menuStyle(.borderlessButton)
|
||
.menuIndicator(.hidden)
|
||
.fixedSize()
|
||
.help("Variable einfügen")
|
||
}
|
||
}
|
||
if field.kind == .multiline || field.id == "keywords" {
|
||
TextField(isMixed ? "‹mehrere Werte›" : "", text: $text, axis: .vertical)
|
||
.lineLimit(field.id == "caption" ? 4...12 : 2...6)
|
||
.textFieldStyle(.roundedBorder)
|
||
} else {
|
||
TextField(isMixed ? "‹mehrere Werte›" : "", text: $text)
|
||
.textFieldStyle(.roundedBorder)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Right-hand inspector: edit IPTC of the current selection.
|
||
struct IPTCInspectorView: View {
|
||
@EnvironmentObject var state: AppState
|
||
@State private var texts: FieldTexts = [:]
|
||
@State private var mixed: Set<String> = []
|
||
@State private var dirty: Set<String> = []
|
||
@State private var loadedSelection: [String] = []
|
||
@State private var newTemplateName = ""
|
||
@State private var showSaveTemplate = false
|
||
|
||
var body: some View {
|
||
VStack(spacing: 0) {
|
||
header
|
||
Divider()
|
||
ScrollView {
|
||
IPTCFieldsForm(texts: $texts, mixed: mixed, dirty: dirty) { dirty.insert($0) }
|
||
.padding(12)
|
||
}
|
||
Divider()
|
||
footer
|
||
}
|
||
.onAppear(perform: reload)
|
||
.onChange(of: state.selection) { _, _ in reload() }
|
||
.onChange(of: state.cursorID) { _, _ in if state.selection.isEmpty { reload() } }
|
||
.onReceive(state.$items) { _ in
|
||
// Refresh when metadata arrives / changes, unless the user is editing.
|
||
if dirty.isEmpty { DispatchQueue.main.async { reload() } }
|
||
}
|
||
.alert("Als Vorlage speichern", isPresented: $showSaveTemplate) {
|
||
TextField("Name", text: $newTemplateName)
|
||
Button("Speichern") {
|
||
let values = IPTCValues(texts: texts)
|
||
state.templates.append(IPTCTemplate(name: newTemplateName.isEmpty ? "Neue Vorlage" : newTemplateName, values: values))
|
||
}
|
||
Button("Abbrechen", role: .cancel) {}
|
||
}
|
||
}
|
||
|
||
private var header: some View {
|
||
HStack {
|
||
let n = state.selectedItems.count
|
||
VStack(alignment: .leading) {
|
||
Text("IPTC").font(.headline)
|
||
Text(n == 0 ? "Keine Auswahl" : n == 1 ? state.selectedItems[0].filename : "\(n) Bilder ausgewählt")
|
||
.font(.caption).foregroundStyle(.secondary).lineLimit(1)
|
||
}
|
||
Spacer()
|
||
Menu {
|
||
ForEach(state.templates) { t in
|
||
Button(t.name) { state.applyTemplate(t) }
|
||
}
|
||
if state.templates.isEmpty { Text("Noch keine Vorlagen") }
|
||
Divider()
|
||
Button("Vorlagen verwalten …") { state.showTemplateManager = true }
|
||
Button("PM-Vorlage importieren …") { state.importPMTemplate() }
|
||
} label: {
|
||
Label("Vorlage", systemImage: "doc.on.clipboard")
|
||
}
|
||
.fixedSize()
|
||
}
|
||
.padding(12)
|
||
}
|
||
|
||
private var footer: some View {
|
||
HStack {
|
||
Button("Als Vorlage …") { newTemplateName = ""; showSaveTemplate = true }
|
||
Spacer()
|
||
Button("Verwerfen") { dirty = []; reload() }.disabled(dirty.isEmpty)
|
||
Button("Übernehmen") {
|
||
let fields = IPTCField.all.filter { dirty.contains($0.id) }
|
||
state.applyIPTC(IPTCValues(texts: texts), fields: fields)
|
||
dirty = []
|
||
}
|
||
.keyboardShortcut(.return, modifiers: .command)
|
||
.buttonStyle(.borderedProminent)
|
||
.disabled(dirty.isEmpty || state.selectedItems.isEmpty)
|
||
}
|
||
.padding(12)
|
||
}
|
||
|
||
private func reload() {
|
||
let items = state.selectedItems
|
||
let ids = items.map(\.id)
|
||
if ids != loadedSelection { dirty = [] }
|
||
loadedSelection = ids
|
||
guard dirty.isEmpty else { return }
|
||
guard let first = items.first else { texts = [:]; mixed = []; return }
|
||
var t = first.iptc.texts()
|
||
var m = Set<String>()
|
||
for item in items.dropFirst() {
|
||
let other = item.iptc.texts()
|
||
for f in IPTCField.all where (other[f.id] ?? "") != (t[f.id] ?? "") {
|
||
m.insert(f.id)
|
||
t[f.id] = ""
|
||
}
|
||
}
|
||
texts = t
|
||
mixed = m
|
||
}
|
||
}
|