|
| 1 | +import { SandpackCodeEditor, SandpackLayout, SandpackProvider, useActiveCode } from "@codesandbox/sandpack-react"; |
| 2 | +import { useMantineColorScheme } from "@mantine/core"; |
| 3 | +import { memo, useEffect, useRef, useState } from "react"; |
| 4 | + |
| 5 | +// Internal component to sync code changes back to parent |
| 6 | +const CodeSync = ({ onChange, initialCode }: { onChange?: (code: string) => void; initialCode: string }) => { |
| 7 | + const { code, updateCode } = useActiveCode(); |
| 8 | + const prevCodeRef = useRef<string>(initialCode); |
| 9 | + const isExternalUpdate = useRef(false); |
| 10 | + |
| 11 | + // Sync external code changes into sandpack |
| 12 | + useEffect(() => { |
| 13 | + if (initialCode !== prevCodeRef.current && initialCode !== code) { |
| 14 | + isExternalUpdate.current = true; |
| 15 | + updateCode(initialCode, false); |
| 16 | + prevCodeRef.current = initialCode; |
| 17 | + } |
| 18 | + }, [initialCode, updateCode, code]); |
| 19 | + |
| 20 | + // Notify parent of internal code changes |
| 21 | + useEffect(() => { |
| 22 | + if (isExternalUpdate.current) { |
| 23 | + isExternalUpdate.current = false; |
| 24 | + return; |
| 25 | + } |
| 26 | + if (code !== prevCodeRef.current) { |
| 27 | + prevCodeRef.current = code; |
| 28 | + onChange?.(code); |
| 29 | + } |
| 30 | + }, [code, onChange]); |
| 31 | + |
| 32 | + return null; |
| 33 | +}; |
| 34 | + |
| 35 | +export interface CodeEditorProps { |
| 36 | + code: string; |
| 37 | + onChange?: (code: string) => void; |
| 38 | + lang?: string; |
| 39 | + minHeight?: string; |
| 40 | + readOnly?: boolean; |
| 41 | + showLineNumbers?: boolean; |
| 42 | +} |
| 43 | + |
| 44 | +// Map common language names to file extensions |
| 45 | +const getFileExtension = (lang: string) => { |
| 46 | + const langMap: Record<string, string> = { |
| 47 | + typescript: "ts", |
| 48 | + javascript: "js", |
| 49 | + ts: "ts", |
| 50 | + js: "js", |
| 51 | + tsx: "tsx", |
| 52 | + jsx: "jsx", |
| 53 | + json: "json", |
| 54 | + html: "html", |
| 55 | + css: "css", |
| 56 | + scss: "scss", |
| 57 | + less: "less", |
| 58 | + markdown: "md", |
| 59 | + md: "md", |
| 60 | + python: "py", |
| 61 | + py: "py", |
| 62 | + rust: "rs", |
| 63 | + rs: "rs", |
| 64 | + go: "go", |
| 65 | + java: "java", |
| 66 | + c: "c", |
| 67 | + cpp: "cpp", |
| 68 | + "c++": "cpp", |
| 69 | + diff: "diff", |
| 70 | + patch: "diff", |
| 71 | + vue: "vue", |
| 72 | + svelte: "svelte", |
| 73 | + shell: "sh", |
| 74 | + bash: "sh", |
| 75 | + sh: "sh", |
| 76 | + yaml: "yaml", |
| 77 | + yml: "yaml", |
| 78 | + xml: "xml", |
| 79 | + sql: "sql", |
| 80 | + graphql: "graphql", |
| 81 | + swift: "swift", |
| 82 | + kotlin: "kt", |
| 83 | + ruby: "rb", |
| 84 | + php: "php", |
| 85 | + txt: "txt", |
| 86 | + text: "txt", |
| 87 | + }; |
| 88 | + return langMap[lang.toLowerCase()] || "txt"; |
| 89 | +}; |
| 90 | + |
| 91 | +export const CodeEditor = memo( |
| 92 | + ({ code, onChange, lang = "ts", minHeight = "200px", readOnly = false, showLineNumbers = true }: CodeEditorProps) => { |
| 93 | + const { colorScheme } = useMantineColorScheme(); |
| 94 | + const [height, setHeight] = useState(minHeight); |
| 95 | + const containerRef = useRef<HTMLDivElement>(null); |
| 96 | + const isResizing = useRef(false); |
| 97 | + const startY = useRef(0); |
| 98 | + const startHeight = useRef(0); |
| 99 | + |
| 100 | + const ext = getFileExtension(lang); |
| 101 | + const filePath = `/main.${ext}`; |
| 102 | + |
| 103 | + const handleMouseDown = (e: React.MouseEvent) => { |
| 104 | + isResizing.current = true; |
| 105 | + startY.current = e.clientY; |
| 106 | + startHeight.current = containerRef.current?.offsetHeight || parseInt(minHeight); |
| 107 | + document.body.style.cursor = "ns-resize"; |
| 108 | + document.body.style.userSelect = "none"; |
| 109 | + |
| 110 | + const handleMouseMove = (e: MouseEvent) => { |
| 111 | + if (!isResizing.current) return; |
| 112 | + const delta = e.clientY - startY.current; |
| 113 | + const newHeight = Math.max(parseInt(minHeight), startHeight.current + delta); |
| 114 | + setHeight(`${newHeight}px`); |
| 115 | + }; |
| 116 | + |
| 117 | + const handleMouseUp = () => { |
| 118 | + isResizing.current = false; |
| 119 | + document.body.style.cursor = ""; |
| 120 | + document.body.style.userSelect = ""; |
| 121 | + document.removeEventListener("mousemove", handleMouseMove); |
| 122 | + document.removeEventListener("mouseup", handleMouseUp); |
| 123 | + }; |
| 124 | + |
| 125 | + document.addEventListener("mousemove", handleMouseMove); |
| 126 | + document.addEventListener("mouseup", handleMouseUp); |
| 127 | + }; |
| 128 | + |
| 129 | + return ( |
| 130 | + <div ref={containerRef} className="relative" style={{ height }}> |
| 131 | + <SandpackProvider |
| 132 | + files={{ |
| 133 | + [filePath]: { |
| 134 | + code, |
| 135 | + active: true, |
| 136 | + }, |
| 137 | + }} |
| 138 | + theme={colorScheme === "dark" ? "dark" : "light"} |
| 139 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 140 | + // @ts-ignore |
| 141 | + style={{ ["--sp-layout-height"]: "100%", height: "100%" }} |
| 142 | + > |
| 143 | + <SandpackLayout className="border-color h-full overflow-hidden rounded-[6px] border"> |
| 144 | + <SandpackCodeEditor showLineNumbers={showLineNumbers} showReadOnly={false} readOnly={readOnly} /> |
| 145 | + </SandpackLayout> |
| 146 | + {!readOnly && onChange && <CodeSync onChange={onChange} initialCode={code} />} |
| 147 | + </SandpackProvider> |
| 148 | + {/* Resize handle */} |
| 149 | + <div |
| 150 | + className="border-color absolute bottom-0 left-0 right-0 flex h-[8px] cursor-ns-resize items-center justify-center border-t bg-gray-100 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700" |
| 151 | + onMouseDown={handleMouseDown} |
| 152 | + > |
| 153 | + <div className="h-[2px] w-[30px] rounded bg-gray-400" /> |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + ); |
| 157 | + } |
| 158 | +); |
| 159 | + |
| 160 | +CodeEditor.displayName = "CodeEditor"; |
0 commit comments