From b7ec2f0e24b70d3bf73c3768e82adb1860a38c57 Mon Sep 17 00:00:00 2001 From: zmq248570 Date: Mon, 4 May 2020 19:50:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=BC=95=E7=94=A8funcparse=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/demo/package.json | 3 +- packages/demo/src/vision/funcParser.ts | 114 ------------------ packages/demo/src/vision/index.ts | 58 ++++----- packages/demo/src/vision/module.d.ts | 2 +- .../src/renderer-view.tsx | 10 -- 5 files changed, 29 insertions(+), 158 deletions(-) delete mode 100644 packages/demo/src/vision/funcParser.ts diff --git a/packages/demo/package.json b/packages/demo/package.json index 9ff3918c5..562476b97 100644 --- a/packages/demo/package.json +++ b/packages/demo/package.json @@ -40,7 +40,8 @@ "@alife/theme-lowcode-dark": "^0.1.0", "@alife/theme-lowcode-light": "^0.1.0", "react": "^16.8.1", - "react-dom": "^16.8.1" + "react-dom": "^16.8.1", + "@ali/vu-function-parser": "^2.5.0-beta.0" }, "devDependencies": { "@ali/iceluna-cli": "^0.0.16", diff --git a/packages/demo/src/vision/funcParser.ts b/packages/demo/src/vision/funcParser.ts deleted file mode 100644 index 8d96fb8f1..000000000 --- a/packages/demo/src/vision/funcParser.ts +++ /dev/null @@ -1,114 +0,0 @@ -function getWrappedFunction(func: any, sourceCode: any) { - const result = function (...args) { - try { - return func.apply(this, args); - } catch (e) { - const message = `项目代码执行出错(非引擎内部错误)!非生产环境可以通过在 URL 上加入 ?debug_all 来打开调试模式。更多请点击这里:https://ur.alipay.com/2JbYP - -------------------------------------------------------- - 错误信息:${e.message} - ${sourceCode ? `以下是原始函数: - -------------------------------------------------------- - ${sourceCode} - --------------------------------------------------------` : ''} - 点击下方函数的可以进入并调试方法: - -------------------------------------------------------- - `; - console.error(message, func); - } - }; - return result; - } - - const USE_STRICT = /^(["'])use strict\1;?/i; - - /** - * parse a function string into Function object, isolateWindow (if any) will be used - * as the window object instead of global window - * - * @param {String} func function string need parse - * @param {Object} isolateWindow the isolateWindow used when running the action - */ - export default function (func: any, isolateWindow: any, ctx: any) { - let sourceCode = func; - - // 兼容 ES6 方案,即 funcString 为对象的情况 - // if (typeof func === 'object' && func !== null) { - // if (func.id) { - // let action; - // if (window.VisualEngineUtils) { - // action = window.VisualEngineUtils.ActionUtil.getAction(func.id, { func: true }); - // } else if (window.LeGao && window.LeGao.getContext) { - // const _ctx = ctx || window.LeGao.getContext(); - // action = _ctx.getAction().actionsMap[func.id]; - // } - - // if (!action) { - // const result = () => { - // console.error(`无法找到 ${func.id} 函数`); - // }; - // return result; - // } - // return getWrappedFunction(action.func); - // } - // sourceCode = func.source; - // func = func.compiled || func.source || ''; - // } - - if (!func) { - const result = () => { - window.location.search.indexOf('dev') >= 0 && console.log('解析的函数为空'); - }; - return result; - } - - if (typeof func === 'function') { - // TODO: un-annotate these code after vision-render-service done - // if (isolateWindow) { - // return func(isolateWindow); - // } - return func; - } - - let code = ''; - if (func.indexOf('var __preParser__') >= 0 && func.indexOf('function') !== 0) { - code = ` - ${func} - if (typeof __preParser__ === 'function') { - return __preParser__.apply(this, Array.prototype.slice.call(arguments)); - } - console.warn('code must be a string of function'); - `; - } else { - code = ` - var __preParser__ = ${func.replace(USE_STRICT, '')}; - if (typeof __preParser__ === 'function') { - return __preParser__.apply(this, Array.prototype.slice.call(arguments)); - } - console.warn('code must be a string of function'); - `; - } - - try { - if (isolateWindow) { - code = ` - return function() { - ${code} - } - `; - func = new Function('window', code)(isolateWindow); - } else { - func = new Function(code); - } - } catch (e) { - console.error( - '解析 function 字符串失败,请检查字符串的合法性', - e.message, - `\n${sourceCode}`, - ); - func = function () { - console.error('代码配置有误,请检查相关配置'); - }; - } - - return getWrappedFunction(func, sourceCode); -} diff --git a/packages/demo/src/vision/index.ts b/packages/demo/src/vision/index.ts index 47a37895d..7a00e33cb 100644 --- a/packages/demo/src/vision/index.ts +++ b/packages/demo/src/vision/index.ts @@ -1,7 +1,7 @@ /* eslint-disable */ import { createElement } from 'react'; import { Button } from '@alifd/next'; -import Engine, { Panes } from '@ali/visualengine'; +import Engine, { Panes, Prototype } from '@ali/visualengine'; import { ActionUtil as actionUtil } from '@ali/visualengine-utils'; import getTrunkPane from '@ali/ve-trunk-pane'; import DatapoolPane from '@ali/ve-datapool-pane'; @@ -18,10 +18,8 @@ import { upgradeAssetsBundle } from './upgrade-assets'; import { isCSSUrl } from '@ali/lowcode-utils'; import { I18nSetter } from '@ali/visualengine-utils'; import VariableSetter from '@ali/vs-variable-setter'; -// import { isObject, isArray } from 'lodash'; -// import funcParser from '@ali/vu-function-parser'; -// import funcParser from './funcParser'; -// import Prototype from '../../../vision-preset/src/index'; +import { isObject, isArray } from 'lodash'; +import funcParser from '@ali/vu-function-parser'; const { editor, skeleton, context, HOOKS, Trunk } = Engine; @@ -329,31 +327,30 @@ function initActionPane() { props, }); } -// const replaceFuncProp = (props) => { -// const replaceProps = {}; +function replaceFuncProp(props?: any){ + const replaceProps = {}; + for (const name in props) { + const prop = props[name]; + if (!prop) { + continue; + } + if ((prop.compiled && prop.source) || prop.type === 'actionRef' || prop.type === 'js') { + replaceProps[name] = funcParser(prop); + } else if (isObject(prop)) { + replaceFuncProp(prop); + } else if (isArray(prop)) { + prop.map((propItem) => { + replaceFuncProp(propItem); + }); + } + } -// for (const name in props) { -// const prop = props[name]; -// if (!prop) { -// continue; -// } -// if ((prop.compiled && prop.source) || prop.type === 'actionRef' || prop.type === 'js') { -// replaceProps[name] = funcParser(prop); -// } else if (isObject(prop)) { -// replaceFuncProp(prop); -// } else if (isArray(prop)) { -// prop.map((propItem) => { -// replaceFuncProp(propItem); -// }); -// } -// } + for (const name in replaceProps) { + props[name] = replaceProps[name]; + } -// for (const name in replaceProps) { -// props[name] = replaceProps[name]; -// } - -// return props; -// }; + return props; +}; // 操作历史与页面历史面板 function initHistoryPane() { @@ -418,6 +415,7 @@ async function init() { subview: true, i18nPane: true, }); + Prototype.addGlobalPropsReducer(replaceFuncProp); await loadAssets(); await loadSchema(); await initTrunkPane(); @@ -425,10 +423,6 @@ async function init() { initI18nPane(); initActionPane(); initDemoPanes(); - // console.log(funcParser,'funcParser') - // debugger - // Prototype.addGlobalPropsReducer(replaceFuncProp); - // debugger initHistoryPane(); Engine.init(); } diff --git a/packages/demo/src/vision/module.d.ts b/packages/demo/src/vision/module.d.ts index edbe109b1..ba843aea9 100644 --- a/packages/demo/src/vision/module.d.ts +++ b/packages/demo/src/vision/module.d.ts @@ -9,4 +9,4 @@ declare module '@ali/ve-page-history'; declare module '@ali/ve-i18n-manage-pane'; declare module '@ali/ve-action-pane'; declare module '@ali/vu-legao-design-fetch-context'; -// declare module "@ali/vu-function-parser"; +declare module "@ali/vu-function-parser"; diff --git a/packages/react-simulator-renderer/src/renderer-view.tsx b/packages/react-simulator-renderer/src/renderer-view.tsx index d00e059b3..7b41312c8 100644 --- a/packages/react-simulator-renderer/src/renderer-view.tsx +++ b/packages/react-simulator-renderer/src/renderer-view.tsx @@ -84,16 +84,6 @@ class Renderer extends Component<{ renderer: SimulatorRenderer }> { const { __id, __desingMode, ...viewProps } = props; viewProps.componentId = __id; viewProps._leaf = host.document.getNode(__id); - if (Component.displayName === 'Calendar') { - // debugger - const testProps = { - // defaultDate: undefined, - // dateCellRender:(date: any)=>{return date.date();} - // onSelect:(value:any)=>{console.log(value,'test')} - // defaultMonth: undefined, - }; - Object.assign(viewProps, testProps); - } return createElement( Component, viewProps,