Clean up whitespace in the editor as well

(cherry picked from commit 6e138db1c0)
This commit is contained in:
vfbd 2022-06-26 14:05:04 -04:00
parent 2a4d37ce60
commit 9d09ae5fea
1 changed files with 43 additions and 2 deletions

View File

@ -1609,7 +1609,7 @@ function applyChunkDeltas(nodes) {
var selected_chunks = buildChunkSetFromNodeArray(getSelectedNodes());
for(var i = 0; i < chunks.length; i++) {
var chunk = document.getElementById("n" + chunks[i]);
if(chunk && formatChunkInnerText(chunk).length != 0 && chunks[i] != '0') {
if(chunk && formatChunkInnerText(chunk).trim().length != 0 && chunks[i] != '0') {
if(!selected_chunks.has(chunks[i])) {
modified_chunks.delete(chunks[i]);
socket.send({'cmd': 'inlineedit', 'chunk': chunks[i], 'data': formatChunkInnerText(chunk)});
@ -1618,7 +1618,7 @@ function applyChunkDeltas(nodes) {
} else {
if(!selected_chunks.has(chunks[i])) {
modified_chunks.delete(chunks[i]);
socket.send({'cmd': 'inlineedit', 'chunk': chunks[i], 'data': ''});
socket.send({'cmd': 'inlineedit', 'chunk': chunks[i], 'data': formatChunkInnerText(chunk)});
}
empty_chunks.add(chunks[i]);
}
@ -1750,6 +1750,46 @@ function highlightEditingChunks() {
}
}
function cleanupChunkWhitespace() {
// Merge empty chunks with the next chunk
var chunks = Array.from(empty_chunks);
chunks.sort(function(e) {parseInt(e)});
for(var i = 0; i < chunks.length; i++) {
var original_chunk = document.getElementById("n" + chunks[i]);
original_chunk.innerText = footer + original_chunk.innerText;
footer = "";
var chunk = original_chunk.nextSibling;
while(chunk) {
if(chunk.tagName === "CHUNK") {
break;
}
chunk = chunk.nextSibling;
}
if(chunk) {
chunk.innerText = original_chunk.innerText + chunk.innerText;
}
original_chunk.innerText = "";
}
// Move whitespace at the end of non-empty chunks into the beginning of the next non-empty chunk
var chunks = Array.from(modified_chunks);
chunks.sort(function(e) {parseInt(e)});
for(var i = 0; i < chunks.length; i++) {
var original_chunk = document.getElementById("n" + chunks[i]);
var chunk = original_chunk.nextSibling;
while(chunk) {
if(chunk.tagName === "CHUNK" && !empty_chunks.has(chunk.getAttribute("n"))) {
break;
}
chunk = chunk.nextSibling;
}
var ln = original_chunk.innerText.trimEnd().length;
if (chunk) {
chunk.innerText = original_chunk.innerText.substring(ln) + chunk.innerText;
}
original_chunk.innerText = original_chunk.innerText.substring(0, ln);
}
}
// This gets run every time the text in a chunk is edited
// or a chunk is deleted
function chunkOnDOMMutate(mutations, observer) {
@ -1828,6 +1868,7 @@ function chunkOnFocusOut(event) {
if(document.activeElement === game_text[0] || game_text[0].contains(document.activeElement)) {
return;
}
cleanupChunkWhitespace();
syncAllModifiedChunks(true);
setTimeout(function() {
var blurred = game_text[0] !== document.activeElement;