-
Notifications
You must be signed in to change notification settings - Fork 940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aspects Page: Expand/Collapse nested objects + Copy JSON #6563
Changes from all commits
98c2a80
a1f4cf1
36834c2
c280795
77dcde9
10542c0
f9e10c3
50909c6
675efb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,49 @@ | |
cursor: pointer; | ||
color: var(--bit-text-color-light, #6c707c); | ||
} | ||
.sectionTitleContainer { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
} | ||
.expandCollapse { | ||
display: flex; | ||
align-items: flex-start; | ||
cursor: pointer; | ||
|
||
img { | ||
border-radius: 8px; | ||
width: 16px; | ||
&:hover { | ||
background-color: var(--bit-border-color-lightest, #ededed); | ||
} | ||
} | ||
} | ||
.copyMessage { | ||
position: unset; | ||
transform: unset; | ||
display: flex; | ||
} | ||
|
||
.toolbar { | ||
flex-direction: row; | ||
display: flex; | ||
align-items: flex-start; | ||
} | ||
.copyIcon { | ||
display: flex; | ||
font-size: var(--bit-p-xs); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add default size |
||
} | ||
.copyButton { | ||
cursor: pointer; | ||
box-sizing: border-box; | ||
border: none; | ||
background-color: var(--bit-bg-color-highlight); | ||
color: var(--bit-accent-color); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also default colors |
||
outline: none; | ||
} | ||
.sectionTitle { | ||
display: flex; | ||
margin-bottom: 16px; | ||
font-weight: bold; | ||
font-size: var(--bit-p-xs); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import classNames from 'classnames'; | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
import JSONFormatter from 'json-formatter-js'; | ||
import { isObject } from 'lodash'; | ||
import { CopiedMessage } from '@teambit/documenter.ui.copied-message'; | ||
import { Icon } from '@teambit/evangelist.elements.icon'; | ||
import copy from 'copy-to-clipboard'; | ||
|
||
import styles from './aspect-box.module.scss'; | ||
|
||
export type AspectBoxProps = { | ||
|
@@ -11,12 +16,10 @@ export type AspectBoxProps = { | |
data: any; | ||
} & React.HTMLAttributes<HTMLDivElement>; | ||
|
||
const collapsedIcon = 'https://static.bit.dev/bit-icons/collapse.svg'; | ||
const expandIcon = 'https://static.bit.dev/bit-icons/expand.svg'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we're doing the same in component drawer so maybe we can make a component and use in both? |
||
|
||
export function AspectBox({ icon, name, config, data, className, ...rest }: AspectBoxProps) { | ||
const configContent = new JSONFormatter(config, 1, { | ||
theme: 'dark', | ||
hoverPreviewEnabled: true, | ||
}); | ||
const dataContent = new JSONFormatter(data, 1, { theme: 'dark' }); | ||
return ( | ||
<div {...rest} className={classNames(styles.aspectBox, className)}> | ||
<div className={styles.titleLine}> | ||
|
@@ -30,22 +33,68 @@ export function AspectBox({ icon, name, config, data, className, ...rest }: Aspe | |
<Icon of="open-tab" /> | ||
</a> */} | ||
</div> | ||
<div className={styles.sectionTitle}>Configuration</div> | ||
<div className={classNames(styles.log, styles.config)}> | ||
<div | ||
ref={(nodeElement) => { | ||
nodeElement && nodeElement.appendChild(configContent.render()); | ||
}} | ||
/> | ||
<DisplayJsonTree title={'Configuration'} object={config} /> | ||
<DisplayJsonTree title={'Data'} object={data} /> | ||
</div> | ||
); | ||
} | ||
|
||
/** | ||
* @todo extract to a separate component | ||
*/ | ||
|
||
function DisplayJsonTree({ object, title }: { object: any; title: string }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any? |
||
const [expandedDepth, setExpandedDepth] = useState<number>(1); | ||
const [isCopied, setIsCopied] = useState(false); | ||
const isDepthGreaterThanOne = Object.keys(object).some((key) => isObject(object[key])); | ||
const jsonContent = new JSONFormatter(object, expandedDepth, { | ||
theme: 'dark', | ||
hoverPreviewEnabled: true, | ||
}); | ||
const collapsedExpandedIcon = expandedDepth === 1 ? expandIcon : collapsedIcon; | ||
|
||
const handleClick = (dataToCopy) => () => { | ||
setIsCopied(true); | ||
setTimeout(() => { | ||
setIsCopied(false); | ||
}, 2000); | ||
copy(JSON.stringify(dataToCopy, null, 2)); | ||
}; | ||
|
||
const toggleExpandedDepth = (state: number) => { | ||
if (state === 1) return Infinity; | ||
return 1; | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className={styles.sectionTitleContainer}> | ||
<div className={styles.sectionTitle}>{title}</div> | ||
<div className={styles.toolbar}> | ||
<CopiedMessage className={styles.copyMessage} show={isCopied} /> | ||
<div className={styles.copy}> | ||
<button className={styles.copyButton} onClick={handleClick(jsonContent.json)}> | ||
<Icon className={styles.copyIcon} of="copy-cmp" /> | ||
</button> | ||
</div> | ||
|
||
{isDepthGreaterThanOne && ( | ||
<div className={styles.expandCollapse}> | ||
<img | ||
src={collapsedExpandedIcon} | ||
onClick={() => setExpandedDepth((value) => toggleExpandedDepth(value))} | ||
/> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
<div className={styles.sectionTitle}>Calculated data</div> | ||
<div className={styles.log}> | ||
<div className={classNames(styles.log, styles.config)}> | ||
<div | ||
ref={(nodeElement) => { | ||
nodeElement && nodeElement.appendChild(dataContent.render()); | ||
nodeElement && nodeElement.replaceChildren(jsonContent.render()); | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
row is by default. I think you can remove this line