fix: 修复 envOverwrite 存在副作用的问题 (#8766)

This commit is contained in:
liaoxuezhi 2023-11-16 17:15:58 +08:00 committed by GitHub
parent 552a6732b9
commit 42951d01c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 32 deletions

View File

@ -2,26 +2,31 @@
* @file 使
*/
import {findObjectsWithKey} from './utils/helper';
import {JSONValueMap, findObjectsWithKey} from './utils/helper';
import isPlainObject from 'lodash/isPlainObject';
const isMobile = (window as any).matchMedia?.('(max-width: 768px)').matches
? true
: false;
// 这里不能用 addSchemaFilter 是因为还需要更深层的替换,比如 select 里的 options
export const envOverwrite = (schema: any, locale?: string) => {
if (locale) {
let schemaNodes = findObjectsWithKey(schema, locale);
for (let schemaNode of schemaNodes) {
Object.assign(schemaNode, schemaNode[locale]);
delete schemaNode[locale];
}
}
return JSONValueMap(
schema,
(value: any) => {
if (!isPlainObject(value)) {
return value;
}
if (isMobile) {
let schemaNodes = findObjectsWithKey(schema, 'mobile');
for (let schemaNode of schemaNodes) {
Object.assign(schemaNode, schemaNode['mobile']);
delete schemaNode['mobile'];
}
}
if (locale && value[locale]) {
const newValue = Object.assign({}, value, value[locale]);
delete newValue[locale];
return newValue;
} else if (isMobile && value.mobile) {
const newValue = Object.assign({}, value, value.mobile);
delete newValue.mobile;
return newValue;
}
},
true
);
};

View File

@ -308,7 +308,7 @@ function AMISRenderer({
}
// 根据环境覆盖 schema这个要在最前面做不然就无法覆盖 validations
envOverwrite(schema, locale);
schema = envOverwrite(schema, locale);
schema = replaceText(schema, options.replaceText, env.replaceTextIgnoreKeys);
return (

View File

@ -1954,6 +1954,7 @@ export function JSONValueMap(
host: Object,
stack: Array<Object>
) => any,
deepFirst: boolean = false,
stack: Array<Object> = []
) {
if (!isPlainObject(json) && !Array.isArray(json)) {
@ -1966,40 +1967,42 @@ export function JSONValueMap(
host: any,
stack: Array<any> = []
) => {
let maped: any = mapper(origin, key, host, stack);
if (deepFirst) {
const value = JSONValueMap(origin, mapper, deepFirst, stack);
return mapper(value, key, host, stack) ?? value;
}
if (maped === origin && (isPlainObject(origin) || Array.isArray(origin))) {
return JSONValueMap(origin, mapper, stack);
let maped: any = mapper(origin, key, host, stack) ?? origin;
// 如果不是深度优先,上层的对象都修改了,就不继续递归进到新返回的对象了
if (maped === origin) {
return JSONValueMap(origin, mapper, deepFirst, stack);
}
return maped;
};
if (Array.isArray(json)) {
let flag = false;
let mapped = json.map((value, index) => {
let result: any = iterator(value, index, json, [json].concat(stack));
if (result !== value) {
flag = true;
return result;
}
return value;
let modified = false;
let arr = json.map((value, index) => {
let newValue: any = iterator(value, index, json, [json].concat(stack));
modified = modified || newValue !== value;
return newValue;
});
return flag ? mapped : json;
return modified ? arr : json;
}
let flag = false;
let modified = false;
const toUpdate: any = {};
Object.keys(json).forEach(key => {
const value: any = json[key];
let result: any = iterator(value, key, json, [json].concat(stack));
if (result !== value) {
flag = true;
modified = true;
toUpdate[key] = result;
return;
}
});
return flag
return modified
? {
...json,
...toUpdate