JengaItemTitle.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import React from 'react'
  2. import cls from 'classnames'
  3. import newEventActions from 'actions/newEvent'
  4. import uiActions from 'actions/ui'
  5. import newEventStores from 'stores/newEvent'
  6. import treeStores from 'stores/tree'
  7. import { getEventNodeById } from 'stores/funcs/newEvent'
  8. import { shallowEqualImmutable } from 'utils/utils'
  9. import { CONTEXT_MENU_TYPE } from 'const/const'
  10. import JengaCite from './JengaCite'
  11. export default class JengaItemTitle extends React.Component {
  12. constructor(props) {
  13. super(props)
  14. this.curEventNodeId = newEventStores.curEventNodeId
  15. this.curBlockId = newEventStores.curBlockId
  16. this.state = {
  17. name: this.getName(),
  18. isActive: this.isActive()
  19. }
  20. this.onTreeChange = this.onTreeChange.bind(this)
  21. this.onEventChange = this.onEventChange.bind(this)
  22. this.isCurEventNodeId = this.isCurEventNodeId.bind(this)
  23. this.getName = this.getName.bind(this)
  24. this.isActive = this.isActive.bind(this)
  25. this.onSelectBlock = this.onSelectBlock.bind(this)
  26. // 右键
  27. this.onContextMenu = this.onContextMenu.bind(this)
  28. }
  29. shouldComponentUpdate(nextProps, nextState) {
  30. return (
  31. !shallowEqualImmutable(this.props, nextProps) ||
  32. !shallowEqualImmutable(this.state, nextState)
  33. )
  34. }
  35. componentDidMount() {
  36. this.unsubscribe = treeStores.listen(this.onTreeChange)
  37. this.unsubscribeEvent = newEventStores.listen(this.onEventChange)
  38. }
  39. componentWillUnmount() {
  40. this.unsubscribe()
  41. this.unsubscribeEvent()
  42. }
  43. componentDidUpdate(preProps) {
  44. if (preProps.eventNodeId !== this.props.eventNodeId) {
  45. this.setState({
  46. name: this.getName(),
  47. isActive: this.isActive()
  48. })
  49. }
  50. }
  51. onTreeChange(status) {
  52. let obj = {}
  53. if (status && status.types) {
  54. if (status.types.includes('changeNodeUIs')) {
  55. if (this.curEventNodeId === status.data.nodeId) {
  56. let uis = status.data.uis
  57. if (
  58. Object.keys(uis).includes('name') &&
  59. this.state.name !== uis.name
  60. ) {
  61. obj.name = uis.name
  62. }
  63. }
  64. }
  65. }
  66. if (Object.keys(obj).length > 0) {
  67. this.setState(obj)
  68. }
  69. }
  70. onEventChange(status) {
  71. let obj = {}
  72. if (status && status.types) {
  73. if (status.types.includes('curEventNodeId')) {
  74. if (this.curEventNodeId !== status.data.curEventNodeId) {
  75. this.curEventNodeId = status.data.curEventNodeId
  76. if (this.isCurEventNodeId(status.data.curEventNodeId)) {
  77. obj.name = this.getName()
  78. }
  79. if (this.state.isActive !== this.isActive()) {
  80. obj.isActive = this.isActive()
  81. }
  82. }
  83. }
  84. if (status.types.includes('curBlockId')) {
  85. if (this.curBlockId !== status.data.curBlockId) {
  86. this.curBlockId = status.data.curBlockId
  87. if (this.state.isActive !== this.isActive()) {
  88. obj.isActive = this.isActive()
  89. }
  90. }
  91. }
  92. }
  93. if (Object.keys(obj).length > 0) {
  94. this.setState(obj)
  95. }
  96. }
  97. isCurEventNodeId(curEventNodeId) {
  98. return !this.props.eventNodeId || this.props.eventNodeId === curEventNodeId
  99. }
  100. getName() {
  101. let nodeId = this.props.eventNodeId || this.curEventNodeId
  102. if (nodeId) {
  103. let node = getEventNodeById(nodeId)
  104. if (node) {
  105. return node.uis.name || node.type
  106. }
  107. }
  108. return ''
  109. }
  110. isActive() {
  111. return (
  112. this.isCurEventNodeId(this.curEventNodeId) && this.curBlockId === null
  113. )
  114. }
  115. onSelectBlock() {
  116. if (
  117. this.props.eventNodeId &&
  118. this.props.eventNodeId !== newEventStores.curEventNodeId
  119. ) {
  120. newEventActions.selectEvent({ eventNodeId: this.props.eventNodeId })
  121. }
  122. if (!this.state.isActive) {
  123. newEventActions.selectBlock({ blockId: null })
  124. }
  125. }
  126. onContextMenu(e) {
  127. e.preventDefault()
  128. e.stopPropagation()
  129. this.onSelectBlock()
  130. let info = {
  131. x: e.pageX,
  132. y: e.pageY,
  133. type: CONTEXT_MENU_TYPE.EVENT
  134. }
  135. uiActions.contextMenu(info)
  136. }
  137. render() {
  138. return (
  139. <div
  140. className={cls('item-title-wrap f--hlc', {
  141. active: this.state.isActive
  142. })}
  143. onClick={this.onSelectBlock}
  144. onContextMenu={this.onContextMenu}
  145. >
  146. <div className="title flex-1">{this.state.name}</div>
  147. <JengaCite eventNodeId={this.props.eventNodeId} />
  148. </div>
  149. )
  150. }
  151. }