Files
CatoandClaude fd7031ea04 feat(composer, field): one box — Composer and DictationField are two shapes of one implementation (0.43.0) (#111)
The four steps 0.41 left in the README are done:
- One frame: TypingBox (typing.tsx) draws the voice bar, box, strip, text,
  tools row and the take's problem for both. The field's tools row is the
  composer's ck-tools, so its paperclip and microphone are bare icons like
  the composer's (they were bordered circles). Frame and text share one set
  of rules in styles.css; each shape keeps only its differences.
- One text hookup: useTypingText (dictation + appendTranscript +
  clipToMaxLength + the latest-text ref).
- One submit-key rule: submitKeyAction in composer.ts, exported.
- Same controls on both: Composer `attachments`; DictationField
  `handleRef` (DictationFieldHandle) and `attachExtras`.

Public APIs are additive. All existing tests unchanged; new unit, render
and browser checks (390px, 320px dark, desktop).


Claude-Session: https://claude.ai/code/session_019MDtmv1SAs6YZn4FrewbXR

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-15 16:50:56 +01:00

136 lines
8.4 KiB
JavaScript

"use client";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useImperativeHandle, useRef, useState, } from "react";
import { composerCanSend, composerOutgoingText, shouldClearDraft, submitKeyAction, } from "../composer.js";
import { useAutoGrow } from "./hooks.js";
import { DEFAULT_TYPING_ATTACH_LABELS, DEFAULT_TYPING_VOICE_LABELS, MicButton, PaperclipButton, TypingBox, submitKeyOf, addFilesThroughPaperclip, mergeAttachLabels, useTypingAttach, useTypingText, } from "./typing.js";
import { IconArrowUp, IconSpinner, IconStop } from "./icons.js";
export const DEFAULT_COMPOSER_LABELS = {
send: "Send",
stop: "Stop generating",
// The microphone's and the paperclip's words are every box's (typing.tsx).
...DEFAULT_TYPING_VOICE_LABELS,
...DEFAULT_TYPING_ATTACH_LABELS,
queue: "Queue a message…",
};
/**
* THE composer: write words — or say them, or paste a screenshot — and send
* them. Enter sends, Shift+Enter breaks the line, an IME composition never
* sends half a word. 16px text, so iOS never zooms the page on focus.
*
* Extracted from loki `components/composer/Composer.tsx` (itself the merge of
* four composers in one app), with heidi's microphone and orangecat's lessons.
*/
export function Composer({ onSend, placeholder, ariaLabel, disabled = false, sendBlockedReason = null, emptyAction, sending = false, onStop, attachmentOnlyText, heldAttachments = 0, queue = false, attach = false, attachments: attachmentsGiven, attachExtras, handleRef, voice = {}, modes, mode, onModeChange, onEmptySlash, value, onValueChange, defaultValue = "", inputRef, density = "comfortable", layout = "stacked", attachmentsAlone = false, above, header, tools, trailing, footer, hint, stagedPosition = "below", labels: labelOverrides, autoFocus, }) {
const labels = {
...DEFAULT_COMPOSER_LABELS,
...labelOverrides,
dictation: { ...DEFAULT_COMPOSER_LABELS.dictation, ...labelOverrides?.dictation },
...mergeAttachLabels(DEFAULT_COMPOSER_LABELS, labelOverrides),
};
const [ownText, setOwnText] = useState(defaultValue);
const controlled = value !== undefined;
const text = controlled ? value : ownText;
// The words and the microphone that adds to them: every box's (typing.tsx).
// `textRef` is the CURRENT draft, for an async send.
const { textRef, setText, dictation } = useTypingText({
text,
commit: (next) => {
if (!controlled)
setOwnText(next);
onValueChange?.(next);
},
voice,
});
const localRef = useRef(null);
const textareaRef = inputRef ?? localRef;
useAutoGrow(textareaRef, text);
const voiceOn = voice !== false && dictation.supported;
const listening = dictation.status === "listening";
const transcribing = dictation.status === "transcribing";
// The paperclip, the same as every other box's (typing.tsx). `take`: the
// app takes the files itself, nothing is staged here.
const paperclip = useTypingAttach({
attach,
controller: attachmentsGiven,
notes: labels.attachNotes,
dictation,
voiceOn: voice !== false,
disabled,
});
const { on: attachOn, attachments } = paperclip;
const attachCount = (attachOn ? attachments.attachments.length : 0) + Math.max(0, heldAttachments);
// A message of only attachments may go out wordless: to the rule below that
// is "the app has said what such a message means", and it means nothing.
const wordless = attachmentsAlone && !attachmentOnlyText;
const canSend = composerCanSend({
text,
attachmentCount: attachCount,
attachmentOnlyText: wordless ? "\u2060" : attachmentOnlyText,
sending,
disabled,
blocked: Boolean(sendBlockedReason),
queue,
});
const submit = async () => {
if (!canSend)
return;
const sent = text;
const outgoing = composerOutgoingText(sent, attachCount, wordless ? undefined : attachmentOnlyText);
const result = await onSend(outgoing, attachOn ? attachments.toWire() : []);
if (!shouldClearDraft(result))
return;
// Only clear what was sent: words typed while a slow send was in flight
// are the next message.
if (textRef.current === sent)
setText("");
attachments.clear();
};
// The handle presses the CURRENT Send, never the one its first render saw.
const submitRef = useRef(submit);
useEffect(() => {
submitRef.current = submit;
});
useImperativeHandle(handleRef, () => ({
setText: (next) => setText(next),
addFiles: (files) => addFilesThroughPaperclip(paperclip, files),
focus: () => textareaRef.current?.focus(),
submit: () => submitRef.current(),
}));
const showModes = Boolean(modes && modes.length > 1);
const inline = layout === "inline";
const extras = attachExtras ?? [];
// One microphone, drawn where the layout puts it: among the tools of a
// stacked box, beside Send in an inline one (where the thumb is).
const mic = _jsx(MicButton, { dictation: dictation, disabled: disabled, labels: labels });
const input = (_jsx("textarea", { ref: textareaRef, className: "ck-input", rows: 1, value: text, autoFocus: autoFocus, disabled: disabled || transcribing, placeholder: listening ? labels.listening : sending && queue ? labels.queue : placeholder, "aria-label": ariaLabel ?? placeholder, enterKeyHint: "send", onChange: (e) => {
const next = e.target.value;
if (onEmptySlash && next === "/" && text === "") {
onEmptySlash();
return;
}
setText(next);
}, onPaste: paperclip.onPaste, onKeyDown: (e) => {
// The one submit-key rule (composer.ts): Enter sends, Shift+Enter
// breaks the line, never mid-word in an IME, never mid-take.
const action = submitKeyAction(submitKeyOf(e), {
shape: "composer",
busy: dictation.status !== "idle",
});
if (action === "type")
return;
e.preventDefault();
if (action === "submit")
void submit();
} }));
const boxHeader = (showModes || header) && (_jsxs(_Fragment, { children: [showModes && (_jsx("div", { className: "ck-modes", role: "group", children: modes.map((m) => (_jsx("button", { type: "button", className: "ck-mode", "aria-pressed": m.id === mode, title: m.hint, onClick: () => onModeChange?.(m.id), children: m.label }, m.id))) })), header] }));
return (_jsx(TypingBox, { wrapClassName: "ck-composer-wrap", frameClassName: "ck-composer-frame", boxClassName: [
"ck-composer",
density === "compact" ? "ck-composer-compact" : "",
inline ? "ck-composer-inline" : "",
]
.filter(Boolean)
.join(" "), dictation: dictation, voiceLabels: labels, paperclip: paperclip, removeLabel: labels.remove, stagedPosition: stagedPosition, above: above, header: boxHeader, input: input, footer: footer, tools: _jsxs("div", { className: "ck-actions", children: [_jsxs("div", { className: "ck-tools", children: [_jsx(PaperclipButton, { paperclip: paperclip, extras: extras, disabled: disabled, labels: labels }), voiceOn && !inline && mic, tools, hint && _jsx("span", { className: "ck-hint", children: hint })] }), _jsxs("div", { className: "ck-submit", children: [trailing, voiceOn && inline && mic, sending && onStop ? (_jsxs(_Fragment, { children: [queue && canSend && (_jsx("button", { type: "button", className: "ck-send", onClick: () => void submit(), "aria-label": labels.send, title: labels.send, children: _jsx(IconArrowUp, {}) })), _jsx("button", { type: "button", className: "ck-send ck-send-stop", onClick: onStop, "aria-label": labels.stop, title: labels.stop, children: _jsx(IconStop, {}) })] })) : (!listening &&
(emptyAction && !canSend && !sending && !text.trim() ? (_jsx("button", { type: "button", className: "ck-send ck-send-empty", disabled: disabled, onClick: emptyAction.onPress, "aria-label": emptyAction.label, title: emptyAction.label, children: emptyAction.icon })) : (_jsx("button", { type: "button", className: "ck-send", disabled: !canSend, onClick: () => void submit(), "aria-label": labels.send, title: sendBlockedReason ?? labels.send, children: sending ? _jsx(IconSpinner, {}) : _jsx(IconArrowUp, {}) }))))] })] }) }));
}
//# sourceMappingURL=Composer.js.map