|
48 | 48 | </div> |
49 | 49 | </div> |
50 | 50 |
|
51 | | - |
52 | 51 | <script type="text/javascript"> |
53 | | - let confetti = new JSConfetti(); |
54 | | - let initTheme = localStorage.getItem('theme') === 'dark' ? "material-darker" : "default" |
55 | | - let sharedCodeMirrorOptions = { |
56 | | - mode: "python", |
57 | | - lineWrapping: true, |
58 | | - lineNumbers: true, |
59 | | - indentUnit: 4, |
60 | | - theme: initTheme, |
61 | | - } |
62 | | - let code_under_test = {{ code_under_test | tojson }}; |
63 | | - let myCodeMirror = CodeMirror(document.getElementById("editor"), { |
64 | | - value: code_under_test, |
65 | | - ...sharedCodeMirrorOptions, |
66 | | - }); |
67 | | - let test_code = {{ test_code | tojson }}; |
68 | | - let testCodeMirror = CodeMirror(document.getElementById("tests"), { |
69 | | - value: test_code, |
70 | | - readOnly: "nocursor", |
71 | | - ...sharedCodeMirrorOptions, |
72 | | - }); |
| 52 | + function renderCodeArea(code_under_test, test_code, level, name) { |
| 53 | + let confetti = new JSConfetti(); |
| 54 | + let initTheme = localStorage.getItem('theme') === 'dark' ? "material-darker" : "default" |
| 55 | + let sharedCodeMirrorOptions = { |
| 56 | + mode: "python", |
| 57 | + lineWrapping: true, |
| 58 | + lineNumbers: true, |
| 59 | + indentUnit: 4, |
| 60 | + theme: initTheme, |
| 61 | + } |
| 62 | + let myCodeMirror = CodeMirror(document.getElementById("editor"), { |
| 63 | + value: code_under_test, |
| 64 | + ...sharedCodeMirrorOptions, |
| 65 | + }); |
| 66 | + let testCodeMirror = CodeMirror(document.getElementById("tests"), { |
| 67 | + value: test_code, |
| 68 | + readOnly: "nocursor", |
| 69 | + ...sharedCodeMirrorOptions, |
| 70 | + }); |
73 | 71 |
|
74 | | - let playgroundLink = document.getElementById("playground-link"); |
75 | | - playgroundLink.addEventListener('click', function (event) { |
76 | | - const code = myCodeMirror.getValue() + testCodeMirror.getValue(); |
77 | | - this.href = "https://pyright-play.net/?code=" + LZString.compressToEncodedURIComponent(code); |
78 | | - }); |
| 72 | + let playgroundLink = document.getElementById("playground-link"); |
| 73 | + playgroundLink.addEventListener('click', function (event) { |
| 74 | + const code = myCodeMirror.getValue() + testCodeMirror.getValue(); |
| 75 | + this.href = "https://pyright-play.net/?code=" + LZString.compressToEncodedURIComponent(code); |
| 76 | + }); |
79 | 77 |
|
80 | | - window.addEventListener('themeSwitch', function (event) { |
81 | | - let theme = localStorage.getItem('theme') === 'dark' ? "material-darker" : "default" |
82 | | - myCodeMirror.setOption("theme", theme); |
83 | | - testCodeMirror.setOption("theme", theme); |
84 | | - }) |
| 78 | + window.addEventListener('themeSwitch', function (event) { |
| 79 | + let theme = localStorage.getItem('theme') === 'dark' ? "material-darker" : "default" |
| 80 | + myCodeMirror.setOption("theme", theme); |
| 81 | + testCodeMirror.setOption("theme", theme); |
| 82 | + }) |
85 | 83 |
|
86 | | - let runButton = document.getElementById('run-button') |
87 | | - runButton.onclick = function () { |
88 | | - console.log(`Run challenge at: ${new Date().toLocaleString()}`); |
| 84 | + let runButton = document.getElementById('run-button') |
| 85 | + runButton.onclick = function () { |
| 86 | + console.log(`Run challenge at: ${new Date().toLocaleString()}`); |
89 | 87 |
|
90 | | - // set button spinner |
91 | | - let rawInnerText = runButton.innerText; |
92 | | - runButton.ariaBusy = true; |
93 | | - runButton.innerText = "" |
| 88 | + // set button spinner |
| 89 | + let rawInnerText = runButton.innerText; |
| 90 | + runButton.ariaBusy = true; |
| 91 | + runButton.innerText = "" |
94 | 92 |
|
95 | | - let code = myCodeMirror.getValue(); |
96 | | - fetch('/run/{{level}}/{{name}}', { |
97 | | - method: 'POST', |
98 | | - body: code |
99 | | - }) |
100 | | - .then(response => response.json()) |
101 | | - .then(json => { |
102 | | - // add confetti effect when passed |
103 | | - if (json.passed) { |
104 | | - confetti.addConfetti() |
105 | | - } |
106 | | - setTimeout(() => { |
107 | | - document.getElementById('answer-link').style.display = 'block'; |
108 | | - }, 500); |
109 | | - document.getElementById("result").innerHTML = json.message; |
110 | | - if (json.debug_info !== undefined) { |
111 | | - console.log(json.debug_info); |
112 | | - } |
113 | | - }) |
114 | | - .catch((error) => { |
115 | | - console.error('Error:', error); |
| 93 | + let code = myCodeMirror.getValue(); |
| 94 | + fetch(`/run/${level}/${name}`, { |
| 95 | + method: 'POST', |
| 96 | + body: code |
116 | 97 | }) |
117 | | - .finally(() => { |
118 | | - // reset button spinner |
119 | | - runButton.ariaBusy = false; |
120 | | - runButton.innerText = rawInnerText; |
121 | | - }); |
122 | | - }; |
| 98 | + .then(response => response.json()) |
| 99 | + .then(json => { |
| 100 | + // add confetti effect when passed |
| 101 | + if (json.passed) { |
| 102 | + confetti.addConfetti() |
| 103 | + } |
| 104 | + setTimeout(() => { |
| 105 | + document.getElementById('answer-link').style.display = 'block'; |
| 106 | + }, 500); |
| 107 | + document.getElementById("result").innerHTML = json.message; |
| 108 | + if (json.debug_info !== undefined) { |
| 109 | + console.log(json.debug_info); |
| 110 | + } |
| 111 | + }) |
| 112 | + .catch((error) => { |
| 113 | + console.error('Error:', error); |
| 114 | + }) |
| 115 | + .finally(() => { |
| 116 | + // reset button spinner |
| 117 | + runButton.ariaBusy = false; |
| 118 | + runButton.innerText = rawInnerText; |
| 119 | + }); |
| 120 | + }; |
123 | 121 |
|
124 | | - let resetButton = document.getElementById('reset-button') |
125 | | - resetButton.onclick = function () { |
126 | | - myCodeMirror.setValue(code_under_test); |
127 | | - }; |
| 122 | + let resetButton = document.getElementById('reset-button') |
| 123 | + resetButton.onclick = function () { |
| 124 | + myCodeMirror.setValue(code_under_test); |
| 125 | + }; |
128 | 126 |
|
129 | | - // Set up hints events and functions |
130 | | - let hintBtn = document.getElementById('read-hints') |
131 | | - hintBtn.onclick = function () { |
132 | | - // Toggle the display of the hints message. |
133 | | - let msgElem = document.getElementsByClassName('hints-message')[0]; |
134 | | - if (msgElem.style.display === 'block') { |
135 | | - msgElem.style.display = 'none'; |
136 | | - } else { |
137 | | - msgElem.style.display = 'block'; |
| 127 | + // Set up hints events and functions |
| 128 | + let hintBtn = document.getElementById('read-hints') |
| 129 | + if (hintBtn !== null) { |
| 130 | + hintBtn.onclick = function () { |
| 131 | + // Toggle the display of the hints message. |
| 132 | + let msgElem = document.getElementsByClassName('hints-message')[0]; |
| 133 | + if (msgElem.style.display === 'block') { |
| 134 | + msgElem.style.display = 'none'; |
| 135 | + } else { |
| 136 | + msgElem.style.display = 'block'; |
| 137 | + } |
| 138 | + }; |
| 139 | + // Make sure to open links in hints in new tab. |
| 140 | + document.querySelectorAll('.hints-message a').forEach(function(elem) { |
| 141 | + elem.setAttribute('target', '_blank'); |
| 142 | + }) |
138 | 143 | } |
139 | | - }; |
140 | | - // Make sure to open links in hints in new tab. |
141 | | - document.querySelectorAll('.hints-message a').forEach(function(elem) { |
142 | | - elem.setAttribute('target', '_blank'); |
143 | | - }) |
144 | 144 |
|
145 | | - // Make sure the current challenge is visible to user. |
146 | | - activeChallengeInList = document.getElementById("{{level}}-{{name}}"); |
147 | | - activeChallengeInList.scrollIntoView({block: 'center'}); |
148 | | - activeChallengeInList.classList.add('active-challenge'); // Highlight |
| 145 | + // Make sure the current challenge is visible to user. |
| 146 | + activeChallengeInList = document.getElementById(`${level}-${name}`); |
| 147 | + activeChallengeInList.classList.add('active-challenge'); // Highlight |
| 148 | + } |
| 149 | + |
| 150 | + codeUnderTest = {{code_under_test | tojson}}; |
| 151 | + testCode = {{ test_code | tojson }}; |
| 152 | + renderCodeArea(codeUnderTest, testCode, "{{level}}", "{{name}}"); |
149 | 153 | </script> |
0 commit comments