fix really really annoying bug

This commit is contained in:
catarak 2016-08-24 13:09:48 -04:00
parent c8074f1501
commit d08c7c6b94
7 changed files with 39 additions and 58 deletions

View file

@ -29,7 +29,6 @@ export function getProject(id) {
type: ActionTypes.SET_PROJECT, type: ActionTypes.SET_PROJECT,
project: response.data, project: response.data,
files: response.data.files, files: response.data.files,
selectedFile: response.data.selectedFile,
owner: response.data.user owner: response.data.user
}); });
getProjectBlobUrls()(dispatch, getState); getProjectBlobUrls()(dispatch, getState);
@ -85,7 +84,6 @@ export function saveProject() {
name: response.data.name, name: response.data.name,
id: response.data.id, id: response.data.id,
owner: response.data.user, owner: response.data.user,
selectedFile: response.data.selectedFile,
files: response.data.files files: response.data.files
}); });
}) })
@ -108,7 +106,6 @@ export function createProject() {
name: response.data.name, name: response.data.name,
id: response.data.id, id: response.data.id,
owner: response.data.user, owner: response.data.user,
selectedFile: response.data.selectedFile,
files: response.data.files files: response.data.files
}); });
}) })

View file

@ -6,7 +6,6 @@ import { connect } from 'react-redux';
import InlineSVG from 'react-inlinesvg'; import InlineSVG from 'react-inlinesvg';
const downArrowUrl = require('../../../images/down-arrow.svg'); const downArrowUrl = require('../../../images/down-arrow.svg');
import classNames from 'classnames'; import classNames from 'classnames';
import { setSelectedFile } from '../reducers/files';
export class FileNode extends React.Component { export class FileNode extends React.Component {
constructor(props) { constructor(props) {
@ -152,7 +151,7 @@ FileNode.propTypes = {
}; };
function mapStateToProps(state, ownProps) { function mapStateToProps(state, ownProps) {
return setSelectedFile(state.files, state.ide.selectedFile || state.files[1].id).find((file) => file.id === ownProps.id); return state.files.find((file) => file.id === ownProps.id);
} }
function mapDispatchToProps(dispatch) { function mapDispatchToProps(dispatch) {

View file

@ -67,7 +67,7 @@ class Sidebar extends React.Component {
/> />
)} )}
</ul> */ } </ul> */ }
<ConnectedFileNode id={'0'} /> <ConnectedFileNode id={this.props.files.filter(file => file.name === 'root')[0].id} />
</nav> </nav>
); );
} }

View file

@ -16,12 +16,11 @@ import * as ProjectActions from '../actions/project';
import * as EditorAccessibilityActions from '../actions/editorAccessibility'; import * as EditorAccessibilityActions from '../actions/editorAccessibility';
import * as PreferencesActions from '../actions/preferences'; import * as PreferencesActions from '../actions/preferences';
import * as UserActions from '../../User/actions'; import * as UserActions from '../../User/actions';
import { getFile, getHTMLFile, getJSFiles, getCSSFiles, setSelectedFile } from '../reducers/files'; import { getHTMLFile, getJSFiles, getCSSFiles } from '../reducers/files';
import SplitPane from 'react-split-pane'; import SplitPane from 'react-split-pane';
import Overlay from '../../App/components/Overlay'; import Overlay from '../../App/components/Overlay';
import SketchList from '../components/SketchList'; import SketchList from '../components/SketchList';
import About from '../components/About'; import About from '../components/About';
import { setDefaultSelectedFile } from '../reducers/ide';
class IDEView extends React.Component { class IDEView extends React.Component {
constructor(props) { constructor(props) {
@ -376,12 +375,12 @@ IDEView.propTypes = {
function mapStateToProps(state) { function mapStateToProps(state) {
return { return {
files: setSelectedFile(state.files, state.ide.selectedFile), files: state.files,
selectedFile: getFile(state.files, state.ide.selectedFile || state.files[1].id), selectedFile: state.files.filter(file => file.isSelected)[0],
htmlFile: getHTMLFile(state.files), htmlFile: getHTMLFile(state.files),
jsFiles: getJSFiles(state.files), jsFiles: getJSFiles(state.files),
cssFiles: getCSSFiles(state.files), cssFiles: getCSSFiles(state.files),
ide: setDefaultSelectedFile(state.ide, state.files), ide: state.ide,
preferences: state.preferences, preferences: state.preferences,
editorAccessibility: state.editorAccessibility, editorAccessibility: state.editorAccessibility,
user: state.user, user: state.user,

View file

@ -36,26 +36,32 @@ function initialState() {
const a = objectID().toHexString(); const a = objectID().toHexString();
const b = objectID().toHexString(); const b = objectID().toHexString();
const c = objectID().toHexString(); const c = objectID().toHexString();
const r = objectID().toHexString();
return [ return [
{ {
name: 'root', name: 'root',
id: '0', id: r,
_id: r,
children: [a, b, c] children: [a, b, c]
}, },
{ {
name: 'sketch.js', name: 'sketch.js',
content: defaultSketch, content: defaultSketch,
id: a id: a,
_id: a,
isSelected: true
}, },
{ {
name: 'index.html', name: 'index.html',
content: defaultHTML, content: defaultHTML,
id: b id: b,
_id: b
}, },
{ {
name: 'style.css', name: 'style.css',
content: defaultCSS, content: defaultCSS,
id: c id: c,
_id: c
}]; }];
} }
@ -137,20 +143,18 @@ const files = (state, action) => {
return Object.assign({}, file, { isEditingName: false }); return Object.assign({}, file, { isEditingName: false });
}); });
case ActionTypes.SET_SELECTED_FILE:
return state.map(file => {
if (file.id === action.selectedFile) {
return Object.assign({}, file, { isSelected: true });
}
return Object.assign({}, file, { isSelected: false });
});
default: default:
return state; return state;
} }
}; };
export const setSelectedFile = (state, id) =>
state.map(file => {
if (file.id === id) {
return Object.assign({}, file, { isSelected: true });
}
return file;
});
export const getFile = (state, id) => state.filter(file => file.id === id)[0];
export const getHTMLFile = (state) => state.filter(file => file.name.match(/.*\.html$/i))[0]; 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 getJSFiles = (state) => state.filter(file => file.name.match(/.*\.js$/i));
export const getCSSFiles = (state) => state.filter(file => file.name.match(/.*\.css$/i)); export const getCSSFiles = (state) => state.filter(file => file.name.match(/.*\.css$/i));

View file

@ -25,10 +25,6 @@ const ide = (state = initialState, action) => {
return Object.assign({}, state, { isTextOutputPlaying: true }); return Object.assign({}, state, { isTextOutputPlaying: true });
case ActionTypes.STOP_TEXT_OUTPUT: case ActionTypes.STOP_TEXT_OUTPUT:
return Object.assign({}, state, { isTextOutputPlaying: false }); return Object.assign({}, state, { isTextOutputPlaying: false });
case ActionTypes.SET_SELECTED_FILE:
case ActionTypes.SET_PROJECT:
case ActionTypes.NEW_PROJECT:
return Object.assign({}, state, { selectedFile: action.selectedFile });
case ActionTypes.CONSOLE_EVENT: case ActionTypes.CONSOLE_EVENT:
return Object.assign({}, state, { consoleEvent: action.event }); return Object.assign({}, state, { consoleEvent: action.event });
case ActionTypes.SHOW_MODAL: case ActionTypes.SHOW_MODAL:
@ -54,15 +50,4 @@ const ide = (state = initialState, action) => {
} }
}; };
export const setDefaultSelectedFile = (state, files) => {
if (!state.selectedFile) {
files.forEach((file) => {
if (file.name !== 'root') {
state.selectedFile = file.id; // eslint-disable-line
}
});
}
return state;
};
export default ide; export default ide;

