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

219 lines
5.7 KiB
JavaScript
Raw Normal View History

2016-08-24 18:22:10 +00:00
import objectID from 'bson-objectid';
import blobUtil from 'blob-util';
import { reset } from 'redux-form';
import apiClient from '../../../utils/apiClient';
import * as ActionTypes from '../../../constants';
import { setUnsavedChanges, closeNewFolderModal, closeNewFileModal } from './ide';
import { setProjectSavedTime } from './project';
2016-06-22 19:58:23 +00: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(id, content) {
2016-06-23 22:29:55 +00:00
return {
type: ActionTypes.UPDATE_FILE_CONTENT,
id,
2016-06-23 22:29:55 +00:00
content
};
}
export function createFile(formProps) {
return (dispatch, getState) => {
const state = getState();
const { parentId } = state.ide;
if (state.project.id) {
const postParams = {
name: createUniqueName(formProps.name, parentId, state.files),
2016-08-25 15:25:22 +00:00
url: formProps.url,
content: formProps.content || '',
2016-09-02 22:11:27 +00:00
parentId,
children: []
};
apiClient.post(`/projects/${state.project.id}/files`, postParams)
.then((response) => {
dispatch({
type: ActionTypes.CREATE_FILE,
...response.data.updatedFile,
2016-08-30 18:39:37 +00:00
parentId
});
dispatch(setProjectSavedTime(response.data.project.updatedAt));
dispatch(closeNewFileModal());
dispatch(reset('new-file'));
// dispatch({
// type: ActionTypes.HIDE_MODAL
// });
dispatch(setUnsavedChanges(true));
})
2020-04-25 14:48:39 +00:00
.catch((error) => {
const { response } = error;
dispatch({
type: ActionTypes.ERROR,
error: response.data
});
});
} else {
2016-08-24 18:22:10 +00:00
const id = objectID().toHexString();
dispatch({
type: ActionTypes.CREATE_FILE,
name: createUniqueName(formProps.name, parentId, state.files),
2016-08-24 18:22:10 +00:00
id,
_id: id,
2016-08-25 04:18:28 +00:00
url: formProps.url,
content: formProps.content || '',
2016-09-02 22:11:27 +00:00
parentId,
children: []
});
dispatch(reset('new-file'));
// dispatch({
// type: ActionTypes.HIDE_MODAL
// });
dispatch(setUnsavedChanges(true));
dispatch(closeNewFileModal());
}
};
}
2016-08-03 19:11:59 +00:00
2016-08-30 03:23:10 +00:00
export function createFolder(formProps) {
return (dispatch, getState) => {
const state = getState();
const { parentId } = state.ide;
2016-08-30 03:23:10 +00:00
if (state.project.id) {
const postParams = {
name: createUniqueName(formProps.name, parentId, state.files),
2016-08-30 03:23:10 +00:00
content: '',
2016-08-30 18:39:37 +00:00
children: [],
parentId,
2016-08-30 03:23:10 +00:00
fileType: 'folder'
};
apiClient.post(`/projects/${state.project.id}/files`, postParams)
.then((response) => {
2016-08-30 03:23:10 +00:00
dispatch({
type: ActionTypes.CREATE_FILE,
...response.data.updatedFile,
2016-08-30 18:39:37 +00:00
parentId
2016-08-30 03:23:10 +00:00
});
dispatch(setProjectSavedTime(response.data.project.updatedAt));
dispatch(closeNewFolderModal());
2016-08-30 03:23:10 +00:00
})
2020-04-25 14:48:39 +00:00
.catch((error) => {
const { response } = error;
dispatch({
type: ActionTypes.ERROR,
error: response.data
});
});
2016-08-30 03:23:10 +00:00
} else {
const id = objectID().toHexString();
dispatch({
type: ActionTypes.CREATE_FILE,
name: createUniqueName(formProps.name, parentId, state.files),
2016-08-30 03:23:10 +00:00
id,
_id: id,
content: '',
// TODO pass parent id from File Tree
2016-08-30 18:39:37 +00:00
parentId,
fileType: 'folder',
children: []
2016-08-30 03:23:10 +00:00
});
dispatch(closeNewFolderModal());
2016-08-30 03:23:10 +00:00
}
};
}
export function updateFileName(id, name) {
return (dispatch) => {
dispatch(setUnsavedChanges(true));
dispatch({
type: ActionTypes.UPDATE_FILE_NAME,
id,
name
});
2016-08-03 19:11:59 +00:00
};
}
2016-08-24 20:06:28 +00:00
export function deleteFile(id, parentId) {
2016-08-24 21:59:15 +00:00
return (dispatch, getState) => {
const state = getState();
2016-08-24 22:52:08 +00:00
if (state.project.id) {
const deleteConfig = {
params: {
2016-08-24 21:59:15 +00:00
parentId
2016-08-24 22:52:08 +00:00
}
};
apiClient.delete(`/projects/${state.project.id}/files/${id}`, deleteConfig)
.then((response) => {
dispatch(setProjectSavedTime(response.data.project.updatedAt));
2016-08-24 22:52:08 +00:00
dispatch({
type: ActionTypes.DELETE_FILE,
id,
parentId
});
})
2020-04-25 14:48:39 +00:00
.catch((error) => {
const { response } = error;
2016-08-24 22:52:08 +00:00
dispatch({
type: ActionTypes.ERROR,
error: response.data
});
2016-08-24 21:59:15 +00:00
});
2016-08-24 22:52:08 +00:00
} else {
dispatch({
type: ActionTypes.DELETE_FILE,
id,
parentId
});
}
2016-08-03 19:11:59 +00: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,
id: file.id,
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;
}