@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 +21 -27
- package/lib/index.d.mts +531 -206
- package/lib/index.d.ts +531 -206
- package/lib/index.esm.js +440 -386
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +483 -429
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +440 -386
- package/lib/index.mjs.map +1 -1
- package/package.json +10 -10
- package/src/editor/Editable.tsx +1 -1
- package/src/editor/PortableTextEditor.tsx +122 -95
- package/src/editor/behavior/behavior.types.ts +9 -0
- package/src/editor/components/Synchronizer.tsx +13 -68
- package/src/editor/create-slate-editor.tsx +2 -14
- package/src/editor/editor-event-listener.tsx +24 -0
- package/src/editor/editor-machine.ts +34 -4
- package/src/editor/editor-provider.tsx +81 -0
- package/src/editor/hooks/useSyncValue.ts +2 -3
- package/src/editor/plugins/createWithMaxBlocks.ts +1 -0
- package/src/editor/plugins/createWithPlaceholderBlock.ts +1 -0
- package/src/editor/plugins/createWithUndoRedo.ts +1 -0
- package/src/editor/plugins/with-plugins.ts +0 -27
- package/src/editor/use-editor.ts +89 -20
- package/src/index.ts +12 -4
- package/src/types/editor.ts +0 -1
package/README.md
CHANGED
|
@@ -58,7 +58,7 @@ const schemaDefinition = defineSchema({
|
|
|
58
58
|
|
|
59
59
|
### Render the Editor Component
|
|
60
60
|
|
|
61
|
-
Use `
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
86
|
+
{/* Create an editor */}
|
|
87
|
+
<EditorProvider
|
|
88
|
+
config={{
|
|
89
|
+
schemaDefinition,
|
|
90
|
+
initialValue: value,
|
|
91
|
+
}}
|
|
107
92
|
>
|
|
108
|
-
{/*
|
|
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
|
-
//
|
|
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
|
-
</
|
|
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 `
|
|
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
|
|
226
|
+
// Obtain the editor instance
|
|
233
227
|
const editorInstance = usePortableTextEditor()
|
|
234
228
|
// Rerender the toolbar whenever the selection changes
|
|
235
229
|
usePortableTextEditorSelection()
|