sumorobot-web/assets/js/ace.js

72 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-08-05 08:53:42 +00:00
// Where Python code is written
var codingEditor = null;
// Where Blockly is converted to Python
var readOnlyCodingEditor = null;
2018-08-18 17:12:05 +00:00
// Range for ace editor line highlighting
var Range = ace.require('ace/range').Range;
2018-08-05 08:53:42 +00:00
window.addEventListener('load', function() {
// Load read only ace editor
readOnlyCodingEditor = ace.edit('readOnlyBlocklyCode');
// Set the style
readOnlyCodingEditor.setTheme('ace/theme/textmate');
readOnlyCodingEditor.session.setMode('ace/mode/python');
readOnlyCodingEditor.session.setTabSize(2);
// Make as read only
readOnlyCodingEditor.setReadOnly(true);
// Disable scrolling warning
readOnlyCodingEditor.$blockScrolling = Infinity;
// Load ace editor
codingEditor = ace.edit('blocklyCode');
// Set the style
codingEditor.setTheme('ace/theme/textmate');
codingEditor.session.setMode('ace/mode/python');
codingEditor.session.setTabSize(2);
// Disable scrolling warning
codingEditor.$blockScrolling = Infinity;
// Enable autocomplete
ace.require('ace/ext/language_tools');
codingEditor.setOptions({
enableSnippets: true,
enableLiveAutocompletion: true,
enableBasicAutocompletion: true
});
// Add autocomplete keywords
codingEditor.completers.push({
getCompletions: function(editor, session, pos, prefix, callback) {
callback(null, [
{value: 'STOP', score: 1000, meta: 'sumorobot'},
{value: 'LEFT', score: 1000, meta: 'sumorobot'},
{value: 'RIGHT', score: 1000, meta: 'sumorobot'},
{value: 'SEARCH', score: 1000, meta: 'sumorobot'},
{value: 'FORWARD', score: 1000, meta: 'sumorobot'},
{value: 'BACKWARD', score: 1000, meta: 'sumorobot'},
{value: 'STATUS', score: 1000, meta: 'sumorobot'},
{value: 'LEFT_LINE', score: 1000, meta: 'sumorobot'},
{value: 'RIGHT_LINE', score: 1000, meta: 'sumorobot'},
{value: 'sumorobot', score: 1000, meta: 'sumorobot'},
{value: 'move', score: 1000, meta: 'sumorobot'},
{value: 'sleep', score: 1000, meta: 'sumorobot'},
{value: 'set_led', score: 1000, meta: 'sumorobot'},
{value: 'is_line', score: 1000, meta: 'sumorobot'},
{value: 'get_line', score: 1000, meta: 'sumorobot'},
{value: 'set_servo', score: 1000, meta: 'sumorobot'},
{value: 'is_opponent', score: 1000, meta: 'sumorobot'},
{value: 'calibrate_line', score: 1000, meta: 'sumorobot'},
{value: 'get_battery_voltage', score: 1000, meta: 'sumorobot'},
{value: 'get_opponent_distance', score: 1000, meta: 'sumorobot'}
]);
}
});
// Set the code to the saved code from local storage or empty
codingEditor.setValue(getLocalStorageItem('sumorobot.code') || '');
// Clear the selection after setting the value
codingEditor.clearSelection();
// Add an change listener for the code editor
codingEditor.on('change', function() {
// When change occurs, save the new code to the localstorage
setLocalStorageItem('sumorobot.code', codingEditor.getValue())
});
});