Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit be78ffc

Browse files
committed
Add config object with feature flags, translation strings and code size check.
1 parent 41a2090 commit be78ffc

4 files changed

Lines changed: 3435 additions & 43 deletions

File tree

‎editor.html‎

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,9 @@
6969
</head>
7070
<body>
7171
<script id="snippet-template" type="x-tmpl-mustache">
72-
<h2><i class="fa fa-cogs"></i> <strong>Code Snippets</strong></h2>
73-
<p>Code snippets are short blocks of code to re-use in your
74-
own programs. There are snippets for most common things you'll
75-
want to do using MicroPython.</p>
76-
<p>Select one of the snippets below, or type the snippet's
77-
<em>trigger</em> then tap the TAB key.</p>
72+
<h2><i class="fa fa-cogs"></i> <strong>{{ title }}</strong></h2>
73+
<p>{{ description }}</p>
74+
<p>{{ instructions }}</p>
7875

7976
<table class="snippet-table">
8077
<tr>
@@ -91,8 +88,8 @@ <h2><i class="fa fa-cogs"></i> <strong>Code Snippets</strong></h2>
9188

9289
</script>
9390
<script id="share-template" type="x-tmpl-mustache">
94-
<h2><i class="fa fa-share-alt"></i> <strong>Share Code</strong></h2>
95-
<p>This link points to your code:
91+
<h2><i class="fa fa-share-alt"></i> <strong>{{ title }}</strong></h2>
92+
<p>{{ description }}
9693
<a href="#" id="direct-link"></a></p>
9794
<span id="twitter-button"></span>
9895
<iframe id="facebook-button" src="" width="59" height="20" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>
@@ -119,7 +116,7 @@ <h2><i class="fa fa-share-alt"></i> <strong>Share Code</strong></h2>
119116
</div>
120117
<div class="roundlabel">Save</div>
121118
</a>
122-
<a href="#" class="roundbutton" id="command-blockly"
119+
<a href="#" class="roundbutton hidden" id="command-blockly"
123120
tabindex="3"
124121
title="Click to create code with blockly">
125122
<div class="roundsymbol">
@@ -129,39 +126,39 @@ <h2><i class="fa fa-share-alt"></i> <strong>Share Code</strong></h2>
129126
</div>
130127
<div class="roundlabel">Blockly</div>
131128
</a>
132-
<a href="#" class="roundbutton" id="command-snippet"
133-
tabindex="3"
129+
<a href="#" class="roundbutton hidden" id="command-snippet"
130+
tabindex="4"
134131
title="Click to select a snippet (code shortcut)">
135132
<div class="roundsymbol">
136133
<i class="fa fa-cogs"></i>
137134
</div>
138135
<div class="roundlabel">Snippets</div>
139136
</a>
140137
<a href="help.html" target="_blank" class="roundbutton"
141-
id="command-help" tabindex="4"
138+
id="command-help" tabindex="5"
142139
title="Open help for this editor in a new tab">
143140
<div class="roundsymbol">
144141
<i class="fa fa-question"></i>
145142
</div>
146143
<div class="roundlabel">Help</div>
147144
</a>
148-
<a href="#" class="roundbutton"
149-
id="command-share" tabindex="5"
145+
<a href="#" class="roundbutton hidden"
146+
id="command-share" tabindex="6"
150147
title="Create a link to share your script">
151148
<div class="roundsymbol">
152149
<i class="fa fa-share-alt"></i>
153150
</div>
154151
<div class="roundlabel">Share</div>
155152
</a>
156153
<div id="script-icons" class="vbox">
157-
<a class="holder zoomer" tabindex="6" id="zoom-in"
154+
<a class="holder zoomer" tabindex="7" id="zoom-in"
158155
href="#">
159156
<div class="status-icon">
160157
<i class="fa fa-search-plus"
161158
title="Zoom in"></i>
162159
</div>
163160
</a>
164-
<a class="holder zoomer" tabindex="7" id="zoom-out"
161+
<a class="holder zoomer" tabindex="8" id="zoom-out"
165162
href="#">
166163
<div class="status-icon">
167164
<i class="fa fa-search-minus"
@@ -172,10 +169,10 @@ <h2><i class="fa fa-share-alt"></i> <strong>Share Code</strong></h2>
172169
</div>
173170
<div class="vbox" id="script-box">
174171
<div id="script-name" class="flex1" contenteditable
175-
tabindex="8">
172+
tabindex="9">
176173
</div>
177174
<div id="script-description" contenteditable
178-
tabindex="9">
175+
tabindex="10">
179176
</div>
180177
</div>
181178
</div>
@@ -193,6 +190,7 @@ <h2><i class="fa fa-share-alt"></i> <strong>Share Code</strong></h2>
193190
<script src="static/js/FileSaver.min.js" type="application/javascript"></script>
194191
<script src="static/js/vex.combined.min.js"
195192
type="application/javascript"></script>
193+
<script src="static/js/encoding.js" type="application/javascript"></script>
196194
<script src="static/js/mustache.min.js"
197195
type="application/javascript"></script>
198196
<script src="blockly/blockly_compressed.js"></script>
@@ -217,7 +215,60 @@ <h2><i class="fa fa-share-alt"></i> <strong>Share Code</strong></h2>
217215
type="application/javascript"></script>
218216
<script>vex.defaultOptions.className = 'vex-theme-wireframe';</script>
219217
<script type="text/javascript" charset="utf-8" async="" src="https://platform.twitter.com/js/button.c9025956238e7d486d74379e14f50282.js"></script>
220-
218+
<script>
219+
// Call the web_editor function to start the editor running.
220+
web_editor({
221+
'translate': {
222+
'code_snippets': {
223+
'title': 'Code Snippets',
224+
'description': "Code snippets are short blocks of code to re-use in your own programs. There are snippets for most common things you'll want to do using MicroPython.",
225+
'instructions': "Select one of the snippets below, or type the snippet's trigger then tap the TAB key.",
226+
'trigger_heading': 'trigger',
227+
'description_heading': 'description',
228+
'docs': 'create a comment to describe your code',
229+
'wh': 'while some condition is True, keep looping over some code',
230+
'with': 'do some stuff with something assigned to a name',
231+
'cl': 'create a new class that defines the behaviour of a new type of object',
232+
'def': 'define a named function that takes some arguments and optionally add a description',
233+
'if': 'if some condition is True, do something',
234+
'ei': 'else if some other condition is True, do something',
235+
'el': 'else do some other thing',
236+
'for': 'for each item in a collection of items do something with each item',
237+
'try': 'try doing something and handle exceptions (errors)'
238+
},
239+
'share': {
240+
'title': 'Share Code',
241+
'description': 'This link points to your code:',
242+
},
243+
'alerts': {
244+
'download': 'Safari has a bug that means your work will be downloaded as an un-named file. Please rename it to something ending in .hex. Alternatively, use a browser such as Firefox or Chrome. They do not suffer from this bug.',
245+
'save': 'Safari has a bug that means your work will be downloaded as an un-named file. Please rename it to something ending in .py. Alternatively, use a browser such as Firefox or Chrome. They do not suffer from this bug.',
246+
'length': 'Oops! Your script is to long given the limited memory on the device.'
247+
},
248+
'confirms': {
249+
'quit': "Some of your changes have not been saved. Quit anyway?",
250+
'blocks': "You have unsaved code. Using blocks will change your code. You may lose your changes. Do you want to continue?"
251+
},
252+
'code': {
253+
'start': "# Add your Python code here. E.g.\n" +
254+
"from microbit import *\n\n\n" +
255+
"while True:\n" +
256+
" display.scroll('Hello, World!')\n" +
257+
" display.show(Image.HEART)\n" +
258+
" sleep(2000)\n"
259+
},
260+
'drop': {
261+
'python': 'Extracted from a Python file',
262+
'hex': 'Extracted from a hex file'
263+
}
264+
},
265+
'flags': {
266+
'blocks': true,
267+
'snippets': true,
268+
'share': true
269+
},
270+
});
271+
</script>
221272
<xml id="blockly-toolbox" style="display: none">
222273

