Code-Ersetzung mit Teams (TXT-Import, nur neue Spieler nachladen, Auto-Reload), Codes ohne Trennzeichen, Cursor bleibt stehen; Auswerfen per diskutil mit Wiederholung direkt nach dem Kopieren
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
This commit is contained in:
1 parent
d39db536a0
commit
25729ebfe3
10 files changed
+615
-134
No files matched your search
@@ -0,0 +1,111 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
import LichtkastenCore
|
||||
|
||||
/// Replaces a code right before the cursor (PM-style live code replacement) and keeps the cursor in place.
|
||||
/// Returns true if something was replaced. Undo (⌘Z) restores the code.
|
||||
@MainActor
|
||||
func applyCodeExpansion(_ tv: NSTextView, codes: CodeReplacements?) -> Bool {
|
||||
guard let codes else { return false }
|
||||
let sel = tv.selectedRange()
|
||||
guard sel.length == 0, let (range, rep) = codes.pendingExpansion(in: tv.string, cursor: sel.location) else { return false }
|
||||
let afterReplaced = sel.location - (range.location + range.length) // e.g. the typed space after "w10"
|
||||
tv.insertText(rep, replacementRange: range)
|
||||
let newCursor = range.location + (rep as NSString).length + afterReplaced
|
||||
tv.setSelectedRange(NSRange(location: min(newCursor, (tv.string as NSString).length), length: 0))
|
||||
return true
|
||||
}
|
||||
|
||||
/// Multi-line field (caption, keywords …) with live code replacement.
|
||||
struct CodeTextView: NSViewRepresentable {
|
||||
@Binding var text: String
|
||||
var codes: CodeReplacements?
|
||||
|
||||
func makeCoordinator() -> Coordinator { Coordinator(self) }
|
||||
|
||||
func makeNSView(context: Context) -> NSScrollView {
|
||||
let sv = NSTextView.scrollableTextView()
|
||||
sv.borderType = .bezelBorder
|
||||
sv.hasVerticalScroller = true
|
||||
guard let tv = sv.documentView as? NSTextView else { return sv }
|
||||
tv.delegate = context.coordinator
|
||||
tv.isRichText = false
|
||||
tv.importsGraphics = false
|
||||
tv.allowsUndo = true
|
||||
tv.font = .systemFont(ofSize: 13)
|
||||
tv.isAutomaticQuoteSubstitutionEnabled = false
|
||||
tv.isAutomaticDashSubstitutionEnabled = false
|
||||
tv.isAutomaticTextReplacementEnabled = false
|
||||
tv.isContinuousSpellCheckingEnabled = true
|
||||
tv.textContainerInset = NSSize(width: 3, height: 4)
|
||||
tv.string = text
|
||||
return sv
|
||||
}
|
||||
|
||||
func updateNSView(_ sv: NSScrollView, context: Context) {
|
||||
context.coordinator.parent = self
|
||||
guard let tv = sv.documentView as? NSTextView, tv.string != text, !context.coordinator.editing else { return }
|
||||
tv.string = text
|
||||
}
|
||||
|
||||
final class Coordinator: NSObject, NSTextViewDelegate {
|
||||
var parent: CodeTextView
|
||||
var expanding = false
|
||||
var editing = false
|
||||
init(_ p: CodeTextView) { parent = p }
|
||||
|
||||
func textDidChange(_ notification: Notification) {
|
||||
guard let tv = notification.object as? NSTextView else { return }
|
||||
if !expanding {
|
||||
expanding = true
|
||||
MainActor.assumeIsolated { _ = applyCodeExpansion(tv, codes: parent.codes) }
|
||||
expanding = false
|
||||
}
|
||||
editing = true
|
||||
parent.text = tv.string
|
||||
DispatchQueue.main.async { self.editing = false }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Single-line field with live code replacement (event, city, test field …).
|
||||
struct CodeTextField: NSViewRepresentable {
|
||||
@Binding var text: String
|
||||
var codes: CodeReplacements?
|
||||
var placeholder: String = ""
|
||||
|
||||
func makeCoordinator() -> Coordinator { Coordinator(self) }
|
||||
|
||||
func makeNSView(context: Context) -> NSTextField {
|
||||
let f = NSTextField(string: text)
|
||||
f.delegate = context.coordinator
|
||||
f.placeholderString = placeholder
|
||||
f.bezelStyle = .roundedBezel
|
||||
f.lineBreakMode = .byTruncatingTail
|
||||
f.cell?.isScrollable = true
|
||||
f.font = .systemFont(ofSize: 13)
|
||||
return f
|
||||
}
|
||||
|
||||
func updateNSView(_ f: NSTextField, context: Context) {
|
||||
context.coordinator.parent = self
|
||||
f.placeholderString = placeholder
|
||||
if f.stringValue != text, f.currentEditor() == nil { f.stringValue = text }
|
||||
}
|
||||
|
||||
final class Coordinator: NSObject, NSTextFieldDelegate {
|
||||
var parent: CodeTextField
|
||||
var expanding = false
|
||||
init(_ p: CodeTextField) { parent = p }
|
||||
|
||||
func controlTextDidChange(_ obj: Notification) {
|
||||
guard let f = obj.object as? NSTextField else { return }
|
||||
if !expanding, let editor = f.currentEditor() as? NSTextView {
|
||||
expanding = true
|
||||
MainActor.assumeIsolated { _ = applyCodeExpansion(editor, codes: parent.codes) }
|
||||
expanding = false
|
||||
}
|
||||
parent.text = f.stringValue
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,93 +1,190 @@
|
||||
import SwiftUI
|
||||
import UniformTypeIdentifiers
|
||||
import LichtkastenCore
|
||||
|
||||
/// PM "Code Replacements": table editor, import/export of PM's tab-delimited file, live test.
|
||||
/// PM "Code Replacement" – but with teams stored in the app:
|
||||
/// left the teams (enable checkbox like PM's file list), right the players of the selected team.
|
||||
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\\"
|
||||
@State private var selectedTeam: UUID?
|
||||
@State private var selectedRows: Set<UUID> = []
|
||||
@State private var newCode = ""
|
||||
@State private var newText = ""
|
||||
@State private var test = "w10 trifft gegen h10, "
|
||||
@State private var search = ""
|
||||
|
||||
struct Row: Identifiable, Hashable {
|
||||
let id = UUID()
|
||||
var code: String
|
||||
var columns: String // replacement columns separated by " | "
|
||||
}
|
||||
private var teamIndex: Int? { state.codeLibrary.sets.firstIndex { $0.id == selectedTeam } }
|
||||
|
||||
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) }
|
||||
}
|
||||
}
|
||||
Text("Diese Teams werden für Code-Ersetzungen verwendet. Bei doppelten Codes gewinnt das obere Team.")
|
||||
.font(.callout).foregroundStyle(.secondary)
|
||||
HSplitView {
|
||||
teamList.frame(minWidth: 300, idealWidth: 360)
|
||||
playerTable.frame(minWidth: 420)
|
||||
}
|
||||
.frame(minHeight: 320)
|
||||
optionsRow
|
||||
testRow
|
||||
}
|
||||
.padding(16)
|
||||
.onAppear(perform: load)
|
||||
.onAppear { if selectedTeam == nil { selectedTeam = state.codeLibrary.sets.first?.id } }
|
||||
}
|
||||
|
||||
private func load() {
|
||||
rows = state.codes.table.sorted { $0.key < $1.key }.map { Row(code: $0.key, columns: $0.value.joined(separator: " | ")) }
|
||||
}
|
||||
// MARK: Teams (PM file list)
|
||||
|
||||
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) }
|
||||
private var teamList: some View {
|
||||
VStack(spacing: 6) {
|
||||
List(selection: $selectedTeam) {
|
||||
ForEach($state.codeLibrary.sets) { $set in
|
||||
HStack {
|
||||
Toggle("", isOn: $set.enabled).toggleStyle(.checkbox).labelsHidden()
|
||||
.help("Aktiv (PM: Enable)")
|
||||
VStack(alignment: .leading, spacing: 1) {
|
||||
Text(set.name).lineLimit(1)
|
||||
Text(set.sourcePath.map { URL(fileURLWithPath: $0).lastPathComponent } ?? "manuell")
|
||||
.font(.caption2).foregroundStyle(.secondary).lineLimit(1)
|
||||
}
|
||||
Spacer()
|
||||
Text("\(set.entries.count)").font(.caption.monospacedDigit()).foregroundStyle(.secondary)
|
||||
}
|
||||
.tag(set.id)
|
||||
.contextMenu {
|
||||
Button("Neue Spieler aus TXT nachladen …") { state.importCodeFiles(into: set.id) }
|
||||
Button("Im Finder zeigen") {
|
||||
if let p = set.sourcePath { NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: p)]) }
|
||||
}.disabled(set.sourcePath == nil)
|
||||
Button("Löschen", role: .destructive) { remove(set.id) }
|
||||
}
|
||||
}
|
||||
.onMove { state.codeLibrary.sets.move(fromOffsets: $0, toOffset: $1) }
|
||||
}
|
||||
HStack(spacing: 6) {
|
||||
Button("Hinzufügen …") { state.importCodeFiles() }
|
||||
.help("TXT-Dateien als Teams importieren (wie PM „Add…“)")
|
||||
Button("Neues Team") {
|
||||
let s = CodeSet(name: "Neues Team")
|
||||
state.codeLibrary.sets.append(s)
|
||||
selectedTeam = s.id
|
||||
}
|
||||
Button("Entfernen") { if let id = selectedTeam { remove(id) } }.disabled(selectedTeam == nil)
|
||||
Spacer()
|
||||
Button("Alle neu laden") { state.reloadAllCodes() }
|
||||
.help("Liest die TXT-Dateien erneut – nur neue Spieler kommen hinzu")
|
||||
}
|
||||
.controlSize(.small)
|
||||
}
|
||||
if table != state.codes.table { state.codes.table = table }
|
||||
}
|
||||
|
||||
private func export() {
|
||||
// MARK: Players of the selected team
|
||||
|
||||
@ViewBuilder
|
||||
private var playerTable: some View {
|
||||
if let i = teamIndex {
|
||||
VStack(alignment: .leading, spacing: 6) {
|
||||
HStack {
|
||||
TextField("Teamname", text: $state.codeLibrary.sets[i].name).font(.headline).textFieldStyle(.roundedBorder)
|
||||
Button("TXT nachladen …") { state.importCodeFiles(into: state.codeLibrary.sets[i].id) }
|
||||
.help("Neue Spieler aus einer TXT hinzufügen – vorhandene bleiben unverändert")
|
||||
}
|
||||
TextField("Suchen", text: $search).textFieldStyle(.roundedBorder)
|
||||
HStack {
|
||||
Text("Code").frame(width: 90, alignment: .leading)
|
||||
Text("Ersetzung (weitere Spalten mit „ | “ trennen)")
|
||||
}
|
||||
.font(.caption.bold())
|
||||
List(selection: $selectedRows) {
|
||||
ForEach($state.codeLibrary.sets[i].entries) { $e in
|
||||
if search.isEmpty || e.code.localizedCaseInsensitiveContains(search)
|
||||
|| e.columns.joined().localizedCaseInsensitiveContains(search) {
|
||||
HStack {
|
||||
TextField("", text: $e.code).frame(width: 90)
|
||||
TextField("", text: Binding(get: { e.columns.joined(separator: " | ") },
|
||||
set: { e.columns = $0.components(separatedBy: " | ") }))
|
||||
}
|
||||
.tag(e.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Quick add (e.g. a new player during the game)
|
||||
HStack {
|
||||
TextField("w23", text: $newCode).frame(width: 90).textFieldStyle(.roundedBorder)
|
||||
TextField("Vorname Nachname (VfL Wolfsburg, 23)", text: $newText).textFieldStyle(.roundedBorder)
|
||||
.onSubmit(addPlayer)
|
||||
Button("Hinzufügen", action: addPlayer).disabled(newCode.isEmpty || newText.isEmpty)
|
||||
Button {
|
||||
state.codeLibrary.sets[i].entries.removeAll { selectedRows.contains($0.id) }
|
||||
selectedRows = []
|
||||
} label: { Image(systemName: "minus") }
|
||||
.disabled(selectedRows.isEmpty)
|
||||
.help("Ausgewählte Spieler löschen")
|
||||
}
|
||||
HStack {
|
||||
Spacer()
|
||||
Button("Als TXT exportieren …") { export(state.codeLibrary.sets[i]) }.controlSize(.small)
|
||||
}
|
||||
}
|
||||
.padding(.leading, 8)
|
||||
} else {
|
||||
Text("Team auswählen oder hinzufügen").foregroundStyle(.secondary)
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Options (PM bottom row)
|
||||
|
||||
private var optionsRow: some View {
|
||||
HStack(spacing: 16) {
|
||||
Toggle("TXT-Dateien automatisch neu laden, wenn sie sich ändern", isOn: $state.codeLibrary.autoReload)
|
||||
Spacer()
|
||||
Toggle("Codes ohne Trennzeichen (w10 + Leertaste)", isOn: $state.codeLibrary.bareCodes)
|
||||
HStack(spacing: 4) {
|
||||
Text("Trennzeichen:")
|
||||
TextField("", text: Binding(get: { state.codeLibrary.delimiter },
|
||||
set: { if let c = $0.last { state.codeLibrary.delimiter = String(c) } }))
|
||||
.frame(width: 28)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
}
|
||||
HStack(spacing: 4) {
|
||||
Text("Standard-Ersetzung:")
|
||||
TextField("", text: $state.codeLibrary.defaultReplacement).frame(width: 140).textFieldStyle(.roundedBorder)
|
||||
}
|
||||
}
|
||||
.font(.callout)
|
||||
}
|
||||
|
||||
private var testRow: some View {
|
||||
HStack {
|
||||
Text("Test:").foregroundStyle(.secondary)
|
||||
CodeTextField(text: $test, codes: state.codes)
|
||||
.frame(height: 24)
|
||||
Text("Tippe z. B. „w10“ + Leertaste oder „\(state.codeLibrary.delimiter)w10\(state.codeLibrary.delimiter)“")
|
||||
.font(.caption).foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Actions
|
||||
|
||||
private func addPlayer() {
|
||||
guard let i = teamIndex, !newCode.isEmpty, !newText.isEmpty else { return }
|
||||
let r = state.codeLibrary.sets[i].merge([CodeEntry(code: newCode.trimmingCharacters(in: .whitespaces),
|
||||
columns: newText.components(separatedBy: " | "))])
|
||||
if r.added == 0 {
|
||||
state.alert = AppAlert(title: "Code existiert schon", message: "„\(newCode)“ ist in diesem Team bereits vergeben.")
|
||||
return
|
||||
}
|
||||
newCode = ""; newText = ""
|
||||
}
|
||||
|
||||
private func remove(_ id: UUID) {
|
||||
state.codeLibrary.sets.removeAll { $0.id == id }
|
||||
if selectedTeam == id { selectedTeam = state.codeLibrary.sets.first?.id }
|
||||
}
|
||||
|
||||
private func export(_ set: CodeSet) {
|
||||
let p = NSSavePanel()
|
||||
p.nameFieldStringValue = "Codes.txt"
|
||||
p.nameFieldStringValue = set.name + ".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"
|
||||
let text = set.entries.map { ([$0.code] + $0.columns).joined(separator: "\t") }.joined(separator: "\n") + "\n"
|
||||
try? text.write(to: url, atomically: true, encoding: .utf8)
|
||||
}
|
||||
}
|
||||
@@ -72,7 +72,7 @@ struct PMMetadataForm: View {
|
||||
VStack(alignment: .leading, spacing: 6) {
|
||||
PMSectionHeader(title: section.rawValue)
|
||||
ForEach(IPTCField.all.filter { $0.section == section }) { field in
|
||||
PMFieldRow(field: field, text: binding(field),
|
||||
PMFieldRow(field: field, text: binding(field), codes: codes,
|
||||
isMixed: mixed.contains(field.id) && !dirty.contains(field.id),
|
||||
isDirty: dirty.contains(field.id),
|
||||
applied: appliedBinding(field))
|
||||
@@ -89,12 +89,8 @@ struct PMMetadataForm: View {
|
||||
|
||||
private func binding(_ f: IPTCField) -> Binding<String> {
|
||||
Binding(get: { texts[f.id] ?? "" },
|
||||
set: { new in
|
||||
var value = new
|
||||
// PM code replacement while typing: "\bvb\" expands as soon as the closing delimiter is typed.
|
||||
if let codes, !codes.table.isEmpty, new.last == codes.delimiter, new.count > (texts[f.id]?.count ?? 0) {
|
||||
value = codes.expand(new)
|
||||
}
|
||||
set: { value in
|
||||
// Code replacement happens inside the text views (cursor stays in place).
|
||||
texts[f.id] = value
|
||||
// Editing a field ticks its checkbox (PM behaviour).
|
||||
applied?.wrappedValue.insert(f.id)
|
||||
@@ -106,6 +102,7 @@ struct PMMetadataForm: View {
|
||||
struct PMFieldRow: View {
|
||||
let field: IPTCField
|
||||
@Binding var text: String
|
||||
var codes: CodeReplacements?
|
||||
let isMixed: Bool
|
||||
let isDirty: Bool
|
||||
let applied: Binding<Bool>?
|
||||
@@ -141,21 +138,16 @@ struct PMFieldRow: View {
|
||||
if field.id == "dateCreated" {
|
||||
DateFieldEditor(text: $text)
|
||||
} else if let h = height {
|
||||
TextEditor(text: $text)
|
||||
.font(.system(size: 13))
|
||||
.scrollContentBackground(.hidden)
|
||||
.padding(4)
|
||||
.background(Color(nsColor: .textBackgroundColor))
|
||||
CodeTextView(text: $text, codes: codes)
|
||||
.overlay(alignment: .topLeading) {
|
||||
if isMixed && text.isEmpty {
|
||||
Text("‹mehrere Werte›").foregroundStyle(.tertiary).padding(8).allowsHitTesting(false)
|
||||
}
|
||||
}
|
||||
.overlay(RoundedRectangle(cornerRadius: 3).stroke(Color.secondary.opacity(0.4)))
|
||||
.frame(height: h)
|
||||
} else {
|
||||
TextField(isMixed ? "‹mehrere Werte›" : "", text: $text)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
CodeTextField(text: $text, codes: codes, placeholder: isMixed ? "‹mehrere Werte›" : "")
|
||||
.frame(height: 22)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
|
||||
@@ -273,21 +273,12 @@ struct LightroomSettingsView: View {
|
||||
|
||||
struct CodesSettings: View {
|
||||
@EnvironmentObject var state: AppState
|
||||
@State private var testInput = "\\bvb\\ gegen \\s04\\"
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section("Code-Ersetzung (wie Photo Mechanic)") {
|
||||
HStack {
|
||||
Text("\(state.codes.table.count) Codes geladen")
|
||||
Spacer()
|
||||
Button("Datei laden …") { state.importCodeReplacements() }
|
||||
Button("Leeren") { state.codes = CodeReplacements() }
|
||||
}
|
||||
Text("Tab-getrennte Textdatei: Code ⇥ Ersetzung ⇥ weitere Spalten. Im Text: \\code\\ oder \\code#2\\ für Spalte 2.")
|
||||
.font(.caption).foregroundStyle(.secondary)
|
||||
TextField("Test", text: $testInput)
|
||||
Text(state.codes.expand(testInput)).font(.callout).textSelection(.enabled)
|
||||
Section("Code-Ersetzung") {
|
||||
Text("\(state.codeLibrary.sets.filter(\.enabled).count) von \(state.codeLibrary.sets.count) Teams aktiv · \(state.codes.table.count) Codes")
|
||||
Button("Code-Ersetzungen öffnen … (⌘⇧K)") { state.openWindow("codes") }
|
||||
}
|
||||
}
|
||||
.formStyle(.grouped)
|
||||
|
||||
Reference in new issue
Block a user