Skip to content

Commit 9e1929c

Browse files
committed
add Table cell align support; add index to LeafPayload;
1 parent b449be0 commit 9e1929c

File tree

4 files changed

+75
-47
lines changed

4 files changed

+75
-47
lines changed

apps/demo/src/pages/PageInput.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ Some note with some content.
4343
4444
With even more sentences, words and other things.
4545
46-
| Time | Name |
47-
| ---- | ---- |
48-
| 8am | Morning |
49-
| 12am | Noon |
50-
| 6pm | Evening |
51-
| 10pm | Night |
46+
| Time | Name | Score |
47+
| ---- | ---- | ----: |
48+
| 8am | Morning | 10 |
49+
| 12am | Noon | 40 |
50+
| 6pm | Evening | 20 |
51+
| 10pm | Night | 30 |
5252
5353
`
5454

packages/md-mui/LeafChildNodes.tsx

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import React from 'react'
1+
import { ReactNode } from 'react'
22
import type { Parents } from 'mdast'
3-
//import { useContentSelection } from '@content-ui/react/ContentSelectionContext'
43
import { ContentLeaf } from '@content-ui/react/ContentLeaf'
5-
//import { isLeafSelected } from '@content-ui/react/Utils/isLeafSelected'
64

75
export const LeafChildNodes = <P extends {} = {}, TChildNodes extends Parents['children'] = Parents['children']>(
86
props:
97
P &
108
{
9+
/**
10+
* A childs index is only passed down when `true`, for performance reasons.
11+
*/
12+
addIndex?: boolean
1113
childNodes: TChildNodes
1214
},
13-
): React.ReactNode => {
14-
// todo: move this hook into the decorators
15-
// const editorSelection = useContentSelection()
16-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
17-
const {childNodes, ...p} = props
15+
): ReactNode => {
16+
const {childNodes, addIndex, ...p} = props
1817
const length = childNodes.length
1918
return childNodes
2019
.map((childNext, i) =>
@@ -23,8 +22,7 @@ export const LeafChildNodes = <P extends {} = {}, TChildNodes extends Parents['c
2322
{...p}
2423
elem={childNext.type}
2524
child={childNext}
26-
// todo: add support for multiple selections, e.g. multiple lines with unselected lines in between
27-
// selected={editorSelection ? isLeafSelected(childNext.position, editorSelection.startLine, editorSelection.endLine) : false}
25+
index={addIndex ? i : undefined}
2826
isFirst={i === 0}
2927
isLast={i === length - 1}
3028
/>,

packages/md-mui/Leafs/LeafTable.tsx

+57-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import React from 'react'
21
import { LeafChildNodes } from '@content-ui/md-mui/LeafChildNodes'
32
import type { Table as MdTable/* TableRow as MdTableRow, TableCell as MdTableCell*/ } from 'mdast'
43
import Table from '@mui/material/Table'
@@ -9,57 +8,84 @@ import TableBody from '@mui/material/TableBody'
98
import { useTheme } from '@mui/material/styles'
109
import { ContentLeafProps } from '@content-ui/react/ContentLeafsContext'
1110
import { useLeafFollower } from '@content-ui/react/useLeafFollower'
11+
import { FC, createContext, PropsWithChildren, useContext, useMemo } from 'react'
1212

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)
1543
// todo: align exists on table level, not on cell level
1644
//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)
1947
return <Table
2048
size={'small'}
2149
sx={{
2250
mt: 1,
2351
mb: 3,
2452
}}
2553
>
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>
4568
</Table>
4669
}
4770

48-
export const LeafTableRow: React.FC<ContentLeafProps<'tableRow'>> = ({child, selected, ...props}) => {
71+
export const LeafTableRow: FC<ContentLeafProps<'tableRow'>> = ({child, selected, ...props}) => {
4972
const rRef = useLeafFollower<HTMLTableRowElement>(selected)
5073
return <TableRow ref={rRef}>
51-
<LeafChildNodes childNodes={child.children} {...props}/>
74+
<LeafChildNodes
75+
childNodes={child.children}
76+
{...props}
77+
addIndex
78+
/>
5279
</TableRow>
5380
}
5481

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}) => {
5683
const {palette} = useTheme()
57-
// const c = child as MdTableCell
84+
const {align} = useTableSettings()
85+
const cellAlign = typeof index === 'number' && align ? align[index] : undefined
5886
// todo: add gfm-advanced support
5987
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}
6389
style={{
6490
backgroundColor: selected ? palette.mode === 'dark' ? 'rgba(5, 115, 115, 0.11)' : 'rgba(206, 230, 228, 0.31)' : undefined,
6591
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,

packages/react/ContentLeafsContext.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ export interface LeafsEngine<TDeco extends ReactDeco<{}, {}, {}>, TRender extend
6969
export interface ContentLeafPayload<TChild extends { type: string } = { type: string }> {
7070
elem: TChild['type']
7171
child: TChild
72+
/**
73+
* A childs index, only passed down if specified by parent, for performance reasons.
74+
*/
75+
index?: number
7276
selected?: boolean
7377
// `true` when first Leaf inside the parent level
7478
isFirst?: boolean

0 commit comments

Comments
 (0)