Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202<script module lang="ts"> import { lineNumbers } from "#lib/components/repo/lineAnchor.ts";
export interface CodeReference { key: string; path: string; lines?: { start: number; end?: number }; }
export interface CommentFormApi { insert: (text: string) => void; }
const referenceRange = (lines: CodeReference["lines"]) => lines && lineNumbers({ start: lines.start, end: lines.end ?? lines.start });</script>
<script lang="ts"> import type { KeyboardEventHandler } from "svelte/elements"; import { onMount } from "svelte"; import Code from "$icon/code"; import X from "$icon/x"; import { applyEdit, insertInline } from "#lib/components/ui/markdownFormat.ts"; import { createAction } from "#lib/action.svelte.ts"; import Button from "#lib/components/ui/Button.svelte"; import Checkbox from "#lib/components/ui/Checkbox.svelte"; import ErrorAlert from "#lib/components/ui/Error.svelte"; import Kbd from "#lib/components/ui/Kbd.svelte"; import { isMac, keyLabel } from "#lib/components/ui/keyLabels.ts"; import LayerCard from "#lib/components/ui/LayerCard.svelte"; import LayerCardSecondary from "#lib/components/ui/LayerCardSecondary.svelte"; import Textarea from "#lib/components/ui/Textarea.svelte";
interface Props { currentCommit?: string; value?: string; pinToCommit?: boolean; allowPinToCommit?: boolean; references?: CodeReference[]; onremovereference?: (reference: CodeReference) => void; placeholder?: string; disabled?: boolean; onsubmit?: (body: string, commit?: string) => void | Promise<void>; }
let { currentCommit = "", value = $bindable(""), pinToCommit = $bindable(false), allowPinToCommit = true, references = [], onremovereference, placeholder = "Write your comment here", disabled = false, onsubmit }: Props = $props();
const publish = createAction(async (event: SubmitEvent) => { event.preventDefault(); const body = value.trim(); if (!body) return; await onsubmit?.(body, pinToCommit ? currentCommit : undefined); // cleared only once the handler resolves, so a failed write doesn't eat what was typed value = ""; });
const pinnable = $derived(allowPinToCommit && references.length === 0); const framed = $derived(references.length > 0 || pinnable);
let field = $state<HTMLTextAreaElement>();
$effect(() => { const el = field; if (!el) return; void value; el.style.height = "auto"; el.style.height = `${Math.min(el.scrollHeight, 160)}px`; });
const short = (hash: string) => hash.slice(0, 8);
let mac = $state(false); onMount(() => { mac = isMac(); });
export const insert: CommentFormApi["insert"] = (text) => { const el = field; if (!el || el.disabled) return; const selection = { value: el.value, start: el.selectionStart, end: el.selectionEnd }; applyEdit(el, insertInline(selection, text)); };
const inert = $derived(disabled || publish.loading || value.trim() === "");
// ctrl/cmd+enter submits, mirroring MarkdownEditor's shortcut const handleKeydown: KeyboardEventHandler<HTMLTextAreaElement> = (e) => { if (e.key !== "Enter" || e.isComposing) return; if (e.metaKey || e.ctrlKey) { e.preventDefault(); e.currentTarget.form?.requestSubmit(); } };</script>
{#snippet composer()} <div class="relative"> <Textarea data-testid="reviewComment" bind:value bind:element={field} {placeholder} disabled={disabled || publish.loading} size="unsized" rows={3} onkeydown={handleKeydown} class="pb-5 pointer-coarse:pb-0" /> <p aria-hidden={value === ""} class={[ "pointer-events-none absolute bottom-1.5 left-2.5 flex items-center gap-md typography-paragraph-mini text-foreground-subtle transition-opacity duration-150 ease-out motion-reduce:transition-none pointer-coarse:hidden", value === "" && "opacity-0" ]} > <span class="flex items-center gap-2xs"> <Kbd>{keyLabel("enter", mac)}</Kbd> Newline </span> <span class="flex items-center gap-2xs"> <span class="flex items-center gap-3xs"> <Kbd>{keyLabel("mod", mac)}</Kbd> <Kbd>{keyLabel("enter", mac)}</Kbd> </span> Send </span> </p> </div> <Button type="submit" size="small" class="hidden w-full pointer-coarse:flex" loading={publish.loading} disabled={inert} > Send comment </Button>{/snippet}
<form onsubmit={publish.run} class="flex flex-col gap-xs"> {#if framed} <LayerCard class={disabled ? "ring-border-disabled" : undefined}> {#if references.length > 0} <ul> {#each references as reference (reference.key)} {@const range = referenceRange(reference.lines)} <li class="flex h-10 items-center gap-xs px-1.5 typography-paragraph-small"> <Code class="size-3.5 shrink-0 text-foreground-subtle" aria-hidden="true" /> <span class="min-w-0 truncate text-foreground-default"> {reference.path} </span> {#if range} <span class="shrink-0 text-foreground-muted">{range}</span> {/if} {#if onremovereference} <Button variant="ghost" size="small" icon={X} class="ml-auto size-9 text-foreground-muted" aria-label="Remove {reference.path}" {disabled} onclick={() => onremovereference?.(reference)} /> {/if} </li> {/each} </ul> {:else} <LayerCardSecondary class="px-1.5 py-0.5"> <Checkbox bind:checked={pinToCommit} {disabled} class="h-xl"> <span class="typography-paragraph-small"> Comment on <span class="typography-monospace-small" >{short(currentCommit)}</span > </span> </Checkbox> </LayerCardSecondary> {/if}
{@render composer()} </LayerCard> {:else} {@render composer()} {/if}
<ErrorAlert label={publish.error} /></form>