Skip to content
This repository has been archived by the owner on Feb 21, 2019. It is now read-only.

How to add table to the rich text ? data format for value is different #73

Open
renyuzh opened this issue May 31, 2018 · 1 comment
Open

Comments

@renyuzh
Copy link

renyuzh commented May 31, 2018

No description provided.

@jay-shah
Copy link

jay-shah commented Aug 6, 2018

@renyuzh you can do it in the same way as shown in the Slate docs

For example:

let value = Value.fromJSON({
    document: {
        nodes: [
            {
            object: 'block',
            type: 'table',
            nodes: [
                {
                    object: 'block',
                    type: 'table_row',
                    nodes: [
                        {
                            object: 'block',
                            type: 'table_cell',
                            nodes: [
                                {
                                    object: 'block',
                                    type: 'paragraph',
                                    nodes: [
                                        {
                                            object: 'text',
                                            leaves: [{ text: '1' }]
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            object: 'block',
                            type: 'table_cell',
                            nodes: [
                                {
                                    object: 'block',
                                    type: 'paragraph',
                                    nodes: [
                                        {
                                            object: 'text',
                                            leaves: [{ text: '2' }]
                                        }
                                    ]
                                }
                            ]
                        },
                       
                    ]
                },
                {
                    object: 'block',
                    type: 'table_row',
                    nodes: [
                        {
                            object: 'block',
                            type: 'table_cell',
                            nodes: [
                                {
                                    object: 'block',
                                    type: 'paragraph',
                                    nodes: [
                                        {
                                            object: 'text',
                                            leaves: [{ text: '1' }]
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            object: 'block',
                            type: 'table_cell',
                            nodes: [
                                {
                                    object: 'block',
                                    type: 'paragraph',
                                    nodes: [
                                        {
                                            object: 'text',
                                            leaves: [{ text: '2' }]
                                        }
                                    ]
                                }
                            ]
                        },
                    ]
                }
            ]
        }]

    }
})

You will have to add to your render node method:

renderNode = (props) => {
    const { node, attributes, children } = props

    switch (node.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>
    }
}

This would create a table as:
screenshot from 2018-08-06 16-03-09

Also be sure to include

const tablePlugin = EditTable({
    typeTable: 'table',
    typeRow: 'table_row',
    typeCell: 'table_cell',
    typeContent: 'paragraph'

})

const plugins = [tablePlugin]; 

render() {
    const { value } = this.state;
    return (
            <Editor
                value={value}
                onChange={this.onChange}
                renderNode={this.renderNode}
                plugins={plugins}
            />
    )
}

into your code.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants