import * as React from 'react' import * as ReactDOM from 'react-dom' import { IDebugInfo } from 'dob' import { Props, State } from './action.type' import * as S from './action.style' import Highlight from './highlight' const reg = (input: string) => { return new RegExp(input, 'g'); }; export class Action extends React.PureComponent{ static defaultProps = new Props() public state = new State() public componentDidMount() { if (this.props.dyDebug) { this.props.dyDebug.event.on('toggleActionDetail', this.handleToggleActionDetail) this.props.dyDebug.event.on('focusActionDetail', this.handleFocusActionDetail) this.props.dyDebug.event.on('unFocusActionDetail', this.handleUnFocusActionDetail) } } public componentWillUnmount() { if (this.props.dyDebug) { this.props.dyDebug.event.off('toggleActionDetail', this.handleToggleActionDetail) this.props.dyDebug.event.off('focusActionDetail', this.handleFocusActionDetail) this.props.dyDebug.event.off('unFocusActionDetail', this.handleUnFocusActionDetail) } } public render() { if (this.props.deep === 0 && this.props.searchValue !== "" && !reg(this.props.searchValue).test(this.props.debugInfo.name)) { return null } return ( { if (this.props.dyDebug) { this.props.dyDebug.event.emit('focusDebug', this.props.debugInfo.id) } }} onMouseLeave={() => { if (this.props.dyDebug) { this.props.dyDebug.event.emit('unFocusDebug', this.props.debugInfo.id) } }} > {this.renderTitle()} {this.state.showDetail && {this.renderDetails(this.props.debugInfo, this.props.deep, this.props.renderKey)} } ) } private renderTitle() { if (this.props.debugInfo.name === null) { return ( Anonymous ) } if (this.props.deep === 0) { return ( ) } return this.props.debugInfo.name } private handleToggleActionDetail = (debugId: number) => { if (debugId !== this.props.debugInfo.id) { return } this.toggleDetail() } private handleFocusActionDetail = (debugId: number) => { if (debugId !== this.props.debugInfo.id) { return } this.setState({ isHighlight: true }) } private handleUnFocusActionDetail = (debugId: number) => { if (debugId !== this.props.debugInfo.id) { return } this.setState({ isHighlight: false }) } private toggleDetail = () => { this.setState(state => { return { showDetail: !this.state.showDetail } }) } /** * 渲染详情 */ private renderDetails = (action: IDebugInfo, deep: number, key: string) => { return action.changeList.map((change, index) => { const newKey = key + '.' + index switch (change.type) { case 'change': const newValue = JSON.stringify(change.value, null, 2) const oldValue = JSON.stringify(change.oldValue, null, 2) return ( {newValue} {oldValue} {change.callStack.join('.')}. {change.key} ) case 'delete': return ( {change.callStack.join('.')}. {change.key} ) case 'action': const renderKey = change.action.id + '.' + index return ( ) default: return null } }) } }