@zyro-sec/session-ui 0.0.0-stage → 1.0.4
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/AGENTS.md +25 -0
- package/bunfig.toml +2 -0
- package/component-tests/basic-tool.fixture.tsx +46 -0
- package/component-tests/basic-tool.spec.ts +63 -0
- package/component-tests/file-media.fixture.tsx +39 -0
- package/component-tests/file-media.spec.ts +13 -0
- package/component-tests/file-tool.spec.ts +62 -0
- package/component-tests/file-viewer.fixture.tsx +109 -0
- package/component-tests/file-viewer.spec.ts +134 -0
- package/component-tests/file.spec.ts +33 -0
- package/component-tests/line-comment-v2.spec.ts +15 -0
- package/component-tests/markdown-code-wrap.spec.ts +94 -0
- package/component-tests/markdown-readiness.spec.ts +95 -0
- package/component-tests/markdown.fixture.tsx +104 -0
- package/component-tests/markdown.spec.ts +1564 -0
- package/component-tests/mermaid-preview.spec.ts +43 -0
- package/component-tests/patch-group.spec.ts +79 -0
- package/component-tests/read-group.spec.ts +72 -0
- package/component-tests/read-image.fixture.tsx +66 -0
- package/component-tests/read-image.spec.ts +154 -0
- package/component-tests/session-compaction.spec.ts +254 -0
- package/component-tests/session-lifecycle.spec.ts +190 -0
- package/component-tests/session-message-projection.spec.ts +639 -0
- package/component-tests/session-notice-projection.spec.ts +20 -0
- package/component-tests/session-review-comments.spec.ts +179 -0
- package/component-tests/session-shell.spec.ts +93 -0
- package/component-tests/session-timeline-mobile.spec.ts +201 -0
- package/component-tests/session-timeline-notices.spec.ts +38 -0
- package/component-tests/session-timeline-reasoning.spec.ts +107 -0
- package/component-tests/session-timeline.spec.ts +157 -0
- package/component-tests/session-tool-projection.spec.ts +323 -0
- package/component-tests/text-lifecycle.fixture.tsx +26 -0
- package/component-tests/text-lifecycle.spec.ts +42 -0
- package/component-tests/timeline-subagent-rows.spec.ts +33 -0
- package/component-tests/tool-disclosure.spec.ts +48 -0
- package/component-tests/tool-group-sticky.spec.ts +37 -0
- package/component-tests/tool-group.spec.ts +290 -0
- package/component-tests/tool-status-title.fixture.tsx +22 -0
- package/component-tests/tool-status-title.spec.ts +30 -0
- package/component-tests/user-message-quotes.spec.ts +111 -0
- package/package.json +91 -4
- package/playwright.components.config.ts +4 -0
- package/playwright.local.config.ts +2 -0
- package/src/actions.ts +54 -0
- package/src/components/apply-patch-file.ts +122 -0
- package/src/components/basic-tool.css +487 -0
- package/src/components/basic-tool.tsx +258 -0
- package/src/components/copy-machine.ts +78 -0
- package/src/components/copy.ts +85 -0
- package/src/components/diagram-preview.tsx +28 -0
- package/src/components/disclosure-machine.ts +188 -0
- package/src/components/disclosure.ts +129 -0
- package/src/components/dock-prompt.stories.tsx +73 -0
- package/src/components/dock-prompt.tsx +29 -0
- package/src/components/file-comment-selection.tsx +146 -0
- package/src/components/file-media-machine.ts +165 -0
- package/src/components/file-media.stories.tsx +112 -0
- package/src/components/file-media.tsx +323 -0
- package/src/components/file-search.tsx +73 -0
- package/src/components/file.css +59 -0
- package/src/components/file.tsx +1300 -0
- package/src/components/image-preview.tsx +35 -0
- package/src/components/line-comment-annotations.tsx +580 -0
- package/src/components/line-comment-machine.ts +262 -0
- package/src/components/line-comment-styles.ts +293 -0
- package/src/components/line-comment.stories.tsx +152 -0
- package/src/components/line-comment.tsx +448 -0
- package/src/components/link-segments.ts +20 -0
- package/src/components/linked-text.tsx +21 -0
- package/src/components/markdown-cache-machine.ts +257 -0
- package/src/components/markdown-cache.tsx +556 -0
- package/src/components/markdown-clipboard.ts +101 -0
- package/src/components/markdown-code-machine.ts +200 -0
- package/src/components/markdown-code-state.ts +22 -0
- package/src/components/markdown-fragment-links.ts +74 -0
- package/src/components/markdown-github.svg +3 -0
- package/src/components/markdown-globe.svg +5 -0
- package/src/components/markdown-image-machine.ts +170 -0
- package/src/components/markdown-image.ts +156 -0
- package/src/components/markdown-inline-code-kind.ts +39 -0
- package/src/components/markdown-mermaid.ts +18 -0
- package/src/components/markdown-phase-machine.ts +102 -0
- package/src/components/markdown-projection.ts +13 -0
- package/src/components/markdown-session-links.ts +183 -0
- package/src/components/markdown-solid.test.tsx +92 -0
- package/src/components/markdown-solid.tsx +113 -0
- package/src/components/markdown-stream.ts +95 -0
- package/src/components/markdown-worker-machine.ts +516 -0
- package/src/components/markdown-worker-protocol.ts +73 -0
- package/src/components/markdown-worker.ts +250 -0
- package/src/components/markdown.css +672 -0
- package/src/components/markdown.stories.tsx +199 -0
- package/src/components/markdown.tsx +1612 -0
- package/src/components/markdown.worker.ts +188 -0
- package/src/components/message-file.ts +102 -0
- package/src/components/message-nav.css +134 -0
- package/src/components/message-nav.tsx +159 -0
- package/src/components/message-part.css +2109 -0
- package/src/components/message-part.tsx +3 -0
- package/src/components/question-prompt-experimental.css +576 -0
- package/src/components/question-prompt.css +527 -0
- package/src/components/quote-highlight.css +59 -0
- package/src/components/review-focus-driver.ts +142 -0
- package/src/components/review-focus-machine.ts +95 -0
- package/src/components/session-compaction.css +270 -0
- package/src/components/session-diff.ts +358 -0
- package/src/components/session-error.tsx +41 -0
- package/src/components/session-retry.tsx +92 -0
- package/src/components/session-review-machine.ts +241 -0
- package/src/components/session-review.css +264 -0
- package/src/components/session-review.stories.tsx +232 -0
- package/src/components/session-review.tsx +733 -0
- package/src/components/session-turn.css +282 -0
- package/src/components/shell-submessage.css +24 -0
- package/src/components/timeline-separator.tsx +69 -0
- package/src/components/tool-count-label.css +57 -0
- package/src/components/tool-count-label.tsx +76 -0
- package/src/components/tool-count-summary.css +102 -0
- package/src/components/tool-count-summary.stories.tsx +99 -0
- package/src/components/tool-count-summary.tsx +44 -0
- package/src/components/tool-error-card.css +178 -0
- package/src/components/tool-error-card.stories.tsx +113 -0
- package/src/components/tool-error-card.tsx +202 -0
- package/src/components/tool-status-title-machine.ts +128 -0
- package/src/components/tool-status-title.css +89 -0
- package/src/components/tool-status-title.tsx +163 -0
- package/src/context/data.tsx +107 -0
- package/src/context/index.ts +1 -0
- package/src/context/markdown.tsx +129 -0
- package/src/context/sources.tsx +34 -0
- package/src/document.ts +8 -0
- package/src/file-presentation.ts +34 -0
- package/src/message/action-machine.ts +72 -0
- package/src/message/agent-label.ts +3 -0
- package/src/message/attachment-card.css +119 -0
- package/src/message/attachment-card.tsx +40 -0
- package/src/message/attachment-media-machine.ts +213 -0
- package/src/message/comment-card.tsx +155 -0
- package/src/message/compaction-entrance-machine.ts +93 -0
- package/src/message/current-message.tsx +157 -0
- package/src/message/current-tool-state.ts +161 -0
- package/src/message/message-content.tsx +1391 -0
- package/src/message/paced-text-machine.ts +73 -0
- package/src/message/prompt-message.stories.tsx +149 -0
- package/src/message/relative-time.ts +45 -0
- package/src/message/user-message-quote-parts.ts +16 -0
- package/src/message/user-message-quotes.stories.tsx +797 -0
- package/src/message/user-message-quotes.tsx +135 -0
- package/src/pierre/comment-hover-machine.ts +53 -0
- package/src/pierre/comment-hover.ts +131 -0
- package/src/pierre/comment-selection-machine.ts +257 -0
- package/src/pierre/commented-lines.ts +106 -0
- package/src/pierre/diff-selection.ts +91 -0
- package/src/pierre/file-find-machine.ts +323 -0
- package/src/pierre/file-find.ts +537 -0
- package/src/pierre/file-runtime.ts +42 -0
- package/src/pierre/file-selection.ts +140 -0
- package/src/pierre/index.ts +344 -0
- package/src/pierre/media.ts +140 -0
- package/src/pierre/render-machine.ts +381 -0
- package/src/pierre/selection-bridge.ts +82 -0
- package/src/pierre/selection-machine.ts +198 -0
- package/src/pierre/virtualizer.ts +114 -0
- package/src/pierre/worker.ts +46 -0
- package/src/sources/experiment.ts +59 -0
- package/src/sources/site-icon.tsx +53 -0
- package/src/sources/sources-chip.tsx +57 -0
- package/src/sources/sources-expand-machine.ts +101 -0
- package/src/sources/sources-panel.tsx +249 -0
- package/src/sources/sources-title.tsx +21 -0
- package/src/sources/sources.css +266 -0
- package/src/sources/sources.ts +141 -0
- package/src/storybook/current-session-fixtures.ts +1492 -0
- package/src/storybook/current-session-scenarios.ts +76 -0
- package/src/storybook/current-session-story.tsx +154 -0
- package/src/storybook/index.ts +43 -0
- package/src/styles/index.css +19 -0
- package/src/timeline/__snapshots__/group-labels.test.ts.snap +110 -0
- package/src/timeline/__snapshots__/placement.test.ts.snap +1041 -0
- package/src/timeline/code-blocks.stories.tsx +105 -0
- package/src/timeline/commentary.stories.tsx +70 -0
- package/src/timeline/commentary.ts +38 -0
- package/src/timeline/detail.ts +111 -0
- package/src/timeline/error-card.stories.tsx +134 -0
- package/src/timeline/error-spacing.stories.tsx +71 -0
- package/src/timeline/facts.ts +493 -0
- package/src/timeline/file-changes.stories.tsx +436 -0
- package/src/timeline/group-label-fixtures.ts +97 -0
- package/src/timeline/mermaid.stories.tsx +131 -0
- package/src/timeline/place.ts +280 -0
- package/src/timeline/placement-fixtures.ts +365 -0
- package/src/timeline/placement-reactivity.ts +133 -0
- package/src/timeline/projection.ts +1261 -0
- package/src/timeline/research-and-agents.stories.tsx +461 -0
- package/src/timeline/session-timeline-row.tsx +1675 -0
- package/src/timeline/session-timeline.tsx +68 -0
- package/src/timeline/steer-arrival-machine.ts +134 -0
- package/src/timeline/terminal-work.stories.tsx +464 -0
- package/src/timeline/timeline-row.stories.tsx +1069 -0
- package/src/timeline/timeline-row.ts +238 -0
- package/src/timeline/turn-phase-machine.ts +112 -0
- package/src/timeline/user-message-fold.stories.tsx +194 -0
- package/src/timeline/user-runs.ts +87 -0
- package/src/timeline-next/__snapshots__/placement.test.ts.snap +990 -0
- package/src/timeline-next/compaction.tsx +236 -0
- package/src/timeline-next/errors.ts +101 -0
- package/src/timeline-next/event-row.tsx +168 -0
- package/src/timeline-next/events.ts +103 -0
- package/src/timeline-next/index.ts +10 -0
- package/src/timeline-next/nested-rail.ts +125 -0
- package/src/timeline-next/projection.ts +1028 -0
- package/src/timeline-next/row.tsx +1728 -0
- package/src/timeline-next/rules.ts +89 -0
- package/src/timeline-next/separator.tsx +73 -0
- package/src/timeline-next/spacing.ts +127 -0
- package/src/timeline-next/status-line.tsx +41 -0
- package/src/timeline-next/status.ts +33 -0
- package/src/timeline-next/stories/compare-renderer.stories.tsx +190 -0
- package/src/timeline-next/stories/compare-sessions/.gitignore +3 -0
- package/src/timeline-next/stories/compare.stories.tsx +486 -0
- package/src/timeline-next/stories/playground.stories.tsx +297 -0
- package/src/timeline-next/stories/session-replay.ts +1156 -0
- package/src/timeline-next/stories/session-timeline.tsx +50 -0
- package/src/timeline-next/stories/subagent-status.stories.tsx +132 -0
- package/src/timeline-next/timeline-next.css +649 -0
- package/src/timeline-next/tools/__snapshots__/activity-label.test.ts.snap +39 -0
- package/src/timeline-next/tools/activity-label.ts +166 -0
- package/src/timeline-next/tools/assistant-content.tsx +84 -0
- package/src/timeline-next/tools/context-group.tsx +619 -0
- package/src/timeline-next/tools/error-card.tsx +147 -0
- package/src/timeline-next/tools/explore.tsx +119 -0
- package/src/timeline-next/tools/file-group.tsx +128 -0
- package/src/timeline-next/tools/patch.tsx +174 -0
- package/src/timeline-next/tools/shell.tsx +186 -0
- package/src/timeline-next/tools/thought.tsx +87 -0
- package/src/timeline-next/tools/tool-display.tsx +119 -0
- package/src/tools/ansi.ts +8 -0
- package/src/tools/context-group-label.ts +123 -0
- package/src/tools/file-runs.ts +35 -0
- package/src/tools/project-path.ts +18 -0
- package/src/tools/read-group.stories.tsx +159 -0
- package/src/tools/run-machine.ts +185 -0
- package/src/tools/shell-output-machine.ts +233 -0
- package/src/tools/shell-output.ts +91 -0
- package/src/tools/subagent-row-variants.stories.tsx +129 -0
- package/src/tools/tool-group.stories.tsx +345 -0
- package/src/tools/tool-renderer.tsx +2876 -0
- package/src/v2/components/line-comment-annotations-v2.tsx +215 -0
- package/src/v2/components/session-file-panel-v2.tsx +149 -0
- package/src/v2/components/session-progress-indicator-v2.css +5 -0
- package/src/v2/components/session-progress-indicator-v2.stories.tsx +86 -0
- package/src/v2/components/session-progress-indicator-v2.tsx +16 -0
- package/src/v2/components/session-review-empty-changes-v2.tsx +17 -0
- package/src/v2/components/session-review-empty-no-git-v2.tsx +28 -0
- package/src/v2/components/session-review-file-preview-v2-virtualize.ts +5 -0
- package/src/v2/components/session-review-file-preview-v2.tsx +266 -0
- package/src/v2/components/session-review-v2.css +653 -0
- package/src/v2/components/session-review-v2.tsx +422 -0
- package/src/v2/components/sidebar-presence-machine.ts +69 -0
- package/tsconfig.json +20 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1300 @@
|
|
|
1
|
+
import { sampledChecksum } from "@zyro-sec/util/encode"
|
|
2
|
+
import {
|
|
3
|
+
areOptionsEqual,
|
|
4
|
+
DEFAULT_VIRTUAL_FILE_METRICS,
|
|
5
|
+
type DiffLineAnnotation,
|
|
6
|
+
type FileContents,
|
|
7
|
+
type FileDiffMetadata,
|
|
8
|
+
File as PierreFile,
|
|
9
|
+
type FileDiffOptions,
|
|
10
|
+
FileDiff,
|
|
11
|
+
type FileOptions,
|
|
12
|
+
type LineAnnotation,
|
|
13
|
+
type SelectedLineRange,
|
|
14
|
+
type VirtualFileMetrics,
|
|
15
|
+
VirtualizedFile,
|
|
16
|
+
VirtualizedFileDiff,
|
|
17
|
+
Virtualizer,
|
|
18
|
+
} from "@pierre/diffs"
|
|
19
|
+
import { type PreloadFileDiffResult, type PreloadMultiFileDiffResult } from "@pierre/diffs/ssr"
|
|
20
|
+
import { createMediaQuery } from "@solid-primitives/media"
|
|
21
|
+
import { makeEventListener } from "@solid-primitives/event-listener"
|
|
22
|
+
import {
|
|
23
|
+
ComponentProps,
|
|
24
|
+
createEffect,
|
|
25
|
+
createMemo,
|
|
26
|
+
createSignal,
|
|
27
|
+
on,
|
|
28
|
+
onCleanup,
|
|
29
|
+
onMount,
|
|
30
|
+
Show,
|
|
31
|
+
splitProps,
|
|
32
|
+
untrack,
|
|
33
|
+
} from "solid-js"
|
|
34
|
+
import { createDefaultOptions, styleVariables } from "../pierre"
|
|
35
|
+
import { markCommentedDiffLines, markCommentedFileLines } from "../pierre/commented-lines"
|
|
36
|
+
import { fixDiffSelection, findDiffSide, type DiffSelectionSide } from "../pierre/diff-selection"
|
|
37
|
+
import { createFileFind } from "../pierre/file-find"
|
|
38
|
+
import { applyViewerScheme, getViewerHost, getViewerRoot, observeViewerScheme } from "../pierre/file-runtime"
|
|
39
|
+
import {
|
|
40
|
+
findCodeSelectionSide,
|
|
41
|
+
findDiffLineNumber,
|
|
42
|
+
findElement,
|
|
43
|
+
findFileLineNumber,
|
|
44
|
+
readShadowLineSelection,
|
|
45
|
+
} from "../pierre/file-selection"
|
|
46
|
+
import {
|
|
47
|
+
diffExpectsRows,
|
|
48
|
+
INITIAL_VIEWER_RENDER_STATE,
|
|
49
|
+
POOL_INIT_TIMEOUT_MS,
|
|
50
|
+
readPoolState,
|
|
51
|
+
ROWS_READY_TIMEOUT_MS,
|
|
52
|
+
selectMountedSpec,
|
|
53
|
+
selectRevision,
|
|
54
|
+
transitionViewerRender,
|
|
55
|
+
type ViewerInstanceSpec,
|
|
56
|
+
type ViewerRenderEffect,
|
|
57
|
+
type ViewerRenderEvent,
|
|
58
|
+
type ViewerRenderRequest,
|
|
59
|
+
type ViewerTarget,
|
|
60
|
+
} from "../pierre/render-machine"
|
|
61
|
+
import {
|
|
62
|
+
createFileSelectionState,
|
|
63
|
+
selectPointerTracking,
|
|
64
|
+
selectSelectedRange,
|
|
65
|
+
transitionFileSelection,
|
|
66
|
+
type FileSelectionEffect,
|
|
67
|
+
type FileSelectionEvent,
|
|
68
|
+
} from "../pierre/selection-machine"
|
|
69
|
+
import type { CommentSelectionSample } from "../pierre/comment-selection-machine"
|
|
70
|
+
import { acquireVirtualizer, virtualizeLineThreshold, virtualMetrics } from "../pierre/virtualizer"
|
|
71
|
+
import { getWorkerPool } from "../pierre/worker"
|
|
72
|
+
import { FileCommentSelection } from "./file-comment-selection"
|
|
73
|
+
import { FileMedia, type FileMediaOptions } from "./file-media"
|
|
74
|
+
import { FileSearchBar } from "./file-search"
|
|
75
|
+
|
|
76
|
+
const codeMetrics = {
|
|
77
|
+
...DEFAULT_VIRTUAL_FILE_METRICS,
|
|
78
|
+
lineHeight: 24,
|
|
79
|
+
spacing: 0,
|
|
80
|
+
} satisfies Partial<VirtualFileMetrics>
|
|
81
|
+
|
|
82
|
+
type SharedProps<T> = {
|
|
83
|
+
annotations?: LineAnnotation<T>[] | DiffLineAnnotation<T>[]
|
|
84
|
+
selectedLines?: SelectedLineRange | null
|
|
85
|
+
commentedLines?: SelectedLineRange[]
|
|
86
|
+
onLineNumberSelectionEnd?: (selection: SelectedLineRange | null) => void
|
|
87
|
+
/** Offers an Add comment button on text selected in the code, and reports the lines it covers. */
|
|
88
|
+
onCommentSelection?: (range: SelectedLineRange) => void
|
|
89
|
+
onRendered?: () => void
|
|
90
|
+
class?: string
|
|
91
|
+
classList?: ComponentProps<"div">["classList"]
|
|
92
|
+
media?: FileMediaOptions
|
|
93
|
+
search?: FileSearchControl
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type FileSearchHandle = {
|
|
97
|
+
focus: () => void
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export type FileSearchControl = {
|
|
101
|
+
register: (handle: FileSearchHandle | null) => void
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export type TextFileProps<T = {}> = FileOptions<T, undefined> &
|
|
105
|
+
SharedProps<T> & {
|
|
106
|
+
mode: "text"
|
|
107
|
+
file: FileContents
|
|
108
|
+
annotations?: LineAnnotation<T>[]
|
|
109
|
+
preloadedDiff?: PreloadMultiFileDiffResult<T, undefined>
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
type DiffPreload<T> = PreloadMultiFileDiffResult<T, undefined> | PreloadFileDiffResult<T, undefined>
|
|
113
|
+
|
|
114
|
+
type DiffBaseProps<T> = FileDiffOptions<T, undefined> &
|
|
115
|
+
SharedProps<T> & {
|
|
116
|
+
mode: "diff"
|
|
117
|
+
annotations?: DiffLineAnnotation<T>[]
|
|
118
|
+
preloadedDiff?: DiffPreload<T>
|
|
119
|
+
virtualize?: boolean
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
type DiffPairProps<T> = DiffBaseProps<T> & {
|
|
123
|
+
before: FileContents
|
|
124
|
+
after: FileContents
|
|
125
|
+
fileDiff?: undefined
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
type DiffPatchProps<T> = DiffBaseProps<T> & {
|
|
129
|
+
fileDiff: FileDiffMetadata
|
|
130
|
+
before?: undefined
|
|
131
|
+
after?: undefined
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export type DiffFileProps<T = {}> = DiffPairProps<T> | DiffPatchProps<T>
|
|
135
|
+
|
|
136
|
+
export type FileProps<T = {}> = TextFileProps<T> | DiffFileProps<T>
|
|
137
|
+
|
|
138
|
+
const sharedKeys = [
|
|
139
|
+
"mode",
|
|
140
|
+
"media",
|
|
141
|
+
"class",
|
|
142
|
+
"classList",
|
|
143
|
+
"annotations",
|
|
144
|
+
"selectedLines",
|
|
145
|
+
"commentedLines",
|
|
146
|
+
"search",
|
|
147
|
+
"onLineSelected",
|
|
148
|
+
"onLineSelectionEnd",
|
|
149
|
+
"onLineNumberSelectionEnd",
|
|
150
|
+
"onCommentSelection",
|
|
151
|
+
"onRendered",
|
|
152
|
+
"preloadedDiff",
|
|
153
|
+
] as const
|
|
154
|
+
|
|
155
|
+
const textKeys = ["file", ...sharedKeys] as const
|
|
156
|
+
|
|
157
|
+
const diffKeys = ["fileDiff", "before", "after", "virtualize", ...sharedKeys] as const
|
|
158
|
+
|
|
159
|
+
// ---------------------------------------------------------------------------
|
|
160
|
+
// Shared viewer hook
|
|
161
|
+
// ---------------------------------------------------------------------------
|
|
162
|
+
|
|
163
|
+
type MouseHit = {
|
|
164
|
+
line: number | undefined
|
|
165
|
+
numberColumn: boolean
|
|
166
|
+
side: DiffSelectionSide | undefined
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
type SelectionCallbacks = {
|
|
170
|
+
onLineSelected?: (range: SelectedLineRange | null) => void
|
|
171
|
+
onLineSelectionEnd?: (range: SelectedLineRange | null) => void
|
|
172
|
+
onLineNumberSelectionEnd?: (range: SelectedLineRange | null) => void
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
type ViewerConfig = {
|
|
176
|
+
enableLineSelection: () => boolean
|
|
177
|
+
selectedLines: () => SelectedLineRange | null | undefined
|
|
178
|
+
callbacks: SelectionCallbacks
|
|
179
|
+
|
|
180
|
+
// mode-specific callbacks
|
|
181
|
+
lineForElement: (node: HTMLElement) => number | undefined
|
|
182
|
+
sideForElement?: (node: HTMLElement) => DiffSelectionSide | undefined
|
|
183
|
+
lineForNode: (node: Node | null) => number | undefined
|
|
184
|
+
sideForNode: (node: Node | null) => SelectedLineRange["side"]
|
|
185
|
+
/** Shows a range in Pierre, adjusted to the rows it has drawn. */
|
|
186
|
+
applySelection: (range: SelectedLineRange | null) => void
|
|
187
|
+
/** Adjusts a range Pierre reports before the machine stores it. */
|
|
188
|
+
normalize?: (range: SelectedLineRange | null) => SelectedLineRange | null | undefined
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
type PointerMove = {
|
|
192
|
+
type: "pointer.move"
|
|
193
|
+
buttons: number
|
|
194
|
+
line: number | undefined
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const POINTER_UP: FileSelectionEvent = Object.freeze({ type: "pointer.up" })
|
|
198
|
+
|
|
199
|
+
const POINTER_CANCELLED: FileSelectionEvent = Object.freeze({ type: "pointer.cancelled" })
|
|
200
|
+
|
|
201
|
+
function useFileViewer(config: ViewerConfig) {
|
|
202
|
+
let wrapper!: HTMLDivElement
|
|
203
|
+
let container!: HTMLDivElement
|
|
204
|
+
let overlay!: HTMLDivElement
|
|
205
|
+
let selection = createFileSelectionState()
|
|
206
|
+
// Pointer moves reuse one hit and one event so hovering and dragging allocate nothing per move.
|
|
207
|
+
const hit: MouseHit = { line: undefined, numberColumn: false, side: undefined }
|
|
208
|
+
const moveEvent: PointerMove = { type: "pointer.move", buttons: 0, line: undefined }
|
|
209
|
+
|
|
210
|
+
const getRoot = () => getViewerRoot(container)
|
|
211
|
+
const getHost = () => getViewerHost(container)
|
|
212
|
+
|
|
213
|
+
const find = createFileFind({
|
|
214
|
+
wrapper: () => wrapper,
|
|
215
|
+
overlay: () => overlay,
|
|
216
|
+
getRoot,
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
// -- selection machine --
|
|
220
|
+
|
|
221
|
+
const run = (effect: FileSelectionEffect) => {
|
|
222
|
+
if (effect.type === "apply-selection") return config.applySelection(effect.range)
|
|
223
|
+
|
|
224
|
+
if (effect.type === "notify-line-selected") return config.callbacks.onLineSelected?.(effect.range)
|
|
225
|
+
|
|
226
|
+
// Report the range as Pierre shows it: a diff drag that ends above its start is stored top to bottom.
|
|
227
|
+
const range = normalize(effect.range)
|
|
228
|
+
|
|
229
|
+
if (config.callbacks.onLineSelectionEnd) return config.callbacks.onLineSelectionEnd(range)
|
|
230
|
+
|
|
231
|
+
if (effect.fromNumberDrag) config.callbacks.onLineNumberSelectionEnd?.(range)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Applying a range makes Pierre report it back through `onLineSelected`, which dispatches again from inside `run`.
|
|
235
|
+
const dispatch = (event: FileSelectionEvent) => {
|
|
236
|
+
const step = transitionFileSelection(selection, event)
|
|
237
|
+
selection = step.state
|
|
238
|
+
step.effects.forEach(run)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const normalize = (range: SelectedLineRange | null) => {
|
|
242
|
+
const next = config.normalize?.(range)
|
|
243
|
+
|
|
244
|
+
return next === undefined ? range : next
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// -- pointer handlers --
|
|
248
|
+
|
|
249
|
+
const handlePointerDown = (event: PointerEvent) => {
|
|
250
|
+
if (event.button !== 0) return
|
|
251
|
+
fillMouseHit(hit, event, config.lineForElement, config.sideForElement)
|
|
252
|
+
dispatch({
|
|
253
|
+
type: "pointer.down",
|
|
254
|
+
button: event.button,
|
|
255
|
+
column: hit.numberColumn ? "numbers" : "text",
|
|
256
|
+
line: hit.line,
|
|
257
|
+
side: hit.side,
|
|
258
|
+
touch: event.pointerType === "touch",
|
|
259
|
+
})
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const handlePointerMove = (event: PointerEvent) => {
|
|
263
|
+
if (!selectPointerTracking(selection)) return
|
|
264
|
+
moveEvent.buttons = event.buttons
|
|
265
|
+
moveEvent.line = undefined
|
|
266
|
+
|
|
267
|
+
if ((event.buttons & 1) !== 0) {
|
|
268
|
+
fillMouseHit(hit, event, config.lineForElement)
|
|
269
|
+
moveEvent.line = hit.line
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
dispatch(moveEvent)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const handlePointerUp = () => {
|
|
276
|
+
if (!selectPointerTracking(selection)) return
|
|
277
|
+
dispatch(POINTER_UP)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const handlePointerCancel = () => {
|
|
281
|
+
if (!selectPointerTracking(selection)) return
|
|
282
|
+
dispatch(POINTER_CANCELLED)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// -- shared effects --
|
|
286
|
+
|
|
287
|
+
onMount(() => {
|
|
288
|
+
onCleanup(observeViewerScheme(getHost))
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
createEffect(() => {
|
|
292
|
+
const range = config.selectedLines() ?? null
|
|
293
|
+
untrack(() => dispatch({ type: "selection.controlled", range }))
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
createEffect(() => {
|
|
297
|
+
if (!config.enableLineSelection()) {
|
|
298
|
+
dispatch({ type: "selection.disabled" })
|
|
299
|
+
|
|
300
|
+
return
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
makeEventListener(container, "pointerdown", handlePointerDown)
|
|
304
|
+
makeEventListener(container, "pointermove", handlePointerMove)
|
|
305
|
+
makeEventListener(window, "pointerup", handlePointerUp)
|
|
306
|
+
makeEventListener(window, "pointercancel", handlePointerCancel)
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
onCleanup(() => dispatch({ type: "disposed" }))
|
|
310
|
+
|
|
311
|
+
return {
|
|
312
|
+
get wrapper() {
|
|
313
|
+
return wrapper
|
|
314
|
+
},
|
|
315
|
+
set wrapper(v: HTMLDivElement) {
|
|
316
|
+
wrapper = v
|
|
317
|
+
},
|
|
318
|
+
get container() {
|
|
319
|
+
return container
|
|
320
|
+
},
|
|
321
|
+
set container(v: HTMLDivElement) {
|
|
322
|
+
container = v
|
|
323
|
+
},
|
|
324
|
+
get overlay() {
|
|
325
|
+
return overlay
|
|
326
|
+
},
|
|
327
|
+
set overlay(v: HTMLDivElement) {
|
|
328
|
+
overlay = v
|
|
329
|
+
},
|
|
330
|
+
getRoot,
|
|
331
|
+
getHost,
|
|
332
|
+
find,
|
|
333
|
+
/** The text selected in the code, as the lines it covers and where it sits on screen. */
|
|
334
|
+
sampleTextSelection: (): CommentSelectionSample | undefined => {
|
|
335
|
+
const root = getRoot()
|
|
336
|
+
|
|
337
|
+
if (!root || !wrapper) return
|
|
338
|
+
|
|
339
|
+
const selected = readShadowLineSelection({
|
|
340
|
+
root,
|
|
341
|
+
lineForNode: config.lineForNode,
|
|
342
|
+
sideForNode: config.sideForNode,
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
const range = selected && normalize(selected.range)
|
|
346
|
+
|
|
347
|
+
if (!selected || !range) return
|
|
348
|
+
|
|
349
|
+
const bounds = visibleBounds(wrapper)
|
|
350
|
+
const anchor = selectionAnchor(selected.text, selected.backward)
|
|
351
|
+
|
|
352
|
+
return {
|
|
353
|
+
range,
|
|
354
|
+
bounds,
|
|
355
|
+
anchor: anchor && anchor.bottom > bounds.top && anchor.top < bounds.bottom ? anchor : undefined,
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
/** Shows the current range again once Pierre has drawn fresh rows. */
|
|
359
|
+
reapplySelection: () => config.applySelection(selectSelectedRange(selection)),
|
|
360
|
+
/** Pierre's own line-number selection, passed in its options. */
|
|
361
|
+
lineCallbacks: {
|
|
362
|
+
onLineSelected: (range: SelectedLineRange | null) =>
|
|
363
|
+
dispatch({ type: "pierre.line-selected", range: normalize(range) }),
|
|
364
|
+
onLineSelectionEnd: (range: SelectedLineRange | null) =>
|
|
365
|
+
dispatch({ type: "pierre.selection-ended", range: normalize(range) }),
|
|
366
|
+
},
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
type Viewer = ReturnType<typeof useFileViewer>
|
|
371
|
+
|
|
372
|
+
type RenderTarget = {
|
|
373
|
+
cleanUp: () => void
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
type VirtualStrategy = {
|
|
377
|
+
get: () => Virtualizer | undefined
|
|
378
|
+
cleanup: () => void
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function useSearchHandle(opts: {
|
|
382
|
+
search: () => FileSearchControl | undefined
|
|
383
|
+
find: ReturnType<typeof createFileFind>
|
|
384
|
+
}) {
|
|
385
|
+
createEffect(() => {
|
|
386
|
+
const search = opts.search()
|
|
387
|
+
|
|
388
|
+
if (!search) return
|
|
389
|
+
|
|
390
|
+
const handle = {
|
|
391
|
+
focus: () => opts.find.focus(),
|
|
392
|
+
} satisfies FileSearchHandle
|
|
393
|
+
|
|
394
|
+
search.register(handle)
|
|
395
|
+
onCleanup(() => search.register(null))
|
|
396
|
+
})
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
type ViewerRenderConfig<I extends RenderTarget, O> = {
|
|
400
|
+
viewer: Viewer
|
|
401
|
+
/** Pierre options; a change redraws, and a draw always uses the latest ones. */
|
|
402
|
+
options: () => O
|
|
403
|
+
/** Reads every other input a draw depends on; a change requests a new draw. */
|
|
404
|
+
request: () => ViewerRenderRequest
|
|
405
|
+
create: (spec: ViewerInstanceSpec, options: O) => I
|
|
406
|
+
/** Applies options to a kept instance and returns whether its rows must render again. */
|
|
407
|
+
update: (instance: I, options: O) => boolean
|
|
408
|
+
draw: (instance: I, spec: ViewerInstanceSpec, forceRender: boolean) => void
|
|
409
|
+
isReady: (root: ShadowRoot) => boolean
|
|
410
|
+
annotations: () => unknown[]
|
|
411
|
+
applyAnnotations: (instance: I) => void
|
|
412
|
+
commentedLines: () => SelectedLineRange[]
|
|
413
|
+
markCommented: (root: ShadowRoot, ranges: SelectedLineRange[]) => void
|
|
414
|
+
/** Scrolls a range into a virtualized window; returns false while rows cannot be measured yet. */
|
|
415
|
+
reveal?: (instance: I, spec: ViewerInstanceSpec, range: SelectedLineRange) => boolean
|
|
416
|
+
onRendered: () => void
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
type ViewerRender<I> = {
|
|
420
|
+
instance: () => I | undefined
|
|
421
|
+
requestReveal: (range: SelectedLineRange | undefined) => void
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function useViewerRender<I extends RenderTarget, O>(config: ViewerRenderConfig<I, O>): ViewerRender<I> {
|
|
425
|
+
const viewer = config.viewer
|
|
426
|
+
let state = INITIAL_VIEWER_RENDER_STATE
|
|
427
|
+
let instance: I | undefined
|
|
428
|
+
let observer: MutationObserver | undefined
|
|
429
|
+
let poolTimer: ReturnType<typeof setTimeout> | undefined
|
|
430
|
+
let rowsTimer: ReturnType<typeof setTimeout> | undefined
|
|
431
|
+
let settleFrame: number | undefined
|
|
432
|
+
let findFrame: number | undefined
|
|
433
|
+
let releaseHeight: VoidFunction | undefined
|
|
434
|
+
const [revision, setRevision] = createSignal(selectRevision(state))
|
|
435
|
+
|
|
436
|
+
const cancelAsync = () => {
|
|
437
|
+
observer?.disconnect()
|
|
438
|
+
observer = undefined
|
|
439
|
+
clearTimeout(poolTimer)
|
|
440
|
+
clearTimeout(rowsTimer)
|
|
441
|
+
poolTimer = undefined
|
|
442
|
+
rowsTimer = undefined
|
|
443
|
+
|
|
444
|
+
if (settleFrame !== undefined) cancelAnimationFrame(settleFrame)
|
|
445
|
+
settleFrame = undefined
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const watchRows = (attempt: number) => {
|
|
449
|
+
const done = () => {
|
|
450
|
+
observer?.disconnect()
|
|
451
|
+
observer = undefined
|
|
452
|
+
clearTimeout(rowsTimer)
|
|
453
|
+
rowsTimer = undefined
|
|
454
|
+
dispatch({ type: "rows.ready", attempt })
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const watchRoot = (root: ShadowRoot) => {
|
|
458
|
+
observer?.disconnect()
|
|
459
|
+
observer = new MutationObserver(() => {
|
|
460
|
+
if (config.isReady(root)) done()
|
|
461
|
+
})
|
|
462
|
+
observer.observe(root, { childList: true, subtree: true })
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const root = viewer.getRoot()
|
|
466
|
+
|
|
467
|
+
if (root && config.isReady(root)) return done()
|
|
468
|
+
|
|
469
|
+
rowsTimer = setTimeout(() => {
|
|
470
|
+
rowsTimer = undefined
|
|
471
|
+
dispatch({ type: "rows.timed-out", attempt })
|
|
472
|
+
}, ROWS_READY_TIMEOUT_MS)
|
|
473
|
+
|
|
474
|
+
if (root) return watchRoot(root)
|
|
475
|
+
|
|
476
|
+
observer = new MutationObserver(() => {
|
|
477
|
+
const next = viewer.getRoot()
|
|
478
|
+
|
|
479
|
+
if (!next) return
|
|
480
|
+
|
|
481
|
+
if (config.isReady(next)) return done()
|
|
482
|
+
watchRoot(next)
|
|
483
|
+
})
|
|
484
|
+
observer.observe(viewer.container, { childList: true, subtree: true })
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const run = (effect: ViewerRenderEffect) => {
|
|
488
|
+
if (effect.type === "cancel-async") return cancelAsync()
|
|
489
|
+
|
|
490
|
+
if (effect.type === "await-pool") {
|
|
491
|
+
poolTimer = setTimeout(() => {
|
|
492
|
+
poolTimer = undefined
|
|
493
|
+
dispatch({ type: "pool.timed-out", attempt: effect.attempt })
|
|
494
|
+
}, POOL_INIT_TIMEOUT_MS)
|
|
495
|
+
void effect.pool.initialize().then(
|
|
496
|
+
() => dispatch({ type: "pool.settled", attempt: effect.attempt, ok: true }),
|
|
497
|
+
() => dispatch({ type: "pool.settled", attempt: effect.attempt, ok: false }),
|
|
498
|
+
)
|
|
499
|
+
|
|
500
|
+
return
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
if (effect.type === "preserve-height") {
|
|
504
|
+
releaseHeight ??= preserve(viewer)
|
|
505
|
+
|
|
506
|
+
return
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (effect.type === "release-height") {
|
|
510
|
+
releaseHeight?.()
|
|
511
|
+
releaseHeight = undefined
|
|
512
|
+
|
|
513
|
+
return
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
if (effect.type === "draw") {
|
|
517
|
+
if (effect.action === "reset") {
|
|
518
|
+
instance?.cleanUp()
|
|
519
|
+
instance = undefined
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
const options = config.options()
|
|
523
|
+
const forceRender = instance ? config.update(instance, options) : false
|
|
524
|
+
|
|
525
|
+
if (!instance) {
|
|
526
|
+
viewer.container.innerHTML = ""
|
|
527
|
+
instance = config.create(effect.spec, options)
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
config.draw(instance, effect.spec, forceRender)
|
|
531
|
+
applyViewerScheme(viewer.getHost())
|
|
532
|
+
|
|
533
|
+
return
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if (effect.type === "sync-annotations")
|
|
537
|
+
return dispatch({ type: "annotations.synced", count: config.annotations().length })
|
|
538
|
+
|
|
539
|
+
if (effect.type === "apply-annotations") {
|
|
540
|
+
if (!instance) return
|
|
541
|
+
config.applyAnnotations(instance)
|
|
542
|
+
|
|
543
|
+
if (findFrame !== undefined) cancelAnimationFrame(findFrame)
|
|
544
|
+
findFrame = requestAnimationFrame(() => {
|
|
545
|
+
findFrame = undefined
|
|
546
|
+
viewer.find.refresh({ reset: true })
|
|
547
|
+
})
|
|
548
|
+
|
|
549
|
+
return
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
if (effect.type === "watch-rows") return watchRows(effect.attempt)
|
|
553
|
+
|
|
554
|
+
if (effect.type === "schedule-settle-frame") {
|
|
555
|
+
settleFrame = requestAnimationFrame(() => {
|
|
556
|
+
settleFrame = undefined
|
|
557
|
+
dispatch({ type: "settle.frame", attempt: effect.attempt })
|
|
558
|
+
})
|
|
559
|
+
|
|
560
|
+
return
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (effect.type === "reapply-selection") return viewer.reapplySelection()
|
|
564
|
+
|
|
565
|
+
if (effect.type === "try-reveal") {
|
|
566
|
+
const spec = selectMountedSpec(state)
|
|
567
|
+
|
|
568
|
+
if (!instance || !spec) return
|
|
569
|
+
|
|
570
|
+
if (!config.reveal || config.reveal(instance, spec, effect.range)) dispatch({ type: "reveal.completed" })
|
|
571
|
+
|
|
572
|
+
return
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (effect.type === "refresh-find") return viewer.find.refresh({ reset: true })
|
|
576
|
+
|
|
577
|
+
if (effect.type === "notify-rendered") return config.onRendered()
|
|
578
|
+
|
|
579
|
+
instance?.cleanUp()
|
|
580
|
+
instance = undefined
|
|
581
|
+
|
|
582
|
+
if (findFrame !== undefined) cancelAnimationFrame(findFrame)
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// Effects can dispatch again synchronously (rows already drawn, annotations synced after a draw).
|
|
586
|
+
const dispatch = (event: ViewerRenderEvent) => {
|
|
587
|
+
const step = transitionViewerRender(state, event)
|
|
588
|
+
const changed = selectRevision(step.state) !== selectRevision(state)
|
|
589
|
+
state = step.state
|
|
590
|
+
step.effects.forEach(run)
|
|
591
|
+
|
|
592
|
+
if (changed && state.status !== "disposed") setRevision(selectRevision(state))
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
createEffect(() => {
|
|
596
|
+
config.options()
|
|
597
|
+
const request = config.request()
|
|
598
|
+
untrack(() => dispatch({ type: "render.requested", request }))
|
|
599
|
+
})
|
|
600
|
+
|
|
601
|
+
// Each draw syncs annotations itself, so only later changes dispatch here.
|
|
602
|
+
createEffect(
|
|
603
|
+
on(config.annotations, (annotations) => dispatch({ type: "annotations.synced", count: annotations.length }), {
|
|
604
|
+
defer: true,
|
|
605
|
+
}),
|
|
606
|
+
)
|
|
607
|
+
|
|
608
|
+
createEffect(() => {
|
|
609
|
+
revision()
|
|
610
|
+
const ranges = config.commentedLines()
|
|
611
|
+
const root = viewer.getRoot()
|
|
612
|
+
|
|
613
|
+
if (!root) return
|
|
614
|
+
|
|
615
|
+
if (ranges.length === 0) {
|
|
616
|
+
config.markCommented(root, ranges)
|
|
617
|
+
|
|
618
|
+
return
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
let frame: number | undefined
|
|
622
|
+
|
|
623
|
+
const mark = () => {
|
|
624
|
+
if (frame !== undefined) cancelAnimationFrame(frame)
|
|
625
|
+
frame = requestAnimationFrame(() => {
|
|
626
|
+
frame = undefined
|
|
627
|
+
config.markCommented(root, ranges)
|
|
628
|
+
})
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
const watch = new MutationObserver(mark)
|
|
632
|
+
|
|
633
|
+
watch.observe(root, { childList: true, subtree: true })
|
|
634
|
+
mark()
|
|
635
|
+
|
|
636
|
+
onCleanup(() => {
|
|
637
|
+
watch.disconnect()
|
|
638
|
+
|
|
639
|
+
if (frame !== undefined) cancelAnimationFrame(frame)
|
|
640
|
+
})
|
|
641
|
+
})
|
|
642
|
+
|
|
643
|
+
onCleanup(() => dispatch({ type: "disposed" }))
|
|
644
|
+
|
|
645
|
+
return {
|
|
646
|
+
instance: () => instance,
|
|
647
|
+
requestReveal: (range) => dispatch({ type: "reveal.requested", range }),
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
function preserve(viewer: Viewer) {
|
|
652
|
+
if (!viewer.container.firstElementChild) return () => {}
|
|
653
|
+
|
|
654
|
+
const root = scrollParent(viewer.wrapper)
|
|
655
|
+
|
|
656
|
+
if (!root) return () => {}
|
|
657
|
+
|
|
658
|
+
const high = viewer.container.getBoundingClientRect().height
|
|
659
|
+
|
|
660
|
+
if (!high) return () => {}
|
|
661
|
+
|
|
662
|
+
const top = viewer.wrapper.getBoundingClientRect().top - root.getBoundingClientRect().top
|
|
663
|
+
const prev = viewer.container.style.minHeight
|
|
664
|
+
viewer.container.style.minHeight = `${Math.ceil(high)}px`
|
|
665
|
+
|
|
666
|
+
let done = false
|
|
667
|
+
|
|
668
|
+
return () => {
|
|
669
|
+
if (done) return
|
|
670
|
+
done = true
|
|
671
|
+
viewer.container.style.minHeight = prev
|
|
672
|
+
|
|
673
|
+
const next = viewer.wrapper.getBoundingClientRect().top - root.getBoundingClientRect().top
|
|
674
|
+
const delta = next - top
|
|
675
|
+
|
|
676
|
+
if (delta) root.scrollTop += delta
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
function scrollParent(el: HTMLElement): HTMLElement | undefined {
|
|
681
|
+
let parent = el.parentElement
|
|
682
|
+
|
|
683
|
+
while (parent) {
|
|
684
|
+
const style = getComputedStyle(parent)
|
|
685
|
+
|
|
686
|
+
if (style.overflowY === "auto" || style.overflowY === "scroll") return parent
|
|
687
|
+
parent = parent.parentElement
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
function createLocalVirtualStrategy(host: () => HTMLDivElement | undefined, enabled: () => boolean): VirtualStrategy {
|
|
692
|
+
let virtualizer: Virtualizer | undefined
|
|
693
|
+
let root: Document | HTMLElement | undefined
|
|
694
|
+
|
|
695
|
+
const release = () => {
|
|
696
|
+
virtualizer?.cleanUp()
|
|
697
|
+
virtualizer = undefined
|
|
698
|
+
root = undefined
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
return {
|
|
702
|
+
get: () => {
|
|
703
|
+
if (!enabled()) {
|
|
704
|
+
release()
|
|
705
|
+
|
|
706
|
+
return
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
if (typeof document === "undefined") return
|
|
710
|
+
|
|
711
|
+
const wrapper = host()
|
|
712
|
+
|
|
713
|
+
if (!wrapper) return
|
|
714
|
+
|
|
715
|
+
const next = scrollParent(wrapper) ?? document
|
|
716
|
+
|
|
717
|
+
if (virtualizer && root === next) return virtualizer
|
|
718
|
+
|
|
719
|
+
release()
|
|
720
|
+
virtualizer = new Virtualizer()
|
|
721
|
+
root = next
|
|
722
|
+
virtualizer.setup(next, next instanceof Document ? undefined : wrapper)
|
|
723
|
+
|
|
724
|
+
return virtualizer
|
|
725
|
+
},
|
|
726
|
+
cleanup: release,
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
function createSharedVirtualStrategy(host: () => HTMLDivElement | undefined, enabled: () => boolean): VirtualStrategy {
|
|
731
|
+
let shared: NonNullable<ReturnType<typeof acquireVirtualizer>> | undefined
|
|
732
|
+
|
|
733
|
+
const release = () => {
|
|
734
|
+
shared?.release()
|
|
735
|
+
shared = undefined
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
return {
|
|
739
|
+
get: () => {
|
|
740
|
+
if (!enabled()) {
|
|
741
|
+
release()
|
|
742
|
+
|
|
743
|
+
return
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
if (shared) return shared.virtualizer
|
|
747
|
+
|
|
748
|
+
const container = host()
|
|
749
|
+
|
|
750
|
+
if (!container) return
|
|
751
|
+
|
|
752
|
+
const result = acquireVirtualizer(container)
|
|
753
|
+
|
|
754
|
+
if (!result) return
|
|
755
|
+
shared = result
|
|
756
|
+
|
|
757
|
+
return result.virtualizer
|
|
758
|
+
},
|
|
759
|
+
cleanup: release,
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
function parseLine(node: HTMLElement) {
|
|
764
|
+
if (!node.dataset.line) return
|
|
765
|
+
const value = parseInt(node.dataset.line, 10)
|
|
766
|
+
|
|
767
|
+
if (Number.isNaN(value)) return
|
|
768
|
+
|
|
769
|
+
return value
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
function parseColumnLine(node: HTMLElement) {
|
|
773
|
+
if (!node.dataset.columnNumber) return
|
|
774
|
+
const value = parseInt(node.dataset.columnNumber, 10)
|
|
775
|
+
|
|
776
|
+
if (Number.isNaN(value)) return
|
|
777
|
+
|
|
778
|
+
return value
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
function fillMouseHit(
|
|
782
|
+
hit: MouseHit,
|
|
783
|
+
event: MouseEvent,
|
|
784
|
+
line: (node: HTMLElement) => number | undefined,
|
|
785
|
+
side?: (node: HTMLElement) => DiffSelectionSide | undefined,
|
|
786
|
+
) {
|
|
787
|
+
hit.line = undefined
|
|
788
|
+
hit.numberColumn = false
|
|
789
|
+
hit.side = undefined
|
|
790
|
+
|
|
791
|
+
for (const item of event.composedPath()) {
|
|
792
|
+
if (!(item instanceof HTMLElement)) continue
|
|
793
|
+
|
|
794
|
+
hit.numberColumn = hit.numberColumn || item.dataset.columnNumber != null
|
|
795
|
+
|
|
796
|
+
// Gutter cells carry the line number only in `data-column-number`.
|
|
797
|
+
if (hit.line === undefined) hit.line = line(item) ?? parseColumnLine(item)
|
|
798
|
+
|
|
799
|
+
if (hit.side === undefined && side) hit.side = side(item)
|
|
800
|
+
|
|
801
|
+
if (hit.numberColumn && hit.line !== undefined && (side == null || hit.side !== undefined)) break
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/** The part of `el` left on screen by the window and every ancestor that clips it. */
|
|
806
|
+
function visibleBounds(el: HTMLElement) {
|
|
807
|
+
const box = el.getBoundingClientRect()
|
|
808
|
+
|
|
809
|
+
const bounds = {
|
|
810
|
+
top: Math.max(box.top, 0),
|
|
811
|
+
bottom: Math.min(box.bottom, window.innerHeight),
|
|
812
|
+
left: box.left,
|
|
813
|
+
right: box.right,
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
for (let parent = el.parentElement; parent; parent = parent.parentElement) {
|
|
817
|
+
if (getComputedStyle(parent).overflowY === "visible") continue
|
|
818
|
+
const clip = parent.getBoundingClientRect()
|
|
819
|
+
bounds.top = Math.max(bounds.top, clip.top)
|
|
820
|
+
bounds.bottom = Math.min(bounds.bottom, clip.bottom)
|
|
821
|
+
bounds.left = Math.max(bounds.left, clip.left)
|
|
822
|
+
bounds.right = Math.min(bounds.right, clip.right)
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
return bounds
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/** The selected text's box, from its first and last glyphs rather than the line boxes the range also covers. */
|
|
829
|
+
function selectionAnchor(range: Range, backward: boolean) {
|
|
830
|
+
const first = edgeRect(range, false)
|
|
831
|
+
const last = edgeRect(range, true)
|
|
832
|
+
|
|
833
|
+
if (!first || !last) return
|
|
834
|
+
|
|
835
|
+
return {
|
|
836
|
+
top: first.top,
|
|
837
|
+
bottom: last.bottom,
|
|
838
|
+
start: first.left,
|
|
839
|
+
end: last.right,
|
|
840
|
+
single: last.top < first.bottom - first.height / 2,
|
|
841
|
+
backward,
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
function edgeRect(range: Range, end: boolean) {
|
|
846
|
+
const node = end ? range.endContainer : range.startContainer
|
|
847
|
+
const offset = end ? range.endOffset : range.startOffset
|
|
848
|
+
|
|
849
|
+
if (node instanceof Text) {
|
|
850
|
+
const glyph = new Range()
|
|
851
|
+
glyph.setStart(node, end ? Math.max(0, offset - 1) : offset)
|
|
852
|
+
glyph.setEnd(node, end ? offset : Math.min(node.length, offset + 1))
|
|
853
|
+
const rect = glyph.getBoundingClientRect()
|
|
854
|
+
|
|
855
|
+
if (rect.height > 0) return rect
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
const rects = [...range.getClientRects()].filter((rect) => rect.height > 0)
|
|
859
|
+
|
|
860
|
+
return end ? rects.at(-1) : rects[0]
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
function diffMouseSide(node: HTMLElement) {
|
|
864
|
+
const type = node.dataset.lineType
|
|
865
|
+
|
|
866
|
+
if (type === "change-deletion") return "deletions" satisfies DiffSelectionSide
|
|
867
|
+
|
|
868
|
+
if (type === "change-addition" || type === "change-additions") return "additions" satisfies DiffSelectionSide
|
|
869
|
+
|
|
870
|
+
if (node.dataset.code == null) return
|
|
871
|
+
|
|
872
|
+
return node.hasAttribute("data-deletions") ? "deletions" : "additions"
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
function diffSelectionSide(node: Node | null) {
|
|
876
|
+
const el = findElement(node)
|
|
877
|
+
|
|
878
|
+
if (!el) return
|
|
879
|
+
|
|
880
|
+
return findDiffSide(el)
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
// ---------------------------------------------------------------------------
|
|
884
|
+
// Shared JSX shell
|
|
885
|
+
// ---------------------------------------------------------------------------
|
|
886
|
+
|
|
887
|
+
function ViewerShell(props: {
|
|
888
|
+
mode: "text" | "diff"
|
|
889
|
+
viewer: ReturnType<typeof useFileViewer>
|
|
890
|
+
class: string | undefined
|
|
891
|
+
classList: ComponentProps<"div">["classList"] | undefined
|
|
892
|
+
onCommentSelection: ((range: SelectedLineRange) => void) | undefined
|
|
893
|
+
}) {
|
|
894
|
+
return (
|
|
895
|
+
<div
|
|
896
|
+
data-component="file"
|
|
897
|
+
data-mode={props.mode}
|
|
898
|
+
dir="ltr"
|
|
899
|
+
style={styleVariables}
|
|
900
|
+
class="relative select-text"
|
|
901
|
+
classList={{
|
|
902
|
+
...props.classList,
|
|
903
|
+
[props.class ?? ""]: !!props.class,
|
|
904
|
+
}}
|
|
905
|
+
ref={(el) => (props.viewer.wrapper = el)}
|
|
906
|
+
tabIndex={-1}
|
|
907
|
+
onPointerDown={props.viewer.find.onPointerDown}
|
|
908
|
+
onFocus={props.viewer.find.onFocus}
|
|
909
|
+
>
|
|
910
|
+
<Show when={props.viewer.find.open()}>
|
|
911
|
+
<FileSearchBar
|
|
912
|
+
pos={props.viewer.find.pos()}
|
|
913
|
+
query={props.viewer.find.query()}
|
|
914
|
+
count={props.viewer.find.count()}
|
|
915
|
+
index={props.viewer.find.index()}
|
|
916
|
+
setInput={props.viewer.find.setInput}
|
|
917
|
+
onInput={props.viewer.find.setQuery}
|
|
918
|
+
onKeyDown={props.viewer.find.onInputKeyDown}
|
|
919
|
+
onClose={props.viewer.find.close}
|
|
920
|
+
onPrev={() => props.viewer.find.next(-1)}
|
|
921
|
+
onNext={() => props.viewer.find.next(1)}
|
|
922
|
+
/>
|
|
923
|
+
</Show>
|
|
924
|
+
<div ref={(el) => (props.viewer.container = el)} />
|
|
925
|
+
<div ref={(el) => (props.viewer.overlay = el)} class="pointer-events-none absolute inset-0 z-0" />
|
|
926
|
+
<Show when={props.onCommentSelection}>
|
|
927
|
+
{(onComment) => (
|
|
928
|
+
<FileCommentSelection
|
|
929
|
+
wrapper={props.viewer.wrapper}
|
|
930
|
+
sample={props.viewer.sampleTextSelection}
|
|
931
|
+
onComment={(range) => onComment()(range)}
|
|
932
|
+
/>
|
|
933
|
+
)}
|
|
934
|
+
</Show>
|
|
935
|
+
</div>
|
|
936
|
+
)
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
// ---------------------------------------------------------------------------
|
|
940
|
+
// TextViewer
|
|
941
|
+
// ---------------------------------------------------------------------------
|
|
942
|
+
|
|
943
|
+
function TextViewer<T>(props: TextFileProps<T>) {
|
|
944
|
+
let viewer!: Viewer
|
|
945
|
+
let render!: ViewerRender<PierreFile<T> | VirtualizedFile<T>>
|
|
946
|
+
|
|
947
|
+
const [local, others] = splitProps(props, textKeys)
|
|
948
|
+
|
|
949
|
+
const lineCount = () => {
|
|
950
|
+
const value = local.file.contents
|
|
951
|
+
const total = value.split("\n").length - (value.endsWith("\n") ? 1 : 0)
|
|
952
|
+
|
|
953
|
+
return Math.max(1, total)
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
const virtual = createMemo(() => lineCount() > virtualizeLineThreshold)
|
|
957
|
+
|
|
958
|
+
const virtuals = createLocalVirtualStrategy(() => viewer.wrapper, virtual)
|
|
959
|
+
|
|
960
|
+
// Rows that are not drawn yet cannot show a range; the machine keeps it and the ready callback applies it again.
|
|
961
|
+
const applySelection = (range: SelectedLineRange | null) => {
|
|
962
|
+
const current = render.instance()
|
|
963
|
+
|
|
964
|
+
if (!current) return
|
|
965
|
+
|
|
966
|
+
if (virtual()) return current.setSelectedLines(range)
|
|
967
|
+
|
|
968
|
+
const root = viewer.getRoot()
|
|
969
|
+
|
|
970
|
+
if (!root) return
|
|
971
|
+
|
|
972
|
+
const total = lineCount()
|
|
973
|
+
|
|
974
|
+
if (root.querySelectorAll("[data-line]").length < total) return
|
|
975
|
+
|
|
976
|
+
if (!range) return current.setSelectedLines(null)
|
|
977
|
+
|
|
978
|
+
const start = Math.min(range.start, range.end)
|
|
979
|
+
const end = Math.max(range.start, range.end)
|
|
980
|
+
|
|
981
|
+
if (start < 1 || end > total) return current.setSelectedLines(null)
|
|
982
|
+
|
|
983
|
+
if (!root.querySelector(`[data-line="${start}"]`) || !root.querySelector(`[data-line="${end}"]`))
|
|
984
|
+
return current.setSelectedLines(null)
|
|
985
|
+
|
|
986
|
+
const normalized = (() => {
|
|
987
|
+
if (range.endSide != null) return { start: range.start, end: range.end }
|
|
988
|
+
|
|
989
|
+
if (range.side !== "deletions") return range
|
|
990
|
+
|
|
991
|
+
if (root.querySelector("[data-deletions]") != null) return range
|
|
992
|
+
|
|
993
|
+
return { start: range.start, end: range.end }
|
|
994
|
+
})()
|
|
995
|
+
|
|
996
|
+
current.setSelectedLines(normalized)
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
viewer = useFileViewer({
|
|
1000
|
+
enableLineSelection: () => props.enableLineSelection === true,
|
|
1001
|
+
selectedLines: () => local.selectedLines,
|
|
1002
|
+
callbacks: local,
|
|
1003
|
+
lineForElement: parseLine,
|
|
1004
|
+
lineForNode: findFileLineNumber,
|
|
1005
|
+
sideForNode: findCodeSelectionSide,
|
|
1006
|
+
applySelection,
|
|
1007
|
+
})
|
|
1008
|
+
|
|
1009
|
+
const options = createMemo(() => ({
|
|
1010
|
+
...createDefaultOptions<T>("unified"),
|
|
1011
|
+
...others,
|
|
1012
|
+
...viewer.lineCallbacks,
|
|
1013
|
+
}))
|
|
1014
|
+
|
|
1015
|
+
useSearchHandle({
|
|
1016
|
+
search: () => local.search,
|
|
1017
|
+
find: viewer.find,
|
|
1018
|
+
})
|
|
1019
|
+
|
|
1020
|
+
// Cleanups run in reverse, so the instance is cleaned up before the virtualizer it uses.
|
|
1021
|
+
onCleanup(() => virtuals.cleanup())
|
|
1022
|
+
|
|
1023
|
+
// An instance keeps the virtualizer and pool it was built with; options and contents update in place.
|
|
1024
|
+
render = useViewerRender({
|
|
1025
|
+
viewer,
|
|
1026
|
+
options,
|
|
1027
|
+
request: () => {
|
|
1028
|
+
const workerPool = getWorkerPool()
|
|
1029
|
+
|
|
1030
|
+
return {
|
|
1031
|
+
spec: {
|
|
1032
|
+
virtualizer: virtuals.get(),
|
|
1033
|
+
workerPool,
|
|
1034
|
+
hunkSeparators: undefined,
|
|
1035
|
+
target: { kind: "text", file: local.file },
|
|
1036
|
+
},
|
|
1037
|
+
pool: readPoolState(workerPool),
|
|
1038
|
+
settleFrames: 0,
|
|
1039
|
+
preserveHeight: false,
|
|
1040
|
+
expectsRows: true,
|
|
1041
|
+
}
|
|
1042
|
+
},
|
|
1043
|
+
create: (spec, opts) =>
|
|
1044
|
+
spec.virtualizer
|
|
1045
|
+
? new VirtualizedFile<T>(opts, spec.virtualizer, codeMetrics, spec.workerPool)
|
|
1046
|
+
: new PierreFile<T>(opts, spec.workerPool),
|
|
1047
|
+
update: (instance, opts) => {
|
|
1048
|
+
const forceRender = !areOptionsEqual(instance.options, opts)
|
|
1049
|
+
instance.setOptions(opts)
|
|
1050
|
+
|
|
1051
|
+
return forceRender
|
|
1052
|
+
},
|
|
1053
|
+
draw: (instance, spec, forceRender) => {
|
|
1054
|
+
if (spec.target.kind !== "text") return
|
|
1055
|
+
instance.render({ file: spec.target.file, forceRender, lineAnnotations: [], containerWrapper: viewer.container })
|
|
1056
|
+
},
|
|
1057
|
+
isReady: (root) => {
|
|
1058
|
+
if (virtual()) return root.querySelector("[data-line]") != null
|
|
1059
|
+
|
|
1060
|
+
return root.querySelectorAll("[data-line]").length >= lineCount()
|
|
1061
|
+
},
|
|
1062
|
+
annotations: () => local.annotations ?? [],
|
|
1063
|
+
applyAnnotations: (instance) => {
|
|
1064
|
+
instance.setLineAnnotations(local.annotations ?? [])
|
|
1065
|
+
instance.rerender()
|
|
1066
|
+
},
|
|
1067
|
+
commentedLines: () => local.commentedLines ?? [],
|
|
1068
|
+
markCommented: markCommentedFileLines,
|
|
1069
|
+
onRendered: () => local.onRendered?.(),
|
|
1070
|
+
})
|
|
1071
|
+
|
|
1072
|
+
return (
|
|
1073
|
+
<ViewerShell
|
|
1074
|
+
mode="text"
|
|
1075
|
+
viewer={viewer}
|
|
1076
|
+
class={local.class}
|
|
1077
|
+
classList={local.classList}
|
|
1078
|
+
onCommentSelection={local.onCommentSelection}
|
|
1079
|
+
/>
|
|
1080
|
+
)
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
// ---------------------------------------------------------------------------
|
|
1084
|
+
// DiffViewer
|
|
1085
|
+
// ---------------------------------------------------------------------------
|
|
1086
|
+
|
|
1087
|
+
function DiffViewer<T>(props: DiffFileProps<T>) {
|
|
1088
|
+
let viewer!: Viewer
|
|
1089
|
+
let render!: ViewerRender<FileDiff<T>>
|
|
1090
|
+
|
|
1091
|
+
const [local, others] = splitProps(props, diffKeys)
|
|
1092
|
+
|
|
1093
|
+
const mobile = createMediaQuery("(max-width: 640px)")
|
|
1094
|
+
|
|
1095
|
+
// Rows outside a virtualized window are not in the DOM yet; keep the range so Pierre applies it when they render.
|
|
1096
|
+
const fixSelection = (range: SelectedLineRange | null) => {
|
|
1097
|
+
const fixed = fixDiffSelection(viewer.getRoot(), range)
|
|
1098
|
+
|
|
1099
|
+
return fixed === null && render.instance() instanceof VirtualizedFileDiff ? range : fixed
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
const applySelection = (range: SelectedLineRange | null) => {
|
|
1103
|
+
const active = render.instance()
|
|
1104
|
+
|
|
1105
|
+
if (!active) return
|
|
1106
|
+
const fixed = fixSelection(range)
|
|
1107
|
+
|
|
1108
|
+
if (fixed !== undefined) active.setSelectedLines(fixed)
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
viewer = useFileViewer({
|
|
1112
|
+
enableLineSelection: () => props.enableLineSelection === true,
|
|
1113
|
+
selectedLines: () => local.selectedLines,
|
|
1114
|
+
callbacks: local,
|
|
1115
|
+
lineForElement: findDiffLineNumber,
|
|
1116
|
+
sideForElement: diffMouseSide,
|
|
1117
|
+
lineForNode: findDiffLineNumber,
|
|
1118
|
+
sideForNode: diffSelectionSide,
|
|
1119
|
+
applySelection,
|
|
1120
|
+
normalize: fixSelection,
|
|
1121
|
+
})
|
|
1122
|
+
|
|
1123
|
+
const virtuals = createSharedVirtualStrategy(
|
|
1124
|
+
() => viewer.container,
|
|
1125
|
+
() => local.virtualize !== false,
|
|
1126
|
+
)
|
|
1127
|
+
|
|
1128
|
+
const large = createMemo(() => {
|
|
1129
|
+
if (local.fileDiff) {
|
|
1130
|
+
const before = local.fileDiff.deletionLines.join("")
|
|
1131
|
+
const after = local.fileDiff.additionLines.join("")
|
|
1132
|
+
|
|
1133
|
+
return Math.max(before.length, after.length) > 500_000
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
const before = local.before?.contents ?? ""
|
|
1137
|
+
const after = local.after?.contents ?? ""
|
|
1138
|
+
|
|
1139
|
+
return Math.max(before.length, after.length) > 500_000
|
|
1140
|
+
})
|
|
1141
|
+
|
|
1142
|
+
const largeOptions = {
|
|
1143
|
+
lineDiffType: "none",
|
|
1144
|
+
maxLineDiffLength: 0,
|
|
1145
|
+
tokenizeMaxLineLength: 1,
|
|
1146
|
+
} satisfies Pick<FileDiffOptions<T, undefined>, "lineDiffType" | "maxLineDiffLength" | "tokenizeMaxLineLength">
|
|
1147
|
+
|
|
1148
|
+
const options = createMemo<FileDiffOptions<T, undefined>>(() => {
|
|
1149
|
+
const base = {
|
|
1150
|
+
...createDefaultOptions(props.diffStyle),
|
|
1151
|
+
...others,
|
|
1152
|
+
...viewer.lineCallbacks,
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
const perf = large() ? { ...base, ...largeOptions } : base
|
|
1156
|
+
|
|
1157
|
+
if (!mobile()) return perf
|
|
1158
|
+
|
|
1159
|
+
return { ...perf, disableLineNumbers: props.disableLineNumbers ?? true }
|
|
1160
|
+
})
|
|
1161
|
+
|
|
1162
|
+
useSearchHandle({
|
|
1163
|
+
search: () => local.search,
|
|
1164
|
+
find: viewer.find,
|
|
1165
|
+
})
|
|
1166
|
+
|
|
1167
|
+
const cacheKey = (contents: string) =>
|
|
1168
|
+
large() ? sampledChecksum(contents) : sampledChecksum(contents, contents.length)
|
|
1169
|
+
|
|
1170
|
+
const target = (): ViewerTarget => {
|
|
1171
|
+
if (local.fileDiff) return { kind: "diff-patch", fileDiff: local.fileDiff }
|
|
1172
|
+
|
|
1173
|
+
if (!local.before || !local.after) return { kind: "diff-empty" }
|
|
1174
|
+
|
|
1175
|
+
return {
|
|
1176
|
+
kind: "diff-files",
|
|
1177
|
+
before: { ...local.before, cacheKey: cacheKey(local.before.contents) },
|
|
1178
|
+
after: { ...local.after, cacheKey: cacheKey(local.after.contents) },
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
// Cleanups run in reverse, so the instance is cleaned up before it releases the shared virtualizer.
|
|
1183
|
+
onCleanup(() => virtuals.cleanup())
|
|
1184
|
+
|
|
1185
|
+
// Pierre beta virtualized instances retain their first diff target and resolve separator metrics at construction.
|
|
1186
|
+
// Plain timeline diffs keep the instance as content streams; virtualized viewers reset only when that is unsafe.
|
|
1187
|
+
render = useViewerRender({
|
|
1188
|
+
viewer,
|
|
1189
|
+
options,
|
|
1190
|
+
request: () => {
|
|
1191
|
+
// Worker render options override per-viewer options, including the large-file fallback.
|
|
1192
|
+
const workerPool = getWorkerPool(large() ? "none" : "word-line")
|
|
1193
|
+
const virtualizer = virtuals.get()
|
|
1194
|
+
const next = target()
|
|
1195
|
+
|
|
1196
|
+
return {
|
|
1197
|
+
spec: {
|
|
1198
|
+
virtualizer,
|
|
1199
|
+
workerPool,
|
|
1200
|
+
hunkSeparators: virtualizer ? options().hunkSeparators : undefined,
|
|
1201
|
+
target: next,
|
|
1202
|
+
},
|
|
1203
|
+
pool: readPoolState(workerPool),
|
|
1204
|
+
settleFrames: 1,
|
|
1205
|
+
preserveHeight: true,
|
|
1206
|
+
expectsRows: diffExpectsRows(next, options().expandUnchanged),
|
|
1207
|
+
}
|
|
1208
|
+
},
|
|
1209
|
+
create: (spec, opts) =>
|
|
1210
|
+
spec.virtualizer
|
|
1211
|
+
? new VirtualizedFileDiff<T>(opts, spec.virtualizer, virtualMetrics, spec.workerPool)
|
|
1212
|
+
: new FileDiff<T>(opts, spec.workerPool),
|
|
1213
|
+
update: (instance, opts) => {
|
|
1214
|
+
const forceRender = !areOptionsEqual(instance.options, opts)
|
|
1215
|
+
instance.setOptions(opts)
|
|
1216
|
+
|
|
1217
|
+
return forceRender
|
|
1218
|
+
},
|
|
1219
|
+
draw: (instance, spec, forceRender) => {
|
|
1220
|
+
if (spec.target.kind === "diff-patch") {
|
|
1221
|
+
instance.render({
|
|
1222
|
+
fileDiff: spec.target.fileDiff,
|
|
1223
|
+
forceRender,
|
|
1224
|
+
lineAnnotations: [],
|
|
1225
|
+
containerWrapper: viewer.container,
|
|
1226
|
+
})
|
|
1227
|
+
|
|
1228
|
+
return
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
if (spec.target.kind !== "diff-files") return
|
|
1232
|
+
|
|
1233
|
+
instance.render({
|
|
1234
|
+
oldFile: spec.target.before,
|
|
1235
|
+
newFile: spec.target.after,
|
|
1236
|
+
forceRender,
|
|
1237
|
+
lineAnnotations: [],
|
|
1238
|
+
containerWrapper: viewer.container,
|
|
1239
|
+
})
|
|
1240
|
+
},
|
|
1241
|
+
isReady: (root) => root.querySelector("[data-line]") != null,
|
|
1242
|
+
annotations: () => local.annotations ?? [],
|
|
1243
|
+
applyAnnotations: (instance) => {
|
|
1244
|
+
instance.setLineAnnotations(local.annotations ?? [])
|
|
1245
|
+
instance.rerender()
|
|
1246
|
+
},
|
|
1247
|
+
commentedLines: () => local.commentedLines ?? [],
|
|
1248
|
+
markCommented: markCommentedDiffLines,
|
|
1249
|
+
// A virtualized diff only renders rows near the viewport, so jumping to a selection must scroll it into range.
|
|
1250
|
+
reveal: (instance, spec, range) => {
|
|
1251
|
+
const virtualizer = spec.virtualizer
|
|
1252
|
+
|
|
1253
|
+
if (!(instance instanceof VirtualizedFileDiff) || !virtualizer) return true
|
|
1254
|
+
|
|
1255
|
+
const position = instance.getLinePosition(Math.min(range.start, range.end), range.side)
|
|
1256
|
+
const root = virtualizer.getRoot()
|
|
1257
|
+
|
|
1258
|
+
// Keep the request until the diff has a layout to measure; the next ready draw retries it.
|
|
1259
|
+
if (!position || !root) return false
|
|
1260
|
+
|
|
1261
|
+
const top = virtualizer.getOffsetInScrollContainer(viewer.container) + position.top
|
|
1262
|
+
const scrollTop = virtualizer.getScrollTop()
|
|
1263
|
+
const height = root instanceof Document ? window.innerHeight : root.clientHeight
|
|
1264
|
+
|
|
1265
|
+
if (top < scrollTop || top + position.height > scrollTop + height) virtualizer.scrollTo({ top: top - height / 2 })
|
|
1266
|
+
|
|
1267
|
+
return true
|
|
1268
|
+
},
|
|
1269
|
+
onRendered: () => local.onRendered?.(),
|
|
1270
|
+
})
|
|
1271
|
+
|
|
1272
|
+
createEffect(
|
|
1273
|
+
on(
|
|
1274
|
+
() => local.selectedLines,
|
|
1275
|
+
(range) => render.requestReveal(range ?? undefined),
|
|
1276
|
+
),
|
|
1277
|
+
)
|
|
1278
|
+
|
|
1279
|
+
return (
|
|
1280
|
+
<ViewerShell
|
|
1281
|
+
mode="diff"
|
|
1282
|
+
viewer={viewer}
|
|
1283
|
+
class={local.class}
|
|
1284
|
+
classList={local.classList}
|
|
1285
|
+
onCommentSelection={local.onCommentSelection}
|
|
1286
|
+
/>
|
|
1287
|
+
)
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
// ---------------------------------------------------------------------------
|
|
1291
|
+
// Public API
|
|
1292
|
+
// ---------------------------------------------------------------------------
|
|
1293
|
+
|
|
1294
|
+
export function File<T>(props: FileProps<T>) {
|
|
1295
|
+
if (props.mode === "text") {
|
|
1296
|
+
return <FileMedia media={props.media} fallback={() => TextViewer(props)} />
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
return <FileMedia media={props.media} fallback={() => DiffViewer(props)} />
|
|
1300
|
+
}
|