From e0d0e403682e9ab95df13bd8a7d75213c831df10 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Fri, 9 Oct 2015 22:20:17 -0700 Subject: [PATCH] add tab functionality to pastebin (resolves #48) The tab key now inserts a tab instead of changing the focus. --- static/js/bin.js | 5 ++--- static/js/paste.js | 3 +++ static/js/util.js | 18 ++++++++++++++++++ templates/display/bin.html | 1 + templates/paste.html | 3 +++ 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 static/js/paste.js create mode 100644 static/js/util.js diff --git a/static/js/bin.js b/static/js/bin.js index 9e8d1a8..6e1bbcc 100644 --- a/static/js/bin.js +++ b/static/js/bin.js @@ -5,7 +5,6 @@ var navlist = document.getElementById("info").getElementsByClassName("right")[0] init(); function init() { - var editA = document.createElement('a'); editA.setAttribute("href", "#"); @@ -23,8 +22,8 @@ function init() { document.getElementById('wordwrap').addEventListener('click', wrap); } - function edit(ev) { + ev.preventDefault(); navlist.remove(); document.getElementById("filename").remove(); @@ -35,9 +34,9 @@ function edit(ev) { var editordiv = document.getElementById("editor"); editordiv.style.display = "block"; + editordiv.addEventListener('keydown', handleTab); } - function paste(ev) { var editordiv = document.getElementById("editor"); document.getElementById("newcontent").value = editordiv.value; diff --git a/static/js/paste.js b/static/js/paste.js new file mode 100644 index 0000000..bd9448a --- /dev/null +++ b/static/js/paste.js @@ -0,0 +1,3 @@ +// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later +document.getElementById('content').addEventListener('keydown', handleTab); +// @license-end diff --git a/static/js/util.js b/static/js/util.js new file mode 100644 index 0000000..cef4eca --- /dev/null +++ b/static/js/util.js @@ -0,0 +1,18 @@ +// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later + +function handleTab(ev) { + // change tab key behavior to insert tab instead of change focus + if(ev.keyCode == 9) { + ev.preventDefault(); + + var val = this.value; + var start = this.selectionStart; + var end = this.selectionEnd; + + this.value = val.substring(0, start) + '\t' + val.substring(end); + this.selectionStart = start + 1; + this.selectionEnd = end + 1; + } +} + +// @license-end diff --git a/templates/display/bin.html b/templates/display/bin.html index 49826d1..9c6382e 100644 --- a/templates/display/bin.html +++ b/templates/display/bin.html @@ -52,5 +52,6 @@ {% endif %} + {% endblock %} diff --git a/templates/paste.html b/templates/paste.html index b7adaf4..1b31aac 100644 --- a/templates/paste.html +++ b/templates/paste.html @@ -33,4 +33,7 @@ + + + {% endblock %}