Skip to content

Commit

Permalink
add simple command history and input clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharpiro committed Jun 8, 2019
1 parent a04b8b8 commit 19e5a90
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
31 changes: 30 additions & 1 deletion Pages/Index.razor
Expand Up @@ -6,7 +6,7 @@
<div id="output-outer"><div id="output">@((MarkupString)Output)</div></div>
<div id="blank-line">&nbsp;</div>
<div id="input-row">
<span>&gt;&nbsp;</span><input id="input" bind="@Input" type="text" onkeyup="@Run" disabled="@Disabled" autofocus />
<span>&gt;&nbsp;</span><input id="input" bind="@Input" type="text" onkeydown="@OnKeyDown" onkeyup="@Run" disabled="@Disabled" autofocus />
</div>
</div>
</div>
Expand All @@ -20,6 +20,8 @@
private IEnumerable<MetadataReference> _references;
private object[] _submissionStates = new object[] { null, null };
private int _submissionIndex = 0;
private List<string> _history = new List<string>();
private int _historyIndex = 0;

protected async override Task OnInitAsync()
{
Expand All @@ -40,6 +42,28 @@
_references = references;
}

public void OnKeyDown(UIKeyboardEventArgs e)
{
if (e.Key == "ArrowUp" && _historyIndex > 0)
{
_historyIndex--;
Input = _history[_historyIndex];
}
else if (e.Key == "ArrowDown" && _historyIndex + 1 < _history.Count)
{
_historyIndex++;
Input = _history[_historyIndex];
}
// todo: doesn't work right when typing new command. Requires Enter to be pressed first sometimes
// has to do with DOM focus I think
// currently handling this with javascript as well
else if (e.Key == "Escape")
{
Input = "";
_historyIndex = _history.Count;
}
}

public async Task Run(UIKeyboardEventArgs e)
{
if (e.Key != "Enter")
Expand All @@ -48,6 +72,11 @@
}

var code = Input;
if (!string.IsNullOrEmpty(code))
{
_history.Add(code);
}
_historyIndex = _history.Count;
Input = "";

await RunSubmission(code);
Expand Down
14 changes: 14 additions & 0 deletions wwwroot/index.html
Expand Up @@ -10,6 +10,20 @@
<body>
<app>Loading...</app>

<script>
// todo: couldn't find a better way to wait for a razor element to load
setTimeout(() => {
input.onkeydown = handleSpecialKeys
}, 1000);
function handleSpecialKeys(event) {
if (event.key == "ArrowDown" || event.key == "ArrowUp") {
event.preventDefault();
}
else if(event.key == "Escape") {
event.srcElement.value = ""
}
}
</script>
<script src="_framework/blazor.webassembly.js"></script>
</body>
</html>

0 comments on commit 19e5a90

Please sign in to comment.