|
2 | 2 | import React from 'react'
|
3 | 3 | import uuid from 'nanoid'
|
4 | 4 | import * as attach from '@attach/attach-embeds'
|
5 |
| - |
6 |
| -// utilities |
7 |
| -import units, { basePath } from './units' |
8 | 5 | // endregion
|
9 | 6 |
|
10 | 7 | // region embed
|
11 |
| -const embed = name => { |
12 |
| - const path = units[name].path |
13 |
| - const webrtcAttributes = units[name].webrtc |
14 |
| - ? { |
15 |
| - allowusermedia: true, |
16 |
| - allowfullscreen: true, |
17 |
| - webkitallowfullscreen: true, |
18 |
| - mozallowfullscreen: true, |
19 |
| - allow: 'camera; microphone; autoplay; fullscreen', |
20 |
| - } |
21 |
| - : {} |
22 |
| - |
23 |
| - return class Embed extends React.Component { |
| 8 | +const embed = name => |
| 9 | + class Embed extends React.Component { |
24 | 10 | constructor(props) {
|
25 | 11 | super(props)
|
26 |
| - this.state = { visibility: 'hidden' } |
27 |
| - this.unitId = uuid() |
28 |
| - this.iframeLoaded = false |
29 |
| - this.iframeRef = React.createRef() |
30 |
| - this.onLoad = this.onLoad.bind(this) |
31 |
| - this.sendMessage = this.sendMessage.bind(this) |
| 12 | + this.embedId = props.embedId || uuid() |
32 | 13 | this.updateProperties = this.updateProperties.bind(this)
|
| 14 | + this.getMergedProperties = this.getMergedProperties.bind(this) |
| 15 | + } |
| 16 | + |
| 17 | + componentDidMount() { |
| 18 | + attach.render() |
33 | 19 | }
|
34 | 20 |
|
35 | 21 | componentDidUpdate(prevProps, prevState) {
|
36 |
| - if (prevProps.properties !== this.props.properties) { |
| 22 | + if (prevProps.properties !== this.props.properties || prevProps.item !== this.props.item) { |
37 | 23 | this.updateProperties()
|
38 | 24 | }
|
39 | 25 | }
|
40 | 26 |
|
41 |
| - sendMessage(type, data) { |
42 |
| - const message = { |
43 |
| - data, |
44 |
| - meta: { |
45 |
| - from: 'main', |
46 |
| - to: name, |
47 |
| - type, |
48 |
| - }, |
| 27 | + getMergedProperties() { |
| 28 | + const properties = { ...this.props.properties } |
| 29 | + if (this.props.item) { |
| 30 | + properties['attach:item'] = this.props.item |
49 | 31 | }
|
50 |
| - |
51 |
| - this.iframeRef.current.contentWindow.postMessage(JSON.stringify(message), basePath) |
| 32 | + return properties |
52 | 33 | }
|
53 | 34 |
|
54 | 35 | updateProperties() {
|
55 |
| - if (this.iframeLoaded) { |
56 |
| - this.sendMessage('UPDATE_PROPERTIES', { properties: this.props.properties }) |
57 |
| - } |
58 |
| - } |
59 |
| - |
60 |
| - onLoad() { |
61 |
| - this.setState({ visibility: 'visible' }) |
62 |
| - this.iframeLoaded = true |
63 |
| - this.sendMessage('START_EMBED', { |
64 |
| - unitId: this.unitId, |
65 |
| - instanceId: window.attachInstanceId, |
66 |
| - properties: this.props.properties, |
67 |
| - }) |
| 36 | + attach.setEmbedProperties(this.embedId, this.getMergedProperties()) |
68 | 37 | }
|
69 | 38 |
|
70 | 39 | render() {
|
71 |
| - const { className = '', style = {} } = this.props |
| 40 | + const { className = '', style = {}, properties = {}, item = '' } = this.props |
72 | 41 |
|
73 | 42 | return (
|
74 |
| - <iframe |
75 |
| - ref={this.iframeRef} |
76 |
| - data-unit-id={this.unitId} |
77 |
| - className={`attach-embed ${className}`} |
78 |
| - onLoad={this.onLoad} |
79 |
| - src={path} |
80 |
| - style={{ |
81 |
| - display: 'block', |
82 |
| - width: '100%', |
83 |
| - height: '100%', |
84 |
| - padding: 0, |
85 |
| - margin: 0, |
86 |
| - border: 'none', |
87 |
| - visibility: this.state.visibility, |
88 |
| - ...style, |
89 |
| - }} |
90 |
| - {...webrtcAttributes} |
| 43 | + <div |
| 44 | + data-embed-id={this.embedId} |
| 45 | + data-property-item={item} |
| 46 | + data-properties={JSON.stringify(properties)} |
| 47 | + className={`attach-${name} ${className}`} |
| 48 | + style={style} |
91 | 49 | />
|
92 | 50 | )
|
93 | 51 | }
|
94 | 52 | }
|
95 |
| -} |
| 53 | + |
96 | 54 | // endregion
|
97 | 55 |
|
98 | 56 | // region export
|
|
0 commit comments