BlockLoopCustom.jsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import React from 'react'
  2. import i18n from 'i18next'
  3. import newEventActions from 'actions/newEvent'
  4. import newEventStores from 'stores/newEvent'
  5. import treeStores from 'stores/tree'
  6. import {
  7. getEventBlockByBid,
  8. emptyCon,
  9. genConObj,
  10. consAstToArray
  11. } from 'stores/funcs/newEvent'
  12. import { cpJson, shallowEqualImmutable } from 'utils/utils'
  13. import { consFormulaShow } from 'utils/commons/ctrlHide'
  14. import { BLOCK_OP_TYPE } from 'const/const'
  15. import BlockLoopConItem from './BlockLoopConItem'
  16. import FormulaEditor from 'src/components/common/formulaEditor/EventFormulaEditor'
  17. export default class BlockLoopCustom extends React.Component {
  18. constructor(props) {
  19. super(props)
  20. this.state = {
  21. option: this.getLoopOption(props.bid),
  22. custom: this.getLoopCustom(this.getLoopOption(props.bid), props.bid)
  23. }
  24. this.onEventChange = this.onEventChange.bind(this)
  25. this.onChangeTimes = this.onChangeTimes.bind(this)
  26. this.getLoopOption = this.getLoopOption.bind(this)
  27. this.getLoopCustom = this.getLoopCustom.bind(this)
  28. this.getCustomCom = this.getCustomCom.bind(this)
  29. this.changeConsAst = this.changeConsAst.bind(this)
  30. this.addBlockCon = this.addBlockCon.bind(this)
  31. this.delBlockCon = this.delBlockCon.bind(this)
  32. }
  33. shouldComponentUpdate(nextProps, nextState) {
  34. return (
  35. !shallowEqualImmutable(this.props, nextProps) ||
  36. !shallowEqualImmutable(this.state, nextState)
  37. )
  38. }
  39. componentDidMount() {
  40. this.unsubscribe = newEventStores.listen(this.onEventChange)
  41. }
  42. componentWillUnmount() {
  43. this.unsubscribe()
  44. }
  45. componentDidUpdate(preProps) {
  46. if (preProps.bid !== this.props.bid) {
  47. this.setState({
  48. option: this.getLoopOption(),
  49. custom: this.getLoopCustom(this.getLoopOption())
  50. })
  51. }
  52. }
  53. onEventChange(status) {
  54. let obj = {}
  55. if (status && status.types) {
  56. if (
  57. status.types.includes('updateBlockProp') &&
  58. this.props.bid === status.data.affectBlockId &&
  59. status.data.prop === 'op'
  60. ) {
  61. let option = this.getLoopOption()
  62. if (this.state.option !== option) {
  63. obj.option = option
  64. obj.custom = this.getLoopCustom(option)
  65. }
  66. }
  67. if (
  68. status.types.includes('updateBlockProp') &&
  69. this.props.bid === status.data.affectBlockId &&
  70. status.data.prop === 'cons'
  71. ) {
  72. this.forceUpdate()
  73. }
  74. }
  75. if (Object.keys(obj).length > 0) {
  76. this.setState(obj)
  77. }
  78. }
  79. genForCons(blockId, value) {
  80. return {
  81. op: 'let',
  82. val: [blockId, 'int'],
  83. args: [{ op: 'step', args: [{ op: 'var', expVal: value, args: [] }] }]
  84. }
  85. }
  86. onChangeTimes(value) {
  87. this.setState(
  88. {
  89. custom: value
  90. },
  91. () => {
  92. let forCons = this.genForCons(this.props.bid, value)
  93. newEventActions.updateBlockProp({
  94. blockId: this.props.bid,
  95. prop: 'cons',
  96. value: forCons
  97. })
  98. }
  99. )
  100. }
  101. getLoopOption(bid) {
  102. let _bid = bid || this.props.bid
  103. let nodeId = this.props.eventNodeId || newEventStores.curEventNodeId
  104. let block = getEventBlockByBid(_bid, nodeId)
  105. if (block) {
  106. return block.op || BLOCK_OP_TYPE.FOR
  107. }
  108. return BLOCK_OP_TYPE.FOR
  109. }
  110. getLoopCustom(option, bid) {
  111. let _bid = bid || this.props.bid
  112. let nodeId = this.props.eventNodeId || newEventStores.curEventNodeId
  113. let block = getEventBlockByBid(_bid, nodeId)
  114. if (block) {
  115. switch (option) {
  116. case BLOCK_OP_TYPE.FOR:
  117. return (
  118. block.args &&
  119. block.args[0] &&
  120. block.args[0].args &&
  121. block.args[0].args[0] === 'step' &&
  122. block.args[0].args[0].args &&
  123. block.args[0].args[0].args[0].expVar
  124. )
  125. case BLOCK_OP_TYPE.WHILE:
  126. return consAstToArray(block.args[0])
  127. }
  128. }
  129. return null
  130. }
  131. changeConsAst() {
  132. // 将cons数组转为ast格式
  133. let cons = cpJson(this.state.custom)
  134. let ors = []
  135. let ands = []
  136. cons.forEach((con, index) => {
  137. if (con.flag !== 'or') {
  138. ands.push(con)
  139. } else {
  140. ors.push(ands)
  141. ands = []
  142. ands.push(con)
  143. }
  144. if (index === cons.length - 1) {
  145. ors.push(ands)
  146. }
  147. })
  148. let consAst = {}
  149. if (ors.length > 1) {
  150. // 有or
  151. consAst.op = 'or'
  152. consAst.args = []
  153. ors.forEach(orItem => {
  154. if (orItem.length > 1) {
  155. // 多个条件
  156. let andObj = {
  157. op: 'and',
  158. args: []
  159. }
  160. orItem.forEach(conItem => {
  161. let conObj = genConObj(conItem)
  162. andObj.args.push(conObj)
  163. })
  164. consAst.args.push(andObj)
  165. } else {
  166. // 单个条件
  167. let conObj = genConObj(orItem[0])
  168. consAst.args.push(conObj)
  169. }
  170. })
  171. } else {
  172. // 没有or
  173. consAst.op = 'and'
  174. consAst.args = []
  175. let andArray = ors[0]
  176. if (andArray.length > 1) {
  177. // 多个条件
  178. andArray.forEach(conItem => {
  179. let conObj = genConObj(conItem)
  180. consAst.args.push(conObj)
  181. })
  182. } else {
  183. // 单个条件
  184. consAst = genConObj(andArray[0])
  185. }
  186. }
  187. let nodeId = this.props.eventNodeId || treeStores.curEventNodeId
  188. newEventActions.updateBlockProp({
  189. nodeId,
  190. blockId: this.props.bid,
  191. prop: 'cons',
  192. value: consAst,
  193. trigger: true,
  194. addRecord: true
  195. })
  196. }
  197. addBlockCon() {
  198. let cons = this.state.custom
  199. let newEmptyCon = emptyCon()
  200. cons.push(newEmptyCon)
  201. }
  202. delBlockCon({ index, resetLast }) {
  203. let cons = this.state.custom
  204. if (resetLast && cons.length === 1) {
  205. cons[0] = emptyCon()
  206. } else {
  207. cons.splice(index, 1)
  208. }
  209. if (cons.length === 1) {
  210. cons[0].flag = 'and'
  211. }
  212. }
  213. getCustomCom() {
  214. let forCom = () => {
  215. return (
  216. <div className="loop-for-wrap f--hlc">
  217. <div className="for-tag f--hcc">
  218. {i18n.t('EventView.jenga_loop_block.forTag')}
  219. </div>
  220. <div className="for-times-wrap f--h">
  221. <FormulaEditor
  222. className="loop-formula-editor loop-for-formula-editor"
  223. uid={this.props.bid + BLOCK_OP_TYPE.FOR}
  224. bid={this.props.bid}
  225. value={this.state.custom}
  226. hasCons={consFormulaShow()}
  227. onChangeCode={this.onChangeTimes}
  228. />
  229. </div>
  230. </div>
  231. )
  232. }
  233. let whileCom = () => {
  234. let cons = (
  235. <div className="con-items-wrap">
  236. {this.state.custom.map((con, index) => {
  237. return (
  238. <BlockLoopConItem
  239. index={index}
  240. con={con}
  241. key={index}
  242. layer={this.props.layer}
  243. groupLayer={this.props.groupLayer}
  244. nestGroupLayer={this.props.nestGroupLayer}
  245. extraGapGroupLayer={this.props.extraGapGroupLayer}
  246. bid={this.props.bid}
  247. eventNodeId={this.props.eventNodeId}
  248. onConsChange={this.changeConsAst}
  249. onAddBlockCon={this.addBlockCon}
  250. onDelBlockCon={this.delBlockCon}
  251. />
  252. )
  253. })}
  254. </div>
  255. )
  256. return (
  257. <div className="loop-while-wrap f--hlc">
  258. {!this.state.custom || this.state.custom.length === 0 ? null : cons}
  259. </div>
  260. )
  261. }
  262. switch (this.state.option) {
  263. case BLOCK_OP_TYPE.FOR:
  264. return forCom()
  265. case BLOCK_OP_TYPE.WHILE:
  266. return whileCom()
  267. }
  268. }
  269. render() {
  270. return <div className="loop-custom flex-1">{this.getCustomCom()}</div>
  271. }
  272. }