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

88 lines
3.7 KiB
JavaScript

/**
* The composer's decisions, kept out of the component so they can be tested
* without a DOM — and so every app that uses the one composer gets exactly the
* same answer to "may this send?" and "does the draft survive?".
*
* From loki `src/components/composer/composer-logic.ts`, where these were
* written after four composers in one app gave four different answers.
*/
export function composerCanSend({ text, attachmentCount, attachmentOnlyText, sending, disabled, blocked, queue = false, }) {
if (disabled || blocked)
return false;
if (sending && !queue)
return false;
if (text.trim().length > 0)
return true;
// A screenshot with no words is still a complete message — but only where
// the app has said what that message means.
return attachmentCount > 0 && Boolean(attachmentOnlyText);
}
/** The words actually sent: the draft, or the app's stand-in for an
* attachments-only send. */
export function composerOutgoingText(text, attachmentCount, attachmentOnlyText) {
const trimmed = text.trim();
if (trimmed)
return trimmed;
return attachmentCount > 0 && attachmentOnlyText ? attachmentOnlyText : "";
}
export function shouldClearDraft(result) {
return result !== false;
}
/** Dictated words join the draft with one space, never glued to the last word. */
export function appendTranscript(draft, said) {
const t = said.trim();
if (!t)
return draft;
return draft.trim() ? `${draft.replace(/\s+$/, "")} ${t}` : t;
}
/**
* Text a field sets itself (a dictated take, a rewrite) kept within the
* field's `maxLength`. The attribute stops typing and pasting at the limit
* and nothing else: a value set in code goes straight past it, and the form
* then refuses or cuts what the person said. Cut at the limit, on a word
* where one is near, so the field never holds more than it may send.
*/
export function clipToMaxLength(text, maxLength) {
if (!maxLength || maxLength < 0 || text.length <= maxLength)
return text;
const cut = text.slice(0, maxLength);
const lastSpace = cut.lastIndexOf(" ");
// Back to the last whole word when that loses little (under 24 characters).
return lastSpace > 0 && maxLength - lastSpace <= 24 ? cut.slice(0, lastSpace) : cut;
}
/**
* THE submit-key rule, the same in every box of every app:
* - a composer: Enter sends, Shift+Enter breaks the line;
* - a multi-line field: Cmd+Enter or Ctrl+Enter submits, Enter breaks the line;
* - a one-line field: Enter submits; Shift+Enter and Alt+Enter do nothing
* (the browser would otherwise post the native form).
* Never during an IME composition (an Enter that chooses a word is left to
* the browser), and never while a take is recorded or transcribed (`busy`) or
* the box may not change (`locked`: disabled, read-only) — then the key that
* would submit is swallowed, so a form is not posted mid-take without the
* words.
*/
export function submitKeyAction(event, { shape, busy = false, locked = false }) {
if (event.key !== "Enter")
return "type";
if (event.isComposing || event.keyCode === 229)
return "type";
if (shape === "composer") {
if (event.shiftKey)
return "type";
return busy || locked ? "block" : "submit";
}
if (shape === "multiline" && !(event.metaKey || event.ctrlKey))
return "type";
if (busy || locked)
return "block";
if (shape === "single" && (event.shiftKey || event.altKey))
return "block";
return "submit";
}
/** `75` → `1:15`. The recording timer. */
export function formatElapsed(seconds) {
const s = Math.max(0, Math.floor(seconds));
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
}
//# sourceMappingURL=composer.js.map