From 07fb301da752a258d36f3abf35e00323fc9b5ad6 Mon Sep 17 00:00:00 2001 From: qkiroc <30946345+qkiroc@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:09:05 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=BD=93=E9=A1=B5=E9=9D=A2=E4=B8=AD?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=AF=B9=E5=BA=94id=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E6=97=B6=E6=89=8D=E5=88=A0=E9=99=A4=E6=94=B9id=E7=9A=84?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F=20(#8213)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: qinhaoyan <30946345+qinhaoyan@users.noreply.github.com> --- .../amis-core/src/components/CustomStyle.tsx | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/amis-core/src/components/CustomStyle.tsx b/packages/amis-core/src/components/CustomStyle.tsx index 5b4e4246a..aa2a3ad73 100644 --- a/packages/amis-core/src/components/CustomStyle.tsx +++ b/packages/amis-core/src/components/CustomStyle.tsx @@ -15,6 +15,8 @@ interface CustomStyleProps { env: RendererEnv; } +export const styleIdCount = new Map(); + export default function (props: CustomStyleProps) { const {config, env} = props; const {themeCss, classNames, id, defaultData, wrapperCustomStyle} = config; @@ -22,6 +24,22 @@ export default function (props: CustomStyleProps) { return null; } + useEffect(() => { + if (styleIdCount.has(id)) { + styleIdCount.set(id, styleIdCount.get(id) + 1); + } else if (id) { + styleIdCount.set(id, 1); + } + return () => { + if (styleIdCount.has(id)) { + styleIdCount.set(id, styleIdCount.get(id) - 1); + if (styleIdCount.get(id) === 0) { + styleIdCount.delete(id); + } + } + }; + }, [id]); + useEffect(() => { if (themeCss && id) { insertCustomStyle( @@ -35,7 +53,7 @@ export default function (props: CustomStyleProps) { } return () => { - if (id) { + if (id && !styleIdCount.get(id)) { removeCustomStyle('', id, env.getModalContainer?.().ownerDocument); } }; @@ -51,7 +69,7 @@ export default function (props: CustomStyleProps) { } return () => { - if (id) { + if (id && !styleIdCount.get(id)) { removeCustomStyle( 'wrapperCustomStyle', id,