Devendra174's picture
Upload folder using huggingface_hub
1e92f2d verified
import React, { useCallback, useMemo } from 'react'
import {
Descendant,
Editor,
Point,
Range,
Element as SlateElement,
createEditor,
} from 'slate'
import { withHistory } from 'slate-history'
import {
Editable,
RenderElementProps,
RenderLeafProps,
Slate,
withReact,
} from 'slate-react'
import { CustomEditor } from './custom-types.d'
import { css } from '@emotion/css'
const TablesExample = () => {
const renderElement = useCallback(
(props: RenderElementProps) => <Element {...props} />,
[]
)
const renderLeaf = useCallback(
(props: RenderLeafProps) => <Leaf {...props} />,
[]
)
const editor = useMemo(
() => withTables(withHistory(withReact(createEditor()))) as CustomEditor,
[]
)
return (
<Slate editor={editor} initialValue={initialValue}>
<Editable renderElement={renderElement} renderLeaf={renderLeaf} />
</Slate>
)
}
const withTables = (editor: CustomEditor) => {
const { deleteBackward, deleteForward, insertBreak } = editor
editor.deleteBackward = (unit: 'character' | 'word' | 'line' | 'block') => {
const { selection } = editor
if (selection && Range.isCollapsed(selection)) {
const [cell] = Editor.nodes(editor, {
match: n =>
!Editor.isEditor(n) &&
SlateElement.isElement(n) &&
n.type === 'table-cell',
})
if (cell) {
const [, cellPath] = cell
const start = Editor.start(editor, cellPath)
if (Point.equals(selection.anchor, start)) {
return
}
}
}
deleteBackward(unit)
}
editor.deleteForward = unit => {
const { selection } = editor
if (selection && Range.isCollapsed(selection)) {
const [cell] = Editor.nodes(editor, {
match: n =>
!Editor.isEditor(n) &&
SlateElement.isElement(n) &&
n.type === 'table-cell',
})
if (cell) {
const [, cellPath] = cell
const end = Editor.end(editor, cellPath)
if (Point.equals(selection.anchor, end)) {
return
}
}
}
deleteForward(unit)
}
editor.insertBreak = () => {
const { selection } = editor
if (selection) {
const [table] = Editor.nodes(editor, {
match: n =>
!Editor.isEditor(n) &&
SlateElement.isElement(n) &&
n.type === 'table',
})
if (table) {
return
}
}
insertBreak()
}
return editor
}
const Element = ({ attributes, children, element }: RenderElementProps) => {
switch (element.type) {
case 'table':
return (
<table
className={css`
// avoid unexpected selection behavior on both sides of the table
position: relative;
`}
>
<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 }: RenderLeafProps) => {
if (leaf.bold) {
children = <strong>{children}</strong>
}
return <span {...attributes}>{children}</span>
}
const initialValue: Descendant[] = [
{
type: 'paragraph',
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' }],
},
],
},
],
},
{
type: 'paragraph',
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