App.js 12 KB

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