Skip to content

feat:add Shadow Root support for the editor#224

Open
xxMudCloudxx wants to merge 3 commits intoantvis:mainfrom
xxMudCloudxx:fix/shadow-root-editor
Open

feat:add Shadow Root support for the editor#224
xxMudCloudxx wants to merge 3 commits intoantvis:mainfrom
xxMudCloudxx:fix/shadow-root-editor

Conversation

@xxMudCloudxx
Copy link
Copy Markdown
Contributor

为编辑器增加对shadow root的支持。

前置pr:#223

@xxMudCloudxx
Copy link
Copy Markdown
Contributor Author

最近有点忙,我先hold一下

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the editor's compatibility with Shadow DOM, allowing it to be seamlessly integrated into web components and other encapsulated environments. It ensures that all editor functionalities, including user interactions, UI overlays, and style management, operate correctly when the editor is hosted within a ShadowRoot, thereby improving modularity and preventing conflicts with the surrounding page's styles and DOM structure.

Highlights

  • Shadow DOM Support: The editor now fully supports being embedded within a Shadow DOM, ensuring proper functionality and style encapsulation.
  • Interaction Handling: The interaction manager has been updated to correctly detect and respond to events originating from elements within a ShadowRoot.
  • Overlay Component Placement: Editor overlay components like Popovers, EditBars, and ResetViewBox buttons can now automatically detect and render within the appropriate ShadowRoot or a specified container.
  • Dynamic Style Injection: Styles for editor components are now dynamically injected into the correct ShadowRoot or document head, preventing global style pollution.
  • SVG Padding Utility: The setSVGPadding utility function has been enhanced to correctly calculate and apply viewBox adjustments for SVG elements located inside a ShadowRoot.
  • New Shadow DOM Utilities: Introduced new utility functions (getOverlayContainer, eventPathContains) to streamline Shadow DOM interaction and event path traversal.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces comprehensive support for Shadow DOM across various editor components and utilities. Key changes include updating InteractionManager to correctly handle click events within shadow roots using event.composedPath() and a new eventPathContains utility. The Popover component, EditBar plugin, and ResetViewBox plugin have been refactored to properly inject styles and append overlay elements into either a specified custom container or the SVG's shadow root, leveraging new OverlayRoot types and getOverlayContainer utility. Style injection functions (injectStyleOnce, ensureEditorStyles, ensureIconButtonStyle, ensureColorPickerStyles, ensurePopoverStyle, ensureFontColorStyles, ensureFontFamilyListStyle, ensureButtonStyle) now accept an optional target node to determine the correct root for style insertion. Additionally, parseOptions and related types have been updated to accept ShadowRoot instances as valid containers, and setSVGPadding now correctly computes viewBox for SVGs within shadow roots. New test cases have been added to validate these Shadow DOM behaviors for InteractionManager, Popover, EditBar, ResetViewBox, parseOptions, and setSVGPadding.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 19, 2026

Codecov Report

❌ Patch coverage is 84.57143% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 49.26%. Comparing base (ea238f5) to head (96710f4).

Files with missing lines Patch % Lines
src/editor/plugins/edit-bar/edit-bar.ts 40.90% 13 Missing ⚠️
...c/editor/plugins/edit-bar/edit-items/icon-color.ts 0.00% 7 Missing ⚠️
...itor/plugins/edit-bar/edit-items/align-elements.ts 0.00% 6 Missing ⚠️
src/editor/plugins/components/popover.ts 96.29% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #224      +/-   ##
==========================================
+ Coverage   46.62%   49.26%   +2.63%     
==========================================
  Files         342      343       +1     
  Lines       28261    28376     +115     
  Branches     2368     2544     +176     
