diff --git a/static/application.js b/static/application.js index 7e7352fb..2f380585 100644 --- a/static/application.js +++ b/static/application.js @@ -1628,6 +1628,21 @@ function highlightEditingChunks() { } } +// This gets run every time the text in a chunk is edited +// or a chunk is deleted +function chunkOnDOMMutate(mutations, observer) { + if(!gametext_bound || !allowedit) { + return; + } + var nodes = []; + for(var i = 0; i < mutations.length; i++) { + var mutation = mutations[i]; + nodes = nodes.concat(Array.from(mutation.addedNodes), Array.from(mutation.removedNodes)); + nodes.push(mutation.target); + } + applyChunkDeltas(nodes); +} + // This gets run every time you try to paste text into the editor function chunkOnPaste(event) { // Register the chunk we're pasting in as having been modified @@ -1705,10 +1720,12 @@ function chunkOnFocusOut(event) { } function bindGametext() { + mutation_observer.observe(game_text[0], {characterData: true, childList: true, subtree: true}); gametext_bound = true; } function unbindGametext() { + mutation_observer.disconnect(); gametext_bound = false; } @@ -2280,6 +2297,7 @@ $(document).ready(function(){ ).on('focusout', chunkOnFocusOut ); + mutation_observer = new MutationObserver(chunkOnDOMMutate); // This is required for the editor to work correctly in Firefox on desktop // because the gods of HTML and JavaScript say so diff --git a/templates/index.html b/templates/index.html index 192ed9fc..40812cee 100644 --- a/templates/index.html +++ b/templates/index.html @@ -17,7 +17,7 @@ - +