Skip to content

Commit 0866b9d

Browse files
author
seraphimrose
committed
dnd com
1 parent 77c4ace commit 0866b9d

File tree

7 files changed

+44
-70
lines changed

7 files changed

+44
-70
lines changed

app/action/entity.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,4 @@ export const addCard = createAction("Add a new card", data => fromJS(data))
77

88
export const removeCard = createAction("Remove a card", data => fromJS(data))
99

10-
export const moveCard = createAction("Move a card", data => fromJS(data))
11-
12-
export const addSlot = createAction("Add a slot", data => fromJS(data))
13-
14-
export const removeSlot = createAction("Remove a slot", data => fromJS(data))
10+
export const moveCard = createAction("Move a card", data => fromJS(data))

app/component/board/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export default class Board extends Component {
116116
key={value}
117117
index={value}
118118
list={list.get(value)}
119+
rawList={list}
119120
/>
120121
))}
121122
{this.state.isAdding ? (

app/component/card/index.js

+22-23
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Icon from 'antd/lib/icon'
44
import { DragSource, DropTarget } from 'react-dnd';
55
import { findDOMNode } from 'react-dom'
66

7-
import { removeCard, addSlot, removeSlot } from 'action/entity'
7+
import { removeCard, moveCard } from 'action/entity'
88
import style from './card.css'
99

1010
const cardDragSource = {
@@ -20,30 +20,31 @@ const cardDropTarget = {
2020
if (props.index === monitor.getItem().index) {
2121
return ;
2222
}
23-
const target = findDOMNode(component).getBoundingClientRect();
24-
const targetMiddleY = (target.bottom - target.top) / 2;
25-
const cursor = monitor.getClientOffset();
2623

27-
const cur = props.list.get('card')
28-
const ind = cur.indexOf(props.index)
29-
30-
props.board.get('list').forEach(v => {
31-
props.dispatch(removeSlot({
32-
listIndex: v
33-
}))
34-
})
24+
const target = findDOMNode(component).getBoundingClientRect()
25+
const targetMiddleY = target.top + (target.bottom - target.top) / 2;
26+
const cursor = monitor.getClientOffset()
3527

28+
let upFlag
3629
if (cursor.y < targetMiddleY) {
37-
props.dispatch(addSlot({
38-
listIndex: props.listIndex,
39-
'new': cur.splice(ind, 0, 'slot')
40-
}))
30+
upFlag = 1
4131
} else {
42-
props.dispatch(addSlot({
43-
listIndex: props.listIndex,
44-
'new': cur.splice(ind + 1, 0, 'slot')
45-
}))
32+
upFlag = 0
4633
}
34+
let fromList
35+
props.rawList.forEach((v, k) => {
36+
if (v.get('card').indexOf(monitor.getItem().index) !== -1) {
37+
fromList = k
38+
}
39+
})
40+
41+
props.dispatch(moveCard({
42+
fromList: fromList,
43+
toList: props.listIndex,
44+
index: monitor.getItem().index,
45+
hoverIndex: props.index,
46+
upFlag: upFlag
47+
}))
4748
}
4849
}
4950

@@ -69,7 +70,6 @@ export default class Card extends Component {
6970

7071
render() {
7172
const {
72-
list,
7373
card,
7474
tag,
7575
member,
@@ -84,8 +84,7 @@ export default class Card extends Component {
8484
return connectDropTarget(connectDragSource(
8585
<div
8686
style={{
87-
//transform: isDragging && "rotate(10deg)",
88-
display: isDragging && "none"
87+
opacity: isDragging && 0.5
8988
}}
9089
className={style.card}
9190
>

app/component/list/index.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,13 @@ export default class List extends Component {
111111
style={{width: 270}}
112112
>
113113
{list.get('card').map(value => (
114-
value !== 'slot' ? (
115-
<Card
116-
{...this.props}
117-
key={value}
118-
index={value}
119-
listIndex={index}
120-
card={card.get(value)}
121-
/>
122-
) : (
123-
<div className={style.slot}></div>
124-
)
114+
<Card
115+
{...this.props}
116+
key={value}
117+
index={value}
118+
listIndex={index}
119+
card={card.get(value)}
120+
/>
125121
))}
126122
</Scrollbars>
127123
{this.state.isAdding ? (

app/component/list/list.css

-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313
padding-bottom: 5px;
1414
}
1515

16-
:local(.slot) {
17-
width: 260px;
18-
height: 100px;
19-
margin-bottom: 8px;
20-
background: #ccc;
21-
border-radius: 5px;
22-
box-shadow: 1px 1px 1px 1px #777;
23-
}
24-
2516
:local(.new) {
2617
width: 260px;
2718
padding: 6px;

app/component/test/index.js

-7
This file was deleted.

app/reducer/entity.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@ export default createReducer({
1111
[actions.addCard]: (state, data) => state.mergeDeep(data),
1212

1313
[actions.removeCard]: (state, data) =>
14-
state//.deleteIn(['card', data.get('index')]) // <-- unnecessary?
15-
.deleteIn(['list', data.get('listIndex'), 'card',
16-
state.getIn(['list', data.get('listIndex'), 'card']).indexOf(data.get('index'))]),
14+
state.deleteIn(['list', data.get('listIndex'), 'card',
15+
state.getIn(['list', data.get('listIndex'), 'card']).indexOf(data.get('index'))]),
1716

18-
[actions.addSlot]: (state, data) =>
19-
state.setIn(['list', data.get('listIndex'), 'card'], data.get('new')),
20-
[actions.removeSlot]: (state, data) => {
21-
const index = state.getIn(['list', data.get('listIndex'), 'card']).indexOf('slot')
22-
return index !== -1 ? state.deleteIn(['list', data.get('listIndex'), 'card', index]) : state
23-
},
24-
25-
26-
[actions.moveCard]: (state, data) =>
27-
state.deleteIn(['list', data.get('fromListIndex'), 'card',
28-
state.getIn(['list', data.get('fromListIndex'), 'card']).indexOf(data.get('index'))])
29-
.setIn(['list', data.get('toListIndex'), 'card'], data.get('new'))
17+
[actions.moveCard]: (state, data) => {
18+
const sourcePos = state.getIn(['list', data.get('fromList'), 'card']).indexOf(data.get('index'))
19+
let insertPos = state.getIn(['list', data.get('toList'), 'card']).indexOf(data.get('hoverIndex'))
20+
if (!data.get('upFlag')) {
21+
insertPos ++
22+
}
23+
const newState = state.deleteIn(['list', data.get('fromList'), 'card', sourcePos])
24+
const rawCard = newState.getIn(['list', data.get('toList'), 'card'])
25+
return newState.setIn(['list', data.get('toList'), 'card'], rawCard.splice(insertPos, 0, data.get('index')))
26+
}
27+
3028
}, initialState)

0 commit comments

Comments
 (0)