diff --git a/gc_little_helper_II.user.js b/gc_little_helper_II.user.js index a43a8105..79602741 100644 --- a/gc_little_helper_II.user.js +++ b/gc_little_helper_II.user.js @@ -4700,6 +4700,67 @@ var mainGC = function() { var isEvent = pageData.isEvent; } let css = ''; + // Have we changed the logtext? + let gclhChanges = false; + let signature = ((!isTB && settings_add_cache_log_signature) + && ((!isDraft) || (isDraft && settings_log_signature_on_fieldnotes))) + || (isTB && settings_add_tb_log_signature); + let keepGClhChanges = false; + let _logtext = ''; // The Logtext + + function buildMirrorObserver() { + $('#gc-md-editor_md')[0].addEventListener('input', () => { + console.log('input') + keepGClhChanges = false; + }); + const config = { childList: true, subtree: true }; + const observer = new MutationObserver(function(mutationsList, observer) { + observer.disconnect(); + mutationsList.forEach(function (_) { + if (gclhChanges || signature) { + console.log('changes'); + gclhChanges = false; + signature = false + keepGClhChanges = true; + } else if (keepGClhChanges) { + console.log('keep'); + $('#gc-md-editor_md')[0].value = _logtext; + } else { + console.log('save text') + _logtext = $('.lt-mirror__canvas')[0].innerHTML; + } + console.log(_logtext) + }); + // Continue Observing + observer.observe($('.lt-mirror__canvas')[0], config); + }); + observer.observe($('.lt-mirror__canvas')[0], config); + } + + const config = { childList: true, subtree: true }; + const logpageObserver = new MutationObserver(function(mutationsList, observer) { + observer.disconnect(); + if ($('.lt-mirror__canvas:not(.gclh_observer)')[0]) { + $('.lt-mirror__canvas').addClass('gclh_observer'); + buildMirrorObserver(); + } + console.log('Body Observer'); + if (signature && $('#gc-md-editor_md')[0].value == '') { + $('#gc-md-editor_md').removeClass('gclh_signature'); + signature = false; + console.log('tryBuildSig') + if ((!isTB && settings_add_cache_log_signature) + || (isTB && settings_add_tb_log_signature)) buildSignature(0); + } + observer.observe(document.body, config); + }); + logpageObserver.observe(document.body, config); + + window.addEventListener('gclhLogTextChanges', () => { + console.log('Chnages by GClh'); + _logtext = $('#gc-md-editor_md')[0].value; + gclhChanges = true; + }) // Default logtypes. function setDefaultLogtype(waitCount) { @@ -4741,23 +4802,28 @@ var mainGC = function() { text = text.replace(/#GCTBName#/ig, aGCTBName).replace(/#GCTBLink#/ig, aGCTBLink).replace(/#GCTBNameLink#/ig, aGCTBNameLink).replace(/#LogDate#/ig, aLogDate); return text; } - if ($('#gc-md-editor_md')[0] && !$('.gclh_signature')[0]) { + if ($('#gc-md-editor_md')[0] && (!$('.gclh_signature')[0])) { + console.log('buildSignature') if ((!isDraft) || (isDraft && settings_log_signature_on_fieldnotes)) { + console.log('actualBuildSignature') let logfield = $('#gc-md-editor_md')[0]; var logtext = logfield.value; var signature = getValue((isTB ? "settings_tb_signature" : "settings_log_signature"), ""); if (!logtext.includes(signature.replace(/^\s*/, ''))) { let text = (logfield.value != '' ? logfield.value + '\n' : '') + replacePlaceholder(signature); logfield.value = text; + _logtext = text; + signature = true; } if (!$('.gclh_signature')[0]) $('#gc-md-editor_md').addClass('gclh_signature'); logfield.dispatchEvent(new Event('input')); + window.dispatchEvent(new Event('gclhLogTextChanges')); logfield.focus(); logfield.selectionStart = 0; logfield.selectionEnd = 0; } } - waitCount++; if (waitCount <= 1000) setTimeout(function(){buildSignature(waitCount);}, 10); + waitCount++; if (waitCount <= 10) setTimeout(function(){buildSignature(waitCount);}, 10); } try { if ((!isTB && settings_add_cache_log_signature) || (isTB && settings_add_tb_log_signature)) buildSignature(0); @@ -4817,6 +4883,7 @@ var mainGC = function() { code += " }"; code += " input.focus();"; code += " document.getElementById('gc-md-editor_md').dispatchEvent(new Event('input'));"; + code += " window.dispatchEvent(new Event('gclhLogTextChanges'));" code += "}"; if (!$('#gclh_LogTemplatesScript')[0]) { injectPageScript(code, 'body', 'gclh_LogTemplatesScript'); @@ -14397,7 +14464,7 @@ var mainGC = function() { GCTBName = GCTBName.replace(/'/g,""); var GCTBLink = $('.loggable-header a.geocache-link')[0].href; var GCTBNameLink = "[" + GCTBName + "](" + GCTBLink + ")"; - if ($('#log-date'[0])) var LogDate = $('#log-date')[0].value; + if ($('#log-date')[0]) var LogDate = $('#log-date')[0].value; return [GCTBName, GCTBLink, GCTBNameLink, LogDate]; } function getGCTBInfo(newLogPage) {