Line data Source code
1 : import * as React from 'react';
2 :
3 : import { type JsonViewProps } from './json_view.js';
4 : import { ToolVisibilityStore, ToolVisibilityStateProvider } from './json_tool_state.js';
5 : import { useNestedExpansionReducer, NestedExpansionStateProvider } from './json_nested_state.js';
6 :
7 : export interface JsonViewerState {
8 : value?: object;
9 : variant?: JsonViewProps['variant'];
10 : onExpand?: JsonViewProps['onExpand'];
11 : onCopied?: JsonViewProps['onCopied'];
12 : beforeCopy?: JsonViewProps['beforeCopy'];
13 : objectSortKeys?: JsonViewProps['objectSortKeys'];
14 : displayObjectSize?: JsonViewProps['displayObjectSize'];
15 : shortenTextAfterLength?: JsonViewProps['shortenTextAfterLength'];
16 : stringEllipsis?: JsonViewProps['stringEllipsis'];
17 : enableClipboard?: JsonViewProps['enableClipboard'];
18 : highlightUpdates?: JsonViewProps['highlightUpdates'];
19 : collapsed?: JsonViewProps['collapsed'];
20 : shouldExpandNodeInitially?: JsonViewProps['shouldExpandNodeInitially'];
21 : indentWidth?: number;
22 : arrayPageSize?: number;
23 : }
24 :
25 1 : export const initialState: JsonViewerState = {
26 1 : variant: 'json',
27 1 : objectSortKeys: false,
28 1 : indentWidth: 15,
29 1 : };
30 :
31 1 : export const Context = React.createContext<JsonViewerState>(initialState);
32 :
33 1 : export const useJsonViewerState = () => {
34 1 : return React.useContext(Context);
35 1 : };
36 :
37 : export interface JsonViewerStateProps {
38 : initialState?: JsonViewerState;
39 : }
40 :
41 1 : export const JsonViewerStateProvider = ({
42 1 : children,
43 1 : initialState: init,
44 1 : }: React.PropsWithChildren<JsonViewerStateProps>) => {
45 : // The JsonView memoizes the state, so this only changes when one of its props changes
46 1 : const state = React.useMemo(() => Object.assign({}, initialState, init), [init]);
47 1 : const [toolStore] = React.useState(() => new ToolVisibilityStore());
48 1 : const [expands, expandsDispatch] = useNestedExpansionReducer();
49 1 : return (
50 1 : <Context.Provider value={state}>
51 1 : <ToolVisibilityStateProvider store={toolStore}>
52 1 : <NestedExpansionStateProvider initial={expands} dispatch={expandsDispatch}>
53 1 : {children}
54 1 : </NestedExpansionStateProvider>
55 1 : </ToolVisibilityStateProvider>
56 1 : </Context.Provider>
57 : );
58 1 : };
|