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