Skip to content

Commit 63487ff

Browse files
author
Aaron Wallentine
committed
Some usability improvements, and grammar fixes.
- The code that automatically pads the bottom of the page so that the user is still able to scroll down and see the bottom of the page even with the console expanded now works on all pages (previously was only working correctly on error pages). - Improve keyboard shortcut handling for 'Home', 'End', 'Ctrl-P', 'Ctrl-N', and 'Ctrl-E'. - 'Home' and 'End' go to beginning and end of line; 'Ctrl-E' end of line; 'Ctrl-P' and 'Ctrl-N' navigate forward and back through command history, just like up and down arrows, to match default key mappings in bash. - Add .moveToBeginning and .moveToEnd helper functions to the REPLConsole JS, and use them in key input handlers to make them easier to read. - Improve styling: font sizes and heights are in rems to adjust to font size in browser; close button is a little bigger and offset from the right margin to be more easily usable in browsers that render the scrollbar over the content. (Some widgets in Gnome shell in Ubuntu do this, maybe others also.) - Some small grammar and English usage fixes in comments and README. Details: - Add a container element around the web console so we can more easily add padding to keep it from obscuring the page - DRY the code that adds a margin to the bottom of the page so that user is still able to scroll down and see the bottom of the page even with the console expanded; make it so it works whenever the console is loaded, not just on error pages. - Remove the extra now-redundant code from the error_page.js.erb and regular_page.js.erb; it now lives in main.js.erb. - It wasn't actually working before on regular pages anyway. - Change font sizes used in console from pixels to rems so they will auto-adjust to font size used on the page. This also makes them just a little bigger by default (unless the page font size is very small). - Increase the size of the "close" button slightly so it is easier to hit with the mouse, and also move it slightly away from the right side, so that it doesn't render under the scrollbar on some browsers (usually on Linux, in my experience; the default Gnome shell scrollbars in Ubuntu 20 made it very hard to use). - Add key handling for 'Home' and 'End' keys. - Change Ctrl-E handler to move cursor to end of line, to match default bash builtin key mappings. - Add Ctrl-P and Ctrl-N handlers to navigate previous and next in command history. - Add .moveToBeginning and .moveToEnd helper functions to the REPLConsole JS, and use them in key input handlers to make them easier to read. - Fix grammar and English usage just a little bit in some comments and README.
1 parent 997a20c commit 63487ff

9 files changed

+83
-84
lines changed

README.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ By default, only requests coming from IPv4 and IPv6 localhosts are allowed.
6767
`config.web_console.permissions` lets you control which IP's have access to
6868
the console.
6969

70-
You can allow single IP's or whole networks. Say you want to share your
70+
You can allow single IPs or whole networks. Say you want to share your
7171
console with `192.168.0.100`:
7272

