BlockActionDelay.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import React from 'react'
  2. import cls from 'classnames'
  3. import i18n from 'i18next'
  4. import { InputNumber } from 'antd'
  5. import onClickOutside from 'react-onclickoutside'
  6. import newEventActions from 'actions/newEvent'
  7. import newEventStores from 'stores/newEvent'
  8. import { getEventNodeById, getEventBlockByBid } from 'stores/funcs/newEvent'
  9. import { isServerRootNode } from 'stores/funcs/event/methodUtils'
  10. import { shallowEqualImmutable } from 'utils/utils'
  11. import iconStrObj from 'src/assets/icons/iconsStr'
  12. class BlockActionDelay extends React.Component {
  13. constructor(props) {
  14. super(props)
  15. this.state = {
  16. showPopUp: false,
  17. delayTime: this.getTime()
  18. }
  19. this.onEventChange = this.onEventChange.bind(this)
  20. this.delayInput = React.createRef()
  21. this.getTime = this.getTime.bind(this)
  22. this.onFocus = this.onFocus.bind(this)
  23. this.onChange = this.onChange.bind(this)
  24. this.showPopUp = this.showPopUp.bind(this)
  25. this.showDelay = this.showDelay.bind(this)
  26. }
  27. componentDidMount() {
  28. this.unsubscribe = newEventStores.listen(this.onEventChange)
  29. }
  30. componentWillUnmount() {
  31. this.unsubscribe()
  32. }
  33. shouldComponentUpdate(nextProps, nextState) {
  34. return (
  35. !shallowEqualImmutable(this.props, nextProps) ||
  36. !shallowEqualImmutable(this.state, nextState)
  37. )
  38. }
  39. componentDidUpdate(preProps) {
  40. if (preProps.bid !== this.props.bid) {
  41. this.setState({
  42. showPopUp: false,
  43. delayTime: this.getTime()
  44. })
  45. }
  46. }
  47. onEventChange(status) {
  48. if (
  49. status.types.includes('updateBlockProp') &&
  50. this.props.bid === status.data.affectBlockId &&
  51. status.data.prop === 'delay'
  52. ) {
  53. this.onChange(this.getTime(), true)
  54. }
  55. }
  56. getTime() {
  57. let nodeId = this.props.eventNodeId || newEventStores.curEventNodeId
  58. let block = getEventBlockByBid(this.props.bid, nodeId)
  59. return block.delay || 0
  60. }
  61. onFocus(e) {
  62. e.target.select()
  63. }
  64. onChange(value, stopUpdateInStore) {
  65. let result = value
  66. if (value === '') {
  67. result = 0
  68. } else if (!isNaN(value)) {
  69. result = parseFloat(value) || 0
  70. } else {
  71. result = this.state.delayTime || 0
  72. }
  73. if (result !== this.state.delayTime) {
  74. this.setState(
  75. {
  76. delayTime: result
  77. },
  78. () => {
  79. if (!stopUpdateInStore) {
  80. let nodeId = this.props.eventNodeId || newEventStores.curEventNodeId
  81. newEventActions.updateBlockProp({
  82. nodeId,
  83. blockId: this.props.bid,
  84. prop: 'delay',
  85. value: result
  86. })
  87. }
  88. this.props.changeDelay && this.props.changeDelay()
  89. }
  90. )
  91. }
  92. }
  93. showPopUp() {
  94. this.setState(
  95. {
  96. showPopUp: !this.state.showPopUp
  97. },
  98. () => {
  99. if (
  100. this.state.showPopUp &&
  101. this.delayInput &&
  102. this.delayInput.current
  103. ) {
  104. this.delayInput.current.inputNumberRef.input.select()
  105. }
  106. }
  107. )
  108. }
  109. handleClickOutside(evt) {
  110. this.setState({
  111. showPopUp: false
  112. })
  113. }
  114. showDelay() {
  115. let nodeId = this.props.eventNodeId || newEventStores.curEventNodeId
  116. let isServer = isServerRootNode(getEventNodeById(nodeId))
  117. return !isServer
  118. }
  119. render() {
  120. return (
  121. <div className={cls('action-delay f--h', { hidden: !this.showDelay() })}>
  122. <button
  123. className="btn-clear btn-action-delay"
  124. onClick={this.showPopUp}
  125. dangerouslySetInnerHTML={{ __html: iconStrObj.delayIcon }}
  126. />
  127. {this.state.showPopUp ? (
  128. <div className="action-delay-popup">
  129. <div className="delay-title">
  130. {i18n.t('EventView.jenga_action_block.delaySetting')}
  131. </div>
  132. <div className="delay-input-wrap">
  133. <InputNumber
  134. ref={this.delayInput}
  135. min={0}
  136. className="normal"
  137. value={this.state.delayTime}
  138. onChange={this.onChange}
  139. onFocus={this.onFocus}
  140. />
  141. </div>
  142. </div>
  143. ) : null}
  144. </div>
  145. )
  146. }
  147. }
  148. export default onClickOutside(BlockActionDelay)