@@ -11,13 +11,13 @@ import {
1111} from '../reducers/tw' ;
1212import {
1313 defaultProjectId ,
14- setProjectId
14+ setProjectId ,
15+ remixProject
1516} from '../reducers/project-state' ;
1617import {
1718 setPlayer ,
1819 setFullScreen
1920} from '../reducers/mode' ;
20- import { generateRandomUsername } from './tw-username' ;
2121import { setSearchParams } from './tw-navigation-utils' ;
2222import { defaultStageSize } from '../reducers/custom-stage-size' ;
2323
@@ -50,28 +50,26 @@ const setLocalStorage = (key, value) => {
5050 }
5151} ;
5252
53- const getLocalStorage = key => {
53+
54+ const readHashProjectId = ( ) => {
55+ if ( location . pathname === '/projects/editor' ) {
56+ return '0' ;
57+ }
5458 try {
55- return localStorage . getItem ( key ) ;
56- } catch ( e ) {
57- // ignore
59+ return location . pathname . split ( '/projects/' ) [ 1 ] . split ( '/editor' ) [ 0 ] . split ( '/fullscreen' ) [ 0 ] . replaceAll ( '/' , '' ) ;
60+ } catch ( e ) {
61+ const match = location . hash . match ( / # ( \d + ) / ) ;
62+ return match === null ? null : match [ 1 ] ;
5863 }
59- return null ;
64+
6065} ;
6166
62- const readHashProjectId = ( ) => {
63- if ( location . pathname == '/projects/editor' ) {
64- return '0'
65- } else {
66- try {
67- return location . pathname . split ( '/projects/' ) [ 1 ] . split ( '/editor' ) [ 0 ] . split ( '/fullscreen' ) [ 0 ] . replaceAll ( '/' , '' ) ;
68- } catch ( e ) {
69- const match = location . hash . match ( / # ( \d + ) / ) ;
70- return match === null ? null : match [ 1 ] ;
71- }
72- }
67+ const shouldRemix = ( ) => {
68+ const urlParams = new URLSearchParams ( location . search ) ;
69+ return urlParams . has ( 'triggerRemix' ) ;
7370} ;
7471
72+
7573class Router {
7674 constructor ( { onSetProjectId, onSetIsPlayerOnly, onSetIsFullScreen} ) {
7775 this . onSetProjectId = onSetProjectId ;
@@ -366,6 +364,18 @@ const TWStateManager = function (WrappedComponent) {
366364 window . addEventListener ( 'popstate' , this . handlePopState ) ;
367365 }
368366 componentDidUpdate ( prevProps ) {
367+ // eslint-disable-next-line max-len
368+ if ( this . props . projectState . loadingState === 'SHOWING_WITH_ID' && prevProps . projectState . loadingState === 'SHOWING_WITH_ID' ) {
369+ if ( shouldRemix ( ) ) {
370+ // remove triggerRemix from URL
371+ const searchParams = new URLSearchParams ( location . search ) ;
372+ searchParams . delete ( 'triggerRemix' ) ;
373+ setSearchParams ( searchParams ) ;
374+
375+ this . props . handleRemix ( ) ;
376+ }
377+ }
378+
369379 if ( this . props . username !== prevProps . username && this . props . username !== this . doNotPersistUsername ) {
370380 // TODO: this always restores the current username once at startup, which is unnecessary
371381 setLocalStorage ( USERNAME_KEY , this . props . username ) ;
@@ -562,6 +572,10 @@ const TWStateManager = function (WrappedComponent) {
562572 reduxProjectId : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
563573 routingStyle : PropTypes . oneOf ( Object . keys ( routers ) ) ,
564574 username : PropTypes . string ,
575+ projectState : PropTypes . shape ( {
576+ loadingState : PropTypes . string
577+ } ) ,
578+ handleRemix : PropTypes . func ,
565579 vm : PropTypes . instanceOf ( VM )
566580 } ;
567581 StateManagerComponent . defaultProps = {
@@ -581,13 +595,15 @@ const TWStateManager = function (WrappedComponent) {
581595 interpolation : state . scratchGui . tw . interpolation ,
582596 turbo : state . scratchGui . vmStatus . turbo ,
583597 username : state . scratchGui . tw . username ,
598+ projectState : state . scratchGui . projectState ,
584599 vm : state . scratchGui . vm
585600 } ) ;
586601 const mapDispatchToProps = dispatch => ( {
587602 onSetIsFullScreen : isFullScreen => dispatch ( setFullScreen ( isFullScreen ) ) ,
588603 onSetIsPlayerOnly : isPlayerOnly => dispatch ( setPlayer ( isPlayerOnly ) ) ,
589604 onSetProjectId : projectId => dispatch ( setProjectId ( projectId ) ) ,
590- onSetUsername : username => dispatch ( setUsername ( username ) )
605+ onSetUsername : username => dispatch ( setUsername ( username ) ) ,
606+ handleRemix : ( ) => dispatch ( remixProject ( ) )
591607 } ) ;
592608 return injectIntl ( connect (
593609 mapStateToProps ,
0 commit comments