File size: 9,949 Bytes
9697ede | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | import { For, Show, createSignal, onMount, splitProps, type ComponentProps, type JSX } from "solid-js"
import { FileIcon } from "../../components/file-icon"
import { useI18n } from "../../context/i18n"
import { useFilteredList } from "../../hooks"
import { ButtonV2 } from "./button-v2"
import "./line-comment-v2.css"
/** Horizontal “more” glyph for the display-card overflow control (Figma outline-dots). */
export function LineCommentV2OverflowIcon(props: ComponentProps<"svg">) {
return (
<svg
{...props}
width={props.width ?? 16}
height={props.height ?? 16}
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
aria-hidden={props["aria-hidden"] ?? "true"}
>
<path d="M2.5 7.5H3.5V8.5H2.5V7.5Z" stroke="currentColor" />
<path d="M7.5 7.5H8.5V8.5H7.5V7.5Z" stroke="currentColor" />
<path d="M12.5 7.5H13.5V8.5H12.5V7.5Z" stroke="currentColor" />
</svg>
)
}
export interface LineCommentV2Props extends ComponentProps<"div"> {
/** Main comment body (text or rich content). */
comment: JSX.Element
/** Line / selection context (e.g. “Comment on line 40”). */
selection: JSX.Element
/** Typically an overflow menu trigger; use `LineCommentV2OverflowIcon` inside `line-comment-v2-overflow`. */
actions?: JSX.Element
}
export function LineCommentV2(props: LineCommentV2Props) {
const [local, rest] = splitProps(props, ["comment", "selection", "actions", "class", "classList"])
return (
<div
{...rest}
data-component="line-comment-v2"
data-variant="display"
classList={{
...local.classList,
[local.class ?? ""]: !!local.class,
}}
>
<div data-slot="line-comment-v2-shell">
<div data-slot="line-comment-v2-column">
<div data-slot="line-comment-v2-text">{local.comment}</div>
<div data-slot="line-comment-v2-meta">{local.selection}</div>
</div>
<Show when={local.actions}>{(actions) => <div data-slot="line-comment-v2-tools">{actions()}</div>}</Show>
</div>
</div>
)
}
export type LineCommentEditorV2Mention = {
items: (query: string) => string[] | Promise<string[]>
}
export interface LineCommentEditorV2Props extends Omit<ComponentProps<"div">, "children" | "onInput" | "onSubmit"> {
/** Visible field label above the textarea (default: “Comment”). */
heading?: JSX.Element | string
value: string
onInput: (value: string) => void
onCancel: () => void
onSubmit: (value: string) => void
selection: JSX.Element
placeholder?: string
rows?: number
cancelLabel?: string
submitLabel?: string
autofocus?: boolean
mention?: LineCommentEditorV2Mention
}
function pathFilename(path: string) {
const index = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"))
return index === -1 ? path : path.slice(index + 1)
}
function pathDirectory(path: string) {
const index = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"))
return index === -1 ? "" : path.slice(0, index + 1)
}
export function LineCommentEditorV2(props: LineCommentEditorV2Props) {
const i18n = useI18n()
let textareaRef: HTMLTextAreaElement | undefined
const [mentionOpen, setMentionOpen] = createSignal(false)
const [local, rest] = splitProps(props, [
"heading",
"value",
"onInput",
"onCancel",
"onSubmit",
"selection",
"placeholder",
"rows",
"cancelLabel",
"submitLabel",
"autofocus",
"mention",
"class",
"classList",
])
const heading = () => local.heading ?? i18n.t("ui.lineComment.submit")
const canSubmit = () => local.value.trim().length > 0
const closeMention = () => {
setMentionOpen(false)
mention.clear()
}
const currentMention = () => {
const textarea = textareaRef
if (!textarea) return
if (!local.mention) return
if (textarea.selectionStart !== textarea.selectionEnd) return
const end = textarea.selectionStart
const match = textarea.value.slice(0, end).match(/@(\S*)$/)
if (!match) return
return {
query: match[1] ?? "",
start: end - match[0].length,
end,
}
}
function selectMention(item: { path: string } | undefined) {
if (!item) return
const textarea = textareaRef
const query = currentMention()
if (!textarea || !query) return
const value = `${textarea.value.slice(0, query.start)}@${item.path} ${textarea.value.slice(query.end)}`
const cursor = query.start + item.path.length + 2
local.onInput(value)
closeMention()
requestAnimationFrame(() => {
textarea.focus()
textarea.setSelectionRange(cursor, cursor)
})
}
const mention = useFilteredList<{ path: string }>({
items: async (query) => {
if (!local.mention) return []
if (!query.trim()) return []
const paths = await local.mention.items(query)
return paths.map((path) => ({ path }))
},
key: (item) => item.path,
filterKeys: ["path"],
skipFilter: () => true,
onSelect: selectMention,
})
const syncMention = () => {
const item = currentMention()
if (!item) {
closeMention()
return
}
setMentionOpen(true)
mention.onInput(item.query)
}
const selectActiveMention = () => {
const items = mention.flat()
if (items.length === 0) return
const active = mention.active()
selectMention(items.find((item) => item.path === active) ?? items[0])
}
const submit = () => {
const v = local.value.trim()
if (!v) return
local.onSubmit(v)
}
onMount(() => {
if (local.autofocus === false) return
requestAnimationFrame(() => textareaRef?.focus())
})
return (
<div
{...rest}
data-component="line-comment-v2"
data-variant="editor"
classList={{
...local.classList,
[local.class ?? ""]: !!local.class,
}}
>
<div data-slot="line-comment-v2-shell">
<div data-slot="line-comment-v2-field">
<div data-slot="line-comment-v2-label">{heading()}</div>
<textarea
ref={(el) => {
textareaRef = el
}}
data-slot="line-comment-v2-textarea"
rows={local.rows ?? 3}
placeholder={local.placeholder ?? i18n.t("ui.lineComment.contextPlaceholder")}
value={local.value}
onInput={(e) => {
local.onInput(e.currentTarget.value)
syncMention()
}}
onClick={() => syncMention()}
onSelect={() => syncMention()}
onKeyDown={(e) => {
e.stopPropagation()
if (e.isComposing || e.keyCode === 229) return
if (mentionOpen()) {
if (e.key === "Escape") {
e.preventDefault()
closeMention()
return
}
if (e.key === "Tab") {
if (mention.flat().length === 0) return
e.preventDefault()
selectActiveMention()
return
}
const nav = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "Enter"
const ctrlNav = e.ctrlKey && !e.metaKey && !e.altKey && !e.shiftKey && (e.key === "n" || e.key === "p")
if ((nav || ctrlNav) && mention.flat().length > 0) {
mention.onKeyDown(e)
e.preventDefault()
return
}
}
if (e.key === "Escape") {
e.preventDefault()
e.currentTarget.blur()
local.onCancel()
return
}
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault()
submit()
}
}}
/>
<Show when={mentionOpen() && mention.flat().length > 0}>
<div data-slot="line-comment-v2-mention-list">
<For each={mention.flat().slice(0, 10)}>
{(item) => {
const directory = item.path.endsWith("/") ? item.path : pathDirectory(item.path)
const name = item.path.endsWith("/") ? "" : pathFilename(item.path)
return (
<button
type="button"
data-slot="line-comment-v2-mention-item"
data-active={mention.active() === item.path ? "" : undefined}
onMouseDown={(event) => event.preventDefault()}
onMouseEnter={() => mention.setActive(item.path)}
onClick={() => selectMention(item)}
>
<FileIcon node={{ path: item.path, type: "file" }} class="shrink-0 size-4" />
<div data-slot="line-comment-v2-mention-path">
<span data-slot="line-comment-v2-mention-dir">{directory}</span>
<Show when={name}>
<span data-slot="line-comment-v2-mention-file">{name}</span>
</Show>
</div>
</button>
)
}}
</For>
</div>
</Show>
</div>
<div data-slot="line-comment-v2-footer">
<div data-slot="line-comment-v2-footer-meta">{local.selection}</div>
<div data-slot="line-comment-v2-footer-actions">
<ButtonV2 type="button" size="normal" variant="neutral" onClick={() => local.onCancel()}>
{local.cancelLabel ?? i18n.t("ui.lineComment.cancel")}
</ButtonV2>
<ButtonV2 type="button" size="normal" variant="contrast" disabled={!canSubmit()} onClick={submit}>
{local.submitLabel ?? i18n.t("ui.lineComment.submit")}
</ButtonV2>
</div>
</div>
</div>
</div>
)
}
|