mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-23 08:50:59 +01:00
184 lines
4.4 KiB
JavaScript
184 lines
4.4 KiB
JavaScript
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(
|
|
() => withTables(withHistory(withReact(createEditor()))),
|
|
[]
|
|
)
|
|
return (
|
|
<Slate
|
|
editor={editor}
|
|
value={value}
|
|
selection={selection}
|
|
onChange={(value, selection) => {
|
|
setValue(value)
|
|
setSelection(selection)
|
|
}}
|
|
>
|
|
<Editable renderElement={renderElement} renderLeaf={renderLeaf} />
|
|
</Slate>
|
|
)
|
|
}
|
|
|
|
const withTables = editor => {
|
|
const { exec } = editor
|
|
|
|
editor.exec = command => {
|
|
const { selection } = editor
|
|
const { type } = command
|
|
|
|
if (
|
|
(type === 'delete_forward' || type === 'delete_backward') &&
|
|
selection &&
|
|
Range.isCollapsed(selection)
|
|
) {
|
|
const [cell] = Editor.nodes(editor, { match: { type: 'table-cell' } })
|
|
|
|
if (cell) {
|
|
const [, cellPath] = cell
|
|
const edge =
|
|
type === 'delete_backward'
|
|
? Editor.start(editor, cellPath)
|
|
: Editor.end(editor, cellPath)
|
|
|
|
if (Point.equals(selection.anchor, edge)) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
if (type === 'insert_break' && selection) {
|
|
const [table] = Editor.nodes(editor, { match: { type: 'table' } })
|
|
|
|
if (table) {
|
|
return
|
|
}
|
|
}
|
|
|
|
exec(command)
|
|
}
|
|
|
|
return editor
|
|
}
|
|
|
|
const Element = ({ attributes, children, element }) => {
|
|
switch (element.type) {
|
|
case 'table':
|
|
return (
|
|
<table>
|
|
<tbody {...attributes}>{children}</tbody>
|
|
</table>
|
|
)
|
|
case 'table-row':
|
|
return <tr {...attributes}>{children}</tr>
|
|
case 'table-cell':
|
|
return <td {...attributes}>{children}</td>
|
|
default:
|
|
return <p {...attributes}>{children}</p>
|
|
}
|
|
}
|
|
|
|
const Leaf = ({ attributes, children, leaf }) => {
|
|
if (leaf.bold) {
|
|
children = <strong>{children}</strong>
|
|
}
|
|
|
|
return <span {...attributes}>{children}</span>
|
|
}
|
|
|
|
const initialValue = [
|
|
{
|
|
children: [
|
|
{
|
|
text:
|
|
'Since the editor is based on a recursive tree model, similar to an HTML document, you can create complex nested structures, like tables:',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
type: 'table',
|
|
children: [
|
|
{
|
|
type: 'table-row',
|
|
children: [
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: '' }],
|
|
},
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: 'Human', bold: true }],
|
|
},
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: 'Dog', bold: true }],
|
|
},
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: 'Cat', bold: true }],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
type: 'table-row',
|
|
children: [
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: '# of Feet', bold: true }],
|
|
},
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: '2' }],
|
|
},
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: '4' }],
|
|
},
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: '4' }],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
type: 'table-row',
|
|
children: [
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: '# of Lives', bold: true }],
|
|
},
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: '1' }],
|
|
},
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: '1' }],
|
|
},
|
|
{
|
|
type: 'table-cell',
|
|
children: [{ text: '9' }],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
children: [
|
|
{
|
|
text:
|
|
"This table is just a basic example of rendering a table, and it doesn't have fancy functionality. But you could augment it to add support for navigating with arrow keys, displaying table headers, adding column and rows, or even formulas if you wanted to get really crazy!",
|
|
},
|
|
],
|
|
},
|
|
]
|
|
|
|
export default TablesExample
|