-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
type.ts
299 lines (267 loc) · 7.73 KB
/
type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import type { SxProps, Theme } from '@mui/material/styles'
import type { ComponentType, CSSProperties, Dispatch, FC, SetStateAction } from 'react'
import type { Colorspace } from './theme/base16'
export type Path = (string | number)[]
/**
* @param path path to the target value
* @param oldValue
* @param newValue
*/
export type JsonViewerOnChange = <U = unknown>(
path: Path,
oldValue: U,
newValue: U
/*, type: ChangeType */
) => void
/**
* @param path path to the target value
* @param value the value to be copied
* @param copy the function to copy the value to clipboard
*/
export type JsonViewerOnCopy = <U = unknown>(
path: Path,
value: U,
copy: (value: string) => Promise<void>
) => unknown | Promise<unknown>
/**
* @param path path to the target value
* @param value the value to be selected
*/
export type JsonViewerOnSelect = <U = unknown>(
path: Path,
value: U,
) => void
/**
* @param path path to the parent target where the value will be added
*/
export type JsonViewerOnAdd = (
path: Path,
) => void
/**
* @param path path to the target value
* @param value the value to be deleted
*/
export type JsonViewerOnDelete = <U = unknown>(
path: Path,
value: U,
) => void
export interface DataItemProps<ValueType = unknown> {
inspect: boolean
setInspect: Dispatch<SetStateAction<boolean>>
value: ValueType
prevValue: ValueType | undefined
path: Path
nestedIndex?: number
}
export type EditorProps<ValueType = unknown> = {
path: Path
value: ValueType
setValue: Dispatch<ValueType>
abortEditing: () => void
commitEditing: (newValue: string) => void
}
/**
* A data type definition, including methods for checking, serializing, and deserializing values, as well as components for rendering and editing values.
*
* @template ValueType The type of the value represented by this data type
*/
export type DataType<ValueType = unknown> = {
/**
* Determines whether a given value belongs to this data type.
*
* @param value The value to check
* @param path The path to the value within the input data structure
* @returns `true` if the value belongs to this data type, `false` otherwise
*/
is: (value: unknown, path: Path) => boolean
/**
* Convert the value of this data type to a string for editing
*/
serialize?: (value: ValueType) => string
/**
* Converts a string representation of a value back to a value of this data type.
*
* Throws an error if the input is invalid, in which case the editor will ignore the change.
*/
deserialize?: (value: string) => ValueType
/**
* The main component used to render a value of this data type.
*/
Component: ComponentType<DataItemProps<ValueType>>
/**
* An optional custom editor component for editing values of this data type.
*
* You must also provide a `serialize` and `deserialize` function to enable this feature.
*/
Editor?: ComponentType<EditorProps<string>>
/**
* An optional component to render before the value.
*
* In collapsed mode, it will still be rendered as a prefix.
*/
PreComponent?: ComponentType<DataItemProps<ValueType>>
/**
* An optional component to render after the value.
*
* In collapsed mode, it will still be rendered as a suffix.
*/
PostComponent?: ComponentType<DataItemProps<ValueType>>
}
export interface JsonViewerKeyRenderer extends FC<DataItemProps> {
when (props: DataItemProps): boolean
}
export type JsonViewerTheme = 'light' | 'dark' | 'auto' | Colorspace
export type JsonViewerProps<T = unknown> = {
/**
* Any value, `object`, `Array`, primitive type, even `Map` or `Set`.
*/
value: T
/**
* Name of the root value
*
* @default "root"
*/
rootName?: false | string
/**
* Color theme.
*
* @default 'light'
*/
theme?: JsonViewerTheme
className?: string
style?: CSSProperties
/**
* [The `sx` prop](https://mui.com/system/getting-started/the-sx-prop/) lets you style elements inline, using values from the theme.
*
* @see https://mui.com/system/getting-started/the-sx-prop/
*/
sx?: SxProps<Theme>
/**
* Indent width for nested objects
*
* @default 3
*/
indentWidth?: number
/**
* Customize a key, if `keyRenderer.when` returns `true`.
*/
keyRenderer?: JsonViewerKeyRenderer
/**
* Customize the definition of data types.
*
* @see https://viewer.textea.io/how-to/data-types
*/
valueTypes?: DataType<any>[]
/**
* Whether enable add feature.
*
* @default false
* */
enableAdd?: boolean | (<U = unknown>(path: Path, currentValue: U) => boolean)
/**
* Whether enable delete feature.
*
* @default false
* */
enableDelete?: boolean | (<U = unknown>(path: Path, currentValue: U) => boolean)
/**
* Whether enable clipboard feature.
*
* @default true
*/
enableClipboard?: boolean
/**
* Whether this value can be edited.
*
* Provide a function to customize this behavior by returning a boolean based on the value and path.
*
* @default false
*/
editable?: boolean | (<U = unknown>(path: Path, currentValue: U) => boolean)
/** Callback when value changed. */
onChange?: JsonViewerOnChange
/** Callback when value copied, you can use it to customize the copy behavior.<br />\*Note: you will have to write the data to the clipboard by yourself. */
onCopy?: JsonViewerOnCopy
/** Callback when value selected. */
onSelect?: JsonViewerOnSelect
/** Callback when the add button is clicked. This is the function which implements the add feature. Please see the official demo for more details. */
onAdd?: JsonViewerOnAdd
/** Callback when the delete button is clicked. This is the function which implements the delete feature. Please see the official demo for more details. */
onDelete?: JsonViewerOnDelete
/**
* Default inspect depth for nested objects.
* _If the number is set too large, it could result in performance issues._
*
* @default 5
*/
defaultInspectDepth?: number
/**
* Default inspect control for nested objects.
*
* Provide a function to customize which fields should be expanded by default.
*/
defaultInspectControl?: (path: Path, value: unknown) => boolean
/**
* Hide items after reaching the count.
* `Array` and `Object` will be affected.
*
* _If the number is set too large, it could result in performance issues._
*
* @default 30
*/
maxDisplayLength?: number
/**
* When an integer value is assigned, arrays will be displayed in groups by count of the value.
* Groups are displayed with bracket notation and can be expanded and collapsed by clicking on the brackets.
*
* @default 100
*/
groupArraysAfterLength?: number
/**
* Cut off the string after reaching the count.
* Collapsed strings are followed by an ellipsis.
*
* String content can be expanded and collapsed by clicking on the string value.
*
* @default 50
*/
collapseStringsAfterLength?: number | false
/**
* Whether sort keys through `String.prototype.localeCompare()`
*
* @default false
*/
objectSortKeys?: boolean | ((a: string, b: string) => number)
/**
* Whether add quotes on keys.
*
* @default true
*/
quotesOnKeys?: boolean
/**
* Whether display data type labels
*
* @default true
*/
displayDataTypes?: boolean
/**
* Whether display the size of `Object`, `Array`, `Map` and `Set`.
*
* Provide a function to customize this behavior by returning a boolean based on the value and path.
*
* @default true
*/
displaySize?: boolean | ((path: Path, value: unknown) => boolean)
/**
* Whether display comma at the end of items. Just like valid JSON.
*
* @default false
*/
displayComma?: boolean
/**
* Whether to highlight updates.
*
* @default false
*/
highlightUpdates?: boolean
}