markdown: Move open_url
to the MarkdownElement
as on_url_click
(#28269)
Release Notes: - N/A --------- Co-authored-by: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
parent
d3abc61728
commit
0414908c4a
6 changed files with 201 additions and 194 deletions
|
@ -80,7 +80,6 @@ pub struct Markdown {
|
|||
focus_handle: FocusHandle,
|
||||
language_registry: Option<Arc<LanguageRegistry>>,
|
||||
fallback_code_block_language: Option<String>,
|
||||
open_url: Option<Box<dyn Fn(SharedString, &mut Window, &mut App)>>,
|
||||
options: Options,
|
||||
copied_code_blocks: HashSet<ElementId>,
|
||||
}
|
||||
|
@ -116,23 +115,12 @@ impl Markdown {
|
|||
parse_links_only: false,
|
||||
copy_code_block_buttons: true,
|
||||
},
|
||||
open_url: None,
|
||||
copied_code_blocks: HashSet::new(),
|
||||
};
|
||||
this.parse(cx);
|
||||
this
|
||||
}
|
||||
|
||||
pub fn open_url(
|
||||
self,
|
||||
open_url: impl Fn(SharedString, &mut Window, &mut App) + 'static,
|
||||
) -> Self {
|
||||
Self {
|
||||
open_url: Some(Box::new(open_url)),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_text(source: SharedString, cx: &mut Context<Self>) -> Self {
|
||||
let focus_handle = cx.focus_handle();
|
||||
let mut this = Self {
|
||||
|
@ -150,7 +138,6 @@ impl Markdown {
|
|||
parse_links_only: true,
|
||||
copy_code_block_buttons: true,
|
||||
},
|
||||
open_url: None,
|
||||
copied_code_blocks: HashSet::new(),
|
||||
};
|
||||
this.parse(cx);
|
||||
|
@ -328,11 +315,24 @@ impl ParsedMarkdown {
|
|||
pub struct MarkdownElement {
|
||||
markdown: Entity<Markdown>,
|
||||
style: MarkdownStyle,
|
||||
on_url_click: Option<Box<dyn Fn(SharedString, &mut Window, &mut App)>>,
|
||||
}
|
||||
|
||||
impl MarkdownElement {
|
||||
pub fn new(markdown: Entity<Markdown>, style: MarkdownStyle) -> Self {
|
||||
Self { markdown, style }
|
||||
Self {
|
||||
markdown,
|
||||
style,
|
||||
on_url_click: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_url_click(
|
||||
mut self,
|
||||
handler: impl Fn(SharedString, &mut Window, &mut App) + 'static,
|
||||
) -> Self {
|
||||
self.on_url_click = Some(Box::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
fn paint_selection(
|
||||
|
@ -404,7 +404,7 @@ impl MarkdownElement {
|
|||
}
|
||||
|
||||
fn paint_mouse_listeners(
|
||||
&self,
|
||||
&mut self,
|
||||
hitbox: &Hitbox,
|
||||
rendered_text: &RenderedText,
|
||||
window: &mut Window,
|
||||
|
@ -422,6 +422,8 @@ impl MarkdownElement {
|
|||
window.set_cursor_style(CursorStyle::IBeam, Some(hitbox));
|
||||
}
|
||||
|
||||
let on_open_url = self.on_url_click.take();
|
||||
|
||||
self.on_mouse_event(window, cx, {
|
||||
let rendered_text = rendered_text.clone();
|
||||
let hitbox = hitbox.clone();
|
||||
|
@ -493,7 +495,7 @@ impl MarkdownElement {
|
|||
if phase.bubble() {
|
||||
if let Some(pressed_link) = markdown.pressed_link.take() {
|
||||
if Some(&pressed_link) == rendered_text.link_for_position(event.position) {
|
||||
if let Some(open_url) = markdown.open_url.as_mut() {
|
||||
if let Some(open_url) = on_open_url.as_ref() {
|
||||
open_url(pressed_link.destination_url, window, cx);
|
||||
} else {
|
||||
cx.open_url(&pressed_link.destination_url);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue