Clean up whitespace in the editor as well

This commit is contained in:
vfbd 2022-06-24 12:44:00 -04:00
parent 4b16600e49
commit 6e138db1c0
2 changed files with 44 additions and 3 deletions

View File

@ -1717,7 +1717,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)});
@ -1726,7 +1726,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]);
}
@ -1858,6 +1858,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) {
@ -1936,6 +1976,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;

View File

@ -17,7 +17,7 @@
<script src="static/bootstrap.min.js"></script>
<script src="static/bootstrap-toggle.min.js"></script>
<script src="static/rangy-core.min.js"></script>
<script src="static/application.js?ver=1.18.1c"></script>
<script src="static/application.js?ver=1.18.1d"></script>
<script src="static/favicon.js"></script>
{% if flaskwebgui %}
<script src="static/flask_web_gui.js"></script>