mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 06:10:44 +08:00
desktop: Add a compare menu to patch revisions
This commit is contained in:
@@ -77,6 +77,10 @@
|
||||
// The revision and commit the patch is marked merged at, if any.
|
||||
merge?: { revision: string; commit: string };
|
||||
onViewRevision?: (revisionId: string) => void;
|
||||
// The commit to diff the revision's head against instead of its base, such
|
||||
// as the previous revision's head. Read when the tab opens and cleared here
|
||||
// once the diff on screen is anything else: another revision or a commit.
|
||||
compareFrom?: string;
|
||||
}
|
||||
|
||||
/* eslint-disable prefer-const */
|
||||
@@ -101,6 +105,7 @@
|
||||
revisions = [],
|
||||
merge,
|
||||
onViewRevision,
|
||||
compareFrom = $bindable(),
|
||||
}: Props = $props();
|
||||
/* eslint-enable prefer-const */
|
||||
|
||||
@@ -112,7 +117,7 @@
|
||||
// Parent reuses this component across patch revisions; a sibling $effect
|
||||
// resets base and head when patchId changes.
|
||||
// svelte-ignore state_referenced_locally
|
||||
let base = $state(revision.base);
|
||||
let base = $state(compareFrom ?? revision.base);
|
||||
// svelte-ignore state_referenced_locally
|
||||
let head = $state(revision.head);
|
||||
|
||||
@@ -133,6 +138,7 @@
|
||||
selectedCommitData = undefined;
|
||||
editingDescription = false;
|
||||
base = nextBase;
|
||||
compareFrom = undefined;
|
||||
head = nextHead;
|
||||
});
|
||||
});
|
||||
@@ -152,6 +158,7 @@
|
||||
}) {
|
||||
head = headId;
|
||||
base = baseId;
|
||||
compareFrom = undefined;
|
||||
selectedCommit = commitId;
|
||||
selectedCommitData = commit;
|
||||
filesExpanded = showFiles;
|
||||
@@ -334,8 +341,11 @@
|
||||
// list alone decides it: when the head is a strict ancestor of the base, the
|
||||
// tree diff between them is the base's newer commits in reverse, not the
|
||||
// patch's changes.
|
||||
// A comparison is another range: an empty revision still differs from the
|
||||
// one before it.
|
||||
const emptyRevision = $derived(
|
||||
commitListRange === `${revision.base}..${revision.head}` &&
|
||||
compareFrom === undefined &&
|
||||
commitListRange === `${revision.base}..${revision.head}` &&
|
||||
commitList.length === 0,
|
||||
);
|
||||
const earlierRevision = $derived(
|
||||
@@ -352,7 +362,8 @@
|
||||
`${revision.base}..${revision.head}`,
|
||||
selectedCommit,
|
||||
);
|
||||
if (only) {
|
||||
// A comparison is a range of its own, which selecting the commit would end.
|
||||
if (only && compareFrom === undefined) {
|
||||
selectRevision({
|
||||
headId: revision.head,
|
||||
baseId: revision.base,
|
||||
@@ -409,6 +420,13 @@
|
||||
diffView?.scrollToFilesTop();
|
||||
}
|
||||
|
||||
/// Go back to the revision's own changes, which ends a comparison. Driven
|
||||
/// from the tab bar, which the patch view renders.
|
||||
export function showRevisionChanges() {
|
||||
selectRevision({ headId: revision.head, baseId: revision.base });
|
||||
void scrollToDiff();
|
||||
}
|
||||
|
||||
/// Collapse or expand every file. Driven from the tab bar, which the patch
|
||||
/// view renders.
|
||||
export function setAllFilesCollapsed(collapsed: boolean) {
|
||||
@@ -461,7 +479,8 @@
|
||||
useShortcuts(
|
||||
{
|
||||
shortcut: "previousCommit",
|
||||
enabled: () => commitList.length > 1,
|
||||
// The commits column is hidden during a comparison.
|
||||
enabled: () => commitList.length > 1 && compareFrom === undefined,
|
||||
run: () => {
|
||||
const current = commitList.findIndex(c => c.id === selectedCommit);
|
||||
selectCommitAt(stepCommitIndex(current, commitList.length, -1));
|
||||
@@ -469,7 +488,8 @@
|
||||
},
|
||||
{
|
||||
shortcut: "nextCommit",
|
||||
enabled: () => commitList.length > 1,
|
||||
// The commits column is hidden during a comparison.
|
||||
enabled: () => commitList.length > 1 && compareFrom === undefined,
|
||||
run: () => {
|
||||
const current = commitList.findIndex(c => c.id === selectedCommit);
|
||||
selectCommitAt(stepCommitIndex(current, commitList.length, 1));
|
||||
@@ -980,6 +1000,8 @@
|
||||
codeComments={diffCodeComments}
|
||||
commentCommit={isRevisionDiff ? head : undefined}
|
||||
header={diffHeader}
|
||||
overlayLeft={emptyRevision ? undefined : commitsColumn}
|
||||
overlayLeft={emptyRevision || compareFrom !== undefined
|
||||
? undefined
|
||||
: commitsColumn}
|
||||
{filesHeader}
|
||||
stickyTop={tabs} />
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import type { ComponentProps } from "svelte";
|
||||
|
||||
import { slide } from "svelte/transition";
|
||||
|
||||
import { itemDiff } from "@app/lib/cobActivity";
|
||||
@@ -26,6 +28,7 @@
|
||||
import Markdown from "@app/components/Markdown.svelte";
|
||||
import NodeId from "@app/components/NodeId.svelte";
|
||||
import ReviewProgressChip from "@app/components/ReviewProgressChip.svelte";
|
||||
import RevisionCompareMenu from "@app/components/RevisionCompareMenu.svelte";
|
||||
|
||||
interface Props {
|
||||
op: FlattenedPatchOperation;
|
||||
@@ -37,6 +40,8 @@
|
||||
onViewFullReview?: () => void;
|
||||
onOpenReview?: () => void;
|
||||
onOpenChanges?: () => void;
|
||||
// A revision's range and what to do with it, for the compare menu.
|
||||
compare?: ComponentProps<typeof RevisionCompareMenu>;
|
||||
rid?: string;
|
||||
patchId?: string;
|
||||
latest?: boolean;
|
||||
@@ -55,6 +60,7 @@
|
||||
onViewFullReview,
|
||||
onOpenReview,
|
||||
onOpenChanges,
|
||||
compare,
|
||||
rid,
|
||||
patchId,
|
||||
latest = false,
|
||||
@@ -423,6 +429,9 @@
|
||||
{#if expanded}
|
||||
{@render copyLinkButton()}
|
||||
{/if}
|
||||
{#if compare}
|
||||
<RevisionCompareMenu {...compare} />
|
||||
{/if}
|
||||
</div>
|
||||
{#if !firstRevision && desc.body && expanded && !bodyExternal}
|
||||
<div class="revision-body" transition:slide={{ duration: 180 }}>
|
||||
|
||||
@@ -109,7 +109,12 @@
|
||||
// hidden from the diff.
|
||||
hiddenCommentSources?: string[];
|
||||
filesExpanded?: boolean;
|
||||
onViewChanges?: (revisionId: string) => void;
|
||||
// Opens the Changes tab on a revision; `compareFrom` diffs its head against
|
||||
// that commit instead of its base.
|
||||
onViewChanges?: (revisionId: string, compareFrom?: string) => void;
|
||||
// The commit the Changes tab diffs the revision's head against, when it is
|
||||
// not the base. Cleared here once the diff on screen is another range.
|
||||
compareFrom?: string;
|
||||
// The revision and commit the patch is marked merged at, if any.
|
||||
merge?: { revision: string; commit: string };
|
||||
// The patch view's own header, forwarded to the Changes tab so it can render
|
||||
@@ -140,6 +145,7 @@
|
||||
hiddenCommentSources = [],
|
||||
filesExpanded = $bindable(true),
|
||||
onViewChanges,
|
||||
compareFrom = $bindable(),
|
||||
merge,
|
||||
chrome,
|
||||
tabs,
|
||||
@@ -154,6 +160,11 @@
|
||||
changes?.setAllFilesCollapsed(collapsed);
|
||||
}
|
||||
|
||||
/// Forwarded to the Changes tab so the tab bar can end a comparison.
|
||||
export function showRevisionChanges() {
|
||||
changes?.showRevisionChanges();
|
||||
}
|
||||
|
||||
/// Forwarded to the Changes tab so the tab bar's stepper can walk the comments
|
||||
/// on the diff.
|
||||
export function stepComment(delta: number) {
|
||||
@@ -174,6 +185,23 @@
|
||||
// Timeline order, as delivered — see `revisionPosition`.
|
||||
const latestRevisionId = $derived(revisions.at(-1)?.id);
|
||||
const firstRevisionId = $derived(revisions[0]?.id);
|
||||
|
||||
// What each revision's compare menu offers, by revision id: its own base and
|
||||
// the revision before it. Empty when there is no Changes tab to open them in.
|
||||
const compareMenus = $derived(
|
||||
Object.fromEntries(
|
||||
(onViewChanges ? revisions : []).map((rev, index) => [
|
||||
rev.id,
|
||||
{
|
||||
base: rev.base,
|
||||
head: rev.head,
|
||||
previous: revisions[index - 1],
|
||||
onCompare: (from?: string) => onViewChanges?.(rev.id, from),
|
||||
},
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
const targetBranch = $derived(
|
||||
patchTargetBranch === undefined
|
||||
? undefined
|
||||
@@ -1154,6 +1182,7 @@
|
||||
onOpenChanges={onViewChanges
|
||||
? () => onViewChanges?.(revId)
|
||||
: undefined}
|
||||
compare={compareMenus[revId]}
|
||||
{expanded}
|
||||
hideAuthor={opts.hideAuthor}
|
||||
bodyExternal
|
||||
@@ -1406,7 +1435,8 @@
|
||||
onToggle={toggleable ? () => toggleRevision(revId) : undefined}
|
||||
onOpenChanges={onViewChanges
|
||||
? () => onViewChanges?.(revId)
|
||||
: undefined} />
|
||||
: undefined}
|
||||
compare={compareMenus[revId]} />
|
||||
</div>
|
||||
{:else}
|
||||
<PatchActivityItem
|
||||
@@ -1418,7 +1448,8 @@
|
||||
onToggle={toggleable ? () => toggleRevision(revId) : undefined}
|
||||
onOpenChanges={onViewChanges
|
||||
? () => onViewChanges?.(revId)
|
||||
: undefined} />
|
||||
: undefined}
|
||||
compare={compareMenus[revId]} />
|
||||
{/if}
|
||||
{:else if data.op.type === "review"}
|
||||
{@const opId = data.op.id}
|
||||
@@ -1507,7 +1538,8 @@
|
||||
op={data.op}
|
||||
firstRevision
|
||||
openedAsDraft={data.openedAsDraft}
|
||||
hideAuthor={opts.hideAuthor} />
|
||||
hideAuthor={opts.hideAuthor}
|
||||
compare={compareMenus[data.op.id]} />
|
||||
{:else if data.kind === "olderRevisions"}
|
||||
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
||||
<div
|
||||
@@ -1583,6 +1615,7 @@
|
||||
onViewRevision={onViewChanges}
|
||||
{chrome}
|
||||
{tabs}
|
||||
bind:compareFrom
|
||||
bind:showingRevisionDiff
|
||||
bind:filesExpanded
|
||||
bind:commentPosition />
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
<script lang="ts">
|
||||
import { formatOid } from "@app/lib/utils";
|
||||
|
||||
import Button from "@app/components/Button.svelte";
|
||||
import DropdownList from "@app/components/DropdownList.svelte";
|
||||
import DropdownListItem from "@app/components/DropdownListItem.svelte";
|
||||
import Icon from "@app/components/Icon.svelte";
|
||||
import Popover, { closeFocused } from "@app/components/Popover.svelte";
|
||||
|
||||
interface Props {
|
||||
base: string;
|
||||
head: string;
|
||||
// The revision before this one, absent for the first.
|
||||
previous?: { id: string; base: string; head: string };
|
||||
// Called with the commit to diff `head` against, or with nothing for the
|
||||
// revision's own base.
|
||||
onCompare: (from?: string) => void;
|
||||
}
|
||||
|
||||
const { base, head, previous, onCompare }: Props = $props();
|
||||
|
||||
let expanded = $state(false);
|
||||
|
||||
// Diffing two heads on different bases would mix in every change between the
|
||||
// bases, so the previous revision is only offered on the same base.
|
||||
const baseMoved = $derived(previous !== undefined && previous.base !== base);
|
||||
|
||||
// Nothing changed between two revisions on the same commit.
|
||||
const sameHead = $derived(previous?.head === head);
|
||||
|
||||
const range = (from: string) => `${formatOid(from)}..${formatOid(head)}`;
|
||||
|
||||
function compare(from?: string) {
|
||||
closeFocused();
|
||||
onCompare(from);
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.item {
|
||||
display: flex;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
.previous-id {
|
||||
color: var(--color-text-brand);
|
||||
}
|
||||
</style>
|
||||
|
||||
<Popover placement="bottom-end" bind:expanded>
|
||||
{#snippet toggle(onclick)}
|
||||
<Button
|
||||
variant="naked"
|
||||
active={expanded}
|
||||
title="Compare revision"
|
||||
onclick={e => {
|
||||
// The row around it toggles on click.
|
||||
e.stopPropagation();
|
||||
onclick();
|
||||
}}
|
||||
styleHeight="1.5rem"
|
||||
styleWidth="1.5rem"
|
||||
stylePadding="0"
|
||||
styleJustifyContent="center">
|
||||
<Icon name="ellipsis-vertical" />
|
||||
</Button>
|
||||
{/snippet}
|
||||
|
||||
{#snippet popover()}
|
||||
<div
|
||||
style:border="1px solid var(--color-border-subtle)"
|
||||
style:border-radius="var(--border-radius-md)"
|
||||
style:background-color="var(--color-surface-canvas)"
|
||||
style:padding="0.25rem">
|
||||
<DropdownList items={previous ? ["base", "previous"] : ["base"]}>
|
||||
{#snippet item(entry)}
|
||||
{#if entry === "base"}
|
||||
<DropdownListItem
|
||||
selected={false}
|
||||
title={range(base)}
|
||||
onclick={() => compare()}>
|
||||
<span class="item">
|
||||
Compare to base:
|
||||
<span class="txt-id">{formatOid(base)}</span>
|
||||
</span>
|
||||
</DropdownListItem>
|
||||
{:else if previous}
|
||||
<DropdownListItem
|
||||
selected={false}
|
||||
disabled={baseMoved || sameHead}
|
||||
title={baseMoved
|
||||
? "Previous revision has different base"
|
||||
: sameHead
|
||||
? "Same commit as the previous revision"
|
||||
: range(previous.head)}
|
||||
onclick={() =>
|
||||
// An empty previous revision's head is this one's base.
|
||||
compare(previous.head === base ? undefined : previous.head)}>
|
||||
<span class="item">
|
||||
Compare to previous revision:
|
||||
<span class="txt-id previous-id">
|
||||
{formatOid(previous.id)}
|
||||
</span>
|
||||
</span>
|
||||
</DropdownListItem>
|
||||
{/if}
|
||||
{/snippet}
|
||||
</DropdownList>
|
||||
</div>
|
||||
{/snippet}
|
||||
</Popover>
|
||||
@@ -38,6 +38,7 @@
|
||||
absoluteTimestamp,
|
||||
authorForNodeId,
|
||||
didFromPublicKey,
|
||||
formatOid,
|
||||
formatTimestamp,
|
||||
patchStatusLabel,
|
||||
publicKeyFromDid,
|
||||
@@ -186,6 +187,11 @@
|
||||
// When opening a specific revision's changes from the timeline, the target
|
||||
// survives the view switch (the reset effect below honours it once).
|
||||
let pendingRevisionId: string | undefined;
|
||||
let pendingCompareFrom: string | undefined;
|
||||
// The commit the Changes tab diffs the selected revision's head against, when
|
||||
// it is not the base: the previous revision's head, from the compare menu.
|
||||
// Set here only on the way into the tab; the tab ends the comparison itself.
|
||||
let compareFrom: string | undefined = $state();
|
||||
|
||||
// Warm the Changes-tab data for the selected revision in the background, so
|
||||
// opening the tab is instant instead of showing a fetch delay. Both halves of
|
||||
@@ -442,7 +448,9 @@
|
||||
if (patchView !== lastPatchView) {
|
||||
lastPatchView = patchView;
|
||||
selectedRevisionId = pendingRevisionId ?? revisions.slice(-1)[0].id;
|
||||
compareFrom = pendingCompareFrom;
|
||||
pendingRevisionId = undefined;
|
||||
pendingCompareFrom = undefined;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -964,6 +972,10 @@
|
||||
selectedRevision.id,
|
||||
config.publicKey,
|
||||
);
|
||||
// A review is written on the revision's own changes.
|
||||
if (compareFrom !== undefined) {
|
||||
revisionComponent?.showRevisionChanges();
|
||||
}
|
||||
setView("changes");
|
||||
}}
|
||||
title={hasOwnPublishedReviewOnSelected
|
||||
@@ -1151,7 +1163,18 @@
|
||||
Back to latest revision
|
||||
</Button>
|
||||
{/if}
|
||||
{#if orderedRevisions.length > 1}
|
||||
{#if compareFrom !== undefined}
|
||||
<Button
|
||||
variant="outline"
|
||||
title="Show the revision's own changes"
|
||||
onclick={() => revisionComponent?.showRevisionChanges()}>
|
||||
Compare
|
||||
<span class="txt-id">
|
||||
{formatOid(compareFrom)}..{formatOid(selectedRevision.head)}
|
||||
</span>
|
||||
<Icon name="close" />
|
||||
</Button>
|
||||
{:else if orderedRevisions.length > 1}
|
||||
<Popover
|
||||
popoverPadding="0"
|
||||
placement="bottom-start"
|
||||
@@ -1405,14 +1428,21 @@
|
||||
draftReviewId={ownDraftReview?.id}
|
||||
{draftRevisionIds}
|
||||
hiddenCommentSources={[...hiddenCommentSources]}
|
||||
onViewChanges={revisionId => {
|
||||
pendingRevisionId = revisionId;
|
||||
onViewChanges={(revisionId, from) => {
|
||||
// Only a view switch takes the pending pair, so one set on the
|
||||
// Changes tab would wait for a later, unrelated switch.
|
||||
if (patchView !== "changes") {
|
||||
pendingRevisionId = revisionId;
|
||||
pendingCompareFrom = from;
|
||||
}
|
||||
selectedRevisionId = revisionId;
|
||||
compareFrom = from;
|
||||
setView("changes");
|
||||
}}
|
||||
merge={patch.state.status === "merged" ? patch.state : undefined}
|
||||
chrome={patchHeader}
|
||||
{tabs}
|
||||
bind:compareFrom
|
||||
bind:showingRevisionDiff
|
||||
bind:filesExpanded
|
||||
bind:commentPosition />
|
||||
|
||||
Reference in New Issue
Block a user