App.jsx
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 |
import React, { useState } from "react"; import { Container, Form, Button, Row, Col } from "react-bootstrap"; import { Light as SyntaxHighlighter } from "react-syntax-highlighter"; import { docco, solarizedDark, github, monokai, shadesOfPurple } from "react-syntax-highlighter/dist/esm/styles/hljs"; import "bootstrap/dist/css/bootstrap.min.css"; // Example source code for demonstration const exampleCode = { javascript: `const greet = (name) => console.log("Hello, " + name);`, python: `def greet(name):\n print(f"Hello, {name}")`, java: `public class Main {\n public static void main(String[] args) {\n System.out.println("Hello, World!");\n }\n}`, ruby: `def greet(name)\n puts "Hello, #{name}"\nend`, c: `#include <stdio.h>\nint main() {\n printf("Hello, World!\\n");\n return 0;\n}`, html: `<!DOCTYPE html>\n<html>\n<head>\n <title>Hello World</title>\n</head>\n<body>\n <h1>Hello, World!</h1>\n</body>\n</html>`, }; const themes = [ { label: "Light (Docco)", value: docco }, { label: "Dark (Solarized)", value: solarizedDark }, { label: "GitHub", value: github }, { label: "Monokai", value: monokai }, { label: "Shades of Purple", value: shadesOfPurple }, ]; const languages = [ "javascript", "python", "java", "ruby", "c", "html", "css", "php", "typescript", "json", "bash", "sql", "xml", ]; const App = () => { const [language, setLanguage] = useState("javascript"); const [theme, setTheme] = useState(docco); const [code, setCode] = useState(exampleCode.javascript); const handleLanguageChange = (e) => { setLanguage(e.target.value); setCode(exampleCode[e.target.value]); }; const handleThemeChange = (e) => setTheme(themes.find(t => t.label === e.target.value).value); const handleCodeChange = (e) => setCode(e.target.value); return ( <Container className="my-4"> <h1 className="text-center">Source Code Viewer</h1> <Row> <Col md={6}> {/* Language Selection */} <Form.Group controlId="language" className="my-3"> <Form.Label>Select Programming Language</Form.Label> <Form.Control as="select" value={language} onChange={handleLanguageChange}> {languages.map((lang) => ( <option key={lang} value={lang}> {lang.charAt(0).toUpperCase() + lang.slice(1)} </option> ))} </Form.Control> </Form.Group> </Col> <Col md={6}> {/* Theme Selection */} <Form.Group controlId="theme" className="my-3"> <Form.Label>Select Theme</Form.Label> <Form.Control as="select" onChange={handleThemeChange}> {themes.map((t) => ( <option key={t.label} value={t.label}> {t.label} </option> ))} </Form.Control> </Form.Group> </Col> </Row> {/* Code Input Textarea */} <Form.Group controlId="code" className="my-3"> <Form.Label>Enter Code</Form.Label> <Form.Control as="textarea" rows={6} value={code} onChange={handleCodeChange} /> </Form.Group> {/* Display Syntax Highlighted Code */} <div className="mt-4"> <h5>Highlighted Code:</h5> <SyntaxHighlighter language={language} style={theme} showLineNumbers > {code} </SyntaxHighlighter> </div> </Container> ); }; export default App; |