import {
Popover as WCPopover,
Button,
__experimentalNumberControl as NumberControl,
TextareaControl as WCTextareaControl,
} from '@wordpress/components';
import { inlineImage } from '@wordpress/icons';
import { Link, Stack } from '@wordpress/ui';
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { insertObject, useAnchor } from '@wordpress/rich-text';
import type { RichTextValue } from '@wordpress/rich-text';
import {
MediaUpload,
RichTextToolbarButton,
MediaUploadCheck,
} from '@wordpress/block-editor';
import type { FormatEditProps } from '../types';
/**
* The attributes the `core/image` format registers.
*/
interface ImageFormatAttributes {
className?: string;
style?: string;
url?: string;
alt?: string;
}
interface InlineImageUIProps {
value: RichTextValue;
onChange: ( value: RichTextValue ) => void;
activeObjectAttributes: ImageFormatAttributes;
contentRef: React.RefObject< HTMLElement >;
}
const ALLOWED_MEDIA_TYPES = [ 'image' ];
const name = 'core/image';
const title = __( 'Inline image' );
/**
* Extracts the image ID from the className attribute.
*
* @param activeObjectAttributes The attributes of the active object.
* @return The extracted image ID or undefined if not found.
*/
function getCurrentImageId(
activeObjectAttributes: ImageFormatAttributes
): number | undefined {
if ( ! activeObjectAttributes.className ) {
return undefined;
}
const [ , id ] =
activeObjectAttributes.className.match( /wp-image-(\d+)/ ) ?? [];
return id ? parseInt( id, 10 ) : undefined;
}
export const image = {
name,
title,
keywords: [ __( 'photo' ), __( 'media' ) ],
object: true,
tagName: 'img',
className: null,
attributes: {
className: 'class',
style: 'style',
url: 'src',
alt: 'alt',
},
edit: Edit,
};
function InlineUI( {
value,
onChange,
activeObjectAttributes,
contentRef,
}: InlineImageUIProps ) {
const style = activeObjectAttributes.style;
const alt = activeObjectAttributes.alt;
const width = style?.replace( /\D/g, '' );
const [ editedWidth, setEditedWidth ] = useState( width );
const [ editedAlt, setEditedAlt ] = useState( alt );
const hasChanged = editedWidth !== width || editedAlt !== alt;
const popoverAnchor = useAnchor( {
// eslint-disable-next-line react-hooks/refs
editableContentElement: contentRef.current,
settings: image,
} );
return (
);
}
function Edit( {
value,
onChange,
onFocus,
isObjectActive,
activeObjectAttributes,
contentRef,
}: FormatEditProps ) {
return (
{
const newImage = {
type: name,
attributes: {
className: `wp-image-${ id }`,
style: `width: ${ Math.min( imgWidth, 150 ) }px;`,
url,
alt,
},
};
let newValue: RichTextValue;
// Swap the image in place, so its formats, like a link, stay.
if ( isObjectActive && value.start !== undefined ) {
const replacements = value.replacements.slice();
replacements[ value.start ] = newImage;
newValue = {
...value,
replacements,
start: value.end,
end: value.end,
};
} else {
newValue = insertObject( value, newImage );
}
onChange( newValue );
onFocus();
} }
render={ ( { open }: { open: () => void } ) => (
) }
/>
{ isObjectActive && (
) }
);
}