Skip to content

Comments

feat: add get default props#1281

Merged
yiiqii merged 5 commits intofeat/2.8from
feat/add-getDefaultProps
Nov 24, 2025
Merged

feat: add get default props#1281
yiiqii merged 5 commits intofeat/2.8from
feat/add-getDefaultProps

Conversation

@Fryt1
Copy link
Contributor

@Fryt1 Fryt1 commented Nov 21, 2025

增加文本组建默认值,为item.addComponent()做支持

Summary by CodeRabbit

  • Refactor

    • Restructured text initialization to a defaults-based flow with consistent state resets and safer resource disposal.
    • Streamlined update paths for layout and style, making layout width mutable and delegating initialization to update methods.
    • Style updates now reset outline/shadow state and copy color values to avoid shared references.
  • New Features

    • Added a public line-height setter for validated, safe line-height changes.

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 21, 2025

Walkthrough

Text component initialization was refactored to use internal default props and explicit reset/dispose lifecycle steps; TextLayout and TextStyle gained mutable fields and update(options) methods, with constructors delegating to those update methods and color values copied on update.

Changes

Cohort / File(s) Summary
TextComponent initialization refactoring
packages/effects-core/src/plugins/text/text-item.ts
Added private getDefaultProps() to supply default spec.TextComponentData; constructor changed to constructor(engine: Engine) and initializes from defaults. Introduced resetState() and disposeTextTexture() to clear textures/state. fromData now calls resetState before applying options. updateWithOptions made lazy for textStyle/textLayout. Added public setLineHeight and an empty public constructor on TextComponentBase.
TextLayout lazy update
packages/effects-core/src/plugins/text/text-layout.ts
Made maxTextWidth mutable (removed readonly), added update(options: spec.TextContentOptions): void, and updated constructor to call update(options) for initialization.
TextStyle lazy update & color copying
packages/effects-core/src/plugins/text/text-style.ts
Made fontOffset mutable, added update(options: spec.TextContentOptions): void, constructor delegates to update. update resets outline/shadow state, copies textColor, outlineColor, and shadowColor values rather than assigning references, and recalculates fontOffset when needed.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant TextComponent
    participant TextComponentBase
    participant TextStyle
    participant TextLayout

    rect rgb(235,245,255)
    note right of Caller: New construction path
    Caller->>TextComponent: constructor(engine)
    TextComponent->>TextComponent: getDefaultProps()
    TextComponent->>TextComponentBase: init with defaults
    TextComponentBase->>TextStyle: update(defaultOptions)
    TextStyle->>TextStyle: reset & copy colors
    TextComponentBase->>TextLayout: update(defaultOptions)
    TextLayout->>TextLayout: set maxTextWidth/etc.
    end

    rect rgb(255,245,235)
    note right of Caller: fromData reset flow
    Caller->>TextComponent: fromData(data)
    TextComponent->>TextComponent: resetState()
    TextComponent->>TextComponent: disposeTextTexture()
    TextComponent->>TextStyle: update(options)
    TextComponent->>TextLayout: update(options)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Pay attention to texture disposal and equality checks against engine whiteTexture in text-item.ts.
  • Verify update methods are idempotent and preserve prior state invariants in text-style.ts and text-layout.ts.
  • Check callers of TextComponent for compatibility with the reduced constructor signature.

Poem

🐰
Default props snug in a burrowed nest,
I reset and update to do my best.
Textures tuck away, colors copied right,
Line heights hop to a clearer light.
A little rabbit cheers the refactor tonight.

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'feat: add get default props' is overly vague and doesn't clearly communicate the main change to someone scanning PR history. Consider using a more descriptive title like 'feat: add default props to TextComponent' or 'feat: initialize TextComponent with default props' to better convey the purpose and scope of changes.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
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-getDefaultProps

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.

@Fryt1 Fryt1 changed the base branch from main to feat/2.8 November 21, 2025 03:57
Copy link
Contributor

@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: 2

🧹 Nitpick comments (1)
packages/effects-core/src/plugins/text/text-item.ts (1)

270-280: Add comment explaining lineHeight validation.

The validation Math.max(fontSize, value) ensures line height is never smaller than font size, which is good defensive programming. However, adding a brief comment explaining this constraint would help future maintainers understand the reasoning.

Apply this diff to add a clarifying comment:

   setLineHeight (value: number): void {
     const fontSize = this.textStyle.fontSize;
+    // Line height must be at least fontSize to prevent text clipping
     const safe = Math.max(fontSize, value);
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4e71eb9 and 2e53a79.

📒 Files selected for processing (3)
  • packages/effects-core/src/plugins/text/text-item.ts (3 hunks)
  • packages/effects-core/src/plugins/text/text-layout.ts (1 hunks)
  • packages/effects-core/src/plugins/text/text-style.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2024-10-18T09:15:08.038Z
Learnt from: wumaolinmaoan
Repo: galacean/effects-runtime PR: 691
File: packages/effects-core/src/plugins/timeline/playables/color-property-mixer-playable.ts:39-42
Timestamp: 2024-10-18T09:15:08.038Z
Learning: In `packages/effects-core/src/plugins/timeline/playables/color-property-mixer-playable.ts`, when accumulating color components, avoid using `clone()` methods because it can cause additional garbage collection overhead.

Applied to files:

  • packages/effects-core/src/plugins/text/text-item.ts
  • packages/effects-core/src/plugins/text/text-style.ts
🧬 Code graph analysis (1)
packages/effects-core/src/plugins/text/text-item.ts (4)
packages/effects-core/src/engine.ts (1)
  • Engine (49-530)
packages/effects-core/src/canvas-pool.ts (1)
  • canvasPool (47-47)
packages/effects-core/src/plugins/text/text-style.ts (1)
  • TextStyle (3-125)
packages/effects-core/src/plugins/text/text-layout.ts (1)
  • TextLayout (4-178)
⏰ 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). (1)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (8)
packages/effects-core/src/plugins/text/text-layout.ts (1)

