diff --git a/javascript/freepost.js b/javascript/freepost.js index 7bc62b6..df13829 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 51edb45..703c772 100644 --- a/template/edit_comment.twig +++ b/template/edit_comment.twig @@ -14,7 +14,10 @@ {{ item.data.text|markdown|raw }} -
+ {# "shortcut-submit" is a class used exclusively from javascript + # to submit the form when a key (Ctrl+Enter) is pressed. + #} +
diff --git a/template/edit_post.twig b/template/edit_post.twig index 8ff85a2..e88954e 100644 --- a/template/edit_post.twig +++ b/template/edit_post.twig @@ -1,7 +1,10 @@ {% include 'header.twig' %}
- + {# "shortcut-submit" is a class used exclusively from javascript + # to submit the form when a key (Ctrl+Enter) is pressed. + #} +

Title (required)

diff --git a/template/post.twig b/template/post.twig index cad9509..6e60c4f 100644 --- a/template/post.twig +++ b/template/post.twig @@ -36,7 +36,10 @@
{% if user %} - + {# "shortcut-submit" is a class used exclusively from javascript + # to submit the form when a key (Ctrl+Enter) is pressed. + #} + diff --git a/template/reply.twig b/template/reply.twig index 5cd3bcf..7ff15fe 100644 --- a/template/reply.twig +++ b/template/reply.twig @@ -12,7 +12,10 @@ {{ comment.text|markdown|raw }}
-
+ {# "shortcut-submit" is a class used exclusively from javascript + # to submit the form when a key (Ctrl+Enter) is pressed. + #} +
diff --git a/template/submit.twig b/template/submit.twig index 7c63464..42ac21f 100644 --- a/template/submit.twig +++ b/template/submit.twig @@ -1,6 +1,10 @@ {% include 'header.twig' %} - + +{# "shortcut-submit" is a class used exclusively from javascript + # to submit the form when a key (Ctrl+Enter) is pressed. + #} +

Title (required)