Skip to content

Commit 90d718f

Browse files
committed
Remove JSON5 from main components and make it prop
1 parent ae22b3b commit 90d718f

File tree

8 files changed

+19
-13
lines changed

8 files changed

+19
-13
lines changed

demo/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"firebase": "^10.7.2",
2020
"framer-motion": "^11.0.3",
2121
"json-edit-react": "^1.14.1",
22+
"json5": "^2.2.3",
2223
"react": "^18.2.0",
2324
"react-datepicker": "^5.0.0",
2425
"react-dom": "^18.2.0",

demo/src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useEffect, useRef } from 'react'
22
import { useSearch, useLocation } from 'wouter'
3+
import JSON5 from 'json5'
34
import 'react-datepicker/dist/react-datepicker.css'
45
import {
56
JsonEditor,
@@ -362,6 +363,7 @@ function App() {
362363
customNodeDefinitions={dataDefinition?.customNodeDefinitions}
363364
customText={dataDefinition?.customTextDefinitions}
364365
onChange={dataDefinition?.onChange ?? undefined}
366+
jsonParse={JSON5.parse}
365367
/>
366368
</Box>
367369
<VStack w="100%" align="flex-end" gap={4}>

demo/src/_imports.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
*/
44

55
/* Installed package */
6-
export * from 'json-edit-react'
6+
// export * from 'json-edit-react'
77

88
/* Local src */
9-
// export * from './json-edit-react/src'
9+
export * from './json-edit-react/src'
1010

1111
/* Compiled local package */
1212
// export * from './package/build'

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"wrap-ansi": "7.0.0"
3232
},
3333
"dependencies": {
34-
"json5": "^2.2.3",
3534
"object-property-assigner": "^1.3.0",
3635
"object-property-extractor": "^1.0.11"
3736
},

src/CollectionNode.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useEffect, useState, useMemo, useRef } from 'react'
2-
import JSON5 from 'json5'
32
import extractProperty from 'object-property-extractor'
43
import { ValueNodeWrapper } from './ValueNodeWrapper'
54
import { EditButtons, InputButtons } from './ButtonPanels'
@@ -43,8 +42,10 @@ export const CollectionNode: React.FC<CollectionNodeProps> = (props) => {
4342
defaultValue,
4443
translate,
4544
customNodeDefinitions,
45+
jsonParse,
46+
jsonStringify,
4647
} = props
47-
const [stringifiedValue, setStringifiedValue] = useState(JSON.stringify(data, null, 2))
48+
const [stringifiedValue, setStringifiedValue] = useState(jsonStringify(data))
4849

4950
const startCollapsed = collapseFilter(incomingNodeData)
5051
const [collapsed, setCollapsed] = useState(startCollapsed)
@@ -80,7 +81,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = (props) => {
8081
const [isAnimating, setIsAnimating] = useState(false)
8182

8283
useEffect(() => {
83-
setStringifiedValue(JSON.stringify(data, null, 2))
84+
setStringifiedValue(jsonStringify(data))
8485
}, [data])
8586

8687
useEffect(() => {
@@ -150,7 +151,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = (props) => {
150151

151152
const handleEdit = () => {
152153
try {
153-
const value = JSON5.parse(stringifiedValue)
154+
const value = jsonParse(stringifiedValue)
154155
setCurrentlyEditingElement(null)
155156
setError(null)
156157
if (JSON.stringify(value) === JSON.stringify(data)) return
@@ -205,7 +206,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = (props) => {
205206
const handleCancel = () => {
206207
setCurrentlyEditingElement(null)
207208
setError(null)
208-
setStringifiedValue(JSON.stringify(data, null, 2))
209+
setStringifiedValue(jsonStringify(data))
209210
}
210211

211212
// DERIVED VALUES (this makes the JSX conditional logic easier to follow)

src/JsonEditor.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const Editor: React.FC<JsonEditorProps> = ({
5959
id,
6060
customText = {},
6161
customNodeDefinitions = [],
62+
jsonParse = JSON.parse,
63+
jsonStringify = (data: JsonData) => JSON.stringify(data, null, 2),
6264
}) => {
6365
const { getStyles } = useTheme()
6466
const collapseFilter = useCallback(getFilterFunction(collapse), [collapse])
@@ -265,6 +267,8 @@ const Editor: React.FC<JsonEditorProps> = ({
265267
translate,
266268
customNodeDefinitions,
267269
parentData: null,
270+
jsonParse,
271+
jsonStringify,
268272
}
269273

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

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export interface JsonEditorProps {
4343
translations?: Partial<LocalisedStrings>
4444
customNodeDefinitions?: CustomNodeDefinition[]
4545
customText?: CustomTextDefinitions
46+
jsonParse?: (input: string) => JsonData
47+
jsonStringify?: (input: JsonData) => string
4648
}
4749

4850
const ValueDataTypes = ['string', 'number', 'boolean', 'null'] as const
@@ -196,6 +198,8 @@ export interface CollectionNodeProps extends BaseNodeProps {
196198
showCollectionCount: boolean | 'when-closed'
197199
showStringQuotes: boolean
198200
defaultValue: unknown
201+
jsonParse: (input: string) => JsonData
202+
jsonStringify: (data: JsonData) => string
199203
}
200204

201205
export type ValueData = string | number | boolean

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,11 +2087,6 @@ json5@^1.0.2:
20872087
dependencies:
20882088
minimist "^1.2.0"
20892089

2090-
json5@^2.2.3:
2091-
version "2.2.3"
2092-
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
2093-
integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==
2094-
20952090
jsonfile@^6.0.1:
20962091
version "6.1.0"
20972092
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae"

0 commit comments

Comments
 (0)