mirror of
https://github.com/inexorabletash/jsbasic.git
synced 2025-02-18 18:30:53 +00:00
Make Save/Load buttons use files
This commit is contained in:
parent
2e27cda07b
commit
03bad52bdd
@ -111,9 +111,9 @@ By <a href="mailto:inexorabletash@gmail.com">Joshua Bell</a>
|
|||||||
<form id="submission" method="post" enctype="text/plain" action="mailto:inexorabletash@gmail.com?subject=Applesoft%20Sample%20Submission">
|
<form id="submission" method="post" enctype="text/plain" action="mailto:inexorabletash@gmail.com?subject=Applesoft%20Sample%20Submission">
|
||||||
<textarea name="source" id="source" style="display: none;">
|
<textarea name="source" id="source" style="display: none;">
|
||||||
</textarea>
|
</textarea>
|
||||||
|
<button id="btn_save" title="Save">💾</button>
|
||||||
|
<button id="btn_load" title="Load">📂</button>
|
||||||
<input type="submit" id="btn_share" value="Share your sample!">
|
<input type="submit" id="btn_share" value="Share your sample!">
|
||||||
<input type="button" id="btn_save" value="Save Program" title="Save the current program. NOTE: Can only save one program.">
|
|
||||||
<input type="button" id="btn_load" value="Load Program" title="Load a previously saved program. WARNING: The current program will be lost!">
|
|
||||||
<input type="button" id="show_paper" value="Show output" title="Echo all output to a "print-out", so you can copy/paste">
|
<input type="button" id="show_paper" value="Show output" title="Echo all output to a "print-out", so you can copy/paste">
|
||||||
<input type="button" id="hide_paper" value="Hide output" title="Hide the "print-out"">
|
<input type="button" id="hide_paper" value="Hide output" title="Hide the "print-out"">
|
||||||
</form>
|
</form>
|
||||||
|
38
index.js
38
index.js
@ -124,6 +124,7 @@ window.addEventListener('DOMContentLoaded', function () {
|
|||||||
|
|
||||||
stopped = false;
|
stopped = false;
|
||||||
updateUI();
|
updateUI();
|
||||||
|
$('#btn_stop').focus();
|
||||||
|
|
||||||
program.init({
|
program.init({
|
||||||
tty: tty,
|
tty: tty,
|
||||||
@ -147,14 +148,33 @@ window.addEventListener('DOMContentLoaded', function () {
|
|||||||
loadFile('samples/' + sel.value + ".txt", setSource);
|
loadFile('samples/' + sel.value + ".txt", setSource);
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#btn_save').addEventListener('click', function () {
|
var current_file_name;
|
||||||
window.localStorage.setItem("save_program", getSource());
|
$('#btn_save').addEventListener('click', function(e) {
|
||||||
$('#btn_load').disabled = false;
|
e.preventDefault();
|
||||||
|
var a = document.createElement('a');
|
||||||
|
a.download = current_file_name || 'basic_program.txt';
|
||||||
|
a.href = 'data:text/plain;base64,' + window.btoa(getSource());
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
a.parentElement.removeChild(a);
|
||||||
});
|
});
|
||||||
$('#btn_load').addEventListener('click', function () {
|
$('#btn_load').addEventListener('click', function(e) {
|
||||||
setSource(window.localStorage.getItem("save_program"));
|
e.preventDefault();
|
||||||
|
var input = document.createElement('input');
|
||||||
|
input.type = 'file';
|
||||||
|
input.addEventListener('change', function(e) {
|
||||||
|
var file = e.target.files[0];
|
||||||
|
current_file_name = file.name;
|
||||||
|
var reader = new FileReader();
|
||||||
|
reader.addEventListener('load', function() {
|
||||||
|
setSource(reader.result);
|
||||||
|
});
|
||||||
|
reader.readAsText(file);
|
||||||
|
});
|
||||||
|
document.body.appendChild(input);
|
||||||
|
input.click();
|
||||||
|
input.parentElement.removeChild(input);
|
||||||
});
|
});
|
||||||
$('#btn_load').disabled = (window.localStorage.getItem("save_program") === null);
|
|
||||||
|
|
||||||
// Add a "printer" on demand
|
// Add a "printer" on demand
|
||||||
var printer = null;
|
var printer = null;
|
||||||
@ -181,12 +201,14 @@ window.addEventListener('DOMContentLoaded', function () {
|
|||||||
|
|
||||||
var stopped = true;
|
var stopped = true;
|
||||||
function updateUI() {
|
function updateUI() {
|
||||||
|
var btnFocus = (document.activeElement === $("#btn_run") ||
|
||||||
|
document.activeElement === $("#btn_stop"));
|
||||||
$("#btn_stop").disabled = stopped ? "disabled" : "";
|
$("#btn_stop").disabled = stopped ? "disabled" : "";
|
||||||
$("#btn_run").disabled = stopped ? "" : "disabled";
|
$("#btn_run").disabled = stopped ? "" : "disabled";
|
||||||
$("#lb_files").disabled = stopped ? "" : "disabled";
|
$("#lb_files").disabled = stopped ? "" : "disabled";
|
||||||
|
|
||||||
if (stopped) {
|
if (btnFocus) {
|
||||||
$("#btn_run").focus();
|
$(stopped ? "#btn_run" : "#btn_stop").focus();
|
||||||
} else {
|
} else {
|
||||||
tty.focus();
|
tty.focus();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user