import React from 'react'; import Immutable from 'immutable'; import propTypes from 'prop-types'; import style from './index.less'; import Button from './button'; import store from '../../store'; import todo from '../../control/todo'; import { i18n, lan } from '../../unit/const'; export default class Keyboard extends React.Component { componentDidMount() { const touchEventCatch = {}; // 对于手机操作, 触发了touchstart, 将作出记录, 不再触发后面的mouse事件 // 在鼠标触发mousedown时, 移除元素时可以不触发mouseup, 这里做一个兼容, 以mouseout模拟mouseup const mouseDownEventCatch = {}; document.addEventListener('touchstart', (e) => { if (e.preventDefault) { e.preventDefault(); } }, true); // 解决issue: https://github.com/chvin/react-tetris/issues/24 document.addEventListener('touchend', (e) => { if (e.preventDefault) { e.preventDefault(); } }, true); // 阻止双指放大 document.addEventListener('gesturestart', (e) => { if (e.preventDefault) { event.preventDefault(); } }); document.addEventListener('mousedown', (e) => { if (e.preventDefault) { e.preventDefault(); } }, true); Object.keys(todo).forEach((key) => { this[`dom_${key}`].dom.addEventListener('mousedown', () => { if (touchEventCatch[key] === true) { return; } todo[key].down(store); mouseDownEventCatch[key] = true; }, true); this[`dom_${key}`].dom.addEventListener('mouseup', () => { if (touchEventCatch[key] === true) { touchEventCatch[key] = false; return; } todo[key].up(store); mouseDownEventCatch[key] = false; }, true); this[`dom_${key}`].dom.addEventListener('mouseout', () => { if (mouseDownEventCatch[key] === true) { todo[key].up(store); } }, true); this[`dom_${key}`].dom.addEventListener('touchstart', () => { touchEventCatch[key] = true; todo[key].down(store); }, true); this[`dom_${key}`].dom.addEventListener('touchend', () => { todo[key].up(store); }, true); }); } shouldComponentUpdate({ keyboard, filling }) { return !Immutable.is(keyboard, this.props.keyboard) || filling !== this.props.filling; } render() { const keyboard = this.props.keyboard; return (
); } } Keyboard.propTypes = { filling: propTypes.number.isRequired, keyboard: propTypes.object.isRequired, };