-
-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3512 from udecode/feat/setValue
setValue
- Loading branch information
Showing
14 changed files
with
297 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@udecode/plate-core': patch | ||
--- | ||
|
||
- Add `editor.tf.setValue` to replace the editor value | ||
- Fix: move `editor.api.reset` to `editor.tf.reset` |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
title: Controlled Editor Value | ||
description: How to control the editor value. | ||
--- | ||
|
||
Implementing a fully controlled editor value in Plate (and Slate) is complex due to several factors: | ||
|
||
1. The editor state includes more than just the content (`editor.children`). It also includes `editor.selection` and `editor.history`. | ||
|
||
2. Directly replacing `editor.children` can break the selection and history, leading to unexpected behavior or crashes. | ||
|
||
3. All changes to the editor's value should ideally happen through [Transforms](https://docs.slatejs.org/api/transforms) to maintain consistency with selection and history. | ||
|
||
Given these challenges, it's generally recommended to use Plate as an uncontrolled input. However, if you need to make external changes to the editor's content, you can use `editor.tf.setValue(value)` function. | ||
|
||
<Callout className="my-4"> | ||
Using `editor.tf.setValue` will re-render all nodes on each call, so it | ||
should be used carefully and sparingly. It may impact performance if used | ||
frequently or with large documents. | ||
</Callout> | ||
|
||
Alternatively, you can use `editor.tf.reset()` to reset the editor state, which will reset the selection and history. | ||
|
||
```tsx | ||
function App() { | ||
const editor = usePlateEditor({ | ||
value: 'Initial Value', | ||
// Disable the editor if initial value is not yet ready | ||
// enabled: !!value, | ||
}); | ||
|
||
return ( | ||
<div> | ||
<Plate editor={editor}> | ||
<PlateContent /> | ||
</Plate> | ||
|
||
<button | ||
onClick={() => { | ||
// Replace with HTML string | ||
editor.tf.setValue('Replaced Value'); | ||
|
||
// Replace with JSON value | ||
editor.tf.setValue([ | ||
{ | ||
type: 'p', | ||
children: [{ text: 'Replaced Value' }], | ||
}, | ||
]); | ||
|
||
// Replace with empty value | ||
editor.tf.setValue(); | ||
}} | ||
> | ||
Replace Value | ||
</button> | ||
|
||
<button | ||
onClick={() => { | ||
editor.tf.reset(); | ||
}} | ||
> | ||
Reset Editor | ||
</button> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
<ComponentPreview name="controlled-demo" padding="md" /> | ||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react'; | ||
|
||
import { | ||
Plate, | ||
focusEditor, | ||
focusEditorEdge, | ||
usePlateEditor, | ||
} from '@udecode/plate-common/react'; | ||
|
||
import { Button } from '@/registry/default/plate-ui/button'; | ||
import { Editor } from '@/registry/default/plate-ui/editor'; | ||
|
||
export default function ControlledEditorDemo() { | ||
const editor = usePlateEditor({ | ||
value: [ | ||
{ | ||
children: [{ text: 'Initial Value' }], | ||
type: 'p', | ||
}, | ||
], | ||
}); | ||
|
||
return ( | ||
<div> | ||
<Plate editor={editor}> | ||
<Editor /> | ||
</Plate> | ||
|
||
<div className="mt-4 flex flex-col gap-2"> | ||
<Button | ||
onClick={() => { | ||
// Replace with HTML string | ||
editor.tf.setValue([ | ||
{ | ||
children: [{ text: 'Replaced Value' }], | ||
type: 'p', | ||
}, | ||
]); | ||
|
||
focusEditorEdge(editor, { edge: 'end' }); | ||
}} | ||
> | ||
Replace Value | ||
</Button> | ||
|
||
<Button | ||
onClick={() => { | ||
editor.tf.reset(); | ||
|
||
focusEditor(editor); | ||
}} | ||
> | ||
Reset Editor | ||
</Button> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.