@deepseek-ai/dsh-experimental-client-ui-forge 0.2.2-alpha.3 → 0.2.2-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.i18n.yaml +8 -8
- package/README.md +41 -14
- package/README.zh.md +40 -13
- package/lib/client.js +13 -11800
- package/lib/client.js.map +1 -0
- package/lib/index.js +1340 -115
- package/lib/typert.host.js +1715 -212
- package/lib/typert.remote-client.d.ts +42 -5
- package/lib/typert.remote-client.d.ts.map +1 -0
- package/lib/typert.remote-client.js +1523 -244
- package/lib/types/client/ActionToast.d.ts.map +1 -0
- package/lib/types/client/ActionToast.js.map +1 -0
- package/lib/types/client/BaseMenu.d.ts +30 -0
- package/lib/types/client/BaseMenu.d.ts.map +1 -0
- package/lib/types/client/BaseMenu.js.map +1 -0
- package/lib/types/client/ChangeRequestCard.d.ts +9 -5
- package/lib/types/client/ChangeRequestCard.d.ts.map +1 -0
- package/lib/types/client/ChangeRequestCard.js.map +1 -0
- package/lib/types/client/ChangeRequestOverview.d.ts +124 -0
- package/lib/types/client/ChangeRequestOverview.d.ts.map +1 -0
- package/lib/types/client/ChangeRequestOverview.js.map +1 -0
- package/lib/types/client/ChangedFiles.d.ts +2 -0
- package/lib/types/client/ChangedFiles.d.ts.map +1 -0
- package/lib/types/client/ChangedFiles.js.map +1 -0
- package/lib/types/client/ChangesTab.d.ts +30 -5
- package/lib/types/client/ChangesTab.d.ts.map +1 -0
- package/lib/types/client/ChangesTab.js.map +1 -0
- package/lib/types/client/ChangesTabTitle.d.ts.map +1 -0
- package/lib/types/client/ChangesTabTitle.js.map +1 -0
- package/lib/types/client/CommitMenu.d.ts +41 -0
- package/lib/types/client/CommitMenu.d.ts.map +1 -0
- package/lib/types/client/CommitMenu.js.map +1 -0
- package/lib/types/client/DiffFile.d.ts +47 -15
- package/lib/types/client/DiffFile.d.ts.map +1 -0
- package/lib/types/client/DiffFile.js.map +1 -0
- package/lib/types/client/FileLabel.d.ts.map +1 -0
- package/lib/types/client/FileLabel.js.map +1 -0
- package/lib/types/client/FileTreePane.d.ts +4 -2
- package/lib/types/client/FileTreePane.d.ts.map +1 -0
- package/lib/types/client/FileTreePane.js.map +1 -0
- package/lib/types/client/ForgeTab.d.ts +9 -1
- package/lib/types/client/ForgeTab.d.ts.map +1 -0
- package/lib/types/client/ForgeTab.js.map +1 -0
- package/lib/types/client/ForgeTabTitle.d.ts.map +1 -0
- package/lib/types/client/ForgeTabTitle.js.map +1 -0
- package/lib/types/client/HeaderButton.d.ts.map +1 -0
- package/lib/types/client/HeaderButton.js.map +1 -0
- package/lib/types/client/LocalChangesCard.d.ts.map +1 -0
- package/lib/types/client/LocalChangesCard.js.map +1 -0
- package/lib/types/client/RefreshGlyph.d.ts.map +1 -0
- package/lib/types/client/RefreshGlyph.js.map +1 -0
- package/lib/types/client/ReviewComposer.d.ts +20 -6
- package/lib/types/client/ReviewComposer.d.ts.map +1 -0
- package/lib/types/client/ReviewComposer.js.map +1 -0
- package/lib/types/client/ReviewControls.d.ts +79 -0
- package/lib/types/client/ReviewControls.d.ts.map +1 -0
- package/lib/types/client/ReviewControls.js.map +1 -0
- package/lib/types/client/ReviewThread.d.ts +4 -4
- package/lib/types/client/ReviewThread.d.ts.map +1 -0
- package/lib/types/client/ReviewThread.js.map +1 -0
- package/lib/types/client/ReviewThreads.d.ts +108 -0
- package/lib/types/client/ReviewThreads.d.ts.map +1 -0
- package/lib/types/client/ReviewThreads.js.map +1 -0
- package/lib/types/client/SessionHoverForge.d.ts.map +1 -0
- package/lib/types/client/SessionHoverForge.js.map +1 -0
- package/lib/types/client/SessionRowForge.d.ts +1 -1
- package/lib/types/client/SessionRowForge.d.ts.map +1 -0
- package/lib/types/client/SessionRowForge.js.map +1 -0
- package/lib/types/client/SubmitReview.d.ts +12 -4
- package/lib/types/client/SubmitReview.d.ts.map +1 -0
- package/lib/types/client/SubmitReview.js.map +1 -0
- package/lib/types/client/ThreadList.d.ts +43 -0
- package/lib/types/client/ThreadList.d.ts.map +1 -0
- package/lib/types/client/ThreadList.js.map +1 -0
- package/lib/types/client/ViewOptionsMenu.d.ts +31 -0
- package/lib/types/client/ViewOptionsMenu.d.ts.map +1 -0
- package/lib/types/client/ViewOptionsMenu.js.map +1 -0
- package/lib/types/client/WorktreeProgress.d.ts.map +1 -0
- package/lib/types/client/WorktreeProgress.js.map +1 -0
- package/lib/types/client/WorktreeSettings.d.ts +12 -0
- package/lib/types/client/WorktreeSettings.d.ts.map +1 -0
- package/lib/types/client/WorktreeSettings.js.map +1 -0
- package/lib/types/client/WorktreeToggle.d.ts.map +1 -0
- package/lib/types/client/WorktreeToggle.js.map +1 -0
- package/lib/types/client/changes-address.d.ts.map +1 -0
- package/lib/types/client/changes-address.js.map +1 -0
- package/lib/types/client/changes-store.d.ts +20 -5
- package/lib/types/client/changes-store.d.ts.map +1 -0
- package/lib/types/client/changes-store.js.map +1 -0
- package/lib/types/client/checkout.d.ts.map +1 -0
- package/lib/types/client/checkout.js.map +1 -0
- package/lib/types/client/code-selection.d.ts.map +1 -0
- package/lib/types/client/code-selection.js.map +1 -0
- package/lib/types/client/diff-rows.d.ts.map +1 -0
- package/lib/types/client/diff-rows.js.map +1 -0
- package/lib/types/client/file-tree.d.ts.map +1 -0
- package/lib/types/client/file-tree.js.map +1 -0
- package/lib/types/client/format.d.ts.map +1 -0
- package/lib/types/client/format.js.map +1 -0
- package/lib/types/client/glyphs.d.ts +4 -2
- package/lib/types/client/glyphs.d.ts.map +1 -0
- package/lib/types/client/glyphs.js.map +1 -0
- package/lib/types/client/host-viewed.d.ts +84 -0
- package/lib/types/client/host-viewed.d.ts.map +1 -0
- package/lib/types/client/host-viewed.js.map +1 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/index.js.map +1 -0
- package/lib/types/client/locales.d.ts +175 -3
- package/lib/types/client/locales.d.ts.map +1 -0
- package/lib/types/client/locales.js.map +1 -0
- package/lib/types/client/menu-search.d.ts +10 -0
- package/lib/types/client/menu-search.d.ts.map +1 -0
- package/lib/types/client/menu-search.js.map +1 -0
- package/lib/types/client/mount.d.ts +4 -0
- package/lib/types/client/mount.d.ts.map +1 -0
- package/lib/types/client/mount.js.map +1 -0
- package/lib/types/client/review-queue.d.ts +5 -8
- package/lib/types/client/review-queue.d.ts.map +1 -0
- package/lib/types/client/review-queue.js.map +1 -0
- package/lib/types/client/review-threads.d.ts +108 -0
- package/lib/types/client/review-threads.d.ts.map +1 -0
- package/lib/types/client/review-threads.js.map +1 -0
- package/lib/types/client/review.d.ts +31 -4
- package/lib/types/client/review.d.ts.map +1 -0
- package/lib/types/client/review.js.map +1 -0
- package/lib/types/client/section-reach.d.ts +26 -0
- package/lib/types/client/section-reach.d.ts.map +1 -0
- package/lib/types/client/section-reach.js.map +1 -0
- package/lib/types/client/session-list.d.ts.map +1 -0
- package/lib/types/client/session-list.js.map +1 -0
- package/lib/types/client/source.d.ts.map +1 -0
- package/lib/types/client/source.js.map +1 -0
- package/lib/types/client/status.d.ts.map +1 -0
- package/lib/types/client/status.js.map +1 -0
- package/lib/types/client/thread-state.d.ts +68 -0
- package/lib/types/client/thread-state.d.ts.map +1 -0
- package/lib/types/client/thread-state.js.map +1 -0
- package/lib/types/client/toolbar-fold.d.ts +19 -0
- package/lib/types/client/toolbar-fold.d.ts.map +1 -0
- package/lib/types/client/toolbar-fold.js.map +1 -0
- package/lib/types/client/viewed-files.d.ts +70 -0
- package/lib/types/client/viewed-files.d.ts.map +1 -0
- package/lib/types/client/viewed-files.js.map +1 -0
- package/lib/types/client/worktree-settings-form.d.ts +32 -0
- package/lib/types/client/worktree-settings-form.d.ts.map +1 -0
- package/lib/types/client/worktree-settings-form.js.map +1 -0
- package/lib/types/commit-range.d.ts +17 -0
- package/lib/types/commit-range.d.ts.map +1 -0
- package/lib/types/commit-range.js.map +1 -0
- package/lib/types/diff.d.ts +10 -2
- package/lib/types/diff.d.ts.map +1 -0
- package/lib/types/diff.js.map +1 -0
- package/lib/types/git.d.ts.map +1 -0
- package/lib/types/git.js.map +1 -0
- package/lib/types/index.d.ts +194 -14
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/layers.d.ts +66 -0
- package/lib/types/layers.d.ts.map +1 -0
- package/lib/types/layers.js.map +1 -0
- package/lib/types/local-diff.d.ts +54 -6
- package/lib/types/local-diff.d.ts.map +1 -0
- package/lib/types/local-diff.js.map +1 -0
- package/lib/types/panel.d.ts.map +1 -0
- package/lib/types/panel.js.map +1 -0
- package/lib/types/parse.d.ts +16 -0
- package/lib/types/parse.d.ts.map +1 -0
- package/lib/types/parse.js.map +1 -0
- package/lib/types/patch-cache.d.ts +12 -7
- package/lib/types/patch-cache.d.ts.map +1 -0
- package/lib/types/patch-cache.js.map +1 -0
- package/lib/types/review-cache.d.ts +54 -0
- package/lib/types/review-cache.d.ts.map +1 -0
- package/lib/types/review-cache.js.map +1 -0
- package/lib/types/states.d.ts.map +1 -0
- package/lib/types/states.js.map +1 -0
- package/lib/types/types.d.ts +113 -11
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js.map +1 -0
- package/lib/types/wildmatch.d.ts +14 -0
- package/lib/types/wildmatch.d.ts.map +1 -0
- package/lib/types/wildmatch.js.map +1 -0
- package/lib/types/worktree.d.ts +8 -6
- package/lib/types/worktree.d.ts.map +1 -0
- package/lib/types/worktree.js.map +1 -0
- package/package.json +59 -49
- package/src/client/ActionToast.tsx +38 -0
- package/src/client/BaseMenu.module.css +153 -0
- package/src/client/BaseMenu.tsx +180 -0
- package/src/client/ChangeRequestCard.tsx +439 -0
- package/src/client/ChangeRequestOverview.module.css +196 -0
- package/src/client/ChangeRequestOverview.tsx +620 -0
- package/src/client/ChangedFiles.module.css +26 -0
- package/src/client/ChangedFiles.tsx +144 -0
- package/src/client/ChangesTab.module.css +984 -0
- package/src/client/ChangesTab.tsx +1288 -0
- package/src/client/ChangesTabTitle.tsx +28 -0
- package/src/client/CommitMenu.module.css +139 -0
- package/src/client/CommitMenu.tsx +186 -0
- package/src/client/DiffFile.tsx +621 -0
- package/src/client/FileLabel.module.css +53 -0
- package/src/client/FileLabel.tsx +66 -0
- package/src/client/FileTreePane.module.css +10 -0
- package/src/client/FileTreePane.tsx +79 -0
- package/src/client/ForgeTab.module.css +627 -0
- package/src/client/ForgeTab.tsx +263 -0
- package/src/client/ForgeTabTitle.tsx +21 -0
- package/src/client/HeaderButton.module.css +29 -0
- package/src/client/HeaderButton.tsx +68 -0
- package/src/client/LocalChangesCard.tsx +83 -0
- package/src/client/RefreshGlyph.module.css +22 -0
- package/src/client/RefreshGlyph.tsx +35 -0
- package/src/client/ReviewComposer.module.css +37 -0
- package/src/client/ReviewComposer.tsx +128 -0
- package/src/client/ReviewControls.module.css +162 -0
- package/src/client/ReviewControls.tsx +191 -0
- package/src/client/ReviewThread.tsx +96 -0
- package/src/client/ReviewThreads.module.css +326 -0
- package/src/client/ReviewThreads.tsx +443 -0
- package/src/client/SessionHoverForge.tsx +72 -0
- package/src/client/SessionList.module.css +85 -0
- package/src/client/SessionRowForge.tsx +86 -0
- package/src/client/SubmitReview.module.css +26 -0
- package/src/client/SubmitReview.tsx +95 -0
- package/src/client/ThreadList.module.css +218 -0
- package/src/client/ThreadList.tsx +165 -0
- package/src/client/ViewOptionsMenu.tsx +79 -0
- package/src/client/WorktreeProgress.module.css +226 -0
- package/src/client/WorktreeProgress.tsx +280 -0
- package/src/client/WorktreeSettings.module.css +29 -0
- package/src/client/WorktreeSettings.tsx +42 -0
- package/src/client/WorktreeToggle.module.css +132 -0
- package/src/client/WorktreeToggle.tsx +165 -0
- package/src/client/changes-address.ts +110 -0
- package/src/client/changes-store.ts +155 -0
- package/src/client/checkout.ts +75 -0
- package/src/client/code-selection.ts +53 -0
- package/src/client/diff-rows.ts +199 -0
- package/src/client/file-tree.ts +86 -0
- package/src/client/format.ts +37 -0
- package/src/client/glyphs.tsx +151 -0
- package/src/client/host-viewed.ts +195 -0
- package/src/client/index.ts +31 -0
- package/src/client/locales.ts +3446 -0
- package/src/client/menu-search.ts +16 -0
- package/src/client/mount.ts +439 -0
- package/src/client/review-queue.ts +256 -0
- package/src/client/review-threads.ts +204 -0
- package/src/client/review.ts +120 -0
- package/src/client/section-reach.ts +61 -0
- package/src/client/session-list.ts +79 -0
- package/src/client/source.ts +274 -0
- package/src/client/status.ts +171 -0
- package/src/client/thread-state.ts +127 -0
- package/src/client/toolbar-fold.ts +70 -0
- package/src/client/viewed-files.ts +178 -0
- package/src/client/worktree-settings-form.ts +43 -0
- package/src/commit-range.ts +27 -0
- package/src/css-modules.d.ts +5 -0
- package/src/diff.ts +162 -0
- package/src/git.ts +293 -0
- package/src/index.ts +1162 -0
- package/src/layers.ts +163 -0
- package/src/local-diff.ts +438 -0
- package/src/panel.ts +422 -0
- package/src/parse.ts +137 -0
- package/src/patch-cache.ts +73 -0
- package/src/review-cache.ts +98 -0
- package/src/states.ts +350 -0
- package/src/types.ts +504 -0
- package/src/wildmatch.ts +121 -0
- package/src/worktree.ts +576 -0
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One file of the changes view: a sticky header with the path, badges, line
|
|
3
|
+
* counts, and file actions, then its hunks unified or side by side with the
|
|
4
|
+
* unchanged lines between them collapsed into rows that expand them. A section
|
|
5
|
+
* draws and highlights its lines once it comes within reach of the scrollport;
|
|
6
|
+
* it redraws only when its own props change.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { memo, useCallback, useEffect, useMemo, useRef, useState, type CSSProperties, type ReactNode } from 'react'
|
|
10
|
+
import clsx from 'clsx'
|
|
11
|
+
import { flushSync } from 'react-dom'
|
|
12
|
+
import {
|
|
13
|
+
FileTypeIcon, IconChevronDownOutlineRegular, IconChevronsUpDownOutlineRegular, IconEllipsisOutlineRegular, IconPlusOutlineRegular,
|
|
14
|
+
Tag, Tooltip,
|
|
15
|
+
languageForPath, Menu, MenuItemButton, useCodeHighlighter, type CodeHighlighter, type HighlightSpan,
|
|
16
|
+
} from '@deepseek-ai/dsh-client-ui-primitives'
|
|
17
|
+
import type { PropsLocale } from '@deepseek-ai/dsh-client-ui-slots'
|
|
18
|
+
import type { ForgeDiffFile } from '../types.ts'
|
|
19
|
+
import { drawnLines, segmentsOf, splitRows, type DiffRow, type SplitCell, type SplitRow } from './diff-rows.ts'
|
|
20
|
+
import { codeSideAt, readCodeSelection } from './code-selection.ts'
|
|
21
|
+
import { reviewMatches, selectReviewLines, type ReviewSelection } from './review.ts'
|
|
22
|
+
import { ReviewThread, useFileReview } from './ReviewThread.tsx'
|
|
23
|
+
import { OutdatedThreads, ThreadCard, type FileThreads, type LineThread, type ThreadActions } from './ReviewThreads.tsx'
|
|
24
|
+
import { gapOf } from './review-threads.ts'
|
|
25
|
+
import type { FileReviewState } from './review-queue.ts'
|
|
26
|
+
import { ReviewComposer, type ReviewActions } from './ReviewComposer.tsx'
|
|
27
|
+
import reviewCss from './ReviewComposer.module.css'
|
|
28
|
+
import { FileLabel } from './FileLabel.tsx'
|
|
29
|
+
import type { ForgePanelKey, NS } from './locales.ts'
|
|
30
|
+
import type { ViewedState } from './viewed-files.ts'
|
|
31
|
+
import type { SectionReach } from './section-reach.ts'
|
|
32
|
+
import css from './ChangesTab.module.css'
|
|
33
|
+
|
|
34
|
+
type Translate = PropsLocale<typeof NS>['t']
|
|
35
|
+
|
|
36
|
+
/** One action of a file's menu. */
|
|
37
|
+
export interface DiffFileAction {
|
|
38
|
+
/** Dictionary key of the row label. */
|
|
39
|
+
readonly label: ForgePanelKey
|
|
40
|
+
/** Runs the action. */
|
|
41
|
+
readonly run: () => void
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** New side of a file: its lines once read, `loading` while a read runs, or undefined before any. */
|
|
45
|
+
export type DiffFileText = readonly string[] | 'loading' | undefined
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* What every file section of one changes view calls. The view passes the same
|
|
49
|
+
* object on every render, so a section redraws only when its own values change.
|
|
50
|
+
*/
|
|
51
|
+
export interface DiffFileHandlers {
|
|
52
|
+
/** Expand or collapse a file. */
|
|
53
|
+
readonly toggle: (file: ForgeDiffFile) => void
|
|
54
|
+
/** Show a large file anyway. */
|
|
55
|
+
readonly reveal: (path: string) => void
|
|
56
|
+
/** Expand one gap of a file. */
|
|
57
|
+
readonly expand: (file: ForgeDiffFile, index: number) => void
|
|
58
|
+
/** Mark a file viewed, which also collapses it, or clear the mark. */
|
|
59
|
+
readonly view: (file: ForgeDiffFile, viewed: boolean) => void
|
|
60
|
+
/** Rows of a file's menu, read whenever the menu draws. */
|
|
61
|
+
readonly actions: (file: ForgeDiffFile) => readonly DiffFileAction[]
|
|
62
|
+
/** Receive a file's section element, which scrolling to the file targets, or null once it unmounts. */
|
|
63
|
+
readonly section: (path: string, element: HTMLElement | null) => void
|
|
64
|
+
/** Show a file's next unresolved host thread in display order after the last one shown, or its first. */
|
|
65
|
+
readonly nextThread: (path: string) => void
|
|
66
|
+
/** Tells each expanded section when it comes within reach of the scrollport, which draws its lines. */
|
|
67
|
+
readonly reach: SectionReach
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Props of one file section. */
|
|
71
|
+
export interface DiffFileProps {
|
|
72
|
+
/** Whether the owning tab is visible and may show temporary menus. */
|
|
73
|
+
readonly visible: boolean
|
|
74
|
+
/** Comment destinations and the source being reviewed. */
|
|
75
|
+
readonly review?: ReviewActions | undefined
|
|
76
|
+
/** The file and its hunks. */
|
|
77
|
+
readonly file: ForgeDiffFile
|
|
78
|
+
/** Whether the section shows its lines. */
|
|
79
|
+
readonly expanded: boolean
|
|
80
|
+
/** Whether the file shows the large-file notice instead of its lines until revealed. */
|
|
81
|
+
readonly large: boolean
|
|
82
|
+
/** Whether the user revealed a large file. */
|
|
83
|
+
readonly revealed: boolean
|
|
84
|
+
/** The file's new side, which supplies collapsed unchanged lines. */
|
|
85
|
+
readonly text: DiffFileText
|
|
86
|
+
/** Indexes of the expanded gaps. */
|
|
87
|
+
readonly open: ReadonlySet<number>
|
|
88
|
+
/** Whether deletions and additions show in two columns; a file with only additions or only deletions keeps one. */
|
|
89
|
+
readonly split: boolean
|
|
90
|
+
/** Whether long lines wrap. */
|
|
91
|
+
readonly wrap: boolean
|
|
92
|
+
/** Badge text after the path, or empty for none. */
|
|
93
|
+
readonly badge: string
|
|
94
|
+
/** Hover text of the path: both paths of a renamed file, else the path. */
|
|
95
|
+
readonly tooltip: string
|
|
96
|
+
/** Whether the reader marked this change viewed, or marked an earlier change of the file. */
|
|
97
|
+
readonly viewed: ViewedState
|
|
98
|
+
/** Tooltip of the viewed mark: where marks are kept, or why the mark cannot change now. */
|
|
99
|
+
readonly viewedNote: string
|
|
100
|
+
/** Whether the mark cannot change now, while its write is in flight or the host's marks could not be read. */
|
|
101
|
+
readonly viewedDisabled: boolean
|
|
102
|
+
/** Review threads of the file from the code host, or undefined while none are read; the view keeps their identity. */
|
|
103
|
+
readonly threads?: FileThreads | undefined
|
|
104
|
+
/** Whether the section draws its lines at once, as the target of a thread jump, whose card must exist to be centered. */
|
|
105
|
+
readonly drawNow?: boolean | undefined
|
|
106
|
+
/** Actions shared by every section of the view. */
|
|
107
|
+
readonly handlers: DiffFileHandlers
|
|
108
|
+
/** Forge panel copy. */
|
|
109
|
+
readonly t: Translate
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Render added and deleted line counts, or the binary label for binary content.
|
|
114
|
+
* @param props - the counts and copy.
|
|
115
|
+
* @returns the counts.
|
|
116
|
+
*/
|
|
117
|
+
export function Counts({ file, t }: {
|
|
118
|
+
readonly file: Pick<ForgeDiffFile, 'additions' | 'deletions' | 'binary'>
|
|
119
|
+
readonly t: Translate
|
|
120
|
+
}): ReactNode {
|
|
121
|
+
if (file.binary) return <span>{t('diff.binaryLabel')}</span>
|
|
122
|
+
return (
|
|
123
|
+
<span className={css.counts}>
|
|
124
|
+
<span className={css.added}>{`+${file.additions.toLocaleString('en-US')}`}</span>
|
|
125
|
+
<span className={css.deleted}>{`−${file.deletions.toLocaleString('en-US')}`}</span>
|
|
126
|
+
</span>
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const SIGN: Record<DiffRow['kind'], string> = { add: '+', del: '-', context: '' }
|
|
131
|
+
|
|
132
|
+
/** Highlighted tokens of one side's lines by line number, or undefined while the grammar is unavailable. */
|
|
133
|
+
type SideSpans = ReadonlyMap<number, readonly HighlightSpan[]> | undefined
|
|
134
|
+
|
|
135
|
+
/** A segment ready to draw: collapsed lines, or rows with the tokens of each side. */
|
|
136
|
+
type Prepared =
|
|
137
|
+
| { readonly kind: 'gap'; readonly index: number; readonly count: number | null }
|
|
138
|
+
| { readonly kind: 'rows'; readonly rows: readonly DiffRow[]; readonly old: SideSpans; readonly new: SideSpans }
|
|
139
|
+
|
|
140
|
+
/** Highlight one side of a run of rows as one fragment, so tokens spanning lines keep their state. */
|
|
141
|
+
function sideSpans(rows: readonly DiffRow[], side: 'old' | 'new', highlighter: CodeHighlighter): SideSpans {
|
|
142
|
+
const numbered: { readonly no: number; readonly text: string }[] = []
|
|
143
|
+
for (const row of rows) {
|
|
144
|
+
const no = row[side]
|
|
145
|
+
if (no !== null) numbered.push({ no, text: row.text })
|
|
146
|
+
}
|
|
147
|
+
if (numbered.length === 0) return new Map()
|
|
148
|
+
const highlighted = highlighter(numbered.map(line => line.text).join('\n'))
|
|
149
|
+
if (highlighted === undefined) return undefined
|
|
150
|
+
// Shiki omits a final empty token row, so a final empty line gets no tokens.
|
|
151
|
+
return new Map(numbered.map((line, index) => [line.no, highlighted[index] ?? []]))
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function Code({ text, spans, old, next }: {
|
|
155
|
+
readonly text: string
|
|
156
|
+
readonly spans: readonly HighlightSpan[] | undefined
|
|
157
|
+
readonly old: number | null
|
|
158
|
+
readonly next: number | null
|
|
159
|
+
}): ReactNode {
|
|
160
|
+
return (
|
|
161
|
+
<span className={css.text} data-review-code="" data-old-line={old ?? undefined} data-new-line={next ?? undefined} data-diff-code={spans === undefined ? undefined : ''}>
|
|
162
|
+
{spans === undefined ? text : spans.map((span, index) => <span key={index} style={span.style}>{span.text}</span>)}
|
|
163
|
+
</span>
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Whether a side-by-side row repeats an unchanged line or shows a change. */
|
|
168
|
+
/** Whether two comments cover the same lines of the same side. */
|
|
169
|
+
function sameRange(a: ReviewSelection['comment'], b: ReviewSelection['comment']): boolean {
|
|
170
|
+
return a.side === b.side && a.startLine === b.startLine && a.endLine === b.endLine
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Whether a selection covers one line of a side. */
|
|
174
|
+
function covers(item: ReviewSelection, side: 'L' | 'R', line: number): boolean {
|
|
175
|
+
return item.comment.side === side && line >= item.comment.startLine && line <= item.comment.endLine
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** The editor key of a range, such as `R3-4`. */
|
|
179
|
+
function rangeKey(item: ReviewSelection): string {
|
|
180
|
+
return `${item.comment.side}${item.comment.startLine}-${item.comment.endLine}`
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function pairKind(pair: SplitRow): 'context' | 'change' {
|
|
184
|
+
return pair.left !== null && pair.left.kind === 'context' ? 'context' : 'change'
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** The hunks of one file with their collapsed and expanded unchanged lines. */
|
|
188
|
+
function Lines({ file, text, open, handlers, split, wrap, review, visible, comments, threads, t }: Pick<DiffFileProps,
|
|
189
|
+
'file' | 'text' | 'open' | 'handlers' | 'split' | 'wrap' | 'review' | 'visible' | 'threads' | 't'> & { readonly comments: FileReviewState }): ReactNode {
|
|
190
|
+
const lines = typeof text === 'object' ? text : undefined
|
|
191
|
+
const columns = split && file.additions > 0 && file.deletions > 0
|
|
192
|
+
const singleSide = file.status === 'added' ? 'R' : file.status === 'deleted' ? 'L' : null
|
|
193
|
+
let maximum = Math.max(1, lines?.length ?? 0, (lines?.length ?? 0) + file.deletions - file.additions)
|
|
194
|
+
for (const hunk of file.hunks) maximum = Math.max(maximum, hunk.oldStart + hunk.oldLines - 1, hunk.newStart + hunk.newLines - 1)
|
|
195
|
+
const style: CSSProperties & { '--forge-number-width': string } = { '--forge-number-width': `calc(${Math.max(2, String(maximum).length)}ch + 12px)` }
|
|
196
|
+
const highlighter = useCodeHighlighter(languageForPath(file.path))
|
|
197
|
+
const [selection, setSelection] = useState<ReviewSelection | null>(null)
|
|
198
|
+
// Mirrors `selection` for the release of a hover-plus drag, whose listener outlives the render that chose the range.
|
|
199
|
+
const chosen = useRef<ReviewSelection | null>(null)
|
|
200
|
+
const select = (next: ReviewSelection | null): void => {
|
|
201
|
+
chosen.current = next
|
|
202
|
+
setSelection(next)
|
|
203
|
+
}
|
|
204
|
+
// Comments being written, in the order they opened; each keeps the range it opened with while new ranges are chosen.
|
|
205
|
+
const [editors, setEditors] = useState<readonly ReviewSelection[]>([])
|
|
206
|
+
const root = useRef<HTMLDivElement>(null)
|
|
207
|
+
const origin = useRef<{ side: 'L' | 'R'; line: number } | null>(null)
|
|
208
|
+
// `add` is a gutter drag that began on a row's hover plus and opens the editor when the pointer is released.
|
|
209
|
+
const dragging = useRef<'gutter' | 'add' | 'text' | null>(null)
|
|
210
|
+
const [selecting, setSelecting] = useState(false)
|
|
211
|
+
const [nativeSide, setNativeSide] = useState<'L' | 'R' | null>(null)
|
|
212
|
+
const nativeSelection = useCallback((): void => {
|
|
213
|
+
if (nativeSide === null || root.current === null || review === undefined) return
|
|
214
|
+
const selected = readCodeSelection(root.current, nativeSide)
|
|
215
|
+
if (selected === undefined) {
|
|
216
|
+
chosen.current = null
|
|
217
|
+
setSelection(null)
|
|
218
|
+
setNativeSide(null)
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
const comment = selectReviewLines(file, selected.side, selected.from, selected.to)
|
|
222
|
+
const previous = chosen.current
|
|
223
|
+
const next = comment === undefined ? null
|
|
224
|
+
: previous !== null && sameRange(previous.comment, comment) ? previous : { source: review.source, comment }
|
|
225
|
+
chosen.current = next
|
|
226
|
+
setSelection(next)
|
|
227
|
+
}, [nativeSide, review, file])
|
|
228
|
+
useEffect(() => {
|
|
229
|
+
if (nativeSide === null) return
|
|
230
|
+
const changed = (): void => { if (dragging.current === null) nativeSelection() }
|
|
231
|
+
document.addEventListener('selectionchange', changed)
|
|
232
|
+
return () => { document.removeEventListener('selectionchange', changed) }
|
|
233
|
+
}, [nativeSide, nativeSelection])
|
|
234
|
+
/** Open an editor for a range, or focus the one already open for it, and clear the selection for the next range. */
|
|
235
|
+
const compose = (target: ReviewSelection): void => {
|
|
236
|
+
const open = root.current?.querySelector<HTMLTextAreaElement>(`[data-forge-comment="${rangeKey(target)}"] textarea`) ?? null
|
|
237
|
+
if (open !== null) open.focus()
|
|
238
|
+
else setEditors(list => [...list, target])
|
|
239
|
+
const native = window.getSelection()
|
|
240
|
+
if (root.current?.contains(native?.anchorNode ?? null) === true) native?.removeAllRanges()
|
|
241
|
+
select(null)
|
|
242
|
+
setNativeSide(null)
|
|
243
|
+
origin.current = null
|
|
244
|
+
}
|
|
245
|
+
useEffect(() => {
|
|
246
|
+
if (nativeSide === null) return
|
|
247
|
+
const copy = (event: ClipboardEvent): void => {
|
|
248
|
+
if (root.current === null || event.clipboardData === null
|
|
249
|
+
|| (event.target instanceof Element && event.target.closest('input, textarea, [contenteditable="true"]') !== null)) return
|
|
250
|
+
const selected = readCodeSelection(root.current, nativeSide)
|
|
251
|
+
if (selected === undefined) return
|
|
252
|
+
event.preventDefault()
|
|
253
|
+
event.clipboardData.setData('text/plain', selected.text)
|
|
254
|
+
}
|
|
255
|
+
document.addEventListener('copy', copy)
|
|
256
|
+
return () => { document.removeEventListener('copy', copy) }
|
|
257
|
+
}, [nativeSide])
|
|
258
|
+
useEffect(() => {
|
|
259
|
+
if (!selecting) return
|
|
260
|
+
const finish = (event: PointerEvent): void => {
|
|
261
|
+
if (dragging.current === 'text') nativeSelection()
|
|
262
|
+
const release = dragging.current === 'add' && event.type === 'pointerup' ? chosen.current : null
|
|
263
|
+
dragging.current = null
|
|
264
|
+
setSelecting(false)
|
|
265
|
+
if (release !== null) compose(release)
|
|
266
|
+
}
|
|
267
|
+
window.addEventListener('pointerup', finish)
|
|
268
|
+
window.addEventListener('pointercancel', finish)
|
|
269
|
+
return () => {
|
|
270
|
+
window.removeEventListener('pointerup', finish)
|
|
271
|
+
window.removeEventListener('pointercancel', finish)
|
|
272
|
+
}
|
|
273
|
+
}, [selecting, nativeSelection])
|
|
274
|
+
const prepared = useMemo(() => segmentsOf(file, lines, open).map((segment): Prepared => segment.kind === 'gap'
|
|
275
|
+
? segment
|
|
276
|
+
: { ...segment, old: sideSpans(segment.rows, 'old', highlighter), new: sideSpans(segment.rows, 'new', highlighter) }), [file, lines, open, highlighter])
|
|
277
|
+
// Returns whether the range was accepted; a range across hunks or outside them keeps the previous selection.
|
|
278
|
+
const choose = (side: 'L' | 'R', line: number, extend: boolean): boolean => {
|
|
279
|
+
/* v8 ignore next -- the line buttons and hover pluses that choose a range draw only with review actions */
|
|
280
|
+
if (review === undefined) return false
|
|
281
|
+
const from = extend && origin.current?.side === side ? origin.current.line : line
|
|
282
|
+
const comment = selectReviewLines(file, side, from, line)
|
|
283
|
+
if (comment === undefined) return false
|
|
284
|
+
if (!extend || origin.current?.side !== side) origin.current = { side, line }
|
|
285
|
+
select({ source: review.source, comment })
|
|
286
|
+
return true
|
|
287
|
+
}
|
|
288
|
+
// A drag that began on a hover plus extends through every row with a line on its starting side, wherever the pointer crosses that row.
|
|
289
|
+
const extendAdd = (buttons: number, old: number | null, next: number | null): void => {
|
|
290
|
+
const from = origin.current
|
|
291
|
+
if (dragging.current !== 'add' || buttons !== 1 || from === null) return
|
|
292
|
+
const line = from.side === 'L' ? old : next
|
|
293
|
+
if (line !== null) choose(from.side, line, true)
|
|
294
|
+
}
|
|
295
|
+
const selected = (side: 'L' | 'R', line: number | null): boolean => line !== null && selection !== null && covers(selection, side, line)
|
|
296
|
+
// The browser paints a native text range itself; a gutter range and every open editor's range take the highlight.
|
|
297
|
+
const marked = (side: 'L' | 'R', line: number | null): boolean => line !== null
|
|
298
|
+
&& ((selected(side, line) && nativeSide === null) || editors.some(item => covers(item, side, line)))
|
|
299
|
+
// `hover` adds the mouse shortcut: a plus shown while a hovering pointer is over this side of the row.
|
|
300
|
+
const number = (side: 'L' | 'R', line: number | null, hover: boolean): ReactNode => {
|
|
301
|
+
if (line === null || review === undefined || !file.hunks.some((hunk) => {
|
|
302
|
+
const start = side === 'L' ? hunk.oldStart : hunk.newStart
|
|
303
|
+
return line >= start && line < start + (side === 'L' ? hunk.oldLines : hunk.newLines)
|
|
304
|
+
})) {
|
|
305
|
+
return <span className={css.number}>{line ?? ''}</span>
|
|
306
|
+
}
|
|
307
|
+
const plus = !selecting && selection !== null && selection.comment.side === side && selection.comment.endLine === line
|
|
308
|
+
? selection : null
|
|
309
|
+
return <span className={clsx(css.number, reviewCss.gutter)} data-review-side={side}
|
|
310
|
+
onPointerEnter={(event) => { if (dragging.current === 'gutter' && origin.current?.side === side && event.buttons === 1) choose(side, line, true) }}>
|
|
311
|
+
<button type="button" className={reviewCss.line}
|
|
312
|
+
aria-label={t('review.selectLine', { line: `${side}${line}` })} aria-pressed={selected(side, line)}
|
|
313
|
+
onPointerDown={(event) => {
|
|
314
|
+
if (event.button !== 0) return
|
|
315
|
+
event.preventDefault()
|
|
316
|
+
window.getSelection()?.removeAllRanges()
|
|
317
|
+
setNativeSide(null)
|
|
318
|
+
dragging.current = 'gutter'
|
|
319
|
+
setSelecting(true)
|
|
320
|
+
choose(side, line, event.shiftKey)
|
|
321
|
+
}}
|
|
322
|
+
onClick={(event) => {
|
|
323
|
+
if (event.detail !== 0) return
|
|
324
|
+
window.getSelection()?.removeAllRanges()
|
|
325
|
+
setNativeSide(null)
|
|
326
|
+
choose(side, line, event.shiftKey)
|
|
327
|
+
}}>{line}</button>
|
|
328
|
+
{plus !== null && (
|
|
329
|
+
<Tooltip label={t('review.addComment')} portal>
|
|
330
|
+
<button type="button" className={reviewCss.add} aria-label={t('review.addComment')}
|
|
331
|
+
onPointerDown={(event) => { event.preventDefault() }} onClick={() => { compose(plus) }}>
|
|
332
|
+
<IconPlusOutlineRegular size={14} />
|
|
333
|
+
</button>
|
|
334
|
+
</Tooltip>
|
|
335
|
+
)}
|
|
336
|
+
{/* Keyboard and assistive technology reach the same editor through the line button and the labeled plus above. */}
|
|
337
|
+
{hover && plus === null && (
|
|
338
|
+
<span className={reviewCss.hoverAdd} aria-hidden="true" data-review-add={`${side}${line}`}
|
|
339
|
+
onPointerDown={(event) => {
|
|
340
|
+
if (event.button !== 0) return
|
|
341
|
+
event.preventDefault()
|
|
342
|
+
// A refused range keeps the previous selection, which the release must not open.
|
|
343
|
+
if (!choose(side, line, event.shiftKey)) return
|
|
344
|
+
window.getSelection()?.removeAllRanges()
|
|
345
|
+
setNativeSide(null)
|
|
346
|
+
dragging.current = 'add'
|
|
347
|
+
setSelecting(true)
|
|
348
|
+
}}>
|
|
349
|
+
<IconPlusOutlineRegular size={14} />
|
|
350
|
+
</span>
|
|
351
|
+
)}
|
|
352
|
+
</span>
|
|
353
|
+
}
|
|
354
|
+
const cell = (value: SplitCell | null, spans: SideSpans, side: 'L' | 'R'): ReactNode => value === null
|
|
355
|
+
? <span className={clsx(css.cell, css.empty)} data-diff-side={side} />
|
|
356
|
+
: <span className={clsx(css.cell, css[value.kind], marked(side, value.no) && reviewCss.selected)}
|
|
357
|
+
data-diff-side={side}>
|
|
358
|
+
{number(side, value.no, true)}<Code text={value.text} spans={spans?.get(value.no)} old={side === 'L' ? value.no : null} next={side === 'R' ? value.no : null} />
|
|
359
|
+
</span>
|
|
360
|
+
const endsAt = (old: number | null, next: number | null): boolean => selection !== null
|
|
361
|
+
&& (selection.comment.side === 'L' ? old : next) === selection.comment.endLine
|
|
362
|
+
const rows: ReactNode[] = []
|
|
363
|
+
const unplacedEditors = new Set(editors)
|
|
364
|
+
const remaining = new Set(comments.threads)
|
|
365
|
+
// A host thread follows the row of its last line, or the collapsed unchanged lines that hold it.
|
|
366
|
+
const unplaced = new Set(threads === undefined ? [] : threads.lines.map(item => ({ ...item, actions: threads.actions })))
|
|
367
|
+
const threadCard = (item: LineThread & { readonly actions: ThreadActions }): ReactNode =>
|
|
368
|
+
<ThreadCard key={`thread-${item.thread.id}`} thread={item.thread} place={item.place} actions={item.actions} t={t} />
|
|
369
|
+
// The editors, the saved comments, then the host threads whose ranges end on this side's line.
|
|
370
|
+
const commentsAt = (side: 'L' | 'R', line: number | null): ReactNode[] => {
|
|
371
|
+
const nodes: ReactNode[] = []
|
|
372
|
+
if (line === null) return nodes
|
|
373
|
+
if (review !== undefined) {
|
|
374
|
+
for (const item of unplacedEditors) {
|
|
375
|
+
if (item.comment.side !== side || item.comment.endLine !== line) continue
|
|
376
|
+
nodes.push(editorOf(item, review))
|
|
377
|
+
unplacedEditors.delete(item)
|
|
378
|
+
}
|
|
379
|
+
for (const thread of remaining) {
|
|
380
|
+
const comment = thread.selection.comment
|
|
381
|
+
if (comment.side !== side || comment.endLine !== line || !reviewMatches(thread.selection, review.source, file)) continue
|
|
382
|
+
nodes.push(<ReviewThread key={`record-${thread.key}`} thread={thread} actions={review} locked={comments.locked} outdated={false} t={t} />)
|
|
383
|
+
remaining.delete(thread)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
for (const item of unplaced) {
|
|
387
|
+
if (item.place.side !== side || item.place.endLine !== line) continue
|
|
388
|
+
nodes.push(threadCard(item))
|
|
389
|
+
unplaced.delete(item)
|
|
390
|
+
}
|
|
391
|
+
return nodes
|
|
392
|
+
}
|
|
393
|
+
const editorOf = (item: ReviewSelection, actions: ReviewActions): ReactNode =>
|
|
394
|
+
<ReviewComposer key={`comment-${rangeKey(item)}`} selection={item} visible={visible} actions={actions}
|
|
395
|
+
close={() => { setEditors(list => list.filter(open => !sameRange(open.comment, item.comment))) }} t={t} />
|
|
396
|
+
// Side by side, a row's comments sit in a row of two cells under its columns, each continuing its line's background.
|
|
397
|
+
const notes = (key: string, pair: SplitRow): void => {
|
|
398
|
+
const left = commentsAt('L', pair.left?.no ?? null)
|
|
399
|
+
const right = commentsAt('R', pair.right?.no ?? null)
|
|
400
|
+
if (left.length === 0 && right.length === 0) return
|
|
401
|
+
const side = (value: SplitCell | null, nodes: ReactNode[]): ReactNode => (
|
|
402
|
+
<div className={clsx(css.noteCell, css[value?.kind ?? 'empty'])}>
|
|
403
|
+
<span className={css.number} />
|
|
404
|
+
<div className={css.noteStack} data-forge-comments="">{nodes}</div>
|
|
405
|
+
</div>
|
|
406
|
+
)
|
|
407
|
+
rows.push(<div key={key} className={css.notes} data-forge-notes="">{side(pair.left, left)}{side(pair.right, right)}</div>)
|
|
408
|
+
}
|
|
409
|
+
for (const [at, segment] of prepared.entries()) {
|
|
410
|
+
if (segment.kind === 'gap') {
|
|
411
|
+
rows.push(<button key={`gap-${segment.index}`} type="button" className={css.gap} disabled={text === 'loading'}
|
|
412
|
+
data-forge-diff-gap={segment.index} onClick={() => { handlers.expand(file, segment.index) }}>
|
|
413
|
+
<IconChevronsUpDownOutlineRegular size={12} />
|
|
414
|
+
{segment.count === null ? t('diff.unchangedRest') : t('diff.unchanged', { count: segment.count.toLocaleString('en-US') })}
|
|
415
|
+
</button>)
|
|
416
|
+
for (const item of unplaced) {
|
|
417
|
+
if (gapOf(file, item.place.side, item.place.endLine) !== segment.index) continue
|
|
418
|
+
rows.push(threadCard(item))
|
|
419
|
+
unplaced.delete(item)
|
|
420
|
+
}
|
|
421
|
+
} else if (columns) {
|
|
422
|
+
for (const [index, pair] of splitRows(segment.rows).entries()) {
|
|
423
|
+
rows.push(<div key={`${at}-${index}`} className={css.pair} data-diff-pair={pairKind(pair)}
|
|
424
|
+
onPointerEnter={(event) => { extendAdd(event.buttons, pair.left?.no ?? null, pair.right?.no ?? null) }}>
|
|
425
|
+
{cell(pair.left, segment.old, 'L')}{cell(pair.right, segment.new, 'R')}
|
|
426
|
+
</div>)
|
|
427
|
+
notes(`notes-${at}-${index}`, pair)
|
|
428
|
+
}
|
|
429
|
+
} else {
|
|
430
|
+
for (const [index, row] of segment.rows.entries()) {
|
|
431
|
+
// Both numbers place their plus in the sign column, so a row showing the labeled plus offers no hover plus.
|
|
432
|
+
const labeled = !selecting && endsAt(row.old, row.new)
|
|
433
|
+
rows.push(<div key={`${at}-${index}`} className={clsx(css.line, css[row.kind], (marked('L', row.old) || marked('R', row.new)) && reviewCss.selected)} data-diff-line={row.kind}
|
|
434
|
+
onPointerEnter={(event) => { extendAdd(event.buttons, row.old, row.new) }}>
|
|
435
|
+
{singleSide !== 'R' && number('L', row.old, row.kind === 'del' && !labeled)}{singleSide !== 'L' && number('R', row.new, row.kind !== 'del' && !labeled)}<span className={css.sign}>{SIGN[row.kind]}</span>
|
|
436
|
+
<Code text={row.text} old={row.old} next={row.new} spans={row.kind === 'del' ? segment.old?.get(row.old) : segment.new?.get(row.new)} />
|
|
437
|
+
</div>)
|
|
438
|
+
rows.push(...commentsAt('L', row.old), ...commentsAt('R', row.new))
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
if (review !== undefined) {
|
|
443
|
+
// An editor whose last line no longer draws, after a refresh or a narrower choice of commits, follows the lines.
|
|
444
|
+
for (const item of unplacedEditors) rows.push(editorOf(item, review))
|
|
445
|
+
for (const thread of remaining) {
|
|
446
|
+
rows.push(<ReviewThread key={`record-${thread.key}`} thread={thread} actions={review} locked={comments.locked} outdated t={t} />)
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
for (const item of unplaced) rows.push(threadCard(item))
|
|
450
|
+
return <div ref={root} className={css.code} style={style} data-single-side={singleSide ?? undefined} data-split={columns ? '' : undefined} data-wrap={wrap ? '' : undefined}
|
|
451
|
+
data-review={review === undefined ? undefined : ''}
|
|
452
|
+
data-native-side={nativeSide ?? undefined}
|
|
453
|
+
onPointerDownCapture={(event) => {
|
|
454
|
+
if (event.button !== 0 || !(event.target instanceof Node)) return
|
|
455
|
+
const side = codeSideAt(event.currentTarget, event.target)
|
|
456
|
+
if (side === undefined) return
|
|
457
|
+
if (!event.shiftKey || nativeSide !== side) window.getSelection()?.removeAllRanges()
|
|
458
|
+
dragging.current = 'text'
|
|
459
|
+
// The chosen column must allow selection before the browser starts its native text range.
|
|
460
|
+
flushSync(() => {
|
|
461
|
+
setNativeSide(side)
|
|
462
|
+
select(null)
|
|
463
|
+
setSelecting(true)
|
|
464
|
+
})
|
|
465
|
+
}}>{rows}</div>
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Whether a file's body draws its hunks line by line rather than a note or a collapsed strip. */
|
|
469
|
+
function drawsLines(file: ForgeDiffFile, large: boolean, revealed: boolean): boolean {
|
|
470
|
+
return !file.binary && !file.oversized && file.hunks.length > 0 && (!large || revealed)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/** The file's lines or the note that replaces them, with its saved comments; host threads on lines follow when no lines show. */
|
|
474
|
+
function Content(props: DiffFileProps): ReactNode {
|
|
475
|
+
const { file, large, revealed, handlers, review, threads, t } = props
|
|
476
|
+
const comments = useFileReview(review, file.path)
|
|
477
|
+
const records = review === undefined ? null : comments.threads.map(thread => <ReviewThread key={thread.key} thread={thread}
|
|
478
|
+
actions={review} locked={comments.locked} outdated={!reviewMatches(thread.selection, review.source, file)} t={t} />)
|
|
479
|
+
const lineThreads = threads?.lines.map(item => <ThreadCard key={item.thread.id} thread={item.thread} place={item.place}
|
|
480
|
+
actions={threads.actions} t={t} />)
|
|
481
|
+
if (file.binary) return <><p className={css.fileNote}>{t('diff.binary')}</p>{records}{lineThreads}</>
|
|
482
|
+
if (file.oversized) return <><p className={css.fileNote}>{t('diff.fileTooLarge')}</p>{records}{lineThreads}</>
|
|
483
|
+
if (file.hunks.length === 0) return <>{records}{lineThreads}</>
|
|
484
|
+
if (large && !revealed) {
|
|
485
|
+
// A large file starts as one collapsed strip, drawn and opened like the collapsed unchanged lines.
|
|
486
|
+
return (
|
|
487
|
+
<><button type="button" className={css.gap} onClick={() => { handlers.reveal(file.path) }}>
|
|
488
|
+
<IconChevronsUpDownOutlineRegular size={12} />
|
|
489
|
+
{t('diff.largeCollapsed', { count: (file.additions + file.deletions).toLocaleString('en-US') })}
|
|
490
|
+
</button>{records}{lineThreads}</>
|
|
491
|
+
)
|
|
492
|
+
}
|
|
493
|
+
return <Lines {...props} comments={comments} />
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/** Host threads on the whole file, the lines, then the host threads that later commits made outdated. */
|
|
497
|
+
function Body(props: DiffFileProps): ReactNode {
|
|
498
|
+
const { threads, t } = props
|
|
499
|
+
return <>
|
|
500
|
+
{threads?.file.map(thread => <ThreadCard key={thread.id} thread={thread} place={{ kind: 'file' }} actions={threads.actions} t={t} />)}
|
|
501
|
+
<Content {...props} />
|
|
502
|
+
{threads !== undefined && <OutdatedThreads threads={threads.outdated} actions={threads.actions} t={t} />}
|
|
503
|
+
</>
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** File actions live only while their owning tab is visible. */
|
|
507
|
+
function FileMenu({ name, file, handlers, t }: {
|
|
508
|
+
readonly name: string
|
|
509
|
+
readonly file: ForgeDiffFile
|
|
510
|
+
readonly handlers: DiffFileHandlers
|
|
511
|
+
readonly t: Translate
|
|
512
|
+
}): ReactNode {
|
|
513
|
+
const [menuOpen, setMenuOpen] = useState(false)
|
|
514
|
+
return (
|
|
515
|
+
<Menu
|
|
516
|
+
open={menuOpen}
|
|
517
|
+
portal
|
|
518
|
+
align="end"
|
|
519
|
+
dense
|
|
520
|
+
autoFocus
|
|
521
|
+
onClose={() => { setMenuOpen(false) }}
|
|
522
|
+
anchor={(
|
|
523
|
+
<button
|
|
524
|
+
type="button"
|
|
525
|
+
className={css.tool}
|
|
526
|
+
aria-haspopup="menu"
|
|
527
|
+
aria-expanded={menuOpen}
|
|
528
|
+
aria-label={t('diff.moreActions', { name })}
|
|
529
|
+
onClick={() => { setMenuOpen(value => !value) }}
|
|
530
|
+
>
|
|
531
|
+
<IconEllipsisOutlineRegular />
|
|
532
|
+
</button>
|
|
533
|
+
)}
|
|
534
|
+
>
|
|
535
|
+
{handlers.actions(file).map(action => (
|
|
536
|
+
<MenuItemButton
|
|
537
|
+
key={action.label}
|
|
538
|
+
onSelect={() => {
|
|
539
|
+
setMenuOpen(false)
|
|
540
|
+
action.run()
|
|
541
|
+
}}
|
|
542
|
+
>
|
|
543
|
+
{t(action.label)}
|
|
544
|
+
</MenuItemButton>
|
|
545
|
+
))}
|
|
546
|
+
</Menu>
|
|
547
|
+
)
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Render one file of a diff. Until the section first comes within reach of the scrollport or a thread jump targets it,
|
|
552
|
+
* an expanded file that draws lines shows a box of about their height instead.
|
|
553
|
+
* @param props - the file, its section and gap state, layout choices, shared actions, and copy.
|
|
554
|
+
* @returns the file section.
|
|
555
|
+
*/
|
|
556
|
+
function DiffFileSection(props: DiffFileProps): ReactNode {
|
|
557
|
+
const { file, expanded, large, revealed, badge, tooltip, viewed, viewedNote, viewedDisabled, threads, handlers, t } = props
|
|
558
|
+
const name = file.path.slice(file.path.lastIndexOf('/') + 1)
|
|
559
|
+
const element = useRef<HTMLElement | null>(null)
|
|
560
|
+
const [reached, setReached] = useState(false)
|
|
561
|
+
// A thread jump draws the section for good, so it does not fall back to the box once the jump ends.
|
|
562
|
+
if (props.drawNow === true && !reached) setReached(true)
|
|
563
|
+
const waiting = expanded && !reached && props.drawNow !== true && drawsLines(file, large, revealed)
|
|
564
|
+
useEffect(() => {
|
|
565
|
+
/* v8 ignore next -- React attaches the section before it runs effects */
|
|
566
|
+
if (!waiting || element.current === null) return
|
|
567
|
+
return handlers.reach.observe(element.current, () => { setReached(true) })
|
|
568
|
+
}, [waiting, handlers.reach])
|
|
569
|
+
const sectionRef = useCallback((node: HTMLElement | null) => {
|
|
570
|
+
element.current = node
|
|
571
|
+
handlers.section(file.path, node)
|
|
572
|
+
}, [handlers, file.path])
|
|
573
|
+
const placeholder: CSSProperties & { '--forge-rows': number } = { '--forge-rows': drawnLines(file) }
|
|
574
|
+
return (
|
|
575
|
+
<section ref={sectionRef} className={css.file} data-forge-diff-file={file.path} data-expanded={expanded ? '' : undefined}
|
|
576
|
+
data-viewed={viewed}>
|
|
577
|
+
<div className={css.fileHeader}>
|
|
578
|
+
<button
|
|
579
|
+
type="button"
|
|
580
|
+
className={css.fileToggle}
|
|
581
|
+
aria-expanded={expanded}
|
|
582
|
+
aria-label={t(expanded ? 'change.collapse' : 'change.expand', { name })}
|
|
583
|
+
onClick={() => { handlers.toggle(file) }}
|
|
584
|
+
>
|
|
585
|
+
<IconChevronDownOutlineRegular size={14} className={css.chevron} />
|
|
586
|
+
</button>
|
|
587
|
+
<span className={css.fileTitle}>
|
|
588
|
+
<FileTypeIcon path={file.path} size={16} className={css.fileIcon} />
|
|
589
|
+
<span className={css.filePath}>
|
|
590
|
+
<FileLabel path={file.path} tooltip={tooltip} />
|
|
591
|
+
</span>
|
|
592
|
+
{viewed === 'changed' && (
|
|
593
|
+
<Tag tone="warning" className={css.changedTag}><span className={css.changedText}>{t('viewed.changed')}</span></Tag>
|
|
594
|
+
)}
|
|
595
|
+
{threads !== undefined && threads.unresolved > 0 && (
|
|
596
|
+
<Tooltip label={t('thread.next')} side="bottom">
|
|
597
|
+
<button type="button" className={css.threadCount} aria-label={t('thread.unresolvedAria', { name, count: threads.unresolved })}
|
|
598
|
+
onClick={() => { handlers.nextThread(file.path) }}>
|
|
599
|
+
<span className={css.threadCountText}>{t('thread.unresolvedCount', { count: threads.unresolved })}</span>
|
|
600
|
+
<span className={css.threadCountNumber}>{threads.unresolved}</span>
|
|
601
|
+
</button>
|
|
602
|
+
</Tooltip>
|
|
603
|
+
)}
|
|
604
|
+
<span className={css.fileSpacer} data-forge-path-spacer="" />
|
|
605
|
+
{badge !== '' && <span className={css.badge}>{badge}</span>}
|
|
606
|
+
</span>
|
|
607
|
+
{!file.binary && <Counts file={file} t={t} />}
|
|
608
|
+
<label className={css.viewed} title={viewedNote} data-disabled={viewedDisabled ? '' : undefined}>
|
|
609
|
+
<input type="checkbox" checked={viewed === 'viewed'} aria-label={t('viewed.aria', { name })} disabled={viewedDisabled}
|
|
610
|
+
onChange={(event) => { handlers.view(file, event.target.checked) }} />
|
|
611
|
+
<span className={css.viewedLabel} aria-hidden="true">{t('viewed.label')}</span>
|
|
612
|
+
</label>
|
|
613
|
+
{props.visible && <FileMenu name={name} file={file} handlers={handlers} t={t} />}
|
|
614
|
+
</div>
|
|
615
|
+
{expanded && (waiting ? <div className={css.unreached} style={placeholder} data-forge-diff-unreached="" /> : <Body {...props} />)}
|
|
616
|
+
</section>
|
|
617
|
+
)
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** One file of a diff, redrawn only when its props change. */
|
|
621
|
+
export const DiffFile = memo(DiffFileSection)
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/* A changed file's name, then its directory in smaller subdued type. */
|
|
2
|
+
.label {
|
|
3
|
+
/* Keep the hidden accessible path inside the file's own scroll container. */
|
|
4
|
+
position: relative;
|
|
5
|
+
display: flex;
|
|
6
|
+
min-width: 0;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/* The name keeps its natural width; the directory takes only the space left, so it shortens first. */
|
|
10
|
+
.parts {
|
|
11
|
+
display: flex;
|
|
12
|
+
align-items: baseline;
|
|
13
|
+
gap: 6px;
|
|
14
|
+
min-width: 0;
|
|
15
|
+
overflow: hidden;
|
|
16
|
+
white-space: nowrap;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.name {
|
|
20
|
+
flex: 0 1 auto;
|
|
21
|
+
color: var(--dsw-alias-label-primary);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.directory {
|
|
25
|
+
flex: 1 1 0;
|
|
26
|
+
color: var(--dsw-alias-label-tertiary);
|
|
27
|
+
font-size: 11px;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
.shortened {
|
|
31
|
+
display: flex;
|
|
32
|
+
min-width: 0;
|
|
33
|
+
overflow: hidden;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.head {
|
|
37
|
+
min-width: 0;
|
|
38
|
+
overflow: hidden;
|
|
39
|
+
text-overflow: ellipsis;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.tail {
|
|
43
|
+
flex: none;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.visuallyHidden {
|
|
47
|
+
position: absolute;
|
|
48
|
+
width: 1px;
|
|
49
|
+
height: 1px;
|
|
50
|
+
overflow: hidden;
|
|
51
|
+
clip-path: inset(50%);
|
|
52
|
+
white-space: nowrap;
|
|
53
|
+
}
|