1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-01-19 14:27:07 +01:00
slate/site/examples/huge-document.tsx
Wendell Hu 912d4b79da
docs: convert examples to typescript (#3766)
* docs: convert examples to typescript

* docs: convert remaining examples

* docs: update next.js

* ci: fix lint

* docs: fix next.js path

* docs: cleanup

* update

Co-authored-by: wendellhu <wendellhu@tencent.com>
2020-08-19 09:14:51 -07:00

44 lines
1.1 KiB
TypeScript

import React, { useState, useMemo, useCallback } from 'react'
import faker from 'faker'
import { createEditor } from 'slate'
import { Slate, Editable, withReact } from 'slate-react'
const HEADINGS = 100
const PARAGRAPHS = 7
const initialValue = []
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({
children: [{ text: faker.lorem.paragraph() }],
})
}
}
const HugeDocumentExample = () => {
const [value, setValue] = useState(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