import classNames from 'classnames'; import {connect} from 'react-redux'; import {compose} from 'redux'; import {defineMessages, FormattedMessage, injectIntl, intlShape} from 'react-intl'; import PropTypes from 'prop-types'; import bindAll from 'lodash.bindall'; import bowser from 'bowser'; import React from 'react'; import VM from 'scratch-vm'; import Box from '../box/box.jsx'; import Button from '../button/button.jsx'; import CommunityButton from './community-button.jsx'; import ShareButton from './share-button.jsx'; import {ComingSoonTooltip} from '../coming-soon/coming-soon.jsx'; import Divider from '../divider/divider.jsx'; import SaveStatus from './save-status.jsx'; import ProjectWatcher from '../../containers/project-watcher.jsx'; import MenuBarMenu from './menu-bar-menu.jsx'; import {MenuItem, MenuSection} from '../menu/menu.jsx'; import ProjectTitleInput from './project-title-input.jsx'; import AuthorInfo from './author-info.jsx'; import AccountNav from '../../containers/account-nav.jsx'; import LoginDropdown from './login-dropdown.jsx'; import SB3Downloader from '../../containers/sb3-downloader.jsx'; import DeletionRestorer from '../../containers/deletion-restorer.jsx'; import TurboMode from '../../containers/turbo-mode.jsx'; import MenuBarHOC from '../../containers/menu-bar-hoc.jsx'; import SettingsMenu from './settings-menu.jsx'; import {openTipsLibrary, openDebugModal} from '../../reducers/modals'; import {setPlayer} from '../../reducers/mode'; import { isTimeTravel220022BC, isTimeTravel1920, isTimeTravel1990, isTimeTravel2020, isTimeTravelNow, setTimeTravel } from '../../reducers/time-travel'; import { autoUpdateProject, getIsUpdating, getIsShowingProject, manualUpdateProject, requestNewProject, remixProject, saveProjectAsCopy } from '../../reducers/project-state'; import { openAboutMenu, closeAboutMenu, aboutMenuOpen, openAccountMenu, closeAccountMenu, accountMenuOpen, openFileMenu, closeFileMenu, fileMenuOpen, openEditMenu, closeEditMenu, editMenuOpen, openLoginMenu, closeLoginMenu, loginMenuOpen, openModeMenu, closeModeMenu, modeMenuOpen, settingsMenuOpen, openSettingsMenu, closeSettingsMenu } from '../../reducers/menus'; import collectMetadata from '../../lib/collect-metadata'; import styles from './menu-bar.css'; import helpIcon from '../../lib/assets/icon--tutorials.svg'; import mystuffIcon from './icon--mystuff.png'; import profileIcon from './icon--profile.png'; import remixIcon from './icon--remix.svg'; import dropdownCaret from './dropdown-caret.svg'; import aboutIcon from './icon--about.svg'; import fileIcon from './icon--file.svg'; import editIcon from './icon--edit.svg'; import debugIcon from '../debug-modal/icons/icon--debug.svg'; import scratchLogo from './scratch-logo.svg'; import ninetiesLogo from './nineties_logo.svg'; import catLogo from './cat_logo.svg'; import prehistoricLogo from './prehistoric-logo.svg'; import oldtimeyLogo from './oldtimey-logo.svg'; import sharedMessages from '../../lib/shared-messages'; const ariaMessages = defineMessages({ tutorials: { id: 'gui.menuBar.tutorialsLibrary', defaultMessage: 'Tutorials', description: 'accessibility text for the tutorials button' }, debug: { id: 'gui.menuBar.debug', defaultMessage: 'Debug', description: 'accessibility text for the debug button' } }); const MenuBarItemTooltip = ({ children, className, enable, id, place = 'bottom' }) => { if (enable) { return ( {children} ); } return ( {children} ); }; MenuBarItemTooltip.propTypes = { children: PropTypes.node, className: PropTypes.string, enable: PropTypes.bool, id: PropTypes.string, place: PropTypes.oneOf(['top', 'bottom', 'left', 'right']) }; const MenuItemTooltip = ({id, isRtl, children, className}) => ( {children} ); MenuItemTooltip.propTypes = { children: PropTypes.node, className: PropTypes.string, id: PropTypes.string, isRtl: PropTypes.bool }; const AboutButton = props => ( ); // Show the About button only if we have a handler for it (like in the desktop app) const aboutButton = this.buildAboutMenu(this.props.onClickAbout); return (
Scratch
{(this.props.canChangeTheme || this.props.canChangeLanguage) && ()} {(this.props.canManageFiles) && (
{newProjectMessage} {(this.props.canSave || this.props.canCreateCopy || this.props.canRemix) && ( {this.props.canSave && ( {saveNowMessage} )} {this.props.canCreateCopy && ( {createCopyMessage} )} {this.props.canRemix && ( {remixMessage} )} )} {this.props.intl.formatMessage(sharedMessages.loadFromComputerTitle)} {(className, downloadProjectCallback) => ( )}
)}
{(handleRestore, {restorable, deletedItem}) => ( {this.restoreOptionMessage(deletedItem)} )} {(toggleTurboMode, {turboMode}) => ( {turboMode ? ( ) : ( )} )}
{this.props.isTotallyNormal && (
{'✓'} {' '} {'✓'} {' '}
)}
{this.props.canEditTitle ? (
) : ((this.props.authorUsername && this.props.authorUsername !== this.props.username) ? ( ) : null)}
{this.props.canShare ? ( (this.props.isShowingProject || this.props.isUpdating) && ( { waitForUpdate => ( { this.handleClickShare(waitForUpdate); }} /* eslint-enable react/jsx-no-bind */ /> ) } ) ) : ( this.props.showComingSoon ? ( ) : [] )} {this.props.canRemix ? remixButton : []}
{this.props.enableCommunity ? ( (this.props.isShowingProject || this.props.isUpdating) && ( { waitForUpdate => ( { this.handleClickSeeCommunity(waitForUpdate); }} /* eslint-enable react/jsx-no-bind */ /> ) } ) ) : (this.props.showComingSoon ? ( ) : [])}
{/* show the proper UI in the account menu, given whether the user is logged in, and whether a session is available to log in with */}
{this.props.canSave && ( )}
{this.props.sessionExists ? ( this.props.username ? ( // ************ user is logged in ************
) : ( // ********* user not logged in, but a session exists // ********* so they can choose to log in
) ) : ( // ******** no login session is available, so don't show login stuff {this.props.showComingSoon ? (
{'scratch-cat'}
) : []}
)}
{aboutButton}
); } } MenuBar.propTypes = { aboutMenuOpen: PropTypes.bool, accountMenuOpen: PropTypes.bool, authorId: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), authorThumbnailUrl: PropTypes.string, authorUsername: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), autoUpdateProject: PropTypes.func, canChangeLanguage: PropTypes.bool, canChangeTheme: PropTypes.bool, canCreateCopy: PropTypes.bool, canCreateNew: PropTypes.bool, canEditTitle: PropTypes.bool, canManageFiles: PropTypes.bool, canRemix: PropTypes.bool, canSave: PropTypes.bool, canShare: PropTypes.bool, className: PropTypes.string, confirmReadyToReplaceProject: PropTypes.func, currentLocale: PropTypes.string.isRequired, editMenuOpen: PropTypes.bool, enableCommunity: PropTypes.bool, fileMenuOpen: PropTypes.bool, intl: intlShape, isRtl: PropTypes.bool, isShared: PropTypes.bool, isShowingProject: PropTypes.bool, isTotallyNormal: PropTypes.bool, isUpdating: PropTypes.bool, locale: PropTypes.string.isRequired, loginMenuOpen: PropTypes.bool, logo: PropTypes.string, mode1920: PropTypes.bool, mode1990: PropTypes.bool, mode2020: PropTypes.bool, mode220022BC: PropTypes.bool, modeMenuOpen: PropTypes.bool, modeNow: PropTypes.bool, onClickAbout: PropTypes.oneOfType([ PropTypes.func, // button mode: call this callback when the About button is clicked PropTypes.arrayOf( // menu mode: list of items in the About menu PropTypes.shape({ title: PropTypes.string, // text for the menu item onClick: PropTypes.func // call this callback when the menu item is clicked }) ) ]), onClickAccount: PropTypes.func, onClickEdit: PropTypes.func, onClickFile: PropTypes.func, onClickLogin: PropTypes.func, onClickLogo: PropTypes.func, onClickMode: PropTypes.func, onClickNew: PropTypes.func, onClickRemix: PropTypes.func, onClickSave: PropTypes.func, onClickSaveAsCopy: PropTypes.func, onClickSettings: PropTypes.func, onLogOut: PropTypes.func, onOpenRegistration: PropTypes.func, onOpenTipLibrary: PropTypes.func, onOpenDebugModal: PropTypes.func, onProjectTelemetryEvent: PropTypes.func, onRequestCloseAbout: PropTypes.func, onRequestCloseAccount: PropTypes.func, onRequestCloseEdit: PropTypes.func, onRequestCloseFile: PropTypes.func, onRequestCloseLogin: PropTypes.func, onRequestCloseMode: PropTypes.func, onRequestCloseSettings: PropTypes.func, onRequestOpenAbout: PropTypes.func, onSeeCommunity: PropTypes.func, onSetTimeTravelMode: PropTypes.func, onShare: PropTypes.func, onStartSelectingFileUpload: PropTypes.func, onToggleLoginOpen: PropTypes.func, projectTitle: PropTypes.string, renderLogin: PropTypes.func, sessionExists: PropTypes.bool, settingsMenuOpen: PropTypes.bool, shouldSaveBeforeTransition: PropTypes.func, showComingSoon: PropTypes.bool, username: PropTypes.string, userOwnsProject: PropTypes.bool, vm: PropTypes.instanceOf(VM).isRequired }; MenuBar.defaultProps = { logo: scratchLogo, onShare: () => {} }; const mapStateToProps = (state, ownProps) => { const loadingState = state.scratchGui.projectState.loadingState; const user = state.session && state.session.session && state.session.session.user; return { aboutMenuOpen: aboutMenuOpen(state), accountMenuOpen: accountMenuOpen(state), currentLocale: state.locales.locale, fileMenuOpen: fileMenuOpen(state), editMenuOpen: editMenuOpen(state), isRtl: state.locales.isRtl, isUpdating: getIsUpdating(loadingState), isShowingProject: getIsShowingProject(loadingState), locale: state.locales.locale, loginMenuOpen: loginMenuOpen(state), modeMenuOpen: modeMenuOpen(state), projectTitle: state.scratchGui.projectTitle, sessionExists: state.session && typeof state.session.session !== 'undefined', settingsMenuOpen: settingsMenuOpen(state), username: user ? user.username : null, userOwnsProject: ownProps.authorUsername && user && (ownProps.authorUsername === user.username), vm: state.scratchGui.vm, mode220022BC: isTimeTravel220022BC(state), mode1920: isTimeTravel1920(state), mode1990: isTimeTravel1990(state), mode2020: isTimeTravel2020(state), modeNow: isTimeTravelNow(state) }; }; const mapDispatchToProps = dispatch => ({ autoUpdateProject: () => dispatch(autoUpdateProject()), onOpenTipLibrary: () => dispatch(openTipsLibrary()), onOpenDebugModal: () => dispatch(openDebugModal()), onClickAccount: () => dispatch(openAccountMenu()), onRequestCloseAccount: () => dispatch(closeAccountMenu()), onClickFile: () => dispatch(openFileMenu()), onRequestCloseFile: () => dispatch(closeFileMenu()), onClickEdit: () => dispatch(openEditMenu()), onRequestCloseEdit: () => dispatch(closeEditMenu()), onClickLogin: () => dispatch(openLoginMenu()), onRequestCloseLogin: () => dispatch(closeLoginMenu()), onClickMode: () => dispatch(openModeMenu()), onRequestCloseMode: () => dispatch(closeModeMenu()), onRequestOpenAbout: () => dispatch(openAboutMenu()), onRequestCloseAbout: () => dispatch(closeAboutMenu()), onClickSettings: () => dispatch(openSettingsMenu()), onRequestCloseSettings: () => dispatch(closeSettingsMenu()), onClickNew: needSave => dispatch(requestNewProject(needSave)), onClickRemix: () => dispatch(remixProject()), onClickSave: () => dispatch(manualUpdateProject()), onClickSaveAsCopy: () => dispatch(saveProjectAsCopy()), onSeeCommunity: () => dispatch(setPlayer(true)), onSetTimeTravelMode: mode => dispatch(setTimeTravel(mode)) }); export default compose( injectIntl, MenuBarHOC, connect( mapStateToProps, mapDispatchToProps ) )(MenuBar);