CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-udecode--plate-media

Comprehensive media handling capabilities for the Plate rich text editor framework, supporting images, videos, audio files, and embeddable content

Pending
Overview
Eval results
Files

base-plugins.mddocs/

Base Media Plugins

Core Slate plugins that provide the foundation for all media handling in Plate. Each plugin defines element types, configurations, and basic transforms for different media types.

Capabilities

Base Image Plugin

Complete image plugin with upload, embed, and HTML parsing capabilities.

/**
 * Complete image plugin with upload, embed, and HTML parsing capabilities
 * Handles automatic image detection from URLs and file uploads
 */
const BaseImagePlugin: TSlatePlugin<ImageConfig>;

interface ImageConfig extends MediaPluginOptions {
  disableEmbedInsert?: boolean;
  disableUploadInsert?: boolean;
  uploadImage?: (dataUrl: ArrayBuffer | string) => Promise<string> | string;
}

Usage Example:

import { BaseImagePlugin } from "@udecode/plate-media";
import { createSlateEditor } from "@udecode/plate";

const editor = createSlateEditor({
  plugins: [
    BaseImagePlugin.configure({
      options: {
        disableEmbedInsert: false,
        disableUploadInsert: false,
        uploadImage: async (file) => {
          // Your upload logic
          const formData = new FormData();
          formData.append('file', file);
          const response = await fetch('/upload', {
            method: 'POST',
            body: formData
          });
          const data = await response.json();
          return data.url;
        }
      }
    })
  ]
});

Base Video Plugin

Base plugin for video elements with width and height attribute support.

/**
 * Base plugin for video elements with void node configuration
 * Supports width and height attributes for responsive sizing
 */
const BaseVideoPlugin: TSlatePlugin<VideoConfig>;

interface VideoConfig extends MediaPluginOptions {
  // Inherits standard media plugin options
}

The plugin configuration includes:

  • key: KEYS.video - Element type identifier
  • node: { dangerouslyAllowAttributes: ['width', 'height'], isElement: true, isVoid: true } - Node behavior
  • Support for width and height HTML attributes for responsive video sizing

Base Audio Plugin

Base plugin for audio elements with void node configuration.

/**
 * Base plugin for audio elements with void node configuration
 * Handles audio file embedding and playback controls
 */
const BaseAudioPlugin: TSlatePlugin<AudioConfig>;

interface AudioConfig extends MediaPluginOptions {
  // Inherits standard media plugin options
}

The plugin configuration includes:

  • key: KEYS.audio - Element type identifier
  • node: { isElement: true, isVoid: true } - Void element behavior
  • Integration with HTML audio element for playback

Base File Plugin

Base plugin for generic file elements with void node configuration.

/**
 * Base plugin for generic file elements with void node configuration
 * Handles file attachments and download functionality
 */
const BaseFilePlugin: TSlatePlugin<FileConfig>;

interface FileConfig extends MediaPluginOptions {
  // Inherits standard media plugin options
}

The plugin configuration includes:

  • key: KEYS.file - Element type identifier
  • node: { isElement: true, isVoid: true } - Void element behavior
  • Support for any file type attachment and download links

Editor Overrides

Advanced functionality that extends editor behavior for automatic media handling.

Image Embed Override

/**
 * Automatically inserts images when pasting image URLs
 * Detects image URLs in clipboard content and converts to image elements
 */
function withImageEmbed(config: ImageConfig): EditorOverride<ImageConfig>;

Usage Example:

import { withImageEmbed } from "@udecode/plate-media";

const editor = createSlateEditor({
  plugins: [
    BaseImagePlugin.configure({
      overrides: {
        insert: withImageEmbed
      }
    })
  ]
});

// Now pasting "https://example.com/image.jpg" will automatically create an image element

Image Upload Override

/**
 * Handles image uploads from clipboard and file drops
 * Processes files from drag & drop or paste operations
 */
function withImageUpload(config: ImageConfig): EditorOverride<ImageConfig>;

Usage Example:

import { withImageUpload } from "@udecode/plate-media";

const editor = createSlateEditor({
  plugins: [
    BaseImagePlugin.configure({
      overrides: {
        insert: withImageUpload,
        drop: withImageUpload
      },
      options: {
        uploadImage: async (file) => {
          // Handle the file upload
          return uploadToServer(file);
        }
      }
    })
  ]
});

// Now drag & drop or paste of image files will trigger upload

Configuration Options

Media Plugin Options

Base configuration interface shared across all media plugins.

interface MediaPluginOptions {
  /** Custom URL validation function */
  isUrl?: (text: string) => boolean;
  /** URL transformation function for processing URLs before embedding */
  transformUrl?: (url: string) => string;
}

Usage Example:

const mediaOptions: MediaPluginOptions = {
  isUrl: (text) => {
    // Custom URL validation logic
    return /^https?:\/\/.+/.test(text);
  },
  transformUrl: (url) => {
    // Transform URLs (e.g., add query parameters)
    return url + '?editor=plate';
  }
};

Element Keys

Constants used to identify different media element types in the Slate editor.

enum KEYS {
  image = 'img',
  video = 'video', 
  audio = 'audio',
  file = 'file',
  mediaEmbed = 'media_embed',
  placeholder = 'placeholder'
}

These keys are used internally by the plugins to:

  • Register element types with Slate
  • Identify elements during transforms
  • Apply type-specific rendering and behavior

Install with Tessl CLI

npx tessl i tessl/npm-udecode--plate-media

docs

base-plugins.md

image.md

index.md

media-embeds.md

react-components.md

upload-system.md

tile.json