==========================================
+ Hits        13177    13979     +802     
+ Misses      15072    14386     -686     
+ Partials       12       11       -1     
Flag Coverage Δ
infographic 49.26% <84.57%> (+2.63%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/editor/interactions/dblclick-edit-text.ts 77.77% <100.00%> (+0.11%) ⬆️
src/editor/managers/interaction.ts 76.59% <100.00%> (+0.37%) ⬆️
src/editor/plugins/components/button.ts 91.89% <100.00%> (+0.46%) ⬆️
src/editor/plugins/components/color-picker.ts 80.78% <100.00%> (+63.46%) ⬆️
...c/editor/plugins/edit-bar/edit-items/font-align.ts 74.72% <100.00%> (+53.79%) ⬆️
...c/editor/plugins/edit-bar/edit-items/font-color.ts 85.91% <100.00%> (+75.30%) ⬆️
.../editor/plugins/edit-bar/edit-items/font-family.ts 81.90% <100.00%> (+72.99%) ⬆️
...rc/editor/plugins/edit-bar/edit-items/font-size.ts 79.38% <100.00%> (+63.07%) ⬆️
src/editor/plugins/edit-bar/edit-items/types.ts 100.00% <ø> (ø)
src/editor/plugins/reset-viewbox.ts 85.63% <100.00%> (+9.19%) ⬆️
... and 8 more

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@xxMudCloudxx xxMudCloudxx force-pushed the fix/shadow-root-editor branch from b7002dd to 3954ed3 Compare March 20, 2026 08:33
@xxMudCloudxx
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces comprehensive support for Shadow DOM, which is a significant enhancement for the editor's flexibility and encapsulation. The changes correctly address several key aspects of Shadow DOM integration, including event handling, style injection, and overlay placement. The new getOverlayContainer and eventPathContains utilities provide robust solutions for navigating and interacting with elements across shadow boundaries. Furthermore, the refactoring of injectStyleOnce to support targeted style injection into ShadowRoot instances ensures proper style encapsulation. The updated components and their associated tests demonstrate a thorough approach to implementing this feature. Overall, the changes are well-implemented and contribute positively to the editor's architecture.

@xxMudCloudxx
Copy link
Copy Markdown
Contributor Author

📝 Change List (Shadow-root Support)

📂 文件级工具 (Core Utils)

  • src/editor/utils/root.ts:1
    • 新增 overlay 根工具。
    • getOverlayContainer(): 负责从任意节点回溯到 ShadowRootdocument.body
    • eventPathContains(): 统一处理 composedPath() 与普通 DOM 路径的包含关系判断。
  • src/editor/utils/index.ts:9
    • 导出新 root 工具,供编辑器各插件复用。
  • src/utils/style.ts:3
    • injectStyleOnce(): 能力增强,支持从“仅注入 document.head”扩展为“可注入 DocumentShadowRoot”。

🖱️ 交互层 (Interaction Layer)

  • src/editor/managers/interaction.ts:131
    • 优化点击激活逻辑:将 doc.contains(target) 替换为 composedPath + eventPathContains,修复 ShadowRoot 内点击 SVG/组件被误判为外部点击的问题。
  • src/editor/interactions/dblclick-edit-text.ts:91
    • 内联文本编辑器样式注入逻辑改为跟随目标文本所在 root,防止编辑态样式在 ShadowRoot 下丢失。

🧩 基础组件 (Base Components)

  • src/editor/plugins/components/popover.ts
    • L37: getContainer 类型扩展为 HTMLElement | ShadowRoot;默认挂载目标从 document.body 改为 getOverlayContainer(trigger)
    • L157: 在 Portal 模式下新增基于实际挂载容器的本地坐标换算;当 Popover 挂载到 ShadowRoot 或自定义容器时,会以对应宿主/父容器为基准计算 left/top,修复弹层本应显示在 edit-bar 上方却错误偏移到下方的问题。
    • L213: 外部点击关闭逻辑改为基于事件路径判断,解决 ShadowRoot Portal 内点击内容导致 Popover 误关闭的问题。
  • src/editor/plugins/components/button.ts:8
    • IconButton 新增 root 参数,确保按钮样式注入到正确作用域。
  • src/editor/plugins/components/color-picker.ts:5
    • ColorPicker 新增 root 参数,支持颜色面板样式进入 ShadowRoot。

🏗️ Edit Bar 主体 (Edit Bar Core)

  • src/editor/plugins/edit-bar/edit-bar.ts
    • L33: EditBarOptions.getContainer 类型扩展支持 ShadowRoot
    • L142: 编辑条容器创建时移除默认固定挂载 document.body 的逻辑。
    • L238: 新增 resolveOverlayRoot(),默认从编辑器 SVG 所在 root 推导 overlay 容器。
  • src/editor/plugins/edit-bar/edit-items/types.ts:4
    • EditItemOptions.root: 将 root 作为统一上下文透传给所有编辑项。

🎨 Edit Bar 子项 (Edit Bar Items)

  • 对齐与布局: align-elements.ts, font-align.ts 改为 root 感知,透传样式与 Popover 容器。
  • 文字属性: font-color.ts, font-family.ts, font-size.ts 均支持 root 参数,确保 grid 样式、颜色选择器、下拉列表及其 Popover 跟随正确宿主。
  • 图标属性: icon-color.ts 颜色面板增加 root 支持。

⚙️ 其他插件 (Other Plugins)

  • src/editor/plugins/reset-viewbox.ts
    • 类型扩展支持 ShadowRoot
    • 新增 resolveOverlayRoot() 逻辑。
    • 修复按钮样式注入目标,解决按钮在 ShadowRoot 下样式丢失或挂载位置错误的问题。

✅ 测试用例 (Testing)

  • interaction-manager.test.ts: 新增 ShadowRoot 点击路径激活测试。
  • popover.test.ts: 新增 ShadowRoot portal 内点击不关闭测试。
  • overlay-root.test.ts:
    • 验证 EditBar 默认将子 overlay 和样式挂载到 SVG 的 ShadowRoot。
    • 验证 ResetViewBox 在自定义容器与默认 ShadowRoot 场景下的兼容性。

@xxMudCloudxx xxMudCloudxx marked this pull request as ready for review March 27, 2026 10:24
@xxMudCloudxx xxMudCloudxx added resolved This issue has been resolved and is now available in the latest release labels Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

resolved This issue has been resolved and is now available in the latest release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants