mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-01 11:12:42 +02:00
update walkthroughs
This commit is contained in:
@@ -9,8 +9,24 @@ We'll show you how. Let's start with our app from earlier:
|
||||
```js
|
||||
const App = () => {
|
||||
const editor = useMemo(() => withReact(createEditor()), [])
|
||||
const [selection, setSelection] = useState(null)
|
||||
const [value, setValue] = useState([
|
||||
{
|
||||
type: 'paragraph',
|
||||
children: [{ text: 'A line of text in a paragraph.' }],
|
||||
},
|
||||
])
|
||||
|
||||
return (
|
||||
<Slate editor={editor} defaultValue={defaultValue}>
|
||||
<Slate
|
||||
editor={editor}
|
||||
value={value}
|
||||
selection={selection}
|
||||
onChange={(value, selection) => {
|
||||
setValue(value)
|
||||
setSelection(selection)
|
||||
}}
|
||||
>
|
||||
<Editable
|
||||
onKeyDown={event => {
|
||||
if (event.key === '&') {
|
||||
@@ -60,6 +76,13 @@ Now, let's add that renderer to our `Editor`:
|
||||
```js
|
||||
const App = () => {
|
||||
const editor = useMemo(() => withReact(createEditor()), [])
|
||||
const [selection, setSelection] = useState(null)
|
||||
const [value, setValue] = useState([
|
||||
{
|
||||
type: 'paragraph',
|
||||
children: [{ text: 'A line of text in a paragraph.' }],
|
||||
},
|
||||
])
|
||||
|
||||
// Define a rendering function based on the element passed to `props`. We use
|
||||
// `useCallback` here to memoize the function for subsequent renders.
|
||||
@@ -73,7 +96,15 @@ const App = () => {
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<Slate editor={editor} defaultValue={defaultValue}>
|
||||
<Slate
|
||||
editor={editor}
|
||||
value={value}
|
||||
selection={selection}
|
||||
onChange={(value, selection) => {
|
||||
setValue(value)
|
||||
setSelection(selection)
|
||||
}}
|
||||
>
|
||||
<Editable
|
||||
// Pass in the `renderElement` function.
|
||||
renderElement={renderElement}
|
||||
@@ -109,6 +140,14 @@ import { Editor } from 'slate'
|
||||
|
||||
const App = () => {
|
||||
const editor = useMemo(() => withReact(createEditor()), [])
|
||||
const [selection, setSelection] = useState(null)
|
||||
const [value, setValue] = useState([
|
||||
{
|
||||
type: 'paragraph',
|
||||
children: [{ text: 'A line of text in a paragraph.' }],
|
||||
},
|
||||
])
|
||||
|
||||
const renderElement = useCallback(props => {
|
||||
switch (props.element.type) {
|
||||
case 'code':
|
||||
@@ -119,7 +158,15 @@ const App = () => {
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<Slate editor={editor} defaultValue={defaultValue}>
|
||||
<Slate
|
||||
editor={editor}
|
||||
value={value}
|
||||
selection={selection}
|
||||
onChange={(value, selection) => {
|
||||
setValue(value)
|
||||
setSelection(selection)
|
||||
}}
|
||||
>
|
||||
<Editable
|
||||
renderElement={renderElement}
|
||||
onKeyDown={event => {
|
||||
@@ -154,8 +201,15 @@ But we forgot one thing. When you hit `` Ctrl-` `` again, it should change the c
|
||||
|
||||
```js
|
||||
const App = () => {
|
||||
const [value, setValue] = useState(initialValue)
|
||||
const editor = useMemo(() => withReact(createEditor()), [])
|
||||
const [selection, setSelection] = useState(null)
|
||||
const [value, setValue] = useState([
|
||||
{
|
||||
type: 'paragraph',
|
||||
children: [{ text: 'A line of text in a paragraph.' }],
|
||||
},
|
||||
])
|
||||
|
||||
const renderElement = useCallback(props => {
|
||||
switch (props.element.type) {
|
||||
case 'code':
|
||||
@@ -166,20 +220,26 @@ const App = () => {
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<Slate editor={editor} defaultValue={defaultValue}>
|
||||
<Slate
|
||||
editor={editor}
|
||||
value={value}
|
||||
selection={selection}
|
||||
onChange={(value, selection) => {
|
||||
setValue(value)
|
||||
setSelection(selection)
|
||||
}}
|
||||
>
|
||||
<Editable
|
||||
renderElement={renderElement}
|
||||
onKeyDown={event => {
|
||||
if (event.key === '`' && event.ctrlKey) {
|
||||
event.preventDefault()
|
||||
// Determine whether any of the currently selected blocks are code blocks.
|
||||
const [node] = Editor.nodes(editor, { match: { type: 'code' } })
|
||||
const isCodeActive = !!node
|
||||
|
||||
// Toggle the block type depending on `isCode`.
|
||||
const [match] = Editor.nodes(editor, { match: { type: 'code' } })
|
||||
// Toggle the block type depending on whether there's already a match.
|
||||
Editor.setNodes(
|
||||
editor,
|
||||
{ type: isCodeActive ? 'paragraph' : 'code' },
|
||||
{ type: match ? 'paragraph' : 'code' },
|
||||
{ match: 'block' }
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user