App.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import React from 'react'
  2. import PropTypes from 'prop-types'
  3. import GridItem from './GridItem'
  4. import './style.css'
  5. const layoutItemForkey = (layout, key) => {
  6. for (let i = 0, length = layout.length; i < length; i++) {
  7. if (key === layout[i].key) {
  8. return layout[i]
  9. }
  10. }
  11. }
  12. const MapLayoutTostate = (layout, children) => {
  13. return layout.map((child, index) => {
  14. let newChild = { ...child, isUserMove: true, key: children[index].key }
  15. return newChild
  16. })
  17. }
  18. const syncLayout = (layout, key, GridX, GridY, isUserMove) => {
  19. const newlayout = layout.map((item) => {
  20. if (item.key === key) {
  21. item.GridX = GridX
  22. item.GridY = GridY
  23. item.isUserMove = isUserMove
  24. return item
  25. }
  26. return item
  27. })
  28. return newlayout
  29. }
  30. const collision = (a, b) => {
  31. if (a.GridX === b.GridX && a.GridY === b.GridY &&
  32. a.w === b.w && a.h === b.h) {
  33. return true
  34. }
  35. if (a.GridX + a.w <= b.GridX) return false
  36. if (a.GridX >= b.GridX + b.w) return false
  37. if (a.GridY + a.h <= b.GridY) return false
  38. if (a.GridY >= b.GridY + b.h) return false
  39. return true
  40. }
  41. const sortLayout = (layout) => {
  42. return [].concat(layout).sort((a, b) => {
  43. if (a.GridY > b.GridY || (a.GridY === b.GridY && a.GridX > b.GridX)) {
  44. return 1
  45. } else if (a.GridY === b.GridY && a.GridX === b.GridX) {
  46. return 0
  47. }
  48. return -1
  49. })
  50. }
  51. /**获取layout中,item第一个碰撞到的物体 */
  52. const getFirstCollison = (layout, item) => {
  53. for (let i = 0, length = layout.length; i < length; i++) {
  54. if (collision(layout[i], item)) {
  55. return layout[i]
  56. }
  57. }
  58. return null
  59. }
  60. const compactItem = (finishedLayout, item) => {
  61. const newItem = { ...item }
  62. if (finishedLayout.length === 0) {
  63. return { ...newItem, GridY: 0 }
  64. }
  65. /**
  66. * 类似一个递归调用
  67. *
  68. */
  69. while (true) {
  70. let FirstCollison = getFirstCollison(finishedLayout, newItem)
  71. if (FirstCollison) {
  72. newItem.GridY = FirstCollison.GridY + FirstCollison.h
  73. return newItem
  74. }
  75. newItem.GridY--
  76. if (newItem.GridY < 0) {
  77. return { ...newItem, GridY: 0 }
  78. }
  79. }
  80. return newItem
  81. }
  82. const compactLayout = (layout) => {
  83. let sorted = sortLayout(layout)
  84. const needCompact = Array(layout.length)
  85. const compareList = []
  86. for (let i = 0, length = sorted.length; i < length; i++) {
  87. let finished = compactItem(compareList, sorted[i])
  88. finished.isUserMove = false
  89. compareList.push(finished)
  90. needCompact[layout.indexOf(sorted[i])] = finished
  91. }
  92. return needCompact
  93. }
  94. const layoutCheck = (layout, layoutItem, key, fristItemkey, moving) => {
  95. let i = [], movedItem = []
  96. let newlayout = layout.map((item, idx) => {
  97. if (item.key !== key) {
  98. if (collision(item, layoutItem)) {
  99. i.push(item.key)
  100. let offsetY = layoutItem.GridY
  101. if (layoutItem.GridY > item.GridY && layoutItem.GridY < item.GridY + item.h) {
  102. /**
  103. * 元素向上移动时,元素的上面空间不足,则不移动这个元素
  104. * 当元素移动到GridY>所要向上交换的元素时,就不会进入这里,直接交换元素
  105. *
  106. */
  107. offsetY = item.GridY
  108. }
  109. if (moving > 0) {
  110. /**
  111. * 这个地方的实现有点奇妙了,moving用于检查最开始移动的方块
  112. * layoutItem.GridY > item.h*(3/4) 这个做会让方块移动比较准确和精确
  113. * 如果是其他数字,很可能会出现不可预计的效果
  114. * 建议取值范围在1/2 ~ 3/4之间
  115. */
  116. if (layoutItem.GridY < item.GridY) {
  117. let collision;
  118. let copy = { ...item }
  119. while (true) {
  120. let newLayout = layout.filter((item) => {
  121. if (item.key !== key && (item.key !== copy.key) ) {
  122. return item
  123. }
  124. })
  125. collision = getFirstCollison(newLayout, copy)
  126. if (collision) {
  127. offsetY = collision.GridY + collision.h
  128. // console.log('移动到', offsetY, '操纵的物体底部', copy.key, '碰撞顶部', copy.GridY,'key',collision.key)
  129. break
  130. } else {
  131. copy.GridY--
  132. }
  133. if(copy.GridY < 0){
  134. // console.log('移动到', offsetY, '操纵的物体底部', copy.key, '碰撞顶部', copy.GridY,)
  135. offsetY = 0
  136. break
  137. }
  138. }
  139. }
  140. }
  141. movedItem.push({ ...item, GridY: offsetY, isUserMove: false })
  142. return { ...item, GridY: offsetY, isUserMove: false }
  143. }
  144. } else if (fristItemkey === key) {
  145. return { ...item, GridX: layoutItem.GridX, GridY: layoutItem.GridY, isUserMove: true }
  146. }
  147. return item
  148. })
  149. /** 递归调用,将layout中的所有重叠元素全部移动 */
  150. if (i.length > 0 && movedItem.length > 0) {
  151. for (let c = 0; c < Math.min(movedItem.length, i.length); c++) {
  152. newlayout = layoutCheck(newlayout, movedItem[c], i[c], fristItemkey, undefined)
  153. }
  154. }
  155. return newlayout
  156. }
  157. class DraggerLayout extends React.Component {
  158. constructor(props) {
  159. super(props)
  160. this.onDrag = this.onDrag.bind(this)
  161. this.onDragStart = this.onDragStart.bind(this)
  162. this.onDragEnd = this.onDragEnd.bind(this)
  163. }
  164. static PropTypes = {
  165. /**外部属性 */
  166. layout: PropTypes.array,
  167. col: PropTypes.number,
  168. width: PropTypes.number,
  169. /**每个元素的最小高度 */
  170. rowHeight: PropTypes.number,
  171. padding: PropTypes.number,
  172. }
  173. state = {
  174. GridXMoving: 0,
  175. GridYMoving: 0,
  176. wMoving: 0,
  177. hMoving: 0,
  178. placeholderShow: false,
  179. placeholderMoving: false,
  180. layout: MapLayoutTostate(this.props.layout, this.props.children)
  181. }
  182. onDragStart(bundles) {
  183. const { GridX, GridY, w, h, UniqueKey } = bundles
  184. const newlayout = syncLayout(this.state.layout, UniqueKey, GridX, GridY, true)
  185. this.setState({
  186. GridXMoving: GridX,
  187. GridYMoving: GridY,
  188. wMoving: w,
  189. hMoving: h,
  190. placeholderShow: true,
  191. placeholderMoving: true,
  192. layout: newlayout,
  193. })
  194. }
  195. onDrag(layoutItem, key) {
  196. const { GridX, GridY } = layoutItem
  197. const moving = GridY - this.state.GridYMoving
  198. const newLayout = layoutCheck(this.state.layout, layoutItem, key, key/*用户移动方块的key */, moving)
  199. // const compactedLayout = compactLayout(newLayout)
  200. // for (let i = 0; i < compactedLayout.length; i++) {
  201. // if (key === compactedLayout[i].key) {
  202. // /**
  203. // * 特殊点:当我们移动元素的时候,元素在layout中的位置不断改变
  204. // * 但是当isUserMove=true的时候,鼠标拖拽的元素不会随着位图变化而变化
  205. // * 但是实际layout中的位置还是会改变
  206. // * (isUserMove=true用于接触placeholder和元素的绑定)
  207. // */
  208. // compactedLayout[i].isUserMove = true
  209. // layoutItem.GridX = compactedLayout[i].GridX
  210. // layoutItem.GridY = compactedLayout[i].GridY
  211. // break
  212. // }
  213. // }
  214. this.setState({
  215. GridXMoving: layoutItem.GridX,
  216. GridYMoving: layoutItem.GridY,
  217. layout: newLayout
  218. })
  219. }
  220. onDragEnd(key) {
  221. const compactedLayout = compactLayout(this.state.layout)
  222. this.setState({
  223. placeholderShow: false,
  224. layout: compactedLayout
  225. })
  226. }
  227. placeholder() {
  228. if (!this.state.placeholderShow) return null
  229. const { col, width, padding, rowHeight } = this.props
  230. const { GridXMoving, GridYMoving, wMoving, hMoving, placeholderMoving } = this.state
  231. return (
  232. <GridItem
  233. col={col}
  234. containerWidth={width}
  235. containerPadding={padding}
  236. rowHeight={rowHeight}
  237. GridX={GridXMoving}
  238. GridY={GridYMoving}
  239. w={wMoving}
  240. h={hMoving}
  241. style={{ background: '#a31', zIndex: -1, transition: ' all .15s' }}
  242. isUserMove={!placeholderMoving}
  243. >
  244. </GridItem >
  245. )
  246. }
  247. componentDidMount() {
  248. let that = this
  249. setTimeout(function () {
  250. that.setState({
  251. layout: compactLayout(that.state.layout)
  252. })
  253. }, 1);
  254. }
  255. getGridItem(child, index) {
  256. const { layout } = this.state
  257. const { col, width, padding, rowHeight } = this.props
  258. const renderItem = layoutItemForkey(layout, child.key)
  259. return (
  260. <GridItem
  261. col={col}
  262. containerWidth={width}
  263. containerPadding={padding}
  264. rowHeight={rowHeight}
  265. GridX={renderItem.GridX}
  266. GridY={renderItem.GridY}
  267. w={renderItem.w}
  268. h={renderItem.h}
  269. onDrag={this.onDrag}
  270. onDragStart={this.onDragStart}
  271. onDragEnd={this.onDragEnd}
  272. index={index}
  273. isUserMove={renderItem.isUserMove}
  274. style={{ background: '#329' }}
  275. UniqueKey={child.key}
  276. >
  277. {child}
  278. </GridItem >
  279. )
  280. }
  281. render() {
  282. const { layout, col, width, padding, rowHeight } = this.props
  283. return (
  284. <div
  285. className='DraggerLayout'
  286. style={{ position: 'absolute', left: 100, width: this.props.width, height: 500, border: '1px solid black' }}
  287. >
  288. {React.Children.map(this.props.children,
  289. (child, index) => this.getGridItem(child, index)
  290. )}
  291. {this.placeholder()}
  292. </div>
  293. )
  294. }
  295. }
  296. export const LayoutDemo = () => {
  297. const layout = [{
  298. GridX: 3, GridY: 2, w: 4, h: 4
  299. }, {
  300. GridX: 0, GridY: 2, w: 1, h: 3
  301. }, {
  302. GridX: 3, GridY: 6, w: 2, h: 1
  303. }, {
  304. GridX: 3, GridY: 8, w: 1, h: 4
  305. }, {
  306. GridX: 3, GridY: 8, w: 3, h: 4
  307. }, {
  308. GridX: 3, GridY: 8, w: 6, h: 2
  309. }, {
  310. GridX: 3, GridY: 8, w: 2, h: 1
  311. }, {
  312. GridX: 3, GridY: 8, w: 7, h: 4
  313. }]
  314. return (
  315. <DraggerLayout layout={layout} width={1000}>
  316. <p key='a'>a</p>
  317. <p key='b'>b</p>
  318. <p key='c'>c</p>
  319. <p key='d'>d</p>
  320. <p key='e'>e</p>
  321. <p key='f'>f</p>
  322. <p key='g'>g</p>
  323. <p key='h'>h</p>
  324. </DraggerLayout>
  325. )
  326. }