feat: add redo and undo commands

This commit is contained in:
dscyrescotti
2024-07-14 18:16:13 +07:00
parent c1e2fa227e
commit bd5701cf88
4 changed files with 53 additions and 40 deletions

View File

@@ -15,15 +15,11 @@ final class Application: NSObject, ObservableObject {
extension Application {
func openMemo(_ memoObject: MemoObject?) {
withAnimation(.easeOut) {
self.memoObject = memoObject
}
self.memoObject = memoObject
}
func closeMemo() {
withAnimation(.easeOut) {
self.memoObject = nil
}
self.memoObject = nil
}
}

View File

@@ -19,32 +19,40 @@ struct DashboardView: View {
var body: some View {
#if os(macOS)
NavigationSplitView(columnVisibility: $columnVisibility) {
Sidebar(sidebarItem: $sidebarItem, horizontalSizeClass: horizontalSizeClass)
} detail: {
switch sidebarItem {
case .memos:
MemosView()
case .trash:
TrashView(sidebarItem: $sidebarItem)
default:
MemosView()
ZStack {
if let memo = application.memoObject {
NavigationStack {
MemoView(memo: memo)
.onDisappear {
withPersistence(\.viewContext) { context in
try context.saveIfNeeded()
context.refreshAllObjects()
}
}
.navigationTitle("")
}
.transition(.opacity)
.matchedGeometryEffect(id: "pop-up", in: namespace)
} else {
NavigationSplitView(columnVisibility: $columnVisibility) {
Sidebar(sidebarItem: $sidebarItem, horizontalSizeClass: horizontalSizeClass)
} detail: {
switch sidebarItem {
case .memos:
MemosView()
case .trash:
TrashView(sidebarItem: $sidebarItem)
default:
MemosView()
}
}
.transition(.opacity)
.matchedGeometryEffect(id: "pop-up", in: namespace)
}
}
.animation(.easeIn, value: application.memoObject)
.toolbar(application.memoObject == nil ? .visible : .hidden, for: .windowToolbar)
.toolbarBackground(application.memoObject == nil ? .clear : Color(nsColor: .windowBackgroundColor), for: .windowToolbar)
.overlay {
if let memo = application.memoObject {
MemoView(memo: memo)
.onDisappear {
withPersistence(\.viewContext) { context in
try context.saveIfNeeded()
context.refreshAllObjects()
}
}
.transition(.move(edge: .bottom))
}
}
.onChange(of: columnVisibility) { oldValue, newValue in
application.changeSidebarVisibility(newValue == .all ? .shown : .hidden)
}

View File

@@ -61,6 +61,8 @@ struct MemoView: View {
loadingIndicator("Loading photo...")
}
}
.focusedSceneObject(history)
.focusedSceneValue(\.activeSceneKey, .memo)
}
private var canvasView: some View {

View File

@@ -8,21 +8,28 @@
import SwiftUI
struct EditCommands: Commands {
@FocusedValue(\.activeSceneKey) private var appScene
@FocusedObject var history: History?
var body: some Commands {
CommandGroup(replacing: .undoRedo) {
// memo view
Button {
} label: {
Text("Undo")
if appScene == .memo, let history {
Button {
history.historyPublisher.send(.undo)
} label: {
Text("Undo")
}
.keyboardShortcut("z", modifiers: [.command])
.disabled(history.undoDisabled)
Button {
history.historyPublisher.send(.redo)
} label: {
Text("Redo")
}
.keyboardShortcut("z", modifiers: [.command, .shift])
.disabled(history.redoDisabled)
}
.keyboardShortcut("z", modifiers: [.command])
Button {
} label: {
Text("Redo")
}
.keyboardShortcut("z", modifiers: [.command, .shift])
}
CommandGroup(replacing: .pasteboard) { }
}