Card Header Node Type

Category: Complex • Detailed implementation guide with view integration notes

Schema Definition

Schema definition for the Card Header node type:

{
  cardHeader: {
    content: 'block+',
  }
}

Model Representation

Example model representation:

{
  type: 'cardHeader',
  children: [
    {
      type: 'paragraph',
      children: [{ type: 'text', text: 'Card Title' }]
    }
  ]
}

HTML Serialization

Converting model to HTML:

function serializeCardHeader(node) {
  return '<div data-type="card-header" class="card-header">' + 
         serializeChildren(node.children) + '</div>';
}

HTML Deserialization

Parsing HTML to model:

function parseCardHeader(domNode) {
  return {
    type: 'cardHeader',
    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 card header
const header = document.createElement('div');
header.setAttribute('data-type', 'card-header');
header.className = 'card-header';
header.contentEditable = 'true';
node.children.forEach(child => {
  header.appendChild(renderNode(child));
});

Common Issues

Common Pitfalls: These are issues frequently encountered when implementing this node type. Review carefully before implementation.

Common issues and solutions:

// Issue: Empty header
// Solution: Ensure at least one block
function validateCardHeader(header) {
  if (!header.querySelector('p, h1, h2, h3, h4, h5, h6')) {
    const p = document.createElement('p');
    header.appendChild(p);
  }
}

Implementation

Complete implementation example:

class CardHeaderNode {
  constructor(children) {
    this.type = 'cardHeader';
    this.children = children || [];
  }
  
  toDOM() {
    const header = document.createElement('div');
    header.setAttribute('data-type', 'card-header');
    header.className = 'card-header';
    this.children.forEach(child => {
      header.appendChild(child.toDOM());
    });
    return header;
  }
  
  static fromDOM(domNode) {
    return new CardHeaderNode(
      Array.from(domNode.childNodes)
        .map(node => parseNode(node))
        .filter(Boolean)
    );
  }
}