3232
3333<script >
3434/* metaService: engine.setting.event.BindEventsDialog */
35- import { ast2String , string2Ast } from ' @opentiny/tiny-engine-common/js/ast'
35+ import { string2Ast } from ' @opentiny/tiny-engine-common/js/ast'
3636import {
3737 getMergeMeta ,
3838 useCanvas ,
@@ -44,6 +44,7 @@ import {
4444} from ' @opentiny/tiny-engine-meta-register'
4545import { Button , DialogBox , TinyAlert } from ' @opentiny/vue'
4646import { nextTick , provide , reactive , ref } from ' vue'
47+ import MagicString from ' magic-string'
4748import meta from ' ../../meta'
4849
4950const dialogVisible = ref (false )
@@ -147,38 +148,39 @@ export default {
147148
148149 const getFormatParams = (extraParams ) => Array .from ({ length: extraParams .length }, (v , i ) => ` args${ i} ` ).join (' ,' )
149150
150- const getFunctionBody = () => {
151- let method = getMethods ()? .[state .bindMethodInfo .name ]? .value
152- let preBody = ' {}'
153- let isAsync = false
154- let isGenerator = false
151+ const rewriteMethodParams = (method , name , formatParams , extraParams , enableExtraParams ) => {
152+ const finalParams = enableExtraParams && extraParams .length ? ` event,${ formatParams} ` : formatParams
153+ const defaultMethod = ` function ${ name} (${ finalParams} ) {\n }\n `
155154
156- if (method) {
157- let astStr = {}
158- try {
159- astStr = string2Ast (method)
160- } catch (e) {
161- method = method .replace (' function' , ` function ${ state .bindMethodInfo .name } ` )
162- astStr = string2Ast (method)
163- }
155+ // 没有现存方法,直接拼接一个新的
156+ if (! method) {
157+ return defaultMethod
158+ }
164159
165- if (astStr ? . program ? . body [ 0 ] ? . body ) {
166- preBody = ast2String ( astStr . program . body [ 0 ]. body )
167- }
160+ try {
161+ const magicStr = new MagicString (method )
162+ const astStr = string2Ast (method)
168163
169- if (astStr? .program ? .body [0 ]? .async ) {
170- isAsync = true
164+ // 解析出来不是函数声明,直接返回默认拼接的函数
165+ if (astStr? .program ? .body [0 ]? .type !== ' FunctionDeclaration' ) {
166+ return defaultMethod
171167 }
172168
173- if (astStr? .program ? .body [0 ]? .generator ) {
174- isGenerator = true
169+ // 参数数量一致,不需要改写参数,直接返回
170+ // extraParams.length 是传入的参数数量,+1 是 event 参数
171+ if (astStr? .program ? .body [0 ].params .length === extraParams .length + 1 ) {
172+ return method
175173 }
176- }
177174
178- return {
179- preBody: preBody || ' {\n }' ,
180- isAsync,
181- isGenerator
175+ // 参数数量不一致,需要改写参数
176+ const start = astStr? .program ? .body [0 ].id .end
177+ const end = astStr? .program ? .body [0 ].body .start
178+ magicStr .remove (start, end)
179+ magicStr .appendLeft (start, ` (${ finalParams} )` )
180+ return magicStr .toString ()
181+ } catch (e) {
182+ // 尝试改写失败了,直接返回拼接的
183+ return defaultMethod
182184 }
183185 }
184186
@@ -214,14 +216,12 @@ export default {
214216 bindMethod ({ ... state .bindMethodInfo , params, extra: extraParams })
215217
216218 // 需要在bindMethod之后
217- const { preBody: functionBody , isAsync , isGenerator } = getFunctionBody ()
218219 const { name } = state .bindMethodInfo
219- const functionName = ` ${ isAsync ? ' async' : ' ' } function${ isGenerator ? ' *' : ' ' } ${ name} `
220+ const methodValue = getMethods ()? .[state .bindMethodInfo .name ]? .value
221+ const functionStr = rewriteMethodParams (methodValue, name, formatParams, extraParams, state .enableExtraParams )
220222 const method = {
221223 name,
222- content: state .enableExtraParams
223- ? ` ${ functionName} (eventArgs,${ formatParams} ) ${ functionBody} `
224- : ` ${ functionName} (${ formatParams} ) ${ functionBody} `
224+ content: functionStr
225225 }
226226 const { beforeSaveMethod } = getOptions (meta .id )
227227
0 commit comments