1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { v4 as uuid } from 'uuid' ;
5
+
1
6
/**
2
7
* WordPress dependencies
3
8
*/
4
- import { select } from '@wordpress/data' ;
9
+ import { select , dispatch } from '@wordpress/data' ;
5
10
import { uploadMedia } from '@wordpress/media-utils' ;
6
11
7
12
/**
@@ -32,7 +37,16 @@ export default function mediaUpload( {
32
37
onFileChange,
33
38
} ) {
34
39
const { getCurrentPost, getEditorSettings } = select ( editorStore ) ;
40
+ const {
41
+ lockPostAutosaving,
42
+ unlockPostAutosaving,
43
+ lockPostSaving,
44
+ unlockPostSaving,
45
+ } = dispatch ( editorStore ) ;
46
+
35
47
const wpAllowedMimeTypes = getEditorSettings ( ) . allowedMimeTypes ;
48
+ const lockKey = `image-upload-${ uuid ( ) } ` ;
49
+ let imageIsUploading = false ;
36
50
maxUploadFileSize =
37
51
maxUploadFileSize || getEditorSettings ( ) . maxUploadFileSize ;
38
52
const currentPost = getCurrentPost ( ) ;
@@ -41,18 +55,39 @@ export default function mediaUpload( {
41
55
typeof currentPost ?. id === 'number'
42
56
? currentPost . id
43
57
: currentPost ?. wp_id ;
58
+ const setSaveLock = ( ) => {
59
+ lockPostSaving ( lockKey ) ;
60
+ lockPostAutosaving ( lockKey ) ;
61
+ imageIsUploading = true ;
62
+ } ;
63
+
44
64
const postData = currentPostId ? { post : currentPostId } : { } ;
65
+ const clearSaveLock = ( ) => {
66
+ unlockPostSaving ( lockKey ) ;
67
+ unlockPostAutosaving ( lockKey ) ;
68
+ imageIsUploading = false ;
69
+ } ;
45
70
46
71
uploadMedia ( {
47
72
allowedTypes,
48
73
filesList,
49
- onFileChange,
74
+ onFileChange : ( file ) => {
75
+ if ( ! imageIsUploading ) {
76
+ setSaveLock ( ) ;
77
+ } else {
78
+ clearSaveLock ( ) ;
79
+ }
80
+ onFileChange ( file ) ;
81
+ } ,
50
82
additionalData : {
51
83
...postData ,
52
84
...additionalData ,
53
85
} ,
54
86
maxUploadFileSize,
55
- onError : ( { message } ) => onError ( message ) ,
87
+ onError : ( { message } ) => {
88
+ clearSaveLock ( ) ;
89
+ onError ( message ) ;
90
+ } ,
56
91
wpAllowedMimeTypes,
57
92
} ) ;
58
93
}
0 commit comments