p5.js-web-editor/client/modules/IDE/actions/files.js

250 lines
6.2 KiB
JavaScript
Raw Normal View History

import axios from 'axios';
2016-08-24 20:22:10 +02:00
import objectID from 'bson-objectid';
import blobUtil from 'blob-util';
import { reset } from 'redux-form';
import * as ActionTypes from '../../../constants';
import { setUnsavedChanges } from './ide';
const ROOT_URL = process.env.API_URL;
2016-06-22 21:58:23 +02:00
function appendToFilename(filename, string) {
const dotIndex = filename.lastIndexOf('.');
if (dotIndex === -1) return filename + string;
return filename.substring(0, dotIndex) + string + filename.substring(dotIndex);
}
function createUniqueName(name, parentId, files) {
const siblingFiles = files.find(file => file.id === parentId)
.children.map(childFileId => files.find(file => file.id === childFileId));
let testName = name;
let index = 1;
let existingName = siblingFiles.find(file => name === file.name);
while (existingName) {
testName = appendToFilename(name, `-${index}`);
index += 1;
existingName = siblingFiles.find((file) => testName === file.name); // eslint-disable-line
}
return testName;
}
export function updateFileContent(name, content) {
2016-06-24 00:29:55 +02:00
return {
type: ActionTypes.UPDATE_FILE_CONTENT,
2016-06-24 00:29:55 +02:00
name,
content
};
}
export function createFile(formProps) {
return (dispatch, getState) => {
const state = getState();
const selectedFile = state.files.find(file => file.isSelectedFile);
2016-08-30 20:39:37 +02:00
const rootFile = state.files.find(file => file.name === 'root');
let parentId;
if (selectedFile.fileType === 'folder') {
parentId = selectedFile.id;
} else {
parentId = rootFile.id;
}
if (state.project.id) {
const postParams = {
name: createUniqueName(formProps.name, parentId, state.files),
2016-08-25 17:25:22 +02:00
url: formProps.url,
content: formProps.content || '',
2016-09-03 00:11:27 +02:00
parentId,
children: []
};
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
.then((response) => {
dispatch({
type: ActionTypes.CREATE_FILE,
2016-08-24 23:29:44 +02:00
...response.data,
2016-08-30 20:39:37 +02:00
parentId
});
dispatch(reset('new-file'));
// dispatch({
// type: ActionTypes.HIDE_MODAL
// });
dispatch(setUnsavedChanges(true));
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
}));
} else {
2016-08-24 20:22:10 +02:00
const id = objectID().toHexString();
dispatch({
type: ActionTypes.CREATE_FILE,
name: createUniqueName(formProps.name, parentId, state.files),
2016-08-24 20:22:10 +02:00
id,
_id: id,
2016-08-25 06:18:28 +02:00
url: formProps.url,
content: formProps.content || '',
2016-09-03 00:11:27 +02:00
parentId,
children: []
});
dispatch(reset('new-file'));
// dispatch({
// type: ActionTypes.HIDE_MODAL
// });
dispatch(setUnsavedChanges(true));
}
};
}
2016-08-03 21:11:59 +02:00
2016-08-30 05:23:10 +02:00
export function createFolder(formProps) {
return (dispatch, getState) => {
const state = getState();
const selectedFile = state.files.find(file => file.isSelectedFile);
2016-08-30 20:39:37 +02:00
const rootFile = state.files.find(file => file.name === 'root');
let parentId;
if (selectedFile.fileType === 'folder') {
parentId = selectedFile.id;
} else {
parentId = rootFile.id;
}
2016-08-30 05:23:10 +02:00
if (state.project.id) {
const postParams = {
name: createUniqueName(formProps.name, parentId, state.files),
2016-08-30 05:23:10 +02:00
content: '',
2016-08-30 20:39:37 +02:00
children: [],
parentId,
2016-08-30 05:23:10 +02:00
fileType: 'folder'
};
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
.then((response) => {
2016-08-30 05:23:10 +02:00
dispatch({
type: ActionTypes.CREATE_FILE,
...response.data,
2016-08-30 20:39:37 +02:00
parentId
2016-08-30 05:23:10 +02:00
});
dispatch({
type: ActionTypes.CLOSE_NEW_FOLDER_MODAL
});
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
}));
} else {
const id = objectID().toHexString();
dispatch({
type: ActionTypes.CREATE_FILE,
name: createUniqueName(formProps.name, parentId, state.files),
2016-08-30 05:23:10 +02:00
id,
_id: id,
content: '',
// TODO pass parent id from File Tree
2016-08-30 20:39:37 +02:00
parentId,
fileType: 'folder',
children: []
2016-08-30 05:23:10 +02:00
});
dispatch({
type: ActionTypes.CLOSE_NEW_FOLDER_MODAL
});
}
};
}
2016-08-03 21:11:59 +02:00
export function showFileOptions(fileId) {
return {
type: ActionTypes.SHOW_FILE_OPTIONS,
id: fileId
};
}
export function hideFileOptions(fileId) {
return {
type: ActionTypes.HIDE_FILE_OPTIONS,
id: fileId
};
}
2016-08-03 23:10:03 +02:00
export function showEditFileName(id) {
return {
type: ActionTypes.SHOW_EDIT_FILE_NAME,
id
};
}
export function hideEditFileName(id) {
return {
type: ActionTypes.HIDE_EDIT_FILE_NAME,
id
};
}
2016-08-03 21:11:59 +02:00
export function updateFileName(id, name) {
return {
type: ActionTypes.UPDATE_FILE_NAME,
id,
name
};
}
2016-08-24 22:06:28 +02:00
export function deleteFile(id, parentId) {
2016-08-24 23:59:15 +02:00
return (dispatch, getState) => {
const state = getState();
2016-08-25 00:52:08 +02:00
if (state.project.id) {
const deleteConfig = {
params: {
2016-08-24 23:59:15 +02:00
parentId
2016-08-25 00:52:08 +02:00
}
};
axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true })
.then(() => {
dispatch({
type: ActionTypes.DELETE_FILE,
id,
parentId
});
})
.catch((response) => {
2016-08-25 00:52:08 +02:00
dispatch({
type: ActionTypes.ERROR,
error: response.data
});
2016-08-24 23:59:15 +02:00
});
2016-08-25 00:52:08 +02:00
} else {
dispatch({
type: ActionTypes.DELETE_FILE,
id,
parentId
});
}
2016-08-03 21:11:59 +02:00
};
}
export function showFolderChildren(id) {
return {
type: ActionTypes.SHOW_FOLDER_CHILDREN,
id
};
}
export function hideFolderChildren(id) {
return {
type: ActionTypes.HIDE_FOLDER_CHILDREN,
id
};
}
export function setBlobUrl(file, blobURL) {
return {
type: ActionTypes.SET_BLOB_URL,
name: file.name,
blobURL
};
}
export function getBlobUrl(file) {
if (file.blobUrl) {
blobUtil.revokeObjectURL(file.blobUrl);
}
const fileBlob = blobUtil.createBlob([file.content], { type: 'text/plain' });
const blobURL = blobUtil.createObjectURL(fileBlob);
return blobURL;
}