Skip to content

Commit

Permalink
use leptos callback proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
rambip committed Oct 2, 2023
1 parent bc189e3 commit 366fe7e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use web_sys::MouseEvent;
use pulldown_cmark_wikilink::{ParserOffsetIter, Options, LinkType, Event};

mod utils;
use utils::{Callback, HtmlCallback};

mod component;

use core::ops::Range;
Expand Down Expand Up @@ -92,7 +90,7 @@ pub fn Markdown(

///
#[prop(optional, into)]
render_links: Option<HtmlCallback<LinkDescription>>,
render_links: Option<Callback<LinkDescription, HtmlElement<AnyElement>>>,

/// the name of the theme used for syntax highlighting.
/// Only the default themes of [syntect::Theme] are supported
Expand Down
17 changes: 9 additions & 8 deletions src/render.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use leptos::*;
use leptos::html::AnyElement;
use core::ops::Range;

use katex;
Expand All @@ -9,7 +10,7 @@ use web_sys::MouseEvent;

use pulldown_cmark_wikilink::{Event, Tag, TagEnd, CodeBlockKind, Alignment, MathMode, HeadingLevel};

use crate::utils::{as_closing_tag, Callback, HtmlCallback};
use crate::utils::as_closing_tag;
use super::{LinkDescription, MarkdownMouseEvent, ComponentMap, MdComponentProps};

use super::component::ComponentCall;
Expand All @@ -24,7 +25,7 @@ pub fn make_callback(context: &RenderContext, position: Range<usize>)
mouse_event: x,
position: position.clone()
};
onclick.call(click_event)
onclick(click_event)
}
}

Expand All @@ -43,7 +44,7 @@ pub struct RenderContext {
onclick: Callback<MarkdownMouseEvent>,

/// callback used to render links
render_links: Option<HtmlCallback<LinkDescription>>,
render_links: Option<Callback<LinkDescription, HtmlElement<AnyElement>>>,

/// components
components: ComponentMap
Expand All @@ -54,7 +55,7 @@ impl RenderContext
{
pub fn new(theme_name: Option<String>,
onclick: Option<Callback<MarkdownMouseEvent>>,
render_links: Option<HtmlCallback<LinkDescription>>,
render_links: Option<Callback<LinkDescription, HtmlElement<AnyElement>>>,
components: ComponentMap)
-> Self
{
Expand Down Expand Up @@ -219,14 +220,14 @@ where I: Iterator<Item=(Event<'a>, Range<usize>)>
};
let children = sub_renderer.collect_view();
Ok(
comp.call(MdComponentProps{
comp(MdComponentProps{
attributes: description.attributes,
children
}))
}
else {
Ok(
comp.call(MdComponentProps{
comp(MdComponentProps{
attributes: description.attributes,
children: ().into_view()
})
Expand Down Expand Up @@ -343,7 +344,7 @@ fn render_tasklist_marker(context: &RenderContext, m: bool, position: Range<usiz
mouse_event: e,
position: position.clone()
};
onclick.call(click_event)
onclick(click_event)
};
view!{
<input type="checkbox" checked=m on:click=callback>
Expand Down Expand Up @@ -463,7 +464,7 @@ fn render_link(context: &RenderContext, link: LinkDescription)
-> Result<View, HtmlError>
{
match (&context.render_links, link.image) {
(Some(f), _) => Ok(f.call(link).into_view()),
(Some(f), _) => Ok(f(link).into_view()),
(None, false) => Ok(view!{
<a href={link.url}>
{link.content}
Expand Down
47 changes: 0 additions & 47 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use pulldown_cmark_wikilink::{Tag, TagEnd};
use std::rc::Rc;

use leptos::html::{HtmlElement, AnyElement, ElementDescriptor};

pub fn as_closing_tag(t: &Tag) -> TagEnd {
match t {
Expand All @@ -24,47 +21,3 @@ pub fn as_closing_tag(t: &Tag) -> TagEnd {
Tag::MetadataBlock(k) => TagEnd::MetadataBlock(*k),
}
}

#[derive(Clone)]
pub struct Callback<In,Out=()>(Rc<dyn Fn(In) -> Out>);

impl <In,Out> Callback<In,Out> {
pub fn new<F: Fn(In) -> Out + 'static>(f: F) -> Self {
Callback(Rc::new(f))
}

pub fn call(&self, value: In) -> Out {
self.0(value)
}
}

impl<In,Out,F> From<F> for Callback<In,Out>
where F: Fn(In) -> Out + 'static {
fn from(value: F) -> Callback<In,Out> {
Callback::new(value)
}
}

#[derive(Clone)]
pub struct HtmlCallback<In>(Rc<dyn Fn(In) -> HtmlElement<AnyElement>>);

impl<In> HtmlCallback<In> {
pub fn new<F, H>(f: F) -> Self
where H: ElementDescriptor + 'static,
F: Fn(In) -> HtmlElement<H> + 'static
{
HtmlCallback(Rc::new(move |x| f(x).into_any()))
}

pub fn call(&self, value: In) -> HtmlElement<AnyElement> {
self.0(value)
}
}

impl<In,D,F> From<F> for HtmlCallback<In>
where F: Fn(In) -> HtmlElement<D> + 'static,
D: ElementDescriptor + 'static {
fn from(value: F) -> HtmlCallback<In> {
HtmlCallback::new(value)
}
}

0 comments on commit 366fe7e

Please sign in to comment.