Match up attributes to postMessage

This commit is contained in:
Michael Thomas 2022-01-15 04:21:40 +00:00
parent cb5abdbb9a
commit 22a0fad4dd
7 changed files with 46 additions and 25 deletions

View File

@ -4,7 +4,7 @@
import * as React from 'react'; import * as React from 'react';
import { Button, Modal, FocusableIframe } from '@wordpress/components'; import { Button, Modal, FocusableIframe } from '@wordpress/components';
import { baseUrl } from '../globals'; import { baseUrl } from '@/globals';
type DialogProps = { type DialogProps = {
onSelectForm: Function; onSelectForm: Function;
@ -34,6 +34,7 @@ class SelectDialog extends React.Component<DialogProps, DialogState> {
handlePostMessage = ( event: MessageEvent ) => { handlePostMessage = ( event: MessageEvent ) => {
console.log( 'PostMessage recieved:', event.data ); console.log( 'PostMessage recieved:', event.data );
this.props.onSelectForm( event.data ); this.props.onSelectForm( event.data );
this.setState({ isOpen: false });
} }
render() { render() {
@ -43,8 +44,16 @@ class SelectDialog extends React.Component<DialogProps, DialogState> {
Choose a form Choose a form
</Button> </Button>
{ this.state.isOpen && ( { this.state.isOpen && (
<Modal title="Cognito Forms" className="cognito-modal" onRequestClose={ () => this.setState( { isOpen: false } ) } shouldCloseOnClickOutside={ false }> <Modal
<FocusableIframe style={ { width: '500px', height: '500px', display: 'block' } } src={ `${ baseUrl }/integrations/cms` }></FocusableIframe> title="Cognito Forms"
className="cognito-modal"
onRequestClose={ () => this.setState( { isOpen: false } ) }
shouldCloseOnClickOutside={ false }
>
<FocusableIframe
style={ { width: '500px', height: '500px', display: 'block' } }
src={ `${ baseUrl }/integrations/cms` }
></FocusableIframe>
</Modal> </Modal>
) } ) }
</div> </div>

View File

@ -4,17 +4,19 @@ import { __ } from '@wordpress/i18n';
import { BlockEditProps } from '@wordpress/blocks'; import { BlockEditProps } from '@wordpress/blocks';
import { Placeholder, ExternalLink } from '@wordpress/components'; import { Placeholder, ExternalLink } from '@wordpress/components';
import CogIcon from './assets/cogicon'; import CogIcon from '@/assets/cogicon';
import SelectDialog from './components/select-dialog'; import SelectDialog from '@/components/select-dialog';
import { BlockAttributes, EmbedMode } from './types'; import { BlockAttributes, EmbedMode } from '@/types';
const Edit: React.FC<BlockEditProps<BlockAttributes>> = ( { attributes, setAttributes, className } ) => { const Edit: React.FC<BlockEditProps<BlockAttributes>> = ( { attributes, setAttributes, className } ) => {
const handleForm = ( form: string ) => { const handleForm = ( form: string ) => {
const formObj = JSON.parse( form ); const formObj = JSON.parse( form );
setAttributes( { setAttributes( {
formId: formObj.id, formId: formObj.formId,
seamlessEmbedCode: formObj.seamlessEmbedCode, seamlessEmbedCode: formObj.embedCodes.Seamless,
iframeEmbedCode: formObj.iframeEmbedCode, iframeEmbedCode: formObj.embedCodes.IFrame,
ampEmbedCode: formObj.embedCodes.Amp,
embedMode: EmbedMode.Seamless, embedMode: EmbedMode.Seamless,
} ); } );
}; };
@ -40,17 +42,16 @@ const Edit: React.FC<BlockEditProps<BlockAttributes>> = ( { attributes, setAttri
{ __( 'Learn more about embeds' ) } { __( 'Learn more about embeds' ) }
</ExternalLink> </ExternalLink>
</div> </div>
{/* <PlainText
onChange={ content => props.setAttributes({ url: content }) }
value={ props.attributes.url }
placeholder="Your form url"
className="cognito__form"
/> */}
</Placeholder> </Placeholder>
); );
} }
return ( <div dangerouslySetInnerHTML={ { __html: attributes.iframeEmbedCode } }></div> ); return (
<div
className="cog-wp-embed cog-wp-embed__preview"
dangerouslySetInnerHTML={ { __html: attributes.iframeEmbedCode } } // Must be Iframe embed code as others do not work in Gutenberg
></div>
);
} }
export default Edit; export default Edit;

View File

@ -19,7 +19,7 @@ registerBlockType<BlockAttributes> ( 'cognito-forms/cognito-embed', {
title: 'Cognito Forms', title: 'Cognito Forms',
icon: CogIcon, icon: CogIcon,
description: __( description: __(
'An example typescript block.', 'Easily build powerful forms.',
'cognito' 'cognito'
), ),
category: 'embed', category: 'embed',
@ -29,7 +29,7 @@ registerBlockType<BlockAttributes> ( 'cognito-forms/cognito-embed', {
}, },
attributes: { attributes: {
formId: { formId: {
type: 'number', type: 'string',
}, },
seamlessEmbedCode: { seamlessEmbedCode: {
type: 'string', type: 'string',

View File

@ -4,21 +4,24 @@ import { BlockSaveProps } from '@wordpress/blocks';
import { EmbedMode, BlockAttributes } from './types'; import { EmbedMode, BlockAttributes } from './types';
const Save: React.FC<BlockSaveProps<BlockAttributes>> = ( props: any ) => { const Save: React.FC<BlockSaveProps<BlockAttributes>> = ( { attributes: { formId, embedMode, seamlessEmbedCode, iframeEmbedCode } } ) => {
let embedCode = ''; let embedCode = '';
switch ( props.attributes.embedMode ) { switch ( embedMode ) {
case EmbedMode.Seamless: case EmbedMode.Seamless:
embedCode = props.attributes.seamlessEmbedCode; embedCode = seamlessEmbedCode;
break; break;
case EmbedMode.IFrame: case EmbedMode.IFrame:
embedCode = props.attributes.iframeEmbedCode; embedCode = iframeEmbedCode;
break; break;
} }
console.log(formId);
return ( return (
<div <div
className={ props.className } className="cog-wp-embed"
data-form={ formId }
dangerouslySetInnerHTML={ { __html: embedCode } } dangerouslySetInnerHTML={ { __html: embedCode } }
> >
</div> </div>

View File

@ -10,6 +10,11 @@
.cognito-modal > .components-modal__content { .cognito-modal > .components-modal__content {
padding: initial; padding: initial;
} }
.cognito-modal > .components-modal__content > .components-modal__header { .cognito-modal > .components-modal__content > .components-modal__header {
margin: initial; margin: initial;
} }
.cog-wp-embed__preview > iframe {
height: 500px;
}

View File

@ -4,7 +4,7 @@ export enum EmbedMode {
} }
export interface BlockAttributes { export interface BlockAttributes {
formId: number, formId: string,
iframeEmbedCode: string; iframeEmbedCode: string;
seamlessEmbedCode: string; seamlessEmbedCode: string;
ampEmbedCode: string; ampEmbedCode: string;

View File

@ -6,7 +6,10 @@
"module": "commonjs", "module": "commonjs",
"noImplicitAny": true, "noImplicitAny": true,
"allowJs": true, "allowJs": true,
"allowSyntheticDefaultImports": true "allowSyntheticDefaultImports": true,
"paths": {
"@/*": ["./src/*"]
}
}, },
"include": [ "include": [
"src" "src"