BO
443 9
Asked
Viewed
1k times

I can't seem to solve this JavaScript riddle I've gotten myself into and it's bugging me.

var autosavedContent = localStorage.getItem("smde_{{ $uniqueID }}");

if (!simplemde.value()) {
    simplemde.value(autosavedContent);
    console.log("This part doesn't work by itself.");
} else {
    simplemde.value({!! json_encode($body ?? '') !!});
}

if (!simplemde.value()) {
    simplemde.value(autosavedContent);
    console.log("Value set successfully.");
} else {
    console.log("Editor already has content.");
}

That is the JavaScript code that works. (The console logs are for troubleshooting) but if I remove the second if statement (the one with the console logs) which is essentially doing the same thing as the first, it doesn't work anymore.

I'm using SimpleMDE markdown editor and trying to make it so if I'm making a post the autosaved content is loaded but if I have content to edit, the content that is being retrieved from the database is the content that gets put into the editor.

While the content that comes from the database does get placed into the editor, the autosaved content part (The first condition of the if statement) only works if that condition is put twice. For some reason, the first if statement always goes to the 'else' condition while the second if statement gets the first condition triggered properly and than it places the autosaved content.

The textarea is completely empty when I need the autosave content to be loaded so simplemde.value() should be triggered as false but it only triggers as false the second time only.

if (document.getElementById("markdown-editor").value == '') {
    simplemde.value(autosavedContent);
    console.log("This part doesn't work by itself.");
} else {
    simplemde.value({!! json_encode($body ?? '') !!});
}

if (!simplemde.value()) {
    simplemde.value(autosavedContent);
    console.log("Value set successfully.");
} else {
    console.log("Editor already has content.");
}

Trying it that way, the first condition of the first if statement still doesn't get triggered. Just the second if statement.

Both of those if statements are inside document.addEventListener("DOMContentLoaded", function() { ... });

I also tried the following which didn't work properly as well

if (simplemde.value()) {
    console.log('Maybe this works?');
    simplemde.value({!! json_encode($body ?? '') !!});
} else {
    simplemde.value(autosavedContent);
    console.log("Value set successfully.");
}
  • 0
    No errors or warnings or anything in the console besides what I had hardcoded for troubleshooting — Bogey
add a comment
0

1 Answer

  • Votes
  • Oldest
  • Latest
BO
443 9
Answered
Updated
            var editable = {!! json_encode($body ?? false) !!};
            
            if (!editable) {
                simplemde.value(simplemde.value());
            } else {
                simplemde.value(editable);
            }

Doing it that way solved my redundancy issue. For some reason the value of simplemde.value() was different before and after the first if statement making the first if statement to always hit the second condition which set it to empty since there was no body to edit which triggered the first condition of the second if statement.

I guess simplemde already was retrieving the autosaved content and putting it into simplemde.value() which I was clearing with the first statement and finally doing what I need with the second statement.

add a comment
0