@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.
Files changed (261) hide show
  1. package/AGENTS.md +25 -0
  2. package/bunfig.toml +2 -0
  3. package/component-tests/basic-tool.fixture.tsx +46 -0
  4. package/component-tests/basic-tool.spec.ts +63 -0
  5. package/component-tests/file-media.fixture.tsx +39 -0
  6. package/component-tests/file-media.spec.ts +13 -0
  7. package/component-tests/file-tool.spec.ts +62 -0
  8. package/component-tests/file-viewer.fixture.tsx +109 -0
  9. package/component-tests/file-viewer.spec.ts +134 -0
  10. package/component-tests/file.spec.ts +33 -0
  11. package/component-tests/line-comment-v2.spec.ts +15 -0
  12. package/component-tests/markdown-code-wrap.spec.ts +94 -0
  13. package/component-tests/markdown-readiness.spec.ts +95 -0
  14. package/component-tests/markdown.fixture.tsx +104 -0
  15. package/component-tests/markdown.spec.ts +1564 -0
  16. package/component-tests/mermaid-preview.spec.ts +43 -0
  17. package/component-tests/patch-group.spec.ts +79 -0
  18. package/component-tests/read-group.spec.ts +72 -0
  19. package/component-tests/read-image.fixture.tsx +66 -0
  20. package/component-tests/read-image.spec.ts +154 -0
  21. package/component-tests/session-compaction.spec.ts +254 -0
  22. package/component-tests/session-lifecycle.spec.ts +190 -0
  23. package/component-tests/session-message-projection.spec.ts +639 -0
  24. package/component-tests/session-notice-projection.spec.ts +20 -0
  25. package/component-tests/session-review-comments.spec.ts +179 -0
  26. package/component-tests/session-shell.spec.ts +93 -0
  27. package/component-tests/session-timeline-mobile.spec.ts +201 -0
  28. package/component-tests/session-timeline-notices.spec.ts +38 -0
  29. package/component-tests/session-timeline-reasoning.spec.ts +107 -0
  30. package/component-tests/session-timeline.spec.ts +157 -0
  31. package/component-tests/session-tool-projection.spec.ts +323 -0
  32. package/component-tests/text-lifecycle.fixture.tsx +26 -0
  33. package/component-tests/text-lifecycle.spec.ts +42 -0
  34. package/component-tests/timeline-subagent-rows.spec.ts +33 -0
  35. package/component-tests/tool-disclosure.spec.ts +48 -0
  36. package/component-tests/tool-group-sticky.spec.ts +37 -0
  37. package/component-tests/tool-group.spec.ts +290 -0
  38. package/component-tests/tool-status-title.fixture.tsx +22 -0
  39. package/component-tests/tool-status-title.spec.ts +30 -0
  40. package/component-tests/user-message-quotes.spec.ts +111 -0
  41. package/package.json +91 -4
  42. package/playwright.components.config.ts +4 -0
  43. package/playwright.local.config.ts +2 -0
  44. package/src/actions.ts +54 -0
  45. package/src/components/apply-patch-file.ts +122 -0
  46. package/src/components/basic-tool.css +487 -0
  47. package/src/components/basic-tool.tsx +258 -0
  48. package/src/components/copy-machine.ts +78 -0
  49. package/src/components/copy.ts +85 -0
  50. package/src/components/diagram-preview.tsx +28 -0
  51. package/src/components/disclosure-machine.ts +188 -0
  52. package/src/components/disclosure.ts +129 -0
  53. package/src/components/dock-prompt.stories.tsx +73 -0
  54. package/src/components/dock-prompt.tsx +29 -0
  55. package/src/components/file-comment-selection.tsx +146 -0
  56. package/src/components/file-media-machine.ts +165 -0
  57. package/src/components/file-media.stories.tsx +112 -0
  58. package/src/components/file-media.tsx +323 -0
  59. package/src/components/file-search.tsx +73 -0
  60. package/src/components/file.css +59 -0
  61. package/src/components/file.tsx +1300 -0
  62. package/src/components/image-preview.tsx +35 -0
  63. package/src/components/line-comment-annotations.tsx +580 -0
  64. package/src/components/line-comment-machine.ts +262 -0
  65. package/src/components/line-comment-styles.ts +293 -0
  66. package/src/components/line-comment.stories.tsx +152 -0
  67. package/src/components/line-comment.tsx +448 -0
  68. package/src/components/link-segments.ts +20 -0
  69. package/src/components/linked-text.tsx +21 -0
  70. package/src/components/markdown-cache-machine.ts +257 -0
  71. package/src/components/markdown-cache.tsx +556 -0
  72. package/src/components/markdown-clipboard.ts +101 -0
  73. package/src/components/markdown-code-machine.ts +200 -0
  74. package/src/components/markdown-code-state.ts +22 -0
  75. package/src/components/markdown-fragment-links.ts +74 -0
  76. package/src/components/markdown-github.svg +3 -0
  77. package/src/components/markdown-globe.svg +5 -0
  78. package/src/components/markdown-image-machine.ts +170 -0
  79. package/src/components/markdown-image.ts +156 -0
  80. package/src/components/markdown-inline-code-kind.ts +39 -0
  81. package/src/components/markdown-mermaid.ts +18 -0
  82. package/src/components/markdown-phase-machine.ts +102 -0
  83. package/src/components/markdown-projection.ts +13 -0
  84. package/src/components/markdown-session-links.ts +183 -0
  85. package/src/components/markdown-solid.test.tsx +92 -0
  86. package/src/components/markdown-solid.tsx +113 -0
  87. package/src/components/markdown-stream.ts +95 -0
  88. package/src/components/markdown-worker-machine.ts +516 -0
  89. package/src/components/markdown-worker-protocol.ts +73 -0
  90. package/src/components/markdown-worker.ts +250 -0
  91. package/src/components/markdown.css +672 -0
  92. package/src/components/markdown.stories.tsx +199 -0
  93. package/src/components/markdown.tsx +1612 -0
  94. package/src/components/markdown.worker.ts +188 -0
  95. package/src/components/message-file.ts +102 -0
  96. package/src/components/message-nav.css +134 -0
  97. package/src/components/message-nav.tsx +159 -0
  98. package/src/components/message-part.css +2109 -0
  99. package/src/components/message-part.tsx +3 -0
  100. package/src/components/question-prompt-experimental.css +576 -0
  101. package/src/components/question-prompt.css +527 -0
  102. package/src/components/quote-highlight.css +59 -0
  103. package/src/components/review-focus-driver.ts +142 -0
  104. package/src/components/review-focus-machine.ts +95 -0
  105. package/src/components/session-compaction.css +270 -0
  106. package/src/components/session-diff.ts +358 -0
  107. package/src/components/session-error.tsx +41 -0
  108. package/src/components/session-retry.tsx +92 -0
  109. package/src/components/session-review-machine.ts +241 -0
  110. package/src/components/session-review.css +264 -0
  111. package/src/components/session-review.stories.tsx +232 -0
  112. package/src/components/session-review.tsx +733 -0
  113. package/src/components/session-turn.css +282 -0
  114. package/src/components/shell-submessage.css +24 -0
  115. package/src/components/timeline-separator.tsx +69 -0
  116. package/src/components/tool-count-label.css +57 -0
  117. package/src/components/tool-count-label.tsx +76 -0
  118. package/src/components/tool-count-summary.css +102 -0
  119. package/src/components/tool-count-summary.stories.tsx +99 -0
  120. package/src/components/tool-count-summary.tsx +44 -0
  121. package/src/components/tool-error-card.css +178 -0
  122. package/src/components/tool-error-card.stories.tsx +113 -0
  123. package/src/components/tool-error-card.tsx +202 -0
  124. package/src/components/tool-status-title-machine.ts +128 -0
  125. package/src/components/tool-status-title.css +89 -0
  126. package/src/components/tool-status-title.tsx +163 -0
  127. package/src/context/data.tsx +107 -0
  128. package/src/context/index.ts +1 -0
  129. package/src/context/markdown.tsx +129 -0
  130. package/src/context/sources.tsx +34 -0
  131. package/src/document.ts +8 -0
  132. package/src/file-presentation.ts +34 -0
  133. package/src/message/action-machine.ts +72 -0
  134. package/src/message/agent-label.ts +3 -0
  135. package/src/message/attachment-card.css +119 -0
  136. package/src/message/attachment-card.tsx +40 -0
  137. package/src/message/attachment-media-machine.ts +213 -0
  138. package/src/message/comment-card.tsx +155 -0
  139. package/src/message/compaction-entrance-machine.ts +93 -0
  140. package/src/message/current-message.tsx +157 -0
  141. package/src/message/current-tool-state.ts +161 -0
  142. package/src/message/message-content.tsx +1391 -0
  143. package/src/message/paced-text-machine.ts +73 -0
  144. package/src/message/prompt-message.stories.tsx +149 -0
  145. package/src/message/relative-time.ts +45 -0
  146. package/src/message/user-message-quote-parts.ts +16 -0
  147. package/src/message/user-message-quotes.stories.tsx +797 -0
  148. package/src/message/user-message-quotes.tsx +135 -0
  149. package/src/pierre/comment-hover-machine.ts +53 -0
  150. package/src/pierre/comment-hover.ts +131 -0
  151. package/src/pierre/comment-selection-machine.ts +257 -0
  152. package/src/pierre/commented-lines.ts +106 -0
  153. package/src/pierre/diff-selection.ts +91 -0
  154. package/src/pierre/file-find-machine.ts +323 -0
  155. package/src/pierre/file-find.ts +537 -0
  156. package/src/pierre/file-runtime.ts +42 -0
  157. package/src/pierre/file-selection.ts +140 -0
  158. package/src/pierre/index.ts +344 -0
  159. package/src/pierre/media.ts +140 -0
  160. package/src/pierre/render-machine.ts +381 -0
  161. package/src/pierre/selection-bridge.ts +82 -0
  162. package/src/pierre/selection-machine.ts +198 -0
  163. package/src/pierre/virtualizer.ts +114 -0
  164. package/src/pierre/worker.ts +46 -0
  165. package/src/sources/experiment.ts +59 -0
  166. package/src/sources/site-icon.tsx +53 -0
  167. package/src/sources/sources-chip.tsx +57 -0
  168. package/src/sources/sources-expand-machine.ts +101 -0
  169. package/src/sources/sources-panel.tsx +249 -0
  170. package/src/sources/sources-title.tsx +21 -0
  171. package/src/sources/sources.css +266 -0
  172. package/src/sources/sources.ts +141 -0
  173. package/src/storybook/current-session-fixtures.ts +1492 -0
  174. package/src/storybook/current-session-scenarios.ts +76 -0
  175. package/src/storybook/current-session-story.tsx +154 -0
  176. package/src/storybook/index.ts +43 -0
  177. package/src/styles/index.css +19 -0
  178. package/src/timeline/__snapshots__/group-labels.test.ts.snap +110 -0
  179. package/src/timeline/__snapshots__/placement.test.ts.snap +1041 -0
  180. package/src/timeline/code-blocks.stories.tsx +105 -0
  181. package/src/timeline/commentary.stories.tsx +70 -0
  182. package/src/timeline/commentary.ts +38 -0
  183. package/src/timeline/detail.ts +111 -0
  184. package/src/timeline/error-card.stories.tsx +134 -0
  185. package/src/timeline/error-spacing.stories.tsx +71 -0
  186. package/src/timeline/facts.ts +493 -0
  187. package/src/timeline/file-changes.stories.tsx +436 -0
  188. package/src/timeline/group-label-fixtures.ts +97 -0
  189. package/src/timeline/mermaid.stories.tsx +131 -0
  190. package/src/timeline/place.ts +280 -0
  191. package/src/timeline/placement-fixtures.ts +365 -0
  192. package/src/timeline/placement-reactivity.ts +133 -0
  193. package/src/timeline/projection.ts +1261 -0
  194. package/src/timeline/research-and-agents.stories.tsx +461 -0
  195. package/src/timeline/session-timeline-row.tsx +1675 -0
  196. package/src/timeline/session-timeline.tsx +68 -0
  197. package/src/timeline/steer-arrival-machine.ts +134 -0
  198. package/src/timeline/terminal-work.stories.tsx +464 -0
  199. package/src/timeline/timeline-row.stories.tsx +1069 -0
  200. package/src/timeline/timeline-row.ts +238 -0
  201. package/src/timeline/turn-phase-machine.ts +112 -0
  202. package/src/timeline/user-message-fold.stories.tsx +194 -0
  203. package/src/timeline/user-runs.ts +87 -0
  204. package/src/timeline-next/__snapshots__/placement.test.ts.snap +990 -0
  205. package/src/timeline-next/compaction.tsx +236 -0
  206. package/src/timeline-next/errors.ts +101 -0
  207. package/src/timeline-next/event-row.tsx +168 -0
  208. package/src/timeline-next/events.ts +103 -0
  209. package/src/timeline-next/index.ts +10 -0
  210. package/src/timeline-next/nested-rail.ts +125 -0
  211. package/src/timeline-next/projection.ts +1028 -0
  212. package/src/timeline-next/row.tsx +1728 -0
  213. package/src/timeline-next/rules.ts +89 -0
  214. package/src/timeline-next/separator.tsx +73 -0
  215. package/src/timeline-next/spacing.ts +127 -0
  216. package/src/timeline-next/status-line.tsx +41 -0
  217. package/src/timeline-next/status.ts +33 -0
  218. package/src/timeline-next/stories/compare-renderer.stories.tsx +190 -0
  219. package/src/timeline-next/stories/compare-sessions/.gitignore +3 -0
  220. package/src/timeline-next/stories/compare.stories.tsx +486 -0
  221. package/src/timeline-next/stories/playground.stories.tsx +297 -0
  222. package/src/timeline-next/stories/session-replay.ts +1156 -0
  223. package/src/timeline-next/stories/session-timeline.tsx +50 -0
  224. package/src/timeline-next/stories/subagent-status.stories.tsx +132 -0
  225. package/src/timeline-next/timeline-next.css +649 -0
  226. package/src/timeline-next/tools/__snapshots__/activity-label.test.ts.snap +39 -0
  227. package/src/timeline-next/tools/activity-label.ts +166 -0
  228. package/src/timeline-next/tools/assistant-content.tsx +84 -0
  229. package/src/timeline-next/tools/context-group.tsx +619 -0
  230. package/src/timeline-next/tools/error-card.tsx +147 -0
  231. package/src/timeline-next/tools/explore.tsx +119 -0
  232. package/src/timeline-next/tools/file-group.tsx +128 -0
  233. package/src/timeline-next/tools/patch.tsx +174 -0
  234. package/src/timeline-next/tools/shell.tsx +186 -0
  235. package/src/timeline-next/tools/thought.tsx +87 -0
  236. package/src/timeline-next/tools/tool-display.tsx +119 -0
  237. package/src/tools/ansi.ts +8 -0
  238. package/src/tools/context-group-label.ts +123 -0
  239. package/src/tools/file-runs.ts +35 -0
  240. package/src/tools/project-path.ts +18 -0
  241. package/src/tools/read-group.stories.tsx +159 -0
  242. package/src/tools/run-machine.ts +185 -0
  243. package/src/tools/shell-output-machine.ts +233 -0
  244. package/src/tools/shell-output.ts +91 -0
  245. package/src/tools/subagent-row-variants.stories.tsx +129 -0
  246. package/src/tools/tool-group.stories.tsx +345 -0
  247. package/src/tools/tool-renderer.tsx +2876 -0
  248. package/src/v2/components/line-comment-annotations-v2.tsx +215 -0
  249. package/src/v2/components/session-file-panel-v2.tsx +149 -0
  250. package/src/v2/components/session-progress-indicator-v2.css +5 -0
  251. package/src/v2/components/session-progress-indicator-v2.stories.tsx +86 -0
  252. package/src/v2/components/session-progress-indicator-v2.tsx +16 -0
  253. package/src/v2/components/session-review-empty-changes-v2.tsx +17 -0
  254. package/src/v2/components/session-review-empty-no-git-v2.tsx +28 -0
  255. package/src/v2/components/session-review-file-preview-v2-virtualize.ts +5 -0
  256. package/src/v2/components/session-review-file-preview-v2.tsx +266 -0
  257. package/src/v2/components/session-review-v2.css +653 -0
  258. package/src/v2/components/session-review-v2.tsx +422 -0
  259. package/src/v2/components/sidebar-presence-machine.ts +69 -0
  260. package/tsconfig.json +20 -0
  261. package/README.md +0 -3
