Skip to content

Commit

Permalink
Block API: Introduce "local" attributes and use it for the image block (
Browse files Browse the repository at this point in the history
WordPress#63076)

Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: mtias <matveb@git.wordpress.org>
Co-authored-by: talldan <talldanwp@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: noisysocks <noisysocks@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
  • Loading branch information
9 people authored and carstingaxion committed Jul 18, 2024
1 parent 414548c commit d0a592d
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 30 deletions.
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 )
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

0 comments on commit d0a592d

Please sign in to comment.