Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 25 additions & 110 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,109 +185,10 @@ export default function CodeEditor() {
const [autoRun, setAutoRun] = useState(true)
const [splitRatio, setSplitRatio] = useState(50)
const [isResizing, setIsResizing] = useState(false)


// use effect for handling full screen mode
useEffect(() => {
const handleFullscreenChange = () => {
setIsFullscreen(Boolean(document.fullscreenElement));
};

document.addEventListener("fullscreenchange", handleFullscreenChange);
return () => {
document.removeEventListener("fullscreenchange", handleFullscreenChange);
};
}, []);

const handleFullscreenToggle = async () => {
try {
if (document.fullscreenElement) {
if (document.exitFullscreen) {
await document.exitFullscreen();
}
return;
}

if (isFullscreen) {
setIsFullscreen(false);
return;
}

if (containerRef.current?.requestFullscreen) {
await containerRef.current.requestFullscreen();
return;
}

setIsFullscreen(true);
} catch (err) {
console.error("Error attempting to toggle fullscreen:", err);
setIsFullscreen((prev) => !prev);
}
};

const containerRef = useRef<HTMLDivElement>(null)
const previewRef = useRef<HTMLIFrameElement>(null)
const [isMobile, setIsMobile] = useState(false)

useEffect(() => {
const updateIsMobile = () => {
setIsMobile(window.innerWidth < 768)
}

updateIsMobile()
window.addEventListener("resize", updateIsMobile)

return () => {
window.removeEventListener("resize", updateIsMobile)
}
}, [])

const handleDragStart = () => {
isDragging.current = true;
setIsResizing(true);
document.body.style.userSelect = "none";
};

const handleDragMove = useCallback((clientX: number, clientY: number) => {
if (!isDragging.current || !containerRef.current) return;

const rect = containerRef.current.getBoundingClientRect();

let newRatio;
if (isMobile) {
newRatio = ((clientY - rect.top) / rect.height) * 100;
} else {
newRatio = ((clientX - rect.left) / rect.width) * 100;
}

const clampedRatio = Math.max(20, Math.min(80, newRatio));
setSplitRatio(clampedRatio);
}, [isMobile]);

const handleMouseMove = useCallback((e: globalThis.MouseEvent) => {
handleDragMove(e.clientX, e.clientY);
}, [handleDragMove]);

const handleTouchMove = useCallback((e: globalThis.TouchEvent) => {
if (isDragging.current) {
handleDragMove(e.touches[0].clientX, e.touches[0].clientY);
}
}, [handleDragMove]);

const handleDragEnd = useCallback(() => {
isDragging.current = false;
setIsResizing(false);
document.body.style.userSelect = "auto";
document.body.style.cursor = "default";
}, []);

// Tracks which template is currently active

const [isMobile, setIsMobile] = useState(false)
const [consoleErrors, setConsoleErrors] = useState<Array<{message: string; line?: number; col?: number}>>([])
const [consoleOpen, setConsoleOpen] = useState(false)
const [moreSheetOpen, setMoreSheetOpen] = useState(false)

const [currentTemplateId, setCurrentTemplateId] = useState<string | null>(null)
const [templateSnapshots, setTemplateSnapshots] = useState<Record<string, CodeContent>>(() => {
if (typeof window === "undefined") return {}
Expand Down Expand Up @@ -378,6 +279,18 @@ const handleDragEnd = useCallback(() => {
}
}

const handleFullscreenToggle = async () => {
try {
if (document.fullscreenElement) {
if (document.exitFullscreen) await document.exitFullscreen()
} else if (containerRef.current?.requestFullscreen) {
await containerRef.current.requestFullscreen()
}
} catch (err) {
console.error("Error toggling fullscreen:", err)
Comment on lines +282 to +290
}
}
Comment on lines +282 to +292
Comment on lines +282 to +292

const handleDragStart = useCallback(() => {
isDragging.current = true
setIsResizing(true)
Expand Down Expand Up @@ -607,7 +520,7 @@ if (layout === "preview") setLayout("split")
id: "action-fullscreen", label: isFullscreen ? "Exit fullscreen" : "Enter fullscreen", group: "Actions",
icon: isFullscreen ? <Minimize2 className="w-4 h-4" /> : <Maximize2 className="w-4 h-4" />,
keywords: "expand maximize zoom",
perform: () => setIsFullscreen((v) => !v),
perform: handleFullscreenToggle,
},
{
id: "action-theme", label: theme === "light" ? "Switch to dark mode" : "Switch to light mode", group: "Actions",
Expand Down Expand Up @@ -666,7 +579,7 @@ if (layout === "preview") setLayout("split")
{ label: "Import file", icon: <Upload className="w-5 h-5" />, action: importCode },
{ label: "Share link", icon: <LinkIcon className="w-5 h-5" />, action: copyShareLink },
{ label: "Open in tab", icon: <Maximize2 className="w-5 h-5" />, action: () => { if (previewRef.current?.src) window.open(previewRef.current.src, "_blank") } },
{ label: isFullscreen ? "Exit fullscreen" : "Fullscreen", icon: isFullscreen ? <Minimize2 className="w-5 h-5" /> : <Maximize2 className="w-5 h-5" />, action: () => { setIsFullscreen(v => !v); setMoreSheetOpen(false) } },
{ label: isFullscreen ? "Exit fullscreen" : "Fullscreen", icon: isFullscreen ? <Minimize2 className="w-5 h-5" /> : <Maximize2 className="w-5 h-5" />, action: handleFullscreenToggle },
{ label: "Command palette", icon: <Search className="w-5 h-5" />, action: () => { setMoreSheetOpen(false); setPaletteOpen(true) } },
{ label: theme === "light" ? "Dark mode" : "Light mode", icon: theme === "light" ? <Moon className="w-5 h-5" /> : <Sun className="w-5 h-5" />, action: () => { toggleTheme(); setMoreSheetOpen(false) } },
].map((item) => (
Expand Down Expand Up @@ -736,7 +649,7 @@ if (layout === "preview") setLayout("split")
<Button variant="outline" size="sm" className="h-8 text-xs" onClick={importCode}><Upload className="w-3.5 h-3.5 mr-1.5" />Import</Button>
<Button variant="outline" size="sm" className="h-8 text-xs" onClick={downloadCode}><Download className="w-3.5 h-3.5 mr-1.5" />Download</Button>
<CopyButton text={shareUrl} />
<Button variant="outline" size="sm" className="h-8 w-8 p-0" onClick={() => setIsFullscreen(!isFullscreen)}>
<Button variant="outline" size="sm" className="h-8 w-8 p-0" onClick={handleFullscreenToggle}>
{isFullscreen ? <Minimize2 className="w-4 h-4" /> : <Maximize2 className="w-4 h-4" />}
</Button>
<Button variant="outline" size="sm" className="h-8 w-8 p-0" onClick={toggleTheme}>
Expand Down Expand Up @@ -767,10 +680,11 @@ if (layout === "preview") setLayout("split")
<div
style={
layout === "split"
? isMobile
? { height: `${splitRatio}%` }
: { width: `${splitRatio}%` }
: { flex: 1 }
? {
width: isMobile ? "100%" : `${splitRatio}%`,
height: isMobile ? `${splitRatio}%` : "100%",
}
: { height: "100%", width: "100%" }
}
className="flex flex-col overflow-hidden shrink-0 border-b md:border-b-0 md:border-r border-gray-200 dark:border-gray-700"
>
Expand Down Expand Up @@ -818,10 +732,11 @@ if (layout === "preview") setLayout("split")
<div
style={
layout === "split"
? isMobile
? { height: `${100 - splitRatio}%` }
: { width: `${100 - splitRatio}%` }
: { flex: 1 }
? {
width: isMobile ? "100%" : `${100 - splitRatio}%`,
height: isMobile ? `${100 - splitRatio}%` : "100%",
}
: { height: "100%", width: "100%" }
}
className="flex flex-col overflow-hidden shrink-0"
>
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.