@portabletext/editor 1.10.2 → 1.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -58,7 +58,7 @@ const schemaDefinition = defineSchema({
58
58
 
59
59
  ### Render the Editor Component
60
60
 
61
- Use `useEditor` to create an `editor` and pass that into `PortableTextEditor`. Use the `editor.on` method to listen for `mutation` changes inside the editor so you can use and store the value produced.
61
+ Use `EditorProvider` to configure an editor and use `EditorEventListener` to listen for `mutation` changes inside the editor so you can use and store the value produced.
62
62
 
63
63
  ```tsx
64
64
  function App() {
@@ -81,31 +81,24 @@ function App() {
81
81
  ],
82
82
  )
83
83
 
84
- // Create an editor
85
- const editor = useEditor({
86
- schemaDefinition,
87
- // With an optional initial value
88
- initialValue: value,
89
- })
90
-
91
- // Subscribe to editor changes
92
- useEffect(() => {
93
- const subscription = editor.on('mutation', (mutation) => {
94
- setValue(mutation.snapshot)
95
- })
96
-
97
- return () => {
98
- subscription.unsubscribe()
99
- }
100
- }, [editor])
101
-
102
84
  return (
103
85
  <>
104
- <PortableTextEditor
105
- // Pass in the `editor` you created earlier
106
- editor={editor}
86
+ {/* Create an editor */}
87
+ <EditorProvider
88
+ config={{
89
+ schemaDefinition,
90
+ initialValue: value,
91
+ }}
107
92
  >
108
- {/* Toolbar needs to be rendered inside the `PortableTextEditor` component */}
93
+ {/* Subscribe to editor changes */}
94
+ <EditorEventListener
95
+ on={(event) => {
96
+ if (event.type === 'mutation') {
97
+ setValue(event.snapshot)
98
+ }
99
+ }}
100
+ />
101
+ {/* Toolbar needs to be rendered inside the `EditorProvider` component */}
109
102
  <Toolbar />
110
103
  {/* Component that controls the actual rendering of the editor */}
111
104
  <PortableTextEditable
@@ -121,10 +114,11 @@ function App() {
121
114
  // Control how inline objects are rendered
122
115
  renderChild={renderChild}
123
116
  // Rendering lists is harder and most likely requires a fair amount of CSS
124
- // However, we still need to return and render the list item's children to ensure proper rendering
117
+ // First, return the children like here
118
+ // Next, look in the imported `editor.css` file to see how list styles are implemented
125
119
  renderListItem={(props) => <>{props.children}</>}
126
120
  />
127
- </PortableTextEditor>
121
+ </EditorProvider>
128
122
  <pre style={{border: '1px dashed black', padding: '0.5em'}}>
129
123
  {JSON.stringify(value, null, 2)}
130
124
  </pre>
@@ -225,11 +219,11 @@ function isStockTicker(
225
219
 
226
220
  ### Render the Toolbar
227
221
 
228
- Your toolbar needs to be rendered within `PortableTextEditor` because it requires a reference to the `editorInstance` that it produces. To toggle marks and styles and to insert objects, you'll have to use this `editorInstance` together with static methods on the `PortableTextEditor` class.
222
+ Your toolbar needs to be rendered within `EditorProvider` because it requires a reference to the `editorInstance` that it produces. To toggle marks and styles and to insert objects, you'll have to use this `editorInstance` together with static methods on the `PortableTextEditor` class.
229
223
 
230
224
  ```tsx
231
225
  function Toolbar() {
232
- // Obtain the editor instance provided from the `PortableTextEditor` component
226
+ // Obtain the editor instance
233
227
  const editorInstance = usePortableTextEditor()
234
228
  // Rerender the toolbar whenever the selection changes
235
229
  usePortableTextEditorSelection()