diff options
Diffstat (limited to 'public/js/writing_create.js')
| -rw-r--r-- | public/js/writing_create.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/public/js/writing_create.js b/public/js/writing_create.js new file mode 100644 index 0000000..e1c88c0 --- /dev/null +++ b/public/js/writing_create.js @@ -0,0 +1,82 @@ +let contentPreview; +let inputText; +let dom = {}; +let editorHistory = []; + + +$(function() { + + if (typeof marked == 'undefined'){ + console.error("marked lib not loaded"); + return; + } else { + console.log("marked loaded"); + console.log(marked); + } + initDOM(); + + + marked.setOptions({ + breaks: true, + sanitize: true + }) + + dom.inputText.oninput = ()=>{ + dom.contentPreview.innerHTML = marked.parse(dom.inputText.value); + }; + + let restoredContent = localStorage.getItem('writing_draft_content'); + if (restoredContent != null){ + if (localStorage.getItem('writing_draft_expiration') < Date.now()){ + localStorage.removeItem('writing_draft_content'); + localStorage.removeItem('writing_draft_expiration'); + } else { + dom.inputText.value = restoredContent; + dom.contentPreview.innerHTML = marked.parse(dom.inputText.value); + } + } + + //periodically save the contents to client to prevent data loss + const intervalId = setInterval(() => { + const content = dom.inputText.value; + localStorage.setItem('writing_draft_content', content); + localStorage.setItem('writing_draft_expiration', Date.now() + 1000*3600*96); + }, 5000); + +}); + +function initDOM(){ + dom.inputText = $('#input_content')[0]; + dom.contentPreview = $('#content_preview')[0]; + dom.findStr = $('#input_findstr')[0]; + dom.replaceStr = $('#input_replacestr')[0]; + dom.btnReplace = $('#button_replace')[0]; + dom.btnUndo = $('#button_undo')[0]; + console.log(dom); + dom.btnReplace.onclick = performReplace; + dom.btnUndo.onclick = ()=>{ + if (editorHistory.length > 0){ + dom.inputText.value = editorHistory.pop(); + dom.contentPreview.innerHTML = marked.parse(dom.inputText.value); + } + }; + dom.toggleMultiline = $('#input_multiline')[0]; +} + +function performReplace(){ + + + // Save current state to history for undo + editorHistory.push(dom.inputText.value); + + // Perform the replacement + const regex = new RegExp(dom.findStr.value, dom.toggleMultiline.checked ? 'gm' : 'g'); + dom.inputText.value = dom.inputText.value.replace(regex, dom.replaceStr.value); + dom.contentPreview.innerHTML = marked.parse(dom.inputText.value); + + + editorHistory.push(dom.inputText.value); + + dom.inputText.value = dom.inputText.value.replace(regex, dom.replaceStr.value); + dom.contentPreview.innerHTML = marked.parse(dom.inputText.value); +};
\ No newline at end of file |
