From: Malte Bublitz Date: Sun, 2 Jun 2024 20:56:34 +0000 (+0200) Subject: notepad: Tab-Unterstützung X-Git-Url: https://git.rt3x.de/?a=commitdiff_plain;h=72e45bca281169c95ce7f78bdf51ef5895221b1a;p=startseite.malte70.de.git notepad: Tab-Unterstützung --- diff --git a/js/main.js b/js/main.js index 5ad0a46..68d6285 100644 --- a/js/main.js +++ b/js/main.js @@ -10,9 +10,7 @@ window.onload = () => { whoami.update(); //whoami.settings2(); - document.querySelector( - "button#settings" - ).onclick = function() { + document.querySelector("button#settings").onclick = function() { //window.whoami.settings(); //window.whoami.update(); whoami.settings2(); diff --git a/js/notepad.js b/js/notepad.js index f0a2131..860ff01 100644 --- a/js/notepad.js +++ b/js/notepad.js @@ -9,19 +9,42 @@ * @param {string} storageKey string local Storage key */ window.notepad = function(selector, storageKey) { - //console.log("window.notepad(\""+selector+"\", \""+storageKey+"\")") - let elem = document.querySelector(selector); - + + /** + * Load content from localStorage if possible + */ if (localStorage.getItem(storageKey) !== null) { console.debug("window.notepad(): Loading from LocalStorage") elem.innerHTML = localStorage.getItem("notepad"); } - + + /** + * Save to localStorage after loosing focus + */ elem.addEventListener("focusout", function() { console.debug("window.notepad(): Focus lost. Storing on LocalStorage"); localStorage.setItem(storageKey, document.querySelector(selector).innerHTML); - }); + + /** + * Allow inserting Tabs + * + * @see https://jsfiddle.net/2wAzx/13/ + */ + elem.onkeydown = function(e) { + if (e.keyCode === 9) { // tab was pressed + // get caret position/selection + var val = this.value, + start = this.selectionStart, + end = this.selectionEnd; + // set textarea value to: text before caret + tab + text after caret + this.value = val.substring(0, start) + '\t' + val.substring(end); + // put caret at right position again + this.selectionStart = this.selectionEnd = start + 1; + // prevent the focus lose + return false; + } + }; };