From b1531cbfb09c7902c8adb9e4449cfc0ce54d8426 Mon Sep 17 00:00:00 2001 From: zPlus <-> Date: Mon, 27 Mar 2017 21:24:54 +0200 Subject: [PATCH] Fix #40 Ctrl+Enter to submit post/comment --- javascript/freepost.js | 31 +++++++++++++++++++++++++------ template/edit_comment.twig | 5 ++++- template/edit_post.twig | 5 ++++- template/post.twig | 5 ++++- template/reply.twig | 5 ++++- template/submit.twig | 6 +++++- 6 files changed, 46 insertions(+), 11 deletions(-) diff --git a/javascript/freepost.js b/javascript/freepost.js index 7bc62b6f..df138294 100644 --- a/javascript/freepost.js +++ b/javascript/freepost.js @@ -27,11 +27,13 @@ @licend The above is the entire license notice for the JavaScript code in this page. */ - -/* This is a small script to hide the "up/down" arrow when users upvote - * posts and comments. The only reason for this is to give some feedback - * to the user after clicks. +/** + * Store which keys have been pressed. + * When a key has been pressed, pressed_key[e.keyCode] will be set + * to TRUE. When a key is released, pressed_key[e.keyCode] will be + * set to FALSE. */ +var pressed_key = []; /** * Change arrows class when voting. @@ -110,5 +112,22 @@ document.addEventListener ('DOMContentLoaded', function() { vote_sections[i].children[0].addEventListener ('click', function () { vote ('up', this.parentNode) }); vote_sections[i].children[2].addEventListener ('click', function () { vote ('down', this.parentNode) }); } - -}); + + // Bind onkeydown()/onkeyup() event to keys + document.onkeydown = document.onkeyup = function(e) { + // Set the current key code as TRUE/FALSE + pressed_key[e.keyCode] = e.type == 'keydown'; + + // If Ctrl+Enter have been pressed + // Key codes: Ctrl=17, Enter=13 + if (pressed_key[17] && pressed_key[13]) + { + // Select all forms in the current page with class "shortcut-submit" + let forms = document.querySelectorAll ("form.shortcut-submit"); + + for (let i = 0; i < forms.length; i++) + forms[i].submit (); + } + } + +}); \ No newline at end of file diff --git a/template/edit_comment.twig b/template/edit_comment.twig index 51edb459..703c772b 100644 --- a/template/edit_comment.twig +++ b/template/edit_comment.twig @@ -14,7 +14,10 @@ {{ item.data.text|markdown|raw }} -