Skip to content

feat(config): add reset functions for global configurations#979

Merged
tbouffard merged 5 commits intomainfrom
feat/add_reset_stencilShapeConfig
Dec 23, 2025
Merged

feat(config): add reset functions for global configurations#979
tbouffard merged 5 commits intomainfrom
feat/add_reset_stencilShapeConfig

Conversation

@tbouffard
Copy link
Copy Markdown
Member

@tbouffard tbouffard commented Dec 19, 2025

Add functions to reset configuration objects to their default values:

  • resetGlobalConfig(): restores GlobalConfig to defaults, including the original NoOpLogger and NoOpI18n instances
  • resetStencilShapeConfig(): restores StencilShapeConfig to defaults

Other changes:

  • Change StencilShape from default export to named export for better star re-export support
  • Use reset functions in Storybook preview to ensure consistent state between stories
  • Add tests for the new reset functions
  • Update documentation with new reset functions

These reset functions are useful for testing scenarios and Storybook stories where configuration state needs to be restored between runs.

Summary by CodeRabbit

  • New Features

    • Added ability to reset global configuration and stencil-shape configuration back to defaults; widened public API surface for stencil shapes.
  • Bug Fixes / Tests

    • Expanded tests to validate full configuration restoration after resets.
  • Documentation

    • Updated global-configuration docs to include reset capabilities and minor clarity/grammar improvements.

✏️ Tip: You can customize this high-level summary in your review settings.

  - Add resetGlobalConfig to restore GlobalConfig to default values, including the original NoOpLogger and NoOpI18n instances
  - Add resetStencilShapeConfig to restore StencilShapeConfig to defaults
  - Update resetStyleDefaultsConfig test for consistency
@tbouffard tbouffard added the enhancement New feature or request label Dec 19, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Dec 19, 2025

Walkthrough

Adds exported reset functions to restore GlobalConfig, StyleDefaultsConfig, and StencilShapeConfig to default snapshots; converts StencilShape to a named export and updates imports/exports, docs, Storybook preview, and tests to validate full-object restoration.

Changes

