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

Story whitespace cleanup backport
This commit is contained in:
henk717 2022-06-26 20:35:50 +02:00 committed by GitHub
commit ad9ec6eaba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 5 deletions

View File

@ -3110,6 +3110,7 @@ def actionsubmit(data, actionmode=0, force_submit=False, force_prompt_gen=False,
if(not vars.gamestarted): if(not vars.gamestarted):
vars.submission = data vars.submission = data
execute_inmod() execute_inmod()
vars.submission = re.sub(r"[^\S\r\n]*([\r\n]*)$", r"\1", vars.submission) # Remove trailing whitespace, excluding newlines
data = vars.submission data = vars.submission
if(not force_submit and len(data.strip()) == 0): if(not force_submit and len(data.strip()) == 0):
assert False assert False
@ -3168,6 +3169,7 @@ def actionsubmit(data, actionmode=0, force_submit=False, force_prompt_gen=False,
data = applyinputformatting(data) data = applyinputformatting(data)
vars.submission = data vars.submission = data
execute_inmod() execute_inmod()
vars.submission = re.sub(r"[^\S\r\n]*([\r\n]*)$", r"\1", vars.submission) # Remove trailing whitespace, excluding newlines
data = vars.submission data = vars.submission
# Dont append submission if it's a blank/continue action # Dont append submission if it's a blank/continue action
if(data != ""): if(data != ""):
@ -5091,6 +5093,7 @@ def loadRequest(loadpath, filename=None):
vars.actions_metadata[i] = {'Selected Text': text, 'Alternative Text': []} vars.actions_metadata[i] = {'Selected Text': text, 'Alternative Text': []}
i+=1 i+=1
footer = ""
if(len(vars.prompt.strip()) == 0): if(len(vars.prompt.strip()) == 0):
while(len(actions)): while(len(actions)):
@ -5100,9 +5103,25 @@ def loadRequest(loadpath, filename=None):
break break
else: else:
vars.gamestarted = False 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): if(vars.gamestarted):
for s in actions: 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 # Try not to break older save files
if("authorsnote" in js): if("authorsnote" in js):

View File

@ -881,6 +881,7 @@ function dosubmit(disallow_abort) {
if((disallow_abort || gamestate !== "wait") && !memorymode && !gamestarted && ((!adventure || !action_mode) && txt.trim().length == 0)) { if((disallow_abort || gamestate !== "wait") && !memorymode && !gamestarted && ((!adventure || !action_mode) && txt.trim().length == 0)) {
return; return;
} }
chunkOnFocusOut("override");
input_text.val(""); input_text.val("");
hideMessage(); hideMessage();
hidegenseqs(); hidegenseqs();
@ -1609,7 +1610,7 @@ function applyChunkDeltas(nodes) {
var selected_chunks = buildChunkSetFromNodeArray(getSelectedNodes()); var selected_chunks = buildChunkSetFromNodeArray(getSelectedNodes());
for(var i = 0; i < chunks.length; i++) { for(var i = 0; i < chunks.length; i++) {
var chunk = document.getElementById("n" + chunks[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])) { if(!selected_chunks.has(chunks[i])) {
modified_chunks.delete(chunks[i]); modified_chunks.delete(chunks[i]);
socket.send({'cmd': 'inlineedit', 'chunk': chunks[i], 'data': formatChunkInnerText(chunk)}); socket.send({'cmd': 'inlineedit', 'chunk': chunks[i], 'data': formatChunkInnerText(chunk)});
@ -1618,7 +1619,7 @@ function applyChunkDeltas(nodes) {
} else { } else {
if(!selected_chunks.has(chunks[i])) { if(!selected_chunks.has(chunks[i])) {
modified_chunks.delete(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]); empty_chunks.add(chunks[i]);
} }
@ -1750,6 +1751,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 // This gets run every time the text in a chunk is edited
// or a chunk is deleted // or a chunk is deleted
function chunkOnDOMMutate(mutations, observer) { function chunkOnDOMMutate(mutations, observer) {
@ -1821,13 +1862,14 @@ function chunkOnKeyDownSelectionChange(event) {
// This gets run when you defocus the editor by clicking // This gets run when you defocus the editor by clicking
// outside of the editor or by pressing escape or tab // outside of the editor or by pressing escape or tab
function chunkOnFocusOut(event) { function chunkOnFocusOut(event) {
if(!gametext_bound || !allowedit || event.target !== game_text[0]) { if(event !== "override" && (!gametext_bound || !allowedit || event.target !== game_text[0])) {
return; return;
} }
setTimeout(function() { setTimeout(function() {
if(document.activeElement === game_text[0] || game_text[0].contains(document.activeElement)) { if(document.activeElement === game_text[0] || game_text[0].contains(document.activeElement)) {
return; return;
} }
cleanupChunkWhitespace();
syncAllModifiedChunks(true); syncAllModifiedChunks(true);
setTimeout(function() { setTimeout(function() {
var blurred = game_text[0] !== document.activeElement; var blurred = game_text[0] !== document.activeElement;

View File

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

View File

@ -91,6 +91,9 @@ def removespecialchars(txt, vars=None):
# If the next action follows a sentence closure, add a space # If the next action follows a sentence closure, add a space
#==================================================================# #==================================================================#
def addsentencespacing(txt, vars): def addsentencespacing(txt, vars):
# Don't add sentence spacing if submission is empty or starts with whitespace
if(len(txt) == 0 or len(txt) != len(txt.lstrip())):
return txt
# Get last character of last action # Get last character of last action
if(len(vars.actions) > 0): if(len(vars.actions) > 0):
if(len(vars.actions[vars.actions.get_last_key()]) > 0): if(len(vars.actions[vars.actions.get_last_key()]) > 0):