Skip to content

Commit 28cbe8e

Browse files
authored
feat(ui): add dark theme for editor and adjust some ui details (laike9m#24)
* Update styles and error message format * Add dark theme support to code mirror * Add theme initialization to code editor. * Rename the variable for shared CodeMirror options
1 parent c4942ef commit 28cbe8e

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

templates/base.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<!DOCTYPE html>
22
<html lang="en" data-theme="light">
3-
43
<head>
54
<meta charset="UTF-8">
65
<meta name="viewport" content="width=device-width, initial-scale=1.0">
76
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css">
7+
<style type="text/css">
8+
/* override some style in picocss */
9+
h1, h2, h3, h4, h5, h6,
10+
p {
11+
margin-bottom: 0;
12+
}
13+
</style>
814
{% block head %}{% endblock %}
915
</head>
1016

templates/challenge.html

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{{ super() }}
55

66
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.15/codemirror.min.css" />
7+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.15/theme/material-darker.min.css" />
78
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.15/codemirror.min.js"></script>
89
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.15/mode/python/python.min.js"></script>
910
<title>Python Type Challenge - {{ name }}</title>
@@ -59,6 +60,11 @@
5960
margin-right: 20px;
6061
}
6162

63+
.challenge-header__exerpt {
64+
margin-bottom: 1.5rem;
65+
line-height: 1.5;
66+
}
67+
6268
.challenge-main {
6369
display: flex;
6470
justify-content: space-between;
@@ -168,7 +174,7 @@
168174
<span>Challenge - {{ name }}</span>
169175
{%include 'components/badge.html' with context %}
170176
</div>
171-
<p class="challenge-header__exerpt" style="margin-top: 0; line-height: 1.5">
177+
<p class="challenge-header__exerpt">
172178
Complete code following the instructions below, so that there are no type errors
173179
throughout the test code except for those lines followed by a <code># expect-type-error</code> comment.
174180
You don't need to implement the function, just add type annotations.
@@ -194,24 +200,32 @@
194200
</div>
195201

196202
<script type="text/javascript">
197-
let codeMirrorShared = {
203+
let initTheme = localStorage.getItem('theme') === 'dark' ? "material-darker" : "default"
204+
let sharedCodeMirrorOptions = {
198205
mode: "python",
199206
lineWrapping: true,
200207
lineNumbers: true,
201208
indentUnit: 4,
209+
theme: initTheme,
202210
}
203211
let code_under_test = {{ code_under_test | tojson }};
204212
let myCodeMirror = CodeMirror(document.getElementById("editor"), {
205213
value: code_under_test,
206-
...codeMirrorShared,
214+
...sharedCodeMirrorOptions,
207215
});
208216
let test_code = {{ test_code | tojson }};
209-
CodeMirror(document.getElementById("tests"), {
217+
let testCodeMirror = CodeMirror(document.getElementById("tests"), {
210218
value: test_code,
211219
readOnly: "nocursor",
212-
...codeMirrorShared,
220+
...sharedCodeMirrorOptions,
213221
});
214222

223+
window.addEventListener('themeSwitch', function (event) {
224+
let theme = localStorage.getItem('theme') === 'dark' ? "material-darker" : "default"
225+
myCodeMirror.setOption("theme", theme);
226+
testCodeMirror.setOption("theme", theme);
227+
})
228+
215229
let runButton = document.getElementById('run-button')
216230
runButton.onclick = function () {
217231
// set button spinner

templates/components/darkmode.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
themeSwitch.checked = true;
5555
}
5656
}
57+
58+
let themeSwitchEvt = new Event('themeSwitch');
5759
themeSwitch.addEventListener('change', function (event) {
5860
if (event.target.checked) {
5961
document.documentElement.setAttribute('data-theme', 'dark');
@@ -62,6 +64,8 @@
6264
document.documentElement.setAttribute('data-theme', 'light');
6365
localStorage.setItem('theme', 'light');
6466
}
67+
// dispatch the event used to update the code mirror theme
68+
window.dispatchEvent(themeSwitchEvt);
6569
});
6670
</script>
6771
</div>

templates/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313
align-items: center;
1414
margin: 10px auto;
1515
}
16+
17+
.title {
18+
text-align: center;
19+
margin-bottom: 1.5rem;
20+
}
1621
</style>
1722
{% endblock %}
1823

1924
{% block content %}
2025
<div class="container">
21-
<h1 style="text-align: center;">Welcome to Python Type Challenges!</h1>
26+
<h1 class="title">Welcome to Python Type Challenges!</h1>
2227
{% include 'components/challenge_list.html' %}
2328
</div>
2429
{% endblock%}

views/views.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import gc
21
import platform
32

4-
from flask import Blueprint, make_response, redirect, render_template, request
3+
from flask import Blueprint, redirect, render_template, request
54

65
from .utils import challenge_manager
76

@@ -39,6 +38,6 @@ def run_challenge(name):
3938
if result.passed:
4039
return "<h2>✅ Congratulations! You completed the challenge 🎉</h2>"
4140

42-
error_message = "<h2>❌ Challenge failed 😢\n\n</h2>"
43-
error_message += f"\nError:\n{result.stdout}{result.stderr}\n\n"
41+
error_message = "<h2>❌ Challenge failed 😢</h2>"
42+
error_message += f"<p>Error:\n{result.stdout}{result.stderr}</p>"
4443
return error_message

0 commit comments

Comments
 (0)