Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180<script module lang="ts"> export interface CodeReference { key: string; path: string; lines?: { start: number; end?: number }; }
const referenceRange = (lines: CodeReference["lines"]) => { if (!lines) return undefined; return lines.end !== undefined && lines.end !== lines.start ? `${lines.start}-${lines.end}` : `${lines.start}`; };</script>
<script lang="ts"> import type { KeyboardEventHandler } from "svelte/elements"; import SendHorizontal from "$icon/send-horizontal"; import Code from "$icon/code"; import X from "$icon/x"; import { createAction } from "$lib/action.svelte"; 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 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 lines = $derived(value.split("\n").length); const grown = $derived(lines > 1);
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);
const inert = $derived(disabled || publish.loading || value.trim() === "");
// ctrl/cmd+enter submits, mirroring MarkdownEditor's shortcut const handleKeydown: KeyboardEventHandler<HTMLTextAreaElement> = (e) => { if ((e.metaKey || e.ctrlKey) && e.key === "Enter") { e.preventDefault(); e.currentTarget.form?.requestSubmit(); } };</script>
{#snippet composer()} <div class={grown ? "flex flex-col" : "flex items-stretch"}> <Textarea bind:value bind:element={field} {placeholder} disabled={disabled || publish.loading} size="unsized" rows={1} onkeydown={handleKeydown} class="min-w-0 flex-1 {grown ? 'rounded-b-none' : 'rounded-r-none'}" /> {#if grown} <Button type="submit" icon={SendHorizontal} iconSide="right" size="small" class="-mt-px h-9 w-full rounded-t-none disabled:border-border-default disabled:bg-background-default" loading={publish.loading} disabled={inert} > Send comment </Button> {:else} <Button type="submit" icon={SendHorizontal} size="small" aria-label="Send comment" class="-ml-px w-9.5 self-stretch [--btn-radius-l:0px] disabled:border-border-default disabled:bg-background-default" loading={publish.loading} disabled={inert} /> {/if} </div>{/snippet}
<form onsubmit={publish.run} class="flex flex-col gap-2"> {#if framed} <div class={[ "rounded-sm border bg-background-subtle p-1", disabled ? "border-border-disabled" : "border-border-default" ]} > {#if references.length > 0} <ul> {#each references as reference (reference.key)} {@const range = referenceRange(reference.lines)} <li class="flex h-10 items-center gap-2 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 h-9 w-9.5 text-foreground-muted" aria-label="Remove {reference.path}" {disabled} onclick={() => onremovereference?.(reference)} /> {/if} </li> {/each} </ul> {:else} <Checkbox bind:checked={pinToCommit} {disabled} class="h-8 px-1.5"> <span class="typography-paragraph-small"> Comment on {short(currentCommit)} </span> </Checkbox> {/if}
{@render composer()} </div> {:else} {@render composer()} {/if}
<ErrorAlert label={publish.error} /></form>