From 6045df2bc614a6bbced9dd8e5352c0270ddcf1f6 Mon Sep 17 00:00:00 2001 From: Bruno Postle Date: Sat, 25 Apr 2026 21:07:07 +0100 Subject: [PATCH] viewer: inject render_diff thumbnail on commit page for .ifc changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- forgejo/templates/custom/footer.tmpl | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/forgejo/templates/custom/footer.tmpl b/forgejo/templates/custom/footer.tmpl index a5edf8c..ea7cbbf 100644 --- a/forgejo/templates/custom/footer.tmpl +++ b/forgejo/templates/custom/footer.tmpl @@ -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}).