@@ -32,6 +32,7 @@ import {
3232 projectError
3333} from '../reducers/project-state' ;
3434import { GUIStoragePropType } from '../gui-config' ;
35+ import { getProjectThumbnail , storeProjectThumbnail } from './store-project-thumbnail' ;
3536
3637/**
3738 * Higher Order Component to provide behavior for saving projects.
@@ -47,7 +48,6 @@ const ProjectSaverHOC = function (WrappedComponent) {
4748 constructor ( props ) {
4849 super ( props ) ;
4950 bindAll ( this , [
50- 'getProjectThumbnail' ,
5151 'leavePageConfirm' ,
5252 'tryToAutoSave'
5353 ] ) ;
@@ -61,7 +61,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
6161 // Allow the GUI consumer to pass in a function to receive a trigger
6262 // for triggering thumbnail or whole project saves.
6363 // These functions are called with null on unmount to prevent stale references.
64- this . props . onSetProjectThumbnailer ( this . getProjectThumbnail ) ;
64+ this . props . onSetProjectThumbnailer ( callback => getProjectThumbnail ( this . props . vm , callback ) ) ;
6565 this . props . onSetProjectSaver ( this . tryToAutoSave ) ;
6666 }
6767 componentDidUpdate ( prevProps ) {
@@ -72,9 +72,21 @@ const ProjectSaverHOC = function (WrappedComponent) {
7272 this . reportTelemetryEvent ( 'projectDidLoad' ) ;
7373 }
7474
75- if ( this . props . saveThumbnailOnLoad && this . props . isShowingWithId &&
76- ! prevProps . isShowingWithId ) {
77- setTimeout ( ( ) => this . storeProjectThumbnail ( this . props . reduxProjectId ) ) ;
75+ if (
76+ ! this . props . manuallySaveThumbnails &&
77+ this . props . onUpdateProjectThumbnail &&
78+ this . props . saveThumbnailOnLoad &&
79+ this . props . isShowingWithId &&
80+ ! prevProps . isShowingWithId
81+ ) {
82+ setTimeout ( ( ) =>
83+ storeProjectThumbnail ( this . props . vm , dataURI => {
84+ this . props . onUpdateProjectThumbnail (
85+ this . props . reduxProjectId ,
86+ dataURItoBlob ( dataURI )
87+ ) ;
88+ } )
89+ ) ;
7890 }
7991
8092 if ( this . props . projectChanged && ! prevProps . projectChanged ) {
@@ -172,7 +184,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
172184 } ) ;
173185 }
174186 createNewProjectToStorage ( ) {
175- return this . storeProject ( null )
187+ return this . storeProject ( null , { } , { isCreatingProject : true } )
176188 . then ( response => {
177189 this . props . onCreatedProject ( response . id . toString ( ) , this . props . loadingState ) ;
178190 } )
@@ -218,8 +230,9 @@ const ProjectSaverHOC = function (WrappedComponent) {
218230 * @param {number|string|undefined } projectId - defined value will PUT/update; undefined/null will POST/create
219231 * @return {Promise } - resolves with json object containing project's existing or new id
220232 * @param {?object } requestParams - object of params to add to request body
233+ * @param {?object } options - additional options for the store operation
221234 */
222- storeProject ( projectId , requestParams ) {
235+ storeProject ( projectId , requestParams , options ) {
223236 requestParams = requestParams || { } ;
224237 this . clearAutoSaveTimeout ( ) ;
225238 // Serialize VM state now before embarking on
@@ -256,8 +269,16 @@ const ProjectSaverHOC = function (WrappedComponent) {
256269 . then ( response => {
257270 this . props . onSetProjectUnchanged ( ) ;
258271 const id = response . id . toString ( ) ;
259- if ( id && this . props . onUpdateProjectThumbnail ) {
260- this . storeProjectThumbnail ( id ) ;
272+ if ( this . props . onUpdateProjectThumbnail && id && (
273+ ! this . props . manuallySaveThumbnails ||
274+ // Always save thumbnail on project creation
275+ options ?. isCreatingProject ) ) {
276+ storeProjectThumbnail ( this . props . vm , dataURI => {
277+ this . props . onUpdateProjectThumbnail (
278+ id ,
279+ dataURItoBlob ( dataURI )
280+ ) ;
281+ } ) ;
261282 }
262283 this . reportTelemetryEvent ( 'projectDidSave' ) ;
263284 return response ;
@@ -268,32 +289,6 @@ const ProjectSaverHOC = function (WrappedComponent) {
268289 } ) ;
269290 }
270291
271- /**
272- * Store a snapshot of the project once it has been saved/created.
273- * Needs to happen _after_ save because the project must have an ID.
274- * @param {!string } projectId - id of the project, must be defined.
275- */
276- storeProjectThumbnail ( projectId ) {
277- try {
278- this . getProjectThumbnail ( dataURI => {
279- this . props . onUpdateProjectThumbnail ( projectId , dataURItoBlob ( dataURI ) ) ;
280- } ) ;
281- } catch ( e ) {
282- log . error ( 'Project thumbnail save error' , e ) ;
283- // This is intentionally fire/forget because a failure
284- // to save the thumbnail is not vitally important to the user.
285- }
286- }
287-
288- getProjectThumbnail ( callback ) {
289- this . props . vm . postIOData ( 'video' , { forceTransparentPreview : true } ) ;
290- this . props . vm . renderer . requestSnapshot ( dataURI => {
291- this . props . vm . postIOData ( 'video' , { forceTransparentPreview : false } ) ;
292- callback ( dataURI ) ;
293- } ) ;
294- this . props . vm . renderer . draw ( ) ;
295- }
296-
297292 /**
298293 * Report a telemetry event.
299294 * @param {string } event - one of `projectWasCreated`, `projectDidLoad`, `projectDidSave`, `projectWasUploaded`
@@ -346,7 +341,6 @@ const ProjectSaverHOC = function (WrappedComponent) {
346341 onShowSavingAlert,
347342 onUpdatedProject,
348343 onUpdateProjectData,
349- onUpdateProjectThumbnail,
350344 noBeforeUnloadHandler,
351345 reduxProjectId,
352346 reduxProjectTitle,
@@ -408,6 +402,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
408402 saveThumbnailOnLoad : PropTypes . bool ,
409403 storage : GUIStoragePropType ,
410404 setAutoSaveTimeoutId : PropTypes . func . isRequired ,
405+ manuallySaveThumbnails : PropTypes . bool ,
411406 vm : PropTypes . instanceOf ( VM ) . isRequired
412407 } ;
413408 ProjectSaverComponent . defaultProps = {
@@ -437,6 +432,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
437432 isManualUpdating : getIsManualUpdating ( loadingState ) ,
438433 loadingState : loadingState ,
439434 locale : state . locales . locale ,
435+ manuallySaveThumbnails : ownProps . manuallySaveThumbnails ?? false ,
440436 onUpdateProjectThumbnail :
441437 ownProps . onUpdateProjectThumbnail ??
442438 storage . saveProjectThumbnail ?. bind ( storage ) ,
0 commit comments