mirror of
https://github.com/open-webui/computer.git
synced 2026-11-22 21:50:43 -05:00
refac
This commit is contained in:
1 parent
27d404ba59
commit
c5d4bf76a5
12 files changed
+461
-324
No files matched your search
@@ -39,8 +39,10 @@ export const getGitLog = (root: string, limit = 30) =>
|
||||
|
||||
export const getGitDiff = (params: string) => fetchJSON(`/api/git/diff?${params}`);
|
||||
|
||||
export const getGitShow = (root: string, ref: string) =>
|
||||
fetchJSON(`/api/git/show?root=${encodeURIComponent(root)}&ref=${encodeURIComponent(ref)}`);
|
||||
export const getGitShow = (root: string, ref: string, ignoreWhitespace = false) =>
|
||||
fetchJSON(
|
||||
`/api/git/show?root=${encodeURIComponent(root)}&ref=${encodeURIComponent(ref)}&ignore_whitespace=${ignoreWhitespace}`
|
||||
);
|
||||
|
||||
export const getGitBranches = (root: string) =>
|
||||
fetchJSON(`/api/git/branches?root=${encodeURIComponent(root)}`);
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
<script lang="ts">
|
||||
import { diffDisplayMode } from '$lib/stores/gitDiffSettings';
|
||||
import {
|
||||
groupDiffLines,
|
||||
numberDiffLines,
|
||||
splitDiffRows,
|
||||
withInlineDiffSegments,
|
||||
languageForPath,
|
||||
type DiffHunk,
|
||||
type DiffLine,
|
||||
type InlineDiffLine
|
||||
} from '$lib/utils/diff';
|
||||
import SyntaxDiffLine from './SyntaxDiffLine.svelte';
|
||||
|
||||
interface Props {
|
||||
hunk: DiffHunk;
|
||||
path: string;
|
||||
showNumbers?: boolean;
|
||||
}
|
||||
|
||||
let { hunk, path, showNumbers = true }: Props = $props();
|
||||
|
||||
const lines = $derived(withInlineDiffSegments(numberDiffLines(hunk)));
|
||||
const language = $derived(languageForPath(path));
|
||||
|
||||
function blockClass(type: DiffLine['type']): string {
|
||||
if (type === 'added')
|
||||
return 'bg-green-100 border-l-[0.1875rem] border-l-green-500 dark:bg-green-500/15 dark:border-l-green-400';
|
||||
if (type === 'removed') return 'bg-red-100 diff-gutter-removed dark:bg-red-500/15';
|
||||
return '';
|
||||
}
|
||||
|
||||
function textClass(type: DiffLine['type']): string {
|
||||
if (type === 'added') return 'text-green-900 dark:text-green-300';
|
||||
if (type === 'removed') return 'text-red-900 dark:text-red-300';
|
||||
return 'text-gray-600 dark:text-gray-400';
|
||||
}
|
||||
|
||||
function prefixClass(type: DiffLine['type']): string {
|
||||
if (type === 'added') return 'text-green-600 dark:text-green-400';
|
||||
if (type === 'removed') return 'text-red-500 dark:text-red-400';
|
||||
return 'text-gray-400 dark:text-gray-600';
|
||||
}
|
||||
|
||||
function linePrefix(type: DiffLine['type']): string {
|
||||
if (type === 'added') return '+';
|
||||
if (type === 'removed') return '-';
|
||||
return ' ';
|
||||
}
|
||||
|
||||
function blankSegments(): InlineDiffLine['segments'] {
|
||||
return [{ text: ' ', changed: false }];
|
||||
}
|
||||
|
||||
function splitCellClass(line: InlineDiffLine | null): string {
|
||||
if (!line) return '';
|
||||
if (line.type === 'added') return 'bg-green-100 dark:bg-green-500/15';
|
||||
if (line.type === 'removed') return 'bg-red-100 dark:bg-red-500/15';
|
||||
return '';
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if $diffDisplayMode === 'split'}
|
||||
{#each splitDiffRows(lines) as row}
|
||||
<div
|
||||
class="grid w-full {showNumbers
|
||||
? 'grid-cols-[2.75rem_minmax(0,1fr)_2.75rem_minmax(0,1fr)]'
|
||||
: 'grid-cols-[minmax(0,1fr)_minmax(0,1fr)]'}"
|
||||
>
|
||||
{#if showNumbers}
|
||||
<span
|
||||
class="select-none border-r border-black/5 px-2 text-right text-gray-400 dark:border-white/4 dark:text-gray-600 {splitCellClass(
|
||||
row.oldLine
|
||||
)}">{row.oldLine?.oldNumber ?? ''}</span
|
||||
>
|
||||
{/if}
|
||||
<div class="min-w-0 {splitCellClass(row.oldLine)}">
|
||||
{#if row.oldLine}
|
||||
<SyntaxDiffLine
|
||||
type={row.oldLine.type}
|
||||
content={row.oldLine.content || ' '}
|
||||
segments={row.oldLine.segments}
|
||||
{language}
|
||||
wrap
|
||||
class={textClass(row.oldLine.type)}
|
||||
/>
|
||||
{:else}
|
||||
<SyntaxDiffLine
|
||||
type="context"
|
||||
content=" "
|
||||
segments={blankSegments()}
|
||||
language="text"
|
||||
wrap
|
||||
class="text-transparent"
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
||||
{#if showNumbers}
|
||||
<span
|
||||
class="select-none border-l border-r border-black/5 px-2 text-right text-gray-400 dark:border-white/4 dark:text-gray-600 {splitCellClass(
|
||||
row.newLine
|
||||
)}">{row.newLine?.newNumber ?? ''}</span
|
||||
>
|
||||
{/if}
|
||||
<div
|
||||
class="min-w-0 border-l border-black/5 dark:border-white/4 {splitCellClass(row.newLine)}"
|
||||
>
|
||||
{#if row.newLine}
|
||||
<SyntaxDiffLine
|
||||
type={row.newLine.type}
|
||||
content={row.newLine.content || ' '}
|
||||
segments={row.newLine.segments}
|
||||
{language}
|
||||
wrap
|
||||
class={textClass(row.newLine.type)}
|
||||
/>
|
||||
{:else}
|
||||
<SyntaxDiffLine
|
||||
type="context"
|
||||
content=" "
|
||||
segments={blankSegments()}
|
||||
language="text"
|
||||
wrap
|
||||
class="text-transparent"
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
{:else}
|
||||
{#each groupDiffLines(lines) as group}
|
||||
<div class="w-full {blockClass(group.type)}">
|
||||
{#each group.lines as line}
|
||||
{#if showNumbers}
|
||||
<div class="grid w-full grid-cols-[2.75rem_2.75rem_1.25rem_auto]">
|
||||
<span
|
||||
class="select-none border-r border-black/5 px-2 text-right text-gray-400 dark:border-white/4 dark:text-gray-600"
|
||||
>{line.oldNumber ?? ''}</span
|
||||
>
|
||||
<span
|
||||
class="select-none border-r border-black/5 px-2 text-right text-gray-400 dark:border-white/4 dark:text-gray-600"
|
||||
>{line.newNumber ?? ''}</span
|
||||
>
|
||||
<span class="select-none px-1 text-center {prefixClass(line.type)}"
|
||||
>{linePrefix(line.type)}</span
|
||||
>
|
||||
<SyntaxDiffLine
|
||||
type={line.type}
|
||||
content={line.content || ' '}
|
||||
segments={line.segments}
|
||||
{language}
|
||||
class={textClass(line.type)}
|
||||
/>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="flex min-w-max">
|
||||
<span class="select-none px-2 text-center {prefixClass(line.type)}"
|
||||
>{linePrefix(line.type)}</span
|
||||
>
|
||||
<SyntaxDiffLine
|
||||
type={line.type}
|
||||
content={line.content || ' '}
|
||||
segments={line.segments}
|
||||
{language}
|
||||
class={textClass(line.type)}
|
||||
/>
|
||||
</div>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
{/each}
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.diff-gutter-removed {
|
||||
border-left: 0.1875rem solid transparent;
|
||||
border-image: repeating-linear-gradient(
|
||||
-45deg,
|
||||
#ef4444 0,
|
||||
#ef4444 1px,
|
||||
transparent 1px,
|
||||
transparent 0.1875rem
|
||||
)
|
||||
3;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,94 @@
|
||||
<script lang="ts">
|
||||
import { diffDisplayMode, hideWhitespaceChanges } from '$lib/stores/gitDiffSettings';
|
||||
import DropdownMenu from './DropdownMenu.svelte';
|
||||
import Icon from './Icon.svelte';
|
||||
|
||||
interface Props {
|
||||
anchor: HTMLElement;
|
||||
onclose: () => void;
|
||||
preferAbove?: boolean;
|
||||
forceAbove?: boolean;
|
||||
}
|
||||
|
||||
let { anchor, onclose, preferAbove = false, forceAbove = false }: Props = $props();
|
||||
|
||||
function toggleWhitespace() {
|
||||
hideWhitespaceChanges.set(!$hideWhitespaceChanges);
|
||||
}
|
||||
</script>
|
||||
|
||||
<DropdownMenu
|
||||
items={[]}
|
||||
{anchor}
|
||||
{onclose}
|
||||
{preferAbove}
|
||||
{forceAbove}
|
||||
align="end"
|
||||
className="w-56"
|
||||
headerDivider={false}
|
||||
footerDivider={false}
|
||||
>
|
||||
<div class="px-1 pb-1 text-xs">
|
||||
<div
|
||||
class="px-2 pb-0.5 pt-1 text-[0.625rem] font-medium uppercase tracking-wide text-gray-400 dark:text-gray-600"
|
||||
>
|
||||
Whitespace
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="app-interactive app-muted flex h-7 w-full items-center gap-2 rounded-xl px-2 text-left text-xs transition-colors duration-75"
|
||||
onclick={toggleWhitespace}
|
||||
aria-pressed={$hideWhitespaceChanges}
|
||||
>
|
||||
<Icon name="code" size={14} class="app-icon-muted shrink-0" />
|
||||
<span class="min-w-0 flex-1 truncate">Hide whitespace</span>
|
||||
<span
|
||||
class="relative h-4 w-7 shrink-0 rounded-full transition-colors duration-150 {$hideWhitespaceChanges
|
||||
? 'bg-gray-900 dark:bg-white'
|
||||
: 'bg-gray-300 dark:bg-gray-700'}"
|
||||
>
|
||||
<span
|
||||
class="absolute top-[0.125rem] h-3 w-3 rounded-full transition-all duration-150 {$hideWhitespaceChanges
|
||||
? 'left-[0.875rem] bg-white dark:bg-black'
|
||||
: 'left-[0.125rem] bg-white dark:bg-gray-500'}"
|
||||
></span>
|
||||
</span>
|
||||
</button>
|
||||
|
||||
<div
|
||||
class="px-2 pb-0.5 pt-1.5 text-[0.625rem] font-medium uppercase tracking-wide text-gray-400 dark:text-gray-600"
|
||||
>
|
||||
Diff Display
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="flex h-7 w-full items-center gap-2 rounded-xl px-2 text-left text-xs transition-colors duration-75 {$diffDisplayMode ===
|
||||
'unified'
|
||||
? 'app-interactive-active'
|
||||
: 'app-interactive app-muted'}"
|
||||
onclick={() => diffDisplayMode.set('unified')}
|
||||
>
|
||||
<Icon name="list" size={14} class="app-icon-muted shrink-0" />
|
||||
<span class="min-w-0 flex-1 truncate text-left">Unified</span>
|
||||
{#if $diffDisplayMode === 'unified'}
|
||||
<Icon name="check" size={12} class="app-icon-muted shrink-0" />
|
||||
{/if}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="flex h-7 w-full items-center gap-2 rounded-xl px-2 text-left text-xs transition-colors duration-75 {$diffDisplayMode ===
|
||||
'split'
|
||||
? 'app-interactive-active'
|
||||
: 'app-interactive app-muted'}"
|
||||
onclick={() => diffDisplayMode.set('split')}
|
||||
>
|
||||
<Icon name="split-view" size={14} class="app-icon-muted shrink-0" />
|
||||
<span class="min-w-0 flex-1 truncate">Split</span>
|
||||
{#if $diffDisplayMode === 'split'}
|
||||
<Icon name="check" size={12} class="app-icon-muted shrink-0" />
|
||||
{/if}
|
||||
</button>
|
||||
</div>
|
||||
</DropdownMenu>
|
||||
@@ -6,6 +6,7 @@
|
||||
import { tooltip } from '$lib/tooltip';
|
||||
import { readFile, writeFile } from '$lib/apis/files';
|
||||
import { getGitDiff } from '$lib/apis/git';
|
||||
import { hideWhitespaceChanges } from '$lib/stores/gitDiffSettings';
|
||||
import { gitStatusStore, type GitFile } from '$lib/stores/gitStatus.svelte';
|
||||
import Icon from './Icon.svelte';
|
||||
import SaveDialog from './SaveDialog.svelte';
|
||||
@@ -20,8 +21,7 @@
|
||||
import SvgPreview from './preview/SvgPreview.svelte';
|
||||
import OfficePreview from './preview/OfficePreview.svelte';
|
||||
import Spinner from './common/Spinner.svelte';
|
||||
import { groupDiffLines, languageForPath, withInlineDiffSegments } from '$lib/utils/diff';
|
||||
import SyntaxDiffLine from './SyntaxDiffLine.svelte';
|
||||
import DiffHunkRows from './DiffHunkRows.svelte';
|
||||
import {
|
||||
EditorState,
|
||||
StateEffect,
|
||||
@@ -526,31 +526,6 @@
|
||||
applyGitLineChanges();
|
||||
}
|
||||
|
||||
function diffBlockClass(type: DiffLine['type']): string {
|
||||
if (type === 'added')
|
||||
return 'bg-green-100 border-l-[0.1875rem] border-l-green-500 dark:bg-green-500/15 dark:border-l-green-400';
|
||||
if (type === 'removed') return 'bg-red-100 diff-gutter-removed dark:bg-red-500/15';
|
||||
return '';
|
||||
}
|
||||
|
||||
function diffTextClass(type: DiffLine['type']): string {
|
||||
if (type === 'added') return 'text-green-900 dark:text-green-300';
|
||||
if (type === 'removed') return 'text-red-900 dark:text-red-300';
|
||||
return 'text-gray-600 dark:text-gray-400';
|
||||
}
|
||||
|
||||
function diffPrefixClass(type: DiffLine['type']): string {
|
||||
if (type === 'added') return 'text-green-600 dark:text-green-400';
|
||||
if (type === 'removed') return 'text-red-500 dark:text-red-400';
|
||||
return 'text-gray-400 dark:text-gray-600';
|
||||
}
|
||||
|
||||
function diffPrefix(type: DiffLine['type']): string {
|
||||
if (type === 'added') return '+';
|
||||
if (type === 'removed') return '-';
|
||||
return ' ';
|
||||
}
|
||||
|
||||
// Editing state: true when the code editor should be active
|
||||
let isEditing = $derived(
|
||||
fileData &&
|
||||
@@ -811,7 +786,8 @@
|
||||
root: ws.path,
|
||||
file: relPath,
|
||||
staged: 'false',
|
||||
untracked: String(fileStatus?.status === 'untracked')
|
||||
untracked: String(fileStatus?.status === 'untracked'),
|
||||
ignore_whitespace: String($hideWhitespaceChanges)
|
||||
});
|
||||
const d = (await getGitDiff(params.toString())) as { files?: DiffFileEntry[] };
|
||||
diffFiles = d.files ?? [];
|
||||
@@ -1226,34 +1202,7 @@
|
||||
<span></span>
|
||||
<code class="whitespace-pre px-2 py-0.5">{hunk.header}</code>
|
||||
</div>
|
||||
{#each groupDiffLines(withInlineDiffSegments(diffNumberedLines(hunk))) as group}
|
||||
<div class="w-full {diffBlockClass(group.type)}">
|
||||
{#each group.lines as line}
|
||||
<div
|
||||
class="grid w-full grid-cols-[2.75rem_2.75rem_1.25rem_auto] font-mono text-[0.6875rem] leading-[1.125rem]"
|
||||
>
|
||||
<span
|
||||
class="select-none border-r border-black/5 px-2 text-right text-gray-400 dark:border-white/4 dark:text-gray-600"
|
||||
>{line.oldNumber ?? ''}</span
|
||||
>
|
||||
<span
|
||||
class="select-none border-r border-black/5 px-2 text-right text-gray-400 dark:border-white/4 dark:text-gray-600"
|
||||
>{line.newNumber ?? ''}</span
|
||||
>
|
||||
<span class="select-none px-1 text-center {diffPrefixClass(line.type)}"
|
||||
>{diffPrefix(line.type)}</span
|
||||
>
|
||||
<SyntaxDiffLine
|
||||
type={line.type}
|
||||
content={line.content || ' '}
|
||||
segments={line.segments}
|
||||
language={languageForPath(df.path)}
|
||||
class={diffTextClass(line.type)}
|
||||
/>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/each}
|
||||
<DiffHunkRows {hunk} path={df.path} />
|
||||
{/each}
|
||||
{/each}
|
||||
</div>
|
||||
@@ -1475,89 +1424,4 @@
|
||||
font-size: 0.75rem;
|
||||
line-height: 1.125rem;
|
||||
}
|
||||
|
||||
.diff-hunk-header {
|
||||
padding: 0.125rem 0.5rem;
|
||||
color: var(--color-gray-400);
|
||||
background: var(--color-gray-50);
|
||||
}
|
||||
|
||||
:global(.dark) .diff-hunk-header {
|
||||
background: rgba(255, 255, 255, 0.03);
|
||||
color: var(--color-gray-600);
|
||||
}
|
||||
|
||||
.diff-line {
|
||||
padding: 0 0.5rem;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.diff-prefix {
|
||||
display: inline-block;
|
||||
width: 1rem;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.diff-added {
|
||||
background: #dcfce7;
|
||||
color: #14532d;
|
||||
}
|
||||
|
||||
:global(.dark) .diff-added {
|
||||
background: rgba(34, 197, 94, 0.12);
|
||||
color: #86efac;
|
||||
}
|
||||
|
||||
.diff-added .diff-prefix {
|
||||
color: #16a34a;
|
||||
}
|
||||
:global(.dark) .diff-added .diff-prefix {
|
||||
color: #4ade80;
|
||||
}
|
||||
|
||||
.diff-removed {
|
||||
background: #fee2e2;
|
||||
color: #7f1d1d;
|
||||
}
|
||||
|
||||
:global(.dark) .diff-removed {
|
||||
background: rgba(239, 68, 68, 0.12);
|
||||
color: #fca5a5;
|
||||
}
|
||||
|
||||
.diff-removed .diff-prefix {
|
||||
color: #ef4444;
|
||||
}
|
||||
:global(.dark) .diff-removed .diff-prefix {
|
||||
color: #f87171;
|
||||
}
|
||||
|
||||
.diff-ctx {
|
||||
color: var(--color-gray-600);
|
||||
}
|
||||
|
||||
:global(.dark) .diff-ctx {
|
||||
color: var(--color-gray-400);
|
||||
}
|
||||
|
||||
.diff-ctx .diff-prefix {
|
||||
color: var(--color-gray-400);
|
||||
}
|
||||
|
||||
:global(.dark) .diff-ctx .diff-prefix {
|
||||
color: var(--color-gray-600);
|
||||
}
|
||||
|
||||
.diff-gutter-removed {
|
||||
border-left: 0.1875rem solid transparent;
|
||||
border-image: repeating-linear-gradient(
|
||||
-45deg,
|
||||
#ef4444 0,
|
||||
#ef4444 1px,
|
||||
transparent 1px,
|
||||
transparent 0.1875rem
|
||||
)
|
||||
3;
|
||||
}
|
||||
</style>
|
||||
@@ -26,21 +26,16 @@
|
||||
renameGitBranch
|
||||
} from '$lib/apis/git';
|
||||
import { gitStatusStore } from '$lib/stores/gitStatus.svelte';
|
||||
import { diffDisplayMode, hideWhitespaceChanges } from '$lib/stores/gitDiffSettings';
|
||||
import Icon from './Icon.svelte';
|
||||
import DropdownMenu from './DropdownMenu.svelte';
|
||||
import Modal from './Modal.svelte';
|
||||
import { tooltip } from '$lib/tooltip';
|
||||
import { t } from '$lib/i18n';
|
||||
import Spinner from '$lib/components/common/Spinner.svelte';
|
||||
import {
|
||||
groupDiffLines,
|
||||
languageForPath,
|
||||
numberDiffLines,
|
||||
withInlineDiffSegments,
|
||||
type DiffFile,
|
||||
type DiffLine
|
||||
} from '$lib/utils/diff';
|
||||
import SyntaxDiffLine from './SyntaxDiffLine.svelte';
|
||||
import DiffSettingsMenu from './DiffSettingsMenu.svelte';
|
||||
import type { DiffFile } from '$lib/utils/diff';
|
||||
import DiffHunkRows from './DiffHunkRows.svelte';
|
||||
|
||||
type GitFile = {
|
||||
path: string;
|
||||
@@ -84,8 +79,10 @@
|
||||
let showDiff = $state(false);
|
||||
let showBranches = $state(false);
|
||||
let showWorktrees = $state(false);
|
||||
let showDiffSettings = $state(false);
|
||||
let branchBtnEl = $state<HTMLButtonElement | undefined>();
|
||||
let worktreeBtnEl = $state<HTMLButtonElement | undefined>();
|
||||
let diffSettingsBtnEl = $state<HTMLButtonElement | undefined>();
|
||||
let branchSearchInputEl = $state<HTMLInputElement | undefined>();
|
||||
let worktreeSearchInputEl = $state<HTMLInputElement | undefined>();
|
||||
let newBranchInputEl = $state<HTMLInputElement | undefined>();
|
||||
@@ -187,6 +184,15 @@
|
||||
}
|
||||
});
|
||||
|
||||
let _prevHideWhitespace = false;
|
||||
$effect(() => {
|
||||
const hideWhitespace = $hideWhitespaceChanges;
|
||||
if (hideWhitespace === _prevHideWhitespace) return;
|
||||
_prevHideWhitespace = hideWhitespace;
|
||||
if (!expanded || !showDiff) return;
|
||||
reloadSelectedDiff();
|
||||
});
|
||||
|
||||
// Auto-select first file
|
||||
$effect(() => {
|
||||
if (
|
||||
@@ -213,7 +219,8 @@
|
||||
const params = new URLSearchParams({
|
||||
root: workspacePath,
|
||||
file: path,
|
||||
staged: String(staged)
|
||||
staged: String(staged),
|
||||
ignore_whitespace: String($hideWhitespaceChanges)
|
||||
});
|
||||
if (untracked) params.set('untracked', 'true');
|
||||
const d = await getGitDiff(params.toString());
|
||||
@@ -228,7 +235,7 @@
|
||||
selectedFile = null;
|
||||
showDiff = true;
|
||||
try {
|
||||
const d = await getGitShow(workspacePath, c.hash);
|
||||
const d = await getGitShow(workspacePath, c.hash, $hideWhitespaceChanges);
|
||||
fileDiff = d.diff?.files ?? [];
|
||||
} catch {
|
||||
fileDiff = [];
|
||||
@@ -239,6 +246,16 @@
|
||||
showDiff = false;
|
||||
}
|
||||
|
||||
function reloadSelectedDiff() {
|
||||
if (selectedCommit) {
|
||||
selectCommit(selectedCommit);
|
||||
return;
|
||||
}
|
||||
if (!selectedFile) return;
|
||||
const file = (gitStatus?.files ?? []).find((item) => item.path === selectedFile);
|
||||
if (file) selectFile(file.path, file.staged, file.status === 'untracked');
|
||||
}
|
||||
|
||||
async function loadHistory() {
|
||||
try {
|
||||
commits = await getGitLog(workspacePath, 50);
|
||||
@@ -717,35 +734,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
function blockClass(type: DiffLine['type']): string {
|
||||
switch (type) {
|
||||
case 'added':
|
||||
return 'bg-green-100 border-l-[0.1875rem] border-l-green-500 dark:bg-green-500/15 dark:border-l-green-400';
|
||||
case 'removed':
|
||||
return 'bg-red-100 diff-gutter-removed dark:bg-red-500/15';
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function textClass(type: DiffLine['type']): string {
|
||||
if (type === 'added') return 'text-green-900 dark:text-green-300';
|
||||
if (type === 'removed') return 'text-red-900 dark:text-red-300';
|
||||
return 'text-gray-600 dark:text-gray-400';
|
||||
}
|
||||
|
||||
function prefixClass(type: DiffLine['type']): string {
|
||||
if (type === 'added') return 'text-green-600 dark:text-green-400';
|
||||
if (type === 'removed') return 'text-red-500 dark:text-red-400';
|
||||
return 'text-gray-400 dark:text-gray-600';
|
||||
}
|
||||
|
||||
function linePrefix(type: DiffLine['type']): string {
|
||||
if (type === 'added') return '+';
|
||||
if (type === 'removed') return '-';
|
||||
return ' ';
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (!expanded) return;
|
||||
function onKeydown(e: KeyboardEvent) {
|
||||
@@ -1184,6 +1172,16 @@
|
||||
</a>
|
||||
{/if}
|
||||
|
||||
<button
|
||||
bind:this={diffSettingsBtnEl}
|
||||
class="flex items-center justify-center w-6 h-6 rounded-md text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 hover:bg-gray-100 dark:hover:bg-white/6 transition-colors duration-75"
|
||||
onclick={() => (showDiffSettings = true)}
|
||||
use:tooltip={'Diff settings'}
|
||||
aria-label="Diff settings"
|
||||
>
|
||||
<Icon name="settings" size={12} />
|
||||
</button>
|
||||
|
||||
<!-- Maximize toggle -->
|
||||
<button
|
||||
class="flex items-center justify-center w-6 h-6 rounded-md text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 hover:bg-gray-100 dark:hover:bg-white/6 transition-colors duration-75"
|
||||
@@ -1194,6 +1192,14 @@
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{#if showDiffSettings && diffSettingsBtnEl}
|
||||
<DiffSettingsMenu
|
||||
anchor={diffSettingsBtnEl}
|
||||
onclose={() => (showDiffSettings = false)}
|
||||
preferAbove
|
||||
/>
|
||||
{/if}
|
||||
|
||||
<!-- Content -->
|
||||
<div class="flex min-h-36" style="height: {panelHeight}px;">
|
||||
<!-- Left pane: file list or commit list -->
|
||||
@@ -1407,7 +1413,7 @@
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex-1 overflow-auto">
|
||||
<div class="diff-content">
|
||||
<div class="diff-content" class:diff-content-split={$diffDisplayMode === 'split'}>
|
||||
{#each fileDiff as df}
|
||||
{#if fileDiff.length > 1}
|
||||
<div
|
||||
@@ -1422,24 +1428,7 @@
|
||||
>
|
||||
{hunk.header}
|
||||
</div>
|
||||
{#each groupDiffLines(withInlineDiffSegments(numberDiffLines(hunk))) as group}
|
||||
<div class="w-full {blockClass(group.type)}">
|
||||
{#each group.lines as line}
|
||||
<div class="flex min-w-max">
|
||||
<span class="select-none px-2 text-center {prefixClass(line.type)}"
|
||||
>{linePrefix(line.type)}</span
|
||||
>
|
||||
<SyntaxDiffLine
|
||||
type={line.type}
|
||||
content={line.content || ' '}
|
||||
segments={line.segments}
|
||||
language={languageForPath(df.path)}
|
||||
class={textClass(line.type)}
|
||||
/>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/each}
|
||||
<DiffHunkRows {hunk} path={df.path} showNumbers={false} />
|
||||
{/each}
|
||||
{/each}
|
||||
</div>
|
||||
@@ -1634,16 +1623,8 @@
|
||||
min-width: 100%;
|
||||
}
|
||||
|
||||
.diff-gutter-removed {
|
||||
border-left: 0.1875rem solid transparent;
|
||||
border-image: repeating-linear-gradient(
|
||||
-45deg,
|
||||
#ef4444 0,
|
||||
#ef4444 1px,
|
||||
transparent 1px,
|
||||
transparent 0.1875rem
|
||||
)
|
||||
3;
|
||||
.diff-content-split {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.git-resize-handle {
|
||||
|
||||
@@ -1,21 +1,16 @@
|
||||
<script lang="ts">
|
||||
import { activeWorkspace, openFileTab, gitReviewOpen } from '$lib/stores';
|
||||
import { getGitDiff } from '$lib/apis/git';
|
||||
import { diffDisplayMode, hideWhitespaceChanges } from '$lib/stores/gitDiffSettings';
|
||||
import { gitStatusStore, type GitStatus, type GitFile } from '$lib/stores/gitStatus.svelte';
|
||||
|
||||
import { tooltip } from '$lib/tooltip';
|
||||
import { t } from '$lib/i18n';
|
||||
import {
|
||||
groupDiffLines,
|
||||
languageForPath,
|
||||
numberDiffLines,
|
||||
withInlineDiffSegments,
|
||||
type DiffFile,
|
||||
type DiffLine
|
||||
} from '$lib/utils/diff';
|
||||
import type { DiffFile } from '$lib/utils/diff';
|
||||
import Icon from './Icon.svelte';
|
||||
import Spinner from '$lib/components/common/Spinner.svelte';
|
||||
import SyntaxDiffLine from './SyntaxDiffLine.svelte';
|
||||
import DiffSettingsMenu from './DiffSettingsMenu.svelte';
|
||||
import DiffHunkRows from './DiffHunkRows.svelte';
|
||||
|
||||
type ReviewFile = {
|
||||
key: string;
|
||||
@@ -33,6 +28,8 @@
|
||||
let loading = $state(false);
|
||||
let refreshing = $state(false);
|
||||
let error = $state('');
|
||||
let showDiffSettings = $state(false);
|
||||
let diffSettingsButtonEl = $state<HTMLButtonElement | undefined>();
|
||||
let loadSeq = 0;
|
||||
|
||||
const workspacePath = $derived($activeWorkspace?.path ?? '');
|
||||
@@ -41,16 +38,19 @@
|
||||
|
||||
// React to git status changes from centralized store
|
||||
let _prevStatusRef: GitStatus | null = null;
|
||||
let _prevHideWhitespace = false;
|
||||
$effect(() => {
|
||||
const status = gitStatus;
|
||||
const hideWhitespace = $hideWhitespaceChanges;
|
||||
if (!workspacePath || !status) {
|
||||
reviewFiles = [];
|
||||
return;
|
||||
}
|
||||
// Only re-fetch diffs when status actually changes
|
||||
if (status !== _prevStatusRef) {
|
||||
if (status !== _prevStatusRef || hideWhitespace !== _prevHideWhitespace) {
|
||||
const isInitial = _prevStatusRef === null;
|
||||
_prevStatusRef = status;
|
||||
_prevHideWhitespace = hideWhitespace;
|
||||
fetchDiffs(status, isInitial);
|
||||
}
|
||||
});
|
||||
@@ -76,7 +76,8 @@
|
||||
const params = new URLSearchParams({
|
||||
root,
|
||||
file: file.path,
|
||||
staged: String(file.staged)
|
||||
staged: String(file.staged),
|
||||
ignore_whitespace: String($hideWhitespaceChanges)
|
||||
});
|
||||
if (file.status === 'untracked') params.set('untracked', 'true');
|
||||
|
||||
@@ -182,45 +183,6 @@
|
||||
return { char: '?', label: status, className: 'text-gray-400 dark:text-gray-500' };
|
||||
}
|
||||
}
|
||||
|
||||
function blockClass(type: DiffLine['type']): string {
|
||||
switch (type) {
|
||||
case 'added':
|
||||
return 'bg-green-100 border-l-[0.1875rem] border-l-green-500 dark:bg-green-500/15 dark:border-l-green-400';
|
||||
case 'removed':
|
||||
return 'bg-red-100 diff-gutter-removed dark:bg-red-500/15';
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function textClass(type: DiffLine['type']): string {
|
||||
switch (type) {
|
||||
case 'added':
|
||||
return 'text-green-900 dark:text-green-300';
|
||||
case 'removed':
|
||||
return 'text-red-900 dark:text-red-300';
|
||||
default:
|
||||
return 'text-gray-600 dark:text-gray-400';
|
||||
}
|
||||
}
|
||||
|
||||
function prefixClass(type: DiffLine['type']): string {
|
||||
switch (type) {
|
||||
case 'added':
|
||||
return 'text-green-600 dark:text-green-400';
|
||||
case 'removed':
|
||||
return 'text-red-500 dark:text-red-400';
|
||||
default:
|
||||
return 'text-gray-400 dark:text-gray-600';
|
||||
}
|
||||
}
|
||||
|
||||
function linePrefix(type: DiffLine['type']): string {
|
||||
if (type === 'added') return '+';
|
||||
if (type === 'removed') return '-';
|
||||
return ' ';
|
||||
}
|
||||
</script>
|
||||
|
||||
<div
|
||||
@@ -275,6 +237,16 @@
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<button
|
||||
bind:this={diffSettingsButtonEl}
|
||||
class="flex h-6 w-6 items-center justify-center rounded-md text-gray-400 transition-colors hover:bg-gray-100 hover:text-gray-600 dark:hover:bg-white/6 dark:hover:text-gray-300"
|
||||
onclick={() => (showDiffSettings = true)}
|
||||
aria-label="Diff settings"
|
||||
use:tooltip={'Diff settings'}
|
||||
>
|
||||
<Icon name="settings" size={13} />
|
||||
</button>
|
||||
|
||||
<button
|
||||
class="flex h-6 w-6 items-center justify-center rounded-md text-gray-400 transition-colors hover:bg-gray-100 hover:text-gray-600 dark:hover:bg-white/6 dark:hover:text-gray-300"
|
||||
onclick={() => refreshReview(false)}
|
||||
@@ -286,6 +258,10 @@
|
||||
</button>
|
||||
</header>
|
||||
|
||||
{#if showDiffSettings && diffSettingsButtonEl}
|
||||
<DiffSettingsMenu anchor={diffSettingsButtonEl} onclose={() => (showDiffSettings = false)} />
|
||||
{/if}
|
||||
|
||||
{#if totalChanges > 0}
|
||||
<div
|
||||
class="flex h-8 shrink-0 items-center gap-2 border-b border-gray-200 px-3 dark:border-white/6"
|
||||
@@ -386,7 +362,7 @@
|
||||
<div
|
||||
class="mb-1 overflow-x-auto border-y border-gray-100 bg-white font-mono text-[0.6875rem] leading-[1.125rem] dark:border-white/4 dark:bg-black"
|
||||
>
|
||||
<div class="diff-content">
|
||||
<div class="diff-content" class:diff-content-split={$diffDisplayMode === 'split'}>
|
||||
{#if file.diffFiles.some((diffFile) => diffFile.hunks.length > 0)}
|
||||
{#each file.diffFiles as diffFile}
|
||||
{#if file.diffFiles.length > 1}
|
||||
@@ -405,33 +381,7 @@
|
||||
<span></span>
|
||||
<code class="whitespace-pre px-2 py-0.5">{hunk.header}</code>
|
||||
</div>
|
||||
{#each groupDiffLines(withInlineDiffSegments(numberDiffLines(hunk))) as group}
|
||||
<div class="w-full {blockClass(group.type)}">
|
||||
{#each group.lines as line}
|
||||
<div class="grid w-full grid-cols-[2.75rem_2.75rem_1.25rem_auto]">
|
||||
<span
|
||||
class="select-none border-r border-black/5 px-2 text-right text-gray-400 dark:border-white/4 dark:text-gray-600"
|
||||
>{line.oldNumber ?? ''}</span
|
||||
>
|
||||
<span
|
||||
class="select-none border-r border-black/5 px-2 text-right text-gray-400 dark:border-white/4 dark:text-gray-600"
|
||||
>{line.newNumber ?? ''}</span
|
||||
>
|
||||
<span
|
||||
class="select-none px-1 text-center {prefixClass(line.type)}"
|
||||
>{linePrefix(line.type)}</span
|
||||
>
|
||||
<SyntaxDiffLine
|
||||
type={line.type}
|
||||
content={line.content || ' '}
|
||||
segments={line.segments}
|
||||
language={languageForPath(diffFile.path)}
|
||||
class={textClass(line.type)}
|
||||
/>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/each}
|
||||
<DiffHunkRows {hunk} path={diffFile.path} />
|
||||
{/each}
|
||||
{/each}
|
||||
{:else}
|
||||
@@ -458,15 +408,7 @@
|
||||
min-width: 100%;
|
||||
}
|
||||
|
||||
.diff-gutter-removed {
|
||||
border-left: 0.1875rem solid transparent;
|
||||
border-image: repeating-linear-gradient(
|
||||
-45deg,
|
||||
#ef4444 0,
|
||||
#ef4444 1px,
|
||||
transparent 1px,
|
||||
transparent 0.1875rem
|
||||
)
|
||||
3;
|
||||
.diff-content-split {
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
@@ -143,6 +143,11 @@
|
||||
<path d="M4 18.01L4.01 17.9989" />
|
||||
<path d="M8 12H20" />
|
||||
<path d="M8 18H20" />
|
||||
{:else if name === 'split-view'}
|
||||
<path
|
||||
d="M4 5.6C4 5.26863 4.26863 5 4.6 5H19.4C19.7314 5 20 5.26863 20 5.6V18.4C20 18.7314 19.7314 19 19.4 19H4.6C4.26863 19 4 18.7314 4 18.4V5.6Z"
|
||||
/>
|
||||
<path d="M12 5V19" />
|
||||
{:else if name === 'chevron-right'}
|
||||
<path d="M9 6L15 12L9 18" />
|
||||
{:else if name === 'arrow-left'}
|
||||
|
||||
@@ -68,6 +68,7 @@
|
||||
content: string;
|
||||
segments: InlineDiffSegment[];
|
||||
language: string;
|
||||
wrap?: boolean;
|
||||
class?: string;
|
||||
}
|
||||
|
||||
@@ -76,7 +77,7 @@
|
||||
dark?: string;
|
||||
};
|
||||
|
||||
let { type, content, segments, language, class: className = '' }: Props = $props();
|
||||
let { type, content, segments, language, wrap = false, class: className = '' }: Props = $props();
|
||||
let renderedSegments = $state<RenderSegment[] | null>(null);
|
||||
|
||||
$effect(() => {
|
||||
@@ -187,7 +188,11 @@
|
||||
const html = $derived(htmlForSegments(type, fallbackSegments));
|
||||
</script>
|
||||
|
||||
<code class="syntax-diff-line whitespace-pre px-2 {className}">{@html html}</code>
|
||||
<code
|
||||
class="syntax-diff-line block px-2 {wrap
|
||||
? 'whitespace-pre-wrap break-words'
|
||||
: 'whitespace-pre'} {className}">{@html html}</code
|
||||
>
|
||||
|
||||
<style>
|
||||
.syntax-diff-line :global(span) {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
const HIDE_WHITESPACE_KEY = 'gitDiff:hideWhitespace';
|
||||
const DISPLAY_MODE_KEY = 'gitDiff:displayMode';
|
||||
export type DiffDisplayMode = 'unified' | 'split';
|
||||
|
||||
function persistedBoolean(key: string, fallback: boolean) {
|
||||
const initial =
|
||||
typeof localStorage !== 'undefined' ? localStorage.getItem(key) === 'true' : fallback;
|
||||
const store = writable(initial);
|
||||
store.subscribe((value) => {
|
||||
if (typeof localStorage !== 'undefined') localStorage.setItem(key, String(value));
|
||||
});
|
||||
return store;
|
||||
}
|
||||
|
||||
export const hideWhitespaceChanges = persistedBoolean(HIDE_WHITESPACE_KEY, false);
|
||||
|
||||
const initialDisplayMode =
|
||||
typeof localStorage !== 'undefined'
|
||||
? localStorage.getItem(DISPLAY_MODE_KEY) === 'split'
|
||||
? 'split'
|
||||
: 'unified'
|
||||
: 'unified';
|
||||
|
||||
export const diffDisplayMode = writable<DiffDisplayMode>(initialDisplayMode);
|
||||
diffDisplayMode.subscribe((value) => {
|
||||
if (typeof localStorage !== 'undefined') localStorage.setItem(DISPLAY_MODE_KEY, value);
|
||||
});
|
||||
@@ -6,6 +6,7 @@ export type NumberedDiffLine = DiffLine & { oldNumber: number | null; newNumber:
|
||||
export type InlineDiffSegment = { text: string; changed: boolean };
|
||||
export type InlineDiffLine = NumberedDiffLine & { segments: InlineDiffSegment[] };
|
||||
export type DiffLineGroup<T extends { type: string }> = { type: T['type']; lines: T[] };
|
||||
export type SplitDiffRow = { oldLine: InlineDiffLine | null; newLine: InlineDiffLine | null };
|
||||
|
||||
export function languageForPath(path: string): string {
|
||||
const ext = path.slice(path.lastIndexOf('.')).toLowerCase();
|
||||
@@ -116,6 +117,35 @@ export function withInlineDiffSegments(lines: NumberedDiffLine[]): InlineDiffLin
|
||||
return result;
|
||||
}
|
||||
|
||||
export function splitDiffRows(lines: InlineDiffLine[]): SplitDiffRow[] {
|
||||
const rows: SplitDiffRow[] = [];
|
||||
let i = 0;
|
||||
|
||||
while (i < lines.length) {
|
||||
const line = lines[i];
|
||||
if (line.type === 'context') {
|
||||
rows.push({ oldLine: line, newLine: line });
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
const block: InlineDiffLine[] = [];
|
||||
while (i < lines.length && lines[i].type !== 'context') {
|
||||
block.push(lines[i]);
|
||||
i += 1;
|
||||
}
|
||||
|
||||
const removed = block.filter((item) => item.type === 'removed');
|
||||
const added = block.filter((item) => item.type === 'added');
|
||||
const count = Math.max(removed.length, added.length);
|
||||
for (let index = 0; index < count; index += 1) {
|
||||
rows.push({ oldLine: removed[index] ?? null, newLine: added[index] ?? null });
|
||||
}
|
||||
}
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
function segmentsForPair(
|
||||
oldText: string | undefined,
|
||||
newText: string | undefined,
|
||||
|
||||
+8
-4
@@ -69,12 +69,16 @@ async def git_status(root: str):
|
||||
|
||||
@router.get("/diff")
|
||||
async def git_diff(
|
||||
root: str, file: Optional[str] = None, staged: bool = False, untracked: bool = False
|
||||
root: str,
|
||||
file: Optional[str] = None,
|
||||
staged: bool = False,
|
||||
untracked: bool = False,
|
||||
ignore_whitespace: bool = False,
|
||||
):
|
||||
"""Get diff for working tree or staged changes."""
|
||||
await _require_repo(root)
|
||||
try:
|
||||
return await diff(root, file, staged, untracked)
|
||||
return await diff(root, file, staged, untracked, ignore_whitespace)
|
||||
except GitError as e:
|
||||
_handle_git_error(e)
|
||||
|
||||
@@ -90,11 +94,11 @@ async def git_log(root: str, limit: int = 50, offset: int = 0):
|
||||
|
||||
|
||||
@router.get("/show")
|
||||
async def git_show(root: str, ref: str):
|
||||
async def git_show(root: str, ref: str, ignore_whitespace: bool = False):
|
||||
"""Show a single commit with its diff."""
|
||||
await _require_repo(root)
|
||||
try:
|
||||
return await show(root, ref)
|
||||
return await show(root, ref, ignore_whitespace)
|
||||
except GitError as e:
|
||||
_handle_git_error(e)
|
||||
|
||||
|
||||
+13
-18
@@ -168,24 +168,22 @@ async def diff(
|
||||
file: str | None = None,
|
||||
staged: bool = False,
|
||||
untracked: bool = False,
|
||||
ignore_whitespace: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""Get diff output as structured data."""
|
||||
if untracked and file:
|
||||
# Untracked files: use --no-index to diff against empty
|
||||
null_device = "NUL" if sys.platform == "win32" else "/dev/null"
|
||||
_, out, _ = await _run(
|
||||
"diff",
|
||||
"--no-index",
|
||||
"--unified=3",
|
||||
"--",
|
||||
null_device,
|
||||
file,
|
||||
cwd=root,
|
||||
check=False,
|
||||
)
|
||||
args = ["diff", "--no-index", "--unified=3"]
|
||||
if ignore_whitespace:
|
||||
args.append("--ignore-all-space")
|
||||
args.extend(["--", null_device, file])
|
||||
_, out, _ = await _run(*args, cwd=root, check=False)
|
||||
return _parse_diff(out)
|
||||
|
||||
args = ["diff", "--unified=3"]
|
||||
if ignore_whitespace:
|
||||
args.append("--ignore-all-space")
|
||||
if staged:
|
||||
args.append("--staged")
|
||||
if file:
|
||||
@@ -365,16 +363,13 @@ async def log(
|
||||
return commits
|
||||
|
||||
|
||||
async def show(root: str, ref: str) -> dict[str, Any]:
|
||||
async def show(root: str, ref: str, ignore_whitespace: bool = False) -> dict[str, Any]:
|
||||
"""Show a commit's diff."""
|
||||
fmt = "%H%x00%h%x00%an%x00%aI%x00%s"
|
||||
_, out, _ = await _run(
|
||||
"show",
|
||||
ref,
|
||||
f"--format={fmt}",
|
||||
"--patch",
|
||||
cwd=root,
|
||||
)
|
||||
args = ["show", ref, f"--format={fmt}", "--patch"]
|
||||
if ignore_whitespace:
|
||||
args.append("--ignore-all-space")
|
||||
_, out, _ = await _run(*args, cwd=root)
|
||||
|
||||
# First line is the formatted header, rest is diff
|
||||
lines = out.split("\n", 1)
|
||||
|
||||
Reference in new issue
Block a user