import React, { ComponentType, ReactElement } from 'react'; import { EditorPlugin } from '@draft-js-plugins/editor'; import addImage from './modifiers/addImage'; import ImageComponent, { ImageProps } from './Image'; export interface ImagePluginTheme { image?: string; } const defaultTheme: ImagePluginTheme = {}; export interface ImagePluginConfig { decorator?(component: ComponentType): ComponentType; theme?: ImagePluginTheme; imageComponent?: ComponentType; } export type ImageEditorPlugin = EditorPlugin & { addImage: typeof addImage; }; export default (config: ImagePluginConfig = {}): ImageEditorPlugin => { const theme = config.theme ? config.theme : defaultTheme; let Image = config.imageComponent || ImageComponent; if (config.decorator) { Image = config.decorator(Image); } const ThemedImage = (props: ImageProps): ReactElement => ( ); return { blockRendererFn: (block, { getEditorState }) => { if (block.getType() === 'atomic') { const contentState = getEditorState().getCurrentContent(); const entity = block.getEntityAt(0); if (!entity) return null; const type = contentState.getEntity(entity).getType(); if (type === 'IMAGE' || type === 'image') { return { component: ThemedImage, editable: false, }; } return null; } return null; }, addImage, }; }; export const Image = ImageComponent;