Skip to content

Commit

Permalink
fix keep changes
Browse files Browse the repository at this point in the history
  • Loading branch information
capoaira committed Nov 26, 2023
1 parent 0ca010d commit 6f443fb
Showing 1 changed file with 20 additions and 33 deletions.
53 changes: 20 additions & 33 deletions gc_little_helper_II.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4701,60 +4701,48 @@ var mainGC = function() {
}
let css = '';
// Have we changed the logtext?
let gclhChanges = false;
let signature = ((!isTB && settings_add_cache_log_signature)
let keepGClhChanges = ((!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', () => {
keepGClhChanges = false;
});
const config = { childList: true, subtree: true };
const observer = new MutationObserver(function(mutationsList, observer) {
observer.disconnect();
mutationsList.forEach(function (_) {
if (gclhChanges) {
gclhChanges = false;
signature = false;
keepGClhChanges = true;
} else if (keepGClhChanges) {
$('#gc-md-editor_md')[0].value = _logtext;
}
// When the user enter a key to the logfield, changes by GClh (Signature and Templates) are take over by GS script.
function buildInputLogTextListener(waitCount) {
if ($('#gc-md-editor_md:not(.gclh_text_listener)')[0]) {
$('#gc-md-editor_md')[0].addEventListener('input', () => {
keepGClhChanges = false;
});
// Continue Observing
observer.observe($('.lt-mirror__canvas')[0], config);
});
observer.observe($('.lt-mirror__canvas')[0], config);
$('#gc-md-editor_md').addClass('gclh_text_listener');
}
waitCount++; if (waitCount <= 1000) setTimeout(function(){buildInputLogTextListener(waitCount);}, 10);
}
buildInputLogTextListener(0);

// Sets the Logtext value, if GS deletes it.
const config = { childList: true, subtree: true };
const logpageObserver = new MutationObserver(function(mutationsList, observer) {
const logpageObserver = new MutationObserver(function(_, observer) {
observer.disconnect();
if ($('.lt-mirror__canvas:not(.gclh_observer)')[0]) {
$('.lt-mirror__canvas').addClass('gclh_observer');
buildMirrorObserver();
}
if (signature && $('#gc-md-editor_md')[0]?.value == (pageData.logValues.note || '')) {
if (signature) $('#gc-md-editor_md')[0].value = _logtext;
console.log('keepGClhChanges: ', keepGClhChanges)
if ($('#gc-md-editor_md')[0] && keepGClhChanges) {
console.log('keep')
$('#gc-md-editor_md')[0].value = _logtext;
}
observer.observe(document.body, config);
});
logpageObserver.observe(document.body, config);

// Save State and Logtext after changes by GClh (Signature and Templates).
window.addEventListener('gclhLogTextChanges', () => {
_logtext = $('#gc-md-editor_md')[0].value;
gclhChanges = true;
keepGClhChanges = true;
});

// Achtung: Funktioniert leider nicht !!!!!
// Warning if the last changes in the logtext are done with GClh (Signature and Templates).
function buildWarningForGClhChanges(waitCount) {
if ($('.post-button-container:not(.gclh_change_warning)')[0]) {
$('.post-button-container').addClass('gclh_change_warning');
$('.post-button-container').bind('click', (e) => {
if (gclhChanges || keepGClhChanges) {
if (keepGClhChanges) {
e.preventDefault();
alert('Please wait. The last change you made was with the GClh. Please add any character to the log (you can delete it immediately afterwards) to apply the changes.');
return false;
Expand Down Expand Up @@ -4814,7 +4802,6 @@ var mainGC = function() {
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'));
Expand Down

0 comments on commit 6f443fb

Please sign in to comment.