-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
groupArraysAfterLength
(#21)
- Loading branch information
Showing
9 changed files
with
205 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { Box } from '@mui/material' | ||
import React, { useMemo } from 'react' | ||
|
||
import { useTextColor } from '../../hooks/useColor' | ||
import { useJsonViewerStore } from '../../stores/JsonViewerStore' | ||
import type { DataItemProps } from '../../type' | ||
import { DataKeyPair } from '../DataKeyPair' | ||
|
||
const arrayLb = '[' | ||
const arrayRb = ']' | ||
|
||
export const PreArrayType: React.FC<DataItemProps<unknown[]>> = (props) => { | ||
const metadataColor = useJsonViewerStore(store => store.colorNamespace.base04) | ||
const sizeOfValue = useMemo( | ||
() => props.inspect ? `${Object.keys(props.value).length} Items` : '', | ||
[props.inspect, props.value] | ||
) | ||
return ( | ||
<Box | ||
component='span' className='data-array-start' | ||
sx={{ | ||
letterSpacing: 0.5 | ||
}} | ||
> | ||
{arrayLb} | ||
<Box | ||
component='span' | ||
sx={{ | ||
pl: 0.5, | ||
fontStyle: 'italic', | ||
color: metadataColor | ||
}} | ||
> | ||
{sizeOfValue} | ||
</Box> | ||
</Box> | ||
) | ||
} | ||
|
||
export const PostArrayType: React.FC<DataItemProps<unknown[]>> = (props) => { | ||
const metadataColor = useJsonViewerStore(store => store.colorNamespace.base04) | ||
const sizeOfValue = useMemo( | ||
() => !props.inspect ? `${Object.keys(props.value).length} Items` : '', | ||
[props.inspect, props.value] | ||
) | ||
return ( | ||
<Box component='span' className='data-array-end'> | ||
{arrayRb} | ||
<Box | ||
component='span' | ||
sx={{ | ||
pl: 0.5, | ||
fontStyle: 'italic', | ||
color: metadataColor | ||
}} | ||
> | ||
{sizeOfValue} | ||
</Box> | ||
</Box> | ||
) | ||
} | ||
|
||
export const ArrayType: React.FC<DataItemProps<unknown[]>> = (props) => { | ||
const keyColor = useTextColor() | ||
const groupArraysAfterLength = useJsonViewerStore(store => store.groupArraysAfterLength) | ||
const elements = useMemo(() => { | ||
if (props.value.length <= groupArraysAfterLength) { | ||
return props.value.map((value, index) => { | ||
const path = [...props.path, index] | ||
return ( | ||
<DataKeyPair key={index} path={path} value={value}/> | ||
) | ||
}) | ||
} | ||
const value = props.value.reduce<unknown[][]>((array, value, index) => { | ||
const target = Math.floor(index / groupArraysAfterLength) | ||
if (array[target]) { | ||
array[target].push(value) | ||
} else { | ||
array[target] = [value] | ||
} | ||
return array | ||
}, []) | ||
console.log('value', value) | ||
|
||
return value.map((list, index) => { | ||
const path = [...props.path] | ||
return ( | ||
<DataKeyPair key={index} path={path} value={list} nested/> | ||
) | ||
}) | ||
}, [props.path, props.value, groupArraysAfterLength]) | ||
return ( | ||
<Box | ||
className='data-array' | ||
sx={{ | ||
display: props.inspect ? 'block' : 'inline-block', | ||
pl: props.inspect ? 2 : 0, | ||
color: keyColor | ||
}} | ||
> | ||
{ | ||
props.inspect | ||
? elements | ||
: ( | ||
<Box component='span' className='data-array-body'> | ||
... | ||
</Box> | ||
) | ||
} | ||
</Box> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.