Skip to content

Commit c7089fc

Browse files
committed
Add a toggle to enable side-by-side diff viewer
Store the last toggled diff viewer state in the local storage. The way diffs are viewed is mostly a user preference, but depending on the file and type of changes, the option to quickly toggle it can come in handy. Note that for multi-file submissions, a per-file toggle is included in the tab, but on reload all tabs will have the state of the last toggled switch.
1 parent 7d7ed2e commit c7089fc

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

webapp/public/js/domjudge.js

+15
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ function applyEditorTheme(theme = undefined, isExternal = false)
124124
});
125125
}
126126

127+
function isDiffSideBySide()
128+
{
129+
let sideBySide = localStorage.getItem('domjudge_editor_side_by_side');
130+
if (sideBySide === undefined) {
131+
return true;
132+
} else {
133+
return sideBySide == 'true';
134+
}
135+
}
136+
137+
function setDiffSideBySide(value)
138+
{
139+
localStorage.setItem('domjudge_editor_side_by_side', value);
140+
}
141+
127142
// Send a notification if notifications have been enabled.
128143
// The options argument is passed to the Notification constructor,
129144
// except that the following tags (if found) are interpreted and

webapp/src/Twig/TwigExtension.php

+16
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,10 @@ protected function parseSourceDiff(string $difftext): string
929929
public function showDiff(string $id, SubmissionFile $newFile, SubmissionFile $oldFile): string
930930
{
931931
$editor = <<<HTML
932+
<div class="form-check form-switch">
933+
<input class="form-check-input" type="checkbox" id="__EDITOR__SBS">
934+
<label class="form-check-label" for="__EDITOR__SBS">Use side-by-side diff viewer</label>
935+
</div>
932936
<div class="editor" id="__EDITOR__"></div>
933937
<script>
934938
$(function() {
@@ -943,6 +947,17 @@ public function showDiff(string $id, SubmissionFile $newFile, SubmissionFile $ol
943947
undefined,
944948
monaco.Uri.parse("diff-new/%s")
945949
);
950+
951+
const sideBySide = isDiffSideBySide()
952+
sideBySideSwitch = $("#__EDITOR__SBS");
953+
sideBySideSwitch.prop('checked', sideBySide);
954+
sideBySideSwitch.change(function(e) {
955+
setDiffSideBySide(e.target.checked);
956+
diffEditor.updateOptions({
957+
renderSideBySide: e.target.checked,
958+
});
959+
});
960+
946961
const diffEditor = monaco.editor.createDiffEditor(
947962
document.getElementById("__EDITOR__"), {
948963
scrollbar: {
@@ -952,6 +967,7 @@ public function showDiff(string $id, SubmissionFile $newFile, SubmissionFile $ol
952967
scrollBeyondLastLine: false,
953968
automaticLayout: true,
954969
readOnly: true,
970+
renderSideBySide: sideBySide,
955971
theme: getCurrentEditorTheme(),
956972
});
957973
diffEditor.setModel({

0 commit comments

Comments
 (0)