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

Drag n drop #3008

Open
wants to merge 2 commits into
base: develop
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
1 change: 1 addition & 0 deletions client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const CLEAR_LINT_MESSAGE = 'CLEAR_LINT_MESSAGE';
export const TOGGLE_FORCE_DESKTOP = 'TOGGLE_FORCE_DESKTOP';

export const UPDATE_FILE_NAME = 'UPDATE_FILE_NAME';
export const CHANGE_PARENT = 'CHANGE_PARENT';
export const DELETE_FILE = 'DELETE_FILE';

export const SET_AUTOSAVE = 'SET_AUTOSAVE';
Expand Down
10 changes: 10 additions & 0 deletions client/modules/IDE/actions/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ export function createFile(file, parentId) {
};
}

export function changeParent(id, oldParentId, newParentId) {
return (dispatch) =>
dispatch({
type: ActionTypes.CHANGE_PARENT,
id,
oldParentId,
newParentId
});
}

export function submitFile(formProps, files, parentId, projectId) {
if (projectId) {
const postParams = {
Expand Down
47 changes: 46 additions & 1 deletion client/modules/IDE/components/FileNode.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,45 @@ class FileNode extends React.Component {
this.props.hideFolderChildren(this.props.id);
};

handleDrag = (e) => {
e.dataTransfer.setData('text', `${this.props.id} ${this.props.parentId}`);
};

handleDragOver = (e) => {
e.preventDefault();
if (this.props.fileType === 'folder') {
e.target.parentNode.parentNode.style.border = '1px solid #ed225d';
} else if (this.props.fileType === 'file') {
e.target.parentNode.parentNode.parentNode.parentNode.parentNode.style.border =
'1px solid #ed225d';
}
};

handleDragLeave = (e) => {
e.preventDefault();
if (this.props.fileType === 'folder') {
e.target.parentNode.parentNode.style.border = null;
} else if (this.props.fileType === 'file') {
e.target.parentNode.parentNode.parentNode.parentNode.parentNode.style.border = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to avoid direct manipulation of styles and instead set some state somewhere which controls the style. I’m also concerned that the number of parent levels that we need to go up by might depend on where this specific file I in the tree, and could be incorrect if it’s a few levels deep.

}
};

handleDrop = (e) => {
e.preventDefault();
const dragPayload = e.dataTransfer.getData('text');
const [id, oldParentId] = dragPayload.split(' ');
let newParentId = null;
if (this.props.fileType === 'folder') {
newParentId = this.props.id;
this.props.showFolderChildren(this.props.id);
e.target.parentNode.parentNode.style.border = null;
} else if (this.props.fileType === 'file') {
e.target.parentNode.parentNode.parentNode.parentNode.parentNode.style.border = null;
newParentId = this.props.parentId;
}
this.props.changeParent(id, oldParentId, newParentId);
};

renderChild = (childId) => (
<li key={childId}>
<ConnectedFileNode
Expand Down Expand Up @@ -258,10 +297,15 @@ class FileNode extends React.Component {
const { t } = this.props;

return (
<div className={itemClass}>
<div className={itemClass} onLoad={() => this.getRootID()}>
{!isRoot && (
<div
className="file-item__content"
draggable
onDragStart={(e) => this.handleDrag(e)}
onDrop={(e) => this.handleDrop(e)}
onDragOver={(e) => this.handleDragOver(e)}
onDragLeave={(e) => this.handleDragLeave(e)}
onContextMenu={this.toggleFileOptions}
>
<span className="file-item__spacer"></span>
Expand Down Expand Up @@ -418,6 +462,7 @@ FileNode.propTypes = {
deleteFile: PropTypes.func.isRequired,
updateFileName: PropTypes.func.isRequired,
resetSelectedFile: PropTypes.func.isRequired,
changeParent: PropTypes.func.isRequired,
newFile: PropTypes.func.isRequired,
newFolder: PropTypes.func.isRequired,
showFolderChildren: PropTypes.func.isRequired,
Expand Down
17 changes: 17 additions & 0 deletions client/modules/IDE/reducers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ function updateParent(state, action) {
});
}

function changeParent(state, action) {
return state.map((file) => {
// Add to children list of new parent
if (file.id === action.newParentId) {
file.children = [...file.children, action.id];
}
// Remove from children list of old parent
else if (file.id === action.oldParentId) {
file.children = file.children.filter((childId) => childId !== action.id);
}
return file;
});
}

function renameFile(state, action) {
return state.map((file) => {
if (file.id !== action.id) {
Expand Down Expand Up @@ -230,6 +244,9 @@ const files = (state, action) => {
return file;
});
}
case ActionTypes.CHANGE_PARENT: {
return changeParent(state, action);
}
case ActionTypes.DELETE_FILE: {
const newState = deleteMany(state, [
action.id,
Expand Down
Loading