From 410d594ac059c47ddb50c677a625ca8e7fa27061 Mon Sep 17 00:00:00 2001 From: xfy Date: Wed, 24 Jun 2026 10:35:11 +0800 Subject: [PATCH] fix(bridge): read window.TiptapEditor via Reflect.get, not function call MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The extern 'fn get_module() -> TiptapEditorModule' was compiled by wasm-bindgen to 'window.TiptapEditor()' — a function call. But TiptapEditor is the IIFE module object, not a function, so this threw 'window.TiptapEditor is not a function'. Replace with js_sys::Reflect::get(&window, "TiptapEditor") + dyn_into, which reads the property without invoking it. This bug predated the Vite 8 upgrade but only surfaced after the EditorOptions fix let initialization progress further. --- src/tiptap_bridge.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/tiptap_bridge.rs b/src/tiptap_bridge.rs index a3c180a..f9fb518 100644 --- a/src/tiptap_bridge.rs +++ b/src/tiptap_bridge.rs @@ -35,14 +35,15 @@ pub mod wasm { use wasm_bindgen::JsCast; // —— window.TiptapEditor 模块对象 —— + // + // TiptapEditor 是 IIFE 产物挂在 window 上的模块对象(含 create 等方法), + // 不是函数。wasm-bindgen 对 `fn get_module() -> T` 形式的 extern 会生成 + // `window.TiptapEditor()`(函数调用),会因"not a function"失败。 + // 因此用 js_sys::Reflect::get 做属性访问拿到模块对象,再 dyn_into。 #[wasm_bindgen] extern "C" { pub type TiptapEditorModule; - /// 读取 `window.TiptapEditor`(IIFE 默认导出,顶层 var 即 window 属性)。 - #[wasm_bindgen(js_namespace = window, js_name = TiptapEditor)] - pub fn get_module() -> TiptapEditorModule; - /// 调用 `TiptapEditor.create(containerId, options)`。 /// 找不到容器返回 null(被 Option 捕获);构造失败抛异常(被 catch 捕获)。 #[wasm_bindgen(method, catch)] @@ -53,6 +54,15 @@ pub mod wasm { ) -> Result, JsValue>; } + /// 读取 `window.TiptapEditor`(IIFE 默认导出,顶层 var 即 window 属性)。 + /// 用 Reflect::get 做属性访问——extern fn 形式会被 wasm-bindgen 编成函数调用。 + pub fn get_module() -> TiptapEditorModule { + let window = web_sys::window().expect("no window"); + let val = js_sys::Reflect::get(&window, &"TiptapEditor".into()).expect("window.TiptapEditor missing"); + val.dyn_into::() + .expect("window.TiptapEditor is not TiptapEditorModule") + } + // —— 编辑器实例(TiptapEditorInstance)—— #[wasm_bindgen] extern "C" {