import React from 'react'
import { useEditor, EditorContent } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
import Table from '@tiptap/extension-table'
import TableRow from '@tiptap/extension-table-row'
import TableCell from '@tiptap/extension-table-cell'
import TableHeader from '@tiptap/extension-table-header'
import './styles.scss'
const CustomTableCell = TableCell.extend({
addAttributes() {
return {
// extend the existing attributes …
...this.parent?.(),
// and add a new one …
backgroundColor: {
default: null,
parseHTML: element => {
return {
backgroundColor: element.getAttribute('data-background-color'),
}
},
renderHTML: attributes => {
return {
'data-background-color': attributes.backgroundColor,
style: `background-color: ${attributes.backgroundColor}`,
}
},
},
}
},
})
export const tableHTML = `
| Firstname |
Lastname |
Age |
| Jill |
Smith |
50 |
| Eve |
Jackson |
94 |
| John |
Doe |
80 |
`
const MenuBar = ({ editor }) => {
if (!editor) {
return null
}
return (
<>
>
)
}
export default () => {
const editor = useEditor({
extensions: [
StarterKit,
Table.configure({
resizable: true,
}),
TableRow,
TableHeader,
// Default TableCell
// TableCell,
// Custom TableCell with backgroundColor attribute
CustomTableCell,
],
content: `
Have you seen our tables? They are amazing!
- tables with rows, cells and headers (optional)
- support for
colgroup and rowspan
- and even resizable columns (optional)
Here is an example:
| Name |
Description |
| Cyndi Lauper |
singer |
songwriter |
actress |
| Philipp Kühn |
designer |
developer |
maker |
| Hans Pagel |
wrote this |
that’s it |
`,
})
return (
)
}