Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need positions on Move and onMove function should be called on drop action #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/components/Column.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ const columnTarget = {
const { layout, rowIndex, columnIndex, onMove } = props;
const item = monitor.getItem();
if (item.columnIndex !== columnIndex || item.rowIndex !== rowIndex) {
const movedLayout = moveWidget(layout, {
const originalPosition = {
rowIndex: item.rowIndex,
columnIndex: item.columnIndex,
widgetIndex: item.widgetIndex,
}, {
};

const destination = {
rowIndex,
columnIndex,
}, item.widgetName);
onMove(movedLayout);
};

const movedLayout = moveWidget(layout, originalPosition, destination, item.widgetName);
onMove(movedLayout, originalPosition, destination);
}
},
};
Expand Down
32 changes: 18 additions & 14 deletions lib/components/WidgetFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,66 @@ const boxSource = {
};

const cardTarget = {
hover(props, monitor, component) {
drop(props, monitor, component) {
const dragIndex = monitor.getItem().widgetIndex;
const hoverIndex = props.widgetIndex;
const dropIndex = props.widgetIndex;

// Don't replace items with themselves
if (dragIndex === hoverIndex) {
if (dragIndex === dropIndex) {
return;
}

// Determine rectangle on screen
const hoverBoundingRect = findDOMNode(component).getBoundingClientRect();
const dropBoundingRect = findDOMNode(component).getBoundingClientRect();

// Get vertical middle
const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
const dropMiddleY = (dropBoundingRect.bottom - dropBoundingRect.top) / 2;

// Determine mouse position
const clientOffset = monitor.getClientOffset();

// Get pixels to the top
const hoverClientY = clientOffset.y - hoverBoundingRect.top;
const dropClientY = clientOffset.y - dropBoundingRect.top;

// Only perform the move when the mouse has crossed half of the items height
// When dragging downwards, only move when the cursor is below 50%
// When dragging upwards, only move when the cursor is above 50%

// Dragging downwards
if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
if (dragIndex < dropIndex && dropClientY < dropMiddleY) {
return;
}

// Dragging upwards
if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
if (dragIndex > dropIndex && dropClientY > dropMiddleY) {
return;
}

// Time to actually perform the action
const { layout, columnIndex, rowIndex } = props;

if (monitor.getItem().rowIndex === rowIndex && monitor.getItem().columnIndex === columnIndex) {
const newLayout = sortWidget(layout, {
const originalPosition = {
rowIndex,
columnIndex,
widgetIndex: dragIndex,
}, {
};

const destination = {
rowIndex,
columnIndex,
widgetIndex: hoverIndex,
}, monitor.getItem().widgetName);
widgetIndex: dropIndex,
};

const newLayout = sortWidget(layout, originalPosition, destination, monitor.getItem().widgetName);

props.onMove(newLayout);
props.onMove(newLayout, originalPosition, destination);

// Note: we're mutating the monitor item here!
// Generally it's better to avoid mutations,
// but it's good here for the sake of performance
// to avoid expensive index searches.
monitor.getItem().widgetIndex = hoverIndex; // eslint-disable-line no-param-reassign
monitor.getItem().widgetIndex = dropIndex; // eslint-disable-line no-param-reassign
}
},
};
Expand Down
2 changes: 1 addition & 1 deletion lib/components/Widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Widgets = ({ widgets, widgetTypes, onRemove, layout, columnIndex, rowIndex
const createdWidgets = widgets.map((widget, index) => { // eslint-disable-line arrow-body-style
return (
<WidgetFrame
key={index}
key={`${index}-${widget.key}-${columnIndex}`}
widgetName={widget.key}
title={widgetTypes[widget.key].title}
onRemove={onRemove}
Expand Down