Cohort / File(s) Change Summary
Tests
packages/core/__tests__/util/config.test.ts, packages/core/__tests__/view/shape/stencil/StencilShape.test.ts
New/enhanced tests mutate configs, call reset functions, and assert individual fields and full config objects are restored.
Global config implementation
packages/core/src/util/config.ts
Added resetGlobalConfig() and defaultGlobalConfig; uses shallowCopy to restore GlobalConfig, logger, and i18n.
Stencil shape implementation
packages/core/src/view/shape/stencil/StencilShape.ts, packages/core/src/view/shape/StencilShapeRegistry.ts, packages/core/src/view/shape/Shape.ts
Added resetStencilShapeConfig() and defaultStencilShapeConfig; changed StencilShape to a named export and updated type imports/usages.
Public API / Barrel & docs
packages/core/src/index.ts, packages/website/docs/usage/global-configuration.md
Broadened exports (export *) to expose stencil APIs and reset functions; docs updated to document resetGlobalConfig() and resetStencilShapeConfig() and minor copy edits.
Storybook / preview usage
packages/html/.storybook/preview.ts
Imported and invoked resetGlobalConfig() and resetStencilShapeConfig() in the global reset sequence (after unregisterAllStencilShapes).

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Preview as Storybook Preview
  participant Core as `@maxgraph/core`
  participant Registry as StencilRegistry
  participant LoggerI18n as Logger/I18n

  rect rgb(240,248,255)
    Preview->>Core: import resetGlobalConfig(), resetStencilShapeConfig()
    Preview->>Core: call resetGlobalConfig()
    Core->>LoggerI18n: shallowCopy default logger/i18n into GlobalConfig
    Core-->>Preview: GlobalConfig restored
  end

  rect rgb(245,255,240)
    Preview->>Registry: unregisterAllStencilShapes()
    Registry-->>Preview: all stencils removed
    Preview->>Core: call resetStencilShapeConfig()
    Core->>Registry: shallowCopy defaultStencilShapeConfig -> StencilShapeConfig
    Core-->>Preview: StencilShapeConfig restored
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description lacks required sections from the template such as PR Checklist, issue reference, scope justification, test additions, and documentation updates. Complete the PR description by adding: issue reference (closes #xxx), PR checklist items, justification of scope, test additions, documentation changes, and ensure it follows the required template structure.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding reset functions for global configurations, which aligns with the changeset's primary objective.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/add_reset_stencilShapeConfig

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/core/__tests__/util/config.test.ts (1)

17-17: Consider removing unused import.

The describe function is imported but not used in this test file. Consider removing it to keep the imports clean.

🔎 Proposed fix
-import { describe, expect, test } from '@jest/globals';
+import { expect, test } from '@jest/globals';
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3f073e2 and 0004d35.

📒 Files selected for processing (6)
  • packages/core/__tests__/util/config.test.ts (2 hunks)
  • packages/core/__tests__/view/shape/stencil/StencilShape.test.ts (1 hunks)
  • packages/core/src/index.ts (1 hunks)
  • packages/core/src/util/config.ts (1 hunks)
  • packages/core/src/view/shape/stencil/StencilShape.ts (2 hunks)
  • packages/website/docs/usage/global-configuration.md (4 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Use single quotes in TypeScript/JavaScript code (Prettier config: singleQuote: true).

Files:

  • packages/core/__tests__/view/shape/stencil/StencilShape.test.ts
  • packages/core/__tests__/util/config.test.ts
  • packages/core/src/util/config.ts
  • packages/core/src/view/shape/stencil/StencilShape.ts
  • packages/core/src/index.ts
packages/core/__tests__/**/*.test.ts

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Test files must follow the naming pattern **/*.test.ts and be located in packages/core/__tests__/ directory.

Files:

  • packages/core/__tests__/view/shape/stencil/StencilShape.test.ts
  • packages/core/__tests__/util/config.test.ts
**/*.{ts,tsx,js,jsx,json,md}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Format code using Prettier with tab width 2, trailing comma ES5, print width 90, and end of line auto.

Files:

  • packages/core/__tests__/view/shape/stencil/StencilShape.test.ts
  • packages/website/docs/usage/global-configuration.md
  • packages/core/__tests__/util/config.test.ts
  • packages/core/src/util/config.ts
  • packages/core/src/view/shape/stencil/StencilShape.ts
  • packages/core/src/index.ts
packages/core/__tests__/**/*.ts

📄 CodeRabbit inference engine (CLAUDE.md)

packages/core/__tests__/**/*.ts: Import paths in tests should omit the .js extension (handled by moduleNameMapper)
Tests should be located in packages/core/__tests__/ mirroring the structure of src/ directory

Files:

  • packages/core/__tests__/view/shape/stencil/StencilShape.test.ts
  • packages/core/__tests__/util/config.test.ts
packages/core/src/**/*.ts

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

packages/core/src/**/*.ts: Include .js file extensions in all import statements within packages/core/src/ to comply with ESLint rule n/file-extension-in-import.
Do not use console statements in any code. Use proper logging mechanisms instead (ESLint rule: no-console: error).
Do not use eval() function in any code (ESLint rule: no-eval: error).
Do not use const enums. Convert all const enums to regular enums (ESLint rule forbids const enums).
TypeScript source files in core package must strictly maintain typing with no implicit types and all functions having explicit return type annotations.

packages/core/src/**/*.ts: Use the isNullish function from internal/utils.js when checking for null or undefined on variables that can have falsy values (numbers, strings, booleans)
Use the log() function from internal/utils.js instead of accessing GlobalConfig.logger directly
Use functions from internal/i18n-utils.js (translate, isI18nEnabled) instead of accessing GlobalConfig.i18n directly
Wrap multiple model changes in batchUpdate() to optimize graph updates
Styles should be defined as objects conforming to the CellStyle type
Shapes must be registered via registries (e.g., ShapeRegistry) before use; Graph class auto-registers default shapes, but BaseGraph requires manual registration

Files:

  • packages/core/src/util/config.ts
  • packages/core/src/view/shape/stencil/StencilShape.ts
  • packages/core/src/index.ts
🧠 Learnings (14)
📓 Common learnings
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 582
File: packages/core/__tests__/view/handler/config.test.ts:0-0
Timestamp: 2024-11-24T17:28:08.023Z
Learning: In `packages/core/__tests__/view/handler/config.test.ts`, when writing Jest tests in TypeScript for configuration reset functions, small duplication is acceptable if it maintains test clarity and readability. Avoid refactoring tests in ways that could make them harder to understand, especially when future use cases might require specific checks.
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 849
File: packages/html/stories/DragSource.stories.js:98-101
Timestamp: 2025-06-13T07:48:10.300Z
Learning: User tbouffard prefers answers in English; avoid switching to other languages in future replies.
📚 Learning: 2025-02-08T16:39:11.178Z
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 660
File: packages/core/src/view/geometry/node/StencilShapeRegistry.ts:0-0
Timestamp: 2025-02-08T16:39:11.178Z
Learning: The StencilShapeRegistry.getStencil method's implementation using non-null assertion (!) is validated by dedicated tests to handle null/undefined inputs correctly, returning undefined in such cases.

Applied to files:

  • packages/core/__tests__/view/shape/stencil/StencilShape.test.ts
  • packages/website/docs/usage/global-configuration.md
  • packages/core/src/view/shape/stencil/StencilShape.ts
  • packages/core/src/index.ts
📚 Learning: 2024-11-24T17:28:08.023Z
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 582
File: packages/core/__tests__/view/handler/config.test.ts:0-0
Timestamp: 2024-11-24T17:28:08.023Z
Learning: In `packages/core/__tests__/view/handler/config.test.ts`, when writing Jest tests in TypeScript for configuration reset functions, small duplication is acceptable if it maintains test clarity and readability. Avoid refactoring tests in ways that could make them harder to understand, especially when future use cases might require specific checks.

Applied to files:

  • packages/core/__tests__/view/shape/stencil/StencilShape.test.ts
  • packages/core/__tests__/util/config.test.ts
  • packages/core/src/util/config.ts
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: Applies to packages/core/src/**/*.ts : Shapes must be registered via registries (e.g., `ShapeRegistry`) before use; `Graph` class auto-registers default shapes, but `BaseGraph` requires manual registration

Applied to files:

  • packages/core/__tests__/view/shape/stencil/StencilShape.test.ts
  • packages/website/docs/usage/global-configuration.md
  • packages/core/src/view/shape/stencil/StencilShape.ts
  • packages/core/src/index.ts
📚 Learning: 2025-04-24T12:33:36.243Z
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 776
File: packages/core/src/view/Graph.ts:77-83
Timestamp: 2025-04-24T12:33:36.243Z
Learning: All register functions in maxGraph (`registerDefaultShapes`, `registerDefaultEdgeStyles`, `registerDefaultPerimeters`, and `registerDefaultEdgeMarkers`) have internal state flags to prevent duplicate registrations, making them inherently idempotent.

Applied to files:

  • packages/website/docs/usage/global-configuration.md
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: For prototyping/demos, use `Graph` for automatic setup; for production, use `BaseGraph` with explicit feature registration

Applied to files:

  • packages/website/docs/usage/global-configuration.md
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: Applies to packages/core/src/**/*.ts : Use functions from `internal/i18n-utils.js` (translate, isI18nEnabled) instead of accessing `GlobalConfig.i18n` directly

Applied to files:

  • packages/core/__tests__/util/config.test.ts
  • packages/core/src/util/config.ts
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: Applies to packages/core/__tests__/**/*.ts : Import paths in tests should omit the `.js` extension (handled by moduleNameMapper)

Applied to files:

  • packages/core/__tests__/util/config.test.ts
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: Applies to packages/core/src/**/*.ts : Use the `log()` function from `internal/utils.js` instead of accessing `GlobalConfig.logger` directly

Applied to files:

  • packages/core/__tests__/util/config.test.ts
  • packages/core/src/util/config.ts
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: Applies to packages/core/__tests__/**/*.ts : Tests should be located in `packages/core/__tests__/` mirroring the structure of `src/` directory

Applied to files:

  • packages/core/__tests__/util/config.test.ts
📚 Learning: 2025-12-10T13:43:28.685Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-10T13:43:28.685Z
Learning: Applies to packages/core/__tests__/**/*.test.ts : Test files must follow the naming pattern `**/*.test.ts` and be located in `packages/core/__tests__/` directory.

Applied to files:

  • packages/core/__tests__/util/config.test.ts
📚 Learning: 2025-12-10T13:43:28.685Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-10T13:43:28.685Z
Learning: Applies to packages/core/package.json : Configure imports and exports to use ES2020 module format, with conditional exports supporting both ESM and CommonJS through proper package.json exports field.

Applied to files:

  • packages/core/__tests__/util/config.test.ts
📚 Learning: 2024-11-23T15:15:49.541Z
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 582
File: packages/core/__tests__/util/config.test.ts:20-33
Timestamp: 2024-11-23T15:15:49.541Z
Learning: In `StyleDefaultsConfig`, negative offset values are acceptable; there are no invalid values for properties like `shadowOffsetX` and `shadowOffsetY`, which can be negative.

Applied to files:

  • packages/core/__tests__/util/config.test.ts
📚 Learning: 2024-11-23T16:12:51.380Z
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 582
File: packages/core/src/util/config.ts:0-0
Timestamp: 2024-11-23T16:12:51.380Z
Learning: The `shallowCopy` function in `cloneUtils.ts` is intentionally designed to perform a shallow copy on objects that contain only primitive properties, and deep copying is not necessary.

Applied to files:

  • packages/core/src/view/shape/stencil/StencilShape.ts
🧬 Code graph analysis (3)
packages/core/__tests__/view/shape/stencil/StencilShape.test.ts (2)
packages/core/src/index.ts (2)
  • StencilShapeConfig (127-127)
  • resetStencilShapeConfig (125-125)
packages/core/src/view/shape/stencil/StencilShape.ts (2)
  • StencilShapeConfig (40-56)
  • resetStencilShapeConfig (66-68)
packages/core/__tests__/util/config.test.ts (3)
packages/core/src/util/config.ts (3)
  • GlobalConfig (45-83)
  • resetGlobalConfig (93-95)
  • StyleDefaultsConfig (104-188)
packages/core/src/util/logger.ts (1)
  • ConsoleLogger (55-104)
packages/core/src/index.ts (1)
  • TranslationsAsI18n (133-133)
packages/core/src/util/config.ts (1)
packages/core/src/internal/clone-utils.ts (1)
  • shallowCopy (30-43)
🪛 markdownlint-cli2 (0.18.1)
packages/website/docs/usage/global-configuration.md

31-31: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)


32-32: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)


