|
161 | 161 | ?.addEventListener('change', () => (deleteBackupCheck.checked = true)); |
162 | 162 | } |
163 | 163 |
|
| 164 | + const renderArea = document.getElementById('pgedit-render-area'); |
| 165 | + |
| 166 | + const scrollToRenderArea = () => { |
| 167 | + // Scroll to the top of the render window if the current scroll position is below that. |
| 168 | + const renderAreaRect = renderArea.getBoundingClientRect(); |
| 169 | + const topBarHeight = document.querySelector('.webwork-logo')?.getBoundingClientRect().height ?? 0; |
| 170 | + if (renderAreaRect.top < topBarHeight) window.scrollBy(0, renderAreaRect.top - topBarHeight); |
| 171 | + }; |
| 172 | + |
164 | 173 | // Send a request to the server to perltidy the current PG code in the CodeMirror editor. |
165 | 174 | const tidyPGCode = () => { |
166 | 175 | const request_object = { courseID: document.getElementsByName('courseID')[0]?.value }; |
|
235 | 244 | .catch((err) => showMessage(`Error: ${err?.message ?? err}`)); |
236 | 245 | }; |
237 | 246 |
|
| 247 | + // Send a request to the server to run the PG critic in the CodeMirror editor. |
| 248 | + const runPGCritic = () => { |
| 249 | + const request_object = { courseID: document.getElementsByName('courseID')[0]?.value }; |
| 250 | + |
| 251 | + const user = document.getElementsByName('user')[0]; |
| 252 | + if (user) request_object.user = user.value; |
| 253 | + const sessionKey = document.getElementsByName('key')[0]; |
| 254 | + if (sessionKey) request_object.key = sessionKey.value; |
| 255 | + |
| 256 | + request_object.rpc_command = 'runPGCritic'; |
| 257 | + request_object.pgCode = |
| 258 | + webworkConfig?.pgCodeMirror?.source ?? document.getElementById('problemContents')?.value ?? ''; |
| 259 | + |
| 260 | + fetch(webserviceURL, { method: 'post', mode: 'same-origin', body: new URLSearchParams(request_object) }) |
| 261 | + .then((response) => response.json()) |
| 262 | + .then((data) => { |
| 263 | + if (data.error) throw new Error(data.error); |
| 264 | + if (!data.result_data) throw new Error('An invalid response was received.'); |
| 265 | + renderArea.innerHTML = data.result_data.html; |
| 266 | + scrollToRenderArea(); |
| 267 | + }) |
| 268 | + .catch((err) => showMessage(`Error: ${err?.message ?? err}`)); |
| 269 | + }; |
| 270 | + |
238 | 271 | document.getElementById('take_action')?.addEventListener('click', async (e) => { |
239 | | - if (document.getElementById('current_action')?.value === 'format_code') { |
| 272 | + if (document.getElementById('current_action')?.value === 'code_maintenance') { |
240 | 273 | e.preventDefault(); |
241 | | - if (document.querySelector('input[name="action.format_code"]:checked').value == 'tidyPGCode') { |
| 274 | + if (document.querySelector('input[name="action.code_maintenance"]:checked').value === 'tidyPGCode') { |
242 | 275 | tidyPGCode(); |
243 | 276 | } else if ( |
244 | | - document.querySelector('input[name="action.format_code"]:checked').value == 'convertCodeToPGML' |
| 277 | + document.querySelector('input[name="action.code_maintenance"]:checked').value === 'convertCodeToPGML' |
245 | 278 | ) { |
246 | 279 | convertCodeToPGML(); |
| 280 | + } else if ( |
| 281 | + document.querySelector('input[name="action.code_maintenance"]:checked').value === 'runPGCritic' |
| 282 | + ) { |
| 283 | + runPGCritic(); |
247 | 284 | } |
248 | 285 | return; |
249 | 286 | } |
|
306 | 343 | }); |
307 | 344 |
|
308 | 345 | const renderURL = `${webworkConfig?.webwork_url ?? '/webwork2'}/render_rpc`; |
309 | | - const renderArea = document.getElementById('pgedit-render-area'); |
310 | 346 | const fileType = document.getElementsByName('file_type')[0]?.value; |
311 | 347 |
|
312 | 348 | // This is either the div containing the CodeMirror editor or the problemContents textarea in the case that |
|
391 | 427 | } |
392 | 428 |
|
393 | 429 | adjustIFrameHeight(); |
394 | | - |
395 | | - // Scroll to the top of the render window if the current scroll position is below that. |
396 | | - const renderAreaRect = renderArea.getBoundingClientRect(); |
397 | | - const topBarHeight = document.querySelector('.webwork-logo')?.getBoundingClientRect().height ?? 0; |
398 | | - if (renderAreaRect.top < topBarHeight) window.scrollBy(0, renderAreaRect.top - topBarHeight); |
| 430 | + scrollToRenderArea(); |
399 | 431 | }); |
400 | 432 |
|
401 | 433 | const render = () => |
|
0 commit comments