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

T3ComposerEditor.ios.tsx

187 lines6.5 KB

1import { collectComposerInlineTokens } from "@t3tools/shared/composerInlineTokens";
2import { requireNativeView } from "expo";
3import { useImperativeHandle, useMemo, useRef, type Ref } from "react";
4import type { NativeSyntheticEvent, StyleProp, ViewProps, ViewStyle } from "react-native";
5import { Image, StyleSheet } from "react-native";
6​
7import { markdownFileIconSource } from "@t3tools/mobile-markdown-text/file-icons";
8import { resolveMarkdownFileIcon } from "@t3tools/mobile-markdown-text/links";
9import { MOBILE_TYPOGRAPHY } from "../lib/typography";
10import { useThemeColor } from "../lib/useThemeColor";
11import type { ComposerEditorProps, ComposerEditorSelection } from "./T3ComposerEditor.types";
12​
13const NATIVE_MODULE_NAME = "T3ComposerEditor";
14const EMPTY_SKILLS: NonNullable<ComposerEditorProps["skills"]> = [];
15​
16type NativeEditorEvent = NativeSyntheticEvent<{
17 readonly value: string;
18 readonly selection: ComposerEditorSelection;
19}>;
20​
21type NativeSelectionEvent = NativeSyntheticEvent<{
22 readonly selection: ComposerEditorSelection;
23}>;
24​
25type NativePasteImagesEvent = NativeSyntheticEvent<{
26 readonly uris: ReadonlyArray<string>;
27}>;
28​
29interface NativeComposerEditorRef {
30 focus: () => Promise<void>;
31 blur: () => Promise<void>;
32 setSelection: (start: number, end: number) => Promise<void>;
34​
35interface NativeComposerEditorProps extends ViewProps {
36 readonly ref?: Ref<NativeComposerEditorRef>;
37 readonly value: string;
38 readonly tokensJson: string;
39 readonly selectionJson: string;
40 readonly themeJson: string;
41 readonly placeholder: string;
42 readonly fontFamily: string;
43 readonly fontSize: number;
44 readonly lineHeight: number;
45 readonly contentInsetVertical: number;
46 readonly editable: boolean;
47 readonly scrollEnabled: boolean;
48 readonly autoFocus: boolean;
49 readonly autoCorrect: boolean;
50 readonly spellCheck: boolean;
51 readonly onComposerChange: (event: NativeEditorEvent) => void;
52 readonly onComposerSelectionChange?: (event: NativeSelectionEvent) => void;
53 readonly onComposerPasteImages?: (event: NativePasteImagesEvent) => void;
54 readonly onComposerFocus?: () => void;
55 readonly onComposerBlur?: () => void;
57​
58const NativeView = requireNativeView<NativeComposerEditorProps>(NATIVE_MODULE_NAME);
59​
60function basename(path: string): string {
61 const separator = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
62 return separator >= 0 ? path.slice(separator + 1) : path;
64​
65function fileIconUri(path: string): string {
66 return Image.resolveAssetSource(markdownFileIconSource(resolveMarkdownFileIcon(path))).uri;
68​
69export function ComposerEditor({
70 ref,
71 skills = EMPTY_SKILLS,
72 selection,
73 style,
74 textStyle,
75 onChangeText,
76 onSelectionChange,
77 onPasteImages,
78 onFocus,
79 onBlur,
80 contentInsetVertical = 0,
81 ...props
82}: ComposerEditorProps) {
83 const nativeRef = useRef<NativeComposerEditorRef>(null);
84 const confirmedTokensRef = useRef(collectComposerInlineTokens(props.value));
85 const textColor = useThemeColor("--color-foreground");
86 const placeholderColor = useThemeColor("--color-placeholder");
87 const chipBackground = useThemeColor("--color-subtle");
88 const chipBorder = useThemeColor("--color-border");
89 const chipText = useThemeColor("--color-foreground");
90 const skillBackground = useThemeColor("--color-inline-skill-background");
91 const skillBorder = useThemeColor("--color-inline-skill-border");
92 const skillText = useThemeColor("--color-inline-skill-foreground");
93 const fileTint = useThemeColor("--color-icon-muted");
94​
95 useImperativeHandle(
96 ref,
97 () => ({
98 focus: () => void nativeRef.current?.focus(),
99 blur: () => void nativeRef.current?.blur(),
100 setSelection: (nextSelection) =>
101 void nativeRef.current?.setSelection(nextSelection.start, nextSelection.end),
102 }),
103 [],
104 );
105​
106 const skillLabels = useMemo(
107 () => new Map(skills.map((skill) => [skill.name, skill.displayName?.trim() || skill.name])),
108 [skills],
109 );
110 const tokensJson = useMemo(() => {
111 const tokens = collectComposerInlineTokens(props.value, {
112 preserveTrailingFrom: confirmedTokensRef.current,
113 });
114 confirmedTokensRef.current = tokens;
115 return JSON.stringify(
116 tokens.map((token) => ({
117 type: token.type,
118 source: token.source,
119 start: token.start,
120 end: token.end,
121 label:
122 token.type === "skill"
123 ? (skillLabels.get(token.value) ?? token.value)
124 : basename(token.value),
125 iconUri: token.type === "mention" ? fileIconUri(token.value) : null,
126 })),
127 );
128 }, [props.value, skillLabels]);
129 const themeJson = JSON.stringify({
130 text: String(textColor),
131 placeholder: String(placeholderColor),
132 chipBackground: String(chipBackground),
133 chipBorder: String(chipBorder),
134 chipText: String(chipText),
135 skillBackground: String(skillBackground),
136 skillBorder: String(skillBorder),
137 skillText: String(skillText),
138 fileTint: String(fileTint),
139 });
140 const resolvedTextStyle = StyleSheet.flatten(textStyle) ?? {};
141 return (
142 <NativeView
143 ref={nativeRef}
144 value={props.value}
145 tokensJson={tokensJson}
146 selectionJson={selection ? JSON.stringify(selection) : ""}
147 themeJson={themeJson}
148 placeholder={props.placeholder ?? ""}
149 fontFamily={
150 typeof resolvedTextStyle.fontFamily === "string"
151 ? resolvedTextStyle.fontFamily
152 : "DMSans_400Regular"
153 }
154 fontSize={
155 typeof resolvedTextStyle.fontSize === "number"
156 ? resolvedTextStyle.fontSize
157 : MOBILE_TYPOGRAPHY.composer.fontSize
158 }
159 lineHeight={
160 typeof resolvedTextStyle.lineHeight === "number"
161 ? resolvedTextStyle.lineHeight
162 : MOBILE_TYPOGRAPHY.composer.lineHeight
163 }
164 contentInsetVertical={contentInsetVertical}
165 editable={props.editable ?? true}
166 scrollEnabled={props.scrollEnabled ?? true}
167 autoFocus={props.autoFocus ?? false}
168 autoCorrect={props.autoCorrect ?? true}
169 spellCheck={props.spellCheck ?? true}
170 style={style as StyleProp<ViewStyle>}
171 onComposerChange={(event) => {
172 onChangeText(event.nativeEvent.value);
173 onSelectionChange?.(event.nativeEvent.selection);
174 }}
175 onComposerSelectionChange={(event) => onSelectionChange?.(event.nativeEvent.selection)}
176 onComposerPasteImages={(event) => onPasteImages?.(event.nativeEvent.uris)}
177 onComposerFocus={onFocus}
178 onComposerBlur={onBlur}
179 />
180 );
182​
183export type {
184 ComposerEditorHandle,
185 ComposerEditorProps,
186 ComposerEditorSelection,
187} from "./T3ComposerEditor.types";