mirror of
https://github.com/bitbaum/chatkit.git
synced 2026-12-04 03:47:19 +01:00
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>
198 lines
9.3 KiB
TypeScript
198 lines
9.3 KiB
TypeScript
/**
|
|
* The one way a person types, shared by `Composer` and `DictationField`.
|
|
*
|
|
* Both boxes carry the same microphone and, when the app asks, the same
|
|
* paperclip: the same sheet (Camera · Photos · Files), the same staging with
|
|
* the same limits and notes, the same paste-to-attach, the same drop, the same
|
|
* strip of staged previews with Remove, and a recording that arrives as a file
|
|
* transcribed into the words rather than staged. Everything here is wiring the
|
|
* two components used to keep twice; it lives once, so they cannot drift.
|
|
*
|
|
* Since 0.43 the whole box is here too: the frame (`TypingBox`), the text and
|
|
* the microphone that adds to it (`useTypingText`), and the submit key
|
|
* (`submitKeyAction` in composer.ts). `Composer` and `DictationField` are two
|
|
* shapes of this one box: they differ only in what they put in its slots.
|
|
*
|
|
* Internal: not exported from the package. The public pieces stay where they
|
|
* were (`AttachMenu`, `useAttachments`, `AttachOptions`, `submitKeyAction`).
|
|
*/
|
|
import { type ClipboardEvent, type DragEvent, type FocusEventHandler, type KeyboardEvent, type ReactNode, type Ref } from "react";
|
|
import { type SubmitKeyEvent } from "../composer.js";
|
|
import { type DictationProblem } from "../dictation.js";
|
|
import { type AttachOptions, type AttachSource, type AttachmentNotes } from "../attachments.js";
|
|
import { type AttachmentsController } from "./use-attachments.js";
|
|
import { type DictationController, type UseDictationOptions } from "./use-dictation.js";
|
|
import { type AttachExtra, type AttachMenuLabels } from "./AttachMenu.js";
|
|
import { type VoiceLabels } from "./Voice.js";
|
|
/** The words of the microphone and its take, the same in every box. */
|
|
export type TypingVoiceLabels = {
|
|
voice: string;
|
|
voiceStop: string;
|
|
cancelRecording: string;
|
|
confirmRecording: string;
|
|
listening: string;
|
|
transcribing: string;
|
|
dismiss: string;
|
|
/** Re-send a take whose transcription failed (it is kept). */
|
|
retryTake: string;
|
|
dictation: Record<DictationProblem, string>;
|
|
};
|
|
export declare const DEFAULT_TYPING_VOICE_LABELS: TypingVoiceLabels;
|
|
/** The words of the paperclip and its staged files, the same in every box. */
|
|
export type TypingAttachLabels = {
|
|
attach: string;
|
|
/** The Camera / Photos / Files sheet the paperclip opens on a phone. */
|
|
attachMenu: Omit<AttachMenuLabels, "attach">;
|
|
remove: (name: string) => string;
|
|
/** Why a file did not attach. */
|
|
attachNotes: AttachmentNotes;
|
|
};
|
|
export declare const DEFAULT_TYPING_ATTACH_LABELS: TypingAttachLabels;
|
|
/** Overrides for the nested attach labels: each note and source optional. */
|
|
export type TypingAttachLabelOverrides = {
|
|
attachNotes?: Partial<AttachmentNotes>;
|
|
attachMenu?: Partial<Omit<TypingAttachLabels["attachMenu"], "sources">> & {
|
|
sources?: Partial<TypingAttachLabels["attachMenu"]["sources"]>;
|
|
};
|
|
};
|
|
/** The nested attach labels with an app's overrides, one level deep each. */
|
|
export declare function mergeAttachLabels(defaults: Pick<TypingAttachLabels, "attachNotes" | "attachMenu">, overrides: TypingAttachLabelOverrides | undefined): Pick<TypingAttachLabels, "attachNotes" | "attachMenu">;
|
|
export type TypingAttach = {
|
|
/** The paperclip is offered (`attach` is not `false`). */
|
|
on: boolean;
|
|
/** The staged files: the app's controller when it gave one, else the box's own. */
|
|
attachments: AttachmentsController;
|
|
/** The app takes files itself (`attach.take`): nothing is staged here. */
|
|
take: ((files: File[]) => void) | undefined;
|
|
/** Sources and accept for `AttachMenu`, from the app's options. */
|
|
sources: readonly AttachSource[] | undefined;
|
|
accept: Partial<Record<AttachSource, string>> | undefined;
|
|
/** Everything picked, dropped or handed over: staged, taken, or (a
|
|
* recording, where the microphone has a server leg) transcribed. */
|
|
onFiles: (files: FileList | File[] | null) => void;
|
|
/** Paste-to-attach: a pasted picture (or, with `take`, any pasted file) is
|
|
* attached; pasted text stays text. */
|
|
onPaste: (e: ClipboardEvent) => void;
|
|
/** A file dragged onto the box is a file picked. */
|
|
onDragOver: (e: DragEvent) => void;
|
|
onDrop: (e: DragEvent) => void;
|
|
};
|
|
/**
|
|
* The paperclip's behaviour for a box: one implementation for `Composer` and
|
|
* `DictationField`. `controller` is an `AttachmentsController` the APP owns
|
|
* (`useAttachments`), so it can read the staged files at its own submit and
|
|
* clear them after; without one the box stages into its own.
|
|
*/
|
|
export declare function useTypingAttach({ attach, controller, notes, dictation, voiceOn, disabled, }: {
|
|
attach: boolean | AttachOptions;
|
|
controller?: AttachmentsController;
|
|
notes: Partial<AttachmentNotes>;
|
|
dictation: DictationController;
|
|
/** The microphone is on (not `voice: false`); recordings go to its server leg. */
|
|
voiceOn: boolean;
|
|
disabled: boolean;
|
|
}): TypingAttach;
|
|
/** The staged files: a thumbnail or a name, each with Remove; and the note
|
|
* that says why a file did not attach. */
|
|
export declare function AttachmentStrip({ attachments, remove, }: {
|
|
attachments: AttachmentsController;
|
|
remove: (name: string) => string;
|
|
}): import("react").JSX.Element | null;
|
|
/** THE microphone button: one look, one set of states, in every box. */
|
|
export declare function MicButton({ dictation, disabled, labels, }: {
|
|
dictation: DictationController;
|
|
disabled: boolean;
|
|
labels: {
|
|
voice: string;
|
|
voiceStop: string;
|
|
};
|
|
}): import("react").JSX.Element;
|
|
/**
|
|
* What a page can do to a box it does not type into: `Composer`'s and
|
|
* `DictationField`'s `handleRef`, one shape.
|
|
*/
|
|
export type TypingHandle = {
|
|
/** Replace the words with these. */
|
|
setText: (text: string) => void;
|
|
/** Stage files exactly as the paperclip would (limits, shrinking, notes). */
|
|
addFiles: (files: FileList | File[]) => void;
|
|
/** Put the cursor in the box. */
|
|
focus: () => void;
|
|
/** Press submit: what the submit key does, with the same checks. */
|
|
submit: () => Promise<void>;
|
|
};
|
|
/** A key press as the submit rule reads it (`submitKeyAction`). */
|
|
export declare function submitKeyOf(e: KeyboardEvent): SubmitKeyEvent;
|
|
/** `handleRef`'s `addFiles`: what the paperclip would do with these files. */
|
|
export declare function addFilesThroughPaperclip(paperclip: TypingAttach, files: FileList | File[]): void;
|
|
/**
|
|
* The text of a box and the microphone that adds to it: one hookup for every
|
|
* box. What was said joins the words with one space (`appendTranscript`) and
|
|
* stays within `maxLength` (`clipToMaxLength`); a take that ends after more
|
|
* typing joins the LATEST words, never the ones the take started on.
|
|
*
|
|
* `commit` applies a new text (the box's own state, the app's
|
|
* `onValueChange`); `setText` is the only way the box changes its words.
|
|
*/
|
|
export declare function useTypingText({ text, commit, voice, maxLength, onSaid, }: {
|
|
text: string;
|
|
commit: (next: string) => void;
|
|
voice: Omit<UseDictationOptions, "onText"> | false;
|
|
maxLength?: number;
|
|
/** Runs before what was said lands (a field drops its Undo). */
|
|
onSaid?: () => void;
|
|
}): {
|
|
textRef: import("react").RefObject<string>;
|
|
setText: (next: string | ((prev: string) => string)) => void;
|
|
dictation: DictationController;
|
|
};
|
|
/** THE paperclip button: the same sheet, sources and app rows in every box. */
|
|
export declare function PaperclipButton({ paperclip, extras, disabled, labels, }: {
|
|
paperclip: TypingAttach;
|
|
extras: readonly AttachExtra[];
|
|
disabled: boolean;
|
|
labels: Pick<TypingAttachLabels, "attach" | "attachMenu">;
|
|
}): import("react").JSX.Element | null;
|
|
/**
|
|
* THE box a person types in, drawn once for `Composer` and `DictationField`:
|
|
*
|
|
* wrap
|
|
* above (composer: suggestion chips; field: its label)
|
|
* frame
|
|
* voice bar (covers the box while a take is live)
|
|
* box (a dropped file is a file picked)
|
|
* header (composer: modes, the app's header)
|
|
* strip (staged files, when `stagedPosition` is "above")
|
|
* input (the textarea or one-line input)
|
|
* strip (staged files, below: the default)
|
|
* tools (the tools row: paperclip, microphone, the rest)
|
|
* footer
|
|
* below (field: its error, Improve's panel, Undo)
|
|
* the take's problem, in words
|
|
* after (field: its hint)
|
|
*
|
|
* The class names are each shape's public hooks (`ck-composer…`, `ck-field…`)
|
|
* and stay as they were; the tools row is `ck-tools` in both, so a tool looks
|
|
* the same in every box.
|
|
*/
|
|
export declare function TypingBox({ wrapClassName, wrapRef, onWrapFocus, onWrapBlur, frameClassName, boxClassName, dictation, voiceLabels, paperclip, removeLabel, stagedPosition, above, header, input, tools, footer, below, after, }: {
|
|
wrapClassName: string;
|
|
wrapRef?: Ref<HTMLDivElement>;
|
|
onWrapFocus?: FocusEventHandler<HTMLDivElement>;
|
|
onWrapBlur?: FocusEventHandler<HTMLDivElement>;
|
|
frameClassName: string;
|
|
boxClassName: string;
|
|
dictation: DictationController;
|
|
voiceLabels: VoiceLabels;
|
|
paperclip: TypingAttach;
|
|
removeLabel: (name: string) => string;
|
|
stagedPosition?: "above" | "below";
|
|
above?: ReactNode;
|
|
header?: ReactNode;
|
|
input: ReactNode;
|
|
tools: ReactNode;
|
|
footer?: ReactNode;
|
|
below?: ReactNode;
|
|
after?: ReactNode;
|
|
}): import("react").JSX.Element;
|
|
//# sourceMappingURL=typing.d.ts.map
|