Skip to content

Commit 6b54832

Browse files
committed
Improved file loading and renderer.
1 parent de78056 commit 6b54832

File tree

30 files changed

+373
-316
lines changed

30 files changed

+373
-316
lines changed

src/cache/cache.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/**
2+
* @fileoverview Cache for the Coding with Chrome editor.
3+
*
4+
* @license Copyright 2018 The Coding with Chrome Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @author [email protected] (Markus Bordihn)
19+
*/
20+
goog.provide('cwc.Cache');
21+
22+
goog.require('cwc.framework.External');
23+
goog.require('cwc.framework.Internal');
24+
goog.require('cwc.framework.StyleSheet');
25+
goog.require('cwc.renderer.Helper');
26+
goog.require('cwc.utils.Database');
27+
goog.require('cwc.utils.Logger');
28+
goog.require('cwc.utils.Resources');
29+
30+
31+
/**
32+
* @param {!cwc.utils.Helper} helper
33+
* @constructor
34+
* @struct
35+
* @final
36+
*/
37+
cwc.Cache = function(helper) {
38+
/** @type {string} */
39+
this.name = 'Cache';
40+
41+
/** @type {!cwc.utils.Helper} */
42+
this.helper = helper;
43+
44+
/** @type {!cwc.renderer.Helper} */
45+
this.rendererHelper = new cwc.renderer.Helper();
46+
47+
/** @private {cwc.utils.Database} */
48+
this.cache_ = new cwc.utils.Database(this.name);
49+
50+
/** @private {!cwc.utils.Logger} */
51+
this.log_ = new cwc.utils.Logger(this.name);
52+
53+
/** @private {!string} */
54+
this.version_ = '2';
55+
};
56+
57+
58+
/**
59+
* @return {Promise}
60+
*/
61+
cwc.Cache.prototype.prepare = function() {
62+
return this.cache_.open().then(() => {
63+
this.cache_.getFile('__version__').then((version) => {
64+
this.update(version);
65+
});
66+
});
67+
};
68+
69+
70+
/**
71+
* @param {!string|number} version
72+
*/
73+
cwc.Cache.prototype.update = function(version) {
74+
if (this.version_ >= version) {
75+
this.log_.info('No need for updates ...');
76+
}
77+
this.log_.info('Updating Cache to version', this.version_);
78+
79+
this.log_.info('Loading external frameworks ...');
80+
this.loadFiles(cwc.framework.External);
81+
82+
this.log_.info('Loading internal frameworks ...');
83+
this.loadFiles(cwc.framework.Internal);
84+
85+
this.log_.info('Loading Style Sheets ...');
86+
this.loadFiles(cwc.framework.StyleSheet);
87+
88+
this.cache_.addFile('__version__', this.version_);
89+
};
90+
91+
92+
/**
93+
* Loads files into cache.
94+
* @param {!Object} files
95+
*/
96+
cwc.Cache.prototype.loadFiles = function(files) {
97+
let fileFiles = [];
98+
for (let file of Object.keys(files)) {
99+
if (goog.isString(files[file])) {
100+
fileFiles.push(files[file]);
101+
} else {
102+
for (let subFile of Object.keys(files[file])) {
103+
fileFiles.push(files[file][subFile]);
104+
}
105+
}
106+
}
107+
fileFiles.forEach((file) => {
108+
cwc.utils.Resources.getUriAsText('../' + file).then((content) => {
109+
this.addFile(file, content);
110+
});
111+
});
112+
};
113+
114+
115+
/**
116+
* @param {string!} name
117+
* @param {string!} content
118+
* @param {boolean=} optimize
119+
*/
120+
cwc.Cache.prototype.addFile = function(name, content, optimize = false) {
121+
if (!content) {
122+
this.log_.error('Received empty content for', name);
123+
return;
124+
}
125+
126+
// Add file to server instance if available.
127+
let serverInstance = this.helper.getInstance('server');
128+
if (serverInstance) {
129+
serverInstance.addFile(name, content);
130+
}
131+
132+
if (optimize && !name.includes('.min.') && content.length > 1000) {
133+
// Try to optimize unminimized code by removing comments and white-spaces.
134+
let originalContentLength = content.length;
135+
content = content.replace(/\\n\\n/g, '\\n')
136+
.replace(/ {4}/g, ' ')
137+
.replace(/[ \t]?\/\/.+?\\n/g, '')
138+
.replace(/[ \t]?\/\*.+?\*\/\\n/g, '')
139+
.replace(/[ \t]+\\n/g, '\\n')
140+
.replace(/(\\n){2,}/g, '\\n');
141+
if (originalContentLength > content.length) {
142+
let optimized = Math.ceil(((originalContentLength - content.length) /
143+
originalContentLength) * 100);
144+
if (optimized >= 5) {
145+
this.log_.info('Optimized content from', originalContentLength, 'to',
146+
content.length, 'by', optimized, '%');
147+
}
148+
}
149+
}
150+
let mimeType = cwc.file.getMimeTypeByExtension(name);
151+
let fileContent = this.rendererHelper.getDataUrl(content, mimeType);
152+
if (!fileContent) {
153+
this.log_.error('Received empty file for', name);
154+
return;
155+
}
156+
this.cache_.addFile(name, fileContent);
157+
};
158+
159+
160+
/**
161+
* @param {string} name
162+
* @return {Promise}
163+
*/
164+
cwc.Cache.prototype.getFile = function(name) {
165+
return this.cache_.getFile(name);
166+
};

