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:
Lichtkasten DevandClaude Opus 5.5 committed 2026-11-04 00:19:45 +00:00
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
}
}
}