View file

@ -38,7 +38,8 @@ const fileSchema = new Schema({
content: { type: String }, content: { type: String },
url: { type: String }, url: { type: String },
children: { type: [ String ], default: [] }, children: { type: [ String ], default: [] },
fileType: { type: String } fileType: { type: String },
isSelected: { type: Boolean }
}, { timestamps: true, _id: true }); }, { timestamps: true, _id: true });
fileSchema.virtual('id').get(function(){ fileSchema.virtual('id').get(function(){
@ -53,8 +54,7 @@ const projectSchema = new Schema({
name: { type: String, default: "Hello p5.js, it's the server" }, name: { type: String, default: "Hello p5.js, it's the server" },
user: { type: Schema.Types.ObjectId, ref: 'User' }, user: { type: Schema.Types.ObjectId, ref: 'User' },
files: { type: [ fileSchema ] }, files: { type: [ fileSchema ] },
_id: { type: String, default: shortid.generate }, _id: { type: String, default: shortid.generate }
selectedFile: Schema.Types.ObjectId
}, { timestamps: true }); }, { timestamps: true });
projectSchema.virtual('id').get(function(){ projectSchema.virtual('id').get(function(){
@ -65,21 +65,18 @@ projectSchema.set('toJSON', {
virtuals: true virtuals: true
}); });
projectSchema.pre('save', function createSelectedFile(next) { // projectSchema.pre('save', function createSelectedFile(next) {
const project = this; // const project = this;
if (!project.selectedFile) { // if (project.isNew && project.files.length === 0) {
project.selectedFile = project.files[0]._id; // eslint-disable-line no-underscore-dangle // let a = new ObjectId();
return next(); // let b = new ObjectId();
} // let c = new ObjectId();
if (project.isNew && project.files.length === 0) { // project.files = [{ name: 'sketch.js', content: defaultSketch, _id: a, isSelected: true },
let a = new ObjectId(); // { name: 'index.html', content: defaultHTML, _id: b },
let b = new ObjectId(); // { name: 'style.css', content: defaultCSS, _id: c },
let c = new ObjectId(); // { name: 'root', _id: new ObjectId(), children: [a, b, c] }];
project.files = [{ name: 'sketch.js', content: defaultSketch, _id: a }, // }
{ name: 'index.html', content: defaultHTML, _id: b }, // return next();
{ name: 'style.css', content: defaultCSS, _id: c }, // });
{ name: 'root', _id: '0', children: [a, b, c] }];
}
});
export default mongoose.model('Project', projectSchema); export default mongoose.model('Project', projectSchema);