Skip to content
Closed
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 (), spacing (margin)
- **Attributes:** alt, aspectRatio, blob, 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, uploadedTo, url, width

## Latest Comments

Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@wordpress/date": "file:../date",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
"@wordpress/editor": "14.24.0",
"@wordpress/element": "file:../element",
"@wordpress/escape-html": "file:../escape-html",
"@wordpress/hooks": "file:../hooks",
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 @@ -100,6 +100,10 @@
"source": "attribute",
"selector": "figure > a",
"attribute": "target"
},
"uploadedTo": {
"type": "number",
"default": 0
}
},
"supports": {
Expand Down
123 changes: 121 additions & 2 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import clsx from 'clsx';
import { isBlobURL, createBlobURL } from '@wordpress/blob';
import { createBlock, getBlockBindingsSource } from '@wordpress/blocks';
import { Placeholder } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import {
useDispatch,
useSelect,
subscribe,
select as editorSelect,
} from '@wordpress/data';
import {
BlockIcon,
useBlockProps,
Expand All @@ -19,11 +24,13 @@ import {
__experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles,
useBlockEditingMode,
} from '@wordpress/block-editor';
import { store as postEditorStore } from '@wordpress/editor';
import { useEffect, useRef, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { image as icon, plugins as pluginsIcon } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';
import { useResizeObserver } from '@wordpress/compose';
import { useResizeObserver, useDebounce } from '@wordpress/compose';
import apiFetch from '@wordpress/api-fetch';

/**
* Internal dependencies
Expand Down Expand Up @@ -109,11 +116,21 @@ export function ImageEdit( {
scale,
align,
metadata,
uploadedTo,
} = attributes;

const [ temporaryURL, setTemporaryURL ] = useState( attributes.blob );

const containerRef = useRef();

const {
getCurrentPostId,
isSavingPost,
isPublishingPost,
isAutosavingPost,
} = editorSelect( postEditorStore );

const postId = getCurrentPostId();
// Only observe the max width from the parent container when the parent layout is not flex nor grid.
// This won't work for them because the container width changes with the image.
// TODO: Find a way to observe the container width for flex and grid layouts.
Expand Down Expand Up @@ -228,6 +245,9 @@ export function ImageEdit( {
} );
setTemporaryURL();

// Detach media from post when the reset option is selected.
detachMediaFromPost();

return;
}

Expand Down Expand Up @@ -309,10 +329,109 @@ export function ImageEdit( {
...mediaAttributes,
...additionalAttributes,
linkDestination,
// set uploadedTo as the postId to which the media is attached or current postId if not attached to any post.
uploadedTo: media?.uploadedTo || postId,
} );
setTemporaryURL();
}

/**
* Detach media from post when the reset option is selected.
* Detach only if the media is attached to the current post.
*/
function detachMediaFromPost() {
if ( uploadedTo !== postId ) {
return;
}

apiFetch( {
path: `/wp/v2/media/${ id }`,
method: 'PATCH',
data: {
post: 0,
},
} );

setAttributes( {
uploadedTo: 0,
} );
}

/**
* Attach media to post when the save option is selected.
*/
function attachMediaToPost() {
// Bail early if media ID doesn't exist.
if ( ! id ) {
return;
}

// Bail early if media is already attached to another post.
// Instead of early bail out, we set the uploadedTo to the current postId.
// This is to avoid updating attributes after the post is saved/published which ignores the new attributes.
if ( uploadedTo !== postId ) {
return;
}

// Attach media to post by updating via WP REST API.
apiFetch( {
path: `/wp/v2/media/${ id }`,
method: 'PATCH',
data: {
post: postId,
},
} );
}

// Debounce the attachMediaToPost function to avoid multiple calls.
const debouncedAttachMediaToPost = useDebounce( attachMediaToPost, 250 );

useEffect( () => {
// Prevent subscribing if the media ID doesn't exist.
if ( ! id ) {
return;
}

// Subscribe ONCE, when the block editor mounts.
const unsubscribe = subscribe( () => {
const isSaving = isSavingPost();
const isPublishing = isPublishingPost();
const isAutosaving = isAutosavingPost();

if ( ( isSaving || isPublishing ) && ! isAutosaving ) {
debouncedAttachMediaToPost();
}
} );

return () => unsubscribe();
}, [
id,
isSavingPost,
isPublishingPost,
isAutosavingPost,
debouncedAttachMediaToPost,
] );

// Detach media from post when the block is removed.
useEffect( () => {
// Clean up function to be called when the block is removed.
return () => {
// Bail early if the media is not attached to the current post.
if ( uploadedTo !== postId ) {
return;
}

// Detach media from post by updating via WP REST API.
apiFetch( {
path: `/wp/v2/media/${ id }`,
method: 'PATCH',
data: {
post: 0,
},
} );
};
}, [ uploadedTo, postId, id ] );

function onSelectURL( newURL ) {
if ( newURL !== url ) {
setAttributes( {
Expand Down