Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
94 lines
4.0 KiB
Swift
94 lines
4.0 KiB
Swift
import SwiftUI
|
||
import UniformTypeIdentifiers
|
||
import LichtkastenCore
|
||
|
||
/// PM "Code Replacements": table editor, import/export of PM's tab-delimited file, live test.
|
||
struct CodesWindow: View {
|
||
@EnvironmentObject var state: AppState
|
||
@State private var rows: [Row] = []
|
||
@State private var selection: Set<Row.ID> = []
|
||
@State private var test = "\\vfl\\ gegen \\tsg\\"
|
||
|
||
struct Row: Identifiable, Hashable {
|
||
let id = UUID()
|
||
var code: String
|
||
var columns: String // replacement columns separated by " | "
|
||
}
|
||
|
||
var body: some View {
|
||
VStack(alignment: .leading, spacing: 10) {
|
||
HStack {
|
||
Text("Code-Ersetzungen").font(.title3.bold())
|
||
Spacer()
|
||
Text("Trennzeichen:")
|
||
TextField("", text: Binding(get: { String(state.codes.delimiter) },
|
||
set: { if let c = $0.last { state.codes.delimiter = c } }))
|
||
.frame(width: 30)
|
||
}
|
||
Text("Tippe im IPTC-Feld \\code\\ – sobald das schließende Trennzeichen getippt wird, ersetzt Lichtkasten den Code (wie PM). \\code#2\\ nimmt Spalte 2.")
|
||
.font(.caption).foregroundStyle(.secondary)
|
||
HStack {
|
||
Text("Code").frame(width: 140, alignment: .leading)
|
||
Text("Ersetzung (weitere Spalten mit „ | “ trennen)")
|
||
}
|
||
.font(.caption.bold())
|
||
List(selection: $selection) {
|
||
ForEach($rows) { $r in
|
||
HStack {
|
||
TextField("", text: $r.code).frame(width: 140)
|
||
TextField("", text: $r.columns)
|
||
}
|
||
.tag(r.id)
|
||
}
|
||
}
|
||
.onChange(of: rows) { _, _ in commit() }
|
||
HStack {
|
||
Button { rows.append(Row(code: "", columns: "")) } label: { Image(systemName: "plus") }
|
||
Button { rows.removeAll { selection.contains($0.id) }; commit() } label: { Image(systemName: "minus") }
|
||
.disabled(selection.isEmpty)
|
||
Spacer()
|
||
Button("Datei laden (PM-Format) …") { state.importCodeReplacements(); load() }
|
||
Button("Exportieren …") { export() }
|
||
}
|
||
Divider()
|
||
HStack {
|
||
TextField("Test", text: $test).textFieldStyle(.roundedBorder)
|
||
Image(systemName: "arrow.right")
|
||
Text(state.codes.expand(test)).textSelection(.enabled).frame(maxWidth: .infinity, alignment: .leading)
|
||
}
|
||
Divider()
|
||
DisclosureGroup("Variablen") {
|
||
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], alignment: .leading) {
|
||
ForEach(VariableExpander.documented, id: \.0) { v in
|
||
HStack { Text(v.0).font(.system(.caption, design: .monospaced)).textSelection(.enabled); Text(v.1).font(.caption).foregroundStyle(.secondary) }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.padding(16)
|
||
.onAppear(perform: load)
|
||
}
|
||
|
||
private func load() {
|
||
rows = state.codes.table.sorted { $0.key < $1.key }.map { Row(code: $0.key, columns: $0.value.joined(separator: " | ")) }
|
||
}
|
||
|
||
private func commit() {
|
||
var table: [String: [String]] = [:]
|
||
for r in rows where !r.code.trimmingCharacters(in: .whitespaces).isEmpty {
|
||
table[r.code.trimmingCharacters(in: .whitespaces).lowercased()] =
|
||
r.columns.components(separatedBy: " | ").map { $0.trimmingCharacters(in: .whitespaces) }
|
||
}
|
||
if table != state.codes.table { state.codes.table = table }
|
||
}
|
||
|
||
private func export() {
|
||
let p = NSSavePanel()
|
||
p.nameFieldStringValue = "Codes.txt"
|
||
guard p.runModal() == .OK, let url = p.url else { return }
|
||
let text = state.codes.table.sorted { $0.key < $1.key }
|
||
.map { ([$0.key] + $0.value).joined(separator: "\t") }.joined(separator: "\n") + "\n"
|
||
try? text.write(to: url, atomically: true, encoding: .utf8)
|
||
}
|
||
}
|