33-33: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: build (macos-14)
  • GitHub Check: build (windows-2022)
  • GitHub Check: build (ubuntu-24.04)
🔇 Additional comments (10)
packages/core/src/util/config.ts (2)

35-35: LGTM!

The import of shallowCopy with the .js extension is correct and follows the coding guidelines.


85-95: LGTM! Well-designed reset function.

The implementation correctly captures a snapshot of GlobalConfig at module initialization time, preserving the original NoOpI18n and NoOpLogger instances. The shallowCopy function will restore both primitive properties and object references, ensuring the original instances are restored as intended.

The pattern is consistent with resetStyleDefaultsConfig below and the JSDoc is clear and complete.

packages/core/__tests__/view/shape/stencil/StencilShape.test.ts (1)

1-33: LGTM! Comprehensive test coverage.

The test correctly verifies the reset behavior by:

  1. Capturing the original configuration
  2. Mutating multiple properties (allowEval and defaultLocalized)
  3. Calling the reset function
  4. Asserting both a specific property and full equality

The imports follow test conventions (no .js extensions), and the test structure is clear and easy to understand. Based on learnings, small duplication is acceptable if it maintains test clarity and readability.

packages/core/src/view/shape/stencil/StencilShape.ts (2)

31-31: LGTM!

The import correctly includes the .js extension as required by the coding guidelines for files in packages/core/src/.


