Skip to content

Commit

Permalink
Merge pull request #7 from bijx/bijx/bug-fixes-local-storage
Browse files Browse the repository at this point in the history
Added local storage saving
  • Loading branch information
bijx authored Jan 3, 2024
2 parents 88cdd70 + f8a340d commit abd042a
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ document.getElementById('insertQuote').addEventListener('click', function () {
document.getElementById('insertUrl').addEventListener('click', function () {
const url = document.getElementById('urlInput').value;
const linkText = document.getElementById('urlText').value;
const insertion = `[url=${url}] ${linkText} [/url]`;
const insertion = `[url=${url}]${linkText}[/url]`;
const beforeSelection = editor.value.substring(0, editor.selectionStart);
const afterSelection = editor.value.substring(editor.selectionEnd, editor.value.length);
editor.value = beforeSelection + insertion + afterSelection;
Expand Down Expand Up @@ -157,3 +157,30 @@ editor.addEventListener('keydown', function (e) {
}
}
});


/* LocalStorage */

// Save to localStorage
function saveTextAsLocalStorage() {
const text = editor.value;
localStorage.setItem("markupText", text);
}

// Load from localStorage
function loadTextFromLocalStorage() {
const savedText = localStorage.getItem("markupText");
if (savedText) {
editor.value = savedText;
updatePreview();
}
}

// Event listeners
editor.addEventListener('input', function () {
saveTextAsLocalStorage();
});

document.addEventListener('DOMContentLoaded', (event) => {
loadTextFromLocalStorage();
});

0 comments on commit abd042a

Please sign in to comment.