Skip to content

Commit

Permalink
feat: pass provider prop to widget
Browse files Browse the repository at this point in the history
  • Loading branch information
gorgeousvlad committed Jul 19, 2023
1 parent 3fb0dd0 commit b5a8c45
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/editor/components/DevicePreview/DevicePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface DevicePreviewMobileProps extends PropsWithChildren {
}

const DevicePreviewMobile = ({device, active}: DevicePreviewMobileProps) => {
const {data} = useContext(EditorContext);
const initialData = useContext(EditorContext);
const containerRef = useRef<HTMLDivElement | null>(null);
const deviceIframeRef = useRef<DeviceIframe | null>(null);

Expand All @@ -34,7 +34,7 @@ const DevicePreviewMobile = ({device, active}: DevicePreviewMobileProps) => {

if (containerRef?.current) {
iframe = new DeviceIframe(containerRef?.current, {
initialData: data,
initialData,
className: b('frame', {device}),
});
deviceIframeRef.current = iframe;
Expand All @@ -54,10 +54,10 @@ const DevicePreviewMobile = ({device, active}: DevicePreviewMobileProps) => {
}, [active]);

useEffect(() => {
if (deviceIframeRef.current && data) {
deviceIframeRef.current.onDataUpdate(data);
if (deviceIframeRef.current && initialData) {
deviceIframeRef.current.onDataUpdate(initialData);
}
}, [data]);
}, [initialData]);

return <div className={b({active, device})} ref={containerRef} />;
};
Expand Down
10 changes: 7 additions & 3 deletions src/editor/containers/Editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {EditorProps, ViewModeItem} from '../../types';
import {addCustomDecorator, checkIsMobile, getBlockId} from '../../utils';
import {Form} from '../Form/Form';

export const Editor = ({children, customSchema, onChange, ...rest}: EditorProps) => {
export const Editor = ({children, customSchema, onChange, providerProps, ...rest}: EditorProps) => {
const {
content,
activeBlockIndex,
Expand Down Expand Up @@ -53,12 +53,16 @@ export const Editor = ({children, customSchema, onChange, ...rest}: EditorProps)

const context = useMemo(
() => ({
data: {
constructorProps: {
content,
custom: rest.custom,
},
providerProps: {
...providerProps,
isMobile: checkIsMobile(viewMode),
},
}),
[content, rest.custom],
[content, providerProps, rest.custom, viewMode],
);

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/editor/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import React from 'react';
import {PageConstructorProps, PageConstructorProviderProps} from '../containers/PageConstructor';

export interface EditorContextType {
data?: PageConstructorProps;
provider?: PageConstructorProviderProps;
constructorProps?: PageConstructorProps;
providerProps?: PageConstructorProviderProps;
}

export const EditorContext = React.createContext<Partial<EditorContextType>>({});
3 changes: 2 additions & 1 deletion src/editor/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PageConstructorProps} from '../../containers/PageConstructor';
import {PageConstructorProps, PageConstructorProviderProps} from '../../containers/PageConstructor';
import {BlockDecorationProps, PageContent} from '../../models';
import {SchemaCustomConfig} from '../../schema';
import {EditBlockActions} from '../components/EditBlock/EditBlock';
Expand All @@ -13,6 +13,7 @@ export interface EditorProps
extends Required<Pick<PageConstructorProps, 'content'>>,
Partial<Omit<PageConstructorProps, 'content'>> {
children: (props: EditorOutgoingProps) => React.ReactNode;
providerProps?: PageConstructorProviderProps;
onChange?: (data: PageContent) => void;
customSchema?: SchemaCustomConfig;
}
Expand Down
10 changes: 6 additions & 4 deletions src/editor/widget/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
//@ts-ignore
import bundle from 'widget';

import {PageConstructorProps} from '../../containers/PageConstructor';
import {EditorContextType} from '../context';

import {DeviceFrameMessageType} from './constants';

interface DeviceIframeParams {
initialData?: PageConstructorProps;
initialData?: EditorContextType;
className?: string;
parentCSS?: string;
}

type InitialData = EditorContextType;

export class DeviceIframe {
iframeElement?: HTMLIFrameElement;
private initialData?: PageConstructorProps;
private initialData?: InitialData;

constructor(parentElement: HTMLDivElement, {className = '', initialData}: DeviceIframeParams) {
const iframe = document.createElement('iframe');
Expand All @@ -37,7 +39,7 @@ export class DeviceIframe {
}
}

onDataUpdate(data: PageConstructorProps) {
onDataUpdate(data: InitialData) {
this.iframeElement?.contentWindow?.postMessage(
{
type: DeviceFrameMessageType.Update,
Expand Down
17 changes: 12 additions & 5 deletions src/widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import ReactDOM from 'react-dom';

import {PageConstructor} from '../containers/PageConstructor';
import {PageConstructor, PageConstructorProvider} from '../containers/PageConstructor';
import {DeviceFrameMessageType} from '../editor/widget/constants';

import './styles.scss';
Expand All @@ -12,10 +12,17 @@ if (window.self !== window.top && window.__isEditorDeviceFrame) {
document.body.appendChild(root);

window.addEventListener('message', function (event) {
const {type, data} = event.data;

if (type === DeviceFrameMessageType.Update) {
ReactDOM.render(<PageConstructor {...data} />, root);
if (event.data?.type === DeviceFrameMessageType.Update) {
const {
data: {constructorProps, providerProps},
} = event.data;

ReactDOM.render(
<PageConstructorProvider {...providerProps}>
<PageConstructor {...constructorProps} />
</PageConstructorProvider>,
root,
);
}
});

Expand Down

0 comments on commit b5a8c45

Please sign in to comment.