Skip to content

Commit 92665c3

Browse files
committed
Don't create history entries when new selection matches old
1 parent a28dea6 commit 92665c3

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

web/src/lib/components/diff/concise-diff-view.svelte.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ export interface LineSelection {
5555
end: LineRef;
5656
}
5757

58+
export function lineSelectionsEqual(a: LineSelection | undefined, b: LineSelection | undefined): boolean {
59+
if (!a && !b) return true;
60+
if (!a || !b) return false;
61+
return a.hunk === b.hunk && a.start.idx === b.start.idx && a.end.idx === b.end.idx;
62+
}
63+
5864
export interface UnresolvedLineSelection {
5965
start: UnresolvedLineRef;
6066
end: UnresolvedLineRef;

web/src/lib/diff-viewer.svelte.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
writeLineRef,
1919
parseLineRef,
2020
type UnresolvedLineSelection,
21+
lineSelectionsEqual,
2122
} from "$lib/components/diff/concise-diff-view.svelte";
2223
import { countOccurrences, type FileTreeNodeData, makeFileTree, type LazyPromise, lazyPromise, animationFramePromise, yieldToBrowser } from "$lib/util";
2324
import { onDestroy, onMount, tick } from "svelte";
@@ -422,21 +423,28 @@ export class MultiFileDiffViewerState {
422423
}
423424

424425
setSelection(file: FileDetails, lines: LineSelection | undefined) {
426+
const oldSelection = this.selection;
425427
this.selection = { file, lines };
428+
const selectionChanged = oldSelection?.file.index !== file.index || !lineSelectionsEqual(oldSelection?.lines, lines);
426429

427-
goto(`?${page.url.searchParams}#${makeUrlHashValue(this.selection)}`, {
428-
keepFocus: true,
429-
state: this.createPageState(),
430-
});
430+
if (selectionChanged) {
431+
goto(`?${page.url.searchParams}#${makeUrlHashValue(this.selection)}`, {
432+
keepFocus: true,
433+
state: this.createPageState(),
434+
});
435+
}
431436
}
432437

433438
clearSelection() {
439+
const oldSelection = this.selection;
434440
this.selection = undefined;
435441

436-
goto(`?${page.url.searchParams}`, {
437-
keepFocus: true,
438-
state: this.createPageState(),
439-
});
442+
if (oldSelection !== undefined) {
443+
goto(`?${page.url.searchParams}`, {
444+
keepFocus: true,
445+
state: this.createPageState(),
446+
});
447+
}
440448
}
441449

442450
scrollToFile(index: number, options: { autoExpand?: boolean; smooth?: boolean; focus?: boolean } = {}) {
@@ -604,7 +612,7 @@ export class MultiFileDiffViewerState {
604612

605613
if (this.urlSelection) {
606614
// TODO: This does store store the proper scroll offset on initial load
607-
615+
608616
const urlSelection = this.urlSelection;
609617
this.urlSelection = undefined;
610618
const file = this.fileDetails.find((f) => f.toFile === urlSelection.file);

0 commit comments

Comments
 (0)