feat: add registerDefaultStyleElements to register all builtin style elements - #1142
Conversation
…elements Two documentation pages already referred to a `registerDefaultStyleElements` function as the place to check which style elements maxGraph registers by default, but no such function existed. The four existing helpers (`registerDefaultEdgeMarkers`, `registerDefaultEdgeStyles`, `registerDefaultPerimeters`, `registerDefaultShapes`) had to be called one by one, and the only place listing them all was the private `Graph.registerDefaults()` override. The new function calls the four helpers, so `BaseGraph` consumers who do want every builtin get a single entry point without subclassing, and `Graph.registerDefaults()` now delegates to it instead of duplicating the list. It lives in its own module rather than in `view/style/register.ts` on purpose: adding an import of `view/shape/register-shapes.js` there would create a static edge from a module that applications import for a single edge style helper towards all sixteen builtin shapes. Bundlers drop it thanks to the side-effect-free package declaration, but the granularity varies between bundlers, so the edge is better avoided entirely. The JSDoc and the Global Configuration page both warn that registering everything defeats tree-shaking, to keep the function from reading as the recommended default.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (5)
WalkthroughThe PR adds ChangesDefault style registration
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Graph
participant registerDefaultStyleElements
participant StyleRegistries
Graph->>registerDefaultStyleElements: register default style elements
registerDefaultStyleElements->>StyleRegistries: register edge markers, edge styles, perimeters, and shapes
StyleRegistries-->>Graph: retain registered style elements
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|



Problem
Two documentation pages already referred to a
registerDefaultStyleElementsfunction as the place to check which style elements maxGraph registers by default:packages/website/docs/usage/perimeters.md: "To check the list of registered perimeters, refer to theregisterDefaultStyleElementsfunction."packages/website/docs/usage/edge-styles.md: "To check the list of registered EdgeStyles, refer to theregisterDefaultStyleElementsfunction."No such function existed. The four existing helpers (
registerDefaultEdgeMarkers,registerDefaultEdgeStyles,registerDefaultPerimetersandregisterDefaultShapes) had to be called one by one, and the only place listing them all was the privateGraph.registerDefaults()override, which is not reachable from user code.Solution
Add
registerDefaultStyleElements(), which calls the four helpers.BaseGraphconsumers who do want every builtin now get a single entry point without subclassing, andGraph.registerDefaults()delegates to it instead of duplicating the list, so there is a single source of truth.The two documentation pages quoted above now point at something that exists.
Why a dedicated module
The function lives in
packages/core/src/view/register-style-elements.tsrather than inview/style/register.ts. Adding an import ofview/shape/register-shapes.jsto the latter would create a static edge from a module that applications import for a single edge style helper (registerOrthogonalEdgeStyle, for instance) towards all sixteen builtin shapes. Bundlers drop it thanks to the side-effect-free package declaration, but the granularity of that analysis varies between bundlers, so the edge is better avoided entirely.Tree-shaking caveat
Registering everything defeats tree-shaking, which is the whole point of
BaseGraph. This is stated in the JSDoc and in a warning in the Global Configuration page, so the function does not read as the recommended default. It is meant as a convenience for prototyping, and as the first step of aGraphtoBaseGraphmigration: load everything so the application behaves as before, then trim progressively.Changes
packages/core/src/view/register-style-elements.ts: the new functionpackages/core/src/index.ts: export itpackages/core/src/view/Graph.ts:registerDefaults()delegates to itpackages/core/__tests__/view/register-style-elements.test.ts: 5 tests covering the four registries and idempotencepackages/website/docs/usage/global-configuration.md: document it alongside the four granular functions, with the tree-shaking warningNo
CHANGELOG.mdentry: this is not a breaking change.Related to #665.
Validation
npm run build -w packages/corenpm test -w packages/core: 540 tests, 61 suitesnpm run check:circular-dependencies -w packages/corenpm run lintnpm run build -w packages/websiteSummary by CodeRabbit
New Features
Documentation
Tests