From eb1f154ae0869f09d8590843b9c332c9421f0e3a Mon Sep 17 00:00:00 2001 From: Simon <39297643+QKflame@users.noreply.github.com> Date: Mon, 19 Dec 2022 12:54:53 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BF=AE=E6=94=B9=20render=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E4=B8=AD=20replaceText=20=E4=BB=8E=20options=20?= =?UTF-8?q?=E4=B8=AD=E5=8F=96=E5=80=BC;=202.=20replaceText=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=A2=9E=E5=8A=A0=E6=8B=B7=E8=B4=9D=E9=80=BB=E8=BE=91?= =?UTF-8?q?;=203.=20JSONTranverse=20=E6=96=B9=E6=B3=95=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=20observable=20=E7=9A=84=E5=80=BC=20(#5944)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 王玉振 --- packages/amis-core/src/index.tsx | 2 +- packages/amis-core/src/store/service.ts | 6 +++++- packages/amis-core/src/utils/helper.ts | 10 ++++++---- packages/amis-core/src/utils/replaceText.ts | 15 ++++++++++++--- packages/amis/src/renderers/App.tsx | 6 +++++- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/packages/amis-core/src/index.tsx b/packages/amis-core/src/index.tsx index d73488127..66ab577d6 100644 --- a/packages/amis-core/src/index.tsx +++ b/packages/amis-core/src/index.tsx @@ -230,7 +230,7 @@ export function render( props.useMobileUI = true; } - replaceText(schema, env.replaceText, env.replaceTextIgnoreKeys); + schema = replaceText(schema, options.replaceText, env.replaceTextIgnoreKeys); return ( diff --git a/packages/amis-core/src/store/service.ts b/packages/amis-core/src/store/service.ts index 3686ad322..5fedc1c6e 100644 --- a/packages/amis-core/src/store/service.ts +++ b/packages/amis-core/src/store/service.ts @@ -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 diff --git a/packages/amis-core/src/utils/helper.ts b/packages/amis-core/src/utils/helper.ts index 452c216ba..c40459ccf 100644 --- a/packages/amis-core/src/utils/helper.ts +++ b/packages/amis-core/src/utils/helper.ts @@ -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); + } } }); } diff --git a/packages/amis-core/src/utils/replaceText.ts b/packages/amis-core/src/utils/replaceText.ts index b1c74d20b..42c3e6925 100644 --- a/packages/amis-core/src/utils/replaceText.ts +++ b/packages/amis-core/src/utils/replaceText.ts @@ -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; } diff --git a/packages/amis/src/renderers/App.tsx b/packages/amis/src/renderers/App.tsx index 25229d3bd..3c90792c2 100644 --- a/packages/amis/src/renderers/App.tsx +++ b/packages/amis/src/renderers/App.tsx @@ -245,7 +245,11 @@ export default class App extends React.Component { 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) {