diff --git a/htdocs/js/PGProblemEditor/pgproblemeditor.js b/htdocs/js/PGProblemEditor/pgproblemeditor.js index d36bb38143..855054c941 100644 --- a/htdocs/js/PGProblemEditor/pgproblemeditor.js +++ b/htdocs/js/PGProblemEditor/pgproblemeditor.js @@ -98,15 +98,15 @@ ?.addEventListener('change', () => deleteBackupCheck.checked = true); } - // Send a request to the server to either perltidy or convert_to_PGML the current PG code in the CodeMirror editor. - const formatPGCode = () => { + // Send a request to the server to perltidy the current PG code in the CodeMirror editor. + const tidyPGCode = () => { const request_object = { user: document.getElementById('hidden_user')?.value, courseID: document.getElementsByName('courseID')[0]?.value, key: document.getElementById('hidden_key')?.value }; - request_object.rpc_command = document.querySelector('input[name="action.format_code"]:checked').value; + request_object.rpc_command = 'tidyPGCode'; request_object.pgCode = webworkConfig?.pgCodeMirror?.getValue() ?? document.getElementById('problemContents')?.value ?? ''; @@ -132,8 +132,34 @@ if (webworkConfig?.pgCodeMirror) webworkConfig.pgCodeMirror.setValue(data.result_data.tidiedPGCode); else document.getElementById('problemContents').value = data.result_data.tidiedPGCode; saveTempFile(); - showMessage('Successfully ' - + (request_object.rpc_command == 'tidyPGCode' ? 'perltidied code.' : 'converted code to PGML'), true); + showMessage('Successfuly perltidied code.', true); + } + }) + .catch((err) => showMessage(`Error: ${err?.message ?? err}`)); + }; + + // Send a request to the server to convert_to_PGML the current PG code in the CodeMirror editor. + const convertCodeToPGML = () => { + const request_object = { + user: document.getElementById('hidden_user')?.value, + courseID: document.getElementsByName('courseID')[0]?.value, + key: document.getElementById('hidden_key')?.value + }; + + request_object.rpc_command = 'convertCodeToPGML'; + request_object.pgCode = webworkConfig?.pgCodeMirror?.getValue() + ?? document.getElementById('problemContents')?.value ?? ''; + + fetch(webserviceURL, { method: 'post', mode: 'same-origin', body: new URLSearchParams(request_object) }) + .then((response) => response.json()) + .then((data) => { + if (request_object.pgCode === data.result_data.pgmlCode) { + showMessage('There were no changes to the code.', true); + } else { + if (webworkConfig?.pgCodeMirror) webworkConfig.pgCodeMirror.setValue(data.result_data.pgmlCode); + else document.getElementById('problemContents').value = data.result_data.pgmlCode; + saveTempFile(); + showMessage('Successfully converted code to PGML', true); } }) .catch((err) => showMessage(`Error: ${err?.message ?? err}`)); @@ -142,7 +168,11 @@ document.getElementById('take_action')?.addEventListener('click', async (e) => { if (document.getElementById('current_action')?.value === 'format_code') { e.preventDefault(); - formatPGCode(); + if (document.querySelector('input[name="action.format_code"]:checked').value == "tidyPGCode") { + tidyPGCode(); + } else if (document.querySelector('input[name="action.format_code"]:checked').value == "convertCodeToPGML") { + convertCodeToPGML(); + } return; } diff --git a/lib/WebworkWebservice/ProblemActions.pm b/lib/WebworkWebservice/ProblemActions.pm index 653bbc5d86..8241ee58b5 100644 --- a/lib/WebworkWebservice/ProblemActions.pm +++ b/lib/WebworkWebservice/ProblemActions.pm @@ -151,10 +151,8 @@ sub convertCodeToPGML { my ($invocant, $self, $params) = @_; my $code = $params->{pgCode}; - my $converted_code = convertToPGML($code); - return { - ra_out => { tidiedPGCode => $converted_code }, + ra_out => { pgmlCode => convertToPGML($code) }, text => 'Converted to PGML' }; diff --git a/templates/ContentGenerator/Instructor/PGProblemEditor/format_code_form.html.ep b/templates/ContentGenerator/Instructor/PGProblemEditor/format_code_form.html.ep index cb65dfda10..a66fb176db 100644 --- a/templates/ContentGenerator/Instructor/PGProblemEditor/format_code_form.html.ep +++ b/templates/ContentGenerator/Instructor/PGProblemEditor/format_code_form.html.ep @@ -1,3 +1,4 @@ +% last unless $c->{is_pg};
<%= radio_button 'action.format_code' => 'tidyPGCode', diff --git a/templates/HelpFiles/InstructorPGProblemEditor.html.ep b/templates/HelpFiles/InstructorPGProblemEditor.html.ep index 050cc8cdaf..45692105c8 100644 --- a/templates/HelpFiles/InstructorPGProblemEditor.html.ep +++ b/templates/HelpFiles/InstructorPGProblemEditor.html.ep @@ -131,7 +131,7 @@
<%= maketext('Format Code') %>
- <%= maketext('Reformat the code using perltidy or a conversion to PGML>. Using perltidy will change the code ' + <%= maketext('Reformat the code using perltidy or a conversion to PGML. Using perltidy will change the code ' . 'in the editor window, and save changes to the temporary file. In some cases (if the code contains ' . 'backslashes or double tildes) this can result in odd spacing in the code. The convert to PGML ' . 'feature changes the code in text blocks in the code to use PGML features. Generally the conversion of '