Media Placeholder
A placeholder for media upload with progress indication.
Installation
npm install @udecode/plate-media
Usage
import {
AudioPlugin,
FilePlugin,
ImagePlugin,
MediaEmbedPlugin,
PlaceholderPlugin,
VideoPlugin,
} from '@udecode/plate-media/react';
const plugins = [
// ...otherPlugins,
PlaceholderPlugin.configure({
options: {
disableEmptyPlaceholder: true,
},
render: {
afterEditable: () => <MediaUploadToast />,
},
}),
];
const components = {
// ...otherComponents,
[PlaceholderPlugin.key]: MediaPlaceholderElement,
};
UploadThing Integration
The UploadThing integration provides an easy way to handle file uploads in your editor. Follow these steps to set it up:
-
Install the required dependencies:
npm install @uploadthing/react uploadthing
-
Install the media-placeholder-element component.
-
Set up the UploadThing API route by copying the example implementation.
-
Get your UploadThing API key from the dashboard and add it to your
.env
file:UPLOADTHING_SECRET=your_secret_key
Using your own backend
To implement your own backend for file uploads:
-
Remove the UploadThing implementation:
lib/uploadthing/ api/uploadthing/
-
Create your own upload hook:
function useUploadFile() { // Your implementation here return { isUploading: boolean, progress: number, uploadFile: (file: File) => Promise<UploadedFile>, uploadedFile: UploadedFile | undefined, uploadingFile: File | undefined, }; }
-
The hook should match the interface expected by the media placeholder component:
interface UploadedFile { key: string; url: string; name: string; size: number; type: string; }
Plate UI
Refer to the preview above.
Plate Plus
Plugins
PlaceholderPlugin
Media placeholder element plugin.
Options
-
You can use this option to configure upload limits for each file type, including:
- Maximum file count (e.g.
maxFileCount: 1
) - Maximum file size (e.g.
maxFileSize: '8MB'
) - Minimum file count (e.g.
minFileCount: 1
) - mediaType: Used for passing to the media-placeholder-elements file to distinguish between different file types and their progress bar styles.
default configuration:
uploadConfig: { audio: { maxFileCount: 1, maxFileSize: '8MB', mediaType: AudioPlugin.key, minFileCount: 1, }, blob: { maxFileCount: 1, maxFileSize: '8MB', mediaType: FilePlugin.key, minFileCount: 1, }, image: { maxFileCount: 3, maxFileSize: '4MB', mediaType: ImagePlugin.key, minFileCount: 1, }, pdf: { maxFileCount: 1, maxFileSize: '4MB', mediaType: FilePlugin.key, minFileCount: 1, }, text: { maxFileCount: 1, maxFileSize: '64KB', mediaType: FilePlugin.key, minFileCount: 1, }, video: { maxFileCount: 1, maxFileSize: '16MB', mediaType: VideoPlugin.key, minFileCount: 1, }, },
here is all allowed file types (keys for
uploadConfig
):export const ALLOWED_FILE_TYPES = [ 'image', 'video', 'audio', 'pdf', 'text', 'blob', ] as const;
- Maximum file count (e.g.
- Default:
false
- Default:
false
- Default:
5
Configuration for different file types:
Disable empty placeholder when no file is selected.
Whether we can undo to the placeholder after the file upload is complete.
Maximum number of files that can be uploaded at once.
Transforms
editor.tf.insert.audioPlaceholder
Inserts an audio placeholder element.
Parameters
Options for the insert nodes transform.
editor.tf.insert.filePlaceholder
Inserts a file placeholder element.
Parameters
Options for the insert nodes transform.
editor.tf.insert.imagePlaceholder
Inserts an image placeholder element.
Parameters
Options for the insert nodes transform.
editor.tf.insert.videoPlaceholder
Inserts a video placeholder element.
Parameters
Options for the insert nodes transform.
Types
TPlaceholderElement
type TPlaceholderElement = TElement & {
mediaType: string;
};