fix(core): use dynamic propSchema when extending default block specs#2882
fix(core): use dynamic propSchema when extending default block specs#2882nperez0111 wants to merge 1 commit into
Conversation
createBlockSpec captured blockConfig.propSchema in closures at factory time. When users spread a spec and add new props to the config, the closures still referenced the original propSchema, causing a crash in wrapInBlockStructure when iterating block.props. Thread propSchema through the call context from addNodeAndExtensionsToSpec (which always has the current config) so the closures prefer the dynamic value. Add a spec?.default guard as a safety net for both blocks and inline content. Fixes #1840
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughAdds ChangespropSchema context propagation and extension tests
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant Editor
participant CreateSpec as createBlockSpec
participant Impl as blockImplementation.render
participant Wrap as wrapInBlockStructure
participant DOM
Editor->>CreateSpec: render/toExternalHTML(block, editor)
CreateSpec->>Impl: call(this={propSchema, blockContentDOMAttributes, props})
Impl->>Wrap: build(this.propSchema ?? blockConfig.propSchema)
Wrap-->>DOM: emit data-* attributes for non-default prop values
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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 |
@blocknote/ariakit
@blocknote/code-block
@blocknote/core
@blocknote/mantine
@blocknote/react
@blocknote/server-util
@blocknote/shadcn
@blocknote/xl-ai
@blocknote/xl-docx-exporter
@blocknote/xl-email-exporter
@blocknote/xl-multi-column
@blocknote/xl-odt-exporter
@blocknote/xl-pdf-exporter
commit: |
|
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/core/src/schema/blocks/createSpec.ts (1)
402-421: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winForward
propSchemainto the raw callbacks.blockImplementation.render.call(...)andblockImplementation.toExternalHTML?.call(...)both omitthis.propSchema, so custom block implementations can’t read it even though the callback type exposes it. Passthis.propSchemain both call contexts, not just towrapInBlockStructure.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/core/src/schema/blocks/createSpec.ts` around lines 402 - 421, The raw callback contexts for createSpec block rendering are missing propSchema, so custom implementations cannot access it. Update both blockImplementation.render.call and blockImplementation.toExternalHTML?.call to include this.propSchema alongside blockContentDOMAttributes, using the existing createSpec callback setup so the propSchema is available wherever the callback type expects it.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@packages/core/src/schema/blocks/createSpec.ts`:
- Around line 402-421: The raw callback contexts for createSpec block rendering
are missing propSchema, so custom implementations cannot access it. Update both
blockImplementation.render.call and blockImplementation.toExternalHTML?.call to
include this.propSchema alongside blockContentDOMAttributes, using the existing
createSpec callback setup so the propSchema is available wherever the callback
type expects it.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 098dd3c6-0702-4bf7-845b-9c51de969172
📒 Files selected for processing (5)
packages/core/src/schema/blocks/createSpec.tspackages/core/src/schema/blocks/extendBlockSpec.test.tspackages/core/src/schema/blocks/internal.tspackages/core/src/schema/blocks/types.tspackages/core/src/schema/inlineContent/internal.ts
Summary
Fixes a crash when extending default block specs with additional props by spreading the spec and modifying
config.propSchema. This pattern was recommended in #1840 but didn't work at runtime.Rationale
Users need to attach metadata to default blocks (paragraphs, headings, etc.) without creating fully custom block types. The recommended pattern is:
This crashed with
TypeError: Cannot read properties of undefined (reading 'default')becausecreateBlockSpeccapturedblockConfig.propSchemain closures at factory time. Spreading the spec creates a new config object, but therender/toExternalHTMLclosures still referenced the original propSchema.Changes
createSpec.ts:addNodeAndExtensionsToSpecnow passespropSchema: blockConfig.propSchemathrough thethiscall context in all.call()invocations. The closures increateBlockSpecprefer this dynamic value (this.propSchema ?? blockConfig.propSchema) over the captured one.types.ts: AddedpropSchema?: TPropsto thethisparameter types ofrenderandtoExternalHTMLonBlockImplementation.internal.ts(blocks + inline content): Addedspec?.defaultguard inwrapInBlockStructureandaddInlineContentAttributesas a safety net.extendBlockSpec.test.ts: 8 tests covering the extend-by-spreading pattern: editor creation, prop updates, HTML export, default-value omission, selective extension, HTML round-trip, and mounted DOM rendering.Impact
No breaking changes. The
propSchemaaddition to thethistype is optional and only affects internal call context — user-authoredrender/toExternalHTMLimplementations are unaffected.Testing
Checklist
Fixes #1840
Summary by CodeRabbit
New Features
Bug Fixes