I asked a discussion question about whether or not giving tabs formatting functionality would improve UX, and after trial and error on this solution, would like to present it as a feature request.
According to Anna Lear ♦︎, if we get some support for a method of doing this that doesn't screw anything up, cause problems, nor damage tab order, we might be able to say goodbye to hitting the space-bar 4 times for good.
Here's my solution. Should it be implemented in SE?
Easy tab formatting with key combo Shift + Space
While using <code></code>
to target the easy tab formatting (it was my first suggestion) is a bit rough around the edges, I think you'll find that Shift + Space offers a shortcut that appears to have no downside.
Advantages
No keyboard trap. It doesn't affect Tab Order in any way.
Doesn't break anything. No native functionality in textarea nor OS (that was the issue with Alt + Tab in Windows 8) uses this key combination.
Doesn't require an interface change. This solution is silent, and therefore can be utilized by anyone who wishes to use the functionality without bothering anyone who doesn't.
The code to do this is minimal. Like my other suggestion, the solution is barely over 300 bytes added to the site. Again, credit to dandavis for the original code.
Try it for yourself right now in SE
Go ahead. Put it into the JS console of any Stack Exchange question page, and use Shift + Space for your formatting tabs. Or try the JSFiddle.
$("textarea").keydown(function (e) {
var t = this.value,
n = this.selectionStart,
r = [t.slice(0, n), t.slice(n)];
if (e.shiftKey && e.keyCode == 32) {
this.value = r.join(" ");
this.selectionStart = this.selectionEnd = n + 4;
e.preventDefault()
}
})
Fancier Version... These features aren't part of the feature request!! I'm happy with simply getting the raw shortcut, however, after adding the suggested "bug" fixes and having the code reviewed, this version demonstrates some features worthy of consideration. Here's a nice, well written version after review by David Harkness, demonstrating additional functionality (I've re-uglied it since his review):
$("textarea").keydown(function (e) {
if (e.shiftKey && e.keyCode === 32) {
var pad = " ",
text = this.value,
start = this.selectionStart,
end = this.selectionEnd,
scroll = this.scrollTop,
before = text.slice(0, start),
after = text.slice(start),
selection = text.slice(start, end),
prevChar = before.charAt(before.length - 1),
prevFourChar = "",
beforeLess = before.slice(0, start - 4);
if (e.ctrlKey) {
for (var i = 1; i < 5; i++) {
prevFourChar += before.charAt(before.length - i);
}
if (prevFourChar === pad) {
selection = selection.split("\n" + pad).join("\n");
this.value = beforeLess + selection + after;
this.selectionStart = start - pad.length;
this.selectionEnd = end - pad.length;
}
} else if (prevChar === "" || prevChar === " " || prevChar === "\n") {
selection = pad + selection.split("\n").join("\n" + pad);
this.value = before + selection + after;
this.selectionStart = start + pad.length;
this.selectionEnd = end + pad.length;
}
this.scrollTop = scroll;
e.preventDefault();
}
});
This version demonstrates the inclusion of:
White-space, newline, and line-start targeting, eliminating accidental misuse in the case of hitting space-bar after holding shift to capitalize a letter.
Outdenting support with Ctrl + Shift + Spacebar (Alt + Shift + Space has a collision with a native Windows 8 shortcut).
Multi-line support, allowing a larger selection to be indented and outdented, sort of replacing Ctrl + K to keep the functionality all together with those three keys.
Alternate Shortcut Keys that use Shift for Outdenting
As mentioned in this code review, Shift is normally used to reverse the functionality of a key combo. The only suitable option that adheres to this pattern without any shortcut collisions (that I know of) is:
Indent: Ctrl + Spacebar
Outdent: Shft + Ctrl + Spacebar
Both combinations are comfortable to use based on key position. This one simply offers conformity to Shift's customary functionality.
Shift
+Space
is jump up, inverse ofSpace
; or you mean only on edit field? Just some clarification.$0.02
.