Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 48 additions & 12 deletions crates/markdown/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ pub struct MarkdownElement {
markdown: Entity<Markdown>,
style: MarkdownStyle,
code_block_renderer: CodeBlockRenderer,
on_url_click: Option<Box<dyn Fn(SharedString, &mut Window, &mut App)>>,
on_url_click: Option<Rc<dyn Fn(SharedString, &mut Window, &mut App)>>,
code_span_link: Option<CodeSpanLinkCallback>,
on_source_click: Option<SourceClickCallback>,
on_checkbox_toggle: Option<CheckboxToggleCallback>,
Expand Down Expand Up @@ -1244,7 +1244,7 @@ impl MarkdownElement {
mut self,
handler: impl Fn(SharedString, &mut Window, &mut App) + 'static,
) -> Self {
self.on_url_click = Some(Box::new(handler));
self.on_url_click = Some(Rc::new(handler));
self
}

Expand Down Expand Up @@ -1344,16 +1344,52 @@ impl MarkdownElement {
) {
builder.modify_current_div(|el| el.flex().flex_row().flex_wrap().items_start());

let image_element = div().min_w_0().child(
img(source)
.id(("markdown-image", range.start))
.min_w_0()
.max_w_full()
.rounded_md()
.when_some(height, |this, height| this.h(height))
.when_some(width, |this, width| this.w(width))
.with_fallback(move || image_fallback_element(dest_url.clone(), alt_text.clone())),
);
let image_element = {
let wrapper = div()
.id(("markdown-image-link", range.start))
.min_w_0();
let wrapper = if let Some(link) = (builder.link_depth > 0)
.then(|| builder.rendered_links.last())
.flatten()
&& !self.style.prevent_mouse_interaction
{
let url = link.destination_url.clone();
let markdown = self.markdown.clone();
let url_click = self.on_url_click.clone();
wrapper
.cursor_pointer()
.on_click({
let url = url.clone();
let url_click = url_click.clone();
move |_, window, cx| {
if let Some(ref on_url_click) = url_click {
on_url_click(url.clone(), window, cx);
} else {
cx.open_url(&url);
}
}
})
.capture_any_mouse_down(move |event, _window, cx| {
if event.button == MouseButton::Right {
markdown.update(cx, |md, _| {
md.capture_for_context_menu(Some(url.clone()))
});
}
})
} else {
wrapper
};
wrapper.child(
img(source)
.id(("markdown-image", range.start))
.min_w_0()
.max_w_full()
.rounded_md()
.when_some(height, |this, height| this.h(height))
.when_some(width, |this, width| this.w(width))
.with_fallback(move || image_fallback_element(dest_url.clone(), alt_text.clone())),
)
};

builder.push_image_child(image_element);
}
Expand Down
Loading