mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-02 19:52:32 +02:00
Updates "Saving to a Database" example to distinguish content changes (#4497)
* Updates "Saving to a Database" example to distinguish actual content changes. * Update docs/walkthroughs/06-saving-to-a-database.md * Update docs/walkthroughs/06-saving-to-a-database.md * Update docs/walkthroughs/06-saving-to-a-database.md * Runs prettier Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org>
This commit is contained in:
@@ -28,7 +28,7 @@ That will render a basic Slate editor on your page, and when you type things wil
|
|||||||
|
|
||||||
What we need to do is save the changes you make somewhere. For this example, we'll just be using [Local Storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage), but it will give you an idea for where you'd need to add your own database hooks.
|
What we need to do is save the changes you make somewhere. For this example, we'll just be using [Local Storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage), but it will give you an idea for where you'd need to add your own database hooks.
|
||||||
|
|
||||||
So, in our `onChange` handler, we need to save the `value`:
|
So, in our `onChange` handler, we need to save the `value` if anything besides the selection was changed:
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
const App = () => {
|
const App = () => {
|
||||||
@@ -47,9 +47,14 @@ const App = () => {
|
|||||||
onChange={value => {
|
onChange={value => {
|
||||||
setValue(value)
|
setValue(value)
|
||||||
|
|
||||||
|
const isAstChange = editor.operations.some(
|
||||||
|
op => 'set_selection' !== op.type
|
||||||
|
)
|
||||||
|
if (isAstChange) {
|
||||||
// Save the value to Local Storage.
|
// Save the value to Local Storage.
|
||||||
const content = JSON.stringify(value)
|
const content = JSON.stringify(value)
|
||||||
localStorage.setItem('content', content)
|
localStorage.setItem('content', content)
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Editable />
|
<Editable />
|
||||||
@@ -81,8 +86,14 @@ const App = () => {
|
|||||||
value={value}
|
value={value}
|
||||||
onChange={value => {
|
onChange={value => {
|
||||||
setValue(value)
|
setValue(value)
|
||||||
|
const isAstChange = editor.operations.some(
|
||||||
|
op => 'set_selection' !== op.type
|
||||||
|
)
|
||||||
|
if (isAstChange) {
|
||||||
|
// Save the value to Local Storage.
|
||||||
const content = JSON.stringify(value)
|
const content = JSON.stringify(value)
|
||||||
localStorage.setItem('content', content)
|
localStorage.setItem('content', content)
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Editable />
|
<Editable />
|
||||||
@@ -135,8 +146,13 @@ const App = () => {
|
|||||||
value={value}
|
value={value}
|
||||||
onChange={value => {
|
onChange={value => {
|
||||||
setValue(value)
|
setValue(value)
|
||||||
|
const isAstChange = editor.operations.some(
|
||||||
|
op => 'set_selection' !== op.type
|
||||||
|
)
|
||||||
|
if (isAstChange) {
|
||||||
// Serialize the value and save the string value to Local Storage.
|
// Serialize the value and save the string value to Local Storage.
|
||||||
localStorage.setItem('content', serialize(value))
|
localStorage.setItem('content', serialize(value))
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Editable />
|
<Editable />
|
||||||
|
Reference in New Issue
Block a user