Skip to content

Commit 09ab6a5

Browse files
committed
Store and restore preferred diff tag
1 parent 1de61c5 commit 09ab6a5

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

webapp/public/js/domjudge.js

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ function setDiffMode(value)
138138
localStorage.setItem('domjudge_editor_diff_mode', value);
139139
}
140140

141+
function getDiffTag()
142+
{
143+
let diffTag = localStorage.getItem('domjudge_editor_diff_tag');
144+
if (diffTag === undefined) {
145+
return 'no-diff';
146+
}
147+
return diffTag;
148+
}
149+
150+
function setDiffTag(value)
151+
{
152+
localStorage.setItem('domjudge_editor_diff_tag', value);
153+
}
154+
141155
// Send a notification if notifications have been enabled.
142156
// The options argument is passed to the Notification constructor,
143157
// except that the following tags (if found) are interpreted and
@@ -1295,10 +1309,23 @@ const editors = [];
12951309
function initDiffEditor(editorId) {
12961310
const wrapper = $(`#${editorId}-wrapper`);
12971311

1298-
// TODO: store and restore tag preference in local storage.
1299-
const initialSelect = "";
1312+
const initialTag = getDiffTag();
13001313
const select = wrapper.find(".diff-select");
1301-
select[0].selectedIndex = 0;
1314+
for (let i = 0; i < select[0].options.length; i++) {
1315+
if (select[0].options[i].dataset.tag == initialTag) {
1316+
select[0].selectedIndex = i;
1317+
break;
1318+
}
1319+
}
1320+
// Fall back to other tagged diff if preferred tag is not available for this submission.
1321+
if (initialTag !== "no-diff" && select[0].selectedIndex === 0) {
1322+
for (let i = 1; i < select[0].options.length; i++) {
1323+
if (select[0].options[i].dataset.tag) {
1324+
select[0].selectedIndex = i;
1325+
break;
1326+
}
1327+
}
1328+
}
13021329

13031330
const initialDiffMode = getDiffMode();
13041331
const radios = wrapper.find(`.diff-mode > input[type='radio']`);
@@ -1368,9 +1395,15 @@ function initDiffEditor(editorId) {
13681395
radios.each((_, radio) => {
13691396
radio.disabled = noDiff;
13701397
});
1398+
1399+
const selected = select[0].options[select[0].selectedIndex];
1400+
if (selected && selected.dataset.tag) {
1401+
setDiffTag(selected.dataset.tag);
1402+
}
1403+
13711404
// TODO: add tab panes for deleted source files.
13721405
};
1373-
updateSelect("", true);
1406+
updateSelect(select[0].value, select[0].value === "");
13741407
editor.onDiffSelectChange(updateSelect);
13751408
}
13761409

@@ -1390,6 +1423,13 @@ function initDiffEditorTab(editorId, diffId, rank, models, modifiedModel) {
13901423
theme: getCurrentEditorTheme(),
13911424
});
13921425

1426+
const updateMode = (diffMode) => {
1427+
diffEditor.updateOptions({
1428+
renderSideBySide: diffMode === 'side-by-side',
1429+
});
1430+
};
1431+
editors[editorId].onDiffModeChange(updateMode);
1432+
13931433
const updateSelect = (submitId, noDiff) => {
13941434
if (!noDiff) {
13951435
const model = models[submitId];
@@ -1430,7 +1470,7 @@ function initDiffEditorTab(editorId, diffId, rank, models, modifiedModel) {
14301470
})
14311471
};
14321472
editors[editorId].onDiffSelectChange(updateSelect);
1433-
updateSelect("", true);
1473+
updateSelect(editors[editorId].getDiffSelection(), editors[editorId].getDiffSelection() === "");
14341474

14351475
const updateIcon = () => {
14361476
const noDiff = editors[editorId].getDiffSelection() === "";
@@ -1449,11 +1489,4 @@ function initDiffEditorTab(editorId, diffId, rank, models, modifiedModel) {
14491489
}
14501490
}
14511491
diffEditor.onDidUpdateDiff(updateIcon);
1452-
1453-
const updateMode = (diffMode) => {
1454-
diffEditor.updateOptions({
1455-
renderSideBySide: diffMode === 'side-by-side',
1456-
});
1457-
};
1458-
editors[editorId].onDiffModeChange(updateMode);
14591492
}

webapp/templates/jury/partials/submission_diff.html.twig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
<div class="btn-group">
2525
<a href="#" role="button" class="btn btn-secondary btn-sm pe-none" aria-disabled="true"><i class="fas fa-code-branch"></i></a>
2626
<select class="diff-select btn btn-secondary btn-sm form-select-sm text-start" aria-label="Submission to diff against">
27-
<option value="">No diff</option>
27+
<option value="" data-tag="no-diff">No diff</option>
2828
{%- for other in otherSubmissions %}
29-
<option value="{{ other.submitid }}">
30-
{%- if originalSubmission and originalSubmission.submitid == other.submitid %}
31-
s{{ other.submitid }} (original)
32-
{%- elseif oldSubmission and oldSubmission.submitid == other.submitid %}
33-
s{{ other.submitid }} (previous)
34-
{%- else %}
35-
s{{ other.submitid }}
36-
{%- endif %}
29+
{%- set tag = "" %}
30+
{%- if originalSubmission and originalSubmission.submitid == other.submitid %}
31+
{%- set tag = "original" %}
32+
{%- elseif oldSubmission and oldSubmission.submitid == other.submitid %}
33+
{%- set tag = "previous" %}
34+
{%- endif %}
35+
<option value="{{ other.submitid }}" {%- if tag %} data-tag="{{ tag }}" {%- endif %}>
36+
s{{ other.submitid }} {%- if tag %} ({{ tag }}) {%- endif %}
3737
</option>
3838
{%- endfor %}
3939
</select>

0 commit comments

Comments
 (0)