Show new/deleted files as line-by-line diff
This commit is contained in:
parent
a808d2efbc
commit
2a37762aa5
|
|
@ -42,22 +42,43 @@
|
|||
const targetElement = document.getElementById("diff");
|
||||
const toggle = document.getElementById("unified-toggle");
|
||||
|
||||
function renderDiff(outputFormat) {
|
||||
const diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, {
|
||||
drawFileList: true,
|
||||
matching: "lines",
|
||||
outputFormat,
|
||||
highlight: true,
|
||||
colorScheme: "dark",
|
||||
});
|
||||
diff2htmlUi.draw();
|
||||
diff2htmlUi.highlightCode();
|
||||
// Split raw diff into per-file chunks and classify each
|
||||
function splitDiff(raw) {
|
||||
const files = [];
|
||||
const parts = raw.split(/^(?=diff --git )/m);
|
||||
for (const part of parts) {
|
||||
if (!part.trim()) continue;
|
||||
const isNew = /^new file mode/m.test(part);
|
||||
const isDeleted = /^deleted file mode/m.test(part);
|
||||
files.push({ raw: part, isNew, isDeleted });
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
renderDiff("side-by-side");
|
||||
const files = splitDiff(diffString);
|
||||
|
||||
function renderAll(modifiedFormat) {
|
||||
targetElement.innerHTML = "";
|
||||
for (const file of files) {
|
||||
const div = document.createElement("div");
|
||||
targetElement.appendChild(div);
|
||||
const format = (file.isNew || file.isDeleted) ? "line-by-line" : modifiedFormat;
|
||||
const ui = new Diff2HtmlUI(div, file.raw, {
|
||||
drawFileList: false,
|
||||
matching: "lines",
|
||||
outputFormat: format,
|
||||
highlight: true,
|
||||
colorScheme: "dark",
|
||||
});
|
||||
ui.draw();
|
||||
ui.highlightCode();
|
||||
}
|
||||
}
|
||||
|
||||
renderAll("side-by-side");
|
||||
|
||||
toggle.addEventListener("change", function () {
|
||||
renderDiff(this.checked ? "line-by-line" : "side-by-side");
|
||||
renderAll(this.checked ? "line-by-line" : "side-by-side");
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user