Revert "Remove CopyAsMarkdown" (#45101) (cherry-pick to preview) (#45102)

zed-zippy[bot] and Danilo Leal created

Cherry-pick of #45101 to preview

----
Reverts https://github.com/zed-industries/zed/pull/44933.

It turns out that if you're copying agent responses to paste it anywhere
else that isn't the message editor (e.g., for a follow up prompt),
getting Markdown formatting is helpful. However, with the revert, the
underlying issue in https://github.com/zed-industries/zed/issues/42958
remains, so I'll reopen that issue, unfortunately.

Release Notes:

- N/A

Co-authored-by: Danilo Leal <67129314+danilo-leal@users.noreply.github.com>

Change summary

assets/keymaps/default-linux.json   |  6 +++---
assets/keymaps/default-macos.json   |  2 +-
assets/keymaps/default-windows.json |  2 +-
crates/markdown/src/markdown.rs     | 18 ++++++++++++++++++
4 files changed, 23 insertions(+), 5 deletions(-)

Detailed changes

assets/keymaps/default-linux.json 🔗

@@ -262,9 +262,9 @@
   {
     "context": "AgentPanel > Markdown",
     "bindings": {
-      "copy": "markdown::Copy",
-      "ctrl-insert": "markdown::Copy",
-      "ctrl-c": "markdown::Copy",
+      "copy": "markdown::CopyAsMarkdown",
+      "ctrl-insert": "markdown::CopyAsMarkdown",
+      "ctrl-c": "markdown::CopyAsMarkdown",
     },
   },
   {

assets/keymaps/default-macos.json 🔗

@@ -303,7 +303,7 @@
     "context": "AgentPanel > Markdown",
     "use_key_equivalents": true,
     "bindings": {
-      "cmd-c": "markdown::Copy",
+      "cmd-c": "markdown::CopyAsMarkdown",
     },
   },
   {

assets/keymaps/default-windows.json 🔗

@@ -265,7 +265,7 @@
     "context": "AgentPanel > Markdown",
     "use_key_equivalents": true,
     "bindings": {
-      "ctrl-c": "markdown::Copy",
+      "ctrl-c": "markdown::CopyAsMarkdown",
     },
   },
   {

crates/markdown/src/markdown.rs 🔗

@@ -151,6 +151,8 @@ actions!(
     [
         /// Copies the selected text to the clipboard.
         Copy,
+        /// Copies the selected text as markdown to the clipboard.
+        CopyAsMarkdown
     ]
 );
 
@@ -295,6 +297,14 @@ impl Markdown {
         cx.write_to_clipboard(ClipboardItem::new_string(text));
     }
 
+    fn copy_as_markdown(&self, _: &mut Window, cx: &mut Context<Self>) {
+        if self.selection.end <= self.selection.start {
+            return;
+        }
+        let text = self.source[self.selection.start..self.selection.end].to_string();
+        cx.write_to_clipboard(ClipboardItem::new_string(text));
+    }
+
     fn parse(&mut self, cx: &mut Context<Self>) {
         if self.source.is_empty() {
             return;
@@ -1356,6 +1366,14 @@ impl Element for MarkdownElement {
                 }
             }
         });
+        window.on_action(std::any::TypeId::of::<crate::CopyAsMarkdown>(), {
+            let entity = self.markdown.clone();
+            move |_, phase, window, cx| {
+                if phase == DispatchPhase::Bubble {
+                    entity.update(cx, move |this, cx| this.copy_as_markdown(window, cx))
+                }
+            }
+        });
 
         self.paint_mouse_listeners(hitbox, &rendered_markdown.text, window, cx);
         rendered_markdown.element.paint(window, cx);