src/mode/modder.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ cwc.mode.Modder.prototype.setMode = function(mode) {
115115
guiInstance.showOverlay(false);
116116
}
117117

118+
// Close existing dialogs.
119+
let dialogInstance = this.helper.getInstance('dialog');
120+
if (dialogInstance) {
121+
dialogInstance.close();
122+
}
123+
118124
this.log_.info('Initialize mode and decorate UI for', mode, '…');
119125
this.mode = mode;
120126
this.modder = modeConfig.getMod(this.helper);

src/protocol/low-level/tcp/http_server.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -341,17 +341,7 @@ cwc.protocol.tcp.HTTPServer.prototype.handleRecieve_ = function(receiveInfo) {
341341
this.getFile(requestPath).then((content) => {
342342
if (content !== undefined) {
343343
// File found handling
344-
let contentType = 'text/plain';
345-
if (requestPath.endsWith('.js')) {
346-
contentType = 'text/javascript';
347-
} else if (requestPath.endsWith('.css')) {
348-
contentType = 'text/css';
349-
} else if (requestPath.endsWith('.py')) {
350-
contentType = 'text/x-python';
351-
} else if (requestPath.endsWith('.html') ||
352-
requestPath.endsWith('.htm')) {
353-
contentType = 'text/html';
354-
}
344+
let contentType = cwc.file.getMimeTypeByExtension(requestPath);
355345
this.send200Response(socketId, content, contentType);
356346
} else {
357347
// File not found handling

src/renderer/external/arduino.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,27 @@ cwc.renderer.external.Arduino.prototype.init = function() {
5050

5151
/**
5252
* Arduino render logic.
53-
* @param {Object} editor_content
54-
* @param {Object} editor_flags
53+
* @param {Object} editorContent
5554
* @param {cwc.file.Files} libraryFiles
5655
* @param {!cwc.file.Files} frameworks
57-
* @param {!cwc.file.Files} styleSheets
58-
* @param {cwc.renderer.Helper} renderer_helper
56+
* @param {cwc.renderer.Helper} rendererHelper
5957
* @return {string}
6058
* @export
6159
*/
6260
cwc.renderer.external.Arduino.prototype.render = function(
63-
editor_content,
64-
editor_flags,
61+
editorContent,
6562
libraryFiles,
6663
frameworks,
67-
styleSheets,
68-
renderer_helper) {
69-
let header = renderer_helper.getFrameworkHeader(
64+
rendererHelper) {
65+
let header = rendererHelper.getFrameworkHeader(
7066
/** @type {string} */ (cwc.framework.Internal.ARDUINO), frameworks);
7167
let body = '\n<script>' +
7268
' let customCode = function(arduino) {\n' +
73-
editor_content[cwc.ui.EditorContent.DEFAULT] +
69+
editorContent[cwc.ui.EditorContent.DEFAULT] +
7470
'\n};\n' + ' let runner = new cwc.framework.Runner();\n' +
7571
' let customFramework = new cwc.framework.Arduino(runner);\n' +
7672
' customFramework.listen(customCode);\n' +
7773
'</script>\n';
7874

79-
return renderer_helper.getHTML(body, header);
75+
return rendererHelper.getHTML(body, header);
8076
};

src/renderer/external/ev3.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,26 @@ cwc.renderer.external.EV3.prototype.init = function() {
4949

5050

5151
/**
52-
* @param {Object} editor_content
53-
* @param {Object} editor_flags
52+
* @param {Object} editorContent
5453
* @param {cwc.file.Files} libraryFiles
5554
* @param {!cwc.file.Files} frameworks
56-
* @param {!cwc.file.Files} styleSheets
57-
* @param {cwc.renderer.Helper} renderer_helper
55+
* @param {cwc.renderer.Helper} rendererHelper
5856
* @return {string}
5957
* @export
6058
*/
6159
cwc.renderer.external.EV3.prototype.render = function(
62-
editor_content,
63-
editor_flags,
60+
editorContent,
6461
libraryFiles,
6562
frameworks,
66-
styleSheets,
67-
renderer_helper) {
68-
let header = renderer_helper.getFrameworkHeader(
63+
rendererHelper) {
64+
let header = rendererHelper.getFrameworkHeader(
6965
/** @type {string} */ (cwc.framework.Internal.EV3), frameworks);
7066
let body = '\n<script>' +
7167
' let code = function(ev3) {\n' +
72-
editor_content[cwc.ui.EditorContent.JAVASCRIPT] +
68+
editorContent[cwc.ui.EditorContent.JAVASCRIPT] +
7369
'\n};\n' +
7470
' new cwc.framework.Ev3(code);\n' +
7571
'</script>\n';
7672

77-
return renderer_helper.getHTML(body, header);
73+
return rendererHelper.getHTML(body, header);
7874
};

src/renderer/external/makeblock/mbot.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,27 @@ cwc.renderer.external.makeblock.MBot.prototype.init = function() {
4949

5050

5151
/**
52-
* @param {Object} editor_content
53-
* @param {Object} editor_flags
52+
* @param {Object} editorContent
5453
* @param {cwc.file.Files} libraryFiles
5554
* @param {!cwc.file.Files} frameworks
56-
* @param {!cwc.file.Files} styleSheets
57-
* @param {cwc.renderer.Helper} renderer_helper
55+
* @param {cwc.renderer.Helper} rendererHelper
5856
* @return {string}
5957
* @export
6058
*/
6159
cwc.renderer.external.makeblock.MBot.prototype.render = function(
62-
editor_content,
63-
editor_flags,
60+
editorContent,
6461
libraryFiles,
6562
frameworks,
66-
styleSheets,
67-
renderer_helper) {
68-
let header = renderer_helper.getFrameworkHeader(
63+
rendererHelper) {
64+
let header = rendererHelper.getFrameworkHeader(
6965
/** @type {string} */ (cwc.framework.Internal.MBOT), frameworks);
7066
let body = '\n<script>' +
7167
' let code = function(mbot) {\n' +
72-
editor_content[cwc.ui.EditorContent.JAVASCRIPT] +
68+
editorContent[cwc.ui.EditorContent.JAVASCRIPT] +
7369
'\n};\n'+
7470
' new cwc.framework.makeblock.mBot(code);\n' +
7571
'</script>\n';
7672

77-
let html = renderer_helper.getHTML(body, header);
73+
let html = rendererHelper.getHTML(body, header);
7874
return html;
7975
};

src/renderer/external/makeblock/mbotRanger.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,25 @@ cwc.renderer.external.makeblock.MBotRanger.prototype.init = function() {
4949

5050

5151
/**
52-
* @param {Object} editor_content
53-
* @param {Object} editor_flags
52+
* @param {Object} editorContent
5453
* @param {cwc.file.Files} libraryFiles
5554
* @param {!cwc.file.Files} frameworks
56-
* @param {!cwc.file.Files} styleSheets
57-
* @param {cwc.renderer.Helper} renderer_helper
55+
* @param {cwc.renderer.Helper} rendererHelper
5856
* @return {string}
5957
* @export
6058
*/
6159
cwc.renderer.external.makeblock.MBotRanger.prototype.render = function(
62-
editor_content,
63-
editor_flags,
60+
editorContent,
6461
libraryFiles,
6562
frameworks,
66-
styleSheets,
67-
renderer_helper) {
68-
let header = renderer_helper.getFrameworkHeader(
63+
rendererHelper) {
64+
let header = rendererHelper.getFrameworkHeader(
6965
/** @type {string} */ (cwc.framework.Internal.MBOT_RANGER), frameworks);
70-
let content = editor_content[cwc.ui.EditorContent.JAVASCRIPT];
66+
let content = editorContent[cwc.ui.EditorContent.JAVASCRIPT];
7167
let body = '\n<script>' +
7268
' let code = function(mBotRanger) {\n' + content + '\n};\n'+
7369
' new cwc.framework.makeblock.mBotRanger(code);\n' +
7470
'</script>\n';
75-
let html = renderer_helper.getHTML(body, header);
71+
let html = rendererHelper.getHTML(body, header);
7672
return html;
7773
};

src/renderer/external/pencil_code.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,25 @@ cwc.renderer.external.PencilCode.prototype.init = function() {
4949

5050

5151
/**
52-
* @param {Object} editor_content
53-
* @param {Object} editor_flags
52+
* @param {Object} editorContent
5453
* @param {!cwc.file.Files} libraryFiles
5554
* @param {!cwc.file.Files} frameworks
56-
* @param {!cwc.file.Files} styleSheets
57-
* @param {cwc.renderer.Helper} renderer_helper
55+
* @param {cwc.renderer.Helper} rendererHelper
5856
* @return {!string}
5957
* @export
6058
*/
6159
cwc.renderer.external.PencilCode.prototype.render = function(
62-
editor_content,
63-
editor_flags,
60+
editorContent,
6461
libraryFiles,
6562
frameworks,
66-
styleSheets,
67-
renderer_helper) {
68-
let header = renderer_helper.getFrameworkHeaders([
63+
rendererHelper) {
64+
let header = rendererHelper.getFrameworkHeaders([
6965
cwc.framework.External.COFFEESCRIPT,
7066
cwc.framework.External.JQUERY.V2_2_4,
7167
cwc.framework.External.JQUERY_TURTLE,
7268
], frameworks);
7369
let body = '\n<script type="text/coffeescript">\n' +
74-
'$.turtle();\n' + editor_content[cwc.ui.EditorContent.COFFEESCRIPT] +
70+
'$.turtle();\n' + editorContent[cwc.ui.EditorContent.COFFEESCRIPT] +
7571
'\n</script>\n';
76-
return renderer_helper.getHTMLGrid(body, header);
72+
return rendererHelper.getHTMLGrid(body, header);
7773
};

0 commit comments

Comments
 (0)