|
@@ -1,26 +1,23 @@
|
|
-import * as React from "react";
|
|
|
|
|
|
+import * as React from 'react'
|
|
import GridItem, { GridItemEvent } from './GridItem'
|
|
import GridItem, { GridItemEvent } from './GridItem'
|
|
-import { compactLayout } from './util/compact';
|
|
|
|
-import { getMaxContainerHeight } from './util/sort';
|
|
|
|
-import { layoutCheck } from './util/collison';
|
|
|
|
-import { correctLayout } from './util/correction';
|
|
|
|
-import { stringJoin } from './utils';
|
|
|
|
-import { layoutItemForkey, syncLayout } from './util/initiate';
|
|
|
|
-import { DragactProps, DragactState, DragactLayoutItem } from "./dragact-type";
|
|
|
|
-
|
|
|
|
-import './style.css';
|
|
|
|
|
|
+import { compactLayout } from './util/compact'
|
|
|
|
+import { getMaxContainerHeight } from './util/sort'
|
|
|
|
+import { layoutCheck } from './util/collison'
|
|
|
|
+import { correctLayout } from './util/correction'
|
|
|
|
+import { stringJoin } from './utils'
|
|
|
|
+import { layoutItemForkey, syncLayout } from './util/initiate'
|
|
|
|
+import { DragactProps, DragactState, DragactLayoutItem } from './dragact-type'
|
|
|
|
|
|
|
|
+import './style.css'
|
|
|
|
|
|
export class Dragact extends React.Component<DragactProps, DragactState> {
|
|
export class Dragact extends React.Component<DragactProps, DragactState> {
|
|
-
|
|
|
|
constructor(props: DragactProps) {
|
|
constructor(props: DragactProps) {
|
|
super(props)
|
|
super(props)
|
|
this.onDrag = this.onDrag.bind(this)
|
|
this.onDrag = this.onDrag.bind(this)
|
|
this.onDragStart = this.onDragStart.bind(this)
|
|
this.onDragStart = this.onDragStart.bind(this)
|
|
this.onDragEnd = this.onDragEnd.bind(this)
|
|
this.onDragEnd = this.onDragEnd.bind(this)
|
|
|
|
|
|
- const layout = props.layout;
|
|
|
|
-
|
|
|
|
|
|
+ const layout = props.layout
|
|
|
|
|
|
this.state = {
|
|
this.state = {
|
|
GridXMoving: 0,
|
|
GridXMoving: 0,
|
|
@@ -51,33 +48,57 @@ export class Dragact extends React.Component<DragactProps, DragactState> {
|
|
dragType: 'resize'
|
|
dragType: 'resize'
|
|
})
|
|
})
|
|
}
|
|
}
|
|
- this.props.onDragStart && this.props.onDragStart(layoutItem, this.state.layout);
|
|
|
|
|
|
+ this.props.onDragStart &&
|
|
|
|
+ this.props.onDragStart(layoutItem, this.state.layout)
|
|
}
|
|
}
|
|
|
|
|
|
onResizing = (layoutItem: GridItemEvent) => {
|
|
onResizing = (layoutItem: GridItemEvent) => {
|
|
|
|
+ const newLayout = layoutCheck(
|
|
|
|
+ this.state.layout,
|
|
|
|
+ layoutItem,
|
|
|
|
+ layoutItem.UniqueKey + '',
|
|
|
|
+ layoutItem.UniqueKey + '',
|
|
|
|
+ 0
|
|
|
|
+ )
|
|
|
|
|
|
- const newLayout = layoutCheck(this.state.layout, layoutItem, layoutItem.UniqueKey + '', layoutItem.UniqueKey + '', 0);
|
|
|
|
-
|
|
|
|
- const { compacted, mapLayout } = compactLayout(newLayout, layoutItem, this.state.mapLayout)
|
|
|
|
|
|
+ const { compacted, mapLayout } = compactLayout(
|
|
|
|
+ newLayout,
|
|
|
|
+ layoutItem,
|
|
|
|
+ this.state.mapLayout
|
|
|
|
+ )
|
|
|
|
|
|
this.setState({
|
|
this.setState({
|
|
layout: compacted,
|
|
layout: compacted,
|
|
wMoving: layoutItem.w,
|
|
wMoving: layoutItem.w,
|
|
hMoving: layoutItem.h,
|
|
hMoving: layoutItem.h,
|
|
mapLayout: mapLayout,
|
|
mapLayout: mapLayout,
|
|
- containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
|
|
|
|
|
|
+ containerHeight: getMaxContainerHeight(
|
|
|
|
+ compacted,
|
|
|
|
+ this.props.rowHeight,
|
|
|
|
+ this.props.margin[1],
|
|
|
|
+ this.state.containerHeight
|
|
|
|
+ )
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
onResizeEnd = (layoutItem: GridItemEvent) => {
|
|
onResizeEnd = (layoutItem: GridItemEvent) => {
|
|
- const { compacted, mapLayout } = compactLayout(this.state.layout, undefined, this.state.mapLayout)
|
|
|
|
|
|
+ const { compacted, mapLayout } = compactLayout(
|
|
|
|
+ this.state.layout,
|
|
|
|
+ undefined,
|
|
|
|
+ this.state.mapLayout
|
|
|
|
+ )
|
|
this.setState({
|
|
this.setState({
|
|
placeholderShow: false,
|
|
placeholderShow: false,
|
|
layout: compacted,
|
|
layout: compacted,
|
|
mapLayout: mapLayout,
|
|
mapLayout: mapLayout,
|
|
- containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
|
|
|
|
|
|
+ containerHeight: getMaxContainerHeight(
|
|
|
|
+ compacted,
|
|
|
|
+ this.props.rowHeight,
|
|
|
|
+ this.props.margin[1],
|
|
|
|
+ this.state.containerHeight
|
|
|
|
+ )
|
|
})
|
|
})
|
|
- this.props.onDragEnd && this.props.onDragEnd(layoutItem, compacted);
|
|
|
|
|
|
+ this.props.onDragEnd && this.props.onDragEnd(layoutItem, compacted)
|
|
}
|
|
}
|
|
|
|
|
|
onDragStart(bundles: GridItemEvent) {
|
|
onDragStart(bundles: GridItemEvent) {
|
|
@@ -94,44 +115,76 @@ export class Dragact extends React.Component<DragactProps, DragactState> {
|
|
dragType: 'drag'
|
|
dragType: 'drag'
|
|
})
|
|
})
|
|
}
|
|
}
|
|
- this.props.onDragStart && this.props.onDragStart(bundles, this.state.layout);
|
|
|
|
|
|
+ this.props.onDragStart &&
|
|
|
|
+ this.props.onDragStart(bundles, this.state.layout)
|
|
}
|
|
}
|
|
|
|
|
|
onDrag(layoutItem: GridItemEvent) {
|
|
onDrag(layoutItem: GridItemEvent) {
|
|
- const { GridY, UniqueKey } = layoutItem;
|
|
|
|
- const moving = GridY - this.state.GridYMoving;
|
|
|
|
-
|
|
|
|
- const newLayout = layoutCheck(this.state.layout, layoutItem, UniqueKey + '', UniqueKey + ''/*用户移动方块的key */, moving);
|
|
|
|
- const { compacted, mapLayout } = compactLayout(newLayout, layoutItem, this.state.mapLayout);
|
|
|
|
|
|
+ const { GridY, UniqueKey } = layoutItem
|
|
|
|
+ const moving = GridY - this.state.GridYMoving
|
|
|
|
+
|
|
|
|
+ const newLayout = layoutCheck(
|
|
|
|
+ this.state.layout,
|
|
|
|
+ layoutItem,
|
|
|
|
+ UniqueKey + '',
|
|
|
|
+ UniqueKey + '' /*用户移动方块的key */,
|
|
|
|
+ moving
|
|
|
|
+ )
|
|
|
|
+ const { compacted, mapLayout } = compactLayout(
|
|
|
|
+ newLayout,
|
|
|
|
+ layoutItem,
|
|
|
|
+ this.state.mapLayout
|
|
|
|
+ )
|
|
this.setState({
|
|
this.setState({
|
|
GridXMoving: layoutItem.GridX,
|
|
GridXMoving: layoutItem.GridX,
|
|
GridYMoving: layoutItem.GridY,
|
|
GridYMoving: layoutItem.GridY,
|
|
layout: compacted,
|
|
layout: compacted,
|
|
mapLayout: mapLayout,
|
|
mapLayout: mapLayout,
|
|
- containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
|
|
|
|
|
|
+ containerHeight: getMaxContainerHeight(
|
|
|
|
+ compacted,
|
|
|
|
+ this.props.rowHeight,
|
|
|
|
+ this.props.margin[1],
|
|
|
|
+ this.state.containerHeight
|
|
|
|
+ )
|
|
})
|
|
})
|
|
- this.props.onDrag && this.props.onDrag(layoutItem, compacted);
|
|
|
|
|
|
+ this.props.onDrag && this.props.onDrag(layoutItem, compacted)
|
|
}
|
|
}
|
|
|
|
|
|
onDragEnd(layoutItem: GridItemEvent) {
|
|
onDragEnd(layoutItem: GridItemEvent) {
|
|
- const { compacted, mapLayout } = compactLayout(this.state.layout, undefined, this.state.mapLayout);
|
|
|
|
|
|
+ const { compacted, mapLayout } = compactLayout(
|
|
|
|
+ this.state.layout,
|
|
|
|
+ undefined,
|
|
|
|
+ this.state.mapLayout
|
|
|
|
+ )
|
|
|
|
|
|
this.setState({
|
|
this.setState({
|
|
placeholderShow: false,
|
|
placeholderShow: false,
|
|
layout: compacted,
|
|
layout: compacted,
|
|
mapLayout: mapLayout,
|
|
mapLayout: mapLayout,
|
|
- containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
|
|
|
|
|
|
+ containerHeight: getMaxContainerHeight(
|
|
|
|
+ compacted,
|
|
|
|
+ this.props.rowHeight,
|
|
|
|
+ this.props.margin[1],
|
|
|
|
+ this.state.containerHeight
|
|
|
|
+ )
|
|
})
|
|
})
|
|
- this.props.onDragEnd && this.props.onDragEnd(layoutItem, compacted);
|
|
|
|
|
|
+ this.props.onDragEnd && this.props.onDragEnd(layoutItem, compacted)
|
|
}
|
|
}
|
|
|
|
|
|
renderPlaceholder() {
|
|
renderPlaceholder() {
|
|
if (!this.state.placeholderShow) return null
|
|
if (!this.state.placeholderShow) return null
|
|
var { col, padding, rowHeight, margin, placeholder, width } = this.props
|
|
var { col, padding, rowHeight, margin, placeholder, width } = this.props
|
|
- const { GridXMoving, GridYMoving, wMoving, hMoving, placeholderMoving, dragType } = this.state
|
|
|
|
-
|
|
|
|
- if (!placeholder) return null;
|
|
|
|
- if (!padding) padding = 0;
|
|
|
|
|
|
+ const {
|
|
|
|
+ GridXMoving,
|
|
|
|
+ GridYMoving,
|
|
|
|
+ wMoving,
|
|
|
|
+ hMoving,
|
|
|
|
+ placeholderMoving,
|
|
|
|
+ dragType
|
|
|
|
+ } = this.state
|
|
|
|
+
|
|
|
|
+ if (!placeholder) return null
|
|
|
|
+ if (!padding) padding = 0
|
|
return (
|
|
return (
|
|
<GridItem
|
|
<GridItem
|
|
margin={margin}
|
|
margin={margin}
|
|
@@ -143,7 +196,11 @@ export class Dragact extends React.Component<DragactProps, DragactState> {
|
|
GridY={GridYMoving}
|
|
GridY={GridYMoving}
|
|
w={wMoving}
|
|
w={wMoving}
|
|
h={hMoving}
|
|
h={hMoving}
|
|
- style={{ background: 'rgba(15,15,15,0.3)', zIndex: dragType === 'drag' ? 1 : 10, transition: ' all .15s ease-out' }}
|
|
|
|
|
|
+ style={{
|
|
|
|
+ background: 'rgba(15,15,15,0.3)',
|
|
|
|
+ zIndex: dragType === 'drag' ? 1 : 10,
|
|
|
|
+ transition: ' all .15s ease-out'
|
|
|
|
+ }}
|
|
isUserMove={!placeholderMoving}
|
|
isUserMove={!placeholderMoving}
|
|
dragType={dragType}
|
|
dragType={dragType}
|
|
canDrag={false}
|
|
canDrag={false}
|
|
@@ -155,75 +212,106 @@ export class Dragact extends React.Component<DragactProps, DragactState> {
|
|
}
|
|
}
|
|
|
|
|
|
componentWillReceiveProps(nextProps: any) {
|
|
componentWillReceiveProps(nextProps: any) {
|
|
- if (this.props.layout.length > nextProps.layout.length) { //remove
|
|
|
|
- const mapLayoutCopy = { ...this.state.mapLayout };
|
|
|
|
|
|
+ if (this.props.layout.length > nextProps.layout.length) {
|
|
|
|
+ //remove
|
|
|
|
+ const mapLayoutCopy = { ...this.state.mapLayout }
|
|
nextProps.layout.forEach((child: any) => {
|
|
nextProps.layout.forEach((child: any) => {
|
|
- if ((mapLayoutCopy as any)[child.key + ''] !== void 666) delete (mapLayoutCopy as any)[child.key + ''];
|
|
|
|
|
|
+ if ((mapLayoutCopy as any)[child.key + ''] !== void 666)
|
|
|
|
+ delete (mapLayoutCopy as any)[child.key + '']
|
|
})
|
|
})
|
|
|
|
|
|
for (const key in mapLayoutCopy) {
|
|
for (const key in mapLayoutCopy) {
|
|
- const newLayout = this.state.layout.filter((child) => {
|
|
|
|
|
|
+ const newLayout = this.state.layout.filter(child => {
|
|
if (child.key + '' !== key + '') return child
|
|
if (child.key + '' !== key + '') return child
|
|
})
|
|
})
|
|
|
|
|
|
- const { compacted, mapLayout } = compactLayout(newLayout, undefined, this.state.mapLayout);
|
|
|
|
|
|
+ const { compacted, mapLayout } = compactLayout(
|
|
|
|
+ newLayout,
|
|
|
|
+ undefined,
|
|
|
|
+ this.state.mapLayout
|
|
|
|
+ )
|
|
this.setState({
|
|
this.setState({
|
|
- containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight),
|
|
|
|
|
|
+ containerHeight: getMaxContainerHeight(
|
|
|
|
+ compacted,
|
|
|
|
+ this.props.rowHeight,
|
|
|
|
+ this.props.margin[1],
|
|
|
|
+ this.state.containerHeight
|
|
|
|
+ ),
|
|
layout: compacted,
|
|
layout: compacted,
|
|
mapLayout
|
|
mapLayout
|
|
})
|
|
})
|
|
}
|
|
}
|
|
- } else if (this.props.layout.length < nextProps.layout.length) {//add
|
|
|
|
- var item;
|
|
|
|
|
|
+ } else if (this.props.layout.length < nextProps.layout.length) {
|
|
|
|
+ //add
|
|
|
|
+ var item
|
|
for (const idx in nextProps.layout) {
|
|
for (const idx in nextProps.layout) {
|
|
- const i = nextProps.layout[idx];
|
|
|
|
|
|
+ const i = nextProps.layout[idx]
|
|
if (this.state.mapLayout && !this.state.mapLayout[i.key + '']) {
|
|
if (this.state.mapLayout && !this.state.mapLayout[i.key + '']) {
|
|
- item = i;
|
|
|
|
- break;
|
|
|
|
|
|
+ item = i
|
|
|
|
+ break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (item !== void 666) {
|
|
if (item !== void 666) {
|
|
- const dataSet = { ...item, isUserMove: false, key: item.key + '' };
|
|
|
|
|
|
+ const dataSet = {
|
|
|
|
+ ...item,
|
|
|
|
+ isUserMove: false,
|
|
|
|
+ key: item.key + ''
|
|
|
|
+ }
|
|
var newLayout = [...this.state.layout, dataSet]
|
|
var newLayout = [...this.state.layout, dataSet]
|
|
- const { compacted, mapLayout } = compactLayout(newLayout, undefined, this.state.mapLayout);
|
|
|
|
|
|
+ const { compacted, mapLayout } = compactLayout(
|
|
|
|
+ newLayout,
|
|
|
|
+ undefined,
|
|
|
|
+ this.state.mapLayout
|
|
|
|
+ )
|
|
this.setState({
|
|
this.setState({
|
|
- containerHeight: getMaxContainerHeight(compacted,
|
|
|
|
|
|
+ containerHeight: getMaxContainerHeight(
|
|
|
|
+ compacted,
|
|
this.props.rowHeight,
|
|
this.props.rowHeight,
|
|
this.props.margin[1],
|
|
this.props.margin[1],
|
|
this.state.containerHeight,
|
|
this.state.containerHeight,
|
|
- false),
|
|
|
|
|
|
+ false
|
|
|
|
+ ),
|
|
layout: compacted,
|
|
layout: compacted,
|
|
mapLayout
|
|
mapLayout
|
|
})
|
|
})
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- this.recalculateLayout(nextProps.layout, nextProps.col);
|
|
|
|
|
|
+ this.recalculateLayout(nextProps.layout, nextProps.col)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
recalculateLayout = (layout: DragactLayoutItem[], col: number) => {
|
|
recalculateLayout = (layout: DragactLayoutItem[], col: number) => {
|
|
const corrected = correctLayout(layout, col)
|
|
const corrected = correctLayout(layout, col)
|
|
- const { compacted, mapLayout } = compactLayout(corrected, undefined, undefined);
|
|
|
|
|
|
+ const { compacted, mapLayout } = compactLayout(
|
|
|
|
+ corrected,
|
|
|
|
+ undefined,
|
|
|
|
+ undefined
|
|
|
|
+ )
|
|
this.setState({
|
|
this.setState({
|
|
layout: compacted,
|
|
layout: compacted,
|
|
mapLayout: mapLayout,
|
|
mapLayout: mapLayout,
|
|
- containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight, false)
|
|
|
|
|
|
+ containerHeight: getMaxContainerHeight(
|
|
|
|
+ compacted,
|
|
|
|
+ this.props.rowHeight,
|
|
|
|
+ this.props.margin[1],
|
|
|
|
+ this.state.containerHeight,
|
|
|
|
+ false
|
|
|
|
+ )
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
componentDidMount() {
|
|
componentDidMount() {
|
|
setTimeout(() => {
|
|
setTimeout(() => {
|
|
this.recalculateLayout(this.state.layout, this.props.col)
|
|
this.recalculateLayout(this.state.layout, this.props.col)
|
|
- }, 1);
|
|
|
|
-
|
|
|
|
|
|
+ }, 1)
|
|
}
|
|
}
|
|
|
|
|
|
getGridItem(child: any, index: number) {
|
|
getGridItem(child: any, index: number) {
|
|
- const { dragType, mapLayout } = this.state;
|
|
|
|
- var { col, padding, rowHeight, margin, width } = this.props;
|
|
|
|
|
|
+ const { dragType, mapLayout } = this.state
|
|
|
|
+ var { col, padding, rowHeight, margin, width } = this.props
|
|
if (mapLayout) {
|
|
if (mapLayout) {
|
|
- const renderItem = layoutItemForkey(mapLayout, child.key + '');
|
|
|
|
- if (!padding) padding = 0;
|
|
|
|
|
|
+ const renderItem = layoutItemForkey(mapLayout, child.key + '')
|
|
|
|
+ if (!padding) padding = 0
|
|
return (
|
|
return (
|
|
<GridItem
|
|
<GridItem
|
|
{...renderItem}
|
|
{...renderItem}
|
|
@@ -235,7 +323,11 @@ export class Dragact extends React.Component<DragactProps, DragactState> {
|
|
onDrag={this.onDrag}
|
|
onDrag={this.onDrag}
|
|
onDragStart={this.onDragStart}
|
|
onDragStart={this.onDragStart}
|
|
onDragEnd={this.onDragEnd}
|
|
onDragEnd={this.onDragEnd}
|
|
- isUserMove={renderItem.isUserMove !== void 666 ? renderItem.isUserMove : false}
|
|
|
|
|
|
+ isUserMove={
|
|
|
|
+ renderItem.isUserMove !== void 666
|
|
|
|
+ ? renderItem.isUserMove
|
|
|
|
+ : false
|
|
|
|
+ }
|
|
UniqueKey={child.key}
|
|
UniqueKey={child.key}
|
|
onResizing={this.onResizing}
|
|
onResizing={this.onResizing}
|
|
onResizeStart={this.onResizeStart}
|
|
onResizeStart={this.onResizeStart}
|
|
@@ -243,29 +335,28 @@ export class Dragact extends React.Component<DragactProps, DragactState> {
|
|
dragType={dragType}
|
|
dragType={dragType}
|
|
key={child.key}
|
|
key={child.key}
|
|
>
|
|
>
|
|
- {(GridItemProvided, dragHandle, resizeHandle) => this.props.children(child, {
|
|
|
|
- isDragging: renderItem.isUserMove !== void 666 ? renderItem.isUserMove : false,
|
|
|
|
- props: GridItemProvided,
|
|
|
|
- dragHandle,
|
|
|
|
- resizeHandle
|
|
|
|
- })}
|
|
|
|
- </GridItem >
|
|
|
|
|
|
+ {(GridItemProvided, dragHandle, resizeHandle) =>
|
|
|
|
+ this.props.children(child, {
|
|
|
|
+ isDragging:
|
|
|
|
+ renderItem.isUserMove !== void 666
|
|
|
|
+ ? renderItem.isUserMove
|
|
|
|
+ : false,
|
|
|
|
+ props: GridItemProvided,
|
|
|
|
+ dragHandle,
|
|
|
|
+ resizeHandle
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ </GridItem>
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
render() {
|
|
render() {
|
|
- const {
|
|
|
|
- className,
|
|
|
|
- layout,
|
|
|
|
- style,
|
|
|
|
- width
|
|
|
|
- } = this.props;
|
|
|
|
- const { containerHeight } = this.state;
|
|
|
|
|
|
+ const { className, layout, style, width } = this.props
|
|
|
|
+ const { containerHeight } = this.state
|
|
|
|
|
|
return (
|
|
return (
|
|
<div
|
|
<div
|
|
-
|
|
|
|
className={stringJoin('DraggerLayout', className + '')}
|
|
className={stringJoin('DraggerLayout', className + '')}
|
|
style={{
|
|
style={{
|
|
...style,
|
|
...style,
|
|
@@ -285,10 +376,9 @@ export class Dragact extends React.Component<DragactProps, DragactState> {
|
|
|
|
|
|
//api
|
|
//api
|
|
getLayout() {
|
|
getLayout() {
|
|
- return this.state.layout;
|
|
|
|
|
|
+ return this.state.layout
|
|
}
|
|
}
|
|
|
|
|
|
//api
|
|
//api
|
|
- deleteItem(key: any) {
|
|
|
|
- }
|
|
|
|
|
|
+ deleteItem(key: any) {}
|
|
}
|
|
}
|