Skip to content

Commit

Permalink
Fix/refresh iframe (#6787)
Browse files Browse the repository at this point in the history
Fix/refresh iframe
  • Loading branch information
ndelangen authored May 15, 2019
2 parents d79a030 + 3584591 commit 43e378f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
13 changes: 11 additions & 2 deletions lib/channel-postmessage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,29 @@ export class PostmsgTransport {

private handler: ChannelHandler;

private connected: boolean;

// eslint-disable-next-line @typescript-eslint/no-parameter-properties
constructor(private readonly config: Config) {
this.buffer = [];
this.handler = null;
window.addEventListener('message', this.handleEvent.bind(this), false);
document.addEventListener('DOMContentLoaded', () => this.flush());

// Check whether the config.page parameter has a valid value
if (config.page !== 'manager' && config.page !== 'preview') {
throw new Error(`postmsg-channel: "config.page" cannot be "${config.page}"`);
}
}

setHandler(handler: ChannelHandler): void {
this.handler = handler;
this.handler = (...args) => {
handler.apply(this, args);

if (!this.connected && this.getWindow()) {
this.flush();
this.connected = true;
}
};
}

/**
Expand Down
19 changes: 9 additions & 10 deletions lib/ui/src/components/preview/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import window from 'global';
import React, { Component } from 'react';
import PropTypes from 'prop-types';

// this component renders an iframe, which gets updates via post-messages
export class IFrame extends Component {
iframe = null;

Expand All @@ -12,20 +11,20 @@ export class IFrame extends Component {
}

shouldComponentUpdate(nextProps) {
const { scale, src } = this.props;
return scale !== nextProps.scale || src !== nextProps.src;
}

componentDidUpdate(prevProps) {
const { scale } = this.props;
if (scale !== prevProps.scale) {

if (scale !== nextProps.scale) {
this.setIframeBodyStyle({
width: `${scale * 100}%`,
height: `${scale * 100}%`,
transform: `scale(${1 / scale})`,
width: `${nextProps.scale * 100}%`,
height: `${nextProps.scale * 100}%`,
transform: `scale(${1 / nextProps.scale})`,
transformOrigin: 'top left',
});
}

// this component renders an iframe, which gets updates via post-messages
// never update this component, it will cause the iframe to refresh
return false;
}

setIframeBodyStyle(style) {
Expand Down

0 comments on commit 43e378f

Please sign in to comment.