urgh file loading

This commit is contained in:
Lisa Milne 2023-11-06 12:06:28 +10:00
parent ef30e3100a
commit 1381e55db6

View file

@ -3,7 +3,7 @@ var clite = {
isinit:false,
terminal:null,
input:{
ispass:false,
type:'text',
form:null,
field:null
}
@ -63,14 +63,16 @@ var clite = {
},
file:function(name,callback) {
if (window.location.protocol == 'file:') {
clite.log.write('no file: support');
clite.shell.writeLine('open file: '+name);
clite.term.setCustom({type:'file',callback:callback});
clite.events.refocus();
return;
}
fetch(name)
.then(response => response.text())
.then((data) => {
callback(data)
callback(data);
});
}
}
@ -92,7 +94,7 @@ var clite = {
clite.events.refocus();
clite.core.load.file('data/filesys.txt',alert);
clite.core.load.file('data/filesys.txt',clite.shell.writeLine);
});
}
};
@ -123,7 +125,7 @@ clite.term = {
l.innerHTML = clite.shell.prompt.getHTML();
f.appendChild(l);
var i = document.createElement('input');
i.type = clite.state.input.ispass ? 'password' : 'text';
i.type = clite.term.getType();
i.value = clite.shell.history.getCurrent();
f.appendChild(i);
clite.state.input.form = f;
@ -135,12 +137,36 @@ clite.term = {
i.onkeyup = clite.events.keyup;
i.onfocusout = clite.events.refocus;
i.onblur = clite.events.refocus;
if (i.type == 'file') {
i.onchange = function(e) {
var reader = new FileReader();
reader.onload = function() {
clite.state.input.type.callback(reader.result);
clite.term.setPass(false);
clite.shell.writeLine('loaded');
};
reader.onerror = function() {
clite.state.input.type.callback(null);
clite.term.setPass(false);
clite.shell.writeLine('load failed');
};
reader.readAsText(e.target.files[0]);
}
}
f.onsubmit = function() {return false;};
},
setPass:function(is) {
clite.state.input.ispass = !!is;
if (typeof clite.state.input.ispass != 'Boolean')
clite.state.input.ispass = false;
clite.state.input.type = (!!is) ? 'password' : 'text';
},
setCustom:function(type) {
clite.state.input.type = type;
},
getType:function() {
if (typeof clite.state.input.type == 'string')
return clite.state.input.type;
if (typeof clite.state.input.type == 'object' && typeof clite.state.input.type.type == 'string')
return clite.state.input.type.type;
return 'text';
}
};