GeezerVison is showing github.com — feat: added thisRequiredInJSE API to control whether JSExpression exp… · zfhstudy/lowcode-engine@da7f77e
File treeExpand file treeCollapse file tree Open diff view settings designer/src/builtin-simulator rax-simulator-renderer/src react-simulator-renderer/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/designer/src/builtin-simulator/host.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -228,6 +228,10 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp | 228 | 228 | return this.get('requestHandlersMap') || null; | 229 | 229 | } | 230 | 230 |
| | 231 | + get thisRequiredInJSE(): any { | | 232 | + return engineConfig.get('thisRequiredInJSE') ?? true; | | 233 | + } | | 234 | + | 231 | 235 | @computed get componentsAsset(): Asset | undefined { | 232 | 236 | return this.get('componentsAsset'); | 233 | 237 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/editor-core/src/config.ts +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -133,6 +133,10 @@ const VALID_ENGINE_OPTIONS = { | 133 | 133 | type: 'object', | 134 | 134 | description: '数据源引擎的请求处理器映射', | 135 | 135 | }, | | 136 | + thisRequiredInJSE: { | | 137 | + type: 'boolean', | | 138 | + description: 'JSExpression 是否只支持使用 this 来访问上下文变量', | | 139 | + }, | 136 | 140 | }; | 137 | 141 | export interface EngineOptions { | 138 | 142 | /** | @@ -248,6 +252,12 @@ export interface EngineOptions { | 248 | 252 | * 数据源引擎的请求处理器映射 | 249 | 253 | */ | 250 | 254 | requestHandlersMap?: RequestHandlersMap; | | 255 | + | | 256 | + /** | | 257 | + * @default true | | 258 | + * JSExpression 是否只支持使用 this 来访问上下文变量,假如需要兼容原来的 'state.xxx',则设置为 false | | 259 | + */ | | 260 | + thisRequiredInJSE?: boolean; | 251 | 261 | } | 252 | 262 |
| 253 | 263 | const getStrictModeValue = (engineOptions: EngineOptions, defaultValue: boolean): boolean => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/rax-simulator-renderer/src/renderer-view.tsx +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -219,6 +219,7 @@ class Renderer extends Component<{ | 219 | 219 | onCompGetRef={(schema: any, ref: any) => { | 220 | 220 | documentInstance.mountInstance(schema.id, ref); | 221 | 221 | }} | | 222 | + thisRequiredInJSE={host.thisRequiredInJSE} | 222 | 223 | documentId={document.id} | 223 | 224 | getNode={(id: string) => documentInstance.getNode(id) as any} | 224 | 225 | rendererName="PageRenderer" | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-simulator-renderer/src/renderer-view.tsx +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -189,6 +189,7 @@ class Renderer extends Component<{ | 189 | 189 | setSchemaChangedSymbol={this.setSchemaChangedSymbol} | 190 | 190 | getNode={(id: string) => documentInstance.getNode(id) as Node} | 191 | 191 | rendererName="PageRenderer" | | 192 | + thisRequiredInJSE={host.thisRequiredInJSE} | 192 | 193 | customCreateElement={(Component: any, props: any, children: any) => { | 193 | 194 | const { __id, ...viewProps } = props; | 194 | 195 | viewProps.componentId = __id; | |
Collapse file Copy file name to clipboardExpand all lines: packages/renderer-core/src/renderer/base.tsx +11-8Lines changed: 11 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ import { | 12 | 12 | getValue, | 13 | 13 | parseData, | 14 | 14 | parseExpression, | | 15 | + parseThisRequiredExpression, | 15 | 16 | parseI18n, | 16 | 17 | isEmpty, | 17 | 18 | isSchema, | @@ -83,11 +84,13 @@ export default function baseRendererFactory(): IBaseRenderComponent { | 83 | 84 | getLocale: any; | 84 | 85 | setLocale: any; | 85 | 86 | styleElement: any; | | 87 | + parseExpression: any; | 86 | 88 | [key: string]: any; | 87 | 89 |
| 88 | 90 | constructor(props: IBaseRendererProps, context: IBaseRendererContext) { | 89 | 91 | super(props, context); | 90 | 92 | this.context = context; | | 93 | + this.parseExpression = props?.thisRequiredInJSE ? parseThisRequiredExpression : parseExpression; | 91 | 94 | this.__beforeInit(props); | 92 | 95 | this.__init(props); | 93 | 96 | this.__afterInit(props); | @@ -112,7 +115,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { | 112 | 115 |
| 113 | 116 | if (func) { | 114 | 117 | if (isJSExpression(func) || isJSFunction(func)) { | 115 | | - const fn = parseExpression(func, this); | | 118 | + const fn = props.thisRequiredInJSE ? parseThisRequiredExpression(func, this) : parseExpression(func, this); | 116 | 119 | return fn(props, state); | 117 | 120 | } | 118 | 121 |
| @@ -193,7 +196,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { | 193 | 196 | if (fn) { | 194 | 197 | // TODO, cache | 195 | 198 | if (isJSExpression(fn) || isJSFunction(fn)) { | 196 | | - fn = parseExpression(fn, this); | | 199 | + fn = this.parseExpression(fn, this); | 197 | 200 | } | 198 | 201 | if (typeof fn !== 'function') { | 199 | 202 | console.error(`生命周期${method}类型不符`, fn); | @@ -219,7 +222,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { | 219 | 222 | this.__customMethodsList = customMethodsList; | 220 | 223 | forEach(__schema.methods, (val: any, key: string) => { | 221 | 224 | if (isJSExpression(val) || isJSFunction(val)) { | 222 | | - val = parseExpression(val, this); | | 225 | + val = this.parseExpression(val, this); | 223 | 226 | } | 224 | 227 | if (typeof val !== 'function') { | 225 | 228 | console.error(`自定义函数${key}类型不符`, val); | @@ -414,7 +417,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { | 414 | 417 | const { __appHelper: appHelper, __components: components = {} } = this.props || {}; | 415 | 418 |
| 416 | 419 | if (isJSExpression(schema)) { | 417 | | - return parseExpression(schema, scope); | | 420 | + return this.parseExpression(schema, scope); | 418 | 421 | } | 419 | 422 | if (isI18nData(schema)) { | 420 | 423 | return parseI18n(schema, scope); | @@ -434,7 +437,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { | 434 | 437 | const _children = this.getSchemaChildren(schema); | 435 | 438 | // 解析占位组件 | 436 | 439 | if (schema?.componentName === 'Fragment' && _children) { | 437 | | - const tarChildren = isJSExpression(_children) ? parseExpression(_children, scope) : _children; | | 440 | + const tarChildren = isJSExpression(_children) ? this.parseExpression(_children, scope) : _children; | 438 | 441 | return this.__createVirtualDom(tarChildren, scope, parentInfo); | 439 | 442 | } | 440 | 443 |
| @@ -496,7 +499,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { | 496 | 499 | let scopeKey = ''; | 497 | 500 | // 判断组件是否需要生成scope,且只生成一次,挂在this.__compScopes上 | 498 | 501 | if (Comp.generateScope) { | 499 | | - const key = parseExpression(schema.props?.key, scope); | | 502 | + const key = this.parseExpression(schema.props?.key, scope); | 500 | 503 | if (key) { | 501 | 504 | // 如果组件自己设置key则使用组件自己的key | 502 | 505 | scopeKey = key; | @@ -647,7 +650,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { | 647 | 650 |
| 648 | 651 | _children.forEach((_child: any) => { | 649 | 652 | const _childVirtualDom = this.__createVirtualDom( | 650 | | - isJSExpression(_child) ? parseExpression(_child, scope) : _child, | | 653 | + isJSExpression(_child) ? this.parseExpression(_child, scope) : _child, | 651 | 654 | scope, | 652 | 655 | { | 653 | 656 | schema, | @@ -754,7 +757,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { | 754 | 757 | return checkProps(props); | 755 | 758 | } | 756 | 759 | if (isJSExpression(props)) { | 757 | | - props = parseExpression(props, scope); | | 760 | + props = this.parseExpression(props, scope); | 758 | 761 | // 只有当变量解析出来为模型结构的时候才会继续解析 | 759 | 762 | if (!isSchema(props) && !isJSSlot(props)) return checkProps(props); | 760 | 763 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/renderer-core/src/renderer/renderer.tsx +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,6 +60,7 @@ export default function rendererFactory(): IRenderComponent { | 60 | 60 | schema: {} as RootSchema, | 61 | 61 | onCompGetRef: () => { }, | 62 | 62 | onCompGetCtx: () => { }, | | 63 | + thisRequiredInJSE: true, | 63 | 64 | }; | 64 | 65 |
| 65 | 66 | static findDOMNode = findDOMNode; | |
Collapse file Copy file name to clipboardExpand all lines: packages/renderer-core/src/types/index.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -128,6 +128,11 @@ export interface IRendererProps { | 128 | 128 | faultComponent?: IGeneralComponent; | 129 | 129 | /** 设备信息 */ | 130 | 130 | device?: string; | | 131 | + /** | | 132 | + * @default true | | 133 | + * JSExpression 是否只支持使用 this 来访问上下文变量 | | 134 | + */ | | 135 | + thisRequiredInJSE?: boolean; | 131 | 136 | } | 132 | 137 |
| 133 | 138 | export interface IRendererState { | @@ -148,12 +153,13 @@ export interface IBaseRendererProps { | 148 | 153 | __host?: BuiltinSimulatorHost; | 149 | 154 | __container?: any; | 150 | 155 | config?: Record<string, any>; | 151 | | - designMode?: 'live' | 'design'; | | 156 | + designMode?: 'design'; | 152 | 157 | className?: string; | 153 | 158 | style?: CSSProperties; | 154 | 159 | id?: string | number; | 155 | 160 | getSchemaChangedSymbol?: () => boolean; | 156 | 161 | setSchemaChangedSymbol?: (symbol: boolean) => void; | | 162 | + thisRequiredInJSE?: boolean; | 157 | 163 | documentId?: string; | 158 | 164 | getNode?: any; | 159 | 165 | /** | |
Collapse file Copy file name to clipboardExpand all lines: packages/renderer-core/src/utils/common.ts +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -242,7 +242,7 @@ export function transformStringToFunction(str: string) { | 242 | 242 | * @param self scope object | 243 | 243 | * @returns funtion | 244 | 244 | */ | 245 | | -export function parseExpression(str: any, self: any) { | | 245 | +export function parseExpression(str: any, self: any, thisRequired = false) { | 246 | 246 | try { | 247 | 247 | const contextArr = ['"use strict";', 'var __self = arguments[0];']; | 248 | 248 | contextArr.push('return '); | @@ -259,14 +259,18 @@ export function parseExpression(str: any, self: any) { | 259 | 259 | if (inSameDomain() && (window.parent as any).__newFunc) { | 260 | 260 | return (window.parent as any).__newFunc(tarStr)(self); | 261 | 261 | } | 262 | | - const code = `with($scope || {}) { ${tarStr} }`; | | 262 | + const code = `with(${thisRequired ? '{}' : '$scope || {}'}) { ${tarStr} }`; | 263 | 263 | return new Function('$scope', code)(self); | 264 | 264 | } catch (err) { | 265 | | - logger.error('parseExpression.error', err, str, self); | | 265 | + logger.error('parseExpression.error', err, str, self?.__self ?? self); | 266 | 266 | return undefined; | 267 | 267 | } | 268 | 268 | } | 269 | 269 |
| | 270 | +export function parseThisRequiredExpression(str: any, self: any) { | | 271 | + return parseExpression(str, self, true); | | 272 | +} | | 273 | + | 270 | 274 | /** | 271 | 275 | * capitalize first letter | 272 | 276 | * @param word string to be proccessed | |
Collapse file Copy file name to clipboardExpand all lines: packages/renderer-core/tests/renderer/__snapshots__/renderer.test.tsx.snap +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -994,6 +994,24 @@ exports[`JSExpression JSExpression props with loop 1`] = ` | 994 | 994 | </div> | 995 | 995 | `; | 996 | 996 |
| | 997 | +exports[`JSExpression JSExpression props with loop, and thisRequiredInJSE is true 1`] = ` | | 998 | +<div | | 999 | + className="lce-page" | | 1000 | + style={Object {}} | | 1001 | +> | | 1002 | + <div | | 1003 | + className="div-ut" | | 1004 | + forwardRef={[Function]} | | 1005 | + name1="1" | | 1006 | + /> | | 1007 | + <div | | 1008 | + className="div-ut" | | 1009 | + forwardRef={[Function]} | | 1010 | + name1="2" | | 1011 | + /> | | 1012 | +</div> | | 1013 | +`; | | 1014 | + | 997 | 1015 | exports[`JSExpression JSFunction props 1`] = ` | 998 | 1016 | <div | 999 | 1017 | className="lce-page" | |
Collapse file Copy file name to clipboardExpand all lines: packages/renderer-core/tests/renderer/renderer.test.tsx +47-1Lines changed: 47 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -145,7 +145,9 @@ describe('JSExpression', () => { | 145 | 145 | ] | 146 | 146 | }; | 147 | 147 |
| 148 | | - getComp(schema, components.Div).then(({ component, inst }) => { | | 148 | + getComp(schema, components.Div, { | | 149 | + thisRequiredInJSE: false, | | 150 | + }).then(({ component, inst }) => { | 149 | 151 | // expect(inst[0].props.visible).toBeTruthy(); | 150 | 152 | expect(inst.length).toEqual(2); | 151 | 153 | [1, 2].forEach((i) => { | @@ -157,6 +159,50 @@ describe('JSExpression', () => { | 157 | 159 | }); | 158 | 160 | }); | 159 | 161 |
| | 162 | + it('JSExpression props with loop, and thisRequiredInJSE is true', (done) => { | | 163 | + const schema = { | | 164 | + componentName: 'Page', | | 165 | + props: {}, | | 166 | + state: { | | 167 | + isShowDialog: true, | | 168 | + }, | | 169 | + children: [ | | 170 | + { | | 171 | + componentName: "Div", | | 172 | + loop: [ | | 173 | + { | | 174 | + name: '1', | | 175 | + }, | | 176 | + { | | 177 | + name: '2' | | 178 | + } | | 179 | + ], | | 180 | + props: { | | 181 | + className: "div-ut", | | 182 | + name1: { | | 183 | + type: 'JSExpression', | | 184 | + value: 'this.item.name', | | 185 | + }, | | 186 | + name2: { | | 187 | + type: 'JSExpression', | | 188 | + value: 'item.name', | | 189 | + }, | | 190 | + } | | 191 | + } | | 192 | + ] | | 193 | + }; | | 194 | + | | 195 | + getComp(schema, components.Div).then(({ component, inst }) => { | | 196 | + expect(inst.length).toEqual(2); | | 197 | + [0, 1].forEach((i) => { | | 198 | + expect(inst[i].props[`name1`]).toBe(i + 1 + ''); | | 199 | + expect(inst[i].props[`name2`]).toBe(undefined); | | 200 | + }) | | 201 | + componentSnapshot = component; | | 202 | + done(); | | 203 | + }); | | 204 | + }); | | 205 | + | 160 | 206 | // it('JSFunction props with loop', (done) => { | 161 | 207 | // const schema = { | 162 | 208 | // componentName: 'Page', | |
|