58-68: LGTM! Consistent implementation.

The resetStencilShapeConfig() function follows the same pattern as resetGlobalConfig() and resetStyleDefaultsConfig(), using a snapshot approach with shallowCopy to restore default values. The implementation is clean and the JSDoc is complete.

packages/website/docs/usage/global-configuration.md (2)

31-33: LGTM! Documentation properly updated.

The new reset functions resetGlobalConfig and resetStencilShapeConfig are correctly added to the list with appropriate version tags (since 0.23.0). The formatting is consistent with other entries.

Note: The markdownlint warnings (MD007) about list indentation are false positives. The 2-space indentation is correct for nested list items in Markdown.


47-47: LGTM! Minor clarity improvements.

The wording adjustments enhance readability without changing the meaning:

  • "allow" → "allows you" (line 47)
  • Clarified statement about default stencil shapes (line 63)
  • "XML object" → "XML objects" for grammatical correctness (line 88)

Also applies to: 63-63, 88-88

packages/core/__tests__/util/config.test.ts (2)

27-46: LGTM! Excellent test coverage.

The test thoroughly verifies the reset behavior by:

  1. Capturing the original configuration and specific logger/i18n instances
  2. Replacing them with new instances (ConsoleLogger and TranslationsAsI18n)
  3. Verifying the mutation occurred
  4. Calling resetGlobalConfig()
  5. Asserting that the exact original instances are restored (not just equivalent objects)
  6. Verifying full configuration equality

The test correctly validates that the original NoOpLogger and NoOpI18n instances are restored, which is the key requirement mentioned in the PR description.


48-62: LGTM! Enhanced test consistency.

The addition of the full equality check with toStrictEqual(originalConfig) on line 61 makes this test consistent with the new resetGlobalConfig test. This ensures both specific property checks and comprehensive configuration equality validation.

packages/core/src/index.ts (1)

124-128: LGTM! Public API correctly extended.

The resetStencilShapeConfig export is properly added to the StencilShape export block, making it part of the public API. The formatting is consistent with the rest of the file.

@sonarqubecloud
Copy link
Copy Markdown

@tbouffard tbouffard merged commit e05cde5 into main Dec 23, 2025
12 checks passed
@tbouffard tbouffard deleted the feat/add_reset_stencilShapeConfig branch December 23, 2025 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant