1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-03 04:02:33 +02:00

Change <Slate> to a controlled component (#3216)

* change <Slate> to be a controlled component

* add comment about unstable React API
This commit is contained in:
Ian Storm Taylor
2019-12-05 11:36:44 -05:00
committed by GitHub
parent 4c03b497d9
commit f3fc2c2a54
29 changed files with 305 additions and 110 deletions

View File

@@ -1,4 +1,4 @@
import React, { useMemo, useCallback } from 'react'
import React, { useState, useMemo, useCallback } from 'react'
import {
Slate,
Editable,
@@ -12,13 +12,24 @@ import { css } from 'emotion'
import { withHistory } from 'slate-history'
const CheckListsExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const renderElement = useCallback(props => <Element {...props} />, [])
const editor = useMemo(
() => withChecklists(withHistory(withReact(createEditor()))),
[]
)
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Editable
renderElement={renderElement}
placeholder="Get to work…"

View File

@@ -1,4 +1,4 @@
import React, { useMemo } from 'react'
import React, { useState, useMemo } from 'react'
import { Editor, createEditor } from 'slate'
import {
Slate,
@@ -10,9 +10,19 @@ import {
} from 'slate-react'
const EmbedsExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const editor = useMemo(() => withEmbeds(withReact(createEditor())), [])
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Editable
renderElement={props => <Element {...props} />}
placeholder="Enter some text..."

View File

@@ -1,4 +1,4 @@
import React, { useCallback, useMemo } from 'react'
import React, { useState, useCallback, useMemo } from 'react'
import { Slate, Editable, withReact } from 'slate-react'
import { Editor, createEditor } from 'slate'
import { withHistory } from 'slate-history'
@@ -39,13 +39,23 @@ const schema = [
]
const ForcedLayoutExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const renderElement = useCallback(props => <Element {...props} />, [])
const editor = useMemo(
() => withSchema(withHistory(withReact(createEditor())), schema),
[]
)
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Editable
renderElement={renderElement}
placeholder="Enter a title…"

View File

@@ -1,4 +1,4 @@
import React, { useMemo, useRef, useEffect } from 'react'
import React, { useState, useMemo, useRef, useEffect } from 'react'
import { Slate, Editable, ReactEditor, withReact, useSlate } from 'slate-react'
import { Editor, createEditor } from 'slate'
import { css } from 'emotion'
@@ -8,12 +8,23 @@ import { Button, Icon, Menu, Portal } from '../components'
import { Range } from 'slate'
const HoveringMenuExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const editor = useMemo(
() => withFormatting(withHistory(withReact(createEditor()))),
[]
)
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<HoveringToolbar />
<Editable
renderLeaf={props => <Leaf {...props} />}

View File

@@ -1,4 +1,4 @@
import React, { useMemo, useCallback } from 'react'
import React, { useState, useMemo, useCallback } from 'react'
import faker from 'faker'
import { createEditor } from 'slate'
import { Slate, Editable, withReact } from 'slate-react'
@@ -21,10 +21,20 @@ for (let h = 0; h < HEADINGS; h++) {
}
const HugeDocumentExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const renderElement = useCallback(props => <Element {...props} />, [])
const editor = useMemo(() => withReact(createEditor()), [])
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Editable renderElement={renderElement} spellCheck autoFocus />
</Slate>
)

View File

@@ -1,4 +1,4 @@
import React, { useMemo } from 'react'
import React, { useState, useMemo } from 'react'
import imageExtensions from 'image-extensions'
import isUrl from 'is-url'
import { Editor, createEditor } from 'slate'
@@ -16,12 +16,23 @@ import { css } from 'emotion'
import { Button, Icon, Toolbar } from '../components'
const ImagesExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const editor = useMemo(
() => withImages(withHistory(withReact(createEditor()))),
[]
)
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Toolbar>
<InsertImageButton />
</Toolbar>

View File

@@ -1,4 +1,4 @@
import React, { useMemo } from 'react'
import React, { useState, useMemo } from 'react'
import isUrl from 'is-url'
import { Slate, Editable, withReact, useSlate } from 'slate-react'
import { Editor, createEditor } from 'slate'
@@ -7,12 +7,23 @@ import { withHistory } from 'slate-history'
import { Button, Icon, Toolbar } from '../components'
const LinkExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const editor = useMemo(
() => withLinks(withHistory(withReact(createEditor()))),
[]
)
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Toolbar>
<LinkButton />
</Toolbar>

View File

@@ -1,5 +1,5 @@
import Prism from 'prismjs'
import React, { useCallback, useMemo } from 'react'
import React, { useState, useCallback, useMemo } from 'react'
import { Slate, Editable, withReact } from 'slate-react'
import { Text, createEditor } from 'slate'
import { withHistory } from 'slate-history'
@@ -9,6 +9,8 @@ import { css } from 'emotion'
;Prism.languages.markdown=Prism.languages.extend("markup",{}),Prism.languages.insertBefore("markdown","prolog",{blockquote:{pattern:/^>(?:[\t ]*>)*/m,alias:"punctuation"},code:[{pattern:/^(?: {4}|\t).+/m,alias:"keyword"},{pattern:/``.+?``|`[^`\n]+`/,alias:"keyword"}],title:[{pattern:/\w+.*(?:\r?\n|\r)(?:==+|--+)/,alias:"important",inside:{punctuation:/==+$|--+$/}},{pattern:/(^\s*)#+.+/m,lookbehind:!0,alias:"important",inside:{punctuation:/^#+|#+$/}}],hr:{pattern:/(^\s*)([*-])([\t ]*\2){2,}(?=\s*$)/m,lookbehind:!0,alias:"punctuation"},list:{pattern:/(^\s*)(?:[*+-]|\d+\.)(?=[\t ].)/m,lookbehind:!0,alias:"punctuation"},"url-reference":{pattern:/!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:\\.|[^>\\])+>)(?:[\t ]+(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\)))?/,inside:{variable:{pattern:/^(!?\[)[^\]]+/,lookbehind:!0},string:/(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\))$/,punctuation:/^[\[\]!:]|[<>]/},alias:"url"},bold:{pattern:/(^|[^\\])(\*\*|__)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^\*\*|^__|\*\*$|__$/}},italic:{pattern:/(^|[^\\])([*_])(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^[*_]|[*_]$/}},url:{pattern:/!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[[^\]\n]*\])/,inside:{variable:{pattern:/(!?\[)[^\]]+(?=\]$)/,lookbehind:!0},string:{pattern:/"(?:\\.|[^"\\])*"(?=\)$)/}}}}),Prism.languages.markdown.bold.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.italic.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.bold.inside.italic=Prism.util.clone(Prism.languages.markdown.italic),Prism.languages.markdown.italic.inside.bold=Prism.util.clone(Prism.languages.markdown.bold); // prettier-ignore
const MarkdownPreviewExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const renderLeaf = useCallback(props => <Leaf {...props} />, [])
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
const decorate = useCallback(([node, path]) => {
@@ -50,7 +52,15 @@ const MarkdownPreviewExample = () => {
}, [])
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Editable
decorate={decorate}
renderLeaf={renderLeaf}

View File

@@ -1,4 +1,4 @@
import React, { useCallback, useMemo } from 'react'
import React, { useState, useCallback, useMemo } from 'react'
import { Slate, Editable, withReact } from 'slate-react'
import { Editor, Range, Point, createEditor } from 'slate'
import { withHistory } from 'slate-history'
@@ -17,13 +17,23 @@ const SHORTCUTS = {
}
const MarkdownShortcutsExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const renderElement = useCallback(props => <Element {...props} />, [])
const editor = useMemo(
() => withShortcuts(withReact(withHistory(createEditor()))),
[]
)
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Editable
renderElement={renderElement}
placeholder="Write some markdown..."

View File

@@ -14,6 +14,8 @@ import { Portal } from '../components'
const MentionExample = () => {
const ref = useRef()
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const [target, setTarget] = useState()
const [index, setIndex] = useState(0)
const [search, setSearch] = useState('')
@@ -72,9 +74,11 @@ const MentionExample = () => {
return (
<Slate
editor={editor}
defaultValue={initialValue}
onChange={() => {
const { selection } = editor
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
if (selection && Range.isCollapsed(selection)) {
const [start] = Range.edges(selection)

View File

@@ -1,4 +1,4 @@
import React, { useCallback, useMemo } from 'react'
import React, { useState, useCallback, useMemo } from 'react'
import { jsx } from 'slate-hyperscript'
import { Editor, createEditor } from 'slate'
import { withHistory } from 'slate-history'
@@ -79,6 +79,8 @@ export const deserialize = el => {
}
const PasteHtmlExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const renderElement = useCallback(props => <Element {...props} />, [])
const renderLeaf = useCallback(props => <Leaf {...props} />, [])
const editor = useMemo(
@@ -86,7 +88,15 @@ const PasteHtmlExample = () => {
[]
)
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Editable
renderElement={renderElement}
renderLeaf={renderLeaf}

View File

@@ -1,12 +1,22 @@
import React, { useMemo } from 'react'
import React, { useState, useMemo } from 'react'
import { createEditor } from 'slate'
import { Slate, Editable, withReact } from 'slate-react'
import { withHistory } from 'slate-history'
const PlainTextExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Editable placeholder="Enter some plain text..." />
</Slate>
)

View File

@@ -1,11 +1,21 @@
import React, { useMemo } from 'react'
import React, { useState, useMemo } from 'react'
import { createEditor } from 'slate'
import { Slate, Editable, withReact } from 'slate-react'
const ReadOnlyExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const editor = useMemo(() => withReact(createEditor()), [])
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Editable readOnly placeholder="Enter some plain text..." />
</Slate>
)

View File

@@ -1,4 +1,4 @@
import React, { useCallback, useMemo } from 'react'
import React, { useCallback, useMemo, useState } from 'react'
import isHotkey from 'is-hotkey'
import { Editable, withReact, useSlate, Slate } from 'slate-react'
import { Editor, createEditor } from 'slate'
@@ -23,6 +23,8 @@ const BLOCK_FORMATS = [
]
const RichTextExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const renderElement = useCallback(props => <Element {...props} />, [])
const renderLeaf = useCallback(props => <Leaf {...props} />, [])
const editor = useMemo(
@@ -31,7 +33,15 @@ const RichTextExample = () => {
)
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Toolbar>
<FormatButton format="bold" icon="format_bold" />
<FormatButton format="italic" icon="format_italic" />

View File

@@ -7,6 +7,8 @@ import { withHistory } from 'slate-history'
import { Icon, Toolbar } from '../components'
const SearchHighlightingExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const [search, setSearch] = useState()
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
const decorate = useCallback(
@@ -37,7 +39,15 @@ const SearchHighlightingExample = () => {
)
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Toolbar>
<div
className={css`

View File

@@ -1,9 +1,11 @@
import React, { useCallback, useMemo } from 'react'
import React, { useState, useCallback, useMemo } from 'react'
import { Slate, Editable, withReact } from 'slate-react'
import { Editor, Range, Point, createEditor } from 'slate'
import { withHistory } from 'slate-history'
const TablesExample = () => {
const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)
const renderElement = useCallback(props => <Element {...props} />, [])
const renderLeaf = useCallback(props => <Leaf {...props} />, [])
const editor = useMemo(
@@ -11,7 +13,15 @@ const TablesExample = () => {
[]
)
return (
<Slate editor={editor} defaultValue={initialValue}>
<Slate
editor={editor}
value={value}
selection={selection}
onChange={(value, selection) => {
setValue(value)
setSelection(selection)
}}
>
<Editable renderElement={renderElement} renderLeaf={renderLeaf} />
</Slate>
)