fix: broaden usePortableTextEditor().schemaTypes type for backward compatibility#2296
Closed
christianhg wants to merge 1 commit intomainfrom
Closed
fix: broaden usePortableTextEditor().schemaTypes type for backward compatibility#2296christianhg wants to merge 1 commit intomainfrom
christianhg wants to merge 1 commit intomainfrom
Conversation
…mpatibility Add optional Sanity-compatible properties (icon, component, i18nTitleKey) to the schema types returned by usePortableTextEditor().schemaTypes. This eases migration for consumers that previously accessed these properties when PTE depended on @sanity/types. The PTE-native Schema type in @portabletext/schema remains clean. The broader PortableTextEditorSchemaTypes type lives in @portabletext/editor and is only used for the deprecated PortableTextEditor class.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When PTE depended on
@sanity/types,usePortableTextEditor().schemaTypesreturned types that included Sanity-specific properties likeicon,component, andi18nTitleKey. Consumers (especially toolbar implementations) accessed these properties to render icons and custom components.After v6 removed the
@sanity/typesdependency, these properties disappeared from the type, causing type errors for consumers that accessed them.This PR adds a
PortableTextEditorSchemaTypestype in@portabletext/editorthat extends the clean PTE-native schema types with optional Sanity-compatible properties. The@portabletext/schemapackage is untouched - the broadening only happens at thePortableTextEditor.schemaTypesproperty.The optional properties are:
icon?: unknown- toolbar icons (wasReactNode | ComponentType)component?: unknown- custom render components (wasComponentType<...>)i18nTitleKey?: string- i18n title keysThese are typed as
unknown(orstringfor i18nTitleKey) because PTE itself does not populate them. They may be present when the schema originates from a Sanity schema via@portabletext/sanity-bridge. Consumers doingif (decorator.icon)will get no type error and the truthiness check will correctly skip the branch when the value isundefined.For
blockandspan, the type uses& {[key: string]: unknown}to allow arbitrary property access without type errors, since the oldObjectSchemaTypefrom@sanity/typeshad many properties.Files changed
packages/editor/src/types/schema-compat.ts- new file withPortableTextEditorSchemaTypestypepackages/editor/src/editor/PortableTextEditor.tsx- use broader type forschemaTypespackages/editor/src/index.ts- export the new type