Merge pull request #162 from VE-FORBRYDERNE/whitespace-cleanup

Story whitespace cleanup
This commit is contained in:
henk717 2022-06-25 13:36:03 +02:00 committed by GitHub
commit 10e85db89d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 6 deletions

View File

@ -5474,7 +5474,8 @@ def loadRequest(loadpath, filename=None):
for text in js['actions']:
vars.actions_metadata[i] = {'Selected Text': text, 'Alternative Text': []}
i+=1
footer = ""
if(len(vars.prompt.strip()) == 0):
while(len(actions)):
@ -5484,9 +5485,25 @@ def loadRequest(loadpath, filename=None):
break
else:
vars.gamestarted = False
vars.prompt = vars.prompt.lstrip()
ln = len(vars.prompt.rstrip())
footer += vars.prompt[ln:]
vars.prompt = vars.prompt[:ln]
if(vars.gamestarted):
for s in actions:
vars.actions.append(s)
if(len(s.strip()) == 0):
# If this action only contains whitespace, we merge it with the next action
footer += s
continue
vars.actions.append(footer + s)
footer = ""
# If there is trailing whitespace at the end of an action, we move that whitespace to the beginning of the next action
ln = len(vars.actions[vars.actions.get_last_key()].rstrip())
footer += vars.actions[vars.actions.get_last_key()][ln:]
vars.actions[vars.actions.get_last_key()] = vars.actions[vars.actions.get_last_key()][:ln]
if(len(vars.actions) == 0):
vars.gamestarted = False
# Try not to break older save files
if("authorsnote" in js):

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

@ -4,6 +4,7 @@ body {
chunk {
color: #ffffff;
white-space: pre-wrap;
}
#gametext.adventure action {

View File

@ -9,7 +9,7 @@
<link rel="stylesheet" href="static/bootstrap.min.css">
<link rel="stylesheet" href="static/bootstrap-toggle.min.css">
<link rel="stylesheet" href="static/open-iconic-bootstrap.min.css">
<link rel="stylesheet" href="static/custom.css?ver=1.18.1b">
<link rel="stylesheet" href="static/custom.css?ver=1.18.1c">
<script src="static/jquery-3.6.0.min.js"></script>
<script src="static/jquery-ui.sortable.min.js"></script>
@ -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>