Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Add item for opening Markdown/SVG files in preview tab in right-click menu (#47821)
Danilo Leale752ec1a9264
diff --git a/Cargo.lock b/Cargo.lockindex 0989967dd56..fdc2a0c2241 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -9857,6 +9857,7 @@ dependencies = ["urlencoding","util","workspace",+ "zed_actions",][[package]]@@ -16155,6 +16156,7 @@ dependencies = ["multi_buffer","ui","workspace",+ "zed_actions",][[package]]diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rsindex 14b736acff6..047be5b9f29 100644--- a/crates/editor/src/items.rs+++ b/crates/editor/src/items.rs@@ -59,6 +59,9 @@ use workspace::{item::{BreadcrumbText, FollowEvent, ProjectItemKind},searchable::SearchOptions,};+use zed_actions::preview::{+ markdown::OpenPreview as OpenMarkdownPreview, svg::OpenPreview as OpenSvgPreview,+};pub const MAX_TAB_TITLE_LEN: usize = 24;@@ -1033,6 +1036,48 @@ impl Item for Editor {}}+ fn tab_extra_context_menu_actions(+ &self,+ _window: &mut Window,+ cx: &mut Context<Self>,+ ) -> Vec<(SharedString, Box<dyn gpui::Action>)> {+ let mut actions = Vec::new();++ let is_markdown = self+ .buffer()+ .read(cx)+ .as_singleton()+ .and_then(|buffer| buffer.read(cx).language())+ .is_some_and(|language| language.name().as_ref() == "Markdown");++ let is_svg = self+ .buffer()+ .read(cx)+ .as_singleton()+ .and_then(|buffer| buffer.read(cx).file())+ .is_some_and(|file| {+ std::path::Path::new(file.file_name(cx))+ .extension()+ .is_some_and(|ext| ext.eq_ignore_ascii_case("svg"))+ });++ if is_markdown {+ actions.push((+ "Open Markdown Preview".into(),+ Box::new(OpenMarkdownPreview) as Box<dyn gpui::Action>,+ ));+ }++ if is_svg {+ actions.push((+ "Open SVG Preview".into(),+ Box::new(OpenSvgPreview) as Box<dyn gpui::Action>,+ ));+ }++ actions+ }+fn preserve_preview(&self, cx: &App) -> bool {self.buffer.read(cx).preserve_preview(cx)}diff --git a/crates/editor/src/mouse_context_menu.rs b/crates/editor/src/mouse_context_menu.rsindex ae1d81da505..2cc050dae8b 100644--- a/crates/editor/src/mouse_context_menu.rs+++ b/crates/editor/src/mouse_context_menu.rs@@ -14,6 +14,9 @@ use std::ops::Range;use text::PointUtf16;use workspace::OpenInTerminal;use zed_actions::agent::AddSelectionToThread;+use zed_actions::preview::{+ markdown::OpenPreview as OpenMarkdownPreview, svg::OpenPreview as OpenSvgPreview,+};#[derive(Debug)]pub enum MenuPosition {@@ -218,6 +221,24 @@ pub fn deploy_context_menu(let run_to_cursor = window.is_action_available(&RunToCursor, cx);let disable_ai = DisableAiSettings::get_global(cx).disable_ai;+ let is_markdown = editor+ .buffer()+ .read(cx)+ .as_singleton()+ .and_then(|buffer| buffer.read(cx).language())+ .is_some_and(|language| language.name().as_ref() == "Markdown");++ let is_svg = editor+ .buffer()+ .read(cx)+ .as_singleton()+ .and_then(|buffer| buffer.read(cx).file())+ .is_some_and(|file| {+ std::path::Path::new(file.file_name(cx))+ .extension()+ .is_some_and(|ext| ext.eq_ignore_ascii_case("svg"))+ });+ui::ContextMenu::build(window, cx, |menu, _window, _cx| {let builder = menu.on_blur_subscription(Subscription::new(|| {}))@@ -272,6 +293,12 @@ pub fn deploy_context_menu(},Box::new(RevealInFileManager),)+ .when(is_markdown, |builder| {+ builder.action("Open Markdown Preview", Box::new(OpenMarkdownPreview))+ })+ .when(is_svg, |builder| {+ builder.action("Open SVG Preview", Box::new(OpenSvgPreview))+ }).action_disabled_when(!has_reveal_target,"Open in Terminal",diff --git a/crates/markdown_preview/Cargo.toml b/crates/markdown_preview/Cargo.tomlindex d61ec00cc8c..c9cce94de1f 100644--- a/crates/markdown_preview/Cargo.toml+++ b/crates/markdown_preview/Cargo.toml@@ -34,6 +34,7 @@ ui.workspace = trueurlencoding.workspace = trueutil.workspace = trueworkspace.workspace = true+zed_actions.workspace = true[dev-dependencies]editor = { workspace = true, features = ["test-support"] }diff --git a/crates/markdown_preview/src/markdown_preview.rs b/crates/markdown_preview/src/markdown_preview.rsindex 61c99764add..c7e8e9e9272 100644--- a/crates/markdown_preview/src/markdown_preview.rs+++ b/crates/markdown_preview/src/markdown_preview.rs@@ -7,6 +7,8 @@ pub mod markdown_parser;pub mod markdown_preview_view;pub mod markdown_renderer;+pub use zed_actions::preview::markdown::{OpenPreview, OpenPreviewToTheSide};+actions!(markdown,[@@ -24,10 +26,6 @@ actions!(ScrollUpByItem,/// Scrolls down by one markdown element in the markdown previewScrollDownByItem,- /// Opens a markdown preview for the current file.- OpenPreview,- /// Opens a markdown preview in a split pane.- OpenPreviewToTheSide,/// Opens a following markdown preview that syncs with the editor.OpenFollowingPreview]diff --git a/crates/svg_preview/Cargo.toml b/crates/svg_preview/Cargo.tomlindex 18f55e28d5d..9ee085ee073 100644--- a/crates/svg_preview/Cargo.toml+++ b/crates/svg_preview/Cargo.toml@@ -18,3 +18,4 @@ gpui.workspace = truelanguage.workspace = trueui.workspace = trueworkspace.workspace = true+zed_actions.workspace = truediff --git a/crates/svg_preview/src/svg_preview.rs b/crates/svg_preview/src/svg_preview.rsindex ca1891394d6..060639db5fa 100644--- a/crates/svg_preview/src/svg_preview.rs+++ b/crates/svg_preview/src/svg_preview.rs@@ -3,13 +3,11 @@ use workspace::Workspace;pub mod svg_preview_view;+pub use zed_actions::preview::svg::{OpenPreview, OpenPreviewToTheSide};+actions!(svg,[- /// Opens an SVG preview for the current file.- OpenPreview,- /// Opens an SVG preview in a split pane.- OpenPreviewToTheSide,/// Opens a following SVG preview that syncs with the editor.OpenFollowingPreview]diff --git a/crates/zed_actions/src/lib.rs b/crates/zed_actions/src/lib.rsindex 1d3e8fabeec..84746cfb03e 100644--- a/crates/zed_actions/src/lib.rs+++ b/crates/zed_actions/src/lib.rs@@ -651,3 +651,33 @@ pub mod wsl_actions {pub create_new_window: bool,}}++pub mod preview {+ pub mod markdown {+ use gpui::actions;++ actions!(+ markdown,+ [+ /// Opens a markdown preview for the current file.+ OpenPreview,+ /// Opens a markdown preview in a split pane.+ OpenPreviewToTheSide,+ ]+ );+ }++ pub mod svg {+ use gpui::actions;++ actions!(+ svg,+ [+ /// Opens an SVG preview for the current file.+ OpenPreview,+ /// Opens an SVG preview in a split pane.+ OpenPreviewToTheSide,+ ]+ );+ }+}