Customizing the Portable Text editor not working in V3 #3902
Replies: 1 comment
-
Overview@shanejones I've also come across issues in adding Block editor customizations. The example from the above v2 customization documentation shows adding a custom "highlight" decorator like this: // RichTextEditor.js
import React from 'react'
const highlightIcon = () => (
<span style={{ fontWeight: 'bold' }}>H</span>
)
const highlightRender = props => (
<span style={{ backgroundColor: 'yellow' }}>{props.children}</span>
)
export default {
name: 'content',
title: 'Content',
type: 'array',
of: [
{
type: 'block',
marks: {
decorators: [
{ title: 'Strong', value: 'strong' },
{ title: 'Emphasis', value: 'em' },
{ title: 'Code', value: 'code' },
{
title: 'Highlight',
value: 'highlight',
blockEditor: {
icon: highlightIcon,
render: highlightRender
}
}
]
}
}
]
} In this example the custom decorator being: {
title: 'Highlight',
value: 'highlight',
blockEditor: {
icon: highlightIcon,
render: highlightRender
}
} Issue 1Since the new v3 instances are using typescript files (like import { HighlightIcon, HighlightRender } from '../components/highlight-text' Issue 2The structure for the /** @public */
export declare interface MarksDefinition {
decorators?: DecoratorDefinition[]
annotations?: (SchemaTypeDefinition | TypeReference)[]
} And a /** @public */
export declare interface DecoratorDefinition {
title: string
value: string
icon?: ReactNode | ComponentType
portableText?: {
icon?: ReactNode | ComponentType
render?: ComponentType
}
} Which is different from the v2 version, since Issue 3Even when I place the custom "highlight" icon and render components in a separate
Example schema file based off the new v3-ready vercel blog (note: I'm also using Internal Link annotations, which do work): import { ComposeIcon } from '@sanity/icons'
import { defineType } from 'sanity'
import { HighlightIcon, HighlightRender } from '../components/highlight-text'
export default defineType({
name: 'post',
title: 'Post',
icon: ComposeIcon,
type: 'document',
fields: [
{
name: 'title',
title: 'Title',
type: 'string',
validation: (Rule) => Rule.required(),
},
{
name: 'slug',
title: 'Slug',
type: 'slug',
options: {
source: 'title',
maxLength: 96,
},
validation: (Rule) => Rule.required(),
},
{
name: 'content',
title: 'Content',
type: 'array',
of: [
{
type: 'block',
marks: {
annotations: [
{
name: 'internalLink',
type: 'object',
title: 'Internal link',
icon: ComposeIcon,
fields: [
{
name: 'reference',
type: 'reference',
to: [
{ type: 'post' },
// other types you may want to link to
]
}
]
}
],
decorators: [
// Include normal decorators
{ title: 'Strong', value: 'strong' },
{ title: 'Emphasis', value: 'em' },
{ title: 'Code', value: 'code' },
{ title: 'Underline', value: 'underline' },
{ title: 'Strike', value: 'strike-through' },
// This doesn't seem to work in v3
// @see https://www.sanity.io/docs/customization#e6401a8fe843
{
title: 'Highlight',
value: 'highlight',
icon: HighlightIcon,
portableText: {
icon: HighlightIcon,
render: HighlightRender,
}
}
]
},
},
{
type: 'image',
options: {hotspot: true},
fields: [
{
name: 'alt',
type: 'string',
title: 'Alt Text',
options: {
isHighlighted: true
}
},
]
},
{
type: 'code',
},
],
validation: (Rule) => Rule.required(),
},
{
name: 'excerpt',
title: 'Excerpt',
type: 'text',
},
{
name: 'coverImage',
title: 'Cover Image',
type: 'image',
options: {
hotspot: true,
},
},
{
name: 'publishedAt',
title: 'Published At',
type: 'datetime',
},
{
name: 'author',
title: 'Author',
type: 'reference',
to: [{ type: authorType.name }],
},
],
preview: {
select: {
title: 'title',
author: 'author.name',
media: 'coverImage',
},
prepare(selection) {
const { author } = selection
return { ...selection, subtitle: author && `by ${author}` }
},
},
}) |
Beta Was this translation helpful? Give feedback.
-
https://www.sanity.io/docs/customization
From following the guide lower down the page it seems that you cannot add
blockEditor
withicon
orrender
as children to decorators anymore.I inspected the type declaration for this and that shows
decorators seem to only accept a
TitledListValue
I think this should be the same as the annotations for this to be able to work.
I think we might also need to convert the specific file to
.tsx
now that we have JSX code being added.Beta Was this translation helpful? Give feedback.
All reactions