mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-15 09:21:13 +08:00
1. 修改 render 方法中 replaceText 从 options 中取值; 2. replaceText 方法增加拷贝逻辑; 3. JSONTranverse 方法忽略 observable 的值 (#5944)
Co-authored-by: 王玉振 <wangyuzhen01@wangyuzhen01.local>
This commit is contained in:
parent
ad23b406f9
commit
eb1f154ae0
@ -230,7 +230,7 @@ export function render(
|
||||
props.useMobileUI = true;
|
||||
}
|
||||
|
||||
replaceText(schema, env.replaceText, env.replaceTextIgnoreKeys);
|
||||
schema = replaceText(schema, options.replaceText, env.replaceTextIgnoreKeys);
|
||||
|
||||
return (
|
||||
<EnvContext.Provider value={env}>
|
||||
|
@ -429,7 +429,11 @@ export const ServiceStore = iRendererStore
|
||||
} else {
|
||||
if (json.data) {
|
||||
const env = getEnv(self);
|
||||
replaceText(json.data, env.replaceText, env.replaceTextIgnoreKeys);
|
||||
json.data = replaceText(
|
||||
json.data,
|
||||
env.replaceText,
|
||||
env.replaceTextIgnoreKeys
|
||||
);
|
||||
|
||||
self.schema = Array.isArray(json.data)
|
||||
? json.data
|
||||
|
@ -1673,10 +1673,12 @@ export function JSONTraverse(
|
||||
) {
|
||||
Object.keys(json).forEach(key => {
|
||||
const value: any = json[key];
|
||||
if (isPlainObject(value) || Array.isArray(value)) {
|
||||
JSONTraverse(value, mapper);
|
||||
} else {
|
||||
mapper(value, key, json);
|
||||
if (!isObservable(value)) {
|
||||
if (isPlainObject(value) || Array.isArray(value)) {
|
||||
JSONTraverse(value, mapper);
|
||||
} else {
|
||||
mapper(value, key, json);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* 对文本进行替换
|
||||
*/
|
||||
|
||||
import cloneDeep from 'lodash/cloneDeep';
|
||||
import {isObject, JSONTraverse} from './helper';
|
||||
|
||||
export function replaceText(
|
||||
@ -11,11 +11,17 @@ export function replaceText(
|
||||
) {
|
||||
// 进行文本替换
|
||||
if (replaceText && isObject(replaceText)) {
|
||||
let replicaSchema = cloneDeep(schema);
|
||||
const replaceKeys = Object.keys(replaceText);
|
||||
replaceKeys.sort((a, b) => b.length - a.length); // 避免用户将短的放前面
|
||||
const IgnoreKeys = new Set(replaceTextIgnoreKeys || []);
|
||||
JSONTraverse(schema, (value: any, key: string, object: any) => {
|
||||
if (typeof value === 'string' && !IgnoreKeys.has(key)) {
|
||||
JSONTraverse(replicaSchema, (value: any, key: string, object: any) => {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(object, key);
|
||||
if (
|
||||
typeof value === 'string' &&
|
||||
!IgnoreKeys.has(key) &&
|
||||
descriptor?.writable
|
||||
) {
|
||||
for (const replaceKey of replaceKeys) {
|
||||
if (~value.indexOf(replaceKey)) {
|
||||
value = object[key] = value.replaceAll(
|
||||
@ -26,5 +32,8 @@ export function replaceText(
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return replicaSchema;
|
||||
}
|
||||
return schema;
|
||||
}
|
||||
|
@ -245,7 +245,11 @@ export default class App extends React.Component<AppProps, object> {
|
||||
if (isEffectiveApi(api, store.data)) {
|
||||
const json = await store.fetchInitData(api, store.data, {});
|
||||
if (env.replaceText) {
|
||||
replaceText(json.data, env.replaceText, env.replaceTextIgnoreKeys);
|
||||
json.data = replaceText(
|
||||
json.data,
|
||||
env.replaceText,
|
||||
env.replaceTextIgnoreKeys
|
||||
);
|
||||
}
|
||||
|
||||
if (json?.data.pages) {
|
||||
|
Loading…
Reference in New Issue
Block a user