Video Node Type

Category: Media • Detailed implementation guide with view integration notes

Schema Definition

Schema definition for the Video node type:

{
  video: {
    group: 'block',
    attrs: {
      src: { default: '' },
      poster: { default: null },
      width: { default: null },
      height: { default: null },
      controls: { default: true },
      autoplay: { default: false },
      loop: { default: false }
    }
  }
}

Model Representation

Example model representation:

{
  type: 'video',
  attrs: {
    src: 'https://example.com/video.mp4',
    poster: 'https://example.com/poster.jpg',
    width: 800,
    height: 450,
    controls: true
  }
}

HTML Serialization

Converting model to HTML:

function serializeVideo(node) {
  const attrs = {};
  if (node.attrs.src) attrs.src = escapeHtml(node.attrs.src);
  if (node.attrs.poster) attrs.poster = escapeHtml(node.attrs.poster);
  if (node.attrs.width) attrs.width = node.attrs.width;
  if (node.attrs.height) attrs.height = node.attrs.height;
  if (node.attrs.controls) attrs.controls = true;
  if (node.attrs.autoplay) attrs.autoplay = true;
  if (node.attrs.loop) attrs.loop = true;
  return '<video' + serializeAttrs(attrs) + '></video>';
}

HTML Deserialization

Parsing HTML to model:

function parseVideo(domNode) {
  return {
    type: 'video',
    attrs: {
      src: domNode.getAttribute('src') || '',
      poster: domNode.getAttribute('poster') || null,
      width: parseInt(domNode.getAttribute('width')) || null,
      height: parseInt(domNode.getAttribute('height')) || null,
      controls: domNode.hasAttribute('controls'),
      autoplay: domNode.hasAttribute('autoplay'),
      loop: domNode.hasAttribute('loop')
    }
  };
}

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 video
const video = document.createElement('video');
video.src = node.attrs.src;
if (node.attrs.poster) video.poster = node.attrs.poster;
if (node.attrs.width) video.width = node.attrs.width;
if (node.attrs.height) video.height = node.attrs.height;
video.controls = node.attrs.controls !== false;
video.autoplay = node.attrs.autoplay === true;
video.loop = node.attrs.loop === true;
video.contentEditable = 'false'; // Videos are not editable

Common Issues

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

Common issues and solutions:

// Issue: Video format support
// Solution: Check format support
function checkVideoSupport(format) {
  const video = document.createElement('video');
  return video.canPlayType('video/' + format) !== '';
}

// Issue: Video loading
// Solution: Handle loading states
video.addEventListener('loadstart', () => {
  // Show loading indicator
});
video.addEventListener('canplay', () => {
  // Hide loading indicator
});

// Issue: Video selection
// Solution: Handle video selection in contenteditable
function selectVideo(video) {
  const range = document.createRange();
  range.selectNode(video);
  const selection = window.getSelection();
  selection.removeAllRanges();
  selection.addRange(range);
}

Implementation

Complete implementation example:

class VideoNode {
  constructor(attrs) {
    this.type = 'video';
    this.attrs = {
      src: attrs?.src || '',
      poster: attrs?.poster || null,
      width: attrs?.width || null,
      height: attrs?.height || null,
      controls: attrs?.controls !== false,
      autoplay: attrs?.autoplay === false,
      loop: attrs?.loop === false
    };
  }
  
  toDOM() {
    const video = document.createElement('video');
    Object.assign(video, this.attrs);
    return video;
  }
  
  static fromDOM(domNode) {
    return new VideoNode({
      src: domNode.getAttribute('src') || '',
      poster: domNode.getAttribute('poster') || null,
      width: parseInt(domNode.getAttribute('width')) || null,
      height: parseInt(domNode.getAttribute('height')) || null,
      controls: domNode.hasAttribute('controls'),
      autoplay: domNode.hasAttribute('autoplay'),
      loop: domNode.hasAttribute('loop')
    });
  }
}