From 9aea81e0357fcaa2ffc7a8ab6eb131be492a5763 Mon Sep 17 00:00:00 2001 From: 0x8664b2 <0x8664b2@pm.me> Date: Fri, 20 Jun 2025 17:47:44 -0700 Subject: [PATCH] Undo WIP --- MarkdownViewer/ContentView.swift | 5 ++++- MarkdownViewer/MarkdownViewerApp.swift | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/MarkdownViewer/ContentView.swift b/MarkdownViewer/ContentView.swift index 201ee0e..68fd240 100644 --- a/MarkdownViewer/ContentView.swift +++ b/MarkdownViewer/ContentView.swift @@ -185,8 +185,10 @@ struct MarkdownEditor: NSViewRepresentable { textView.font = NSFont.monospacedSystemFont(ofSize: 14, weight: .regular) textView.textColor = NSColor.textColor textView.backgroundColor = NSColor.textBackgroundColor + textView.allowsUndo = true textView.string = text + // Properly set up the scroll view hierarchy first scrollView.borderType = .noBorder scrollView.hasVerticalScroller = true @@ -217,6 +219,7 @@ struct MarkdownEditor: NSViewRepresentable { if textView.string != text { print("MarkdownEditor: Updating text from \(textView.string.count) to \(text.count) characters") + textView.string = text } } @@ -244,4 +247,4 @@ struct MarkdownEditor: NSViewRepresentable { #Preview { ContentView() -} \ No newline at end of file +} diff --git a/MarkdownViewer/MarkdownViewerApp.swift b/MarkdownViewer/MarkdownViewerApp.swift index ba1b5ca..f6ae021 100644 --- a/MarkdownViewer/MarkdownViewerApp.swift +++ b/MarkdownViewer/MarkdownViewerApp.swift @@ -62,6 +62,20 @@ struct MarkdownViewerApp: App { } } } + + CommandGroup(replacing: .undoRedo) { + Button("Undo") { + print("Menu: Undo clicked, sending action to: \(NSApp.keyWindow?.firstResponder?.className ?? "nil")") + NSApp.sendAction(#selector(UndoManager.undo), to: nil, from: nil) + } + .keyboardShortcut("z", modifiers: .command) + + Button("Redo") { + print("Menu: Redo clicked, sending action to: \(NSApp.keyWindow?.firstResponder?.className ?? "nil")") + NSApp.sendAction(#selector(UndoManager.redo), to: nil, from: nil) + } + .keyboardShortcut("z", modifiers: [.command, .shift]) + } } }