| 14 | /// 修复原 bug:将 JS 创建的按钮改为 Yew 组件,路由切换时自动清理 |
| 15 | #[function_component(TocButton)] |
| 16 | pub fn toc_button() -> Html { |
| 17 | let on_click = Callback::from(|_: MouseEvent| { |
| 18 | // 触发目录显示(与 JS 生成的目录交互) |
| 19 | if let Some(window) = web_sys::window() { |
| 20 | // 如果已存在目录,则直接切换展开/收起 |
| 21 | if let Some(existing) = window |
| 22 | .document() |
| 23 | .and_then(|d| d.query_selector(".article-toc").ok()) |
| 24 | .flatten() |
| 25 | { |
| 26 | let class_list = existing.class_list(); |
| 27 | if class_list.contains("mobile-open") { |
| 28 | let _ = class_list.remove_1("mobile-open"); |
| 29 | } else { |
| 30 | let _ = class_list.add_1("mobile-open"); |
| 31 | } |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | // 若目录未生成,尝试调用全局 generateTOC 后再展开 |
| 36 | if let Ok(gen) = Reflect::get(&window, &JsValue::from_str("generateTOC")) { |
| 37 | if let Ok(func) = gen.dyn_into::<js_sys::Function>() { |
| 38 | let _ = func.call0(&window); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if let Some(toc) = window |
| 43 | .document() |
| 44 | .and_then(|d| d.query_selector(".article-toc").ok()) |
| 45 | .flatten() |
| 46 | { |
| 47 | let class_list = toc.class_list(); |
| 48 | if class_list.contains("mobile-open") { |
| 49 | let _ = class_list.remove_1("mobile-open"); |
| 50 | } else { |
| 51 | let _ = class_list.add_1("mobile-open"); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | }); |
| 56 | |
| 57 | html! { |
| 58 | <div class={classes!( |
| 59 | "mobile-toc-fab", |
| 60 | "fixed", "left-8", "bottom-8", "z-[99]", |
| 61 | "w-12", "h-12", |
| 62 | "bg-[var(--primary)]", "text-white", |
| 63 | "rounded-full", |
| 64 | "items-center", "justify-center", |
| 65 | "shadow-[0_4px_12px_rgba(29,158,216,0.4)]", |
| 66 | "transition-all", "duration-300", |
| 67 | "active:scale-95", |
| 68 | "max-md:left-6", "max-md:bottom-6", "max-md:w-11", "max-md:h-11", "max-md:text-base" |
| 69 | )}> |
| 70 | <TooltipIconButton |
| 71 | icon={IconName::List} |
| 72 | tooltip={t::TOOLTIP} |
| 73 | position={TooltipPosition::Top} |