Skip to content

Commit

Permalink
use latest api
Browse files Browse the repository at this point in the history
  • Loading branch information
rambip committed Nov 28, 2023
1 parent 4d02f32 commit dbc236c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
target
dist
result
.envrc
.direnv
64 changes: 32 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 38 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use rust_web_markdown::{
render_markdown, ElementAttributes, HtmlElement, Context,
MdComponentProps as VMdComponentProps
MdComponentProps as VMdComponentProps,
CowStr
};

pub type MdComponentProps = VMdComponentProps<View>;

pub use rust_web_markdown::{
LinkDescription, MarkdownMouseEvent, Options,
LinkDescription, Options,
};

use web_sys::MouseEvent;
Expand All @@ -15,6 +16,7 @@ use leptos::*;
use leptos::html::AnyElement;

use std::collections::HashMap;
use core::ops::Range;


#[cfg(feature="debug")]
Expand All @@ -24,6 +26,18 @@ pub mod debug {
pub struct EventInfo(pub WriteSignal<Vec<String>>);
}

#[derive(Clone, Debug)]
pub struct MarkdownMouseEvent {
/// the original mouse event triggered when a text element was clicked on
pub mouse_event: MouseEvent,

/// the corresponding range in the markdown source, as a slice of [`u8`][u8]
pub position: Range<usize>,

// TODO: add a clonable tag for the type of the element
// pub tag: pulldown_cmark::Tag<'a>,
}

impl<'a> Context<'a, 'static> for &'a __MdProps {
type View = View;

Expand All @@ -39,7 +53,6 @@ impl<'a> Context<'a, 'static> for &'a __MdProps {
frontmatter: self.frontmatter.as_ref(),
hard_line_breaks: self.hard_line_breaks.get(),
wikilinks: self.wikilinks.get(),
on_click: self.on_click.as_ref(),
parse_options: self.parse_options.as_ref(),
render_links: self.render_links.as_ref(),
theme: self.theme.as_deref(),
Expand Down Expand Up @@ -133,15 +146,15 @@ impl<'a> Context<'a, 'static> for &'a __MdProps {
children.into_iter().collect()
}

fn el_a(self, children: Self::View, href: &str) -> Self::View {
fn el_a(self, children: Self::View, href: String) -> Self::View {
view! {<a href={href.to_string()}>{children}</a>}.into_view()
}

fn el_img(self, src: &str, alt: &str) -> Self::View {
fn el_img(self, src: String, alt: String) -> Self::View {
view! {<img src={src.to_string()} alt={alt.to_string()}/>}.into_view()
}

fn el_text(self, text: &str) -> Self::View {
fn el_text(self, text: CowStr<'a>) -> Self::View {
text.to_string().into_view()
}

Expand Down Expand Up @@ -192,6 +205,25 @@ impl<'a> Context<'a, 'static> for &'a __MdProps {
) -> Self::Handler<T> {
Callback::new(f)
}

fn make_md_handler(self, position: Range<usize>, stop_propagation: bool) -> Self::Handler<MouseEvent> {
match self.on_click {
Some(f) => {
let position = position.clone();
Callback::new(move |e: MouseEvent| {
if stop_propagation {
e.stop_propagation()
}
let report = MarkdownMouseEvent {
position: position.clone(),
mouse_event: e
};
Callable::call(&f, report)
})
}
None => Callback::new(move |_| ())
}
}
}


Expand Down

0 comments on commit dbc236c

Please sign in to comment.