Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block API: Introduce "local" attributes and use it for the image block #63076

Merged
merged 7 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre
- **Name:** core/image
- **Category:** media
- **Supports:** align (center, full, left, right, wide), anchor, color (~~background~~, ~~text~~), filter (duotone), interactivity, shadow ()
- **Attributes:** alt, aspectRatio, caption, height, href, id, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width
- **Attributes:** alt, aspectRatio, blob, caption, height, href, id, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width

## Latest Comments

Expand Down
15 changes: 8 additions & 7 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function GalleryEdit( props ) {
return (
ALLOWED_MEDIA_TYPES.some(
( mediaType ) => mediaTypeSelector?.indexOf( mediaType ) === 0
) || file.url?.indexOf( 'blob:' ) === 0
) || file.blob
);
}

Expand All @@ -249,9 +249,9 @@ function GalleryEdit( props ) {
const imageArray = newFileUploads
? Array.from( selectedImages ).map( ( file ) => {
if ( ! file.url ) {
return pickRelevantMediaFiles( {
url: createBlobURL( file ),
} );
return {
blob: createBlobURL( file ),
};
}

return file;
Expand All @@ -271,9 +271,9 @@ function GalleryEdit( props ) {
.filter( ( file ) => file.url || isValidFileType( file ) )
.map( ( file ) => {
if ( ! file.url ) {
return pickRelevantMediaFiles( {
url: createBlobURL( file ),
} );
return {
blob: file.blob || createBlobURL( file ),
};
}

return file;
Expand Down Expand Up @@ -307,6 +307,7 @@ function GalleryEdit( props ) {
const newBlocks = newImageList.map( ( image ) => {
return createBlock( 'core/image', {
id: image.id,
blob: image.blob,
url: image.url,
caption: image.caption,
alt: image.alt,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/gallery/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ const transforms = {
if ( isGalleryV2Enabled() ) {
const innerBlocks = files.map( ( file ) =>
createBlock( 'core/image', {
url: createBlobURL( file ),
blob: createBlobURL( file ),
} )
);

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"keywords": [ "img", "photo", "picture" ],
"textdomain": "default",
"attributes": {
"blob": {
"type": "string",
"__experimentalRole": "local"
},
"url": {
"type": "string",
"source": "attribute",
Expand Down
29 changes: 10 additions & 19 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,6 @@ export const pickRelevantMediaFiles = ( image, size ) => {
return imageProps;
};

/**
* Is the URL a temporary blob URL? A blob URL is one that is used temporarily
* while the image is being uploaded and will not have an id yet allocated.
*
* @param {number=} id The id of the image.
* @param {string=} url The url of the image.
*
* @return {boolean} Is the URL a Blob URL
*/
const isTemporaryImage = ( id, url ) => ! id && isBlobURL( url );

/**
* Is the url for the image hosted externally. An externally hosted image has no
* id and is not a blob url.
Expand Down Expand Up @@ -118,9 +107,7 @@ export function ImageEdit( {
align,
metadata,
} = attributes;
const [ temporaryURL, setTemporaryURL ] = useState( () => {
return isTemporaryImage( id, url ) ? url : undefined;
} );
const [ temporaryURL, setTemporaryURL ] = useState( attributes.blob );

const altRef = useRef();
useEffect( () => {
Expand Down Expand Up @@ -157,8 +144,8 @@ export function ImageEdit( {
src: undefined,
id: undefined,
url: undefined,
blob: undefined,
} );
setTemporaryURL( undefined );
}

function onSelectImage( media ) {
Expand All @@ -169,7 +156,9 @@ export function ImageEdit( {
id: undefined,
title: undefined,
caption: undefined,
blob: undefined,
} );
setTemporaryURL();

return;
}
Expand All @@ -179,8 +168,6 @@ export function ImageEdit( {
return;
}

setTemporaryURL();

const { imageDefaultSize } = getSettings();

// Try to use the previous selected image size if its available
Expand Down Expand Up @@ -254,24 +241,28 @@ export function ImageEdit( {
mediaAttributes.href = href;

setAttributes( {
blob: undefined,
...mediaAttributes,
...additionalAttributes,
linkDestination,
} );
setTemporaryURL();
}

function onSelectURL( newURL ) {
if ( newURL !== url ) {
setAttributes( {
blob: undefined,
url: newURL,
id: undefined,
sizeSlug: getSettings().imageDefaultSize,
} );
setTemporaryURL();
}
}

useUploadMediaFromBlobURL( {
url,
url: temporaryURL,
allowedTypes: ALLOWED_MEDIA_TYPES,
onChange: onSelectImage,
onError: onUploadError,
Expand All @@ -292,7 +283,7 @@ export function ImageEdit( {
const shadowProps = getShadowClassesAndStyles( attributes );

const classes = clsx( className, {
'is-transient': temporaryURL,
'is-transient': !! temporaryURL,
'is-resized': !! width || !! height,
[ `size-${ sizeSlug }` ]: sizeSlug,
'has-custom-border':
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/image/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const transforms = {
transform( files ) {
const blocks = files.map( ( file ) => {
return createBlock( 'core/image', {
url: createBlobURL( file ),
blob: createBlobURL( file ),
} );
} );
return blocks;
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/utils/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export function useUploadMediaFromBlobURL( args = {} ) {
if ( hasUploadStarted.current ) {
return;
}

if (
! latestArgs.current.url ||
! isBlobURL( latestArgs.current.url )
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
5 changes: 5 additions & 0 deletions packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ export function getCommentAttributes( blockType, attributes ) {
return accumulator;
}

// Ignore all local attributes
if ( attributeSchema.__experimentalRole === 'local' ) {
return accumulator;
}

// Ignore default value.
if (
'default' in attributeSchema &&
Expand Down
24 changes: 24 additions & 0 deletions packages/blocks/src/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,30 @@ describe( 'block serializer', () => {

expect( attributes ).toEqual( { fruit: 'bananas' } );
} );

it( 'should ingore local attributes', () => {
const attributes = getCommentAttributes(
{
attributes: {
blob: {
type: 'string',
__experimentalRole: 'local',
},
url: {
type: 'string',
},
},
},
{
blob: 'blob://false-url.com',
url: 'http://real-url.com',
}
);

expect( attributes ).toEqual( {
url: 'http://real-url.com',
} );
} );
} );

describe( 'serializeAttributes()', () => {
Expand Down
Loading