dragact.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import * as React from "react";
  2. import GridItem, { GridItemEvent } from './GridItem'
  3. import { compactLayout } from './util/compact';
  4. import { getMaxContainerHeight } from './util/sort';
  5. import { layoutCheck } from './util/collison';
  6. import { correctLayout } from './util/correction';
  7. import { stringJoin } from './utils';
  8. import { layoutItemForkey, syncLayout } from './util/initiate';
  9. import './style.css';
  10. export interface DragactLayoutItem {
  11. GridX: number
  12. GridY: number
  13. static?: Boolean
  14. w: number
  15. h: number
  16. isUserMove?: Boolean
  17. key?: number | string
  18. handle?: Boolean
  19. canDrag?: Boolean
  20. canResize?: Boolean
  21. }
  22. export interface DragactProps {
  23. layout: DragactLayoutItem[] //暂时不推荐使用
  24. /**
  25. * 宽度切分比
  26. * 这个参数会把容器的宽度平均分为col等份
  27. * 于是容器内元素的最小宽度就等于 containerWidth/col
  28. */
  29. col: number,
  30. /**
  31. * 容器的宽度
  32. */
  33. width: number,
  34. /**容器内每个元素的最小高度 */
  35. rowHeight: number,
  36. /**
  37. * 容器内部的padding
  38. */
  39. padding?: number,
  40. children: any[] | any
  41. //
  42. // interface GridItemEvent {
  43. // event: any //浏览器拖动事件
  44. // GridX: number //在布局中的x格子
  45. // GridY: number //在布局中的y格子
  46. // w: number //元素的宽度
  47. // h: number //元素的高度
  48. // UniqueKey: string | number //元素的唯一key
  49. // }
  50. /**
  51. * 拖动开始的回调
  52. */
  53. onDragStart?: (event: GridItemEvent) => void
  54. /**
  55. * 拖动中的回调
  56. */
  57. onDrag?: (event: GridItemEvent) => void
  58. /**
  59. * 拖动结束的回调
  60. */
  61. onDragEnd?: (event: GridItemEvent) => void
  62. /**
  63. * 每个元素的margin,第一个参数是左右,第二个参数是上下
  64. */
  65. margin: [number, number]
  66. /**
  67. * layout的名字
  68. */
  69. className: number | string
  70. /**是否有placeholder */
  71. placeholder?: Boolean
  72. style?: React.CSSProperties
  73. }
  74. export interface mapLayout {
  75. [key: string]: DragactLayoutItem
  76. }
  77. interface DragactState {
  78. GridXMoving: number
  79. GridYMoving: number
  80. wMoving: number
  81. hMoving: number
  82. placeholderShow: Boolean
  83. placeholderMoving: Boolean
  84. layout: DragactLayoutItem[]
  85. containerHeight: number
  86. dragType: 'drag' | 'resize'
  87. mapLayout: mapLayout | undefined
  88. }
  89. export class Dragact extends React.Component<DragactProps, DragactState> {
  90. constructor(props: DragactProps) {
  91. super(props)
  92. this.onDrag = this.onDrag.bind(this)
  93. this.onDragStart = this.onDragStart.bind(this)
  94. this.onDragEnd = this.onDragEnd.bind(this)
  95. const layout = props.layout;
  96. this.state = {
  97. GridXMoving: 0,
  98. GridYMoving: 0,
  99. wMoving: 0,
  100. hMoving: 0,
  101. placeholderShow: false,
  102. placeholderMoving: false,
  103. layout: layout,
  104. containerHeight: 500,
  105. dragType: 'drag',
  106. mapLayout: undefined
  107. }
  108. }
  109. onResizeStart = (layoutItem: GridItemEvent) => {
  110. const { GridX, GridY, w, h } = layoutItem
  111. if (this.state.mapLayout) {
  112. const newlayout = syncLayout(this.state.mapLayout, layoutItem)
  113. this.setState({
  114. GridXMoving: GridX,
  115. GridYMoving: GridY,
  116. wMoving: w,
  117. hMoving: h,
  118. placeholderShow: true,
  119. placeholderMoving: true,
  120. mapLayout: newlayout,
  121. dragType: 'resize'
  122. })
  123. }
  124. }
  125. onResizing = (layoutItem: GridItemEvent) => {
  126. const newLayout = layoutCheck(this.state.layout, layoutItem, layoutItem.UniqueKey, layoutItem.UniqueKey, 0);
  127. const { compacted, mapLayout } = compactLayout(newLayout, layoutItem, this.state.mapLayout)
  128. this.setState({
  129. layout: compacted,
  130. wMoving: layoutItem.w,
  131. hMoving: layoutItem.h,
  132. mapLayout: mapLayout,
  133. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
  134. })
  135. }
  136. onResizeEnd = (layoutItem: GridItemEvent) => {
  137. const { compacted, mapLayout } = compactLayout(this.state.layout, undefined, this.state.mapLayout)
  138. this.setState({
  139. placeholderShow: false,
  140. layout: compacted,
  141. mapLayout: mapLayout,
  142. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
  143. })
  144. this.props.onDragEnd && this.props.onDragEnd(layoutItem);
  145. }
  146. onDragStart(bundles: GridItemEvent) {
  147. const { GridX, GridY, w, h } = bundles
  148. if (this.state.mapLayout) {
  149. const newlayout = syncLayout(this.state.mapLayout, bundles)
  150. // this.state = {
  151. // ...this.state,
  152. // GridXMoving: GridX,
  153. // GridYMoving: GridY,
  154. // wMoving: w,
  155. // hMoving: h,
  156. // placeholderShow: true,
  157. // placeholderMoving: true,
  158. // mapLayout: newlayout,
  159. // dragType: 'drag'
  160. // }
  161. this.setState({
  162. GridXMoving: GridX,
  163. GridYMoving: GridY,
  164. wMoving: w,
  165. hMoving: h,
  166. placeholderShow: true,
  167. placeholderMoving: true,
  168. mapLayout: newlayout,
  169. dragType: 'drag'
  170. })
  171. }
  172. this.props.onDragStart && this.props.onDragStart(bundles)
  173. }
  174. onDrag(layoutItem: GridItemEvent) {
  175. const { GridY, UniqueKey } = layoutItem;
  176. const moving = GridY - this.state.GridYMoving;
  177. const newLayout = layoutCheck(this.state.layout, layoutItem, UniqueKey, UniqueKey/*用户移动方块的key */, moving);
  178. const { compacted, mapLayout } = compactLayout(newLayout, layoutItem, this.state.mapLayout);
  179. this.setState({
  180. GridXMoving: layoutItem.GridX,
  181. GridYMoving: layoutItem.GridY,
  182. layout: compacted,
  183. mapLayout: mapLayout,
  184. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
  185. })
  186. this.props.onDrag && this.props.onDrag(layoutItem);
  187. }
  188. onDragEnd(layoutItem: GridItemEvent) {
  189. const { compacted, mapLayout } = compactLayout(this.state.layout, undefined, this.state.mapLayout)
  190. this.setState({
  191. placeholderShow: false,
  192. layout: compacted,
  193. mapLayout: mapLayout,
  194. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
  195. })
  196. this.props.onDragEnd && this.props.onDragEnd(layoutItem);
  197. }
  198. renderPlaceholder() {
  199. if (!this.state.placeholderShow) return null
  200. var { col, width, padding, rowHeight, margin, placeholder } = this.props
  201. const { GridXMoving, GridYMoving, wMoving, hMoving, placeholderMoving, dragType } = this.state
  202. if (!placeholder) return null;
  203. if (!padding) padding = 0;
  204. return (
  205. <GridItem
  206. margin={margin}
  207. col={col}
  208. containerWidth={width}
  209. containerPadding={[padding, padding]}
  210. rowHeight={rowHeight}
  211. GridX={GridXMoving}
  212. GridY={GridYMoving}
  213. w={wMoving}
  214. h={hMoving}
  215. style={{ background: 'rgba(15,15,15,0.3)', zIndex: dragType === 'drag' ? 1 : 10, transition: ' all .15s ease-out' }}
  216. isUserMove={!placeholderMoving}
  217. dragType={dragType}
  218. canDrag={false}
  219. canResize={false}
  220. />
  221. )
  222. }
  223. componentWillReceiveProps(nextProps: any) {
  224. if (this.props.children.length > nextProps.children.length) { //remove
  225. const mapLayoutCopy = { ...this.state.mapLayout };
  226. nextProps.children.forEach((child: any) => {
  227. if ((mapLayoutCopy as any)[child.key] !== void 666) delete (mapLayoutCopy as any)[child.key];
  228. })
  229. for (const key in mapLayoutCopy) {
  230. const newLayout = this.state.layout.filter((child) => {
  231. if (child.key !== key) return child
  232. })
  233. const { compacted, mapLayout } = compactLayout(newLayout, undefined, this.state.mapLayout);
  234. this.setState({
  235. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight),
  236. layout: compacted,
  237. mapLayout
  238. })
  239. }
  240. }
  241. if (this.props.children.length < nextProps.children.length) { //add
  242. var item;
  243. for (const idx in nextProps.children) {
  244. const i = nextProps.children[idx];
  245. if (this.state.mapLayout && !this.state.mapLayout[i.key]) {
  246. item = i;
  247. break;
  248. }
  249. }
  250. if (item !== void 666) {
  251. const dataSet = { ...item.props['data-set'], isUserMove: false, key: item.key };
  252. var newLayout = [...this.state.layout, dataSet]
  253. newLayout = correctLayout(newLayout, this.props.col)
  254. const { compacted, mapLayout } = compactLayout(newLayout, undefined, this.state.mapLayout);
  255. this.setState({
  256. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight),
  257. layout: compacted,
  258. mapLayout
  259. })
  260. }
  261. }
  262. }
  263. componentDidMount() {
  264. setTimeout(() => {
  265. let layout = correctLayout(this.state.layout, this.props.col)
  266. const { compacted, mapLayout } = compactLayout(layout, undefined, this.state.mapLayout);
  267. this.setState({
  268. layout: compacted,
  269. mapLayout: mapLayout,
  270. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
  271. })
  272. }, 1);
  273. }
  274. getGridItem(child: any, index: number) {
  275. const { dragType, mapLayout } = this.state;
  276. var { col, width, padding, rowHeight, margin } = this.props;
  277. if (mapLayout) {
  278. const renderItem = layoutItemForkey(mapLayout, child.key);
  279. if (!padding) padding = 0;
  280. return (
  281. <GridItem
  282. {...renderItem}
  283. margin={margin}
  284. col={col}
  285. containerWidth={width}
  286. containerPadding={[padding, padding]}
  287. rowHeight={rowHeight}
  288. onDrag={this.onDrag}
  289. onDragStart={this.onDragStart}
  290. onDragEnd={this.onDragEnd}
  291. isUserMove={renderItem.isUserMove !== void 666 ? renderItem.isUserMove : false}
  292. UniqueKey={child.key}
  293. onResizing={this.onResizing}
  294. onResizeStart={this.onResizeStart}
  295. onResizeEnd={this.onResizeEnd}
  296. dragType={dragType}
  297. key={child.key}
  298. >
  299. {this.props.children(child, renderItem.isUserMove)}
  300. </GridItem >
  301. )
  302. }
  303. }
  304. render() {
  305. const {
  306. width,
  307. className,
  308. layout,
  309. style
  310. } = this.props;
  311. const { containerHeight } = this.state;
  312. return (
  313. <div
  314. className={stringJoin('DraggerLayout', className + '')}
  315. style={{
  316. ...style,
  317. left: 100,
  318. width: width,
  319. height: containerHeight,
  320. zIndex: 1
  321. }}
  322. >
  323. {layout.map((item, index) => {
  324. return this.getGridItem(item, index)
  325. })}
  326. {this.renderPlaceholder()}
  327. </div>
  328. )
  329. }
  330. //api
  331. getLayout() {
  332. return this.state.layout;
  333. }
  334. //api
  335. deleteItem(key: any) {
  336. }
  337. }