-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4609ffc
commit 4b24ce9
Showing
4 changed files
with
79 additions
and
71 deletions.
There are no files selected for viewing
82 changes: 13 additions & 69 deletions
82
src/editor/components/DeviceEmulation/DeviceEmulation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,23 @@ | ||
import React, {Fragment, PropsWithChildren, useContext, useEffect, useRef} from 'react'; | ||
import React, {Fragment, PropsWithChildren} from 'react'; | ||
|
||
import {block} from '../../../utils'; | ||
import {EditorContext} from '../../context'; | ||
import {ViewModeItem} from '../../types'; | ||
import {DeviceIframe} from '../../widget'; | ||
|
||
import './DeviceEmulation.scss'; | ||
|
||
const b = block('device-emulation'); | ||
import DeviceEmulationMobile from './DeviceEmulationMobile/DeviceEmulationMobile'; | ||
import {isMobileDevice, mobileDevices} from './utils'; | ||
|
||
export interface DeviceEmulationProps extends PropsWithChildren { | ||
mode: ViewModeItem; | ||
} | ||
|
||
type MobileDevice = ViewModeItem.Mobile | ViewModeItem.Tablet; | ||
|
||
const mobileDevices = [ViewModeItem.Tablet, ViewModeItem.Mobile] as const; | ||
const isMobileDevice = (mode: ViewModeItem): mode is MobileDevice => | ||
mobileDevices.includes(mode as MobileDevice); | ||
|
||
interface DeviceEmulationMobileProps extends PropsWithChildren { | ||
device: MobileDevice; | ||
active: boolean; | ||
} | ||
|
||
const DeviceEmulationMobile = ({device, active}: DeviceEmulationMobileProps) => { | ||
const {deviceEmulationSettings, ...initialData} = useContext(EditorContext); | ||
const containerRef = useRef<HTMLDivElement | null>(null); | ||
const deviceIframeRef = useRef<DeviceIframe | null>(null); | ||
|
||
useEffect(() => { | ||
let iframe: DeviceIframe; | ||
|
||
if (containerRef?.current) { | ||
iframe = new DeviceIframe(containerRef?.current, { | ||
initialData, | ||
className: b('frame', {device}), | ||
settings: deviceEmulationSettings, | ||
}); | ||
deviceIframeRef.current = iframe; | ||
} | ||
|
||
return () => { | ||
iframe?.destroy(); | ||
}; | ||
// render iframe only once, then update it's data with postMessage | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [device]); | ||
|
||
useEffect(() => { | ||
if (deviceIframeRef.current) { | ||
deviceIframeRef.current.onActivenessUpdate(active); | ||
} | ||
}, [active]); | ||
|
||
useEffect(() => { | ||
if (deviceIframeRef.current && initialData) { | ||
deviceIframeRef.current.onDataUpdate(initialData); | ||
} | ||
}, [initialData]); | ||
|
||
return <div className={b({active, device})} ref={containerRef} />; | ||
}; | ||
|
||
const DeviceEmulation = ({children, mode}: DeviceEmulationProps) => { | ||
return ( | ||
<Fragment> | ||
{!isMobileDevice(mode) && children} | ||
{mobileDevices.map((device) => ( | ||
<DeviceEmulationMobile key={device} device={device} active={mode === device}> | ||
{children} | ||
</DeviceEmulationMobile> | ||
))} | ||
</Fragment> | ||
); | ||
}; | ||
const DeviceEmulation = ({children, mode}: DeviceEmulationProps) => ( | ||
<Fragment> | ||
{!isMobileDevice(mode) && children} | ||
{mobileDevices.map((device) => ( | ||
<DeviceEmulationMobile key={device} device={device} active={mode === device}> | ||
{children} | ||
</DeviceEmulationMobile> | ||
))} | ||
</Fragment> | ||
); | ||
|
||
export default DeviceEmulation; |
4 changes: 2 additions & 2 deletions
4
...ents/DeviceEmulation/DeviceEmulation.scss → ...mulationMobile/DeviceEmulationMobile.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/editor/components/DeviceEmulation/DeviceEmulationMobile/DeviceEmulationMobile.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, {PropsWithChildren, useContext, useEffect, useRef} from 'react'; | ||
|
||
import {block} from '../../../../utils'; | ||
import {EditorContext} from '../../../context'; | ||
import {DeviceIframe} from '../../../widget'; | ||
import {MobileDevice} from '../utils'; | ||
|
||
import './DeviceEmulationMobile.scss'; | ||
|
||
const b = block('device-emulation-mobile'); | ||
|
||
interface DeviceEmulationMobileProps extends PropsWithChildren { | ||
device: MobileDevice; | ||
active: boolean; | ||
} | ||
|
||
const DeviceEmulationMobile = ({device, active}: DeviceEmulationMobileProps) => { | ||
const {deviceEmulationSettings, ...initialData} = useContext(EditorContext); | ||
const containerRef = useRef<HTMLDivElement | null>(null); | ||
const deviceIframeRef = useRef<DeviceIframe | null>(null); | ||
|
||
useEffect(() => { | ||
let iframe: DeviceIframe; | ||
|
||
if (containerRef?.current) { | ||
iframe = new DeviceIframe(containerRef?.current, { | ||
initialData, | ||
className: b('frame', {device}), | ||
settings: deviceEmulationSettings, | ||
}); | ||
deviceIframeRef.current = iframe; | ||
} | ||
|
||
return () => { | ||
iframe?.destroy(); | ||
}; | ||
// render iframe only once, then update it's data with postMessage | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [device]); | ||
|
||
useEffect(() => { | ||
if (deviceIframeRef.current) { | ||
deviceIframeRef.current.onActivenessUpdate(active); | ||
} | ||
}, [active]); | ||
|
||
useEffect(() => { | ||
if (deviceIframeRef.current && initialData) { | ||
deviceIframeRef.current.onDataUpdate(initialData); | ||
} | ||
}, [initialData]); | ||
|
||
return <div className={b({active, device})} ref={containerRef} />; | ||
}; | ||
|
||
export default DeviceEmulationMobile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {ViewModeItem} from '../../types'; | ||
|
||
export type MobileDevice = ViewModeItem.Mobile | ViewModeItem.Tablet; | ||
|
||
export const mobileDevices = [ViewModeItem.Tablet, ViewModeItem.Mobile] as const; | ||
|
||
export const isMobileDevice = (mode: ViewModeItem): mode is MobileDevice => | ||
[ViewModeItem.Tablet, ViewModeItem.Mobile].includes(mode as MobileDevice); |