25-67: LGTM! Clean refactor to support dynamic reconfiguration.

The refactor from readonly maxTextWidth to mutable, combined with the new update(options) method, enables runtime reconfiguration of layout properties. The constructor delegation to update() follows the DRY principle and aligns with the parallel changes in TextStyle.

packages/effects-core/src/plugins/text/text-style.ts (3)

75-81: LGTM! Consistent refactor pattern.

The mutable fontOffset and constructor delegation to update(options) align with the refactor pattern in TextLayout, enabling consistent re-initialization across text components.


84-84: Good use of spread operators for color cloning.

Using spread operators ([...color]) to clone color arrays avoids shared references while minimizing garbage collection overhead compared to clone methods. This aligns with established patterns in the codebase.

Based on learnings.

Also applies to: 99-99, 113-113


92-108: Excellent defensive reset pattern.

Explicitly resetting outline, shadow, and font offset state before applying new options ensures clean re-initialization when update() is called multiple times. This prevents stale values from persisting across updates.

Also applies to: 118-119

packages/effects-core/src/plugins/text/text-item.ts (4)

88-104: Constructor refactor aligns with default props pattern.

The constructor now initializes from default props instead of accepting incoming props, supporting the PR objective of enabling item.addComponent() with default values. Canvas resource initialization remains consistent.


132-140: Good state reset pattern with proper texture cleanup.

The resetState() method properly disposes textures and resets internal state flags, ensuring clean re-initialization during fromData() calls. The texture disposal check against engine.whiteTexture correctly prevents disposal of the shared sentinel texture.


116-130: Proper lifecycle management in fromData().

Calling resetState() before applying new data ensures textures are disposed and internal state is reset, preventing resource leaks and stale state during re-initialization. This complements the new update-based initialization pattern.


185-201: Lazy initialization pattern supports flexible re-initialization.

The lazy initialization of textStyle and textLayout avoids unnecessary object creation while supporting both initial default-props construction and subsequent data-driven updates. This pattern integrates well with the new update() methods in TextStyle and TextLayout.

Copy link
Contributor

@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/effects-core/src/plugins/text/text-item.ts (1)

64-65: Replace deprecated substr() with substring().

The substr() method is deprecated. Use substring() instead for future compatibility.

Apply this diff:

-      id: `default-id-${Math.random().toString(36).substr(2, 9)}`,
-      item: { id: `default-item-${Math.random().toString(36).substr(2, 9)}` },
+      id: `default-id-${Math.random().toString(36).substring(2, 11)}`,
+      item: { id: `default-item-${Math.random().toString(36).substring(2, 11)}` },
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2e53a79 and e64c906.

📒 Files selected for processing (1)
  • packages/effects-core/src/plugins/text/text-item.ts (4 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2024-10-18T09:15:08.038Z
Learnt from: wumaolinmaoan
Repo: galacean/effects-runtime PR: 691
File: packages/effects-core/src/plugins/timeline/playables/color-property-mixer-playable.ts:39-42
Timestamp: 2024-10-18T09:15:08.038Z
Learning: In `packages/effects-core/src/plugins/timeline/playables/color-property-mixer-playable.ts`, when accumulating color components, avoid using `clone()` methods because it can cause additional garbage collection overhead.

Applied to files:

  • packages/effects-core/src/plugins/text/text-item.ts
🧬 Code graph analysis (1)
packages/effects-core/src/plugins/text/text-item.ts (3)
packages/effects-core/src/engine.ts (1)
  • Engine (49-530)
packages/effects-core/src/plugins/text/text-style.ts (1)
  • TextStyle (3-125)
packages/effects-core/src/plugins/text/text-layout.ts (1)
  • TextLayout (4-178)
⏰ 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). (1)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (5)
packages/effects-core/src/plugins/text/text-item.ts (5)

88-104: LGTM! Clean initialization flow for parameterless construction.

The constructor now properly supports item.addComponent() without parameters by initializing canvas resources first, then applying default props. The initialization order is correct.


122-122: LGTM! Proper state cleanup before loading new data.

Adding resetState() before applying new data ensures texture resources are disposed and state variables are reset, preventing resource leaks.


132-140: LGTM! Well-structured cleanup method.

The resetState() method properly disposes texture resources and resets state variables, providing a clean slate for initialization or reinitialization.


170-171: LGTM! Empty constructor aligns with new initialization pattern.

The empty constructor is appropriate since initialization now occurs through updateWithOptions() called from TextComponent's constructor, supporting the mixin pattern correctly.


177-193: LGTM! Efficient lazy initialization pattern.

The change from always creating new TextStyle and TextLayout instances to conditionally updating existing ones improves performance and aligns with the new update() methods added to those classes. This supports both initial construction and subsequent updates correctly.

@yiiqii yiiqii merged commit 0c5e9c0 into feat/2.8 Nov 24, 2025
2 checks passed
@yiiqii yiiqii deleted the feat/add-getDefaultProps branch November 24, 2025 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants