forked from ianstormtaylor/slate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuge-document.tsx
44 lines (38 loc) · 1.21 KB
/
huge-document.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import React, { useState, useMemo, useCallback } from 'react'
import faker from 'faker'
import { createEditor, Descendant } from 'slate'
import { Slate, Editable, withReact } from 'slate-react'
const HEADINGS = 100
const PARAGRAPHS = 7
const initialValue: Descendant[] = []
for (let h = 0; h < HEADINGS; h++) {
initialValue.push({
type: 'heading',
children: [{ text: faker.lorem.sentence() }],
})
for (let p = 0; p < PARAGRAPHS; p++) {
initialValue.push({
type: 'paragraph',
children: [{ text: faker.lorem.paragraph() }],
})
}
}
const HugeDocumentExample = () => {
const [value, setValue] = useState<Descendant[]>(initialValue)
const renderElement = useCallback(props => <Element {...props} />, [])
const editor = useMemo(() => withReact(createEditor()), [])
return (
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
<Editable renderElement={renderElement} spellCheck autoFocus />
</Slate>
)
}
const Element = ({ attributes, children, element }) => {
switch (element.type) {
case 'heading':
return <h1 {...attributes}>{children}</h1>
default:
return <p {...attributes}>{children}</p>
}
}
export default HugeDocumentExample