From 5ed586b67420c38fdca5b4c81375f001c94ee6fb Mon Sep 17 00:00:00 2001 From: Vincenzo Favara Date: Tue, 3 Nov 2026 16:28:05 +0100 Subject: [PATCH] Open a commit's details under its row in the Git Graph Clicking a commit in History put its details after the last loaded row, and every paint of the table started it again from the top. So the table jumped when a commit was clicked, and the details showed up at the end of the list, far from the commit and out of view in a long history. The details now open right under the clicked row (a comparison under the row clicked first), and the graph's lanes run on beside them. Opening, loading, switching Tree/List, folding a folder and closing keep the table where it was. Once the details have loaded, the table scrolls to center the row and its details, and only the table scrolls, not the tab around it. Beside the details a lane still bends within one row, then runs straight. --- public/git-graph-render.js | 11 ++- public/git-graph.css | 7 ++ public/project-git-graph-view.js | 86 +++++++++++++------- test/project-git-graph-view.test.js | 117 ++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+), 32 deletions(-) diff --git a/public/git-graph-render.js b/public/git-graph-render.js index e755c5269..3c1731952 100644 --- a/public/git-graph-render.js +++ b/public/git-graph-render.js @@ -168,6 +168,8 @@ function gitGraphRenderGraphSvg(rows, laneAssignments, opts = {}) { const rowY = Array.isArray(opts.rowY) && opts.rowY.length === rows.length ? opts.rowY : null; const yOf = i => (rowY ? rowY[i] : rowHeight / 2 + i * rowHeight); const height = rowY && opts.height ? opts.height : rows.length * rowHeight; + // A table row's own height: measured rows are taller than rowHeight. + const bendHeight = opts.bendHeight || rowHeight; const paths = []; const nodes = []; @@ -182,15 +184,18 @@ function gitGraphRenderGraphSvg(rows, laneAssignments, opts = {}) { // An edge bends into its own lane on the first row, runs straight down, // and only bends again on the last row if its parent sits in another lane. // A parent that isn't loaded yet keeps the lane running off the bottom. + // Across an inline Commit Details panel a bend still takes one row's + // height, as everywhere else, and the line runs straight beside the panel. const via = laneWidth / 2 + (edge.viaLane != null ? edge.viaLane : edge.toLane) * laneWidth; const targetRow = byHash.get(edge.parentHash); const loaded = targetRow !== undefined; const tx = loaded ? laneWidth / 2 + edge.toLane * laneWidth : via; const ty = loaded ? yOf(targetRow) : height + rowHeight / 2; const endRow = loaded ? targetRow : rows.length; - const route = endRow - i > 1 - ? { via, top: yOf(i + 1), bottom: loaded ? yOf(targetRow - 1) : height } - : null; + const span = endRow - i; + const top = Math.min(span > 1 ? yOf(i + 1) : ty, cy + bendHeight); + const bottom = loaded ? Math.max(top, span > 1 ? yOf(targetRow - 1) : top, ty - bendHeight) : height; + const route = span > 1 || ty - cy > bendHeight * 1.5 ? { via, top, bottom } : null; paths.push(gitGraphEdgePath(cx, cy, tx, ty, color, style, route)); } diff --git a/public/git-graph.css b/public/git-graph.css index 8b6449706..acbf66d0f 100644 --- a/public/git-graph.css +++ b/public/git-graph.css @@ -321,6 +321,13 @@ padding: 14px 16px 16px; } +/* The panel sits under its row and leaves the graph column free, so the lanes + run on beside it to the rows below. */ +.gg-details-inline { + margin-left: var(--gg-graph-width, 60px); + border-bottom: 1px solid rgba(255,255,255,0.07); +} + .gg-details-close { position: absolute; top: 8px; diff --git a/public/project-git-graph-view.js b/public/project-git-graph-view.js index 022648d09..d86376ef3 100644 --- a/public/project-git-graph-view.js +++ b/public/project-git-graph-view.js @@ -411,6 +411,11 @@ function gitGraphPaint(project, state, body, opts = {}) { gitGraphWireFindWidget(project, state, body); gitGraphWireHeader(project, state, body); gitGraphWireRows(project, state, body); + // Before the details: bringing a just-opened panel into view starts from here. + if (prevScrollTop != null) { + const newWrap = body.querySelector('.gg-table-wrap'); + if (newWrap) newWrap.scrollTop = prevScrollTop; + } if (state.detailsData) { // The inline details markup already came back inside #gg-tbody's own // innerHTML (gitGraphRowsHtml/gitGraphInlineDetailsHtml), so only the @@ -418,11 +423,6 @@ function gitGraphPaint(project, state, body, opts = {}) { const inline = body.querySelector('#gg-details-inline'); if (inline) { gitGraphWireDetailsPanel(project, state, body, inline); gitGraphMaybeAutoCenterDetails(state, inline); } } - - if (prevScrollTop != null) { - const newWrap = body.querySelector('.gg-table-wrap'); - if (newWrap) newWrap.scrollTop = prevScrollTop; - } } function gitGraphRepoPickerHtml(repos, active) { @@ -642,11 +642,22 @@ function gitGraphWireHeader(project, state, body) { // --- Rows --- +/** The inline Commit Details open right under the row they belong to: the + * commit's own row, or for a comparison the row clicked first. Only when that + * row is no longer loaded do they fall to the end of the list. */ function gitGraphRowsHtml(state) { if (!state.rows.length) return '
No commits yet.
'; const showCols = { date: state.columns.date, author: state.columns.author, commit: state.columns.commit }; - return state.rows.map((row, index) => gitGraphRowHtml(row, state, showCols, index)).join('') + - (state.detailsData ? gitGraphInlineDetailsHtml(state) : ''); + const data = state.detailsData; + const anchor = data ? (data.mode === 'compare' ? state.selectedHash : data.hash) : null; + let placed = false; + const html = state.rows.map((row, index) => { + const rowHtml = gitGraphRowHtml(row, state, showCols, index); + if (!data || placed || row.hash !== anchor) return rowHtml; + placed = true; + return rowHtml + gitGraphInlineDetailsHtml(state); + }).join(''); + return html + (data && !placed ? gitGraphInlineDetailsHtml(state) : ''); } function gitGraphRowHtml(row, state, showCols, index) { @@ -682,13 +693,15 @@ function gitGraphWireRows(project, state, body) { // (every row repeating every other row's paths/circles) was O(rows²) // DOM nodes; this is O(rows). const rowY = []; + let rowHeight = 0; tbody.querySelectorAll('.gg-row[data-row-index]').forEach((el) => { rowY[Number(el.dataset.rowIndex)] = el.offsetTop + el.offsetHeight / 2; + rowHeight = rowHeight || el.offsetHeight; }); const measured = rowY.length === state.rows.length && rowY.some(y => y > 0); const svg = gitGraphRenderGraphSvg(state.rows, state.layout, { style: state.graphStyle, uncommittedChangesStyle: state.uncommittedChangesStyle, - rowY: measured ? rowY : null, height: measured ? tbody.scrollHeight : null, + rowY: measured ? rowY : null, height: measured ? tbody.scrollHeight : null, bendHeight: measured ? rowHeight : null, }); const clip = document.createElement('div'); clip.className = 'gg-graph-clip'; // decorative only — clicks/right-clicks fall through to the row underneath @@ -796,7 +809,7 @@ function gitGraphOpenDetails(project, state, body, row) { state.detailsLoading = true; state.detailsData = { mode: 'single', hash: row.hash, kind: row.kind }; state.detailsJustOpened = true; - gitGraphPaint(project, state, body); + gitGraphPaintDetails(project, state, body); const fetch = row.kind === 'uncommitted' ? gitGraphApi('getGitGraphCompareDetail', project.id, repo.path, state.headHash, null) : gitGraphApi('getGitGraphCommitDetail', project.id, repo.path, row.hash); @@ -808,7 +821,7 @@ function gitGraphOpenDetails(project, state, body, row) { } else { state.detailsData = { mode: 'single', hash: row.hash, kind: row.kind, error: (result && result.error) || 'Could not load commit details.' }; } - gitGraphPaint(project, state, body); + gitGraphPaintDetails(project, state, body); }); } @@ -818,7 +831,7 @@ function gitGraphOpenComparison(project, state, body, fromHash, toHash) { state.detailsLoading = true; state.detailsData = { mode: 'compare', fromHash, toHash }; state.detailsJustOpened = true; - gitGraphPaint(project, state, body); + gitGraphPaintDetails(project, state, body); const toArg = toHash === '#uncommitted' ? null : toHash; gitGraphApi('getGitGraphCompareDetail', project.id, repo.path, fromHash === '#uncommitted' ? toHash : fromHash, fromHash === '#uncommitted' ? null : toArg) .then((result) => { @@ -826,33 +839,48 @@ function gitGraphOpenComparison(project, state, body, fromHash, toHash) { state.detailsLoading = false; if (result && result.ok) state.detailsData = { mode: 'compare', fromHash, toHash, files: result.files || [] }; else state.detailsData = { mode: 'compare', fromHash, toHash, error: (result && result.error) || 'Could not load comparison.' }; - gitGraphPaint(project, state, body); + gitGraphPaintDetails(project, state, body); }); } +/** The panel sits among the rows, so painting it repaints the table: that + * keeps the graph's lines measured around it, and the scroll position where it + * was (a fresh table starts at the top). */ +function gitGraphPaintDetails(project, state, body) { + gitGraphPaint(project, state, body, { preserveScroll: true }); +} + function gitGraphInlineDetailsHtml(state) { return `
${gitGraphDetailsBodyHtml(state)}
`; } function gitGraphRenderDetailsPanel(project, state, body) { - const container = body.querySelector('#gg-details-inline'); - if (!container) return; - container.innerHTML = gitGraphDetailsBodyHtml(state); - gitGraphWireDetailsPanel(project, state, body, container); -} - -/** Scrolls the panel into view exactly once per open — the moment it opens, - * not on every subsequent repaint of an already-open panel. - * `state.detailsJustOpened` is set by gitGraphOpenDetails/OpenComparison and - * cleared the first time this runs *with the real content loaded*. The - * request's first paint (still `detailsLoading`) shows only the tiny - * "Loading…" placeholder, so centering on it would land short of where the - * eventual, much taller panel actually ends up — wait for the repaint that - * carries the real commit/diff content before centering and clearing it. */ + // A new height (Tree/List, a folder folded) moves every row below the panel. + gitGraphPaintDetails(project, state, body); +} + +/** Brings the panel into view exactly once per open, not on every later + * repaint of an already-open panel: `state.detailsJustOpened` is set by + * gitGraphOpenDetails/OpenComparison and cleared the first time this runs + * *with the real content loaded*. The request's first paint (still + * `detailsLoading`) shows only the tiny "Loading…" placeholder, so centering + * on it would land short of where the eventual, much taller panel ends up. + * The clicked row and its panel are centered in the table, or the row goes to + * the top if they are taller than the table. Only the table scrolls, never + * the tab around it. */ function gitGraphMaybeAutoCenterDetails(state, container) { if (!state.detailsJustOpened || state.detailsLoading) return; state.detailsJustOpened = false; - if (container && typeof container.scrollIntoView === 'function') container.scrollIntoView({ block: 'center' }); + const wrap = container && typeof container.closest === 'function' ? container.closest('.gg-table-wrap') : null; + if (!wrap) return; + const head = wrap.querySelector('.gg-thead'); + const headHeight = head ? head.offsetHeight : 0; + const origin = wrap.getBoundingClientRect().top - wrap.scrollTop; + const row = container.previousElementSibling; + const top = (row && row.classList && row.classList.contains('gg-row') ? row : container).getBoundingClientRect().top - origin; + const bottom = container.getBoundingClientRect().bottom - origin; + const room = wrap.clientHeight - headHeight - (bottom - top); + wrap.scrollTop = Math.max(0, top - headHeight - Math.max(0, room / 2)); } function gitGraphDetailsBodyHtml(state) { @@ -981,7 +1009,7 @@ function gitGraphCloseDetails(project, state, body) { state.detailsData = null; state.selectedHash = null; state.compareHash = null; - gitGraphPaint(project, state, body); + gitGraphPaintDetails(project, state, body); } function gitGraphOpenFileDiff(project, state, body, repo, filePath, opts = {}) { @@ -1236,7 +1264,7 @@ if (typeof module !== 'undefined' && module.exports) { gitGraphLoadGraph, gitGraphLoadRepositories, gitGraphPaint, gitGraphApi, renderProjectGitGraphTab, gitGraphSingleCommitHeaderHtml, gitGraphLinkifyBody, gitGraphFileRowHtml, gitGraphFileListHtml, gitGraphOpenDetails, gitGraphOpenComparison, gitGraphCloseDetails, - gitGraphMaybeAutoCenterDetails, gitGraphIsAncestor, gitGraphHandleRowClick, gitGraphBindKeyboard, + gitGraphMaybeAutoCenterDetails, gitGraphRowsHtml, gitGraphIsAncestor, gitGraphHandleRowClick, gitGraphBindKeyboard, gitGraphSameBranchNeighbor, gitGraphAlternateBranchNeighbor, gitGraphMoveDetailsFocus, gitGraphSameStringSet, gitGraphRepoList, gitGraphEnsureWatcherBound, }; diff --git a/test/project-git-graph-view.test.js b/test/project-git-graph-view.test.js index 86018eec3..ad397ddae 100644 --- a/test/project-git-graph-view.test.js +++ b/test/project-git-graph-view.test.js @@ -786,3 +786,120 @@ test('gitGraphEnsureWatcherBound binds once and repaints into the current Git Hi delete require.cache[modulePath]; } }); + +// === Inline Commit Details open under the clicked row and stay in view === + +function inlineDetailsState(id) { + const state = view.gitGraphState(id); + state.repositories = [{ path: '/repo', git: true }]; + state.selectedRepo = '/repo'; + state.rows = [ + { hash: 'c3', kind: 'commit', parents: ['c2'], subject: 'third' }, + { hash: 'c2', kind: 'commit', parents: ['c1'], subject: 'second' }, + { hash: 'c1', kind: 'commit', parents: [], subject: 'first' }, + ]; + state.layout = state.rows.map(() => ({ lane: 0, colorIndex: 0, edges: [] })); + return state; +} + +/** Index of each row and of the inline panel in the rows markup. */ +function inlineOrder(html) { + return [...html.matchAll(/data-gg-hash="(c\d)" data-row-index|id="gg-details-inline"/g)].map(m => m[1] || 'details'); +} + +test('the inline Commit Details sit right under the clicked row, not after the last loaded one', () => { + const state = inlineDetailsState('proj-inline-1'); + state.selectedHash = 'c2'; + state.detailsData = { mode: 'single', hash: 'c2', kind: 'commit', commit: null, files: [] }; + assert.deepEqual(inlineOrder(view.gitGraphRowsHtml(state)), ['c3', 'c2', 'details', 'c1']); +}); + +test('a comparison opens under the row clicked first, and a commit no longer loaded still shows its details at the end', () => { + const state = inlineDetailsState('proj-inline-2'); + state.selectedHash = 'c3'; + state.compareHash = 'c1'; + state.detailsData = { mode: 'compare', fromHash: 'c1', toHash: 'c3', files: [] }; + assert.deepEqual(inlineOrder(view.gitGraphRowsHtml(state)), ['c3', 'details', 'c2', 'c1']); + + state.selectedHash = 'gone'; + state.detailsData = { mode: 'single', hash: 'gone', kind: 'commit', commit: null, files: [] }; + assert.deepEqual(inlineOrder(view.gitGraphRowsHtml(state)), ['c3', 'c2', 'c1', 'details']); +}); + +test('opening, loading and closing the inline details never jumps the table back to the top', async () => { + const state = inlineDetailsState('proj-inline-3'); + const project = { id: 'proj-inline-3' }; + const body = fakeScrollBody(); + global.window = { api: { getGitGraphCommitDetail: () => Promise.resolve({ ok: true, commit: { hash: 'c2', parents: ['c1'] }, files: [] }) } }; + body.querySelector('.gg-table-wrap').scrollTop = 400; + + view.gitGraphOpenDetails(project, state, body, state.rows[1]); + assert.equal(body.querySelector('.gg-table-wrap').scrollTop, 400, 'the loading paint keeps the scroll position'); + await new Promise(resolve => setImmediate(resolve)); + assert.equal(state.detailsLoading, false); + assert.equal(body.querySelector('.gg-table-wrap').scrollTop, 400, 'the loaded paint keeps the scroll position'); + + view.gitGraphCloseDetails(project, state, body); + assert.equal(body.querySelector('.gg-table-wrap').scrollTop, 400, 'closing keeps the scroll position'); +}); + +/** A wrap/row/panel trio with fixed geometry, in the wrap's own coordinates. */ +function centeringFixture({ scrollTop, clientHeight, headHeight, rowTop, rowHeight, panelHeight }) { + const wrap = { + scrollTop, clientHeight, + getBoundingClientRect: () => ({ top: 100 }), + querySelector: (sel) => (sel === '.gg-thead' ? { offsetHeight: headHeight } : null), + }; + const at = (top, height) => () => ({ top: 100 + top - wrap.scrollTop, bottom: 100 + top + height - wrap.scrollTop }); + const row = { classList: { contains: (c) => c === 'gg-row' }, getBoundingClientRect: at(rowTop, rowHeight) }; + const panel = { + previousElementSibling: row, + closest: (sel) => (sel === '.gg-table-wrap' ? wrap : null), + getBoundingClientRect: at(rowTop + rowHeight, panelHeight), + }; + return { wrap, panel }; +} + +test('auto-center waits for the loaded details, then centers the row and its panel inside the table only', () => { + const state = inlineDetailsState('proj-inline-4'); + const { wrap, panel } = centeringFixture({ scrollTop: 0, clientHeight: 600, headHeight: 30, rowTop: 1000, rowHeight: 24, panelHeight: 200 }); + state.detailsJustOpened = true; + state.detailsLoading = true; + view.gitGraphMaybeAutoCenterDetails(state, panel); + assert.equal(wrap.scrollTop, 0, 'a "Loading…" placeholder is not worth scrolling to'); + assert.equal(state.detailsJustOpened, true); + + state.detailsLoading = false; + view.gitGraphMaybeAutoCenterDetails(state, panel); + // 570px visible under the 30px header, a 224px block: (570 - 224) / 2 = 173 above it. + assert.equal(wrap.scrollTop, 1000 - 30 - 173); + assert.equal(state.detailsJustOpened, false, 'only once per open'); + + const before = wrap.scrollTop; + view.gitGraphMaybeAutoCenterDetails(state, panel); + assert.equal(wrap.scrollTop, before, 'a later repaint of the same open panel leaves the scroll alone'); +}); + +test('a panel taller than the table puts its row at the top of the table', () => { + const { wrap, panel } = centeringFixture({ scrollTop: 0, clientHeight: 600, headHeight: 30, rowTop: 1000, rowHeight: 24, panelHeight: 900 }); + const state = inlineDetailsState('proj-inline-5'); + state.detailsJustOpened = true; + view.gitGraphMaybeAutoCenterDetails(state, panel); + assert.equal(wrap.scrollTop, 1000 - 30); +}); + +test('beside an inline panel a lane change still bends within one row, then the line runs straight', () => { + const rows = [{ hash: 'child', kind: 'commit', parents: ['parent'] }, { hash: 'parent', kind: 'commit', parents: [] }]; + const layout = [ + { lane: 1, colorIndex: 0, edges: [{ parentHash: 'parent', toLane: 0, viaLane: 1, style: 'same-lane' }] }, + { lane: 0, colorIndex: 0, edges: [] }, + ]; + const pathOf = (svg) => / render.GG_LANE_WIDTH / 2 + n * render.GG_LANE_WIDTH; + assert.equal(d, `M${lane(1)} 15 L${lane(1)} 45 L${lane(1)} 215 C${lane(1)} 230, ${lane(0)} 230, ${lane(0)} 245`); +});