BlockCite.jsx 3.6 KB

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