Skip to content

feat: add ResetViewBox plugin - #192

Merged
Aarebecca merged 7 commits into
antvis:mainfrom
xxMudCloudxx:feat/reset-viewbox-plugin
Feb 3, 2026
Merged

Aarebecca merged 7 commits into
antvis:mainfrom
xxMudCloudxx:feat/reset-viewbox-plugin

Conversation

@xxMudCloudxx

@xxMudCloudxx xxMudCloudxx commented Jan 30, 2026 •

Copy link
Copy Markdown
Contributor

功能

提供“重置视图”的UI插件。当用户对画布进行缩放(Zoom)或平移(Pan)操作导致视图发生变化时,该插件会在画布右下角显示一个浮动的重置按钮。点击该按钮可将视图恢复到最佳适配(Fit View)状态或初始状态。

功能特性

  • 自动检测: 监听 viewBox:change 事件,仅在视图偏离初始状态时显示按钮。
  • 智能定位: 自动计算 SVG 画布在屏幕上的位置,将按钮定位在画布右下角。
  • 响应式: 监听窗口 resize 事件,自动调整初始状态基准和按钮位置。
  • 可配置: 支持自定义按钮的样式、类名以及挂载容器。
  • 命令集成: 点击重置使用 UpdateOptionsCommand,因此该操作支持撤销/重做(Undo/Redo)。

配置项

属性 类型 说明
style Partial<CSSStyleDeclaration> (可选) 自定义按钮的内联样式对象。
className string (可选) 添加到按钮上的额外的 CSS 类名。
getContainer HTMLElement | (() => HTMLElement) (可选) 指定按钮挂载的父容器。默认为 document.body。

样式

默认样式包含在 constant RESET_BUTTON_CLASS 中,样式 ID 为 RESET_BUTTON_STYLE_ID。

  • 宽/高: 40px
  • 位置: 绝对定位,SVG 右下角内缩 25px
  • 外观: 白色背景,圆形,带阴影悬浮效果和hover变灰

自定义配置项使用示例

// 在编辑器实例化时引入插件
import { Infographic, ResetViewBox } from '@antv/infographic';

const instance = new Infographic({
  // 直接在顶层配置 plugins(注意是复数)
  plugins: [
    new ResetViewBox({
      className: 'my-custom-reset-btn',
      style: { backgroundColor: '#f0f0f0' }
    })
  ],
  
  // ... 其他配置项
  container: '#root',
  data: myData,
});

// 渲染
instance.render();

已解决的问题

  • state层耦合性问题
  • 滚轮缩放的undo/redo栈污染

TODO / Dependencies

  • 验证功能完好
  • 完成 ResetViewBox插件的防御性编程审查
  • 完善单元测试

预览

image
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @xxMudCloudxx, 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 introduces a new ResetViewBox plugin, enhancing the editor's usability by providing a dedicated mechanism to revert the SVG canvas to its original zoom and pan state. It also refines the existing ZoomWheel interaction to address issues with undo/redo stack pollution and state management, ensuring a smoother user experience. Additionally, common UI components have been reorganized into a shared directory, promoting better code structure and reusability across plugins.

Highlights

  • New ResetViewBox Plugin: Introduced a new plugin that provides a UI button to reset the SVG editor's viewBox to its initial state, enhancing navigation and user control.
  • Improved ZoomWheel Interaction: Refactored the ZoomWheel interaction to address undo/redo stack pollution by consolidating zoom actions into a single command upon key release, and improved state management by using state.updateOptions.
  • Component Refactoring: Moved shared UI components (Button, ColorPicker, Icons, Popover, Select) from src/editor/plugins/edit-bar/components to a more general src/editor/plugins/components directory, promoting reusability.
  • ViewBox Change Event: Implemented a viewBox:change event in the state manager, allowing other parts of the application, like the new ResetViewBox plugin, to react to changes in the SVG's view.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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. ↩

@xxMudCloudxx

Copy link
Copy Markdown
Contributor Author

原来Draft PR也会触发CI/CR

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

本次PR引入了ResetViewBox插件,并重构了滚轮缩放功能以解决undo/redo栈污染问题,整体实现很棒。我发现了一些可以改进的地方,主要涉及事件负载的类型安全、代码可读性和魔法数的处理。具体建议请见评论。

Comment thread src/editor/managers/state.ts Resolved
Comment thread src/editor/types/interaction.ts Resolved
Comment thread src/editor/plugins/reset-viewbox.ts Resolved
Comment thread src/editor/plugins/reset-viewbox.ts Resolved
@xxMudCloudxx

