Skip to content
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

Add flags to turn off some features #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions src/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import ModalPrimitive from "./ModalPrimitive";
* @returns {JSX.Element}
*
*/
export default function Editor({input, jsonBoxRef, onChange}) {
export default function Editor({input, jsonBoxRef, onChange, hideInsertObjectButton, expandToGeneration, isReadOnly}) {

const emptyValues = {
path : undefined,
Expand Down Expand Up @@ -236,12 +236,13 @@ export default function Editor({input, jsonBoxRef, onChange}) {
}

<div key={"jsonBody"} className={styles.JsonViewOutput}>

<div className={styles.insertBanner} style={{backgroundColor: focusOnBanner ? userStyle.banner.hoverColor : userStyle.themes.color}}
onMouseOver={()=>{setFocusOnBanner(true)}} onMouseLeave={()=>{setFocusOnBanner(false)}} onClick={()=>{
jsonData !== undefined ? createModal("") : setSelectType(true)}}>
<span className={styles.bannerSpan} style={{color: userStyle.banner.fontColor, font: userStyle.banner.font}}> + Insert Object</span>
</div>
{!hideInsertObjectButton && !isReadOnly &&
<div className={styles.insertBanner} style={{backgroundColor: focusOnBanner ? userStyle.banner.hoverColor : userStyle.themes.color}}
onMouseOver={()=>{setFocusOnBanner(true)}} onMouseLeave={()=>{setFocusOnBanner(false)}} onClick={()=>{
jsonData !== undefined ? createModal("") : setSelectType(true)}}>
<span className={styles.bannerSpan} style={{color: userStyle.banner.fontColor, font: userStyle.banner.font}}> + Insert Object</span>
</div>
}
<div className={styles.jsonListOutput} ref={jsonListOutput}>
<JsonView key={"DisplayJson"}
input={jsonData}
Expand All @@ -250,6 +251,8 @@ export default function Editor({input, jsonBoxRef, onChange}) {
deleteNode={deleteNode}
setPrimitive={setPrimitive}
createModal={createModal}
expandToGeneration={expandToGeneration}
isReadOnly={isReadOnly}
/>
</div>
</div>
Expand Down
30 changes: 20 additions & 10 deletions src/JsonView.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import UserContext from "./UserContext";
* @param createModal creates edit modal component
* @param indent indentation
* @param needLeaf indicates "", [] or {}
* @param expandToGeneration Show content if the child's generation index is > expandToGeneration
* @param isReadOnly true: do not show overlay to edit and delete

* @returns {JSX.Element|[]}
*/
Expand All @@ -31,6 +33,8 @@ export default function JsonView(
createModal,
indent = 1,
needLeaf = true,
expandToGeneration = undefined,
isReadOnly = false
}) {

const typeOfInput = TypeOfValue(input)
Expand Down Expand Up @@ -59,9 +63,9 @@ export default function JsonView(
<div className={styles.dataNode} onClick={()=>{ changePrimitive(input)}}>
<div className={styles.needLeaf} style={{backgroundColor: focusOnLine ? userStyle.themes.hoverColor : ''}} onMouseOver={()=>{setFocusOnLine(true)}} onMouseLeave={()=>{setFocusOnLine(false)}}>
<div>
<JsonView jsonPath={jsonPath} input={input} indent={indent} needLeaf={false}/>
<JsonView jsonPath={jsonPath} input={input} indent={indent} needLeaf={false} expandToGeneration={expandToGeneration} isReadOnly={isReadOnly} />
</div>
{focusOnLine &&
{!isReadOnly && focusOnLine &&
<div className={styles.rightContainer}
style={{backgroundImage: 'linear-gradient(to right, transparent 0, ' + userStyle.themes.hoverColor + ' 0.5em)'}}>
<div style={{
Expand Down Expand Up @@ -123,7 +127,7 @@ export default function JsonView(
<span>{JSON.stringify(input)}</span>
</div>
</div>
{ focusOnLine &&
{!isReadOnly && focusOnLine &&
<div className={styles.rightContainer}
style={{ backgroundImage: 'linear-gradient(to right, transparent 0, ' + userStyle.themes.hoverColor + ' 0.5em)'}}>
<div style={{
Expand Down Expand Up @@ -158,7 +162,7 @@ export default function JsonView(
<ViewNode key={jsonPath + "/" +key} jsonPath={jsonPath} field={key} value={value}
indent={indent} isInArray={input instanceof Array}
deleteNode={deleteNode} setPrimitive={setPrimitive} createModal={createModal} createEditModal={createEditModal}
jsonListOutput={jsonListOutput}/>
jsonListOutput={jsonListOutput} expandToGeneration={expandToGeneration} isReadOnly={isReadOnly}/>
)
})
)
Expand All @@ -178,12 +182,14 @@ export default function JsonView(
* @param setPrimitive changes a primitive value
* @param createModal creates edit modal component
* @param createEditModal saves information with current position node of for modal editor when a user clicks add or edit button
* @param expandToGeneration Show content if the child's generation index is > expandToGeneration
* @param isReadOnly true: do not show edit && delete buttons
* @returns {JSX.Element}
*
*/
function ViewNode({ jsonPath, field, value, jsonListOutput, indent, isInArray, deleteNode, setPrimitive, createModal, createEditModal}) {
function ViewNode({ jsonPath, field, value, jsonListOutput, indent, isInArray, deleteNode, setPrimitive, createModal, createEditModal, expandToGeneration, isReadOnly}) {

const [showContent, setShowContent] = useState(true)
const [showContent, setShowContent] = useState(expandToGeneration === undefined ? true : (jsonPath.match(/\//g) || []).length <= expandToGeneration)
const [focusOnLine, setFocusOnLine] = useState(false)
const isList = value instanceof Object
// current clickNode position
Expand All @@ -194,11 +200,15 @@ function ViewNode({ jsonPath, field, value, jsonListOutput, indent, isInArray, d
<div className={styles.dataContainer}>
<div className={styles.dataNode} ref={inputRef}>
<div className={styles.clickNode}
style={{backgroundColor: focusOnLine ? userStyle.themes.hoverColor : ''}} onMouseOver={()=>{setFocusOnLine(true)}} onMouseLeave={()=>{setFocusOnLine(false)}}
style={{backgroundColor: focusOnLine ? userStyle.themes.hoverColor : '', cursor: isReadOnly ? 'default': 'pointer'}} onMouseOver={()=>{setFocusOnLine(!isReadOnly && true)}} onMouseLeave={()=>{setFocusOnLine(false)}}
onClick={(e)=>{
e.stopPropagation();
e.preventDefault();

if (isReadOnly) {
return;
}

if(isList) {
setShowContent(!showContent)
}else{
Expand All @@ -224,7 +234,7 @@ function ViewNode({ jsonPath, field, value, jsonListOutput, indent, isInArray, d
</div>
</div>

{ focusOnLine &&
{ !isReadOnly && focusOnLine &&
<div className={styles.rightContainer} style={{ backgroundImage: 'linear-gradient(to right, transparent 0, ' + userStyle.themes.hoverColor + ' 0.5em)'}}>
<div style={{font: userStyle.values.font, fontStyle:"italic", color:userStyle.themes.color}}><TypeToString input={value}/></div>
<div className={styles.rightButton}>
Expand All @@ -248,7 +258,7 @@ function ViewNode({ jsonPath, field, value, jsonListOutput, indent, isInArray, d
}

{
!isList && <JsonView input={value} needLeaf={false}/>
!isList && <JsonView input={value} needLeaf={false} expandToGeneration={expandToGeneration} isReadOnly={isReadOnly} />
}
</div>

Expand All @@ -258,7 +268,7 @@ function ViewNode({ jsonPath, field, value, jsonListOutput, indent, isInArray, d
isList && showContent &&
<JsonView jsonPath={jsonPath + '/' + field} input={value} indent={indent + 1}
deleteNode={deleteNode} setPrimitive={setPrimitive} needLeaf={false}
jsonListOutput={jsonListOutput} createModal={createModal}/>
jsonListOutput={jsonListOutput} createModal={createModal} expandToGeneration={expandToGeneration} isReadOnly={isReadOnly} />
}

</div>
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import UserContext from "./UserContext"
* @returns {JSX.Element}
*
*/
function JsonEditor({jsonObject, onChange, theme, bannerStyle, keyStyle, valueStyle, buttonStyle}) {
function JsonEditor({jsonObject, onChange, theme, hideInsertObjectButton, expandToGeneration, isReadOnly, bannerStyle, keyStyle, valueStyle, buttonStyle}) {

const jsonBoxRef = useRef()
const defaultStyle = useContext(UserContext)
Expand All @@ -27,7 +27,7 @@ function JsonEditor({jsonObject, onChange, theme, bannerStyle, keyStyle, valueSt
buttons: buttonStyle === undefined ? defaultStyle.buttons : buttonStyle
}}>
<div className={styles.container} ref={jsonBoxRef}>
<Editor input={jsonObject} jsonBoxRef={jsonBoxRef} onChange={onChange}/>
<Editor input={jsonObject} jsonBoxRef={jsonBoxRef} onChange={onChange} hideInsertObjectButton={hideInsertObjectButton} expandToGeneration={expandToGeneration} isReadOnly={isReadOnly} />
</div>
</UserContext.Provider>
)
Expand Down