Skip to content

Commit 12b974f

Browse files
committed
Fix selection not resolving when it should (navigating back to initial
load state)
1 parent cd20040 commit 12b974f

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { DEFAULT_THEME_LIGHT } from "$lib/global-options.svelte";
1919
import type { WritableBoxedValues } from "svelte-toolbelt";
2020
import type { Attachment } from "svelte/attachments";
2121
import { on } from "svelte/events";
22+
import { watch } from "runed";
2223

2324
export interface UnresolvedLineRef {
2425
/**
@@ -1157,7 +1158,6 @@ export class ConciseDiffViewState<K> {
11571158

11581159
private selectionAnchor: { hunkIdx: number; lineIdx: number } | null = null;
11591160
private dragSelectionState: { hunk: DiffViewerPatchHunk; didMove: boolean } | null = null;
1160-
private suppressNextClick = false;
11611161

11621162
constructor(props: ConciseDiffViewStateProps<K>) {
11631163
this.props = props;
@@ -1166,6 +1166,15 @@ export class ConciseDiffViewState<K> {
11661166
this.update();
11671167
});
11681168

1169+
watch([() => this.diffViewerPatch, () => this.props.unresolvedSelection.current], ([patchPromise, unresolvedSelection], [oldPatchPromise]) => {
1170+
// Update or resolve the selection whenever the patch changes or unresolvedSelection is set
1171+
if (patchPromise !== oldPatchPromise || unresolvedSelection !== undefined) {
1172+
patchPromise.then((patch) => {
1173+
this.resolveOrUpdateSelection(patch);
1174+
});
1175+
}
1176+
});
1177+
11691178
$effect(() => {
11701179
const promise = getBaseColors(this.props.syntaxHighlightingTheme.current, this.props.syntaxHighlighting.current);
11711180
promise.then(
@@ -1209,10 +1218,9 @@ export class ConciseDiffViewState<K> {
12091218
);
12101219
this.cachedState = new ConciseDiffViewCachedState(promise, this.props);
12111220
promise.then(
1212-
(patch) => {
1221+
() => {
12131222
// Don't replace a potentially completed promise with a pending one, wait until the replacement is ready for smooth transitions
12141223
this.diffViewerPatch = promise;
1215-
this.resolveOrUpdateSelection(patch);
12161224
},
12171225
() => {
12181226
// Propagate errors
@@ -1334,11 +1342,6 @@ export class ConciseDiffViewState<K> {
13341342
const onDragEnd = (e: PointerEvent) => {
13351343
element.releasePointerCapture(e.pointerId);
13361344
abortController.abort();
1337-
1338-
// Suppress the click event only if we actually moved during the drag
1339-
if (this.dragSelectionState?.didMove) {
1340-
this.suppressNextClick = true;
1341-
}
13421345
this.dragSelectionState = null;
13431346
};
13441347

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,6 @@ export class MultiFileDiffViewerState {
395395

396396
if (page.state.initialLoad ?? false) {
397397
if (this.selection) {
398-
// TODO: restoring an unresolved selection does not work until something triggers resolveOrUpdateSelection
399398
const hasLines = this.selection.lines || this.selection.unresolvedLines;
400399
this.scrollToFile(this.selection.file.index, {
401400
focus: !hasLines,
@@ -673,6 +672,8 @@ export class MultiFileDiffViewerState {
673672
const selectedFile = urlSelection !== undefined ? this.fileDetails.find((f) => f.toFile === urlSelection.file) : undefined;
674673

675674
if (urlSelection && selectedFile && this.diffMetadata.linkable) {
675+
// this jump doesn't work as consistently as I'd like, but at least it's consistent between navigation and initial load for a given diff...
676+
// seems like the jump to the file header ends up taking priority in some cases, in particular when the file is close to the top of the list?
676677
this.jumpToSelection = urlSelection.lines !== undefined;
677678
this.selection = {
678679
file: selectedFile,

0 commit comments

Comments
 (0)