import './editor.css' import { defineAnnotation, defineBlockObject, defineDecorator, defineInlineObject, defineSchema, defineTextBlock, EditorProvider, keyGenerator, PortableTextEditable, useEditor, useEditorSelector, } from '@portabletext/editor' import type { PortableTextBlock, TextBlockRenderProps, } from '@portabletext/editor' import {EventListenerPlugin, NodePlugin} from '@portabletext/editor/plugins' import * as selectors from '@portabletext/editor/selectors' import {ListIndexProvider, useListIndex} from '@portabletext/plugin-list-index' import {useState} from 'react' // Define the schema for the editor // All options are optional // Only the `name` property is required, but you can define a `title` and an `icon` as well // You can use this schema definition later to build your toolbar const schemaDefinition = defineSchema({ // Decorators are simple marks that don't hold any data decorators: [{name: 'strong'}, {name: 'em'}, {name: 'underline'}], // Annotations are more complex marks that can hold data annotations: [{name: 'link', fields: [{name: 'href', type: 'string'}]}], // Styles apply to entire text blocks // There's always a 'normal' style that can be considered the paragraph style styles: [ {name: 'normal'}, {name: 'h1'}, {name: 'h2'}, {name: 'h3'}, {name: 'blockquote'}, ], // Lists apply to entire text blocks as well lists: [{name: 'bullet'}, {name: 'number'}], // Inline objects hold arbitrary data that can be inserted into the text inlineObjects: [ {name: 'stock-ticker', fields: [{name: 'symbol', type: 'string'}]}, ], // Block objects hold arbitrary data that live side-by-side with text blocks blockObjects: [{name: 'image', fields: [{name: 'src', type: 'string'}]}], }) function App() { const [value, setValue] = useState | undefined>( // Initial value () => [ { _type: 'block', _key: keyGenerator(), children: [ {_type: 'span', _key: keyGenerator(), text: 'Hello, '}, { _type: 'span', _key: keyGenerator(), text: 'world!', marks: ['strong'], }, ], }, ], ) return ( <> {/* Create an editor */} {/* Subscribe to editor changes */} { if (event.type === 'mutation') { setValue(event.value) } }} /> {/* Register how text blocks (styles and lists), decorators, annotations, inline objects, and block objects are rendered */} {/* Toolbar needs to be rendered inside the `EditorProvider` component */} {/* Provides the list numbering that `useListIndex` reads */} {/* Component that controls the actual rendering of the editor */}
        {JSON.stringify(value, null, 2)}
      
) } // Decorators render through a registered node: one entry per decorator // name, or `type: '*'` for a single switching function. const strongDecorator = defineDecorator({ type: 'strong', render: ({children}) => {children}, }) const emDecorator = defineDecorator({ type: 'em', render: ({children}) => {children}, }) const underlineDecorator = defineDecorator({ type: 'underline', render: ({children}) => {children}, }) // Annotations render through a registered node as well. `annotation` is // the markDef object: `{_key, _type, ...fields}`. const linkAnnotation = defineAnnotation({ type: 'link', render: ({children}) => ( {children} ), }) // Text blocks render through a registered node: the `render` callback owns // the block's wrapper element and receives the block itself, so styles and // lists are handled in one place const textBlock = defineTextBlock({ type: 'block', render: (props) => , }) function TextBlock(props: TextBlockRenderProps) { // The editor doesn't compute list numbering; `useListIndex` from // `@portabletext/plugin-list-index` does. The `data-*` attributes below // feed the list CSS: look in the imported `editor.css` file to see how // list styles are implemented const listIndex = useListIndex(props.path) let children = props.children if (props.node.style === 'h1') { children =

{children}

} else if (props.node.style === 'h2') { children =

{children}

} else if (props.node.style === 'h3') { children =

{children}

} else if (props.node.style === 'blockquote') { children =
{children}
} return (
{children}
) } // Inline objects render through a registered node as well. `props.children` // carries a caret spacer the editor needs for caret placement; always // render it inside the wrapper const stockTicker = defineInlineObject({ type: 'stock-ticker', render: (props) => typeof props.node.symbol === 'string' ? ( {props.children} {/* `draggable` makes the object movable and keeps its text unselectable: a draggable element starts a drag instead of a text selection */} {props.node.symbol} ) : ( props.renderDefault(props) ), }) // Block objects render through a registered node as well. `props.children` // carries a caret spacer the editor needs for caret placement; always // render it inside the wrapper const imageBlock = defineBlockObject({ type: 'image', render: (props) => typeof props.node.src === 'string' ? (
{props.children}
{`IMG: ${props.node.src}`}
) : ( props.renderDefault(props) ), }) // A stable module-level reference: a fresh array on every render would // make `NodePlugin` unregister and re-register on every keystroke. const nodes = [ textBlock, strongDecorator, emDecorator, underlineDecorator, linkAnnotation, stockTicker, imageBlock, ] function Toolbar() { // Obtain the editor instance const editor = useEditor() const decoratorButtons = schemaDefinition.decorators.map((decorator) => ( )) const annotationButtons = schemaDefinition.annotations.map((annotation) => ( )) const styleButtons = schemaDefinition.styles.map((style) => ( )) const listButtons = schemaDefinition.lists.map((list) => ( )) const imageButton = ( ) const stockTickerButton = ( ) return ( <>
{decoratorButtons}
{annotationButtons}
{styleButtons}
{listButtons}
{imageButton}
{stockTickerButton}
) } function DecoratorButton(props: {decorator: string}) { // Obtain the editor instance const editor = useEditor() // Check if the decorator is active using a selector const active = useEditorSelector( editor, selectors.isActiveDecorator(props.decorator), ) return ( ) } function AnnotationButton(props: {annotation: {name: string}}) { const editor = useEditor() const active = useEditorSelector( editor, selectors.isActiveAnnotation(props.annotation.name), ) return ( ) } function StyleButton(props: {style: string}) { const editor = useEditor() const active = useEditorSelector(editor, selectors.isActiveStyle(props.style)) return ( ) } function ListButton(props: {list: string}) { const editor = useEditor() const active = useEditorSelector( editor, selectors.isActiveListItem(props.list), ) return ( ) } export default App