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

T3ComposerEditor.tsx

73 lines2.2 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 onPasteText: _onPasteText,
16 style,
17 textStyle,
18 contentInsetVertical = 0,
19 singleLineCentered: _singleLineCentered,
20 enterBehavior: _enterBehavior,
21 readOnly = false,
22 onContentSizeChange,
23 ...props
24}: ComposerEditorProps) {
25 const inputRef = useRef<TextInputInstance>(null);
26 const bodyText = useScaledTextRole("body");
27 const fontFamily = FONT_FAMILIES.regular;
28 const handlePaste = useNativePaste((uris) => onPasteImages?.(uris));
29​
30 useImperativeHandle(
31 ref,
32 () => ({
33 focus: () => inputRef.current?.focus(),
34 blur: () => inputRef.current?.blur(),
35 setSelection: (nextSelection) =>
36 inputRef.current?.setSelection(nextSelection.start, nextSelection.end),
37 }),
38 [],
39 );
40​
41 return (
42 <TextInputWrapper onPaste={handlePaste} style={[{ minHeight: 0 }, style]}>
43 <TextInput
44 ref={inputRef}
45 {...props}
46 editable={(props.editable ?? true) && !readOnly}
47 selection={selection}
48 onSelectionChange={(event) => props.onSelectionChange?.(event.nativeEvent.selection)}
49 onContentSizeChange={(event) => onContentSizeChange?.(event.nativeEvent.contentSize)}
50 multiline={props.multiline ?? true}
51 placeholderTextColorClassName={"accent-placeholder"}
52 className="text-foreground"
53 style={[
54 {
55 flex: 1,
56 minHeight: 0,
57 fontFamily,
58 ...bodyText,
59 paddingVertical: contentInsetVertical,
60 },
61 textStyle,
62 ]}
63 />
64 </TextInputWrapper>
65 );
67​
68export type {
69 ComposerEditorHandle,
70 ComposerEditorProps,
71 ComposerEditorSelection,
72 ComposerTextPaste,
73} from "./T3ComposerEditor.types";