From c3da10bef6369c57d559b20f81b01be8568688b0 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 18 Oct 2023 17:51:26 +0000 Subject: [PATCH] Saving non-UTF8 sort of works. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4535 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/client.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/core/client.js b/core/client.js index 602bd3b7..405790ea 100644 --- a/core/client.js +++ b/core/client.js @@ -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();