Skip to content

Commit f640da8

Browse files
committed
Initialize files with path, update console errors to use file path
1 parent d01c905 commit f640da8

File tree

5 files changed

+66
-39
lines changed

5 files changed

+66
-39
lines changed

client/modules/IDE/actions/ide.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ export function startSketch() {
252252
return (dispatch, getState) => {
253253
dispatch(clearConsole());
254254
dispatch(startVisualSketch());
255+
dispatch(showRuntimeErrorWarning());
255256
const state = getState();
256257
dispatchMessage({
257258
type: MessageTypes.SKETCH,

client/modules/IDE/components/Editor.jsx

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ class Editor extends React.Component {
115115
styleSelectedText: true,
116116
lint: {
117117
onUpdateLinting: (annotations) => {
118-
this.props.hideRuntimeErrorWarning();
119118
this.updateLintingMessageAccessibility(annotations);
120119
},
121120
options: {
@@ -159,6 +158,7 @@ class Editor extends React.Component {
159158
'change',
160159
debounce(() => {
161160
this.props.setUnsavedChanges(true);
161+
this.props.hideRuntimeErrorWarning();
162162
this.props.updateFileContent(this.props.file.id, this._cm.getValue());
163163
if (this.props.autorefresh && this.props.isPlaying) {
164164
this.props.clearConsole();
@@ -246,12 +246,6 @@ class Editor extends React.Component {
246246
);
247247
}
248248

249-
if (prevProps.consoleEvents !== this.props.consoleEvents) {
250-
this.props.showRuntimeErrorWarning();
251-
}
252-
for (let i = 0; i < this._cm.lineCount(); i += 1) {
253-
this._cm.removeLineClass(i, 'background', 'line-runtime-error');
254-
}
255249
if (this.props.runtimeErrorWarningVisible) {
256250
this.props.consoleEvents.forEach((consoleEvent) => {
257251
if (consoleEvent.method === 'error') {
@@ -261,26 +255,24 @@ class Editor extends React.Component {
261255
consoleEvent.data[0].indexOf &&
262256
consoleEvent.data[0].indexOf(')') > -1
263257
) {
264-
const n = consoleEvent.data[0].replace(')', '').split(' ');
265-
const lineNumber = parseInt(n[n.length - 1], 10) - 1;
266-
const { source } = consoleEvent;
267-
const fileName = this.props.file.name;
268-
const errorFromJavaScriptFile = `${source}.js` === fileName;
269-
const errorFromIndexHTML =
270-
source === fileName && fileName === 'index.html';
271-
if (
272-
!Number.isNaN(lineNumber) &&
273-
(errorFromJavaScriptFile || errorFromIndexHTML)
274-
) {
275-
this._cm.addLineClass(
276-
lineNumber,
277-
'background',
278-
'line-runtime-error'
279-
);
280-
}
258+
const sourceAndLoc = consoleEvent.data[0]
259+
.split('\n')[1]
260+
.split('(')[1]
261+
.split(')')[0];
262+
const [source, line, column] = sourceAndLoc.split(':');
263+
const lineNumber = parseInt(line, 10) - 1;
264+
this._cm.addLineClass(
265+
lineNumber,
266+
'background',
267+
'line-runtime-error'
268+
);
281269
}
282270
}
283271
});
272+
} else {
273+
for (let i = 0; i < this._cm.lineCount(); i += 1) {
274+
this._cm.removeLineClass(i, 'background', 'line-runtime-error');
275+
}
284276
}
285277
}
286278

@@ -440,7 +432,7 @@ Editor.propTypes = {
440432
method: PropTypes.string.isRequired,
441433
args: PropTypes.arrayOf(PropTypes.string)
442434
})
443-
),
435+
).isRequired,
444436
updateLintMessage: PropTypes.func.isRequired,
445437
clearLintMessage: PropTypes.func.isRequired,
446438
updateFileContent: PropTypes.func.isRequired,
@@ -471,17 +463,13 @@ Editor.propTypes = {
471463
expandSidebar: PropTypes.func.isRequired,
472464
isUserOwner: PropTypes.bool.isRequired,
473465
clearConsole: PropTypes.func.isRequired,
474-
showRuntimeErrorWarning: PropTypes.func.isRequired,
466+
// showRuntimeErrorWarning: PropTypes.func.isRequired,
475467
hideRuntimeErrorWarning: PropTypes.func.isRequired,
476468
runtimeErrorWarningVisible: PropTypes.bool.isRequired,
477469
provideController: PropTypes.func.isRequired,
478470
t: PropTypes.func.isRequired
479471
};
480472

481-
Editor.defaultProps = {
482-
consoleEvents: []
483-
};
484-
485473
function mapStateToProps(state) {
486474
return {
487475
files: state.files,
@@ -496,7 +484,7 @@ function mapStateToProps(state) {
496484
user: state.user,
497485
project: state.project,
498486
toast: state.toast,
499-
console: state.console,
487+
consoleEvents: state.console,
500488

501489
...state.preferences,
502490
...state.ide,

client/modules/IDE/reducers/files.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,26 @@ const initialState = () => {
5454
_id: a,
5555
isSelectedFile: true,
5656
fileType: 'file',
57-
children: []
57+
children: [],
58+
filePath: ''
5859
},
5960
{
6061
name: 'index.html',
6162
content: defaultHTML,
6263
id: b,
6364
_id: b,
6465
fileType: 'file',
65-
children: []
66+
children: [],
67+
filePath: ''
6668
},
6769
{
6870
name: 'style.css',
6971
content: defaultCSS,
7072
id: c,
7173
_id: c,
7274
fileType: 'file',
73-
children: []
75+
children: [],
76+
filePath: ''
7477
}
7578
];
7679
};
@@ -141,6 +144,26 @@ function renameFile(state, action) {
141144
});
142145
}
143146

147+
function setFilePath(files, fileId, path) {
148+
const file = files.find((f) => f.id === fileId);
149+
file.filePath = path;
150+
const newPath = `${path}${path.length > 0 ? '/' : ''}${file.name}`;
151+
if (file.children.length === 0) return;
152+
file.children.forEach((childFileId) => {
153+
setFilePath(files, childFileId, newPath);
154+
});
155+
}
156+
157+
function setFilePaths(files) {
158+
const updatedFiles = [...files];
159+
const rootPath = '';
160+
const rootFile = files.find((f) => f.name === 'root');
161+
rootFile.children.forEach((fileId) => {
162+
setFilePath(updatedFiles, fileId, rootPath);
163+
});
164+
return updatedFiles;
165+
}
166+
144167
const files = (state, action) => {
145168
if (state === undefined) {
146169
state = initialState(); // eslint-disable-line
@@ -162,12 +185,18 @@ const files = (state, action) => {
162185
return Object.assign({}, file, { blobURL: action.blobURL });
163186
});
164187
case ActionTypes.NEW_PROJECT:
165-
return [...action.files];
188+
return setFilePaths(action.files);
166189
case ActionTypes.SET_PROJECT:
167-
return [...action.files];
190+
return setFilePaths(action.files);
168191
case ActionTypes.RESET_PROJECT:
169192
return initialState();
170193
case ActionTypes.CREATE_FILE: {
194+
const parentFile = state.find((file) => file.id === action.parentId);
195+
const filePath =
196+
parentFile.name === 'root'
197+
? ''
198+
: `${parentFile.filePath}${parentFile.filePath.length > 0 ? '/' : ''}
199+
${parentFile.name}`;
171200
const newState = [
172201
...updateParent(state, action),
173202
{
@@ -177,7 +206,8 @@ const files = (state, action) => {
177206
content: action.content,
178207
url: action.url,
179208
children: action.children,
180-
fileType: action.fileType || 'file'
209+
fileType: action.fileType || 'file',
210+
filePath
181211
}
182212
];
183213
return newState.map((file) => {
@@ -189,6 +219,12 @@ const files = (state, action) => {
189219
}
190220
case ActionTypes.UPDATE_FILE_NAME: {
191221
const newState = renameFile(state, action);
222+
const updatedFile = newState.find((file) => file.id === action.id);
223+
const childPath = `${updatedFile.filePath}
224+
${updatedFile.filePath.length > 0 ? '/' : ''}${updatedFile.name}`;
225+
updatedFile.children.forEach((childId) => {
226+
setFilePath(newState, action.id, childPath);
227+
});
192228
return newState.map((file) => {
193229
if (file.children.includes(action.id)) {
194230
file.children = sortedChildrenId(newState, file.children);

client/modules/IDE/reducers/ide.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const initialState = {
2222
justOpenedProject: false,
2323
previousPath: '/',
2424
errorType: undefined,
25-
runtimeErrorWarningVisible: true,
25+
runtimeErrorWarningVisible: false,
2626
parentId: undefined
2727
};
2828

client/modules/Preview/EmbedFrame.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ function resolveScripts(sketchDoc, files) {
133133
const blobUrl = createBlobUrl(resolvedFile);
134134
script.setAttribute('src', blobUrl);
135135
const blobPath = blobUrl.split('/').pop();
136-
objectUrls[blobUrl] = resolvedFile.name;
136+
objectUrls[blobUrl] = `${resolvedFile.filePath}${
137+
resolvedFile.filePath.length > 0 ? '/' : ''
138+
}${resolvedFile.name}`;
137139
objectPaths[blobPath] = resolvedFile.name;
138140
// script.setAttribute('data-tag', `${startTag}${resolvedFile.name}`);
139141
// script.removeAttribute('src');

0 commit comments

Comments
 (0)