Skip to content

Commit b979ba5

Browse files
committed
Handle renaming of single-file submissions
1 parent a388126 commit b979ba5

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
@@ -1369,6 +1369,33 @@ function initDiffEditor(editorId) {
13691369
const element = wrapper.find(".nav-link[data-rank]")[rank].querySelector('.fa-fw');
13701370
element.className = 'fas fa-fw fa-' + icon;
13711371
},
1372+
'renamedFrom': (rank, oldName) => {
1373+
const navItem = wrapper.find(".nav-link[data-rank]")[rank];
1374+
let renamed = navItem.querySelector('.renamed');
1375+
let arrow = navItem.querySelector('.fa-arrow-right');
1376+
if (oldName === undefined) {
1377+
if (renamed) {
1378+
navItem.removeChild(renamed);
1379+
}
1380+
if (arrow) {
1381+
navItem.removeChild(arrow);
1382+
}
1383+
return;
1384+
}
1385+
1386+
if (!renamed) {
1387+
renamed = document.createElement('span');
1388+
renamed.className = 'renamed';
1389+
navItem.insertBefore(renamed, navItem.childNodes[1]);
1390+
}
1391+
renamed.innerText = ` ${oldName} `;
1392+
1393+
if (!arrow) {
1394+
arrow = document.createElement('i');
1395+
arrow.className = 'fas fa-arrow-right';
1396+
navItem.insertBefore(arrow, navItem.childNodes[2]);
1397+
}
1398+
},
13721399
'onDiffModeChange': (f) => {
13731400
radios.change((e) => {
13741401
const diffMode = e.target.value;
@@ -1431,16 +1458,20 @@ function initDiffEditorTab(editorId, diffId, rank, models, modifiedModel) {
14311458
editors[editorId].onDiffModeChange(updateMode);
14321459

14331460
const updateSelect = (submitId, noDiff) => {
1461+
const model = models[submitId] ??= {'model': empty};
14341462
if (!noDiff) {
1435-
const model = models[submitId];
1436-
if (model === undefined) {
1437-
models[submitId] = {'model': empty};
1438-
} else if (model !== undefined && !model['model']) {
1463+
if (!model['model']) {
14391464
// TODO: show source code instead of diff to empty file?
14401465
model['model'] = monaco.editor.createModel(model['source'], undefined, monaco.Uri.file("test/" + submitId + "/" + model['filename']));
14411466
}
14421467
}
14431468

1469+
if (noDiff || !model['renamedFrom']) {
1470+
editors[editorId].renamedFrom(rank, undefined);
1471+
} else {
1472+
editors[editorId].renamedFrom(rank, model['renamedFrom']);
1473+
}
1474+
14441475
diffEditor.updateOptions({
14451476
renderOverviewRuler: !noDiff,
14461477
});
@@ -1452,7 +1483,6 @@ function initDiffEditorTab(editorId, diffId, rank, models, modifiedModel) {
14521483
// Reset the diff mode to the currently selected mode.
14531484
updateMode(editors[editorId].getDiffMode())
14541485
}
1455-
// TODO: handle single-file submission case with renamed file.
14561486
const oldViewState = diffEditor.saveViewState();
14571487
diffEditor.setModel({
14581488
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 binary file",
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 && !isset($sf[$f->getFilename()])) {
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 binary file",
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)