()
| 11 | import {usePreferences} from '~/components/PreferencesProvider' |
| 12 | |
| 13 | export function JsonEditor() { |
| 14 | const [json] = useJson(); |
| 15 | const { selectedNodeId } = useJsonColumnViewState(); |
| 16 | const { goToNodeId } = useJsonColumnViewAPI(); |
| 17 | const [preferences] = usePreferences(); |
| 18 | |
| 19 | const jsonMapped = useMemo(() => { |
| 20 | return jsonMap.stringify(json, null, preferences?.indent || 2); |
| 21 | }, [json, preferences]); |
| 22 | |
| 23 | const selection = useMemo<{ start: number; end: number } | undefined>(() => { |
| 24 | if (!selectedNodeId) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | const path = new JSONHeroPath(selectedNodeId); |
| 29 | const pointer = path.jsonPointer(); |
| 30 | |
| 31 | const location = jsonMapped.pointers[pointer]; |
| 32 | |
| 33 | if (location) { |
| 34 | if (location.key) { |
| 35 | return { start: location.key.pos, end: location.valueEnd.pos }; |
| 36 | } |
| 37 | |
| 38 | return { start: location.value.pos, end: location.valueEnd.pos }; |
| 39 | } |
| 40 | }, [selectedNodeId, jsonMapped]); |
| 41 | |
| 42 | const currentSelectedLine = useRef<number | undefined>(undefined); |
| 43 | |
| 44 | const onUpdate = useCallback( |
| 45 | (update: ViewUpdate) => { |
| 46 | if (!update.selectionSet) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | const range = update.state.selection.ranges[0]; |
| 51 | const line = update.state.doc.lineAt(range.anchor); |
| 52 | |
| 53 | if ( |
| 54 | currentSelectedLine.current && |
| 55 | currentSelectedLine.current === line.number |
| 56 | ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | currentSelectedLine.current = line.number; |
| 61 | |
| 62 | // Find the key if the selected line using jsonMapped.pointers |
| 63 | const pointerEntry = Object.entries(jsonMapped.pointers).find( |
| 64 | ([pointer, info]) => { |
| 65 | return info.value.line === line.number - 1; |
| 66 | } |
| 67 | ); |
| 68 | |
| 69 | if (!pointerEntry) { |
| 70 | return; |
nothing calls this directly
no test coverage detected