JengaDrag.jsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import React from 'react'
  2. import uiActions from 'actions/ui'
  3. import uiStores from 'stores/ui'
  4. import newEventStores from 'stores/newEvent'
  5. const minWidth = 920 // 最小宽度
  6. export default class JengaDrag extends React.Component {
  7. constructor(props) {
  8. super(props)
  9. this.onEventChange = this.onEventChange.bind(this)
  10. this.getMinWidth = this.getMinWidth.bind(this)
  11. this.resetWidth = this.resetWidth.bind(this)
  12. this.onDragStart = this.onDragStart.bind(this)
  13. this.onDragEnd = this.onDragEnd.bind(this)
  14. this.onDragMove = this.onDragMove.bind(this)
  15. }
  16. componentDidMount() {
  17. this.resetWidth()
  18. this.unsubscribe = newEventStores.listen(this.onEventChange)
  19. }
  20. componentWillUnmount() {
  21. this.unsubscribe()
  22. }
  23. onEventChange(status) {
  24. if (status && status.types) {
  25. if (status.types.includes('showMemo')) {
  26. this.resetWidth()
  27. }
  28. }
  29. }
  30. getMinWidth() {
  31. return minWidth + (newEventStores.showMemo ? 150 : 0)
  32. }
  33. resetWidth(setViewWidth = true, afterSetWidth) {
  34. if (setViewWidth) {
  35. this.viewWidth = this.getMinWidth() + uiStores.jengaViewWidthDelta
  36. }
  37. let eventView = document.querySelector('.event-view')
  38. if (eventView) {
  39. eventView.style.width = this.viewWidth + 'px'
  40. afterSetWidth && afterSetWidth()
  41. }
  42. }
  43. onDragStart(e) {
  44. this.startX = e.pageX
  45. let curMinWidth = this.getMinWidth()
  46. this.startWidth =
  47. this.viewWidth > curMinWidth ? this.viewWidth : curMinWidth
  48. document.addEventListener('mousemove', this.onDragMove)
  49. document.addEventListener('mouseup', this.onDragEnd)
  50. }
  51. onDragEnd() {
  52. document.removeEventListener('mousemove', this.onDragMove)
  53. document.removeEventListener('mouseup', this.onDragEnd)
  54. }
  55. onDragMove(e) {
  56. this.viewWidth = this.startWidth + (e.pageX - this.startX)
  57. let curMinWidth = this.getMinWidth()
  58. if (this.viewWidth < curMinWidth) {
  59. this.viewWidth = curMinWidth
  60. }
  61. // 使用style的方式
  62. this.resetWidth(false, () => {
  63. // 通知其他相关组件去更新宽度
  64. uiActions.changeJengaViewWidthDelta({
  65. delta: this.viewWidth - this.getMinWidth()
  66. })
  67. })
  68. }
  69. render() {
  70. return <div className="jenga-drag" onMouseDown={this.onDragStart} />
  71. }
  72. }