-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -7,3 +7,4 @@ node_modules | |
/packages | ||
/styleguide | ||
/coverage | ||
*.sw* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,120 @@ | ||
/* eslint react/prop-types: 0 */ | ||
import React from 'react' | ||
|
||
export class FrameComponent extends React.PureComponent { | ||
static get actions () { | ||
return new Map([['getState', 'get_state'], ['updateState', 'update_state'], ['notify', 'notify']]) | ||
} | ||
|
||
static getAction (name) { | ||
const keys = [...FrameComponent.actions.keys()] | ||
const names = [...FrameComponent.actions.values()] | ||
|
||
const actionIndex = keys.findIndex(a => a === name) | ||
if (actionIndex === -1) throw new Error('Can not find action') | ||
|
||
return names[actionIndex] | ||
} | ||
|
||
static get type () { | ||
return 'about:iframe#taskdigests' | ||
} | ||
|
||
static isEnabled (env) { | ||
return env.length > 0 | ||
} | ||
|
||
static isComponent (data) { | ||
return data.url && data.url.startsWith(FrameComponent.type) | ||
} | ||
|
||
static data ({ | ||
data = '""', page = 1, title, url, | ||
}) { | ||
return { | ||
data, | ||
page, | ||
title, | ||
url: new URL(`${FrameComponent.type}/${url}`).href, | ||
} | ||
} | ||
|
||
constructor (props) { | ||
super(props) | ||
this.iframeR = React.createRef() | ||
} | ||
|
||
componentDidMount () { | ||
this.iframeWindow.addEventListener('message', this.handleMessage) | ||
} | ||
|
||
componentWillUnmount () { | ||
this.iframeWindow.removeEventListener('message', this.handleMessage) | ||
} | ||
|
||
get iframeWindow () { | ||
return this.iframeR.current && this.iframeR.current.ownerDocument.defaultView | ||
} | ||
|
||
handleMessage = (event) => { | ||
const { handleMessage } = this.props | ||
if (event.source !== this.iframeR.current.contentWindow) return | ||
// bypass iframe-only events | ||
|
||
const { payload, type } = event.data | ||
|
||
handleMessage(type, payload) | ||
} | ||
|
||
postMessage = (message) => { | ||
const el = this.iframeR.current | ||
|
||
if (!message) throw new Error('Could not post message') | ||
|
||
el && el.contentWindow.postMessage(message, '*') | ||
} | ||
|
||
postGetState = (message) => { | ||
this.postMessage({ | ||
type: FrameComponent.actions.get('getState'), | ||
payload: message, | ||
}) | ||
} | ||
|
||
postUpdateState = (message) => { | ||
this.postMessage({ | ||
type: FrameComponent.actions.get('updateState'), | ||
payload: message, | ||
}) | ||
} | ||
|
||
postNotify = (message) => { | ||
this.postMessage({ | ||
type: FrameComponent.actions.get('notify'), | ||
payload: message, | ||
}) | ||
} | ||
|
||
render () { | ||
const { | ||
scope, | ||
title, | ||
url, | ||
} = this.props | ||
|
||
return ( | ||
<iframe | ||
style={{ | ||
border: 0, | ||
display: 'flex', | ||
flex: 1, | ||
height: '100%', | ||
width: '100%', | ||
}} | ||
ref={this.iframeR} | ||
src={`${url}?scope=${scope}`} | ||
title={title} | ||
/> | ||
) | ||
} | ||
} |
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 @@ | ||
### Frame |