复现demo:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Infographic Shadow DOM reproduction</title> <style> body { font-family: sans-serif; margin: 20px; } #host { border: 1px solid #ddd; background: #fff; min-height: 620px; padding: 8px; } #log { margin-top: 16px; padding: 12px; background: #f7f7f7; border: 1px solid #e5e5e5; white-space: pre-wrap; font-family: Consolas, monospace; font-size: 12px; } </style> </head> <body> <h1>Shadow DOM reproduction</h1> <div id="host"></div> <div id="log"></div> <script src="./dist/infographic.min.js"></script> <script> (async function () { const logEl = document.getElementById('log'); const log = (msg) => (logEl.textContent += msg + '\n'); if (!window.AntVInfographic) { log('AntVInfographic 未加载成功,请先检查脚本路径。'); return; } const { Infographic, setDefaultFont } = window.AntVInfographic; setDefaultFont( "'Times New Roman', Times, Georgia, serif, SimSun, STSong", ); const spec = `infographic sequence-pyramid-simple data title 数字化转型层级 items - label 战略创新 desc 数据驱动决策 - label 分析平台 desc 企业洞察 - label 数据整合 desc 统一数据源 - label 基础设施 desc 云和系统基础`; const host = document.getElementById('host'); const shadowRoot = host.attachShadow({ mode: 'open' }); const style = document.createElement('style'); style.textContent = ` #container { min-height: 600px; background: #fff; } `; shadowRoot.appendChild(style); const container = document.createElement('div'); container.id = 'container'; shadowRoot.appendChild(container); try { // 复现方式 1:传 ShadowRoot const ig = new Infographic({ container: shadowRoot, width: 900, height: 600, padding: 24, }); ig.on('rendered', () => log('[OK] rendered')); ig.on('error', (err) => log('[ERR] ' + (err?.stack || err))); await ig.render(spec); } catch (err) { log('[THROW] ' + (err?.stack || err)); } })(); </script> </body> </html>
@Aarebecca 渲染问题比较好解决,但是shadowRoot还会带来编辑器的问题: 当设置 editable: true 且信息图位于 Shadow Tree 内部时,插件UI(如 EditBar 和 ResetViewBox)会渲染在组件外部,不再属于同一个 Shadow 边界。因此,尽管 API 现在可以接受 ShadowRoot 作为容器,但内置的编辑 UI 会出现错位或失效的问题。
我在 #224 用gpt5.4修了一版编辑器的,ai cr过了几轮,但是还没评估修改带来的影响。
关于编辑器shadowRoot bug的复现demo是这个,在dev中: dev/App.tsx
dev/App.tsx
export const App = () => { ///.... return ( <Flex vertical style={{ height: '100%' }}> <Radio.Group options={[ { label: '灵活组合', value: 'composite' }, { label: '模版预览', value: 'preview' }, { label: '数据项预览', value: 'item' }, { label: '流式渲染', value: 'stream' }, + { label: 'ShadowRoot 编辑态', value: 'shadow' }, ]} value={tab} onChange={(e) => handleTabChange(e.target.value)} block optionType="button" buttonStyle="solid" style={{ padding: 16 }} /> {tab === 'composite' ? ( <Composite /> ) : tab === 'preview' ? ( <Preview /> ) : tab === 'shadow' ? ( <ShadowEditableDemo /> + ) : tab === 'stream' ? ( + <StreamPreview /> ) : ( <ItemPreview /> )} </Flex> ); };
dev/ShadowEditableDemo.tsx:
dev/ShadowEditableDemo.tsx
import { getTemplates, Infographic as InfographicRenderer, type InfographicOptions, } from '@antv/infographic'; import { Alert, Button, Card, Flex, Typography } from 'antd'; import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; const COMPONENT_SELECTOR = '[data-element-type="infographic-component"]'; const HIGHLIGHT_STYLE_ID = 'shadow-editable-demo-style'; const DEMO_TEMPLATE = getTemplates().includes('list-grid-simple') ? 'list-grid-simple' : getTemplates()[0]; const DEMO_OPTIONS: InfographicOptions = { template: DEMO_TEMPLATE, editable: true, theme: 'light', svg: { style: { width: '100%', height: '100%', maxHeight: 'none', }, }, data: { title: 'ShadowRoot 编辑态演示', desc: '点击任意标题或卡片文案,观察编辑工具条实际挂载到哪里', items: [ { label: '品牌影响力', desc: '在目标用户群中具备较强认知与信任度', value: 85, }, { label: '技术研发力', desc: '拥有自研核心系统与持续创新能力', value: 90, }, { label: '市场增长快', desc: '近一年用户规模实现快速增长', value: 78, }, { label: '服务满意度', desc: '用户对服务体系整体评分较高', value: 88, }, ], }, }; type Snapshot = { bodyComponents: string[]; shadowComponents: string[]; shadowChildren: string[]; svgConnected: boolean; }; const EMPTY_SNAPSHOT: Snapshot = { bodyComponents: [], shadowComponents: [], shadowChildren: [], svgConnected: false, }; function ensureHighlightStyle(root: Document | ShadowRoot) { if (root.querySelector(`#${HIGHLIGHT_STYLE_ID}`)) return; const style = document.createElement('style'); style.id = HIGHLIGHT_STYLE_ID; style.textContent = ` ${COMPONENT_SELECTOR} { outline: 2px solid #ff7a45 !important; outline-offset: 2px; } `; if (root instanceof Document) { root.head.appendChild(style); } else { root.appendChild(style); } } function describeElement(element: Element) { const tag = element.tagName.toLowerCase(); const className = element instanceof HTMLElement && element.className ? `.${String(element.className).trim().replace(/\s+/g, '.')}` : ''; const role = element.getAttribute('data-element-type'); return role ? `${tag}${className} [${role}]` : `${tag}${className}`; } function collectComponents(root: ParentNode | null) { if (!root || !('querySelectorAll' in root)) return []; return Array.from(root.querySelectorAll(COMPONENT_SELECTOR)).map((element) => describeElement(element), ); } export const ShadowEditableDemo = () => { const hostRef = useRef<HTMLDivElement>(null); const rendererRef = useRef<InfographicRenderer | null>(null); const shadowRootRef = useRef<ShadowRoot | null>(null); const [snapshot, setSnapshot] = useState<Snapshot>(EMPTY_SNAPSHOT); const refreshSnapshot = useCallback(() => { const shadowRoot = shadowRootRef.current; const svg = shadowRoot?.querySelector('svg'); setSnapshot({ bodyComponents: collectComponents(document.body), shadowComponents: collectComponents(shadowRoot), shadowChildren: shadowRoot ? Array.from(shadowRoot.children).map((element) => describeElement(element), ) : [], svgConnected: Boolean(svg?.isConnected), }); }, []); useEffect(() => { ensureHighlightStyle(document); }, []); useEffect(() => { const host = hostRef.current; if (!host) return; const shadowRoot = host.shadowRoot ?? host.attachShadow({ mode: 'open' }); shadowRootRef.current = shadowRoot; ensureHighlightStyle(shadowRoot); if (!rendererRef.current) { const renderer = new InfographicRenderer({ ...DEMO_OPTIONS, container: shadowRoot, }); renderer.render(); rendererRef.current = renderer; } refreshSnapshot(); const bodyObserver = new MutationObserver(() => refreshSnapshot()); const shadowObserver = new MutationObserver(() => refreshSnapshot()); bodyObserver.observe(document.body, { childList: true, subtree: true, }); shadowObserver.observe(shadowRoot, { childList: true, subtree: true, }); host.addEventListener('click', refreshSnapshot); document.body.addEventListener('click', refreshSnapshot); return () => { bodyObserver.disconnect(); shadowObserver.disconnect(); host.removeEventListener('click', refreshSnapshot); document.body.removeEventListener('click', refreshSnapshot); rendererRef.current?.destroy(); rendererRef.current = null; shadowRootRef.current = null; }; }, [refreshSnapshot]); const bodyStatus = useMemo( () => snapshot.bodyComponents.length > 0 ? '检测到编辑器组件已挂到 document.body' : '尚未在 document.body 中看到编辑器组件', [snapshot.bodyComponents.length], ); return ( <Flex vertical gap={16} style={{ padding: 16, flex: 1 }}> <Alert type="warning" showIcon message="ShadowRoot 编辑态问题复现" description={ <div> <div>1. 点击下方图表中的标题或任意文本块。</div> <div>2. 观察橙色描边的编辑器组件是否出现在虚线框外部。</div> <div> 3. 再看右侧 DOM 快照:理想情况应该挂在 shadowRoot 内,但目前会跑到 document.body。 </div> </div> } /> <Flex gap={16} align="stretch" style={{ flex: 1 }}> <Card title="ShadowRoot 容器" size="small" style={{ flex: 1, minHeight: 520 }} extra={ <Button size="small" onClick={refreshSnapshot}> 刷新 DOM 快照 </Button> } > <div ref={hostRef} style={{ height: 460, border: '2px dashed #ff7875', borderRadius: 12, padding: 12, background: 'linear-gradient(180deg, rgba(255,245,245,0.95), rgba(255,255,255,0.95))', overflow: 'hidden', }} /> </Card> <Card title="DOM 快照" size="small" style={{ width: 420 }}> <Flex vertical gap={12}> <Typography.Text strong>状态</Typography.Text> <Typography.Text>{bodyStatus}</Typography.Text> <Typography.Text> SVG 是否已连接到页面树: {snapshot.svgConnected ? '是' : '否'} </Typography.Text> <Typography.Text strong>ShadowRoot 直接子节点</Typography.Text> <div style={{ paddingLeft: 12 }}> {snapshot.shadowChildren.length > 0 ? ( snapshot.shadowChildren.map((item) => <div key={item}>{item}</div>) ) : ( <div>无</div> )} </div> <Typography.Text strong> ShadowRoot 内 infographic-component 节点 </Typography.Text> <div style={{ paddingLeft: 12 }}> {snapshot.shadowComponents.length > 0 ? ( snapshot.shadowComponents.map((item) => ( <div key={`shadow-${item}`}>{item}</div> )) ) : ( <div>无</div> )} </div> <Typography.Text strong> document.body 内 infographic-component 节点 </Typography.Text> <div style={{ paddingLeft: 12 }}> {snapshot.bodyComponents.length > 0 ? ( snapshot.bodyComponents.map((item) => ( <div key={`body-${item}`}>{item}</div> )) ) : ( <div>无</div> )} </div> </Flex> </Card> </Flex> </Flex> ); };
Activity
xxMudCloudxx commentedon Mar 18, 2026
复现demo:
xxMudCloudxx commentedon Mar 20, 2026
@Aarebecca 渲染问题比较好解决,但是shadowRoot还会带来编辑器的问题:
当设置 editable: true 且信息图位于 Shadow Tree 内部时,插件UI(如 EditBar 和 ResetViewBox)会渲染在组件外部,不再属于同一个 Shadow 边界。因此,尽管 API 现在可以接受 ShadowRoot 作为容器,但内置的编辑 UI 会出现错位或失效的问题。
我在 #224 用gpt5.4修了一版编辑器的,ai cr过了几轮,但是还没评估修改带来的影响。
关于编辑器shadowRoot bug的复现demo是这个,在dev中:
dev/App.tsxexport const App = () => { ///.... return ( <Flex vertical style={{ height: '100%' }}> <Radio.Group options={[ { label: '灵活组合', value: 'composite' }, { label: '模版预览', value: 'preview' }, { label: '数据项预览', value: 'item' }, { label: '流式渲染', value: 'stream' }, + { label: 'ShadowRoot 编辑态', value: 'shadow' }, ]} value={tab} onChange={(e) => handleTabChange(e.target.value)} block optionType="button" buttonStyle="solid" style={{ padding: 16 }} /> {tab === 'composite' ? ( <Composite /> ) : tab === 'preview' ? ( <Preview /> ) : tab === 'shadow' ? ( <ShadowEditableDemo /> + ) : tab === 'stream' ? ( + <StreamPreview /> ) : ( <ItemPreview /> )} </Flex> ); };dev/ShadowEditableDemo.tsx: