JengaCite.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import React from 'react'
  2. import cls from 'classnames'
  3. import { shallowEqualImmutable } from 'src/utils/utils'
  4. import citeActions from 'actions/cite'
  5. import citeStores from 'stores/cite'
  6. import newEventStores from 'stores/newEvent'
  7. export default class JengaCite extends React.Component {
  8. constructor(props) {
  9. super(props)
  10. this.state = {
  11. eventPanelMode: newEventStores.eventPanelMode,
  12. curEventNodeId: newEventStores.curEventNodeId,
  13. isActive: false, // 是否处于引用激活态
  14. memoExpanded: newEventStores.showMemo // 备注是否展开
  15. }
  16. this.getIsActive = this.getIsActive.bind(this)
  17. this.onEventChange = this.onEventChange.bind(this)
  18. this.onCiteChange = this.onCiteChange.bind(this)
  19. this.onToggleJengaCite = this.onToggleJengaCite.bind(this)
  20. }
  21. componentDidMount() {
  22. this.unsubscribe = newEventStores.listen(this.onEventChange)
  23. this.unsubscribeCite = citeStores.listen(this.onCiteChange)
  24. }
  25. componentWillUnmount() {
  26. this.unsubscribe()
  27. this.unsubscribeCite()
  28. }
  29. shouldComponentUpdate(nextProps, nextState) {
  30. return (
  31. !shallowEqualImmutable(this.props, nextProps) ||
  32. !shallowEqualImmutable(this.state, nextState)
  33. )
  34. }
  35. componentDidUpdate(preProps) {
  36. let isActive = this.getIsActive()
  37. if (this.state.isActive !== isActive) {
  38. this.setState({
  39. isActive: isActive
  40. })
  41. }
  42. }
  43. onEventChange(status) {
  44. let obj = {}
  45. if (status && status.types) {
  46. if (status.types.includes('curEventNodeId')) {
  47. obj.curEventNodeId = status.data.curEventNodeId
  48. }
  49. if (status.types.includes('showMemo')) {
  50. if (this.state.memoExpanded !== status.data.showMemo) {
  51. obj.memoExpanded = status.data.showMemo
  52. }
  53. }
  54. if (status.data && status.data.showEventView !== undefined) {
  55. if (this.state.eventPanelMode !== status.data.eventPanelMode) {
  56. obj.eventPanelMode = status.data.eventPanelMode
  57. }
  58. }
  59. }
  60. if (Object.keys(obj).length > 0) {
  61. this.setState(obj)
  62. }
  63. }
  64. onCiteChange(status) {
  65. let obj = {}
  66. if (status.types.includes('activeJengaCite')) {
  67. obj.isActive = status.data.eventNodeId === this.props.eventNodeId
  68. }
  69. if (
  70. status.types.includes('deactiveCite') ||
  71. status.types.includes('activeEventsCited') ||
  72. status.types.includes('activePropsCite') ||
  73. status.types.includes('activeBlockCite')
  74. ) {
  75. obj.isActive = false
  76. }
  77. if (Object.keys(obj).length > 0) {
  78. this.setState(obj)
  79. }
  80. }
  81. getIsActive() {
  82. return (
  83. citeStores.curCite &&
  84. citeStores.curCite.eventNodeId === this.props.eventNodeId &&
  85. citeStores.curCite.type === 'jengaCite'
  86. )
  87. }
  88. onToggleJengaCite() {
  89. let curNodeId = this.props.eventNodeId || this.state.curEventNodeId
  90. if (this.state.isActive) {
  91. citeActions.deactiveAllCite({})
  92. } else {
  93. citeActions.activeJengaCite({
  94. eventNodeId: curNodeId
  95. })
  96. }
  97. }
  98. render() {
  99. return this.state.eventPanelMode === 'citedJengas' ? (
  100. <button
  101. className={cls('btn-clear btn-jenga-cite', {
  102. active: this.state.isActive,
  103. 'memo-expanded': this.state.memoExpanded
  104. })}
  105. onClick={this.onToggleJengaCite}
  106. />
  107. ) : null
  108. }
  109. }