diff --git a/packages/demo/src/vision/funcParser.ts b/packages/demo/src/vision/funcParser.ts new file mode 100644 index 000000000..8d96fb8f1 --- /dev/null +++ b/packages/demo/src/vision/funcParser.ts @@ -0,0 +1,114 @@ +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 3761dee2c..5407a3b27 100644 --- a/packages/demo/src/vision/index.ts +++ b/packages/demo/src/vision/index.ts @@ -14,6 +14,11 @@ 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'; + const { editor, skeleton, context, HOOKS, Trunk } = Engine; @@ -283,6 +288,31 @@ function initActionPane() { props, }); } +// const replaceFuncProp = (props) => { +// 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 replaceProps) { +// props[name] = replaceProps[name]; +// } + +// return props; +// }; async function init() { Engine.Env.setEnv('RE_VERSION', '7.2.0'); @@ -297,7 +327,10 @@ async function init() { initI18nPane(); initActionPane(); initDemoPanes(); - + // console.log(funcParser,'funcParser') + // debugger + // Prototype.addGlobalPropsReducer(replaceFuncProp); + // debugger Engine.init(); } init(); diff --git a/packages/demo/src/vision/module.d.ts b/packages/demo/src/vision/module.d.ts index 2217b3e06..d7ac3cb98 100644 --- a/packages/demo/src/vision/module.d.ts +++ b/packages/demo/src/vision/module.d.ts @@ -6,3 +6,4 @@ declare module "@ali/ve-datapool-pane"; 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"; diff --git a/packages/designer/src/document/node/node.ts b/packages/designer/src/document/node/node.ts index 9f19ce0eb..4cc95b4e1 100644 --- a/packages/designer/src/document/node/node.ts +++ b/packages/designer/src/document/node/node.ts @@ -155,6 +155,7 @@ export class Node { private transformProps(props: any): any { // FIXME! support PropsList return this.document.designer.transformProps(props, this, TransformStage.Init); + // TODO: run transducers in metadata.experimental } diff --git a/packages/editor-skeleton/src/components/mixed-setter/style.less b/packages/editor-skeleton/src/components/mixed-setter/style.less index d277ae322..5d6f31674 100644 --- a/packages/editor-skeleton/src/components/mixed-setter/style.less +++ b/packages/editor-skeleton/src/components/mixed-setter/style.less @@ -25,7 +25,7 @@ border-radius: 3px; } } - .next-input,.next-date-picker { + .next-input,.next-date-picker,.next-month-picker { width: 100%; } &.lc-block-setter { diff --git a/packages/material-parser/test/fixtures/fusion-next-component/src/calendar/calendar.jsx b/packages/material-parser/test/fixtures/fusion-next-component/src/calendar/calendar.jsx index 5f3923042..70992e246 100644 --- a/packages/material-parser/test/fixtures/fusion-next-component/src/calendar/calendar.jsx +++ b/packages/material-parser/test/fixtures/fusion-next-component/src/calendar/calendar.jsx @@ -334,7 +334,7 @@ class Calendar extends Component { [CALENDAR_MODE_MONTH]: , [CALENDAR_MODE_YEAR]: , }; - + return (
{ 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, diff --git a/packages/vision-preset/src/bundle/prototype.ts b/packages/vision-preset/src/bundle/prototype.ts index 8fa44e3cd..f7b291873 100644 --- a/packages/vision-preset/src/bundle/prototype.ts +++ b/packages/vision-preset/src/bundle/prototype.ts @@ -5,6 +5,7 @@ import { addBuiltinComponentAction, isComponentMeta, registerMetadataTransducer, + TransformStage, } from '@ali/lowcode-designer'; import { OldPropConfig, @@ -112,9 +113,10 @@ function addGlobalExtraActions(action: () => ReactElement) { upgradeActions(action)?.forEach(addBuiltinComponentAction); } -const GlobalPropsReducers: any[] = []; +// const GlobalPropsReducers: any[] = []; function addGlobalPropsReducer(reducer: () => any) { - GlobalPropsReducers.push(reducer); + // GlobalPropsReducers.push(reducer); + designer.addPropsReducer(reducer, TransformStage.Render); } export interface OldGlobalPropConfig extends OldPropConfig {