Skip to content

Commit 189295b

Browse files
authored
#72 JSON5 option (#78)
1 parent 12baa20 commit 189295b

File tree

7 files changed

+70
-43
lines changed

7 files changed

+70
-43
lines changed

README.md

Lines changed: 39 additions & 37 deletions
Large diffs are not rendered by default.

demo/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ function App() {
309309
customNodeDefinitions={demoData[selectedData]?.customNodeDefinitions}
310310
customText={demoData[selectedData]?.customTextDefinitions}
311311
onChange={demoData[selectedData]?.onChange ?? undefined}
312+
useJSON5Editor
312313
/>
313314
</Box>
314315
<VStack w="100%" align="flex-end" gap={4}>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-edit-react",
3-
"version": "1.12.0-rc1",
3+
"version": "1.12.0-rc2",
44
"description": "React component for editing or viewing JSON/object data",
55
"main": "build/index.cjs.js",
66
"module": "build/index.esm.js",

rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default [
2929
bundleSize(),
3030
sizes(),
3131
],
32-
external: [],
32+
external: ['json5', 'just-clone', 'object-property-assigner', 'object-property-extractor'],
3333
},
3434
{
3535
input: './build/dts/index.d.ts',

src/CollectionNode.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
type ErrorString,
99
type NodeData,
1010
type JerError,
11+
type CollectionData,
1112
ERROR_DISPLAY_TIME,
12-
CollectionData,
1313
} from './types'
1414
import { Icon } from './Icons'
1515
import { filterNode, isCollection } from './filterHelpers'
@@ -55,8 +55,17 @@ export const CollectionNode: React.FC<CollectionNodeProps> = ({
5555
defaultValue,
5656
translate,
5757
customNodeDefinitions,
58+
useJSON5Editor,
5859
} = props
59-
const [stringifiedValue, setStringifiedValue] = useState(JSON.stringify(data, null, 2))
60+
const stringifyJson = useMemo(() => {
61+
if (!useJSON5Editor) return (data: object) => JSON.stringify(data, null, 2)
62+
if (useJSON5Editor instanceof Object) {
63+
return (data: object) => JSON5.stringify(data, useJSON5Editor)
64+
}
65+
return (data: object) => JSON5.stringify(data, { space: 2 })
66+
}, [useJSON5Editor])
67+
68+
const [stringifiedValue, setStringifiedValue] = useState(stringifyJson(data))
6069
const [error, setError] = useState<string | null>(null)
6170

6271
const startCollapsed = collapseFilter(incomingNodeData)
@@ -77,7 +86,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = ({
7786
const [isAnimating, setIsAnimating] = useState(false)
7887

7988
useEffect(() => {
80-
setStringifiedValue(JSON.stringify(data, null, 2))
89+
setStringifiedValue(stringifyJson(data))
8190
}, [data])
8291

8392
useEffect(() => {
@@ -249,7 +258,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = ({
249258
const handleCancel = () => {
250259
setCurrentlyEditingElement(null)
251260
setError(null)
252-
setStringifiedValue(JSON.stringify(data, null, 2))
261+
setStringifiedValue(stringifyJson(data))
253262
}
254263

255264
// DERIVED VALUES (this makes the JSX logic less messy)

src/JsonEditor.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const Editor: React.FC<JsonEditorProps> = ({
5555
id,
5656
customText = {},
5757
customNodeDefinitions = [],
58+
useJSON5Editor = false,
5859
}) => {
5960
const { getStyles, setTheme, setIcons } = useTheme()
6061
const { setCollapseState } = useTreeState()
@@ -196,6 +197,7 @@ const Editor: React.FC<JsonEditorProps> = ({
196197
translate,
197198
customNodeDefinitions,
198199
parentData: null,
200+
useJSON5Editor,
199201
}
200202

201203
const mainContainerStyles = { ...getStyles('container', nodeData), minWidth, maxWidth }

src/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export interface JsonEditorProps {
4040
translations?: Partial<LocalisedStrings>
4141
customNodeDefinitions?: CustomNodeDefinition[]
4242
customText?: CustomTextDefinitions
43+
useJSON5Editor?: boolean | JSON5StringifyOptions
4344
}
4445

4546
const ValueDataTypes = ['string', 'number', 'boolean', 'null'] as const
@@ -175,6 +176,7 @@ export interface CollectionNodeProps extends BaseNodeProps {
175176
showCollectionCount: boolean | 'when-closed'
176177
showStringQuotes: boolean
177178
defaultValue: unknown
179+
useJSON5Editor: boolean | JSON5StringifyOptions
178180
}
179181

180182
export type ValueData = string | number | boolean
@@ -305,3 +307,14 @@ export type ThemeInput =
305307
| Theme
306308
| Partial<ThemeStyles>
307309
| Array<ThemeName | Theme | Partial<ThemeStyles>>
310+
311+
// JSON5 options (https://json5.org/)
312+
export interface JSON5StringifyOptions {
313+
space?: number
314+
quote?: string
315+
replacer?:
316+
| Array<string | number>
317+
| ((this: any, key: string, value: any) => any)
318+
| null
319+
| undefined
320+
}

0 commit comments

Comments
 (0)