Image plugin for Plate rich text editor that enables embedding, uploading, and managing images with advanced features like drag-and-drop, resizing, and captions.
npx @tessl/cli install tessl/npm-udecode--plate-image@15.0.0The @udecode/plate-image package provides a comprehensive image plugin for the Plate rich text editor framework, built on top of Slate.js. It enables developers to embed, upload, and manage images within rich text documents with advanced features including drag-and-drop support, resizing capabilities, clipboard paste functionality, and automatic image upload handling.
npm install @udecode/plate-imageimport { createImagePlugin, ELEMENT_IMAGE, TImageElement } from "@udecode/plate-image";For individual components and utilities:
import {
Image,
insertImage,
useImageElement,
useImageCaptionString,
isImageUrl,
withImage,
withImageUpload,
withImageEmbed
} from "@udecode/plate-image";For component hooks and store management:
import {
useImageCaption,
useImageCaptionState,
useImageResizable,
imageStore,
useImageStore,
imageGlobalStore
} from "@udecode/plate-image";For all types and interfaces:
import {
TImageElement,
ImagePlugin,
ImageCaptionProps,
ImageResizableProps,
ImageCaptionTextareaProps
} from "@udecode/plate-image";import { createImagePlugin, ELEMENT_IMAGE } from "@udecode/plate-image";
import { createPlateEditor } from "@udecode/plate-core";
// Create editor with image plugin
const editor = createPlateEditor({
plugins: [
createImagePlugin({
options: {
uploadImage: async (dataUrl) => {
// Custom upload logic
const response = await fetch('/api/upload', {
method: 'POST',
body: JSON.stringify({ image: dataUrl }),
});
return response.json().url;
}
}
})
]
});
// Insert an image programmatically
import { insertImage } from "@udecode/plate-image";
insertImage(editor, "https://example.com/image.jpg");The @udecode/plate-image plugin is built around several key components:
createImagePlugin provides the main plugin configuration and behaviorwithImage, withImageUpload, withImageEmbed) that extend editor functionalityCore plugin factory and configuration options for integrating image functionality into Plate editors.
function createImagePlugin<ImagePlugin>(options?: {
key?: string;
isElement?: boolean;
isVoid?: boolean;
options?: ImagePlugin;
}): PlatePlugin<ImagePlugin>;
const ELEMENT_IMAGE: string;TypeScript interfaces and types for image elements and plugin configuration.
interface TImageElement extends TElement {
url: string;
width?: number;
caption?: TDescendant[];
}
interface ImagePlugin {
uploadImage?: (dataUrl: string | ArrayBuffer) => Promise<string | ArrayBuffer> | string | ArrayBuffer;
disableUploadInsert?: boolean;
disableEmbedInsert?: boolean;
}Higher-order functions that enhance the Plate editor with image-specific functionality including upload handling and URL embedding.
function withImage<V extends Value = Value, E extends PlateEditor<V> = PlateEditor<V>>(
editor: E,
plugin: WithPlatePlugin<ImagePlugin, V, E>
): E;
function withImageUpload<V extends Value = Value, E extends PlateEditor<V> = PlateEditor<V>>(
editor: E,
plugin: WithPlatePlugin<ImagePlugin, V, E>
): E;
function withImageEmbed<V extends Value = Value, E extends PlateEditor<V> = PlateEditor<V>>(
editor: E,
plugin: WithPlatePlugin<ImagePlugin, V, E>
): E;Complete set of React components for rendering images with captions, resizing handles, and interactive features.
const Image: {
Root: React.ComponentType<any>;
Caption: React.ComponentType<ImageCaptionProps>;
Img: React.ComponentType<any>;
Resizable: React.ComponentType<ImageResizableProps>;
CaptionTextarea: React.ComponentType<ImageCaptionTextareaProps>;
};
interface ImageCaptionProps extends HTMLPropsAs<'figcaption'> {
readOnly?: boolean;
}
interface ImageResizableProps extends Omit<ResizableProps, 'as'>, AsProps<'div'> {
align?: 'left' | 'center' | 'right';
readOnly?: boolean;
}React hooks for accessing image element data, managing captions, and integrating with the image store system.
function useImageElement(): TImageElement;
function useImageCaptionString(): string;Functions for programmatically inserting images and utility functions for image URL validation.
function insertImage<V extends Value>(
editor: PlateEditor<V>,
url: string | ArrayBuffer
): void;
function isImageUrl(url: string): boolean;// Core Plate framework types (from @udecode/plate-core)
interface TElement {
type: string;
children: TDescendant[];
}
interface TDescendant {
[key: string]: any;
}
interface PlateEditor<V extends Value = Value> {
[key: string]: any;
}
interface Value {
[key: string]: any;
}
interface WithPlatePlugin<T, V extends Value, E extends PlateEditor<V>> {
options: T;
[key: string]: any;
}
// HTML and component prop types
interface HTMLPropsAs<T extends keyof JSX.IntrinsicElements> {
[key: string]: any;
}
interface AsProps<T extends keyof JSX.IntrinsicElements> {
as?: T;
}
// External library types
interface ResizableProps {
[key: string]: any;
}