|
| 1 | +import { |
| 2 | + Flex, |
| 3 | + Box, |
| 4 | + IconButton, |
| 5 | + VStack, |
| 6 | + HStack, |
| 7 | + Text, |
| 8 | + Heading, |
| 9 | + RadioGroup, |
| 10 | + Radio, |
| 11 | + FormControl, |
| 12 | + FormLabel, |
| 13 | + useClipboard, |
| 14 | +} from "@chakra-ui/react"; |
| 15 | +import { CopyIcon, CloseIcon } from "@chakra-ui/icons"; |
| 16 | +import { Editor, EditorChange } from "codemirror"; |
| 17 | +import { Controlled } from "react-codemirror2"; |
| 18 | +import { useState, useEffect } from "react"; |
| 19 | +import "codemirror/lib/codemirror.css"; |
| 20 | +import "codemirror/theme/material.css"; |
| 21 | + |
| 22 | +type ConvertType = "toYAML" | "toJSON"; |
| 23 | +const defaultIndent = 2; |
| 24 | + |
| 25 | +const JsonYaml = () => { |
| 26 | + const [input, setInput] = useState<string>(""); |
| 27 | + const [output, setOutput] = useState<string>(""); |
| 28 | + const [indent, setIndent] = useState<number>(defaultIndent); |
| 29 | + const [convertType, setConvertType] = useState<ConvertType>("toYAML"); |
| 30 | + const { onCopy } = useClipboard(output); |
| 31 | + |
| 32 | + const handleChange = (editor: Editor, data: EditorChange, value: string) => { |
| 33 | + setInput(value); |
| 34 | + }; |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + const f = async () => { |
| 38 | + const { toJson, toYaml } = await import("@/utils/json-yaml"); |
| 39 | + if (input === "") { |
| 40 | + setOutput(""); |
| 41 | + return; |
| 42 | + } |
| 43 | + try { |
| 44 | + if (convertType === "toYAML") { |
| 45 | + setOutput(toYaml(input, indent)); |
| 46 | + } else { |
| 47 | + setOutput(toJson(input, indent)); |
| 48 | + } |
| 49 | + } catch (e: any) { |
| 50 | + setOutput(e.message); |
| 51 | + } |
| 52 | + }; |
| 53 | + f(); |
| 54 | + }, [input, indent, convertType]); |
| 55 | + |
| 56 | + return ( |
| 57 | + <VStack spacing="5"> |
| 58 | + <HStack spacing="5" w="100%"> |
| 59 | + {/* Input Box */} |
| 60 | + <Box w="45%"> |
| 61 | + <Flex justifyContent={"space-between"}> |
| 62 | + <Text fontSize="2xl" mb="10px" ml="45%"> |
| 63 | + Input |
| 64 | + </Text> |
| 65 | + <HStack spacing="2"> |
| 66 | + <IconButton |
| 67 | + aria-label="Clear" |
| 68 | + icon={<CloseIcon />} |
| 69 | + onClick={e => setInput("")} |
| 70 | + /> |
| 71 | + </HStack> |
| 72 | + </Flex> |
| 73 | + <Controlled |
| 74 | + onBeforeChange={handleChange} |
| 75 | + value={input} |
| 76 | + options={{ |
| 77 | + lineNumbers: true, |
| 78 | + mode: convertType === "toYAML" ? "json" : "yaml", |
| 79 | + theme: "material", |
| 80 | + }} |
| 81 | + /> |
| 82 | + </Box> |
| 83 | + {/* Output Box */} |
| 84 | + <Box w="45%"> |
| 85 | + <Flex justifyContent={"space-between"}> |
| 86 | + <Text fontSize="2xl" mb="10px" ml="45%"> |
| 87 | + Output |
| 88 | + </Text> |
| 89 | + <HStack spacing="2"> |
| 90 | + <IconButton |
| 91 | + aria-label="Copy" |
| 92 | + icon={<CopyIcon />} |
| 93 | + onClick={onCopy} |
| 94 | + /> |
| 95 | + </HStack> |
| 96 | + </Flex> |
| 97 | + <Controlled |
| 98 | + onBeforeChange={handleChange} |
| 99 | + value={output} |
| 100 | + options={{ |
| 101 | + lineNumbers: true, |
| 102 | + mode: convertType === "toYAML" ? "yaml" : "json", |
| 103 | + readOnly: true, |
| 104 | + theme: "material", |
| 105 | + }} |
| 106 | + /> |
| 107 | + </Box> |
| 108 | + </HStack> |
| 109 | + |
| 110 | + {/* Configuration part */} |
| 111 | + <Flex w="100%"> |
| 112 | + <VStack spacing="3"> |
| 113 | + <Heading size="lg">Configuration</Heading> |
| 114 | + |
| 115 | + <FormControl as="fieldset"> |
| 116 | + <FormLabel as="legend">Convert target</FormLabel> |
| 117 | + <RadioGroup |
| 118 | + value={convertType} |
| 119 | + onChange={value => setConvertType(value as ConvertType)} |
| 120 | + > |
| 121 | + <HStack> |
| 122 | + <Radio value="toYAML">To YAML</Radio> |
| 123 | + <Radio value="toJSON">To JSON</Radio> |
| 124 | + </HStack> |
| 125 | + </RadioGroup> |
| 126 | + </FormControl> |
| 127 | + |
| 128 | + <FormControl as="fieldset"> |
| 129 | + <FormLabel as="legend">Indent</FormLabel> |
| 130 | + <RadioGroup |
| 131 | + onChange={value => setIndent(Number(value))} |
| 132 | + value={indent.toString()} |
| 133 | + > |
| 134 | + <HStack direction="row"> |
| 135 | + <Radio value="2">2</Radio> |
| 136 | + <Radio value="4">4</Radio> |
| 137 | + </HStack> |
| 138 | + </RadioGroup> |
| 139 | + </FormControl> |
| 140 | + </VStack> |
| 141 | + </Flex> |
| 142 | + </VStack> |
| 143 | + ); |
| 144 | +}; |
| 145 | + |
| 146 | +export default JsonYaml; |
0 commit comments