1. 修改 render 方法中 replaceText 从 options 中取值; 2. replaceText 方法增加拷贝逻辑; 3. JSONTranverse 方法忽略 observable 的值 (#5944)

Co-authored-by: 王玉振 <wangyuzhen01@wangyuzhen01.local>
This commit is contained in:
Simon 2022-12-19 12:54:53 +08:00 committed by GitHub
parent ad23b406f9
commit eb1f154ae0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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

View File

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