Skip to content
This repository was archived by the owner on Nov 7, 2026. It is now read-only.

Commit 04aa358

Browse files
committed
Move undo/redo and tab editing out of ContentView
- VisuStructCore: VisuStructEditHistory (undo/redo stacks, 80-step limit) and VisuStructDocumentEditing.sanitizedSelection, with tests. - DiagramSheetTab keeps a VisuStructEditHistory instead of two arrays. - DiagramSheetTabsModel+Editing: currentTab, updateCurrentTab, commitDocument, resetHistoryAndDocument, undo, redo, selectAndClearError, setErrorOnCurrentTab. - ContentView uses these instead of copying tabs by hand at each site. No behavior change intended. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Cp4EQgFQyGbM8MVLiWbJKQ
1 parent 2112797 commit 04aa358

6 files changed

Lines changed: 282 additions & 207 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
3+
extension VisuStructDocumentEditing {
4+
/// Auswahl nach Undo/Redo oder Löschen: leeres Dokument → keine Auswahl; fehlende oder ungültige Auswahl → erstes Element.
5+
public static func sanitizedSelection(_ path: [Int]?, in doc: VisuStructDocument) -> [Int]? {
6+
if doc.elements.isEmpty { return nil }
7+
guard let path else { return [0] }
8+
if (try? element(at: path, in: doc)) == nil { return [0] }
9+
return path
10+
}
11+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Foundation
2+
3+
/// Undo-/Redo-Verlauf eines Diagramms: Schnappschüsse des Dokuments vor jeder Änderung.
4+
public struct VisuStructEditHistory: Equatable, Sendable {
5+
public static let defaultMaxSteps = 80
6+
7+
public private(set) var undoStack: [VisuStructDocument] = []
8+
public private(set) var redoStack: [VisuStructDocument] = []
9+
public let maxSteps: Int
10+
11+
public init(maxSteps: Int = VisuStructEditHistory.defaultMaxSteps) {
12+
self.maxSteps = maxSteps
13+
}
14+
15+
public var canUndo: Bool { !undoStack.isEmpty }
16+
public var canRedo: Bool { !redoStack.isEmpty }
17+
18+
/// Vor einer Änderung: bisherigen Zustand ablegen, Redo verwerfen.
19+
public mutating func record(_ current: VisuStructDocument) {
20+
pushUndo(current)
21+
redoStack.removeAll()
22+
}
23+
24+
/// Liefert den vorherigen Zustand und legt `current` für Redo ab; `nil`, wenn nichts rückgängig zu machen ist.
25+
public mutating func undo(from current: VisuStructDocument) -> VisuStructDocument? {
26+
guard let previous = undoStack.popLast() else { return nil }
27+
redoStack.append(current)
28+
return previous
29+
}
30+
31+
/// Liefert den wiederhergestellten Zustand und legt `current` für Undo ab; `nil`, wenn nichts wiederherzustellen ist.
32+
public mutating func redo(from current: VisuStructDocument) -> VisuStructDocument? {
33+
guard let next = redoStack.popLast() else { return nil }
34+
pushUndo(current)
35+
return next
36+
}
37+
38+
public mutating func reset() {
39+
undoStack.removeAll()
40+
redoStack.removeAll()
41+
}
42+
43+
private mutating func pushUndo(_ document: VisuStructDocument) {
44+
undoStack.append(document)
45+
while undoStack.count > maxSteps {
46+
undoStack.removeFirst()
47+
}
48+
}
49+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import XCTest
2+
import VisuStructCore
3+
4+
final class EditHistoryTests: XCTestCase {
5+
private func document(_ texts: String...) -> VisuStructDocument {
6+
var doc = VisuStructDocumentEditing.newEmpty()
7+
doc.elements = texts.map { VisuStructElement(type: .anweisung, textLines: [$0]) }
8+
return doc
9+
}
10+
11+
func testUndoAndRedoRestoreSnapshots() {
12+
let a = document("a"), b = document("b"), c = document("c")
13+
var history = VisuStructEditHistory()
14+
XCTAssertFalse(history.canUndo)
15+
history.record(a)
16+
history.record(b)
17+
XCTAssertEqual(history.undo(from: c), b)
18+
XCTAssertEqual(history.undo(from: b), a)
19+
XCTAssertNil(history.undo(from: a))
20+
XCTAssertEqual(history.redo(from: a), b)
21+
XCTAssertEqual(history.redo(from: b), c)
22+
XCTAssertNil(history.redo(from: c))
23+
}
24+
25+
func testRecordClearsRedo() {
26+
var history = VisuStructEditHistory()
27+
history.record(document("a"))
28+
_ = history.undo(from: document("b"))
29+
XCTAssertTrue(history.canRedo)
30+
history.record(document("a"))
31+
XCTAssertFalse(history.canRedo)
32+
}
33+
34+
func testUndoStackIsLimited() {
35+
var history = VisuStructEditHistory(maxSteps: 3)
36+
for i in 0..<5 { history.record(document("\(i)")) }
37+
XCTAssertEqual(history.undoStack, [document("2"), document("3"), document("4")])
38+
XCTAssertEqual(VisuStructEditHistory().maxSteps, 80)
39+
}
40+
41+
func testRedoAlsoRespectsLimit() {
42+
var history = VisuStructEditHistory(maxSteps: 2)
43+
history.record(document("0"))
44+
history.record(document("1"))
45+
_ = history.undo(from: document("2"))
46+
history.record(document("x"))
47+
history.record(document("y"))
48+
XCTAssertEqual(history.undoStack.count, 2)
49+
}
50+
51+
func testReset() {
52+
var history = VisuStructEditHistory()
53+
history.record(document("a"))
54+
_ = history.undo(from: document("b"))
55+
history.record(document("c"))
56+
history.reset()
57+
XCTAssertFalse(history.canUndo)
58+
XCTAssertFalse(history.canRedo)
59+
}
60+
61+
func testSanitizedSelection() {
62+
let doc = document("a", "b")
63+
XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection([1], in: doc), [1])
64+
XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection([5], in: doc), [0])
65+
XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection(nil, in: doc), [0])
66+
var empty = doc
67+
empty.elements = []
68+
XCTAssertNil(VisuStructDocumentEditing.sanitizedSelection([0], in: empty))
69+
}
70+
}

0 commit comments

Comments
 (0)