Skip to content

Commit 9a26ba8

Browse files
adamsilversteinryanwelchertyxlaellatrixswissspidy
authored andcommitted
Lock post saving during image uploads (#41120)
Co-authored-by: adamsilverstein <[email protected]> Co-authored-by: ryanwelcher <[email protected]> Co-authored-by: tyxla <[email protected]> Co-authored-by: ellatrix <[email protected]> Co-authored-by: swissspidy <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: annezazu <[email protected]> Co-authored-by: Mamaduka <[email protected]> Co-authored-by: mtias <[email protected]>
1 parent daa5639 commit 9a26ba8

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

package-lock.json

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/editor/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
"is-plain-object": "^5.0.0",
7676
"memize": "^2.1.0",
7777
"react-autosize-textarea": "^7.1.0",
78-
"remove-accents": "^0.5.0"
78+
"remove-accents": "^0.5.0",
79+
"uuid": "^9.0.1"
7980
},
8081
"peerDependencies": {
8182
"react": "^18.0.0",

packages/editor/src/utils/media-upload/index.js

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { v4 as uuid } from 'uuid';
5+
16
/**
27
* WordPress dependencies
38
*/
4-
import { select } from '@wordpress/data';
9+
import { select, dispatch } from '@wordpress/data';
510
import { uploadMedia } from '@wordpress/media-utils';
611

712
/**
@@ -32,7 +37,16 @@ export default function mediaUpload( {
3237
onFileChange,
3338
} ) {
3439
const { getCurrentPost, getEditorSettings } = select( editorStore );
40+
const {
41+
lockPostAutosaving,
42+
unlockPostAutosaving,
43+
lockPostSaving,
44+
unlockPostSaving,
45+
} = dispatch( editorStore );
46+
3547
const wpAllowedMimeTypes = getEditorSettings().allowedMimeTypes;
48+
const lockKey = `image-upload-${ uuid() }`;
49+
let imageIsUploading = false;
3650
maxUploadFileSize =
3751
maxUploadFileSize || getEditorSettings().maxUploadFileSize;
3852
const currentPost = getCurrentPost();
@@ -41,18 +55,39 @@ export default function mediaUpload( {
4155
typeof currentPost?.id === 'number'
4256
? currentPost.id
4357
: currentPost?.wp_id;
58+
const setSaveLock = () => {
59+
lockPostSaving( lockKey );
60+
lockPostAutosaving( lockKey );
61+
imageIsUploading = true;
62+
};
63+
4464
const postData = currentPostId ? { post: currentPostId } : {};
65+
const clearSaveLock = () => {
66+
unlockPostSaving( lockKey );
67+
unlockPostAutosaving( lockKey );
68+
imageIsUploading = false;
69+
};
4570

4671
uploadMedia( {
4772
allowedTypes,
4873
filesList,
49-
onFileChange,
74+
onFileChange: ( file ) => {
75+
if ( ! imageIsUploading ) {
76+
setSaveLock();
77+
} else {
78+
clearSaveLock();
79+
}
80+
onFileChange( file );
81+
},
5082
additionalData: {
5183
...postData,
5284
...additionalData,
5385
},
5486
maxUploadFileSize,
55-
onError: ( { message } ) => onError( message ),
87+
onError: ( { message } ) => {
88+
clearSaveLock();
89+
onError( message );
90+
},
5691
wpAllowedMimeTypes,
5792
} );
5893
}

0 commit comments

Comments
 (0)