mirror of
https://github.com/adoyle0/thaw.git
synced 2025-02-02 08:34:15 -05:00
Use fully qualified syntax in the example of Drawer (#187)
* perf: replace `expect` with `unwrap_or_else` The macro/functions in `expect` is not lazy, which means it will always be called * style: remove needless borrowing * perf: remove needless clone * style: remove needless `format!` * style: use `and_then` instead * style: use `?` instead * style: remove needless closure * fix: use fully qualified syntax instead https://github.com/rust-lang/rust/issues/48919 * style: formatted * style: fix some other clippy issues
This commit is contained in:
parent
51bfb3c05a
commit
18658044c2
13 changed files with 36 additions and 34 deletions
|
@ -4,7 +4,6 @@ use leptos::*;
|
||||||
use thaw::mobile::{NavBar, NavBarRight};
|
use thaw::mobile::{NavBar, NavBarRight};
|
||||||
use thaw::Icon;
|
use thaw::Icon;
|
||||||
|
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn NavBarPage() -> impl IntoView {
|
pub fn NavBarPage() -> impl IntoView {
|
||||||
view! {
|
view! {
|
||||||
|
|
|
@ -13,10 +13,10 @@ let open = Callback::new(move |new_placement: DrawerPlacement| {
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
<ButtonGroup>
|
<ButtonGroup>
|
||||||
<Button on_click=Callback::new(move |_| open.call(DrawerPlacement::Top))>"Top"</Button>
|
<Button on_click=Callback::new(move |_| leptos::Callable::call(&open, DrawerPlacement::Top))>"Top"</Button>
|
||||||
<Button on_click=Callback::new(move |_| open.call(DrawerPlacement::Right))>"Right"</Button>
|
<Button on_click=Callback::new(move |_| leptos::Callable::call(&open, DrawerPlacement::Right))>"Right"</Button>
|
||||||
<Button on_click=Callback::new(move |_| open.call(DrawerPlacement::Bottom))>"Bottom"</Button>
|
<Button on_click=Callback::new(move |_| leptos::Callable::call(&open, DrawerPlacement::Bottom))>"Bottom"</Button>
|
||||||
<Button on_click=Callback::new(move |_| open.call(DrawerPlacement::Left))>"Left"</Button>
|
<Button on_click=Callback::new(move |_| leptos::Callable::call(&open, DrawerPlacement::Left))>"Left"</Button>
|
||||||
</ButtonGroup>
|
</ButtonGroup>
|
||||||
<Drawer title="Title" show placement>
|
<Drawer title="Title" show placement>
|
||||||
"Hello"
|
"Hello"
|
||||||
|
|
|
@ -96,7 +96,7 @@ pub fn include_md(_token_stream: proc_macro::TokenStream) -> proc_macro::TokenSt
|
||||||
links
|
links
|
||||||
);
|
);
|
||||||
syn::parse_str::<ItemFn>(&toc)
|
syn::parse_str::<ItemFn>(&toc)
|
||||||
.expect(&format!("Cannot be resolved as a function: \n {toc}"))
|
.unwrap_or_else(|_| panic!("Cannot be resolved as a function: \n {toc}"))
|
||||||
};
|
};
|
||||||
|
|
||||||
let demos: Vec<ItemFn> = demos
|
let demos: Vec<ItemFn> = demos
|
||||||
|
|
|
@ -18,8 +18,7 @@ pub fn to_tokens(code_block: &NodeCodeBlock, demos: &mut Vec<String>) -> TokenSt
|
||||||
let literal = langs
|
let literal = langs
|
||||||
.iter()
|
.iter()
|
||||||
.find(|lang| lang != &&"demo")
|
.find(|lang| lang != &&"demo")
|
||||||
.map(|lang| highlight_to_html(&code_block.literal, lang))
|
.and_then(|lang| highlight_to_html(&code_block.literal, lang))
|
||||||
.flatten()
|
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
is_highlight = false;
|
is_highlight = false;
|
||||||
code_block.literal.clone()
|
code_block.literal.clone()
|
||||||
|
@ -37,8 +36,7 @@ pub fn to_tokens(code_block: &NodeCodeBlock, demos: &mut Vec<String>) -> TokenSt
|
||||||
let mut is_highlight = true;
|
let mut is_highlight = true;
|
||||||
let literal = langs
|
let literal = langs
|
||||||
.first()
|
.first()
|
||||||
.map(|lang| highlight_to_html(&code_block.literal, lang))
|
.and_then(|lang| highlight_to_html(&code_block.literal, lang))
|
||||||
.flatten()
|
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
is_highlight = false;
|
is_highlight = false;
|
||||||
code_block.literal.clone()
|
code_block.literal.clone()
|
||||||
|
@ -56,10 +54,8 @@ pub fn to_tokens(code_block: &NodeCodeBlock, demos: &mut Vec<String>) -> TokenSt
|
||||||
static SYNTAX_SET: OnceLock<SyntaxSet> = OnceLock::new();
|
static SYNTAX_SET: OnceLock<SyntaxSet> = OnceLock::new();
|
||||||
|
|
||||||
fn highlight_to_html(text: &str, syntax: &str) -> Option<String> {
|
fn highlight_to_html(text: &str, syntax: &str) -> Option<String> {
|
||||||
let syntax_set = SYNTAX_SET.get_or_init(|| SyntaxSet::load_defaults_newlines());
|
let syntax_set = SYNTAX_SET.get_or_init(SyntaxSet::load_defaults_newlines);
|
||||||
let Some(syntax) = syntax_set.find_syntax_by_token(syntax) else {
|
let syntax = syntax_set.find_syntax_by_token(syntax)?;
|
||||||
return None;
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut html_generator = ClassedHTMLGenerator::new_with_class_style(
|
let mut html_generator = ClassedHTMLGenerator::new_with_class_style(
|
||||||
syntax,
|
syntax,
|
||||||
|
|
|
@ -8,7 +8,10 @@ use proc_macro2::{Ident, Span, TokenStream};
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::ItemMacro;
|
use syn::ItemMacro;
|
||||||
|
|
||||||
pub fn parse_markdown(md_text: &str) -> Result<(TokenStream, Vec<String>, Vec<(String, String)>), String> {
|
#[allow(clippy::type_complexity)]
|
||||||
|
pub fn parse_markdown(
|
||||||
|
md_text: &str,
|
||||||
|
) -> Result<(TokenStream, Vec<String>, Vec<(String, String)>), String> {
|
||||||
let mut demos: Vec<String> = vec![];
|
let mut demos: Vec<String> = vec![];
|
||||||
let mut toc: Vec<(String, String)> = vec![];
|
let mut toc: Vec<(String, String)> = vec![];
|
||||||
|
|
||||||
|
@ -16,7 +19,7 @@ pub fn parse_markdown(md_text: &str) -> Result<(TokenStream, Vec<String>, Vec<(S
|
||||||
let mut options = comrak::Options::default();
|
let mut options = comrak::Options::default();
|
||||||
options.extension.table = true;
|
options.extension.table = true;
|
||||||
|
|
||||||
let root = parse_document(&arena, &md_text, &options);
|
let root = parse_document(&arena, md_text, &options);
|
||||||
let body = iter_nodes(md_text, root, &mut demos, &mut toc);
|
let body = iter_nodes(md_text, root, &mut demos, &mut toc);
|
||||||
Ok((body, demos, toc))
|
Ok((body, demos, toc))
|
||||||
}
|
}
|
||||||
|
@ -45,10 +48,12 @@ fn iter_nodes<'a>(
|
||||||
NodeValue::HtmlBlock(node_html_block) => {
|
NodeValue::HtmlBlock(node_html_block) => {
|
||||||
let html =
|
let html =
|
||||||
syn::parse_str::<ItemMacro>(&format!("view! {{ {} }}", node_html_block.literal))
|
syn::parse_str::<ItemMacro>(&format!("view! {{ {} }}", node_html_block.literal))
|
||||||
.expect(&format!(
|
.unwrap_or_else(|_| {
|
||||||
"Cannot be resolved as a macro: \n {}",
|
panic!(
|
||||||
node_html_block.literal
|
"Cannot be resolved as a macro: \n {}",
|
||||||
));
|
node_html_block.literal
|
||||||
|
)
|
||||||
|
});
|
||||||
quote!(
|
quote!(
|
||||||
{
|
{
|
||||||
#html
|
#html
|
||||||
|
@ -62,10 +67,10 @@ fn iter_nodes<'a>(
|
||||||
),
|
),
|
||||||
NodeValue::Heading(node_h) => {
|
NodeValue::Heading(node_h) => {
|
||||||
let sourcepos = node.data.borrow().sourcepos;
|
let sourcepos = node.data.borrow().sourcepos;
|
||||||
let text = range_text(md_text, sourcepos.start.clone(), sourcepos.end.clone());
|
let text = range_text(md_text, sourcepos.start, sourcepos.end);
|
||||||
let level = node_h.level as usize + 1;
|
let level = node_h.level as usize + 1;
|
||||||
let text = text[level..].to_string();
|
let text = text[level..].to_string();
|
||||||
let h_id = format!("{}", text.replace(' ', "-").to_ascii_lowercase());
|
let h_id = text.replace(' ', "-").to_ascii_lowercase().to_string();
|
||||||
toc.push((h_id.clone(), text));
|
toc.push((h_id.clone(), text));
|
||||||
let h = Ident::new(&format!("h{}", node_h.level), Span::call_site());
|
let h = Ident::new(&format!("h{}", node_h.level), Span::call_site());
|
||||||
quote!(
|
quote!(
|
||||||
|
@ -186,7 +191,7 @@ fn range_text(text: &str, start: LineColumn, end: LineColumn) -> &str {
|
||||||
let mut current_line_num = start_line + 1;
|
let mut current_line_num = start_line + 1;
|
||||||
while current_line_num < end_line {
|
while current_line_num < end_line {
|
||||||
let next_line = lines.next().unwrap_or("");
|
let next_line = lines.next().unwrap_or("");
|
||||||
start_line_text = &next_line;
|
start_line_text = next_line;
|
||||||
current_line_num += 1;
|
current_line_num += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,8 @@ use crate::{use_theme, Icon, Theme};
|
||||||
use leptos::{html::ToHtmlElement, *};
|
use leptos::{html::ToHtmlElement, *};
|
||||||
use thaw_components::{CSSTransition, Fallback, OptionComp, Teleport};
|
use thaw_components::{CSSTransition, Fallback, OptionComp, Teleport};
|
||||||
use thaw_utils::{
|
use thaw_utils::{
|
||||||
add_event_listener, class_list, get_scroll_parent, mount_style, EventListenerHandle, OptionalProp,
|
add_event_listener, class_list, get_scroll_parent, mount_style, EventListenerHandle,
|
||||||
|
OptionalProp,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
|
|
|
@ -3,7 +3,6 @@ use crate::theme::ThemeMethod;
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct BackTopTheme {
|
pub struct BackTopTheme {
|
||||||
pub background_color: String,
|
pub background_color: String,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ThemeMethod for BackTopTheme {
|
impl ThemeMethod for BackTopTheme {
|
||||||
|
|
|
@ -5,6 +5,12 @@ use std::{collections::HashSet, rc::Rc};
|
||||||
|
|
||||||
pub struct ClassList(RwSignal<HashSet<Oco<'static, str>>>);
|
pub struct ClassList(RwSignal<HashSet<Oco<'static, str>>>);
|
||||||
|
|
||||||
|
impl Default for ClassList {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ClassList {
|
impl ClassList {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self(RwSignal::new(HashSet::new()))
|
Self(RwSignal::new(HashSet::new()))
|
||||||
|
|
|
@ -4,9 +4,7 @@ use leptos::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn get_scroll_parent(element: &HtmlElement<AnyElement>) -> Option<HtmlElement<AnyElement>> {
|
pub fn get_scroll_parent(element: &HtmlElement<AnyElement>) -> Option<HtmlElement<AnyElement>> {
|
||||||
let Some(parent_element) = get_parent_element(element) else {
|
let parent_element = get_parent_element(element)?;
|
||||||
return None;
|
|
||||||
};
|
|
||||||
|
|
||||||
if parent_element.node_type() == 9 {
|
if parent_element.node_type() == 9 {
|
||||||
return Some(parent_element);
|
return Some(parent_element);
|
||||||
|
|
|
@ -12,9 +12,7 @@ pub fn use_click_position() -> ReadSignal<Option<(i32, i32)>> {
|
||||||
if event.client_x() > 0 || event.client_y() > 0 {
|
if event.client_x() > 0 || event.client_y() > 0 {
|
||||||
return Some((event.client_x(), event.client_y()));
|
return Some((event.client_x(), event.client_y()));
|
||||||
}
|
}
|
||||||
let Some(target) = event.target() else {
|
let target = event.target()?;
|
||||||
return None;
|
|
||||||
};
|
|
||||||
|
|
||||||
let Ok(target) = target.dyn_into::<web_sys::Element>() else {
|
let Ok(target) = target.dyn_into::<web_sys::Element>() else {
|
||||||
return None;
|
return None;
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub fn use_lock_html_scroll(is_lock: MaybeSignal<bool>) {
|
||||||
let style = document()
|
let style = document()
|
||||||
.create_element("style")
|
.create_element("style")
|
||||||
.expect("create style element error");
|
.expect("create style element error");
|
||||||
_ = style.set_attribute("data-id", &format!("thaw-lock-html-scroll"));
|
_ = style.set_attribute("data-id", "thaw-lock-html-scroll");
|
||||||
style.set_text_content(Some("html { overflow: hidden; }"));
|
style.set_text_content(Some("html { overflow: hidden; }"));
|
||||||
_ = head.append_child(&style);
|
_ = head.append_child(&style);
|
||||||
style_el.set_value(Some(style));
|
style_el.set_value(Some(style));
|
||||||
|
|
|
@ -22,7 +22,7 @@ impl NextFrame {
|
||||||
pub fn run(&self, cb: impl FnOnce() + 'static) {
|
pub fn run(&self, cb: impl FnOnce() + 'static) {
|
||||||
self.cancel();
|
self.cancel();
|
||||||
|
|
||||||
let next_frame_hadnle = self.0.clone();
|
let next_frame_hadnle = self.0;
|
||||||
let handle = request_animation_frame_with_handle(move || {
|
let handle = request_animation_frame_with_handle(move || {
|
||||||
let handle = request_animation_frame_with_handle(cb).unwrap();
|
let handle = request_animation_frame_with_handle(cb).unwrap();
|
||||||
next_frame_hadnle.set_value(Some(handle));
|
next_frame_hadnle.set_value(Some(handle));
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use leptos::{leptos_dom::helpers::TimeoutHandle, *};
|
use leptos::{leptos_dom::helpers::TimeoutHandle, *};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
pub fn throttle(cb: impl Fn() + 'static, duration: Duration) -> impl Fn() -> () {
|
pub fn throttle(cb: impl Fn() + 'static, duration: Duration) -> impl Fn() {
|
||||||
let cb = Callback::new(move |_| cb());
|
let cb = Callback::new(move |_| cb());
|
||||||
let timeout_handle = StoredValue::new(None::<TimeoutHandle>);
|
let timeout_handle = StoredValue::new(None::<TimeoutHandle>);
|
||||||
on_cleanup(move || {
|
on_cleanup(move || {
|
||||||
|
|
Loading…
Add table
Reference in a new issue