Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions webapp/public/js/domjudge.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,18 @@ function applyEditorTheme(theme = undefined, isExternal = false)
});
}

function isDiffSideBySide()
function getDiffMode()
{
let sideBySide = localStorage.getItem('domjudge_editor_side_by_side');
if (sideBySide === undefined) {
return true;
let diffMode = localStorage.getItem('domjudge_editor_diff_mode');
if (diffMode === undefined) {
return 'side-by-side';
}
return sideBySide == 'true';
return diffMode;
}

function setDiffSideBySide(value)
function setDiffMode(value)
{
localStorage.setItem('domjudge_editor_side_by_side', value);
localStorage.setItem('domjudge_editor_diff_mode', value);
}

// Send a notification if notifications have been enabled.
Expand Down
53 changes: 37 additions & 16 deletions webapp/src/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public function getGlobals(): array
'hc-light' => ['name' => 'High contrast (light)'],
'hc-black' => ['name' => 'High contrast (dark)'],
],
'diff_modes' => [
'no-diff' => ["name" => "No diff"],
'side-by-side' => ["name" => "Side-by-side"],
'inline' => ["name" => "Inline"],
],
];
}

Expand Down Expand Up @@ -940,10 +945,6 @@ protected function parseSourceDiff(string $difftext): string
public function showDiff(string $id, SubmissionFile $newFile, SubmissionFile $oldFile): string
{
$editor = <<<HTML
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="__EDITOR__SBS">
<label class="form-check-label" for="__EDITOR__SBS">Use side-by-side diff viewer</label>
</div>
<div class="editor" id="__EDITOR__"></div>
<script>
$(function() {
Expand All @@ -959,14 +960,10 @@ public function showDiff(string $id, SubmissionFile $newFile, SubmissionFile $ol
monaco.Uri.parse("diff-new/%s")
);

const sideBySide = isDiffSideBySide()
sideBySideSwitch = $("#__EDITOR__SBS");
sideBySideSwitch.prop('checked', sideBySide);
sideBySideSwitch.change(function(e) {
setDiffSideBySide(e.target.checked);
diffEditor.updateOptions({
renderSideBySide: e.target.checked,
});
const initialDiffMode = getDiffMode();
const radios = $("#diffselect-__EDITOR__ > input[name='__EDITOR__-mode']");
radios.each((_, radio) => {
$(radio).prop('checked', radio.value === initialDiffMode);
});

const diffEditor = monaco.editor.createDiffEditor(
Expand All @@ -978,13 +975,37 @@ public function showDiff(string $id, SubmissionFile $newFile, SubmissionFile $ol
scrollBeyondLastLine: false,
automaticLayout: true,
readOnly: true,
renderSideBySide: sideBySide,
theme: getCurrentEditorTheme(),
});
diffEditor.setModel({
original: originalModel,
modified: modifiedModel,

const updateMode = (diffMode) => {
setDiffMode(diffMode);
const noDiff = diffMode === 'no-diff';
diffEditor.updateOptions({
renderOverviewRuler: !noDiff,
renderSideBySide: diffMode === 'side-by-side',
});

const oldViewState = diffEditor.saveViewState();
diffEditor.setModel({
original: noDiff ? modifiedModel : originalModel,
modified: modifiedModel,
});
diffEditor.restoreViewState(oldViewState);

diffEditor.getOriginalEditor().updateOptions({
lineNumbers: !noDiff,
});
diffEditor.getModifiedEditor().updateOptions({
minimap: {
enabled: noDiff,
},
})
};
radios.change((e) => {
updateMode(e.target.value);
});
updateMode(initialDiffMode);
});
});
</script>
Expand Down
10 changes: 9 additions & 1 deletion webapp/templates/jury/partials/submission_diff.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
{%- for filePair in oldFileStats.changedfiles %}
<div class="tab-pane fade {% if loop.first %}show active{% endif %}" id="diff-{{ filePair.1.submitfileid }}"
role="tabpanel">
{% set diff_id = "diff" ~ filePair.1.submitfileid %}
<div class="mb-1">
<a class="btn btn-secondary btn-sm"
href="{{ path('jury_submission_source', {submission: submission.submitid, fetch: filePair.1.rank}) }}">
Expand All @@ -54,8 +55,15 @@
<i class="fas fa-pencil-alt"></i> Edit
</a>
{% endif %}
<div id="diffselect-{{ diff_id }}" class="btn-group">
<a href="#" role="button" class="btn btn-secondary btn-sm pe-none" aria-disabled="true"><i class="fas fa-code-branch"></i></a>
{%- for mode_id, mode in diff_modes %}
<input type="radio" class="btn-check" name="{{ diff_id }}-mode" id="{{ diff_id }}-{{ mode_id }}" value="{{ mode_id }}" autocomplete="off">
<label class="btn btn-outline-secondary btn-sm" for="{{ diff_id }}-{{ mode_id }}">{{ mode.name }}</label>
{%- endfor %}
</div>
</div>
{{ showDiff("diff" ~ filePair.1.submitfileid, filePair.0, filePair.1) }}
{{ showDiff(diff_id, filePair.0, filePair.1) }}
</div>
{%- endfor %}
{%- for file in oldFileStats.unchangedfiles %}
Expand Down
Loading