Skip to content

Commit

Permalink
split out the convertToPGML code and a few other fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
pstaabp committed Jan 12, 2024
1 parent ded41b3 commit 6fe840c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
42 changes: 36 additions & 6 deletions htdocs/js/PGProblemEditor/pgproblemeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? '';

Expand All @@ -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}`));
Expand All @@ -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;
}

Expand Down
4 changes: 1 addition & 3 deletions lib/WebworkWebservice/ProblemActions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
% last unless $c->{is_pg};
<div>
<div class="form-check">
<%= radio_button 'action.format_code' => 'tidyPGCode',
Expand Down
2 changes: 1 addition & 1 deletion templates/HelpFiles/InstructorPGProblemEditor.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@

<dt><%= maketext('Format Code') %></dt>
<dd>
<%= 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 '
Expand Down

0 comments on commit 6fe840c

Please sign in to comment.