mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-21 23:53:50 +01:00
* chore: moved all ts files for examples to examples/ts * add: tsc to eject js and jsx output * example: add js transpiled examples * example: update example site to show both js and ts code * chore: fix yarn lint * fix(example): getAllExamplesPath
39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
import React, { useMemo, useCallback } from 'react'
|
|
import { faker } from '@faker-js/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({
|
|
type: 'paragraph',
|
|
children: [{ text: faker.lorem.paragraph() }],
|
|
})
|
|
}
|
|
}
|
|
const HugeDocumentExample = () => {
|
|
const renderElement = useCallback(props => <Element {...props} />, [])
|
|
const editor = useMemo(() => withReact(createEditor()), [])
|
|
return (
|
|
<Slate editor={editor} initialValue={initialValue}>
|
|
<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
|