223274
<category name="Accelerometer" colour="20">

‎python-main.js‎

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
0.0.7
2+
0.0.8
33
44
A simple editor that targets MicroPython for the BBC micro:bit.
55
@@ -89,7 +89,10 @@ function pythonEditor(id) {
8989
for (var i = 0; i < script.length; ++i) {
9090
data[4 + i] = script.charCodeAt(i);
9191
}
92-
// TODO check data.length < 0x2000
92+
// check data.length < 0x2000
93+
if(data.length > 8192) {
94+
throw new RangeError('Too long');
95+
}
9396
// convert to .hex format
9497
var addr = 0x3e000; // magic start address in flash
9598
var chunk = new Uint8Array(5 + 16);
@@ -172,7 +175,7 @@ the editor to the DOM (web-page).
172175
173176
See the comments in-line for more information.
174177
*/
175-
function web_editor() {
178+
function web_editor(config) {
176179

177180
// Indicates if there are unsaved changes to the content of the editor.
178181
var dirty = false;
@@ -227,6 +230,20 @@ function web_editor() {
227230
setFontSize(fontSize);
228231
};
229232

233+
// Checks for feature flags in the config object and shows/hides UI
234+
// elements as required.
235+
function setupFeatureFlags() {
236+
if(config.flags.blocks) {
237+
$("#command-blockly").removeClass('hidden');
238+
}
239+
if(config.flags.snippets) {
240+
$("#command-snippet").removeClass('hidden');
241+
}
242+
if(config.flags.share) {
243+
$("#command-share").removeClass('hidden');
244+
}
245+
};
246+
230247
// This function is called by TouchDevelop to cause the editor to be initialised. It sets things up so the user sees their code or, in the case of a new program, uses some sane defaults.
231248
function setupEditor(message) {
232249
// Setup the Ace editor.
@@ -247,12 +264,7 @@ function web_editor() {
247264
EDITOR.setCode(message.code);
248265
} else {
249266
// A sane default starting point for a new script.
250-
EDITOR.setCode("# Add your Python code here. E.g.\n" +
251-
"from microbit import *\n\n\n" +
252-
"while True:\n" +
253-
" display.scroll('Hello, World!')\n" +
254-
" display.show(Image.HEART)\n" +
255-
" sleep(2000)\n");
267+
EDITOR.setCode(config.translate.code.start);
256268
}
257269
EDITOR.ACE.gotoLine(EDITOR.ACE.session.getLength());
258270
// Configure the zoom related buttons.
@@ -281,7 +293,7 @@ function web_editor() {
281293
// Describes what to do if the user attempts to close the editor without first saving their work.
282294
window.addEventListener("beforeunload", function (e) {
283295
if (dirty) {
284-
var confirmationMessage = "Some of your changes have not been saved. Quit anyway?";
296+
var confirmationMessage = config.translate.confirms.quit;
285297
(e || window.event).returnValue = confirmationMessage;
286298
return confirmationMessage;
287299
}
@@ -309,10 +321,15 @@ function web_editor() {
309321
// This function describes what to do when the download button is clicked.
310322
function doDownload() {
311323
var firmware = $("#firmware").text();
312-
var output = EDITOR.getHexFile(firmware);
324+
try {
325+
var output = EDITOR.getHexFile(firmware);
326+
} catch(e) {
327+
alert(config.translate.alerts.length);
328+
return;
329+
}
313330
var ua = navigator.userAgent.toLowerCase();
314331
if((ua.indexOf('safari/') > -1) && (ua.indexOf('chrome') == -1)) {
315-
alert("Safari has a bug that means your work will be downloaded as an un-named file. Please rename it to something ending in .hex. Alternatively, use a browser such as Firefox or Chrome. They do not suffer from this bug.");
332+
alert(config.translate.alerts.download);
316333
window.open('data:application/octet;charset=utf-8,' + encodeURIComponent(output), '_newtab');
317334
} else {
318335
var filename = getName().replace(" ", "_");
@@ -326,7 +343,7 @@ function web_editor() {
326343
var output = EDITOR.getCode();
327344
var ua = navigator.userAgent.toLowerCase();
328345
if((ua.indexOf('safari/') > -1) && (ua.indexOf('chrome') == -1)) {
329-
alert("Safari has a bug that means your work will be downloaded as an un-named file. Please rename it to something ending in .py. Alternatively, use a browser such as Firefox or Chrome. They do not suffer from this bug.");
346+
alert(config.translate.alerts.save);
330347
window.open('data:application/octet;charset=utf-8,' + encodeURIComponent(output), '_newtab');
331348
} else {
332349
var filename = getName().replace(" ", "_");
@@ -345,8 +362,7 @@ function web_editor() {
345362
editor.ACE.setReadOnly(false);
346363
} else {
347364
if(dirty) {
348-
var msg = "You have unsaved code. Using blocks will change your code. You may lose your changes. Do you want to continue?";
349-
if(!confirm(msg)) {
365+
if(!confirm(config.translate.confirms.blocks)) {
350366
return;
351367
}
352368
}
@@ -375,13 +391,17 @@ function web_editor() {
375391
var template = $('#snippet-template').html();
376392
Mustache.parse(template);
377393
var context = {
394+
'title': config.translate.code_snippets.title,
395+
'description': config.translate.code_snippets.description,
396+
'instructions': config.translate.code_snippets.instructions,
397+
'trigger_heading': config.translate.code_snippets.trigger_heading,
398+
'description_heading': config.translate.code_snippets.description_heading,
378399
'snippets': snippetManager.snippetMap.python,
379400
'describe': function() {
380401
return function(text, render) {
381-
name = render(text);
382-
description = name.substring(name.indexOf(' - '),
383-
name.length);
384-
return description.replace(' - ', '');
402+
var name = render(text);
403+
var trigger = name.split(' - ')[0];
404+
return config.translate.code_snippets[trigger];
385405
}
386406
}
387407
}
@@ -419,7 +439,7 @@ function web_editor() {
419439
}).done(function( data ) {
420440
console.log(data);
421441
vex.open({
422-
content: Mustache.render(template, {})
442+
content: Mustache.render(template, config.translate.share)
423443
})
424444
$('#direct-link').attr('href', data.id);
425445
$('#direct-link').text(data.id);
@@ -438,15 +458,15 @@ function web_editor() {
438458
var reader = new FileReader();
439459
if (ext == 'py') {
440460
setName(file.name.replace('.py', ''));
441-
setDescription('Extracted from a Python file');
461+
setDescription(config.translate.drop.python);
442462
reader.onload = function(e) {
443463
EDITOR.setCode(e.target.result);
444464
}
445465
reader.readAsText(file);
446466
EDITOR.ACE.gotoLine(EDITOR.ACE.session.getLength());
447467
} else if (ext == 'hex') {
448468
setName(file.name.replace('.hex', ''));
449-
setDescription('Extracted from a hex file');
469+
setDescription(config.translate.drop.hex);
450470
reader.onload = function(e) {
451471
var code = EDITOR.extractScript(e.target.result);
452472
if (code.length < 8192) {
@@ -495,9 +515,8 @@ function web_editor() {
495515
return result;
496516
}
497517

518+
setupFeatureFlags();
498519
setupEditor(get_qs_context());
499520
setupButtons();
500521
};
501522

502-
// Call the web_editor function to start the editor running.
503-
web_editor();

0 commit comments

Comments
 (0)