Skip to content

Commit

Permalink
Add keyboard command
Browse files Browse the repository at this point in the history
Check for status code
  • Loading branch information
erjanmx committed Mar 6, 2018
1 parent f7e6829 commit f082b1b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/resources/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,36 @@ editor.on('change', function () {
localStorage.setItem('live-tinker', editor.getValue());
});

// Add keyboard commands
editor.commands.addCommand({
name: 'runAll',
bindKey: {win: 'Ctrl-Enter', mac: 'Command-Enter'},
exec: function(editor) {
sendCode(editor.getValue())
}
});
editor.commands.addCommand({
name: 'runSelected',
bindKey: {win: 'Ctrl-Shift-Enter', mac: 'Command-Shift-Enter'},
exec: function(editor) {
sendCode(editor.getSelectedText())
}
});

function sendCode(text) {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/live-tinker/ajax');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
document.Response.document.body.innerHTML = xhr.responseText;
var response = xhr.responseText;
if (xhr.status === 200) {
response = '<pre>' + response + '</pre>';
}

document.Response.document.body.innerHTML = response;
changeButtonsState(false);
};
xhr.send('code=' + encodeURIComponent(text));
xhr.send('c=' + encodeURIComponent(text));

changeButtonsState(true);
document.Response.document.body.innerHTML = '';
Expand Down

0 comments on commit f082b1b

Please sign in to comment.