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

Image block: avoid edit (history/undo) during image upload #22761

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,20 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, { select } ) => {
// Do not add new properties here, use `useDispatch` instead to avoid
// leaking new props to the public API (editor.BlockListBlock filter).
return {
setAttributes( newAttributes ) {
setAttributes( newAttributes, options = {} ) {
const { clientId } = ownProps;
updateBlockAttributes( clientId, newAttributes );

if ( options.silent ) {
const attributes = window.wp.data
.select( 'core/block-editor' )
.getBlockAttributes( clientId );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To do: move to reducer.


for ( const name in newAttributes ) {
attributes[ name ] = newAttributes[ name ];
}
} else {
updateBlockAttributes( clientId, newAttributes );
}
},
onInsertBlocks( blocks, index ) {
const { rootClientId } = ownProps;
Expand Down
30 changes: 26 additions & 4 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ function getFilename( url ) {
}
}

export function useForceUpdate() {
const [ , setState ] = useState();
return () => setState( ( value ) => ! value );
}

export function ImageEdit( {
attributes: {
url = '',
Expand All @@ -119,6 +124,7 @@ export function ImageEdit( {
noticeOperations,
onReplace,
} ) {
const forceUpdate = useForceUpdate();
const ref = useRef();
const { image, maxWidth, isRTL, imageSizes, mediaUpload } = useSelect(
( select ) => {
Expand Down Expand Up @@ -166,6 +172,9 @@ export function ImageEdit( {
return;
}

onSelectImage.count++;

const options = {};
let mediaAttributes = pickRelevantMediaFiles( media );

// If the current image is temporary but an alt text was meanwhile
Expand Down Expand Up @@ -208,12 +217,25 @@ export function ImageEdit( {
mediaAttributes.href = media.link;
}

setAttributes( {
...mediaAttributes,
...additionalAttributes,
} );
if ( onSelectImage.count > 1 ) {
options.silent = true;
}

setAttributes(
{
...mediaAttributes,
...additionalAttributes,
},
options
);

if ( onSelectImage.count > 1 ) {
forceUpdate();
}
}

onSelectImage.count = 0;

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