Download this file
| 1 | /* shortcuts.js — ⌘↩ (Ctrl+Enter elsewhere) submits the form a textarea is in. |
| 2 | * |
| 3 | * Progressive enhancement only: every form still submits with its button, |
| 4 | * scripts on or off. requestSubmit rather than submit(), so the browser's own |
| 5 | * validation runs and busy.js sees an ordinary submit event — the same path |
| 6 | * as a click, never a second one. |
| 7 | * |
| 8 | * aria-keyshortcuts announces the shortcut to assistive technology; it is set |
| 9 | * here rather than in the templates because the shortcut only exists when |
| 10 | * this script does. |
| 11 | */ |
| 12 | (function () { |
| 13 | "use strict"; |
| 14 | |
| 15 | function mark(root) { |
| 16 | root.querySelectorAll("form textarea").forEach(function (t) { |
| 17 | if (!t.hasAttribute("aria-keyshortcuts")) { |
| 18 | t.setAttribute("aria-keyshortcuts", "Meta+Enter Control+Enter"); |
| 19 | } |
| 20 | }); |
| 21 | } |
| 22 | |
| 23 | document.addEventListener("keydown", function (e) { |
| 24 | if (e.key !== "Enter" || !(e.metaKey || e.ctrlKey) || e.isComposing) return; |
| 25 | var t = e.target; |
| 26 | if (!t || t.tagName !== "TEXTAREA" || !t.form) return; |
| 27 | e.preventDefault(); |
| 28 | if (typeof t.form.requestSubmit === "function") { |
| 29 | t.form.requestSubmit(); |
| 30 | } else { |
| 31 | t.form.submit(); |
| 32 | } |
| 33 | }); |
| 34 | |
| 35 | if (document.readyState === "loading") { |
| 36 | document.addEventListener("DOMContentLoaded", function () { mark(document); }); |
| 37 | } else { |
| 38 | mark(document); |
| 39 | } |
| 40 | })(); |
| 41 | |