Spaces:
Running
Running
File size: 6,238 Bytes
f2bee8a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
import React from 'react';
import {defineMessages, FormattedMessage, intlShape, injectIntl} from 'react-intl';
import classNames from 'classnames';
import styles from './loader.css';
import PropTypes from 'prop-types';
import bindAll from 'lodash.bindall';
import topBlock from './top-block.svg';
import middleBlock from './middle-block.svg';
import bottomBlock from './bottom-block.svg';
import * as progressMonitor from './tw-progress-monitor';
import isScratchDesktop from '../../lib/isScratchDesktop';
// tw:
// we make some rather large changes here:
// - remove random message, replaced with message dependent on what is actually being loaded
// - add a progress bar
// - bring in intl so that we can translate everything
// The way of doing this is extremely unusual and weird compared to how things are typically done for performance.
// This is because react updates are too performance crippling to handle the progress bar rapidly updating.
const mainMessages = {
'gui.loader.headline': (
<FormattedMessage
defaultMessage="Loading Project"
description="Main loading message"
id="gui.loader.headline"
/>
),
'gui.loader.creating': (
<FormattedMessage
defaultMessage="Creating Project"
description="Main creating message"
id="gui.loader.creating"
/>
),
'gui.loader.playground': (
<FormattedMessage
defaultMessage="Loading Playground"
description="Playground load message"
id="gui.loader.playground"
/>
)
};
const messages = defineMessages({
generic: {
defaultMessage: 'Loading project …',
description: 'Initial generic loading message',
id: 'tw.loader.generic'
},
projectData: {
defaultMessage: 'Downloading project data …',
description: 'Appears when loading project data',
id: 'tw.loader.data'
},
assetsKnown: {
defaultMessage: 'Downloading assets ({complete}/{total}) …',
description: 'Appears when loading project assets and amount of assets is known',
id: 'tw.loader.assets.known'
},
assetsUnknown: {
defaultMessage: 'Downloading assets …',
description: 'Appears when loading project assets but amount of assets is unknown',
id: 'tw.loader.assets.unknown'
}
});
class LoaderComponent extends React.Component {
constructor (props) {
super(props);
this._state = 0;
this.progress = 0;
this.complete = 0;
this.total = 0;
bindAll(this, [
'barInnerRef',
'handleProgressChange',
'messageRef'
]);
}
componentDidMount () {
if (!isScratchDesktop()) {
progressMonitor.setProgressHandler(this.handleProgressChange);
}
this.updateMessage();
}
componentDidUpdate () {
this.update();
}
componentWillUnmount () {
progressMonitor.setProgressHandler(() => {});
}
handleProgressChange (state, progress, complete, total) {
if (state !== this._state) {
this._state = state;
this.updateMessage();
}
this.progress = progress;
this.complete = complete;
this.total = total;
this.update();
}
update () {
if (this.barInner) {
this.barInner.style.width = `${this.progress * 100}%`;
}
if (this._state === 2) {
this.updateMessage();
}
}
updateMessage () {
if (this._state === 0) {
this.message.textContent = this.props.intl.formatMessage(messages.generic);
} else if (this._state === 1) {
this.message.textContent = this.props.intl.formatMessage(messages.projectData);
} else if (this.total > 0) {
this.message.textContent = this.props.intl.formatMessage(messages.assetsKnown, {
complete: this.complete,
total: this.total
});
} else {
this.message.textContent = this.props.intl.formatMessage(messages.assetsUnknown);
}
}
barInnerRef (element) {
this.barInner = element;
}
messageRef (element) {
this.message = element;
}
render () {
return (
<div
className={classNames(styles.background, {
[styles.fullscreen]: this.props.isFullScreen
})}
>
<div className={styles.container}>
<div className={styles.blockAnimation}>
<img
className={styles.topBlock}
src={topBlock}
/>
<img
className={styles.middleBlock}
src={middleBlock}
/>
<img
className={styles.bottomBlock}
src={bottomBlock}
/>
</div>
<div className={styles.title}>
{mainMessages[this.props.messageId]}
</div>
<div className={styles.messageContainerOuter}>
<div
className={styles.messageContainerInner}
ref={this.messageRef}
/>
</div>
{!isScratchDesktop() && (
<div className={styles.twProgressOuter}>
<div
className={styles.twProgressInner}
ref={this.barInnerRef}
/>
</div>
)}
</div>
</div>
);
}
}
LoaderComponent.propTypes = {
isFullScreen: PropTypes.bool,
intl: intlShape.isRequired,
messageId: PropTypes.string
};
LoaderComponent.defaultProps = {
isFullScreen: false,
messageId: 'gui.loader.headline'
};
export default injectIntl(LoaderComponent);
|