7373
```ruby

lib/web_console/context.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def initialize(binding)
88
end
99

1010
# Extracts entire objects which can be called by the current session unless
11-
# the inputs is present.
11+
# the input is present.
1212
#
1313
# Otherwise, it extracts methods and constants of the object specified by
1414
# the input.
+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
<div id="console"
2-
data-mount-point='<%= @mount_point %>'
3-
data-session-id='<%= @session.id %>'
4-
data-prompt-label='>> '>
5-
</div>
1+
<div id="console-container">
2+
<div id="console"
3+
data-mount-point='<%= @mount_point %>'
4+
data-session-id='<%= @session.id %>'
5+
data-prompt-label='>> '>
6+
</div>
7+
</div>

lib/web_console/templates/console.js.erb

+25-8
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,14 @@ REPLConsole.prototype.onTabKey = function() {
661661
});
662662
};
663663

664+
REPLConsole.prototype.moveToBeginning = function() {
665+
this.setInput(this._input, 0);
666+
};
667+
668+
REPLConsole.prototype.moveToEnd = function() {
669+
this.setInput(this._input, this._input.length);
670+
};
671+
664672
REPLConsole.prototype.onNavigateHistory = function(offset) {
665673
var command = this.commandStorage.navigate(offset) || "";
666674
this.setInput(command);
@@ -680,14 +688,25 @@ REPLConsole.prototype.onKeyDown = function(ev) {
680688
switch (ev.keyCode) {
681689
case 65: // Ctrl-A
682690
if (ev.ctrlKey) {
683-
this.setInput(this._input, 0);
691+
// this.setInput(this._input, 0);
692+
this.moveToBeginning();
684693
ev.preventDefault();
685694
}
686695
break;
687696

697+
case 36: // 'Home' key
698+
this.moveToBeginning();
699+
ev.preventDefault();
700+
break;
701+
702+
case 35: // 'End' key
703+
this.moveToEnd();
704+
ev.preventDefault();
705+
break;
706+
688707
case 69: // Ctrl-E
689708
if (ev.ctrlKey) {
690-
this.onTabKey();
709+
this.moveToEnd();
691710
ev.preventDefault();
692711
}
693712
break;
@@ -706,18 +725,16 @@ REPLConsole.prototype.onKeyDown = function(ev) {
706725
}
707726
break;
708727

709-
case 69: // Ctrl-E
710-
if (ev.ctrlKey) {
711-
this.onTabKey();
712-
ev.preventDefault();
713-
}
714-
break;
715728

716729
case 80: // Ctrl-P
717730
if (! ev.ctrlKey) break;
731+
this.onNavigateHistory(-1);
732+
break;
718733

719734
case 78: // Ctrl-N
720735
if (! ev.ctrlKey) break;
736+
this.onNavigateHistory(1);
737+
break;
721738

722739
case 9: // Tab
723740
this.onTabKey();
+4-36
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// Try intercept traces links in Rails 4.2.
1+
// Try to intercept traces links in Rails 4.2.
22
var traceFrames = document.getElementsByClassName('trace-frames');
33
var selectedFrame, currentSource = document.getElementById('frame-source-0');
44

5-
// Add click listeners for all stack frames
5+
// Add click listeners for all stack frames.
66
for (var i = 0; i < traceFrames.length; i++) {
77
traceFrames[i].addEventListener('click', function(e) {
88
e.preventDefault();
@@ -17,7 +17,7 @@ for (var i = 0; i < traceFrames.length; i++) {
1717
return target.innerHTML;
1818
});
1919

20-
// Change the extracted source code
20+
// Change the extracted source code.
2121
changeSourceExtract(frameId);
2222
});
2323
}
@@ -34,36 +34,4 @@ function changeSourceExtract(frameId) {
3434
el.className = el.className.replace(" hidden", "");
3535
currentSource = el;
3636
}
37-
}
38-
39-
// Push the error page body upwards the size of the console.
40-
//
41-
// While, I wouldn't like to do that on every custom page (so I don't screw
42-
// user's layouts), I think a lot of developers want to see all of the content
43-
// on the default Rails error page.
44-
//
45-
// Since it's quite special as is now, being a bit more special in the name of
46-
// better user experience, won't hurt.
47-
document.addEventListener('DOMContentLoaded', function() {
48-
var consoleElement = document.getElementById('console');
49-
var resizerElement = consoleElement.getElementsByClassName('resizer')[0];
50-
var containerElement = document.getElementById('container');
51-
52-
function setContainerElementBottomMargin(pixels) {
53-
containerElement.style.marginBottom = pixels + 'px';
54-
}
55-
56-
var currentConsoleElementHeight = consoleElement.offsetHeight;
57-
setContainerElementBottomMargin(currentConsoleElementHeight);
58-
59-
resizerElement.addEventListener('mousedown', function(event) {
60-
function recordConsoleElementHeight(event) {
61-
resizerElement.removeEventListener('mouseup', recordConsoleElementHeight);
62-
63-
var currentConsoleElementHeight = consoleElement.offsetHeight;
64-
setContainerElementBottomMargin(currentConsoleElementHeight);
65-
}
66-
67-
resizerElement.addEventListener('mouseup', recordConsoleElementHeight);
68-
});
69-
});
37+
}

lib/web_console/templates/main.js.erb

+29
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
11
REPLConsole.installInto('console');
2+
3+
// Push the page body upwards by the size of the console
4+
// so it doesn't obscure any of the page content.
5+
//
6+
// An event handler is added to adjust the size of the margin
7+
// whenever the console is resized.
8+
document.addEventListener('DOMContentLoaded', function() {
9+
var consoleElement = document.getElementById('console');
10+
var resizerElement = consoleElement.getElementsByClassName('resizer')[0];
11+
var consoleContainerElement = document.getElementById('console-container');
12+
13+
function setConsoleContainerMargin(pixels) {
14+
consoleContainerElement.style.marginTop = pixels + 'px';
15+
}
16+
17+
var currentConsoleElementHeight = consoleElement.offsetHeight;
18+
setConsoleContainerMargin(currentConsoleElementHeight);
19+
20+
resizerElement.addEventListener('mousedown', function(event) {
21+
function recordConsoleElementHeight(event) {
22+
resizerElement.removeEventListener('mouseup', recordConsoleElementHeight);
23+
24+
var currentConsoleElementHeight = consoleElement.offsetHeight;
25+
setConsoleContainerMargin(currentConsoleElementHeight);
26+
}
27+
28+
resizerElement.addEventListener('mouseup', recordConsoleElementHeight);
29+
});
30+
});
+1-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1 @@
1-
// Push the error page body upwards the size of the console.
2-
document.addEventListener('DOMContentLoaded', function() {
3-
var consoleElement = document.getElementById('console');
4-
var resizerElement = consoleElement.getElementsByClassName('resizer')[0];
5-
var bodyElement = document.body;
6-
7-
function setBodyElementBottomMargin(pixels) {
8-
bodyElement.style.marginBottom = pixels + 'px';
9-
}
10-
11-
var currentConsoleElementHeight = consoleElement.offsetHeight;
12-
setBodyElementBottomMargin(currentConsoleElementHeight);
13-
14-
resizerElement.addEventListener('mousedown', function(event) {
15-
function recordConsoleElementHeight(event) {
16-
resizerElement.removeEventListener('mouseup', recordConsoleElementHeight);
17-
18-
var currentConsoleElementHeight = consoleElement.offsetHeight;
19-
setBodyElementBottomMargin(currentConsoleElementHeight);
20-
}
21-
22-
resizerElement.addEventListener('mouseup', recordConsoleElementHeight);
23-
});
24-
});
1+
// Nothing here, for now

lib/web_console/templates/style.css.erb

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#console-container {
2+
--console-initial-height: 10rem;
3+
margin-top: var(--console-initial-height);
4+
}
5+
16
.console .pos-absolute {
27
position: absolute;
38
}
@@ -32,7 +37,7 @@
3237
left: 0;
3338
bottom: 0;
3439
width: 100%;
35-
height: 148px;
40+
height: var(--console-initial-height);
3641
padding: 0;
3742
margin: 0;
3843
background: none repeat scroll 0% 0% #333;
@@ -46,7 +51,7 @@
4651

4752
.console .console-inner {
4853
font-family: monospace;
49-
font-size: 11px;
54+
font-size: 1rem;
5055
width: 100%;
5156
height: 100%;
5257
overflow: unset;
@@ -124,10 +129,11 @@
124129
cursor: pointer;
125130
border-radius: 1px;
126131
font-family: monospace;
127-
font-size: 13px;
128-
width: 14px;
129-
height: 14px;
130-
line-height: 14px;
132+
font-size: 1.2rem;
133+
width: 1.5rem;
134+
height: 1.5rem;
135+
margin-right: 1.5rem; /* allow room for scrollbar on right on some browsers */
136+
line-height: 1.4rem;
131137
text-align: center;
132138
color: #ccc;
133139
}

lib/web_console/view.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def only_on_regular_page(*args)
1717

1818
# Render JavaScript inside a script tag and a closure.
1919
#
20-
# This one lets write JavaScript that will automatically get wrapped in a
21-
# script tag and enclosed in a closure, so you don't have to worry for
22-
# leaking globals, unless you explicitly want to.
20+
# Allows writing JavaScript that will automatically get wrapped in
21+
# a script tag and enclosed in a closure, so we don't have to worry about
22+
# leaking globals, unless we explicitly want to.
2323
def render_javascript(template)
2424
assign(template: template)
2525
assign(nonce: @env["action_dispatch.content_security_policy_nonce"])

0 commit comments

Comments
 (0)