From 2f28a1ddcad64c73b77bc3870ff26b23b723c734 Mon Sep 17 00:00:00 2001 From: "zude.hzd" Date: Mon, 2 Nov 2020 17:00:26 +0800 Subject: [PATCH] fix: update --- .../src/function-setter/index.tsx | 41 ++++++++++++------- .../plugin-event-bind-dialog/src/index.tsx | 30 +++++++------- packages/plugin-source-editor/src/index.tsx | 4 -- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/packages/editor-setters/src/function-setter/index.tsx b/packages/editor-setters/src/function-setter/index.tsx index e4c0971cc..20a6deb04 100644 --- a/packages/editor-setters/src/function-setter/index.tsx +++ b/packages/editor-setters/src/function-setter/index.tsx @@ -1,3 +1,4 @@ + import React, { PureComponent } from 'react'; // import PropTypes from 'prop-types'; import { Button, Icon, Dialog } from '@alifd/next'; @@ -64,8 +65,8 @@ export default class FunctionSetter extends PureComponent { editor.on(`${this.emitEventName}.bindEvent`, this.bindEvent); } - bindEvent = (eventName) => { - this.bindEventCallback(eventName); + bindEvent = (eventName, paramStr) => { + this.bindEventCallback(eventName, paramStr); }; @@ -75,9 +76,17 @@ export default class FunctionSetter extends PureComponent { } - bindFunction = () => { - const { field } = this.props; - field.editor.emit('eventBindDialog.openDialog', field.name, this.emitEventName); + bindFunction = (isEdit) => { + const { field, value } = this.props; + + let paramStr; + + if (value) { + paramStr = this.parseFunctionParam(value.value); + } + + + field.editor.emit('eventBindDialog.openDialog', field.name, this.emitEventName, paramStr, isEdit); }; openDialog = () => { @@ -102,14 +111,16 @@ export default class FunctionSetter extends PureComponent { parseFunctionName = (functionString: string) => { // 因为函数格式是固定的,所以可以按照字符换去匹配获取函数名 - const funNameStr = functionString.split('this.')[1]; + const funNameStr = functionString.split('this.')[1].split('.')[0]; + return funNameStr; + }; - if (funNameStr) { - const endIndex = funNameStr.indexOf('('); - return funNameStr.substr(0, endIndex); - } else { - return ''; + parseFunctionParam = (functionString:string) => { + // eslint-disable-next-line no-useless-escape + const matchList = functionString.match(/\[(\w|\s|\,|")*\]/); + if (matchList?.length) { + return matchList[0].substring(1, matchList[0].length - 1); } }; @@ -158,7 +169,7 @@ export default class FunctionSetter extends PureComponent {
this.focusFunctionName(functionName)}>{functionName} - + this.bindFunction(true)} />
); @@ -177,14 +188,16 @@ export default class FunctionSetter extends PureComponent { }; - bindEventCallback = (eventName: string) => { + bindEventCallback = (eventName: string, paramStr:string) => { const { onChange } = this.props; + onChange({ type: 'JSFunction', - value: `function(){ return this.${eventName}() }`, + value: `function(){ return this.${eventName}.apply(this,Array.prototype.slice.call(arguments).concat([${paramStr || ''}])) }`, }); }; + render() { const { value } = this.props; const { isShowDialog } = this.state; diff --git a/packages/plugin-event-bind-dialog/src/index.tsx b/packages/plugin-event-bind-dialog/src/index.tsx index 978f074e5..7af71729a 100644 --- a/packages/plugin-event-bind-dialog/src/index.tsx +++ b/packages/plugin-event-bind-dialog/src/index.tsx @@ -58,15 +58,11 @@ export default class EventBindDialog extends Component { paramStr: '', }; - openDialog = (bindEventName: string) => { + openDialog = (bindEventName: string, isEdit:boolean) => { this.bindEventName = bindEventName; - this.initEventName(); + this.initEventName(isEdit); - this.setState({ - visiable: true, - selectedEventName: '', - }); }; closeDialog = () => { @@ -77,7 +73,7 @@ export default class EventBindDialog extends Component { componentDidMount() { const { editor, config } = this.props; - editor.on(`${config.pluginKey}.openDialog`, (bindEventName: string, setterName:string, paramStr:string) => { + editor.on(`${config.pluginKey}.openDialog`, (bindEventName: string, setterName:string, paramStr:string, isEdit:boolean) => { console.log(`paramStr:${ paramStr}`); this.setState({ setterName, @@ -95,20 +91,25 @@ export default class EventBindDialog extends Component { } } - this.openDialog(bindEventName); + this.openDialog(bindEventName, isEdit); }); } - initEventName = () => { + initEventName = (isEdit:boolean) => { let eventName = this.bindEventName; - this.eventList.forEach((item) => { - if (item.name === eventName) { - eventName = `${eventName}_new`; - } - }); + + if (!isEdit) { + this.eventList.forEach((item) => { + if (item.name === eventName) { + eventName = `${eventName}_new`; + } + }); + } this.setState({ eventName, + selectedEventName: (isEdit ? eventName : ''), + visiable: true, }); }; @@ -168,6 +169,7 @@ export default class EventBindDialog extends Component { render() { const { selectedEventName, eventName, visiable, paramStr } = this.state; + console.log('selectedEventName:' + selectedEventName); return (