add tab functionality to pastebin (resolves #48)

The tab key now inserts a tab instead of changing the focus.
This commit is contained in:
mutantmonkey 2015-10-09 22:20:17 -07:00
parent 883768f512
commit e0d0e40368
5 changed files with 27 additions and 3 deletions

View File

@ -5,7 +5,6 @@ var navlist = document.getElementById("info").getElementsByClassName("right")[0]
init(); init();
function init() { function init() {
var editA = document.createElement('a'); var editA = document.createElement('a');
editA.setAttribute("href", "#"); editA.setAttribute("href", "#");
@ -23,8 +22,8 @@ function init() {
document.getElementById('wordwrap').addEventListener('click', wrap); document.getElementById('wordwrap').addEventListener('click', wrap);
} }
function edit(ev) { function edit(ev) {
ev.preventDefault();
navlist.remove(); navlist.remove();
document.getElementById("filename").remove(); document.getElementById("filename").remove();
@ -35,9 +34,9 @@ function edit(ev) {
var editordiv = document.getElementById("editor"); var editordiv = document.getElementById("editor");
editordiv.style.display = "block"; editordiv.style.display = "block";
editordiv.addEventListener('keydown', handleTab);
} }
function paste(ev) { function paste(ev) {
var editordiv = document.getElementById("editor"); var editordiv = document.getElementById("editor");
document.getElementById("newcontent").value = editordiv.value; document.getElementById("newcontent").value = editordiv.value;

3
static/js/paste.js Normal file
View File

@ -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

18
static/js/util.js Normal file
View File

@ -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

View File

@ -52,5 +52,6 @@
<script src="/static/js/bin_hljs.js"></script> <script src="/static/js/bin_hljs.js"></script>
{% endif %} {% endif %}
<script src="/static/js/util.js"></script>
<script src="/static/js/bin.js"></script> <script src="/static/js/bin.js"></script>
{% endblock %} {% endblock %}

View File

@ -33,4 +33,7 @@
</div> </div>
</div> </div>
</form> </form>
<script src="/static/js/util.js"></script>
<script src="/static/js/paste.js"></script>
{% endblock %} {% endblock %}