Saving non-UTF8 sort of works.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4535 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-10-18 17:51:26 +00:00
parent c8c8cb305e
commit c3da10bef6

View File

@ -185,15 +185,13 @@ class TfFilesElement extends LitElement {
event.stopPropagation();
this.dropping = 0;
for (let file of event.dataTransfer.files) {
let text = '';
try {
text = await file.text();
} catch {
text = new TextDecoder('latin1').decode(await file.arrayBuffer());
}
let buffer = await file.arrayBuffer();
let text = new TextDecoder('latin1').decode(buffer);
gFiles[file.name] = {
doc: new CodeMirror.Doc(text, guessMode(file.name)),
buffer: buffer,
generation: -1,
isNew: true,
};
gCurrentFile = file.name;
}
@ -561,6 +559,9 @@ function save(save_to) {
document.getElementById("save").disabled = true;
if (gCurrentFile) {
gFiles[gCurrentFile].doc = gEditor.getDoc();
if (!gFiles[gCurrentFile].isNew && !gFiles[gCurrentFile].doc.isClean(gFiles[gCurrentFile].doc.changeGeneration())) {
delete gFiles[gCurrentFile].buffer;
}
}
let save_path = save_to;
@ -576,17 +577,18 @@ function save(save_to) {
let promises = [];
for (let name of Object.keys(gFiles)) {
let file = gFiles[name];
if (file.doc.isClean(file.generation)) {
if (!file.isNew && file.doc.isClean(file.generation)) {
continue;
}
delete file.id;
delete file.isNew;
promises.push(fetch('/save', {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'Content-Type': 'application/binary',
},
body: file.doc.getValue(),
body: file.buffer ?? file.doc.getValue(),
}).then(function(response) {
if (!response.ok) {
throw new Error('Saving "' + name + '": ' + response.status + ' ' + response.statusText);
@ -1016,6 +1018,9 @@ function openFile(name) {
let oldDoc = gEditor.swapDoc(newDoc);
if (gFiles[gCurrentFile]) {
gFiles[gCurrentFile].doc = oldDoc;
if (!gFiles[gCurrentFile].isNew && gFiles[gCurrentFile].doc.isClean(oldDoc.generation)) {
delete gFiles[gCurrentFile].buffer;
}
}
gCurrentFile = name;
updateFiles();