mirror of
https://github.com/dscyrescotti/Memola.git
synced 2026-04-24 01:28:40 +02:00
feat: implement undo and redo logic
This commit is contained in:
@@ -70,6 +70,13 @@ final class GraphicContext: @unchecked Sendable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
previousElement = nil
|
previousElement = nil
|
||||||
|
case .photo(let photo):
|
||||||
|
tree.remove(photo.element, in: photo.photoBox)
|
||||||
|
withPersistence(\.backgroundContext) { [weak photo] context in
|
||||||
|
photo?.object?.element?.graphicContext = nil
|
||||||
|
try context.saveIfNeeded()
|
||||||
|
context.refreshAllObjects()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,6 +112,13 @@ final class GraphicContext: @unchecked Sendable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
previousElement = nil
|
previousElement = nil
|
||||||
|
case .photo(let photo):
|
||||||
|
tree.insert(photo.element, in: photo.photoBox)
|
||||||
|
withPersistence(\.backgroundContext) { [weak self, weak photo] context in
|
||||||
|
photo?.object?.element?.graphicContext = self?.object
|
||||||
|
try context.saveIfNeeded()
|
||||||
|
context.refreshAllObjects()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -304,7 +318,7 @@ extension GraphicContext {
|
|||||||
|
|
||||||
// MARK: - Photo
|
// MARK: - Photo
|
||||||
extension GraphicContext {
|
extension GraphicContext {
|
||||||
func insertPhoto(at point: CGPoint, photoItem: PhotoItem) {
|
func insertPhoto(at point: CGPoint, photoItem: PhotoItem) -> Photo {
|
||||||
let size = photoItem.dimension
|
let size = photoItem.dimension
|
||||||
let origin = point
|
let origin = point
|
||||||
let bounds = [origin.x - size.width / 2, origin.y - size.height / 2, origin.x + size.width / 2, origin.y + size.height / 2]
|
let bounds = [origin.x - size.width / 2, origin.y - size.height / 2, origin.x + size.width / 2, origin.y + size.height / 2]
|
||||||
@@ -331,6 +345,7 @@ extension GraphicContext {
|
|||||||
try context.saveIfNeeded()
|
try context.saveIfNeeded()
|
||||||
}
|
}
|
||||||
self.previousElement = .photo(photo)
|
self.previousElement = .photo(photo)
|
||||||
|
return photo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ extension Canvas {
|
|||||||
|
|
||||||
// MARK: - Photo
|
// MARK: - Photo
|
||||||
extension Canvas {
|
extension Canvas {
|
||||||
func insertPhoto(at point: CGPoint, photoItem: PhotoItem) {
|
func insertPhoto(at point: CGPoint, photoItem: PhotoItem) -> Photo {
|
||||||
graphicContext.insertPhoto(at: point, photoItem: photoItem)
|
graphicContext.insertPhoto(at: point, photoItem: photoItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,20 @@ class History: ObservableObject {
|
|||||||
try context.saveIfNeeded()
|
try context.saveIfNeeded()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case .photo(let _photo):
|
||||||
|
if let url = _photo.bookmark?.getBookmarkURL() {
|
||||||
|
do {
|
||||||
|
try FileManager.default.removeItem(at: url)
|
||||||
|
} catch {
|
||||||
|
NSLog("[Memola] - \(error.localizedDescription)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
withPersistence(\.backgroundContext) { context in
|
||||||
|
if let photo = _photo.object {
|
||||||
|
context.delete(photo)
|
||||||
|
}
|
||||||
|
try context.saveIfNeeded()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
redoStack.removeAll()
|
redoStack.removeAll()
|
||||||
|
|||||||
@@ -9,4 +9,5 @@ import Foundation
|
|||||||
|
|
||||||
enum HistoryEvent {
|
enum HistoryEvent {
|
||||||
case stroke(any Stroke)
|
case stroke(any Stroke)
|
||||||
|
case photo(Photo)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -238,7 +238,8 @@ extension CanvasViewController {
|
|||||||
tool.selectedPhotoItem = nil
|
tool.selectedPhotoItem = nil
|
||||||
}
|
}
|
||||||
let point = gesture.location(in: drawingView)
|
let point = gesture.location(in: drawingView)
|
||||||
canvas.insertPhoto(at: point.muliply(by: drawingView.ratio), photoItem: photoItem)
|
let photo = canvas.insertPhoto(at: point.muliply(by: drawingView.ratio), photoItem: photoItem)
|
||||||
|
history.addUndo(.photo(photo))
|
||||||
drawingView.draw()
|
drawingView.draw()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user