@@ -0,0 +1,135 @@
1
+ import { createSignal, For, onCleanup, onMount, Show } from "solid-js"
2
+ import { FileIcon } from "@zyro-sec/ui/file-icon"
3
+ import { Icon } from "@zyro-sec/ui/icon"
4
+ import { getFilenameTruncated } from "@zyro-sec/util/path"
5
+ import type { SessionUserComment } from "../actions"
6
+ import { commentSegments, selectionLabel } from "./user-message-quote-parts"
7
+
8
+ type Quote = Extract<SessionUserComment, { type: "quote" }>
9
+
10
+ type Reference = Exclude<SessionUserComment, Quote>
11
+
12
+ // Quote text and comments set no `dir`: the bubble's `dir="auto"` skips elements that set their own, so a bubble of
13
+ // only quotes would miss their language. `unicode-bidi: plaintext` gives each paragraph its direction instead.
14
+ export function UserMessageQuotes(props: { comments: SessionUserComment[] }) {
15
+ return (
16
+ <div data-slot="user-message-quotes">
17
+ <For each={props.comments}>
18
+ {(item) =>
19
+ item.type === "quote" ? <UserMessageQuote quote={item} /> : <UserMessageReference reference={item} />
20
+ }
21
+ </For>
22
+ </div>
23
+ )
24
+ }
25
+
26
+ function UserMessageQuote(props: { quote: Quote }) {
27
+ const [open, setOpen] = createSignal(false)
28
+ const [clamped, setClamped] = createSignal(false)
29
+ const [single, setSingle] = createSignal(false)
30
+ // Blank lines around a selection carry nothing, and a quote of only whitespace shows just its comment.
31
+ const text = () => props.quote.text.trim()
32
+ const expandable = () => clamped() || open()
33
+
34
+ const toggle = (element: HTMLElement) => {
35
+ // Ending a text selection inside the quote is a click too; it should not open or close it.
36
+ const selection = window.getSelection()
37
+
38
+ if (selection && !selection.isCollapsed && element.contains(selection.anchorNode)) return
39
+ setOpen((value) => !value)
40
+ }
41
+
42
+ return (
43
+ <div data-slot="user-message-quote-group">
44
+ <Show when={text()}>
45
+ <blockquote data-slot="user-message-quote" data-single-line={single() ? "" : undefined}>
46
+ <div
47
+ data-slot="user-message-quote-text"
48
+ data-open={open() ? "" : undefined}
49
+ data-expandable={expandable() ? "" : undefined}
50
+ role={expandable() ? "button" : undefined}
51
+ tabIndex={expandable() ? 0 : undefined}
52
+ aria-expanded={expandable() ? open() : undefined}
53
+ ref={(element) =>
54
+ onMount(() => {
55
+ const measure = () => {
56
+ setClamped(element.scrollHeight > element.clientHeight + 1)
57
+ setSingle(element.clientHeight <= parseFloat(getComputedStyle(element).lineHeight) + 1)
58
+ }
59
+
60
+ const observer = new ResizeObserver(measure)
61
+
62
+ observer.observe(element)
63
+ void document.fonts.ready.then(measure)
64
+ onCleanup(() => observer.disconnect())
65
+ })
66
+ }
67
+ onClick={(event) => expandable() && toggle(event.currentTarget)}
68
+ onKeyDown={(event) => {
69
+ if (!expandable() || (event.key !== "Enter" && event.key !== " ")) return
70
+ event.preventDefault()
71
+ setOpen((value) => !value)
72
+ }}
73
+ >
74
+ <Show when={props.quote.source}>
75
+ {(source) => (
76
+ <span data-slot="user-message-quote-icon" title={source().label} aria-label={source().label} role="img">
77
+ <Icon name={source().icon} size="small" />
78
+ </span>
79
+ )}
80
+ </Show>
81
+ {text()}
82
+ </div>
83
+ </blockquote>
84
+ </Show>
85
+ <Show when={props.quote.comment?.trim()}>{(comment) => <UserMessageQuoteComment text={comment()} />}</Show>
86
+ </div>
87
+ )
88
+ }
89
+
90
+ function UserMessageReference(props: { reference: Reference }) {
91
+ return (
92
+ <div data-slot="user-message-quote-group">
93
+ <blockquote data-slot="user-message-quote" data-single-line>
94
+ <span data-slot="user-message-quote-reference">
95
+ {props.reference.type === "note" ? (
96
+ <>
97
+ <Icon name={props.reference.icon} size="small" />
98
+ <span data-slot="user-message-quote-reference-label" dir="ltr">
99
+ {props.reference.title?.trim() || props.reference.label}
100
+ </span>
101
+ <Show when={props.reference.subtitle?.trim()}>
102
+ {(subtitle) => (
103
+ <span data-slot="user-message-quote-reference-detail" dir="auto">
104
+ {subtitle()}
105
+ </span>
106
+ )}
107
+ </Show>
108
+ </>
109
+ ) : (
110
+ <>
111
+ <FileIcon
112
+ node={{ path: props.reference.path, type: props.reference.path.endsWith("/") ? "directory" : "file" }}
113
+ />
114
+ <span data-slot="user-message-quote-reference-label" dir="ltr">
115
+ {getFilenameTruncated(props.reference.path, 40)}
116
+ {selectionLabel(props.reference.selection)}
117
+ </span>
118
+ </>
119
+ )}
120
+ </span>
121
+ </blockquote>
122
+ <Show when={props.reference.comment.trim()}>{(comment) => <UserMessageQuoteComment text={comment()} />}</Show>
123
+ </div>
124
+ )
125
+ }
126
+
127
+ function UserMessageQuoteComment(props: { text: string }) {
128
+ return (
129
+ <div data-slot="user-message-quote-comment">
130
+ <For each={commentSegments(props.text)}>
131
+ {(segment) => (segment.mention ? <span data-highlight="file">{segment.text}</span> : segment.text)}
132
+ </For>
133
+ </div>
134
+ )
135
+ }
@@ -0,0 +1,53 @@
1
+ export type HoverCommentSide = "additions" | "deletions"
2
+
3
+ export type HoverCommentLine = {
4
+ readonly lineNumber: number
5
+ readonly side?: HoverCommentSide
6
+ }
7
+
8
+ /**
9
+ * One gutter comment `+` button. Runs on every pointer move and scroll, so it returns the same object when nothing
10
+ * changes and reads its events from one reused object.
11
+ * - `tracking`: listens to the document and caches the last hovered line, so a click still has one after the pointer
12
+ * leaves the code. Leaving the document does not end it: Pierre inserts the button after creating it, and may detach
13
+ * and reattach the viewer.
14
+ * - `disposed`: its owner replaced or unmounted it; the document listeners are gone.
15
+ */
16
+ export type HoverCommentState =
17
+ | { readonly status: "tracking"; readonly line: HoverCommentLine | undefined }
18
+ | { readonly status: "disposed" }
19
+
20
+ /** The line Pierre reports as hovered. Mutable so the driver can reuse one object for every pointer move. */
21
+ export type HoverCommentSync = {
22
+ type: "hover.synced"
23
+ lineNumber: number | undefined
24
+ side: HoverCommentSide | undefined
25
+ }
26
+
27
+ export type HoverCommentEvent = Readonly<HoverCommentSync> | { readonly type: "disposed" }
28
+
29
+ export const INITIAL_HOVER_COMMENT_STATE: HoverCommentState = Object.freeze({ status: "tracking", line: undefined })
30
+
31
+ export const DISPOSED_HOVER_COMMENT_STATE: HoverCommentState = Object.freeze({ status: "disposed" })
32
+
33
+ export function transitionHoverComment(state: HoverCommentState, event: HoverCommentEvent): HoverCommentState {
34
+ if (state.status === "disposed") return state
35
+
36
+ if (event.type === "disposed") return DISPOSED_HOVER_COMMENT_STATE
37
+ const lineNumber = event.lineNumber
38
+
39
+ // No hovered line (the pointer left the code) keeps the cached one.
40
+ if (lineNumber === undefined || (lineNumber === state.line?.lineNumber && event.side === state.line?.side))
41
+ return state
42
+
43
+ return { status: "tracking", line: { lineNumber, side: event.side } }
44
+ }
45
+
46
+ /** Whether the button still listens to the document, and so whether a render may hand it back again. */
47
+ export function selectHoverActive(state: HoverCommentState) {
48
+ return state.status !== "disposed"
49
+ }
50
+
51
+ export function selectHoverLine(state: HoverCommentState) {
52
+ return state.status === "disposed" ? undefined : state.line
53
+ }
@@ -0,0 +1,131 @@
1
+ import {
2
+ INITIAL_HOVER_COMMENT_STATE,
3
+ selectHoverActive,
4
+ selectHoverLine,
5
+ transitionHoverComment,
6
+ type HoverCommentEvent,
7
+ type HoverCommentLine,
8
+ type HoverCommentSync,
9
+ } from "./comment-hover-machine"
10
+
11
+ export type { HoverCommentLine }
12
+
13
+ /** A gutter `+` button and the hover tracking behind it. Dispose it when its viewer stops rendering it. */
14
+ export type HoverComment = {
15
+ button: HTMLButtonElement
16
+ dispose: () => void
17
+ /** Whether the next render may return this button instead of building another. */
18
+ canReuse: () => boolean
19
+ }
20
+
21
+ export function createHoverComment(props: {
22
+ label: string
23
+ getHoveredLine: () => HoverCommentLine | undefined
24
+ onSelect: (line: HoverCommentLine) => void
25
+ /** Whether the line is in the selected range, which a click comments on as a whole rather than starting a new one. */
26
+ isSelected: (line: HoverCommentLine) => boolean
27
+ }): HoverComment | undefined {
28
+ if (typeof document === "undefined") return
29
+
30
+ const button = document.createElement("button")
31
+ button.type = "button"
32
+ button.ariaLabel = props.label
33
+ // A drawn plus, unlike a "+" glyph, is centered regardless of the font's metrics.
34
+ const icon = document.createElementNS("http://www.w3.org/2000/svg", "svg")
35
+ icon.setAttribute("width", "12")
36
+ icon.setAttribute("height", "12")
37
+ icon.setAttribute("viewBox", "0 0 12 12")
38
+ icon.setAttribute("fill", "none")
39
+ icon.setAttribute("aria-hidden", "true")
40
+ const plus = document.createElementNS("http://www.w3.org/2000/svg", "path")
41
+ plus.setAttribute("d", "M6 2V10M2 6H10")
42
+ plus.setAttribute("stroke", "currentColor")
43
+ plus.setAttribute("stroke-width", "1.5")
44
+ icon.append(plus)
45
+ button.append(icon)
46
+ button.style.width = "20px"
47
+ button.style.height = "20px"
48
+ button.style.display = "flex"
49
+ button.style.alignItems = "center"
50
+ button.style.justifyContent = "center"
51
+ button.style.border = "none"
52
+ button.style.borderRadius = "var(--radius-md)"
53
+ button.style.background = "var(--v2-background-bg-inverse)"
54
+ button.style.color = "var(--v2-icon-icon-inverse)"
55
+ button.style.boxShadow = "var(--shadow-xs)"
56
+ button.style.padding = "0"
57
+ button.style.cursor = "pointer"
58
+ button.style.position = "relative"
59
+ // It overlaps the line number, which Pierre stacks at 1; Pierre's own gutter button uses 4.
60
+ button.style.zIndex = "4"
61
+ button.style.left = "-4px"
62
+ button.style.top = "calc((var(--diffs-line-height, 24px) - 20px) / 2)"
63
+
64
+ let state = INITIAL_HOVER_COMMENT_STATE
65
+ // Whether the last press went to Pierre, which then opens the draft itself.
66
+ let forwarded = false
67
+ // Reused for every pointer move and scroll so tracking allocates nothing.
68
+ const syncEvent: HoverCommentSync = { type: "hover.synced", lineNumber: undefined, side: undefined }
69
+
70
+ const dispatch = (event: HoverCommentEvent) => {
71
+ const next = transitionHoverComment(state, event)
72
+
73
+ if (next === state) return
74
+ const active = selectHoverActive(state)
75
+ state = next
76
+
77
+ if (active && !selectHoverActive(state)) {
78
+ document.removeEventListener("pointermove", sync)
79
+ document.removeEventListener("scroll", sync, true)
80
+ }
81
+ }
82
+
83
+ // The hovered line changes when the pointer moves or when content scrolls under a stationary pointer, so track both
84
+ // with passive listeners instead of polling every animation frame.
85
+ const sync = () => {
86
+ if (!button.isConnected) return
87
+ const hovered = props.getHoveredLine()
88
+ syncEvent.lineNumber = hovered?.lineNumber
89
+ syncEvent.side = hovered?.side
90
+ dispatch(syncEvent)
91
+ }
92
+
93
+ document.addEventListener("pointermove", sync, { passive: true })
94
+ document.addEventListener("scroll", sync, { passive: true, capture: true })
95
+ button.addEventListener("mouseenter", sync)
96
+ button.addEventListener("mousemove", sync)
97
+ // Pierre skips presses on a custom gutter button when it selects lines, so hand the press to the line number under
98
+ // the button: dragging from it then selects lines and ends in a draft for them. On a line that is already selected
99
+ // the press stays here, as Pierre would unselect or shrink the range, and the click comments on the whole range.
100
+ button.addEventListener("pointerdown", (event) => {
101
+ event.preventDefault()
102
+ event.stopPropagation()
103
+ sync()
104
+ const line = selectHoverLine(state)
105
+ forwarded = !line || !props.isSelected(line)
106
+
107
+ if (!forwarded) return
108
+ const slot = button.assignedSlot ?? button.parentElement?.assignedSlot
109
+ slot?.closest("[data-column-number]")?.dispatchEvent(new PointerEvent("pointerdown", event))
110
+ })
111
+ button.addEventListener("mousedown", (event) => {
112
+ event.preventDefault()
113
+ event.stopPropagation()
114
+ sync()
115
+ })
116
+ button.addEventListener("click", (event) => {
117
+ event.preventDefault()
118
+ event.stopPropagation()
119
+ sync()
120
+ const line = selectHoverLine(state)
121
+
122
+ // A keyboard press has no pointer, so it never went to Pierre.
123
+ if (line && (event.detail === 0 || !forwarded)) props.onSelect(line)
124
+ })
125
+
126
+ return {
127
+ button,
128
+ dispose: () => dispatch({ type: "disposed" }),
129
+ canReuse: () => selectHoverActive(state),
130
+ }
131
+ }
@@ -0,0 +1,257 @@
1
+ import type { SelectedLineRange } from "@pierre/diffs"
2
+
3
+ /** How long a released selection waits unchanged before the toolbar reads it, as for the timeline's Quote. */
4
+ export const COMMENT_SELECTION_SETTLE_MS = 150
5
+
6
+ /** Space between the toolbar, the selection and the viewer's visible edges. */
7
+ export const COMMENT_SELECTION_GAP = 8
8
+
9
+ export type CommentSelectionRect = {
10
+ readonly top: number
11
+ readonly bottom: number
12
+ readonly left: number
13
+ readonly right: number
14
+ }
15
+
16
+ export type CommentSelectionSize = { readonly width: number; readonly height: number }
17
+
18
+ /** The selected text's box in viewport pixels. `start` is its first glyph's left edge, `end` its last glyph's right. */
19
+ export type CommentSelectionAnchor = {
20
+ readonly top: number
21
+ readonly bottom: number
22
+ readonly start: number
23
+ readonly end: number
24
+ readonly single: boolean
25
+ /** Dragged upwards, so the pointer rests at the start. */
26
+ readonly backward: boolean
27
+ }
28
+
29
+ /** The viewer's text selection, read in one frame. `anchor` is undefined while it is scrolled out of `bounds`. */
30
+ export type CommentSelectionSample = {
31
+ readonly range: SelectedLineRange
32
+ /** The part of the viewer that is on screen. */
33
+ readonly bounds: CommentSelectionRect
34
+ readonly anchor: CommentSelectionAnchor | undefined
35
+ }
36
+
37
+ /**
38
+ * The Add comment toolbar for text selected in a file or diff viewer. `pressing` is a primary press inside the viewer;
39
+ * its selection is read only after the release, once it has stayed unchanged for `COMMENT_SELECTION_SETTLE_MS` under
40
+ * `attempt`. `shown` offers a comment on `range`; `placeAttempt` names the queued placement frame.
41
+ */
42
+ export type CommentSelectionState =
43
+ | { readonly status: "idle"; readonly issued: number }
44
+ | { readonly status: "pressing"; readonly issued: number }
45
+ | { readonly status: "settling"; readonly issued: number; readonly attempt: number }
46
+ | {
47
+ readonly status: "shown"
48
+ readonly issued: number
49
+ readonly range: SelectedLineRange
50
+ readonly bounds: CommentSelectionRect
51
+ readonly anchor: CommentSelectionAnchor | undefined
52
+ /** Zero until the toolbar is measured. */
53
+ readonly size: CommentSelectionSize
54
+ readonly placeAttempt: number | undefined
55
+ }
56
+ | { readonly status: "disposed" }
57
+
58
+ export type CommentSelectionEvent =
59
+ /** A press outside the toolbar; `inside` when it landed in this viewer. */
60
+ | { readonly type: "pointer.down"; readonly button: number; readonly inside: boolean }
61
+ /** `pointerup`, `pointercancel`, `contextmenu` or window `blur`: the press ended. */
62
+ | { readonly type: "pointer.released" }
63
+ | { readonly type: "selection.changed" }
64
+ /** `sample` is undefined when nothing in this viewer is selected. */
65
+ | { readonly type: "settle.sampled"; readonly attempt: number; readonly sample: CommentSelectionSample | undefined }
66
+ /** Something scrolled or resized. */
67
+ | { readonly type: "layout.changed" }
68
+ | { readonly type: "place.sampled"; readonly attempt: number; readonly sample: CommentSelectionSample | undefined }
69
+ | { readonly type: "toolbar.measured"; readonly size: CommentSelectionSize }
70
+ | { readonly type: "escape" }
71
+ | { readonly type: "comment.added" }
72
+ | { readonly type: "disposed" }
73
+
74
+ export type CommentSelectionEffect =
75
+ | { readonly type: "schedule-settle"; readonly attempt: number; readonly delayMs: number }
76
+ | { readonly type: "cancel-settle" }
77
+ | { readonly type: "schedule-place"; readonly attempt: number }
78
+ | { readonly type: "cancel-place" }
79
+ | { readonly type: "clear-selection" }
80
+ | { readonly type: "open-draft"; readonly range: SelectedLineRange }
81
+
82
+ export type CommentSelectionTransition = {
83
+ readonly state: CommentSelectionState
84
+ readonly effects: readonly CommentSelectionEffect[]
85
+ }
86
+
87
+ type Active = Exclude<CommentSelectionState, { status: "disposed" }>
88
+
89
+ const NONE: readonly never[] = []
90
+
91
+ const CANCEL_SETTLE: readonly CommentSelectionEffect[] = [{ type: "cancel-settle" }]
92
+
93
+ const CANCEL_PLACE: readonly CommentSelectionEffect[] = [{ type: "cancel-place" }]
94
+
95
+ const UNMEASURED: CommentSelectionSize = { width: 0, height: 0 }
96
+
97
+ export function createCommentSelectionState(): CommentSelectionState {
98
+ return { status: "idle", issued: 0 }
99
+ }
100
+
101
+ export function transitionCommentSelection(
102
+ state: CommentSelectionState,
103
+ event: CommentSelectionEvent,
104
+ ): CommentSelectionTransition {
105
+ if (state.status === "disposed") return { state, effects: NONE }
106
+
107
+ if (event.type === "disposed") return { state: { status: "disposed" }, effects: cancel(state) }
108
+
109
+ if (event.type === "pointer.down") {
110
+ if (event.button !== 0) return { state, effects: NONE }
111
+
112
+ // A press elsewhere closes the toolbar; one in the viewer starts a selection that the release reads.
113
+ const status = event.inside ? "pressing" : "idle"
114
+
115
+ if (state.status === status) return { state, effects: NONE }
116
+
117
+ return { state: { status, issued: state.issued }, effects: cancel(state) }
118
+ }
119
+
120
+ if (event.type === "pointer.released") {
121
+ if (state.status !== "pressing") return { state, effects: NONE }
122
+
123
+ return settle(state, NONE)
124
+ }
125
+
126
+ if (event.type === "selection.changed") {
127
+ // A drag is read on release, and a selection made elsewhere is not this viewer's.
128
+ if (state.status !== "shown" && state.status !== "settling") return { state, effects: NONE }
129
+
130
+ return settle(state, cancel(state))
131
+ }
132
+
133
+ if (event.type === "settle.sampled") {
134
+ if (state.status !== "settling" || state.attempt !== event.attempt) return { state, effects: NONE }
135
+
136
+ if (!event.sample) return { state: { status: "idle", issued: state.issued }, effects: NONE }
137
+
138
+ return {
139
+ state: {
140
+ status: "shown",
141
+ issued: state.issued,
142
+ range: event.sample.range,
143
+ bounds: event.sample.bounds,
144
+ anchor: event.sample.anchor,
145
+ size: UNMEASURED,
146
+ placeAttempt: undefined,
147
+ },
148
+ effects: NONE,
149
+ }
150
+ }
151
+
152
+ if (event.type === "escape") {
153
+ if (state.status !== "shown" && state.status !== "settling") return { state, effects: NONE }
154
+
155
+ return { state: { status: "idle", issued: state.issued }, effects: cancel(state) }
156
+ }
157
+
158
+ if (state.status !== "shown") return { state, effects: NONE }
159
+
160
+ if (event.type === "layout.changed") {
161
+ // One frame places the toolbar for every scroll and resize before it.
162
+ if (state.placeAttempt !== undefined) return { state, effects: NONE }
163
+ const attempt = state.issued + 1
164
+
165
+ return {
166
+ state: { ...state, issued: attempt, placeAttempt: attempt },
167
+ effects: [{ type: "schedule-place", attempt }],
168
+ }
169
+ }
170
+
171
+ if (event.type === "place.sampled") {
172
+ if (state.placeAttempt !== event.attempt) return { state, effects: NONE }
173
+
174
+ // The selected rows left the viewer, such as when a virtualized diff dropped them.
175
+ if (!event.sample) return { state: { status: "idle", issued: state.issued }, effects: NONE }
176
+
177
+ return {
178
+ state: { ...state, bounds: event.sample.bounds, anchor: event.sample.anchor, placeAttempt: undefined },
179
+ effects: NONE,
180
+ }
181
+ }
182
+
183
+ if (event.type === "toolbar.measured") {
184
+ if (event.size.width === state.size.width && event.size.height === state.size.height)
185
+ return { state, effects: NONE }
186
+
187
+ return { state: { ...state, size: event.size }, effects: NONE }
188
+ }
189
+
190
+ return {
191
+ state: { status: "idle", issued: state.issued },
192
+ effects: [...cancel(state), { type: "clear-selection" }, { type: "open-draft", range: state.range }],
193
+ }
194
+ }
195
+
196
+ /**
197
+ * The toolbar's position, or undefined while it is closed or its selection is out of view. `docked` is for touch
198
+ * screens, which show their own menu over the selection.
199
+ */
200
+ export function selectCommentSelectionToolbar(state: CommentSelectionState, docked = false) {
201
+ if (state.status !== "shown" || !state.anchor) return
202
+
203
+ return {
204
+ place: commentSelectionPosition(state.size, state.bounds, state.anchor, docked),
205
+ measured: state.size.height !== 0,
206
+ }
207
+ }
208
+
209
+ /**
210
+ * Like the terminal's Quote: a one-line selection gets the toolbar centered above it, a taller one gets it beside the
211
+ * end the pointer stopped at, below a downward drag and above an upward one.
212
+ */
213
+ export function commentSelectionPosition(
214
+ size: CommentSelectionSize,
215
+ bounds: CommentSelectionRect,
216
+ anchor: CommentSelectionAnchor,
217
+ docked: boolean,
218
+ ) {
219
+ const gap = COMMENT_SELECTION_GAP
220
+ const pinned = bounds.bottom - size.height - gap
221
+
222
+ if (docked) return { top: pinned, left: (bounds.left + bounds.right - size.width) / 2 }
223
+ const above = anchor.top - size.height - gap
224
+ const below = anchor.bottom + gap
225
+ const fitsAbove = above >= bounds.top + gap && anchor.top <= bounds.bottom
226
+ const fitsBelow = below + size.height <= bounds.bottom - gap && anchor.bottom >= bounds.top
227
+ const prefersAbove = anchor.single || anchor.backward
228
+ const sides = prefersAbove ? [fitsAbove && above, fitsBelow && below] : [fitsBelow && below, fitsAbove && above]
229
+ const top = sides.find((side) => side !== false)
230
+
231
+ const center = anchor.single
232
+ ? (anchor.start + anchor.end - size.width) / 2
233
+ : (anchor.backward ? anchor.start : anchor.end) - size.width / 2
234
+
235
+ return {
236
+ top: top ?? pinned,
237
+ // A viewer narrower than the toolbar pins it to the left edge instead of past it.
238
+ left: Math.max(bounds.left + gap, Math.min(center, bounds.right - size.width - gap)),
239
+ }
240
+ }
241
+
242
+ function settle(state: Active, effects: readonly CommentSelectionEffect[]): CommentSelectionTransition {
243
+ const attempt = state.issued + 1
244
+
245
+ return {
246
+ state: { status: "settling", issued: attempt, attempt },
247
+ effects: [...effects, { type: "schedule-settle", attempt, delayMs: COMMENT_SELECTION_SETTLE_MS }],
248
+ }
249
+ }
250
+
251
+ function cancel(state: Active): readonly CommentSelectionEffect[] {
252
+ if (state.status === "settling") return CANCEL_SETTLE
253
+
254
+ if (state.status === "shown" && state.placeAttempt !== undefined) return CANCEL_PLACE
255
+
256
+ return NONE
257
+ }
@@ -0,0 +1,106 @@
1
+ import { type SelectedLineRange } from "@pierre/diffs"
2
+ import { diffLineIndex, diffRowIndex } from "./diff-selection"
3
+
4
+ export type CommentSide = "additions" | "deletions"
5
+
6
+ function annotationIndex(node: HTMLElement) {
7
+ const value = node.dataset.lineAnnotation?.split(",")[1]
8
+
9
+ if (!value) return
10
+ const line = parseInt(value, 10)
11
+
12
+ if (Number.isNaN(line)) return
13
+
14
+ return line
15
+ }
16
+
17
+ function clear(root: ShadowRoot) {
18
+ const marked = Array.from(root.querySelectorAll("[data-comment-selected]"))
19
+
20
+ for (const node of marked) {
21
+ if (!(node instanceof HTMLElement)) continue
22
+ node.removeAttribute("data-comment-selected")
23
+ }
24
+ }
25
+
26
+ export function markCommentedDiffLines(root: ShadowRoot, ranges: SelectedLineRange[]) {
27
+ clear(root)
28
+
29
+ const diffs = root.querySelector("[data-diff]")
30
+
31
+ if (!(diffs instanceof HTMLElement)) return
32
+
33
+ const split = diffs.dataset.diffType === "split"
34
+
35
+ const rows = Array.from(diffs.querySelectorAll("[data-line-index]")).filter(
36
+ (node): node is HTMLElement => node instanceof HTMLElement,
37
+ )
38
+
39
+ if (rows.length === 0) return
40
+
41
+ const annotations = Array.from(diffs.querySelectorAll("[data-line-annotation]")).filter(
42
+ (node): node is HTMLElement => node instanceof HTMLElement,
43
+ )
44
+
45
+ for (const range of ranges) {
46
+ const start = diffRowIndex(root, split, range.start, range.side as CommentSide | undefined)
47
+
48
+ if (start === undefined) continue
49
+
50
+ const end = (() => {
51
+ const same = range.end === range.start && (range.endSide == null || range.endSide === range.side)
52
+
53
+ if (same) return start
54
+
55
+ return diffRowIndex(root, split, range.end, (range.endSide ?? range.side) as CommentSide | undefined)
56
+ })()
57
+
58
+ if (end === undefined) continue
59
+
60
+ const first = Math.min(start, end)
61
+ const last = Math.max(start, end)
62
+
63
+ for (const row of rows) {
64
+ const idx = diffLineIndex(split, row)
65
+
66
+ if (idx === undefined || idx < first || idx > last) continue
67
+ row.setAttribute("data-comment-selected", "")
68
+ }
69
+
70
+ for (const annotation of annotations) {
71
+ const idx = annotationIndex(annotation)
72
+
73
+ if (idx === undefined || idx < first || idx > last) continue
74
+ annotation.setAttribute("data-comment-selected", "")
75
+ }
76
+ }
77
+ }
78
+
79
+ export function markCommentedFileLines(root: ShadowRoot, ranges: SelectedLineRange[]) {
80
+ clear(root)
81
+
82
+ const annotations = Array.from(root.querySelectorAll("[data-line-annotation]")).filter(
83
+ (node): node is HTMLElement => node instanceof HTMLElement,
84
+ )
85
+
86
+ for (const range of ranges) {
87
+ const start = Math.max(1, Math.min(range.start, range.end))
88
+ const end = Math.max(range.start, range.end)
89
+
90
+ for (let line = start; line <= end; line++) {
91
+ const nodes = Array.from(root.querySelectorAll(`[data-line="${line}"], [data-column-number="${line}"]`))
92
+
93
+ for (const node of nodes) {
94
+ if (!(node instanceof HTMLElement)) continue
95
+ node.setAttribute("data-comment-selected", "")
96
+ }
97
+ }
98
+
99
+ for (const annotation of annotations) {
100
+ const line = annotationIndex(annotation)
101
+
102
+ if (line === undefined || line < start || line > end) continue
103
+ annotation.setAttribute("data-comment-selected", "")
104
+ }
105
+ }
106
+ }