viewer: inject render_diff thumbnail on commit page for .ifc changes

On /owner/repo/commit/{sha} pages, Case 4 injects an image below the
diff-file-header of each IFC file: render_diff for modifications,
plain preview for adds and deletes.  Parent SHA is read from the
existing "parent" commit link in the DOM — no extra API calls.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Bruno Postle 2026-04-25 21:07:07 +01:00
parent 2fdb442fcf
commit 6045df2bc6

View file

@ -95,6 +95,60 @@
return;
}
// -----------------------------------------------------------------------
// Case 4: individual commit page — inject a render_diff image (or plain
// preview for added/deleted files) below the header of each .ifc file
// in the diff.
// -----------------------------------------------------------------------
var commitMatch = window.location.pathname.match(/^\/([^/]+\/[^/]+)\/commit\/([0-9a-f]{40})$/i);
if (commitMatch) {
var repoPath = commitMatch[1];
var headSha = commitMatch[2];
var origin = window.location.origin;
// Extract parent SHA from the "parent" commit link at the top of the page.
var parentSha = null;
var parentLink = document.querySelector("a.primary.sha.label[href*='/commit/']");
if (parentLink) {
var pm = parentLink.getAttribute("href").match(/\/commit\/([0-9a-f]{40})$/i);
if (pm) parentSha = pm[1];
}
document.querySelectorAll(".diff-file-box").forEach(function(box) {
var newFile = box.getAttribute("data-new-filename") || "";
var oldFile = box.getAttribute("data-old-filename") || "";
var newIsIfc = newFile.toLowerCase().endsWith(".ifc");
var oldIsIfc = oldFile.toLowerCase().endsWith(".ifc");
var ifcPath, imgSrc;
if (newIsIfc && oldIsIfc && newFile === oldFile && parentSha) {
// Modified: show colour-coded diff
ifcPath = newFile;
var baseIfc = "ifc://" + host + "/" + repoPath + "@" + parentSha + "?path=" + encodeURIComponent(ifcPath);
var headIfc = "ifc://" + host + "/" + repoPath + "@" + headSha + "?path=" + encodeURIComponent(ifcPath);
imgSrc = origin + "/render_diff?base=" + encodeURIComponent(baseIfc) + "&head=" + encodeURIComponent(headIfc);
} else if (newIsIfc) {
// Added (or renamed to .ifc): plain preview of new version
ifcPath = newFile;
imgSrc = origin + "/preview?url=" + encodeURIComponent("ifc://" + host + "/" + repoPath + "@" + headSha + "?path=" + encodeURIComponent(ifcPath));
} else if (oldIsIfc && parentSha) {
// Deleted (or renamed from .ifc): plain preview of old version
ifcPath = oldFile;
imgSrc = origin + "/preview?url=" + encodeURIComponent("ifc://" + host + "/" + repoPath + "@" + parentSha + "?path=" + encodeURIComponent(ifcPath));
} else {
return;
}
var img = document.createElement("img");
img.src = imgSrc;
img.style.cssText = "max-width:100%;display:block;margin:8px 0";
img.alt = ifcPath;
var header = box.querySelector(".diff-file-header");
if (header) header.insertAdjacentElement("afterend", img);
});
return;
}
// -----------------------------------------------------------------------
// Case 2: file history page — inject a "3D" link next to each commit's
// browse-file link (/src/commit/{hash}/{treepath}).