From 051f6c5a7fd16d00ac2a7298b9ebda2a94871b05 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Thu, 22 Mar 2018 18:03:57 +0200 Subject: [PATCH] Autocomplete bracket and quote characters like in a modern IDE --- client/js/keybinds.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/client/js/keybinds.js b/client/js/keybinds.js index a9469f49..354feda9 100644 --- a/client/js/keybinds.js +++ b/client/js/keybinds.js @@ -127,6 +127,29 @@ for (const hotkey in colorsHotkeys) { }); } +// Autocomplete bracket and quote characters like in a modern IDE +// For example, select `text`, press `[` key, and it becomes `[text]` +const bracketWraps = { + '"': '"', + "'": "'", + "(": ")", + "<": ">", + "[": "]", + "{": "}", + "*": "*", + "`": "`", + "~": "~", + _: "_", +}; + +inputTrap.bind(Object.keys(bracketWraps), function(e) { + if (e.target.selectionStart !== e.target.selectionEnd) { + wrapCursor(e.target, e.key, bracketWraps[e.key]); + + return false; + } +}); + // Ignored keys which should not automatically focus the input bar const ignoredKeys = { 8: true, // Backspace