Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

T3ComposerEditor.tsx

76 lines2.3 KB

1import { TextInputWrapper } from "expo-paste-input";
2import { useImperativeHandle, useRef } from "react";
3import { TextInput, type TextInputInstance } from "react-native";
4​
5import { FONT_FAMILIES } from "../lib/fontFamilies";
6import { useScaledTextRole } from "../features/settings/appearance/useScaledTextRole";
7import { useNativePaste } from "../lib/useNativePaste";
8import type { ComposerEditorProps } from "./T3ComposerEditor.types";
9​
10export function ComposerEditor({
11 ref,
12 skills: _skills,
13 selection,
14 onPasteImages,
15 onPasteImagesPending: _onPasteImagesPending,
16 onPasteText: _onPasteText,
17 style,
18 textStyle,
19 contentInsetVertical = 0,
20 singleLineCentered: _singleLineCentered,
21 enterBehavior: _enterBehavior,
22 readResetGeneration: _readResetGeneration,
23 readOnly = false,
24 onContentSizeChange,
25 ...props
26}: ComposerEditorProps) {
27 const inputRef = useRef<TextInputInstance>(null);
28 const bodyText = useScaledTextRole("body");
29 const fontFamily = FONT_FAMILIES.regular;
30 const handlePaste = useNativePaste((uris) => onPasteImages?.(uris));
31​
32 useImperativeHandle(
33 ref,
34 () => ({
35 focus: () => inputRef.current?.focus(),
36 blur: () => inputRef.current?.blur(),
37 setSelection: (nextSelection) =>
38 inputRef.current?.setSelection(nextSelection.start, nextSelection.end),
39 flushPendingInput: async () => {},
40 }),
41 [],
42 );
43​
44 return (
45 <TextInputWrapper onPaste={handlePaste} style={[{ minHeight: 0 }, style]}>
46 <TextInput
47 ref={inputRef}
48 {...props}
49 editable={(props.editable ?? true) && !readOnly}
50 selection={selection}
51 onSelectionChange={(event) => props.onSelectionChange?.(event.nativeEvent.selection)}
52 onContentSizeChange={(event) => onContentSizeChange?.(event.nativeEvent.contentSize)}
53 multiline={props.multiline ?? true}
54 placeholderTextColorClassName={"accent-placeholder"}
55 className="text-foreground"
56 style={[
57 {
58 flex: 1,
59 minHeight: 0,
60 fontFamily,
61 ...bodyText,
62 paddingVertical: contentInsetVertical,
63 },
64 textStyle,
65 ]}
66 />
67 </TextInputWrapper>
68 );
70​
71export type {
72 ComposerEditorHandle,
73 ComposerEditorProps,
74 ComposerEditorSelection,
75 ComposerTextPaste,
76} from "./T3ComposerEditor.types";