|
1 | 1 | import React from "react" |
2 | 2 |
|
3 | 3 | class Retool extends React.Component { |
4 | | - constructor(props) { |
5 | | - super(props) |
| 4 | + constructor(props) { |
| 5 | + super(props) |
6 | 6 |
|
7 | | - if (!this.props.url) |
8 | | - throw new Error("Please pass a url into the Retool component.") |
| 7 | + if (!this.props.url) |
| 8 | + throw new Error("Please pass a url into the Retool component.") |
9 | 9 |
|
10 | | - this.state = { |
11 | | - url: props.url, |
12 | | - elementWatchers: {}, |
13 | | - parentData: this.props.data || {}, |
14 | | - } |
| 10 | + this.state = { |
| 11 | + url: props.url, |
| 12 | + elementWatchers: {}, |
| 13 | + parentData: this.props.data || {}, |
15 | 14 | } |
| 15 | + } |
16 | 16 |
|
17 | | - componentDidMount() { |
18 | | - this.startListening() |
19 | | - this.startWatchers() |
20 | | - } |
| 17 | + componentDidMount() { |
| 18 | + this.startListening() |
| 19 | + this.startWatchers() |
| 20 | + } |
21 | 21 |
|
22 | | - startListening = () => { |
23 | | - if (this.iframe) { |
24 | | - window.addEventListener("message", (e) => this.handle(e)) |
25 | | - } |
| 22 | + startListening = () => { |
| 23 | + if (this.iframe) { |
| 24 | + window.addEventListener("message", (e) => this.handle(e)) |
26 | 25 | } |
27 | | - |
28 | | - startWatchers = () => { |
29 | | - var watcherKeys = Object.keys(this.state.elementWatchers) |
30 | | - |
31 | | - for (var i = 0; i < watcherKeys.length; i++) { |
32 | | - var key = watcherKeys[i] |
33 | | - var watcher = this.state.elementWatchers[key] |
34 | | - var selector = watcher.selector |
35 | | - const value = this.dataFromSelector(selector) |
36 | | - if (value !== watcher.prevValue) { |
37 | | - watcher.prevValue = value |
38 | | - watcher.iframe.contentWindow.postMessage( |
39 | | - { |
40 | | - type: "PARENT_WINDOW_RESULT", |
41 | | - result: value, |
42 | | - id: watcher.queryId, |
43 | | - pageName: watcher.pageName, |
44 | | - }, |
45 | | - "*" |
46 | | - ) |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - setTimeout(this.startWatchers, 100) |
| 26 | + } |
| 27 | + |
| 28 | + startWatchers = () => { |
| 29 | + var watcherKeys = Object.keys(this.state.elementWatchers) |
| 30 | + |
| 31 | + for (var i = 0; i < watcherKeys.length; i++) { |
| 32 | + var key = watcherKeys[i] |
| 33 | + var watcher = this.state.elementWatchers[key] |
| 34 | + var selector = watcher.selector |
| 35 | + const value = this.dataFromSelector(selector) |
| 36 | + if (value !== watcher.prevValue) { |
| 37 | + watcher.prevValue = value |
| 38 | + watcher.iframe.contentWindow.postMessage( |
| 39 | + { |
| 40 | + type: "PARENT_WINDOW_RESULT", |
| 41 | + result: value, |
| 42 | + id: watcher.queryId, |
| 43 | + pageName: watcher.pageName, |
| 44 | + }, |
| 45 | + "*" |
| 46 | + ) |
| 47 | + } |
51 | 48 | } |
52 | 49 |
|
53 | | - createOrReplaceWatcher = (selector, pageName, queryId) => { |
54 | | - var watcherId = pageName + "-" + queryId |
55 | | - var watchers = { ...this.state.elementWatchers } |
| 50 | + setTimeout(this.startWatchers, 100) |
| 51 | + } |
56 | 52 |
|
57 | | - watchers[watcherId] = { |
58 | | - iframe: this.iframe, |
59 | | - selector: selector, |
60 | | - pageName: pageName, |
61 | | - queryId: queryId, |
62 | | - prevValue: null, |
63 | | - } |
| 53 | + createOrReplaceWatcher = (selector, pageName, queryId) => { |
| 54 | + var watcherId = pageName + "-" + queryId |
| 55 | + var watchers = { ...this.state.elementWatchers } |
64 | 56 |
|
65 | | - this.setState({ elementWatchers: watchers }) |
| 57 | + watchers[watcherId] = { |
| 58 | + iframe: this.iframe, |
| 59 | + selector: selector, |
| 60 | + pageName: pageName, |
| 61 | + queryId: queryId, |
| 62 | + prevValue: null, |
66 | 63 | } |
67 | 64 |
|
68 | | - dataFromSelector = (selector) => { |
69 | | - // Two places the app might be asking for data: |
70 | | - // 1. The textContent of an HTML element. |
71 | | - // 2. From data passed into this component |
72 | | - const matchingInjectedData = this.state.parentData[selector] |
73 | | - const nodeData = document.querySelector(selector)?.textContent |
74 | | - return matchingInjectedData || nodeData || null |
| 65 | + this.setState({ elementWatchers: watchers }) |
| 66 | + } |
| 67 | + |
| 68 | + dataFromSelector = (selector) => { |
| 69 | + // Two places the app might be asking for data: |
| 70 | + // 1. The textContent of an HTML element. |
| 71 | + // 2. From data passed into this component |
| 72 | + const matchingInjectedData = this.state.parentData[selector] |
| 73 | + const nodeData = document.querySelector(selector)?.textContent |
| 74 | + return matchingInjectedData || nodeData || null |
| 75 | + } |
| 76 | + |
| 77 | + postMessageForSelector = (messageType, eventData) => { |
| 78 | + const maybeData = this.dataFromSelector(eventData.selector) |
| 79 | + |
| 80 | + if (maybeData) { |
| 81 | + this.iframe.contentWindow.postMessage( |
| 82 | + { |
| 83 | + type: messageType, |
| 84 | + result: maybeData, |
| 85 | + id: eventData.id, |
| 86 | + pageName: eventData.pageName, |
| 87 | + }, |
| 88 | + "*" |
| 89 | + ) |
| 90 | + } else { |
| 91 | + console.log( |
| 92 | + `Not sending data back to Retool, nothing found for selector: ${eventData.selector}` |
| 93 | + ) |
75 | 94 | } |
76 | | - |
77 | | - postMessageForSelector = (messageType, eventData) => { |
78 | | - const maybeData = this.dataFromSelector(eventData.selector) |
79 | | - |
80 | | - if (maybeData) { |
81 | | - this.iframe.contentWindow.postMessage( |
82 | | - { |
83 | | - type: messageType, |
84 | | - result: maybeData, |
85 | | - id: eventData.id, |
86 | | - pageName: eventData.pageName, |
87 | | - }, |
88 | | - "*" |
89 | | - ) |
90 | | - } else { |
91 | | - console.log( |
92 | | - `Not sending data back to Retool, nothing found for selector: ${eventData.selector}` |
93 | | - ) |
94 | | - } |
| 95 | + } |
| 96 | + |
| 97 | + handle = (event) => { |
| 98 | + if (!this.iframe.contentWindow) return |
| 99 | + if (event.data.type === "PARENT_WINDOW_QUERY") { |
| 100 | + this.createOrReplaceWatcher( |
| 101 | + event.data.selector, |
| 102 | + event.data.pageName, |
| 103 | + event.data.id |
| 104 | + ) |
| 105 | + this.postMessageForSelector("PARENT_WINDOW_RESULT", event.data) |
95 | 106 | } |
96 | 107 |
|
97 | | - handle = (event) => { |
98 | | - if (!this.iframe.contentWindow) return |
99 | | - if (event.data.type === "PARENT_WINDOW_QUERY") { |
100 | | - this.createOrReplaceWatcher( |
101 | | - event.data.selector, |
102 | | - event.data.pageName, |
103 | | - event.data.id |
104 | | - ) |
105 | | - this.postMessageForSelector("PARENT_WINDOW_RESULT", event.data) |
106 | | - } |
107 | | - |
108 | | - if (event.data.type === "PARENT_WINDOW_PREVIEW_QUERY") { |
109 | | - this.postMessageForSelector( |
110 | | - "PARENT_WINDOW_PREVIEW_RESULT", |
111 | | - event.data |
112 | | - ) |
113 | | - } |
114 | | - } |
115 | | - |
116 | | - render() { |
117 | | - return ( |
118 | | - <iframe |
119 | | - height="100%" |
120 | | - width="100%" |
121 | | - frameBorder="none" |
122 | | - src={this.state.url} |
123 | | - ref={(e) => { |
124 | | - this.iframe = e |
125 | | - }} |
126 | | - title="retool" |
127 | | - ></iframe> |
128 | | - ) |
| 108 | + if (event.data.type === "PARENT_WINDOW_PREVIEW_QUERY") { |
| 109 | + this.postMessageForSelector("PARENT_WINDOW_PREVIEW_RESULT", event.data) |
129 | 110 | } |
| 111 | + } |
| 112 | + |
| 113 | + render() { |
| 114 | + return ( |
| 115 | + <iframe |
| 116 | + height="100%" |
| 117 | + width="100%" |
| 118 | + frameBorder="none" |
| 119 | + src={this.state.url} |
| 120 | + ref={(e) => { |
| 121 | + this.iframe = e |
| 122 | + }} |
| 123 | + title="retool" |
| 124 | + ></iframe> |
| 125 | + ) |
| 126 | + } |
130 | 127 | } |
131 | 128 |
|
132 | 129 | export default Retool |
0 commit comments