2016-06-16 20:07:38 +00:00
|
|
|
import mongoose from 'mongoose';
|
|
|
|
const Schema = mongoose.Schema;
|
2016-07-07 17:04:54 +00:00
|
|
|
const ObjectIdSchema = Schema.ObjectId;
|
|
|
|
const ObjectId = mongoose.Types.ObjectId;
|
2016-06-17 20:40:13 +00:00
|
|
|
import shortid from 'shortid';
|
2016-06-16 20:07:38 +00:00
|
|
|
|
2016-07-06 21:29:07 +00:00
|
|
|
const defaultSketch = `function setup() {
|
2016-06-29 16:52:16 +00:00
|
|
|
createCanvas(400, 400);
|
|
|
|
}
|
2016-07-06 21:29:07 +00:00
|
|
|
function draw() {
|
2016-06-29 16:52:16 +00:00
|
|
|
background(220);
|
|
|
|
}`
|
|
|
|
|
2016-07-15 21:23:59 +00:00
|
|
|
const defaultHTML =
|
2016-07-12 01:54:08 +00:00
|
|
|
`<!DOCTYPE html>
|
2016-07-06 19:09:05 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
2016-08-10 19:03:05 +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">
|
2016-07-06 19:09:05 +00:00
|
|
|
</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-06 19:09:05 +00:00
|
|
|
|
2016-06-17 18:11:52 +00:00
|
|
|
const fileSchema = new Schema({
|
2016-06-23 22:29:55 +00:00
|
|
|
name: { type: String, default: 'sketch.js' },
|
2016-08-24 21:59:15 +00:00
|
|
|
content: { type: String, default: '' },
|
2016-08-23 04:10:42 +00:00
|
|
|
url: { type: String },
|
2016-08-24 03:01:20 +00:00
|
|
|
children: { type: [ String ], default: [] },
|
2016-08-25 21:32:27 +00:00
|
|
|
fileType: { type: String, default: 'file' },
|
2016-09-14 19:57:52 +00:00
|
|
|
isSelectedFile: { type: Boolean }
|
2016-07-07 17:04:54 +00:00
|
|
|
}, { timestamps: true, _id: true });
|
2016-06-17 18:11:52 +00:00
|
|
|
|
2016-07-07 17:50:52 +00:00
|
|
|
fileSchema.virtual('id').get(function(){
|
|
|
|
return this._id.toHexString();
|
|
|
|
});
|
|
|
|
|
|
|
|
fileSchema.set('toJSON', {
|
|
|
|
virtuals: true
|
|
|
|
});
|
|
|
|
|
2016-06-16 20:07:38 +00:00
|
|
|
const projectSchema = new Schema({
|
2016-06-23 22:29:55 +00:00
|
|
|
name: { type: String, default: "Hello p5.js, it's the server" },
|
|
|
|
user: { type: Schema.Types.ObjectId, ref: 'User' },
|
2016-08-24 03:01:20 +00:00
|
|
|
files: { type: [ fileSchema ] },
|
2016-08-24 17:09:48 +00:00
|
|
|
_id: { type: String, default: shortid.generate }
|
2016-06-23 22:29:55 +00:00
|
|
|
}, { timestamps: true });
|
2016-06-16 20:07:38 +00:00
|
|
|
|
2016-07-07 17:50:52 +00:00
|
|
|
projectSchema.virtual('id').get(function(){
|
|
|
|
return this._id;
|
|
|
|
});
|
|
|
|
|
|
|
|
projectSchema.set('toJSON', {
|
|
|
|
virtuals: true
|
|
|
|
});
|
|
|
|
|
2016-08-24 17:09:48 +00:00
|
|
|
// projectSchema.pre('save', function createSelectedFile(next) {
|
|
|
|
// const project = this;
|
|
|
|
// if (project.isNew && project.files.length === 0) {
|
|
|
|
// let a = new ObjectId();
|
|
|
|
// let b = new ObjectId();
|
|
|
|
// let c = new ObjectId();
|
2016-09-14 19:57:52 +00:00
|
|
|
// project.files = [{ name: 'sketch.js', content: defaultSketch, _id: a, isSelectedFile: true },
|
2016-08-24 17:09:48 +00:00
|
|
|
// { name: 'index.html', content: defaultHTML, _id: b },
|
|
|
|
// { name: 'style.css', content: defaultCSS, _id: c },
|
|
|
|
// { name: 'root', _id: new ObjectId(), children: [a, b, c] }];
|
|
|
|
// }
|
|
|
|
// return next();
|
|
|
|
// });
|
2016-07-08 18:57:22 +00:00
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
export default mongoose.model('Project', projectSchema);
|