Skip to content

Commit 77cb61a

Browse files
committed
Handle renaming of single-file submissions
1 parent 492d880 commit 77cb61a

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

webapp/public/js/domjudge.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,33 @@ function initDiffEditor(editorId) {
13621362
const element = wrapper.find(".nav-link[data-rank]")[rank].querySelector('.fa-fw');
13631363
element.className = 'fas fa-fw fa-' + icon;
13641364
},
1365+
'renamedFrom': (rank, oldName) => {
1366+
const navItem = wrapper.find(".nav-link[data-rank]")[rank];
1367+
let renamedFromName = navItem.querySelector('.renamed');
1368+
let arrow = navItem.querySelector('.fa-arrow-right');
1369+
if (oldName === undefined) {
1370+
if (renamedFromName) {
1371+
navItem.removeChild(renamedFromName);
1372+
}
1373+
if (arrow) {
1374+
navItem.removeChild(arrow);
1375+
}
1376+
return;
1377+
}
1378+
1379+
if (!renamedFromName) {
1380+
renamedFromName = document.createElement('span');
1381+
renamedFromName.className = 'renamed';
1382+
navItem.insertBefore(renamedFromName, navItem.childNodes[1]);
1383+
}
1384+
renamedFromName.innerText = ` ${oldName} `;
1385+
1386+
if (!arrow) {
1387+
arrow = document.createElement('i');
1388+
arrow.className = 'fas fa-arrow-right';
1389+
navItem.insertBefore(arrow, navItem.childNodes[2]);
1390+
}
1391+
},
13651392
'onDiffModeChange': (f) => {
13661393
radios.change((e) => {
13671394
const diffMode = e.target.value;
@@ -1424,16 +1451,20 @@ function initDiffEditorTab(editorId, diffId, rank, models, modifiedModel) {
14241451
editors[editorId].onDiffModeChange(updateMode);
14251452

14261453
const updateSelect = (submitId, noDiff) => {
1454+
const model = models[submitId] ??= {'model': empty};
14271455
if (!noDiff) {
1428-
const model = models[submitId];
1429-
if (model === undefined) {
1430-
models[submitId] = {'model': empty};
1431-
} else if (model !== undefined && !model['model']) {
1456+
if (!model['model']) {
14321457
// TODO: show source code instead of diff to empty file?
14331458
model['model'] = monaco.editor.createModel(model['source'], undefined, monaco.Uri.file("test/" + submitId + "/" + model['filename']));
14341459
}
14351460
}
14361461

1462+
if (noDiff || !model['renamedFrom']) {
1463+
editors[editorId].renamedFrom(rank, undefined);
1464+
} else {
1465+
editors[editorId].renamedFrom(rank, model['renamedFrom']);
1466+
}
1467+
14371468
diffEditor.updateOptions({
14381469
renderOverviewRuler: !noDiff,
14391470
});
@@ -1445,7 +1476,6 @@ function initDiffEditorTab(editorId, diffId, rank, models, modifiedModel) {
14451476
// Reset the diff mode to the currently selected mode.
14461477
updateMode(editors[editorId].getDiffMode())
14471478
}
1448-
// TODO: handle single-file submission case with renamed file.
14491479
const oldViewState = diffEditor.saveViewState();
14501480
diffEditor.setModel({
14511481
original: noDiff ? modifiedModel : models[submitId]['model'],

webapp/src/Controller/Jury/SubmissionController.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,27 @@ public function sourceAction(
906906
foreach ($oldFiles as $f) {
907907
$submitId = $f->getSubmission()->getSubmitid();
908908
$otherFiles[$submitId] ??= [];
909-
$otherFiles[$submitId][$f->getFilename()] = $f;
909+
$otherFiles[$submitId][$f->getFilename()] = [
910+
'filename' => $f->getFilename(),
911+
'source' => mb_check_encoding($f->getSourcecode(), 'UTF-8') ? $f->getSourcecode() : "Could not display file as UTF-8, is it binary?",
912+
];
913+
}
914+
915+
// Handle file renaming for a single-file submission.
916+
if (count($files) === 1) {
917+
$f = $files[0];
918+
foreach ($otherSubmissions as $s) {
919+
$sf = $otherFiles[$s->getSubmitid()];
920+
if (count($sf) === 1 && !array_key_exists($f->getFilename(), $sf)) {
921+
$oldName = array_key_first($sf);
922+
$otherFiles[$s->getSubmitid()] = [
923+
$f->getFilename() => [
924+
'renamedFrom' => $oldName,
925+
...$sf[$oldName]
926+
],
927+
];
928+
}
929+
}
910930
}
911931

912932
return $this->render('jury/submission_source.html.twig', [

webapp/src/Twig/TwigExtension.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -978,15 +978,9 @@ public function showDiff(string $editorId, string $diffId, SubmissionFile $newFi
978978

979979
$others = [];
980980
foreach ($otherFiles as $submissionId => $files) {
981-
foreach ($files as $f) {
982-
// TODO: this renames the old file to multiple files, need more data in `showDiff`.
983-
if (($newFile->getRank() === 1 && count($files) === 1) || ($f->getFilename() == $newFile->getFilename())) {
984-
// TODO: add `tag` containing `previous` / `original`
985-
$others[$submissionId] = [
986-
'filename' => $f->getFilename(),
987-
'source' => mb_check_encoding($f->getSourcecode(), 'UTF-8') ? $f->getSourcecode() : "Could not display file as UTF-8, is it binary?",
988-
];
989-
}
981+
if (isset($files[$newFile->getFilename()])) {
982+
// TODO: add `tag` containing `previous` / `original`
983+
$others[$submissionId] = $files[$newFile->getFilename()];
990984
}
991985
}
992986

0 commit comments

Comments
 (0)