Skip to content

Commit 9ebc5f4

Browse files
committed
feat: table initial reimpl
1 parent a71c940 commit 9ebc5f4

File tree

1 file changed

+108
-15
lines changed

1 file changed

+108
-15
lines changed

package/src/components/Message/MessageSimple/utils/renderText.tsx

Lines changed: 108 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,24 @@ const ReactiveScrollView = ({ children }: { children: ReactNode }) => {
4141
const [scrollViewXOffset, setScrollViewXOffset] = useState(0);
4242
const scrollViewRef = useRef<ScrollView>(null);
4343

44-
const scrollTo = useCallback((translation: number) => {
45-
if (scrollViewRef.current) {
46-
scrollViewRef.current.scrollTo({
47-
animated: false,
48-
x: translation,
49-
});
50-
}
51-
}, []);
44+
const scrollTo = useCallback(
45+
(translation: number) => {
46+
if (scrollViewRef.current) {
47+
scrollViewRef.current.scrollTo({
48+
animated: false,
49+
x: translation,
50+
});
51+
}
52+
},
53+
[scrollViewRef],
54+
);
5255

5356
const panGesture = Gesture.Pan()
5457
.activeOffsetX([-10, 10])
5558
.onUpdate((event) => {
5659
const { translationX } = event;
5760

58-
if (scrollViewRef.current) {
59-
runOnJS(scrollTo)(scrollViewXOffset - translationX);
60-
}
61+
runOnJS(scrollTo)(scrollViewXOffset - translationX);
6162
})
6263
.onEnd((event) => {
6364
const { translationX } = event;
@@ -118,6 +119,33 @@ const defaultMarkdownStyles: MarkdownStyle = {
118119
marginBottom: 8,
119120
marginTop: 8,
120121
},
122+
table: {
123+
borderColor: '#222222',
124+
borderRadius: 3,
125+
borderWidth: 1,
126+
},
127+
tableHeader: {
128+
backgroundColor: '#222222',
129+
flexDirection: 'row',
130+
justifyContent: 'space-around',
131+
},
132+
tableHeaderCell: {
133+
color: '#ffffff',
134+
fontWeight: 'bold',
135+
padding: 5,
136+
},
137+
tableRow: {
138+
//borderBottomWidth: 1,
139+
borderColor: '#222222',
140+
flexDirection: 'row',
141+
justifyContent: 'space-around',
142+
},
143+
tableRowCell: {
144+
padding: 5,
145+
},
146+
tableRowLast: {
147+
borderColor: 'transparent',
148+
},
121149
};
122150

123151
const mentionsParseFunction: ParseFunction = (capture, parse, state) => ({
@@ -188,6 +216,34 @@ export const renderText = <
188216
color: colors.accent_blue,
189217
...markdownStyles?.mentions,
190218
},
219+
table: {
220+
borderColor: '#222222',
221+
borderRadius: 3,
222+
borderWidth: 1,
223+
flex: 1,
224+
},
225+
tableHeader: {
226+
backgroundColor: '#222222',
227+
flexDirection: 'row',
228+
justifyContent: 'space-around',
229+
},
230+
tableHeaderCell: {
231+
color: '#ffffff',
232+
fontWeight: 'bold',
233+
padding: 5,
234+
},
235+
tableRow: {
236+
borderColor: '#222222',
237+
flexDirection: 'row',
238+
justifyContent: 'space-around',
239+
},
240+
tableRowCell: {
241+
flex: 1,
242+
padding: 5,
243+
},
244+
tableRowLast: {
245+
borderColor: 'transparent',
246+
},
191247
text: {
192248
...defaultMarkdownStyles.text,
193249
color: colors.black,
@@ -327,13 +383,49 @@ export const renderText = <
327383
);
328384

329385
const CodeBlockReact: ReactNodeOutput = (node, _, state) => (
330-
<ReactiveScrollView>
331-
<Text key={state.key} style={styles.codeBlock}>
332-
{node.content}
333-
</Text>
386+
<ReactiveScrollView key={state.key}>
387+
<Text style={styles.codeBlock}>{node.content}</Text>
334388
</ReactiveScrollView>
335389
);
336390

391+
const tableReact: ReactNodeOutput = (node, output, state) => {
392+
const headers = node?.header?.map((content: SingleASTNode, idx: number) => (
393+
<Text key={idx} style={styles.tableHeaderCell}>
394+
{output(content, state)}
395+
</Text>
396+
));
397+
398+
const header = (
399+
<View key={-1} style={styles.tableHeader}>
400+
{headers}
401+
</View>
402+
);
403+
404+
const rows = node?.cells?.map((row: SingleASTNode, r: number) => {
405+
const cells = row.map((content: SingleASTNode, c: number) => (
406+
<View key={c} style={styles.tableRowCell}>
407+
{output(content, state)}
408+
</View>
409+
));
410+
const rowStyles = [styles.tableRow];
411+
if (node.cells.length - 1 === r) {
412+
rowStyles.push(styles.tableRowLast);
413+
}
414+
415+
return (
416+
<View key={r} style={rowStyles}>
417+
{cells}
418+
</View>
419+
);
420+
});
421+
422+
return (
423+
<ReactiveScrollView key={state.key}>
424+
<View style={styles.table}>{[header, rows]}</View>
425+
</ReactiveScrollView>
426+
);
427+
};
428+
337429
const customRules = {
338430
// do not render images, we will scrape them out of the message and show on attachment card component
339431
image: { match: () => null },
@@ -355,6 +447,7 @@ export const renderText = <
355447
}
356448
: {}),
357449
codeBlock: { react: CodeBlockReact },
450+
table: { react: tableReact },
358451
};
359452

360453
return (

0 commit comments

Comments
 (0)