BlockMemo.jsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import React from 'react'
  2. import cls from 'classnames'
  3. import i18n from 'i18next'
  4. import newEventActions from 'actions/newEvent'
  5. import newEventStores from 'stores/newEvent'
  6. import { getEventBlockByBid, getEventNodeById } from 'stores/funcs/newEvent'
  7. import { shallowEqualImmutable } from 'utils/utils'
  8. import { autoTextarea } from 'utils/commons/autoTextarea'
  9. class EditMemo extends React.Component {
  10. constructor(props) {
  11. super(props)
  12. this.state = {
  13. value: props.value
  14. }
  15. this.textarea = React.createRef()
  16. this.resetTextarea = this.resetTextarea.bind(this)
  17. this.onChange = this.onChange.bind(this)
  18. this.onBlur = this.onBlur.bind(this)
  19. this.getZIndex = this.getZIndex.bind(this)
  20. }
  21. componentDidMount() {
  22. if (this.textarea && this.textarea.current) {
  23. this.resetTextarea()
  24. }
  25. }
  26. componentDidUpdate(preProps) {
  27. if (preProps.value !== this.props.value) {
  28. this.setState(
  29. {
  30. value: this.props.value
  31. },
  32. () => {
  33. if (this.textarea && this.textarea.current) {
  34. this.resetTextarea()
  35. }
  36. }
  37. )
  38. }
  39. }
  40. resetTextarea() {
  41. this.textarea.current.removeAttribute('style')
  42. delete this.textarea.current._length
  43. delete this.textarea.current.currHeight
  44. autoTextarea(this.textarea.current, 0, undefined)
  45. }
  46. onChange(e) {
  47. let val = e && e.target ? e.target.value : e
  48. this.setState({
  49. value: val
  50. })
  51. }
  52. onBlur() {
  53. this.props.onChange && this.props.onChange({ memo: this.state.value })
  54. }
  55. getZIndex() {
  56. let max = 99999
  57. if (this.props.active) {
  58. return max
  59. } else if (this.props.order !== undefined) {
  60. return max - (this.props.order + 1)
  61. } else {
  62. return 0
  63. }
  64. }
  65. render() {
  66. return (
  67. <div
  68. style={{ zIndex: this.getZIndex() }}
  69. className={cls('block-memo f--hlc', this.props.className, {
  70. active: this.props.active
  71. })}
  72. >
  73. <textarea
  74. ref={this.textarea}
  75. placeholder={i18n.t('EventView.jenga_nav.memo')}
  76. value={this.state.value}
  77. className="memo-textarea"
  78. onChange={this.onChange}
  79. onBlur={this.onBlur}
  80. />
  81. </div>
  82. )
  83. }
  84. }
  85. export default class BlockMemo extends React.Component {
  86. constructor(props) {
  87. super(props)
  88. this.curEventNodeId = newEventStores.curEventNodeId
  89. this.state = {
  90. expanded: newEventStores.showMemo, // 是否显示memo
  91. memo: this.getBlockMemo(),
  92. order: this.getOrder()
  93. }
  94. this.onEventChange = this.onEventChange.bind(this)
  95. this.getOrder = this.getOrder.bind(this)
  96. this.getBlockMemo = this.getBlockMemo.bind(this)
  97. this.onChangeMemo = this.onChangeMemo.bind(this)
  98. this.editContent = this.editContent.bind(this)
  99. }
  100. shouldComponentUpdate(nextProps, nextState) {
  101. return (
  102. !shallowEqualImmutable(this.props, nextProps) ||
  103. !shallowEqualImmutable(this.state, nextState)
  104. )
  105. }
  106. componentDidMount() {
  107. this.unsubscribe = newEventStores.listen(this.onEventChange)
  108. }
  109. componentWillUnmount() {
  110. this.unsubscribe()
  111. }
  112. componentDidUpdate(preProps) {
  113. if (preProps.bid !== this.props.bid) {
  114. let memo = this.getBlockMemo()
  115. if (memo !== this.state.memo) {
  116. this.setState({
  117. memo: memo
  118. })
  119. }
  120. }
  121. }
  122. onEventChange(status) {
  123. let obj = {}
  124. if (status && status.types) {
  125. if (status.types.includes('showMemo')) {
  126. if (this.state.expanded !== status.data.showMemo) {
  127. obj.expanded = status.data.showMemo
  128. }
  129. }
  130. if (
  131. status.types.includes('updateBlockProp') &&
  132. this.props.bid === status.data.affectBlockId &&
  133. status.data.prop === 'com'
  134. ) {
  135. obj.memo = this.getBlockMemo()
  136. }
  137. if (status.types.includes('curEventNodeId')) {
  138. if (this.curEventNodeId !== status.data.curEventNodeId) {
  139. this.curEventNodeId = status.data.curEventNodeId
  140. }
  141. }
  142. if (status.types.includes('curEventBlockOrder')) {
  143. let order = this.getOrder()
  144. if (order !== this.state.order) {
  145. obj.order = order
  146. }
  147. }
  148. }
  149. if (Object.keys(obj).length > 0) {
  150. this.setState(obj)
  151. }
  152. }
  153. getOrder() {
  154. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  155. let eventNode = getEventNodeById(curNodeId)
  156. if (eventNode) {
  157. return eventNode.events.order.indexOf(this.props.bid)
  158. }
  159. return 0
  160. }
  161. getBlockMemo() {
  162. let bid = this.props.bid
  163. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  164. let block = getEventBlockByBid(bid, curNodeId)
  165. if (block && block.com) {
  166. return block.com
  167. }
  168. return ''
  169. }
  170. onChangeMemo(e) {
  171. if (e && e.memo !== undefined && this.state.memo !== e.memo) {
  172. this.setState(
  173. {
  174. memo: e.memo
  175. },
  176. () => {
  177. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  178. newEventActions.updateBlockProp({
  179. nodeId: curNodeId,
  180. blockId: this.props.bid,
  181. prop: 'com',
  182. value: this.state.memo,
  183. addRecord: true,
  184. trigger: false
  185. })
  186. }
  187. )
  188. }
  189. }
  190. editContent() {
  191. return (
  192. <EditMemo
  193. className={this.props.className}
  194. active={this.props.active}
  195. order={this.state.order}
  196. value={this.state.memo}
  197. onChange={this.onChangeMemo}
  198. />
  199. )
  200. }
  201. render() {
  202. return this.state.expanded ? this.editContent() : null
  203. }
  204. }