Skip to content

Commit 08e6d9d

Browse files
committed
Wait for gutter cells to render before rendering jump to next uncovered line button
1 parent e98592c commit 08e6d9d

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/github-injector.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function setupJumpToUncoveredLineHotkey() {
7979
document.addEventListener("keydown", uncoveredLineKeyListener);
8080
}
8181

82-
function tryInjectDiffCommitUI(): void {
82+
async function tryInjectDiffCommitUI(): Promise<void> {
8383
const rootElement =
8484
document.getElementById("diff-content-parent") || document.body;
8585
if (rootElement.classList.contains("qlty-diff-ui")) return;
@@ -92,32 +92,36 @@ function tryInjectDiffCommitUI(): void {
9292
links.push(link);
9393
});
9494

95-
if (links.length === 0) {
96-
return; // No links to process
95+
await processFileLinks(rootElement, links);
96+
97+
if (isPRPage()) {
98+
addPRPageBadge();
99+
addNextUncoveredLineButton();
100+
} else {
101+
addDiffPageBadge();
97102
}
98103

99-
links.forEach(async (link) => {
104+
console.log("[qlty] injected diff UI");
105+
rootElement.classList.add("qlty-diff-ui");
106+
}
107+
108+
async function processFileLinks(rootElement: HTMLElement, links: Element[]) {
109+
const promises: Promise<void>[] = [];
110+
111+
for (const link of links) {
100112
const path = (link as HTMLElement).innerText.replace("\u200E", "");
101113
const fileId = link.getAttribute("href")?.split("#")[1] ?? "";
102114

103-
await injectIntoFileContainer(
115+
promises.push(injectIntoFileContainer(
104116
link.parentElement?.parentElement ?? link,
105117
path,
106118
rootElement.querySelectorAll(
107119
`[data-diff-anchor="${fileId}"] td:nth-last-child(2)`,
108120
),
109-
);
110-
});
111-
112-
if (isPRPage()) {
113-
addPRPageBadge();
114-
addNextUncoveredLineButton();
115-
} else {
116-
addDiffPageBadge();
121+
));
117122
}
118123

119-
console.log("[qlty] injected diff UI");
120-
rootElement.classList.add("qlty-diff-ui");
124+
return Promise.all(promises);
121125
}
122126

123127
function isPRPage(): boolean {
@@ -174,6 +178,13 @@ function jumpToNextUncoveredLine() {
174178
el.parentElement?.classList.contains("selected-line"),
175179
);
176180

181+
const currentElement = uncoveredLineDivs[currentlySelectedIndex];
182+
if (currentElement) {
183+
// Click the current line to deselect it; this is necessary to make sure
184+
// the selected line doesn't stay selected (occurs sometimes during back/forward nav)
185+
selectLinkableLine(currentElement.parentElement!);
186+
}
187+
177188
const nextIndex = (currentlySelectedIndex + 1) % uncoveredLineDivs.length;
178189
const nextElement = uncoveredLineDivs[nextIndex];
179190
nextElement.scrollIntoView({ behavior: "smooth", block: "center" });
@@ -184,6 +195,10 @@ function jumpToNextUncoveredLine() {
184195
return;
185196
}
186197

198+
selectLinkableLine(linkableLine);
199+
}
200+
201+
function selectLinkableLine(linkableLine: HTMLElement) {
187202
// Click the line to highlight it
188203
// If we don't also trigger a click event, then our line gets unhighlighted on
189204
// subsequent button clicks

0 commit comments

Comments
 (0)