Initial commit of 001code-html Scratch frontend project.
Includes scratch-gui, scratch-vm, scratch-blocks, scratch-render, scratch-l10n, and deployment config. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
48
scratch-gui/src/lib/font-loader-hoc.jsx
Normal file
48
scratch-gui/src/lib/font-loader-hoc.jsx
Normal file
@@ -0,0 +1,48 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import omit from 'lodash.omit';
|
||||
import {connect} from 'react-redux';
|
||||
import {setFontsLoaded} from '../reducers/fonts-loaded';
|
||||
import {loadFonts} from 'scratch-render-fonts';
|
||||
|
||||
/* Higher Order Component to provide behavior for loading fonts.
|
||||
* @param {React.Component} WrappedComponent component to receive fontsLoaded prop
|
||||
* @returns {React.Component} component with font loading behavior
|
||||
*/
|
||||
const FontLoaderHOC = function (WrappedComponent) {
|
||||
class FontLoaderComponent extends React.Component {
|
||||
componentDidMount () {
|
||||
if (this.props.fontsLoaded) return;
|
||||
|
||||
loadFonts().then(() => this.props.onSetFontsLoaded());
|
||||
}
|
||||
render () {
|
||||
const componentProps = omit(this.props, ['onSetFontsLoaded']);
|
||||
return (
|
||||
<WrappedComponent
|
||||
{...componentProps}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
FontLoaderComponent.propTypes = {
|
||||
fontsLoaded: PropTypes.bool.isRequired,
|
||||
onSetFontsLoaded: PropTypes.func.isRequired
|
||||
};
|
||||
const mapStateToProps = state => ({
|
||||
fontsLoaded: state.scratchGui.fontsLoaded
|
||||
});
|
||||
const mapDispatchToProps = dispatch => ({
|
||||
onSetFontsLoaded: () => dispatch(setFontsLoaded())
|
||||
});
|
||||
return connect(
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
)(FontLoaderComponent);
|
||||
};
|
||||
|
||||
export {
|
||||
FontLoaderHOC as default
|
||||
};
|
||||
Reference in New Issue
Block a user