({ children }: { children: ReactNode })
| 30 | ); |
| 31 | |
| 32 | export function JsonColumnViewProvider({ children }: { children: ReactNode }) { |
| 33 | const [json] = useJson(); |
| 34 | const { doc, path: initialNodeId } = useJsonDoc(); |
| 35 | |
| 36 | const rootNode = React.useMemo(() => { |
| 37 | return generateColumnViewNode(json); |
| 38 | }, [json]); |
| 39 | |
| 40 | const jsonReducer = React.useCallback( |
| 41 | ( |
| 42 | state: ColumnViewState, |
| 43 | action: ColumnViewAction, |
| 44 | changes: ColumnViewState |
| 45 | ): ColumnViewState => { |
| 46 | if (action.type === "MOVE_UP" || action.type == "MOVE_DOWN") { |
| 47 | const { selectedNodeId } = state; |
| 48 | const { highlightedNodeId } = changes; |
| 49 | |
| 50 | invariant(selectedNodeId, "expected selectedNodeId"); |
| 51 | invariant(highlightedNodeId, "expected highlightedNodeId"); |
| 52 | |
| 53 | const calculatedPath = calculateStablePath( |
| 54 | selectedNodeId, |
| 55 | highlightedNodeId, |
| 56 | json |
| 57 | ); |
| 58 | |
| 59 | return { |
| 60 | ...changes, |
| 61 | selectedNodeId: calculatedPath, |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | if ( |
| 66 | action.type === "MOVE_TO_PARENT" && |
| 67 | action.source && |
| 68 | action.source.altKey |
| 69 | ) { |
| 70 | const { selectedNodeId } = state; |
| 71 | |
| 72 | return { |
| 73 | ...changes, |
| 74 | selectedNodeId, |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | if (action.type === "MOVE_TO_CHILDREN") { |
| 79 | const { selectedNodeId, highlightedNodeId } = state; |
| 80 | |
| 81 | invariant(selectedNodeId, "expected selectedNodeId"); |
| 82 | invariant(highlightedNodeId, "expected highlightedNodeId"); |
| 83 | |
| 84 | // If the previous highlightedNodeId is an ancestor of the previous selectedNodeId |
| 85 | if (isAncestorOf(highlightedNodeId, selectedNodeId)) { |
| 86 | // Get the next child of the highlightedNodeId in the path of selectedNodeId |
| 87 | // And make the highlightedNodeId that next child |
| 88 | // And keep the selectedNodeId unchanged |
| 89 | const highlightedPath = new JSONHeroPath(highlightedNodeId); |
nothing calls this directly
no test coverage detected