|
1 | 1 | import type { SetStateAction } from 'react'
|
2 |
| -import create from 'zustand' |
| 2 | +import type { StoreApi } from 'zustand' |
| 3 | +import { create } from 'zustand' |
3 | 4 | import createContext from 'zustand/context'
|
4 |
| -import { combine } from 'zustand/middleware' |
5 | 5 |
|
6 | 6 | import type {
|
7 | 7 | JsonViewerOnChange,
|
@@ -38,85 +38,75 @@ export type JsonViewerState<T = unknown> = {
|
38 | 38 | onSelect: JsonViewerOnSelect | undefined
|
39 | 39 | keyRenderer: JsonViewerKeyRenderer
|
40 | 40 | displayObjectSize: boolean
|
41 |
| -} |
42 | 41 |
|
43 |
| -export type JsonViewerActions = { |
44 | 42 | getInspectCache: (path: Path, nestedIndex?: number) => boolean
|
45 | 43 | setInspectCache: (
|
46 | 44 | path: Path, action: SetStateAction<boolean>, nestedIndex?: number) => void
|
47 | 45 | setHover: (path: Path | null, nestedIndex?: number) => void
|
48 | 46 | }
|
49 | 47 |
|
50 |
| -export const createJsonViewerStore = <T = unknown> (props: JsonViewerProps<T>) => |
51 |
| - create( |
52 |
| - combine<JsonViewerState<T>, JsonViewerActions>( |
53 |
| - { |
54 |
| - // provided by user |
55 |
| - enableClipboard: props.enableClipboard ?? true, |
56 |
| - indentWidth: props.indentWidth ?? 3, |
57 |
| - groupArraysAfterLength: props.groupArraysAfterLength ?? 100, |
58 |
| - collapseStringsAfterLength: |
59 |
| - (props.collapseStringsAfterLength === false) |
60 |
| - ? Number.MAX_VALUE |
61 |
| - : props.collapseStringsAfterLength ?? 50, |
62 |
| - maxDisplayLength: props.maxDisplayLength ?? 30, |
63 |
| - rootName: props.rootName ?? 'root', |
64 |
| - onChange: props.onChange ?? (() => {}), |
65 |
| - onCopy: props.onCopy ?? undefined, |
66 |
| - onSelect: props.onSelect ?? undefined, |
67 |
| - keyRenderer: props.keyRenderer ?? DefaultKeyRenderer, |
68 |
| - editable: props.editable ?? false, |
69 |
| - defaultInspectDepth: props.defaultInspectDepth ?? 5, |
70 |
| - objectSortKeys: props.objectSortKeys ?? false, |
71 |
| - quotesOnKeys: props.quotesOnKeys ?? true, |
72 |
| - displayDataTypes: props.displayDataTypes ?? true, |
73 |
| - // internal state |
74 |
| - inspectCache: {}, |
75 |
| - hoverPath: null, |
76 |
| - colorspace: lightColorspace, |
77 |
| - value: props.value, |
78 |
| - displayObjectSize: props.displayObjectSize ?? true |
79 |
| - }, |
80 |
| - (set, get) => ({ |
81 |
| - getInspectCache: (path, nestedIndex) => { |
82 |
| - const target = nestedIndex !== undefined |
83 |
| - ? path.join('.') + |
| 48 | +export const createJsonViewerStore = <T = unknown> (props: JsonViewerProps<T>) => { |
| 49 | + return create<JsonViewerState>()((set, get) => ({ |
| 50 | + // provided by user |
| 51 | + enableClipboard: props.enableClipboard ?? true, |
| 52 | + indentWidth: props.indentWidth ?? 3, |
| 53 | + groupArraysAfterLength: props.groupArraysAfterLength ?? 100, |
| 54 | + collapseStringsAfterLength: |
| 55 | + (props.collapseStringsAfterLength === false) |
| 56 | + ? Number.MAX_VALUE |
| 57 | + : props.collapseStringsAfterLength ?? 50, |
| 58 | + maxDisplayLength: props.maxDisplayLength ?? 30, |
| 59 | + rootName: props.rootName ?? 'root', |
| 60 | + onChange: props.onChange ?? (() => {}), |
| 61 | + onCopy: props.onCopy ?? undefined, |
| 62 | + onSelect: props.onSelect ?? undefined, |
| 63 | + keyRenderer: props.keyRenderer ?? DefaultKeyRenderer, |
| 64 | + editable: props.editable ?? false, |
| 65 | + defaultInspectDepth: props.defaultInspectDepth ?? 5, |
| 66 | + objectSortKeys: props.objectSortKeys ?? false, |
| 67 | + quotesOnKeys: props.quotesOnKeys ?? true, |
| 68 | + displayDataTypes: props.displayDataTypes ?? true, |
| 69 | + // internal state |
| 70 | + inspectCache: {}, |
| 71 | + hoverPath: null, |
| 72 | + colorspace: lightColorspace, |
| 73 | + value: props.value, |
| 74 | + displayObjectSize: props.displayObjectSize ?? true, |
| 75 | + |
| 76 | + getInspectCache: (path, nestedIndex) => { |
| 77 | + const target = nestedIndex !== undefined |
| 78 | + ? path.join('.') + |
84 | 79 | `[${nestedIndex}]nt`
|
85 |
| - : path.join('.') |
86 |
| - return get().inspectCache[target] |
87 |
| - }, |
88 |
| - setInspectCache: (path, action, nestedIndex) => { |
89 |
| - const target = nestedIndex !== undefined |
90 |
| - ? path.join('.') + |
| 80 | + : path.join('.') |
| 81 | + return get().inspectCache[target] |
| 82 | + }, |
| 83 | + setInspectCache: (path, action, nestedIndex) => { |
| 84 | + const target = nestedIndex !== undefined |
| 85 | + ? path.join('.') + |
91 | 86 | `[${nestedIndex}]nt`
|
92 |
| - : path.join('.') |
93 |
| - set(state => ({ |
94 |
| - inspectCache: { |
95 |
| - ...state.inspectCache, |
96 |
| - [target]: typeof action === 'function' |
97 |
| - ? action( |
98 |
| - state.inspectCache[target]) |
99 |
| - : action |
100 |
| - } |
101 |
| - })) |
102 |
| - }, |
103 |
| - setHover: (path, nestedIndex) => { |
104 |
| - set({ |
105 |
| - hoverPath: path |
106 |
| - ? ({ |
107 |
| - path, |
108 |
| - nestedIndex |
109 |
| - }) |
110 |
| - : null |
111 |
| - }) |
| 87 | + : path.join('.') |
| 88 | + set(state => ({ |
| 89 | + inspectCache: { |
| 90 | + ...state.inspectCache, |
| 91 | + [target]: typeof action === 'function' |
| 92 | + ? action( |
| 93 | + state.inspectCache[target]) |
| 94 | + : action |
112 | 95 | }
|
| 96 | + })) |
| 97 | + }, |
| 98 | + setHover: (path, nestedIndex) => { |
| 99 | + set({ |
| 100 | + hoverPath: path |
| 101 | + ? ({ path, nestedIndex }) |
| 102 | + : null |
113 | 103 | })
|
114 |
| - ) |
115 |
| - ) |
116 |
| -export type JsonViewerStore = ReturnType<typeof createJsonViewerStore> |
| 104 | + } |
| 105 | + })) |
| 106 | +} |
117 | 107 |
|
118 | 108 | export const {
|
119 | 109 | useStore: useJsonViewerStore,
|
120 | 110 | useStoreApi: useJsonViewerStoreApi,
|
121 | 111 | Provider: JsonViewerProvider
|
122 |
| -} = createContext<JsonViewerStore>() |
| 112 | +} = createContext<StoreApi<JsonViewerState>>() |
0 commit comments