BlockCommentWrap.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React from 'react'
  2. import i18n from 'i18next'
  3. import { message } from 'antd'
  4. import TextareaAutosize from 'react-autosize-textarea'
  5. import newEventActions from 'actions/newEvent'
  6. import newEventStores from 'stores/newEvent'
  7. import { getEventBlockByBid } from 'stores/funcs/newEvent'
  8. import { BLOCK_OP_TYPE } from 'const/const'
  9. import { copyToClipboard } from 'utils/commons/copyToClipboard'
  10. import { shallowEqualImmutable } from 'utils/utils'
  11. export default class BlockCommentWrap extends React.Component {
  12. constructor(props) {
  13. super(props)
  14. this.state = {
  15. showComment: newEventStores.showComment,
  16. comment: this.getComment()
  17. }
  18. this.onEventChange = this.onEventChange.bind(this)
  19. this.getComment = this.getComment.bind(this)
  20. this.copy = this.copy.bind(this)
  21. this.onChange = this.onChange.bind(this)
  22. this.onBlur = this.onBlur.bind(this)
  23. }
  24. shouldComponentUpdate(nextProps, nextState) {
  25. return (
  26. !shallowEqualImmutable(this.props, nextProps) ||
  27. !shallowEqualImmutable(this.state, nextState)
  28. )
  29. }
  30. componentDidMount() {
  31. this.unsubscribe = newEventStores.listen(this.onEventChange)
  32. let { changeShowComment } = this.props
  33. let { showComment } = this.state
  34. changeShowComment && changeShowComment(showComment)
  35. }
  36. componentWillUnmount() {
  37. this.unsubscribe()
  38. }
  39. componentDidUpdate(preProps) {
  40. if (preProps.bid !== this.props.bid) {
  41. this.setState({
  42. comment: this.getComment()
  43. })
  44. }
  45. }
  46. onEventChange(status) {
  47. let obj = {}
  48. if (status && status.types) {
  49. if (status.types.includes('changeShowComment')) {
  50. let { showComment } = this.state
  51. if (showComment !== status.data.showComment) {
  52. obj.showComment = status.data.showComment
  53. }
  54. }
  55. }
  56. if (Object.keys(obj).length > 0) {
  57. this.setState(obj, () => {
  58. let { changeShowComment } = this.props
  59. let { showComment } = this.state
  60. changeShowComment && changeShowComment(showComment)
  61. })
  62. }
  63. }
  64. getComment() {
  65. let result = ''
  66. let nodeId = this.props.eventNodeId || newEventStores.curEventNodeId
  67. let block = getEventBlockByBid(this.props.bid, nodeId)
  68. if (block && block.op === BLOCK_OP_TYPE.NOP) {
  69. result = block.val || ''
  70. }
  71. return result
  72. }
  73. copy() {
  74. copyToClipboard(this.state.comment)
  75. message.success(i18n.t('EventView.jenga_log.copy.success'), 1)
  76. }
  77. onChange(e) {
  78. let value = e && e.target ? e.target.value : e
  79. this.setState({
  80. comment: value
  81. })
  82. }
  83. onBlur() {
  84. if (this.getComment() !== this.state.comment) {
  85. let nodeId = this.props.eventNodeId || newEventStores.curEventNodeId
  86. newEventActions.updateBlockProp({
  87. nodeId,
  88. blockId: this.props.bid,
  89. prop: 'val',
  90. value: this.state.comment
  91. })
  92. }
  93. }
  94. render() {
  95. let { comment } = this.state
  96. return (
  97. <div className="comment-wrap">
  98. {!this.state.showComment ? (
  99. <div className="zip-wrap" />
  100. ) : (
  101. <div className="input-wrap f--h">
  102. <button className="btn-clear btn-icon" onClick={this.copy} />
  103. <div className="input-area f--hlc">
  104. <TextareaAutosize
  105. value={comment || ''}
  106. placeholder={'备注详情'}
  107. onChange={this.onChange}
  108. onBlur={this.onBlur}
  109. className={'comment-textarea'}
  110. maxRows={4}
  111. async={true}
  112. />
  113. </div>
  114. </div>
  115. )}
  116. </div>
  117. )
  118. }
  119. }