Skip to content

Commit 257fce3

Browse files
committed
add device change
1 parent 23dadd4 commit 257fce3

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

components/App.tsx

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
FaUndo,
1111
FaCopy,
1212
FaChevronLeft,
13+
FaLaptopCode,
14+
FaMobileAlt,
1315
} from "react-icons/fa";
1416

1517
import { AiFillCodepenCircle } from "react-icons/ai";
@@ -25,7 +27,7 @@ import toast from "react-hot-toast";
2527
import {UploadFileContext} from './contexts/UploadFileContext';
2628
import {SettingContext} from './contexts/SettingContext';
2729
import {HistoryContext} from './contexts/HistoryContext';
28-
import {EditorContext} from './contexts/EditorContext';
30+
import {EditorContext, deviceType} from './contexts/EditorContext';
2931
import NativePreview from './components/NativeMobile';
3032
import { TemplateContext } from './contexts/TemplateContext';
3133
import UpdateChatInput from './components/chatInput/Update';
@@ -78,7 +80,7 @@ function App() {
7880
const {settings, setSettings, initCreate, setInitCreate, initCreateText, setInitCreateText} = useContext(SettingContext);
7981

8082
const {history, addHistory, currentVersion, setCurrentVersion, resetHistory, updateHistoryCode} = useContext(HistoryContext);
81-
const { enableEdit, setEnableEdit } = useContext(EditorContext)
83+
const { enableEdit, setEnableEdit, device, setDevice } = useContext(EditorContext)
8284
const [tabValue, setTabValue] = useState<string>(settings.generatedCodeConfig == GeneratedCodeConfig.REACT_NATIVE ? 'native' : 'desktop')
8385
const [ template, setTemplate ] = useState<any>({});
8486
const [openDialog, setOpenDialog] = useState<boolean>(false);
@@ -520,6 +522,25 @@ ${error.stack}
520522
>
521523
<FaDownload />
522524
</span>
525+
<span
526+
className="hover:bg-slate-200 rounded-sm w-[36px] h-[36px] flex items-center justify-center border-black border-2"
527+
onClick={() => {
528+
if (device === deviceType.PC) {
529+
setDevice(deviceType.MOBILE)
530+
} else {
531+
setDevice(deviceType.PC)
532+
}
533+
}}
534+
>
535+
{
536+
device === deviceType.PC ? (
537+
<FaLaptopCode className="w-[20px]"/>
538+
) : (
539+
<FaMobileAlt className="h-[20px]"/>
540+
)
541+
}
542+
543+
</span>
523544
<span
524545
className={classNames(
525546
"hover:bg-slate-200 rounded-full border-black border-2 w-[36px] h-[36px] flex items-center justify-center",
@@ -583,11 +604,19 @@ ${error.stack}
583604

584605
<div
585606
className={
586-
classNames('h-full', {
607+
classNames('h-full flex justify-center', {
587608
'hidden': tabValue !== 'desktop'
588609
})
589610
}
590-
>
611+
>
612+
<div
613+
className={
614+
classNames("h-full", {
615+
"w-full": device === deviceType.PC,
616+
"w-[375px]": device === deviceType.MOBILE
617+
}
618+
)}
619+
>
591620
<PreviewBox
592621
code={generatedCode}
593622
appState={appState}
@@ -598,6 +627,8 @@ ${error.stack}
598627
history={history}
599628
fixBug={fixBug}
600629
/>
630+
</div>
631+
601632
{/* <Preview code={generatedCode} device="desktop" appState={appState} fixBug={fixBug}/> */}
602633
</div>
603634
<div

components/contexts/EditorContext.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
import { createContext, ReactNode, useState, useEffect, useRef, Dispatch, SetStateAction } from 'react';
22

3+
export enum deviceType {
4+
'PC' = 'pc',
5+
'MOBILE' = 'mobile',
6+
}
7+
38
interface editorContextType {
49
enableEdit: boolean;
5-
setEnableEdit: Dispatch<SetStateAction<boolean>>
10+
setEnableEdit: Dispatch<SetStateAction<boolean>>;
11+
device: deviceType;
12+
setDevice: Dispatch<SetStateAction<deviceType>>;
613
}
714

815
const initialValue = {
916
enableEdit: true,
1017
setEnableEdit: (value: SetStateAction<boolean>) => {},
18+
device: deviceType.PC,
19+
setDevice: (value: SetStateAction<deviceType>) => {},
20+
1121
}
1222

1323
export const EditorContext = createContext<editorContextType>(initialValue);
1424

1525
export default function EditorProvider({ children }: { children: ReactNode }) {
1626
const [enableEdit, setEnableEdit] = useState<boolean>(true);
17-
27+
const [device, setDevice] = useState<deviceType>(deviceType.PC);
1828
return (
1929
<EditorContext.Provider
2030
value={{
2131
enableEdit,
22-
setEnableEdit
32+
setEnableEdit,
33+
device,
34+
setDevice
2335
}}
2436
>
2537
{children}

engine/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import useThrottle from "../components/hooks/useThrottle";
66
import {setHtmlCodeUid} from '../components/compiler';
77
import html2canvas from "html2canvas";
88
import {HistoryContext} from '../components/contexts/HistoryContext';
9-
import {EditorContext} from '../components/contexts/EditorContext';
9+
import {EditorContext, deviceType} from '../components/contexts/EditorContext';
1010
import { cloneDeep } from 'lodash';
1111
import {
1212
FaBug
1313
} from "react-icons/fa";
1414
import filesTemplate from './apps/react-shadcnui/files-template';
1515
import { useSandpack, SandpackProvider } from "@codesandbox/sandpack-react";
16+
import { cn } from "../components/lib/utils"
17+
import classNames from "classnames";
1618

1719
// filesTemplate['/src/Preview.jsx'] = `
1820
// import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "components/ui/card";
@@ -109,7 +111,7 @@ export default function PreviewBox({ code, appState, sendMessageChange, history,
109111
// const { dispatch, listen } = useSandpack();
110112

111113

112-
const { enableEdit, setEnableEdit } = useContext(EditorContext);
114+
const { enableEdit, setEnableEdit, device } = useContext(EditorContext);
113115
const [filesObj, setFilesObj]= useState<any>(filesTemplate);
114116

115117

@@ -231,7 +233,9 @@ export default function PreviewBox({ code, appState, sendMessageChange, history,
231233
};
232234

233235
return (
234-
<div className="border-[4px] border-black rounded-[10px] shadow-lg w-full h-full">
236+
<div
237+
className="border-[4px] border-black rounded-[10px] shadow-lg h-full w-full"
238+
>
235239
{
236240
showDebug && (
237241
<span

0 commit comments

Comments
 (0)