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

145 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-06-22 19:58:23 +00:00
import * as ActionTypes from '../../../constants';
2016-05-05 21:48:26 +00:00
const defaultSketch = `function setup() {
2016-06-23 22:29:55 +00:00
createCanvas(400, 400);
2016-05-09 22:28:38 +00:00
}
function draw() {
2016-06-23 22:29:55 +00:00
background(220);
}`;
const defaultHTML =
2016-07-12 01:54:08 +00:00
`<!DOCTYPE html>
<html>
<head>
2016-07-12 15:13:09 +00:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.min.js"></script>
2016-07-12 01:54:08 +00:00
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<script src="sketch.js"></script>
</body>
</html>
`;
2016-07-12 01:54:08 +00:00
const defaultCSS =
`html, body {
overflow: hidden;
margin: 0;
padding: 0;
}
`;
2016-07-08 18:57:22 +00:00
// if the project has never been saved,
const initialState = [
{
name: 'sketch.js',
2016-07-08 18:57:22 +00:00
content: defaultSketch,
id: '1'
},
{
name: 'index.html',
2016-07-08 18:57:22 +00:00
content: defaultHTML,
id: '2'
2016-07-12 01:54:08 +00:00
},
{
name: 'style.css',
content: defaultCSS,
id: '3'
2016-08-23 04:10:42 +00:00
},
{
name: 'root',
id: '0',
children: ['1', '2', '3']
}];
2016-05-05 21:48:26 +00:00
const files = (state = initialState, action) => {
2016-06-23 22:29:55 +00:00
switch (action.type) {
case ActionTypes.UPDATE_FILE_CONTENT:
return state.map(file => {
if (file.name !== action.name) {
return file;
}
return Object.assign({}, file, { content: action.content });
});
2016-07-19 22:26:46 +00:00
case ActionTypes.SET_BLOB_URL:
return state.map(file => {
if (file.name !== action.name) {
return file;
}
return Object.assign({}, file, { blobURL: action.blobURL });
});
2016-06-23 22:29:55 +00:00
case ActionTypes.NEW_PROJECT:
return [...action.files];
2016-06-29 16:52:16 +00:00
case ActionTypes.SET_PROJECT:
return [...action.files];
case ActionTypes.RESET_PROJECT:
return initialState;
case ActionTypes.CREATE_FILE:
2016-08-25 04:18:28 +00:00
return [...state, { name: action.name, id: action.id, content: action.content, url: action.url }];
2016-08-03 19:11:59 +00:00
case ActionTypes.SHOW_FILE_OPTIONS:
return state.map(file => {
if (file.id !== action.id) {
return file;
}
return Object.assign({}, file, { isOptionsOpen: true });
});
case ActionTypes.HIDE_FILE_OPTIONS:
return state.map(file => {
if (file.id !== action.id) {
return file;
}
return Object.assign({}, file, { isOptionsOpen: false });
});
case ActionTypes.UPDATE_FILE_NAME:
return state.map(file => {
if (file.id !== action.id) {
return file;
}
return Object.assign({}, file, { name: action.name });
});
case ActionTypes.DELETE_FILE:
return state.filter(file => file.id !== action.id);
2016-08-03 21:10:03 +00:00
case ActionTypes.SHOW_EDIT_FILE_NAME:
return state.map(file => {
if (file.id !== action.id) {
return file;
}
return Object.assign({}, file, { isEditingName: true });
});
case ActionTypes.HIDE_EDIT_FILE_NAME:
return state.map(file => {
if (file.id !== action.id) {
return file;
}
return Object.assign({}, file, { isEditingName: false });
});
2016-06-23 22:29:55 +00:00
default:
return state;
}
};
2016-05-05 21:48:26 +00:00
2016-08-03 19:11:59 +00:00
export const setSelectedFile = (state, id) =>
state.map(file => {
if (file.id === id) {
return Object.assign({}, file, { isSelected: true });
}
return file;
});
2016-07-08 18:57:22 +00:00
export const getFile = (state, id) => state.filter(file => file.id === id)[0];
2016-08-25 16:39:36 +00:00
export const getHTMLFile = (state) => state.filter(file => file.name.match(/.*\.html$/i))[0];
export const getJSFiles = (state) => state.filter(file => file.name.match(/.*\.js$/i));
export const getCSSFiles = (state) => state.filter(file => file.name.match(/.*\.css$/i));
2016-07-20 01:36:21 +00:00
export const getLinkedFiles = (state) => state.filter(file => file.url);
2016-07-08 18:57:22 +00:00
export default files;