Skip to content

Commit

Permalink
allow user to edit all code; update layout for more code editor space
Browse files Browse the repository at this point in the history
  • Loading branch information
gregsdennis committed Jun 12, 2024
1 parent cdb6b9b commit 44c932f
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 54 deletions.
3 changes: 1 addition & 2 deletions LearnJsonEverything/Services/CompilationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public static (ILessonRunner<T>?, string[]) GetRunner<T>(LessonData lesson)
if (_references is null)
return (null, ["Compilation assemblies still loading. Please wait until complete and try again."]);

var fullSource = lesson.ContextCode
.Replace("/* USER CODE */", lesson.UserCode ?? string.Empty);
var fullSource = lesson.UserCode ?? string.Empty;

Console.WriteLine($"Compiling...\n\n{fullSource}");

Expand Down
7 changes: 0 additions & 7 deletions LearnJsonEverything/Services/InstructionsBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Text;
using System.Text.Json.Nodes;
using Humanizer;
using Json.More;

namespace LearnJsonEverything.Services;

Expand All @@ -19,12 +18,6 @@ public static class InstructionsBuilder
/* INSTRUCTIONS */
#### Code template
```csharp
/* CONTEXT CODE */
```
#### Tests
/* TESTS */
Expand Down
7 changes: 5 additions & 2 deletions LearnJsonEverything/Services/Runners/SchemaHost.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Json.Schema;
using System.Text.Json;
using Json.Schema;

namespace LearnJsonEverything.Services.Runners;

Expand All @@ -16,9 +17,11 @@ public string[] Run(LessonData lesson)
foreach (var test in lesson.Tests)
{
var expectedValidity = test!["isValid"]!.GetValue<bool>();
Console.WriteLine($"Running `{test["instance"].Print()}`");
var result = runner.Run(test.AsObject());
Console.WriteLine($"Result: {JsonSerializer.Serialize(result, SerializerContext.Default.EvaluationResults)}");
correct &= expectedValidity == result.IsValid;
results.Add($"{(expectedValidity == result.IsValid ? Iconography.SuccessIcon : Iconography.ErrorIcon)} {test["instance"]!.Print()}");
results.Add($"{(expectedValidity == result.IsValid ? Iconography.SuccessIcon : Iconography.ErrorIcon)} {test["instance"].Print()}");
}

lesson.Achieved |= correct;
Expand Down
1 change: 1 addition & 0 deletions LearnJsonEverything/Services/SerializationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static string ToLiteral(this string valueTextForCompiler)
}

[JsonSerializable(typeof(JsonSchema))]
[JsonSerializable(typeof(EvaluationResults))]
[JsonSerializable(typeof(JsonNode))]
[JsonSerializable(typeof(JsonObject))]
[JsonSerializable(typeof(JsonArray))]
Expand Down
11 changes: 10 additions & 1 deletion LearnJsonEverything/Shared/Teacher.razor
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<button class="btn btn-primary m-1" disabled="@_previousButtonDisabled" @onclick="PreviousLesson">&lt; Previous</button>
<button class="btn btn-primary m-1" @onclick="Run">Run</button>
<button class="btn btn-primary m-1" @onclick="RevealSolution">Reveal Solution</button>
<button class="btn btn-primary m-1" @onclick="ResetLesson">Reset</button>
<button class="btn btn-primary m-1" disabled="@_nextButtonDisabled" @onclick="NextLesson">Next &gt;</button>
</div>
</div>
Expand Down Expand Up @@ -108,6 +109,14 @@
return _codeEditor.SetValue(_currentLesson?.Solution ?? string.Empty);
}

private async Task ResetLesson()
{
_currentLesson!.UserCode = null;
_currentLesson.Achieved = false;
await LoadLesson();
await SaveLessonPlanCompletion();
}

private Task PreviousLesson()
{
_currentLesson = _lessons.GetPrevious(_currentLesson?.Id);
Expand All @@ -130,7 +139,7 @@
_currentLesson ??= _lessons[0];
Instructions = InstructionsBuilder.BuildInstructions(_currentLesson);
_nextButtonDisabled = !CanMoveToNextLesson();
await _codeEditor.SetValue(_currentLesson.UserCode ?? string.Empty);
await _codeEditor.SetValue(_currentLesson.UserCode ?? _currentLesson.ContextCode);
await _outputEditor.SetValue(string.Empty);
UpdateNavigation();
await JsRuntime.InvokeVoidAsync("BlazorScrollToTopOfElement", "instructions-panel");
Expand Down
7 changes: 5 additions & 2 deletions LearnJsonEverything/Shared/Teacher.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
padding: 5px;
}

#workspace, #output {
height: calc(50% - 45px/2);
#workspace {
height: calc(70% - 45px/2);
}
#output {
height: calc(30% - 45px/2);
}

#controls {
Expand Down
20 changes: 18 additions & 2 deletions LearnJsonEverything/wwwroot/data/lessons/path.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
title: Parsing
instructions: |
Parse the given JSON Path text into a `path` variable.
inputTemplate: ''
contextCode: |-
using System.Text.Json;
using System.Text.Json.Nodes;
Expand All @@ -42,7 +41,24 @@
}
}
solution: |-
var path = JsonPath.Parse(pathText);
using System.Text.Json;
using System.Text.Json.Nodes;
using Json.Path;
namespace LearnJsonEverything;
public class Lesson : ILessonRunner<PathResult>
{
public PathResult Run(JsonObject test)
{
var data = test["data"];
var pathText = "$.foo.bar";
var path = JsonPath.Parse(pathText);
return path.Evaluate(data);
}
}
tests:
- data: { "foo": { "bar": "a string" } }
result: ['a string']
Loading

0 comments on commit 44c932f

Please sign in to comment.