From 62d0ce3adff80aee7a7e823db5afd5c2fece9e7b Mon Sep 17 00:00:00 2001 From: David Leoni Date: Fri, 14 Aug 2020 09:36:03 +0200 Subject: [PATCH] debugging-tools #15: added BotchLifeTreeItem --- src/components/botch/botch-life-tree-item.jsx | 174 ++++++++++++++++++ src/components/botch/botch-life-tree.jsx | 11 +- src/containers/botch-debug-tab.jsx | 4 +- 3 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 src/components/botch/botch-life-tree-item.jsx diff --git a/src/components/botch/botch-life-tree-item.jsx b/src/components/botch/botch-life-tree-item.jsx new file mode 100644 index 00000000000..56650ebe21b --- /dev/null +++ b/src/components/botch/botch-life-tree-item.jsx @@ -0,0 +1,174 @@ +import bindAll from 'lodash.bindall'; +import PropTypes from 'prop-types'; +import React from 'react'; +import {injectIntl} from 'react-intl'; + +import LibraryItemComponent from '../../components/library-item/library-item.jsx'; + +class BotchLifeTreeItem extends React.PureComponent { + constructor (props) { + super(props); + bindAll(this, [ + 'handleBlur', + 'handleClick', + 'handleFocus', + 'handleKeyPress', + 'handleMouseEnter', + 'handleMouseLeave', + 'handlePlay', + 'handleStop', + 'rotateIcon', + 'startRotatingIcons', + 'stopRotatingIcons' + ]); + this.state = { + iconIndex: 0, + isRotatingIcon: false + }; + } + componentWillUnmount () { + clearInterval(this.intervalId); + } + handleBlur (id) { + this.handleMouseLeave(id); + } + handleClick (e) { + if (!this.props.disabled) { + this.props.onSelect(this.props.id); + } + e.preventDefault(); + } + handleFocus (id) { + if (!this.props.showPlayButton) { + this.handleMouseEnter(id); + } + } + handleKeyPress (e) { + if (e.key === ' ' || e.key === 'Enter') { + e.preventDefault(); + this.props.onSelect(this.props.id); + } + } + handleMouseEnter () { + // only show hover effects on the item if not showing a play button + if (!this.props.showPlayButton) { + this.props.onMouseEnter(this.props.id); + if (this.props.icons && this.props.icons.length) { + this.stopRotatingIcons(); + this.setState({ + isRotatingIcon: true + }, this.startRotatingIcons); + } + } + } + handleMouseLeave () { + // only show hover effects on the item if not showing a play button + if (!this.props.showPlayButton) { + this.props.onMouseLeave(this.props.id); + if (this.props.icons && this.props.icons.length) { + this.setState({ + isRotatingIcon: false + }, this.stopRotatingIcons); + } + } + } + handlePlay () { + this.props.onMouseEnter(this.props.id); + } + handleStop () { + this.props.onMouseLeave(this.props.id); + } + startRotatingIcons () { + this.rotateIcon(); + this.intervalId = setInterval(this.rotateIcon, 300); + } + stopRotatingIcons () { + if (this.intervalId) { + this.intervalId = clearInterval(this.intervalId); + } + } + rotateIcon () { + const nextIconIndex = (this.state.iconIndex + 1) % this.props.icons.length; + this.setState({iconIndex: nextIconIndex}); + } + curIconMd5 () { + const iconMd5Prop = this.props.iconMd5; + if (this.props.icons && + this.state.isRotatingIcon && + this.state.iconIndex < this.props.icons.length) { + const icon = this.props.icons[this.state.iconIndex] || {}; + return icon.md5ext || // 3.0 library format + icon.baseLayerMD5 || // 2.0 library format, TODO GH-5084 + iconMd5Prop; + } + return iconMd5Prop; + } + render () { + const iconMd5 = this.curIconMd5(); + const iconURL = iconMd5 ? + `https://cdn.assets.scratch.mit.edu/internalapi/asset/${iconMd5}/get/` : + this.props.iconRawURL; + return ( +