fix:初始化时对function进行转化

This commit is contained in:
zmq248570 2020-05-04 17:59:52 +08:00
parent 6c6df45677
commit da3628ae5b
8 changed files with 166 additions and 5 deletions

View File

@ -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);
}

View File

@ -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();

View File

@ -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";

View File

@ -155,6 +155,7 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
private transformProps(props: any): any {
// FIXME! support PropsList
return this.document.designer.transformProps(props, this, TransformStage.Init);
// TODO: run transducers in metadata.experimental
}

View File

@ -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 {

View File

@ -334,7 +334,7 @@ class Calendar extends Component {
[CALENDAR_MODE_MONTH]: <MonthPanelHeader {...headerProps} />,
[CALENDAR_MODE_YEAR]: <YearPanelHeader {...headerProps} />,
};
return (
<div
{...obj.pickOthers(Calendar.propTypes, others)}

View File

@ -84,6 +84,16 @@ 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,

View File

@ -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 {