Schema Definition
Schema definition for the Table Cell node type:
{
tableCell: {
content: 'block+',
tableRole: 'cell',
isolating: true,
attrs: {
colspan: { default: 1 },
rowspan: { default: 1 }
},
node.attrs.rowspan > 1 ? { rowspan: node.attrs.rowspan } : {}, 0]
}
}Model Representation
Example model representation:
{
type: 'tableCell',
attrs: { colspan: 1, rowspan: 1 },
children: [
{
type: 'paragraph',
children: [{ type: 'text', text: 'Cell content' }]
}
]
}HTML Serialization
Converting model to HTML:
function serializeTableCell(node) {
const attrs = {};
if (node.attrs.colspan > 1) attrs.colspan = node.attrs.colspan;
if (node.attrs.rowspan > 1) attrs.rowspan = node.attrs.rowspan;
return '<td' + serializeAttrs(attrs) + '>' +
serializeChildren(node.children) + '</td>';
}HTML Deserialization
Parsing HTML to model:
function parseTableCell(domNode) {
return {
type: 'tableCell',
attrs: {
colspan: parseInt(domNode.getAttribute('colspan')) || 1,
rowspan: parseInt(domNode.getAttribute('rowspan')) || 1
},
children: Array.from(domNode.childNodes)
.map(node => parseNode(node))
.filter(Boolean)
};
}View Integration
View Integration Notes: Pay special attention to contenteditable behavior, selection handling, and event management when implementing this node type in your view layer.
View integration code:
// Rendering table cell
const td = document.createElement('td');
td.contentEditable = 'true';
if (node.attrs.colspan > 1) td.colSpan = node.attrs.colspan;
if (node.attrs.rowspan > 1) td.rowSpan = node.attrs.rowspan;
node.children.forEach(child => {
td.appendChild(renderNode(child));
});
// Handling Enter in table cell
function handleEnterInTableCell(e) {
e.preventDefault();
// Create new paragraph in cell
const p = document.createElement('p');
insertNode({ type: 'paragraph', children: [] });
}Common Issues
Common Pitfalls: These are issues frequently encountered when implementing this node type. Review carefully before implementation.
Common issues and solutions:
// Issue: Empty cells
// Solution: Ensure at least one block
function validateTableCell(cell) {
if (!cell.querySelector('p, ul, ol, h1, h2, h3, h4, h5, h6')) {
const p = document.createElement('p');
cell.appendChild(p);
}
}
// Issue: Cell selection
// Solution: Handle cell selection
function selectCell(td) {
const range = document.createRange();
range.selectNodeContents(td);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
// Issue: Colspan/rowspan validation
// Solution: Validate span attributes
function validateCellSpan(cell, table) {
// Ensure colspan/rowspan don't exceed table dimensions
}Implementation
Complete implementation example:
class TableCellNode {
constructor(attrs, children) {
this.type = 'tableCell';
this.attrs = {
colspan: attrs?.colspan || 1,
rowspan: attrs?.rowspan || 1
};
this.children = children || [];
}
toDOM() {
const td = document.createElement('td');
if (this.attrs.colspan > 1) td.colSpan = this.attrs.colspan;
if (this.attrs.rowspan > 1) td.rowSpan = this.attrs.rowspan;
this.children.forEach(child => {
td.appendChild(child.toDOM());
});
return td;
}
static fromDOM(domNode) {
return new TableCellNode(
{
colspan: parseInt(domNode.getAttribute('colspan')) || 1,
rowspan: parseInt(domNode.getAttribute('rowspan')) || 1
},
Array.from(domNode.childNodes)
.map(node => parseNode(node))
.filter(Boolean)
);
}
}