fix: 修复 replaceText 不支持 schemaApi 问题 (#4300)

This commit is contained in:
吴多益 2022-05-12 10:25:41 +08:00 committed by GitHub
parent acc101b425
commit 73b35ee49d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 21 deletions

View File

@ -263,9 +263,8 @@ const predefined = {
label: '记住登录'
},
{
type: 'submit',
btnClassName: 'btn-default',
label: '登录'
type: 'static',
value: 'AMIS_HOST'
}
]
},
@ -303,6 +302,10 @@ const predefined = {
type: 'datetime',
labelClassName: 'text-muted',
name: 'date'
},
{
type: 'static',
value: 'AMIS_HOST'
}
]
}

View File

@ -35,6 +35,7 @@ import {OnEventProps} from './utils/renderer-event';
import {enableDebug} from './utils/debug';
import type {ToastLevel, ToastConf} from './components/Toast';
import {replaceText} from './utils/replaceText';
export interface TestFunc {
(
@ -447,24 +448,7 @@ export function render(
props.useMobileUI = true;
}
// 进行文本替换
if (env.replaceText && isObject(env.replaceText)) {
const replaceKeys = Object.keys(env.replaceText);
replaceKeys.sort((a, b) => b.length - a.length); // 避免用户将短的放前面
const replaceTextIgnoreKeys = new Set(env.replaceTextIgnoreKeys || []);
JSONTraverse(schema, (value: any, key: string, object: any) => {
if (typeof value === 'string' && !replaceTextIgnoreKeys.has(key)) {
for (const replaceKey of replaceKeys) {
if (~value.indexOf(replaceKey)) {
value = object[key] = value.replaceAll(
replaceKey,
env.replaceText[replaceKey]
);
}
}
}
});
}
replaceText(schema, env.replaceText, env.replaceTextIgnoreKeys);
return (
<EnvContext.Provider value={env}>

View File

@ -4,6 +4,7 @@ import {Api, ApiObject, Payload, fetchOptions} from '../types';
import {extendObject, isEmpty, isObject} from '../utils/helper';
import {ServerError} from '../utils/errors';
import {normalizeApiResponseData} from '../utils/api';
import {replaceText} from '../utils/replaceText';
export const ServiceStore = iRendererStore
.named('ServiceStore')
@ -402,6 +403,9 @@ export const ServiceStore = iRendererStore
);
} else {
if (json.data) {
const env = getEnv(self);
replaceText(json.data, env.replaceText, env.replaceTextIgnoreKeys);
self.schema = Array.isArray(json.data)
? json.data
: {

30
src/utils/replaceText.ts Normal file
View File

@ -0,0 +1,30 @@
/**
*
*/
import {isObject, JSONTraverse} from './helper';
export function replaceText(
schema: any,
replaceText: {[propName: string]: string},
replaceTextIgnoreKeys: String[]
) {
// 进行文本替换
if (replaceText && isObject(replaceText)) {
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)) {
for (const replaceKey of replaceKeys) {
if (~value.indexOf(replaceKey)) {
value = object[key] = value.replaceAll(
replaceKey,
replaceText[replaceKey]
);
}
}
}
});
}
}