Skip to content

Commit 6342045

Browse files
committed
readding support for ESlint + added triggerRemix URL param support
1 parent 0ba2319 commit 6342045

File tree

5 files changed

+63
-56
lines changed

5 files changed

+63
-56
lines changed

src/lib/tw-project-meta-fetcher-hoc.jsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ import {connect} from 'react-redux';
44
import log from './log';
55

66

7+
// eslint-disable-next-line import/no-commonjs
78
const {API_HOST} = require('./brand');
89

910
import {setProjectTitle} from '../reducers/project-title';
10-
import {setAuthor, setDescription,setUsername} from '../reducers/tw';
11+
import {setAuthor, setDescription, setUsername} from '../reducers/tw';
1112

1213
import storage from './storage';
1314

1415
export const fetchProjectMeta = async projectId => {
1516
const urls = [
16-
API_HOST+`/projectsMETA?id=${projectId}&token=${storage.getProjectToken()}`,
17-
API_HOST+`/projectsMETA?id=${projectId}&token=${storage.getProjectToken()}`
17+
`${API_HOST}/projectsMETA?id=${projectId}&token=${storage.getProjectToken()}`,
18+
`${API_HOST}/projectsMETA?id=${projectId}&token=${storage.getProjectToken()}`
1819
];
1920
let firstError;
2021
for (const url of urls) {
@@ -54,7 +55,7 @@ const setIndexable = indexable => {
5455

5556
const TWProjectMetaFetcherHOC = function (WrappedComponent) {
5657
class ProjectMetaFetcherComponent extends React.Component {
57-
constructor(props) {
58+
constructor (props) {
5859
super(props);
5960
this.state = {
6061
canSave: false,
@@ -85,7 +86,7 @@ const TWProjectMetaFetcherHOC = function (WrappedComponent) {
8586
const username = data.username;
8687
if (username) {
8788
this.props.onSetUsername(username);
88-
this.setState({ canUseCloud: true });
89+
this.setState({canUseCloud: true});
8990
}
9091
const authorName = data.author.username;
9192
const authorThumbnail = `https://trampoline.turbowarp.org/avatars/${data.author.id}`;
@@ -96,8 +97,8 @@ const TWProjectMetaFetcherHOC = function (WrappedComponent) {
9697
this.props.onSetDescription(instructions, credits);
9798
}
9899

99-
this.setState({ canSave: data.canSave == "true" });
100-
this.setState({ canRemix: data.canRemix == "true" });
100+
this.setState({canSave: data.canSave === 'true'});
101+
this.setState({canRemix: data.canRemix === 'true'});
101102
setIndexable(true);
102103
})
103104
.catch(err => {
@@ -118,7 +119,7 @@ const TWProjectMetaFetcherHOC = function (WrappedComponent) {
118119
onSetDescription,
119120
onSetProjectTitle,
120121
onSetUsername,
121-
/* eslint-enable no-unused-vars */
122+
/* eslint-enable no-unused-vars */
122123
...props
123124
} = this.props;
124125
return (
@@ -135,7 +136,8 @@ const TWProjectMetaFetcherHOC = function (WrappedComponent) {
135136
reduxProjectId: PropTypes.string,
136137
onSetAuthor: PropTypes.func,
137138
onSetDescription: PropTypes.func,
138-
onSetProjectTitle: PropTypes.func
139+
onSetProjectTitle: PropTypes.func,
140+
onSetUsername: PropTypes.func
139141
};
140142
const mapStateToProps = state => ({
141143
reduxProjectId: state.scratchGui.projectState.projectId

src/lib/tw-state-manager-hoc.jsx

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import {
1111
} from '../reducers/tw';
1212
import {
1313
defaultProjectId,
14-
setProjectId
14+
setProjectId,
15+
remixProject
1516
} from '../reducers/project-state';
1617
import {
1718
setPlayer,
1819
setFullScreen
1920
} from '../reducers/mode';
20-
import {generateRandomUsername} from './tw-username';
2121
import {setSearchParams} from './tw-navigation-utils';
2222
import {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+
7573
class 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,

src/playground/render-gui.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ const restrictedMode = searchParams.get('onlyEditor') === '1';
1111

1212
const RenderGUI = props => (
1313
<GUI
14-
cloudHost={cloudHost}
14+
cloudHost={cloudHost}
1515
hasCloudPermission
1616
canCreateNew={creatingNewProject}
1717
isShared={!restrictedMode}
18-
canShare={false} //just do it from project page
19-
basePath={process.env.ROOT}
20-
canEditTitle={false} //just do it from project page
18+
canShare={false} // just do it from project page
19+
basePath={process.env.ROOT}
20+
canEditTitle={false} // just do it from project page
2121
enableCommunity={!restrictedMode}
2222
{...props}
2323
/>

src/playground/render-interface.jsx

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,16 @@ import PropTypes from 'prop-types';
1919
import React from 'react';
2020
import {connect} from 'react-redux';
2121
import {compose} from 'redux';
22-
import {FormattedMessage, defineMessages, injectIntl, intlShape} from 'react-intl';
22+
import {defineMessages, injectIntl, intlShape} from 'react-intl';
2323
import {getIsLoading} from '../reducers/project-state.js';
2424
import AppStateHOC from '../lib/app-state-hoc.jsx';
2525
import ErrorBoundaryHOC from '../lib/error-boundary-hoc.jsx';
2626
import TWProjectMetaFetcherHOC from '../lib/tw-project-meta-fetcher-hoc.jsx';
2727
import TWStateManagerHOC from '../lib/tw-state-manager-hoc.jsx';
28-
import SBFileUploaderHOC from '../lib/sb-file-uploader-hoc.jsx';
2928
import TWPackagerIntegrationHOC from '../lib/tw-packager-integration-hoc.jsx';
3029
import SettingsStore from '../addons/settings-store-singleton';
3130
import '../lib/tw-fix-history-api';
3231
import GUI from './render-gui.jsx';
33-
import MenuBar from '../components/menu-bar/menu-bar.jsx';
34-
import ProjectInput from '../components/tw-project-input/project-input.jsx';
35-
import FeaturedProjects from '../components/tw-featured-projects/featured-projects.jsx';
36-
import Description from '../components/tw-description/description.jsx';
37-
import BrowserModal from '../components/browser-modal/browser-modal.jsx';
38-
import CloudVariableBadge from '../containers/tw-cloud-variable-badge.jsx';
39-
import {isBrowserSupported} from '../lib/tw-environment-support-prober';
4032
import AddonChannels from '../addons/channels';
4133
import {loadServiceWorker} from './load-service-worker';
4234
import runAddons from '../addons/entry';
@@ -45,14 +37,14 @@ import {APP_NAME} from '../lib/brand.js';
4537

4638
import styles from './interface.css';
4739

48-
const isInvalidEmbed = window.parent == window;
40+
const isInvalidEmbed = window.parent === window;
4941

50-
const handleClickAddonSettings = addonId => {
42+
const handleClickAddonSettings = () => {
5143
// addonId might be a string of the addon to focus on, undefined, or an event (treat like undefined)
52-
/*const path = process.env.ROUTING_STYLE === 'wildcard' ? 'addons' : 'addons.html';
44+
/* const path = process.env.ROUTING_STYLE === 'wildcard' ? 'addons' : 'addons.html';
5345
const url = `${process.env.ROOT}${path}${typeof addonId === 'string' ? `#${addonId}` : ''}`;
54-
window.open(url);*/
55-
window.parent.postMessage({ type: "block-compiler-action", action: "addonsPage"}, "*");
46+
window.open(url); */
47+
window.parent.postMessage({type: 'block-compiler-action', action: 'addonsPage'}, '*');
5648
};
5749

5850
const messages = defineMessages({
@@ -63,15 +55,11 @@ const messages = defineMessages({
6355
}
6456
});
6557

66-
const WrappedMenuBar = compose(
67-
SBFileUploaderHOC,
68-
TWPackagerIntegrationHOC
69-
)(MenuBar);
70-
7158
if (AddonChannels.reloadChannel) {
7259
AddonChannels.reloadChannel.addEventListener('message', () => {
60+
// eslint-disable-next-line no-negated-condition
7361
if (window.top !== window.self) { // if in iframe
74-
window.parent.postMessage({ type: "block-compiler-action", action: "reload"}, "*");
62+
window.parent.postMessage({type: 'block-compiler-action', action: 'reload'}, '*');
7563
} else {
7664
location.reload();
7765
}

src/reducers/project-state.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,14 @@ const createProject = () => ({
374374
});
375375

376376
const doneCreatingProject = (id, loadingState) => {
377-
window.parent.postMessage({ type: "block-compiler-action", action: "doneCreatingProject", projectID: id}, "*");
377+
window.parent.postMessage({type: 'block-compiler-action', action: 'doneCreatingProject', projectID: id}, '*');
378378

379379
const searchParams = new URLSearchParams(location.search);
380380
if (searchParams.has('new_project')) searchParams.delete('new_project');
381-
if(searchParams.has('username')) searchParams.delete('username');
382-
if(searchParams.has('token')) searchParams.delete('token');
383-
const newUrl = `${location.pathname}${searchParams.toString() ? '?' + searchParams.toString() : ''}${location.hash}`;
381+
if (searchParams.has('username')) searchParams.delete('username');
382+
if (searchParams.has('token')) searchParams.delete('token');
383+
// eslint-disable-next-line max-len
384+
const newUrl = `${location.pathname}${searchParams.toString() ? `?${searchParams.toString()}` : ''}${location.hash}`;
384385
window.history.replaceState({}, document.title, newUrl);
385386

386387
switch (loadingState) {

0 commit comments

Comments
 (0)