1
- import React from 'react'
2
1
import { LeafChildNodes } from '@content-ui/md-mui/LeafChildNodes'
3
2
import type { Table as MdTable /* TableRow as MdTableRow, TableCell as MdTableCell*/ } from 'mdast'
4
3
import Table from '@mui/material/Table'
@@ -9,57 +8,84 @@ import TableBody from '@mui/material/TableBody'
9
8
import { useTheme } from '@mui/material/styles'
10
9
import { ContentLeafProps } from '@content-ui/react/ContentLeafsContext'
11
10
import { useLeafFollower } from '@content-ui/react/useLeafFollower'
11
+ import { FC , createContext , PropsWithChildren , useContext , useMemo } from 'react'
12
12
13
- export const LeafTable : React . FC < ContentLeafProps < 'table' > > = ( { child} ) => {
14
- const c = child as MdTable
13
+ export type TableSettings = {
14
+ align : MdTable [ 'align' ]
15
+ }
16
+
17
+ const TableSettingsContext = createContext < TableSettings > ( {
18
+ align : undefined ,
19
+ } )
20
+
21
+ export const useTableSettings = ( ) : TableSettings => useContext ( TableSettingsContext )
22
+
23
+ export const TableSettingsProvider = (
24
+ {
25
+ children,
26
+ align,
27
+ } : PropsWithChildren < TableSettings > ,
28
+ ) => {
29
+ const ctx = useMemo ( ( ) => {
30
+ return {
31
+ align,
32
+ }
33
+ } , [
34
+ align ,
35
+ ] )
36
+
37
+ return < TableSettingsContext . Provider value = { ctx } > { children } </ TableSettingsContext . Provider >
38
+ }
39
+
40
+
41
+ export const LeafTable : FC < ContentLeafProps < 'table' > > = ( { child} ) => {
42
+ console . log ( 'child-t' , child )
15
43
// todo: align exists on table level, not on cell level
16
44
//const align = c.align
17
- const headerRow = c . children . slice ( 0 , 1 )
18
- const contentRows = c . children . slice ( 1 )
45
+ const headerRow = child . children . slice ( 0 , 1 )
46
+ const contentRows = child . children . slice ( 1 )
19
47
return < Table
20
48
size = { 'small' }
21
49
sx = { {
22
50
mt : 1 ,
23
51
mb : 3 ,
24
52
} }
25
53
>
26
- < TableHead >
27
- < LeafChildNodes childNodes = { headerRow } />
28
- </ TableHead >
29
- < TableBody >
30
- < LeafChildNodes < { tableSettings ?: any } >
31
- childNodes = { contentRows }
32
- //tableSettings={{align}}
33
- />
34
- { /*contentRows.map((childNext, i) =>
35
- <ContentLeaf
36
- key={i}
37
- elem={childNext.type}
38
- child={childNext}
39
- selected={isLeafSelected(childNext.position, editorSelection?.startLine, editorSelection?.endLine)}
40
- isFirst={i === 0}
41
- isLast={i === length - 1}
42
- //{...p as unknown as P}
43
- />)*/ }
44
- </ TableBody >
54
+ < TableSettingsProvider
55
+ align = { child . align }
56
+ >
57
+ < TableHead >
58
+ < LeafChildNodes
59
+ childNodes = { headerRow }
60
+ />
61
+ </ TableHead >
62
+ < TableBody >
63
+ < LeafChildNodes
64
+ childNodes = { contentRows }
65
+ />
66
+ </ TableBody >
67
+ </ TableSettingsProvider >
45
68
</ Table >
46
69
}
47
70
48
- export const LeafTableRow : React . FC < ContentLeafProps < 'tableRow' > > = ( { child, selected, ...props } ) => {
71
+ export const LeafTableRow : FC < ContentLeafProps < 'tableRow' > > = ( { child, selected, ...props } ) => {
49
72
const rRef = useLeafFollower < HTMLTableRowElement > ( selected )
50
73
return < TableRow ref = { rRef } >
51
- < LeafChildNodes childNodes = { child . children } { ...props } />
74
+ < LeafChildNodes
75
+ childNodes = { child . children }
76
+ { ...props }
77
+ addIndex
78
+ />
52
79
</ TableRow >
53
80
}
54
81
55
- export const LeafTableCell : React . FC < ContentLeafProps < 'tableCell' > & { tableSettings ?: any } > = ( { child, selected, ...props } ) => {
82
+ export const LeafTableCell : FC < ContentLeafProps < 'tableCell' > & { tableSettings ?: any } > = ( { child, selected, index , ...props } ) => {
56
83
const { palette} = useTheme ( )
57
- // const c = child as MdTableCell
84
+ const { align} = useTableSettings ( )
85
+ const cellAlign = typeof index === 'number' && align ? align [ index ] : undefined
58
86
// todo: add gfm-advanced support
59
87
return < TableCell
60
- // todo: this needs to know the cell index
61
- // align={tableSettings?.align[cellNo] || c.data?.align as TableCellProps['align']}
62
- // align={c.data?.align as TableCellProps['align']}
88
+ align = { cellAlign || undefined }
63
89
style = { {
64
90
backgroundColor : selected ? palette . mode === 'dark' ? 'rgba(5, 115, 115, 0.11)' : 'rgba(206, 230, 228, 0.31)' : undefined ,
65
91
boxShadow : selected ? palette . mode === 'dark' ? '-8px 0px 0px 0px rgba(5, 115, 115, 0.11)' : '-8px 0px 0px 0px rgba(206, 230, 228, 0.31)' : undefined ,
0 commit comments