diff --git a/glass-easel-template-compiler/glass_easel_template_compiler.h b/glass-easel-template-compiler/glass_easel_template_compiler.h index 88827b3..5f612ac 100644 --- a/glass-easel-template-compiler/glass_easel_template_compiler.h +++ b/glass-easel-template-compiler/glass_easel_template_compiler.h @@ -122,8 +122,6 @@ void tmpl_group_set_extra_runtime_script(TmplGroup *self, const uint8_t *content_buf, size_t content_len); -void tmpl_group_set_filter_funcs(TmplGroup *self, const uint8_t *content_buf, size_t content_len); - int32_t tmpl_group_set_inline_script(TmplGroup *self, const uint8_t *path_buf, size_t path_len, diff --git a/glass-easel-template-compiler/src/cbinding.rs b/glass-easel-template-compiler/src/cbinding.rs index bef5c4a..0af47f3 100644 --- a/glass-easel-template-compiler/src/cbinding.rs +++ b/glass-easel-template-compiler/src/cbinding.rs @@ -307,16 +307,6 @@ impl TmplGroup { 0 } - #[no_mangle] - pub unsafe extern "C" fn tmpl_group_set_filter_funcs( - &mut self, - content_buf: &u8, - content_len: usize, - ) { - let content = String::from_utf8_lossy(slice::from_raw_parts(content_buf, content_len)); - self.inner_mut().set_filter_funcs(&content); - } - #[no_mangle] pub unsafe extern "C" fn tmpl_group_get_runtime_string(&self) -> StrRef { self.inner().get_runtime_string().into() diff --git a/glass-easel-template-compiler/src/group.rs b/glass-easel-template-compiler/src/group.rs index 2953316..44b6936 100644 --- a/glass-easel-template-compiler/src/group.rs +++ b/glass-easel-template-compiler/src/group.rs @@ -88,6 +88,29 @@ var D = (() => { })() "#; +fn runtime_fns( + w: &mut JsFunctionScopeWriter, + need_wxs_runtime: bool, +) -> Result<(), TmplError> { + for (k, v) in RUNTIME_ITEMS.iter() { + w.expr_stmt(|w| { + write!(w, "var {}={}", k, v)?; + Ok(()) + })?; + } + if need_wxs_runtime { + w.expr_stmt(|w| { + write!(w, "{}", WXS_RUNTIME)?; + Ok(()) + })?; + w.expr_stmt(|w| { + write!(w, "var Q={{A:(x)=>x,B:(x)=>x}}")?; + Ok(()) + })?; + } + Ok(()) +} + fn runtime_var_list() -> Vec<&'static str> { RUNTIME_ITEMS.iter().map(|(k, _)| *k).collect() } @@ -127,7 +150,6 @@ pub struct TmplGroup { has_scripts: bool, extra_runtime_string: String, dev_mode: bool, - filter_funcs: Option, } impl TmplGroup { @@ -139,7 +161,6 @@ impl TmplGroup { has_scripts: false, extra_runtime_string: String::new(), dev_mode: false, - filter_funcs: None, } } @@ -257,7 +278,7 @@ impl TmplGroup { pub fn get_runtime_string(&self) -> String { let mut w = JsTopScopeWriter::new(String::new()); w.function_scope(|w| { - self.runtime_fns(w)?; + runtime_fns(w, self.has_scripts)?; Ok(()) }) .unwrap(); @@ -340,48 +361,11 @@ impl TmplGroup { Ok(w.finish()) } - pub fn set_filter_funcs(&mut self, funcs: &str) { - self.filter_funcs = Some(funcs.to_string()) - } - - pub fn clear_filter_funcs(&mut self) { - self.filter_funcs = None - } - - pub fn has_filter_funcs(&self) -> bool { - self.filter_funcs.is_some() - } - - fn runtime_fns( - &self, - w: &mut JsFunctionScopeWriter, - ) -> Result<(), TmplError> { - for (k, v) in RUNTIME_ITEMS.iter() { - w.expr_stmt(|w| { - write!(w, "var {}={}", k, v)?; - Ok(()) - })?; - } - if self.has_scripts { - w.expr_stmt(|w| { - write!(w, "{}", WXS_RUNTIME)?; - Ok(()) - })?; - if let Some(filter_funcs) = &self.filter_funcs { - w.expr_stmt(|w| { - write!(w, "var Q={}", filter_funcs)?; - Ok(()) - })?; - } - } - Ok(()) - } - fn write_group_global_content( &self, w: &mut JsFunctionScopeWriter, ) -> Result<(), TmplError> { - self.runtime_fns(w)?; + runtime_fns(w, self.has_scripts)?; if self.extra_runtime_string.len() > 0 { w.custom_stmt_str(&self.extra_runtime_string)?; } @@ -476,7 +460,7 @@ impl TmplGroup { pub fn export_globals(&self) -> Result { let mut w = JsTopScopeWriter::new(String::new()); w.function_scope(|w| { - self.runtime_fns(w)?; + runtime_fns(w, self.has_scripts)?; if self.extra_runtime_string.len() > 0 { w.custom_stmt_str(&self.extra_runtime_string)?; } diff --git a/glass-easel-template-compiler/src/js_bindings.rs b/glass-easel-template-compiler/src/js_bindings.rs index 720a3d5..22b362b 100644 --- a/glass-easel-template-compiler/src/js_bindings.rs +++ b/glass-easel-template-compiler/src/js_bindings.rs @@ -145,11 +145,6 @@ impl TmplGroup { Ok(()) } - #[wasm_bindgen(js_name = "setFilterFuncs")] - pub fn set_filter_funcs(&mut self, s: &str) { - self.group.set_filter_funcs(s) - } - #[wasm_bindgen(js_name = "getRuntimeString")] pub fn get_runtime_string(&self) -> String { self.group.get_runtime_string() diff --git a/glass-easel-template-compiler/src/proc_gen/tag.rs b/glass-easel-template-compiler/src/proc_gen/tag.rs index 1550a88..41f8f12 100644 --- a/glass-easel-template-compiler/src/proc_gen/tag.rs +++ b/glass-easel-template-compiler/src/proc_gen/tag.rs @@ -61,11 +61,15 @@ impl Template { w.expr_stmt(|w| { write!(w, "H[{key}]=", key = gen_lit_str(key))?; w.function_args("R,C,D,U", |w| { - if has_scripts && group.has_filter_funcs() { + if has_scripts { w.expr_stmt(|w| { write!(w, "R.setFnFilter(Q.A,Q.B)")?; Ok(()) })?; + w.expr_stmt(|w| { + write!(w, "R.setEventListenerWrapper(Q.C)")?; + Ok(()) + })?; } let mut writer = JsTopScopeWriter::new(String::new()); writer.align(w); diff --git a/glass-easel/src/tmpl/proc_gen_wrapper.ts b/glass-easel/src/tmpl/proc_gen_wrapper.ts index 88a72c3..d21052a 100644 --- a/glass-easel/src/tmpl/proc_gen_wrapper.ts +++ b/glass-easel/src/tmpl/proc_gen_wrapper.ts @@ -1158,8 +1158,17 @@ export class ProcGenWrapper { } } - /** @deprecated */ - setFnFilter() {} + // set change properties filter + setFnFilter(changePropFilter: ChangePropFilter) { + this.changePropFilter = changePropFilter + } + + // set event listener wrapper + setEventListenerWrapper(eventListenerWrapper?: EventListenerWrapper) { + if (typeof eventListenerWrapper === 'function') { + this.eventListenerWrapper = eventListenerWrapper + } + } // get dev args object devArgs(elem: Element): TmplDevArgs { diff --git a/glass-easel/tests/base/env.ts b/glass-easel/tests/base/env.ts index 67d2811..bdea340 100644 --- a/glass-easel/tests/base/env.ts +++ b/glass-easel/tests/base/env.ts @@ -79,19 +79,22 @@ type TemplateOptions = { type FilterFuncs = { A?: glassEasel.template.ChangePropFilter - B?: glassEasel.template.EventListenerWrapper + C?: glassEasel.template.EventListenerWrapper } export const tmpl = (src: string, options?: TemplateOptions, filterFuncs?: FilterFuncs) => { const group = TmplGroup.newDev() - if (filterFuncs !== undefined) { - const A = filterFuncs.A || (() => {}) - const B = filterFuncs.B || (() => {}) - group.setFilterFuncs(`{A:${A.toString()},B:${B.toString()}}`) - } group.addTmpl('', src) - const genObjectSrc = `return ${group.getTmplGenObjectGroups()}` + let genObjectSrc = `return ${group.getTmplGenObjectGroups()}` group.free() + if (filterFuncs !== undefined) { + const A = filterFuncs.A || ((x) => x) + const C = filterFuncs.C || 'undefined' + genObjectSrc = genObjectSrc.replace( + 'var Q={A:(x)=>x,B:(x)=>x}', + `var Q={A:${A.toString()},C:${C.toString()}}`, + ) + } // console.info(genObjectSrc) // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const genObjectGroupList = new Function(genObjectSrc)() as { [key: string]: any } diff --git a/glass-easel/tests/tmpl/event.test.ts b/glass-easel/tests/tmpl/event.test.ts index 3d508ad..e840894 100644 --- a/glass-easel/tests/tmpl/event.test.ts +++ b/glass-easel/tests/tmpl/event.test.ts @@ -17,7 +17,7 @@ const testCases = (testBackend: glassEasel.GeneralBackendContext) => { `, {}, { - B: (elem, event, listener) => { + C: (elem, event, listener) => { switch (event.getEventName()) { case 'customEv': { event.target = Object.assign(event.target, { id: 'b' })