Skip to content

Commit 44c932f

Browse files
committed
allow user to edit all code; update layout for more code editor space
1 parent cdb6b9b commit 44c932f

File tree

9 files changed

+428
-54
lines changed

9 files changed

+428
-54
lines changed

LearnJsonEverything/Services/CompilationHelpers.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public static (ILessonRunner<T>?, string[]) GetRunner<T>(LessonData lesson)
7272
if (_references is null)
7373
return (null, ["Compilation assemblies still loading. Please wait until complete and try again."]);
7474

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

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

LearnJsonEverything/Services/InstructionsBuilder.cs

-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Text;
22
using System.Text.Json.Nodes;
33
using Humanizer;
4-
using Json.More;
54

65
namespace LearnJsonEverything.Services;
76

@@ -19,12 +18,6 @@ public static class InstructionsBuilder
1918
2019
/* INSTRUCTIONS */
2120
22-
#### Code template
23-
24-
```csharp
25-
/* CONTEXT CODE */
26-
```
27-
2821
#### Tests
2922
/* TESTS */
3023

LearnJsonEverything/Services/Runners/SchemaHost.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Json.Schema;
1+
using System.Text.Json;
2+
using Json.Schema;
23

34
namespace LearnJsonEverything.Services.Runners;
45

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

2427
lesson.Achieved |= correct;

LearnJsonEverything/Services/SerializationHelpers.cs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static string ToLiteral(this string valueTextForCompiler)
2828
}
2929

3030
[JsonSerializable(typeof(JsonSchema))]
31+
[JsonSerializable(typeof(EvaluationResults))]
3132
[JsonSerializable(typeof(JsonNode))]
3233
[JsonSerializable(typeof(JsonObject))]
3334
[JsonSerializable(typeof(JsonArray))]

LearnJsonEverything/Shared/Teacher.razor

+10-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<button class="btn btn-primary m-1" disabled="@_previousButtonDisabled" @onclick="PreviousLesson">&lt; Previous</button>
4848
<button class="btn btn-primary m-1" @onclick="Run">Run</button>
4949
<button class="btn btn-primary m-1" @onclick="RevealSolution">Reveal Solution</button>
50+
<button class="btn btn-primary m-1" @onclick="ResetLesson">Reset</button>
5051
<button class="btn btn-primary m-1" disabled="@_nextButtonDisabled" @onclick="NextLesson">Next &gt;</button>
5152
</div>
5253
</div>
@@ -108,6 +109,14 @@
108109
return _codeEditor.SetValue(_currentLesson?.Solution ?? string.Empty);
109110
}
110111

112+
private async Task ResetLesson()
113+
{
114+
_currentLesson!.UserCode = null;
115+
_currentLesson.Achieved = false;
116+
await LoadLesson();
117+
await SaveLessonPlanCompletion();
118+
}
119+
111120
private Task PreviousLesson()
112121
{
113122
_currentLesson = _lessons.GetPrevious(_currentLesson?.Id);
@@ -130,7 +139,7 @@
130139
_currentLesson ??= _lessons[0];
131140
Instructions = InstructionsBuilder.BuildInstructions(_currentLesson);
132141
_nextButtonDisabled = !CanMoveToNextLesson();
133-
await _codeEditor.SetValue(_currentLesson.UserCode ?? string.Empty);
142+
await _codeEditor.SetValue(_currentLesson.UserCode ?? _currentLesson.ContextCode);
134143
await _outputEditor.SetValue(string.Empty);
135144
UpdateNavigation();
136145
await JsRuntime.InvokeVoidAsync("BlazorScrollToTopOfElement", "instructions-panel");

LearnJsonEverything/Shared/Teacher.razor.css

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
padding: 5px;
33
}
44

5-
#workspace, #output {
6-
height: calc(50% - 45px/2);
5+
#workspace {
6+
height: calc(70% - 45px/2);
7+
}
8+
#output {
9+
height: calc(30% - 45px/2);
710
}
811

912
#controls {

LearnJsonEverything/wwwroot/data/lessons/path.yaml

+18-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
title: Parsing
2222
instructions: |
2323
Parse the given JSON Path text into a `path` variable.
24-
inputTemplate: ''
2524
contextCode: |-
2625
using System.Text.Json;
2726
using System.Text.Json.Nodes;
@@ -42,7 +41,24 @@
4241
}
4342
}
4443
solution: |-
45-
var path = JsonPath.Parse(pathText);
44+
using System.Text.Json;
45+
using System.Text.Json.Nodes;
46+
using Json.Path;
47+
48+
namespace LearnJsonEverything;
49+
50+
public class Lesson : ILessonRunner<PathResult>
51+
{
52+
public PathResult Run(JsonObject test)
53+
{
54+
var data = test["data"];
55+
var pathText = "$.foo.bar";
56+
57+
var path = JsonPath.Parse(pathText);
58+
59+
return path.Evaluate(data);
60+
}
61+
}
4662
tests:
4763
- data: { "foo": { "bar": "a string" } }
4864
result: ['a string']

0 commit comments

Comments
 (0)