diff --git a/src/components/post/post_content.rs b/src/components/post/post_content.rs index 1ae213c..1dd1aad 100644 --- a/src/components/post/post_content.rs +++ b/src/components/post/post_content.rs @@ -4,6 +4,27 @@ use dioxus::prelude::*; +/// 读取 `window` 上的可选全局函数并调用;函数未定义/为 null 时静默跳过。 +/// +/// 替代 `js_sys::eval("if(window.__x) window.__x(...)")` 字符串拼贴模式:用 +/// `Reflect::get` 取属性 + `Function::apply` 调用,无字符串求值,与 `tiptap_bridge` +/// 的类型化 extern 风格一致。 +#[cfg(target_arch = "wasm32")] +fn invoke_optional_global(window: &web_sys::Window, name: &str, args: &[wasm_bindgen::JsValue]) { + use wasm_bindgen::JsCast; + if let Ok(fn_val) = js_sys::Reflect::get(window, &name.into()) { + if !fn_val.is_undefined() && !fn_val.is_null() { + let arr = js_sys::Array::new(); + for a in args { + arr.push(a); + } + let _ = fn_val + .unchecked_into::() + .apply(window, &arr); + } + } +} + /// 文章内容组件。 /// /// Props: @@ -18,14 +39,21 @@ use dioxus::prelude::*; pub fn PostContent(content_html: String) -> Element { #[cfg(target_arch = "wasm32")] use_effect(move || { - let _ = js_sys::eval("window.__initPostContent('.post-content')"); + let window = web_sys::window().unwrap(); + + // 调用 window.__initPostContent('.post-content'):函数不存在时静默跳过 + // (与旧 eval 中的 if 守卫语义一致)。 + invoke_optional_global(&window, "__initPostContent", &[".post-content".into()]); + // lightbox 改由 Dioxus.toml 全局