Copy link
Copy Markdown
Contributor Author

/gemini review

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

这个 PR 引入了 ResetViewBox 插件,并重构了滚轮缩放功能以解决 undo/redo 栈污染问题,做得很好。通过将缩放时的状态更新直接交由 state.updateOptions 处理,避免了为每一次缩放步长都创建命令,然后在缩放结束后(通过监听 keyup 事件)创建单个命令记录到历史记录中,这个实现非常巧妙。

不过,在 StateManager 的实现中存在一些问题。将自定义的 applyOptionUpdates 函数替换为 lodash.merge 导致了部分重要功能的回归,例如无法通过传入 undefined 来删除属性。此外,viewBox 和 padding 的更新逻辑也存在缺陷。我还对新的 ResetViewBox 插件的样式实现提出了一些改进建议。请查看具体的审查评论。

Comment thread src/editor/managers/state.ts Resolved
Comment thread src/editor/managers/state.ts Resolved
Comment thread src/editor/plugins/reset-viewbox.ts Resolved
@codecov-commenter

codecov-commenter commented Jan 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.62963% with 44 lines in your changes missing coverage. Please review.
✅ Project coverage is 45.57%. Comparing base (bb8761a) to head (97716f8).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/editor/plugins/reset-viewbox.ts 82.22% 24 Missing ⚠️
src/editor/editor.ts 7.69% 12 Missing ⚠️
src/utils/padding.ts 11.11% 8 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #192      +/-   ##
==========================================
+ Coverage   45.22%   45.57%   +0.34%     
==========================================
  Files         334      335       +1     
  Lines       26635    26820     +185     
  Branches     2115     2151      +36     
==========================================
+ Hits        12046    12222     +176     
- Misses      14579    14588       +9     
  Partials       10       10              
Flag Coverage Δ
infographic 45.57% <79.62%> (+0.34%) ⬆️

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

Files with missing lines Coverage Δ
src/editor/interactions/zoom-wheel.ts 100.00% <100.00%> (ø)
src/editor/managers/state.ts 98.67% <100.00%> (+0.04%) ⬆️
src/editor/plugins/components/button.ts 91.42% <100.00%> (ø)
src/editor/plugins/components/color-picker.ts 17.32% <100.00%> (ø)
src/editor/plugins/components/icons.ts 100.00% <100.00%> (ø)
src/editor/plugins/components/index.ts 100.00% <ø> (ø)
src/editor/plugins/components/popover.ts 2.63% <100.00%> (ø)
src/editor/plugins/components/select.ts 6.76% <100.00%> (ø)
...itor/plugins/edit-bar/edit-items/align-elements.ts 6.16% <100.00%> (ø)
...c/editor/plugins/edit-bar/edit-items/font-align.ts 20.93% <100.00%> (ø)
... and 12 more

... and 1 file 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 changed the title [Draft] feat: add ResetViewBox plugin Jan 30, 2026
@xxMudCloudxx

xxMudCloudxx commented Feb 2, 2026 •

Copy link
Copy Markdown
Contributor Author

@Aarebecca 我把原来的undefine视为删除的业务逻辑加回来了,同时先前的 StateManager承担了两个职责:

  • 管理状态 (Model):维护 this.options 数据。
  • 更新视图 (View/Controller):直接操作 DOM(getDocument().setAttribute)。

我做了以下重构:

  • StateManager 职责纯化:消除了 StateManager 对视图层(DOM 操作)的直接依赖。移除了 setAttribute、removeAttribute 以及 setSVGPadding 的调用,改为派发 viewBox:change 和 padding:change 事件。
  • Editor 响应式更新:在 Editor构造函数中引入了事件监听逻辑。通过统一的监听器处理 viewBox 和 padding 的视觉呈现。

但 DOM 同步逻辑目前还是静态地实现在 Editor 构造函数中(针对 viewBox 和 padding, 虽然padding目前没有交互插件和插件修改,但是我还是保留了下来), 我想后续可以增加一个command来让Editor动态监听这些属性的修改,而不是写死在Editor中,这样子就可以让使用者通过第三方插件或自定义交互引入了新的配置项(例如自定义背景色、滤镜等)时,不需要修改Editor的代码。
最近几天我出门了,到时候回家的时候我想一下方案再提一个新的pr来实现

@Aarebecca
Aarebecca merged commit 31047f1 into antvis:main Feb 3, 2026
1 check passed
@Aarebecca

Copy link
Copy Markdown
Contributor

@xxMudCloudxx 效果不错🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants