diff --git a/Shared/Sources/Document/Accessibility/DocumentPresenter.swift b/Shared/Sources/Document/Accessibility/DocumentPresenter.swift index 86a4bd12..41282c19 100644 --- a/Shared/Sources/Document/Accessibility/DocumentPresenter.swift +++ b/Shared/Sources/Document/Accessibility/DocumentPresenter.swift @@ -64,11 +64,9 @@ open class DocumentPresenter { frame: CGRect(origin: origin, size: image.size)) - add(frame, into: nil, at: document.artboard.frames.count) - - document.undo?.registerUndo(withTarget: self, handler: { target in - target.remove(frame) - }) + add(frame, + into: nil, + at: document.artboard.frames.count) } public func append(control: any ArtboardElement) { @@ -124,6 +122,12 @@ open class DocumentPresenter { publishControlChanges() select(model) + + document.undo?.registerUndo( + withTarget: self, + handler: { presenter in + presenter.remove(model) + }) } @discardableResult diff --git a/Shared/Tests/DocumentTests/Models/DocumentPresenterTests.swift b/Shared/Tests/DocumentTests/Models/DocumentPresenterTests.swift index 2f85f3f5..6b9e6968 100644 --- a/Shared/Tests/DocumentTests/Models/DocumentPresenterTests.swift +++ b/Shared/Tests/DocumentTests/Models/DocumentPresenterTests.swift @@ -32,8 +32,11 @@ final class DocumentPresenterTests: XCTestCase { sut.append(control: element1) XCTAssertEqual(sut.controlsWithoutFrame.count, 1) - sut.document.undo?.undo() + sut.undo() XCTAssertTrue(sut.controlsWithoutFrame.isEmpty) + + sut.redo() + XCTAssertFalse(sut.controlsWithoutFrame.isEmpty) } // MARK: - Container @@ -76,6 +79,9 @@ final class DocumentPresenterTests: XCTestCase { sut.undo() XCTAssertEqual(container.elements.count, 2) + + sut.redo() + XCTAssertEqual(container.elements.count, 1) } func test_container_whenRemoveContainer_shouldRemoveEverything() throws { @@ -86,6 +92,9 @@ final class DocumentPresenterTests: XCTestCase { sut.undo() XCTAssertEqual(sut.controlsWithoutFrame.count, 1) + + sut.redo() + XCTAssertTrue(sut.controlsWithoutFrame.isEmpty) } // MARK: Artboard @@ -112,6 +121,9 @@ final class DocumentPresenterTests: XCTestCase { sut.undo() XCTAssertFalse(document.artboard.frames.isEmpty) + + sut.redo() + XCTAssertTrue(document.artboard.frames.isEmpty) } // MARK: DSL @@ -133,6 +145,7 @@ final class DocumentPresenterTests: XCTestCase { } } +// MARK: - Undo extension DocumentPresenter { func disableUndoRegistration() { document.undo?.disableUndoRegistration() @@ -144,4 +157,8 @@ extension DocumentPresenter { func undo() { document.undo?.undo() } + + func redo() { + document.undo?.redo() + } }