Skip to content

Commit 2916eb0

Browse files
committed
Initial commit.
1 parent 361630c commit 2916eb0

File tree

8 files changed

+15605
-59
lines changed

8 files changed

+15605
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# production
1212
/build
13+
/dist
1314

1415
# misc
1516
.DS_Store

package-lock.json

Lines changed: 15461 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
{
22
"name": "react-retool",
33
"version": "0.1.0",
4-
"private": true,
4+
"private": false,
5+
"babel": {
6+
"presets": [
7+
"@babel/preset-react"
8+
],
9+
"plugins": [
10+
[
11+
"@babel/plugin-proposal-class-properties"
12+
]
13+
]
14+
},
515
"dependencies": {
616
"@testing-library/jest-dom": "^5.11.4",
717
"@testing-library/react": "^11.1.0",
@@ -15,7 +25,8 @@
1525
"start": "react-scripts start",
1626
"build": "react-scripts build",
1727
"test": "react-scripts test",
18-
"eject": "react-scripts eject"
28+
"eject": "react-scripts eject",
29+
"publish:npm": "rm -rf dist && mkdir dist && babel src/components -d dist --copy-files"
1930
},
2031
"eslintConfig": {
2132
"extends": [
@@ -34,5 +45,10 @@
3445
"last 1 firefox version",
3546
"last 1 safari version"
3647
]
48+
},
49+
"devDependencies": {
50+
"@babel/cli": "^7.13.16",
51+
"@babel/plugin-proposal-class-properties": "^7.13.0",
52+
"@babel/preset-react": "^7.13.13"
3753
}
3854
}

src/App.css

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +0,0 @@
1-
.App {
2-
text-align: center;
3-
}
4-
5-
.App-logo {
6-
height: 40vmin;
7-
pointer-events: none;
8-
}
9-
10-
@media (prefers-reduced-motion: no-preference) {
11-
.App-logo {
12-
animation: App-logo-spin infinite 20s linear;
13-
}
14-
}
15-
16-
.App-header {
17-
background-color: #282c34;
18-
min-height: 100vh;
19-
display: flex;
20-
flex-direction: column;
21-
align-items: center;
22-
justify-content: center;
23-
font-size: calc(10px + 2vmin);
24-
color: white;
25-
}
26-
27-
.App-link {
28-
color: #61dafb;
29-
}
30-
31-
@keyframes App-logo-spin {
32-
from {
33-
transform: rotate(0deg);
34-
}
35-
to {
36-
transform: rotate(360deg);
37-
}
38-
}

src/App.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
import logo from './logo.svg';
22
import './App.css';
3+
import Retool from './components/Retool'
34

45
function App() {
56
return (
6-
<div className="App">
7-
<header className="App-header">
8-
<img src={logo} className="App-logo" alt="logo" />
9-
<p>
10-
Edit <code>src/App.js</code> and save to reload.
11-
</p>
12-
<a
13-
className="App-link"
14-
href="https://reactjs.org"
15-
target="_blank"
16-
rel="noopener noreferrer"
17-
>
18-
Learn React
19-
</a>
20-
</header>
21-
</div>
7+
<Retool url="https://retoolin.tryretool.com/embedded/public/f7607e1f-670a-4ebf-9a09-be54cf17181e"/>
228
);
239
}
2410

src/components/Retool.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import React from 'react';
2+
import './retool.css';
3+
4+
class Retool extends React.Component {
5+
constructor(props) {
6+
super(props);
7+
this.state = {
8+
url: props.url,
9+
elementWatchers: {}
10+
}
11+
}
12+
13+
componentDidMount(){
14+
this.startListening()
15+
this.startWatchers()
16+
}
17+
18+
startListening = () => {
19+
window.addEventListener('message', (e) => this.handle(e) );
20+
}
21+
22+
startWatchers = () => {
23+
var watcherKeys = Object.keys(this.state.elementWatchers)
24+
25+
for (var i = 0; i < watcherKeys.length; i++) {
26+
var key = watcherKeys[i]
27+
var watcher = this.state.elementWatchers[key]
28+
var selector = watcher.selector
29+
var node = document.querySelector(selector)
30+
var value = node?.textContent
31+
if (value !== watcher.prevValue) {
32+
watcher.prevValue = value
33+
watcher.iframe.contentWindow.postMessage(
34+
{
35+
type: 'PARENT_WINDOW_RESULT',
36+
result: value,
37+
id: watcher.queryId,
38+
pageName: watcher.pageName,
39+
},
40+
'*',
41+
)
42+
}
43+
}
44+
45+
setTimeout(this.startWatchers, 100)
46+
}
47+
48+
createOrReplaceWatcher = (selector, pageName, queryId) => {
49+
var watcherId = pageName + '-' + queryId
50+
var watchers = {...this.state.elementWatchers}
51+
52+
watchers[watcherId] = {
53+
iframe: this.iframe,
54+
selector: selector,
55+
pageName: pageName,
56+
queryId: queryId,
57+
prevValue: null,
58+
}
59+
60+
this.setState({elementWatchers: watchers})
61+
}
62+
63+
handle = (event) => {
64+
if (!this.iframe.contentWindow) return
65+
66+
var node;
67+
68+
if (event.data.type === 'PARENT_WINDOW_QUERY') {
69+
node = document.querySelector(event.data.selector)
70+
this.createOrReplaceWatcher(event.data.selector, event.data.pageName, event.data.id)
71+
72+
this.iframe.contentWindow.postMessage(
73+
{
74+
type: 'PARENT_WINDOW_RESULT',
75+
result: node?.textContent,
76+
id: event.data.id,
77+
pageName: event.data.pageName,
78+
},
79+
'*',
80+
)
81+
}
82+
83+
if (event.data.type === 'PARENT_WINDOW_PREVIEW_QUERY') {
84+
node = document.querySelector(event.data.selector)
85+
this.iframe.contentWindow.postMessage(
86+
{
87+
type: 'PARENT_WINDOW_PREVIEW_RESULT',
88+
result: node?.textContent,
89+
id: event.data.id,
90+
},
91+
'*',
92+
)
93+
}
94+
95+
}
96+
97+
render() {
98+
return (
99+
<iframe
100+
frameBorder="none"
101+
src={this.state.url}
102+
ref = {e => {
103+
this.iframe = e
104+
}}
105+
>
106+
</iframe>
107+
);
108+
}
109+
}
110+
111+
export default Retool;
112+

src/components/retool.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
iframe {
2+
width: 100%;
3+
height: 100%
4+
}

src/index.css

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ body {
55
sans-serif;
66
-webkit-font-smoothing: antialiased;
77
-moz-osx-font-smoothing: grayscale;
8+
height:100%
89
}
910

10-
code {
11-
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12-
monospace;
11+
html {
12+
height:100%
1313
}
14+
15+
#root {
16+
height: 100%;
17+
}

0 commit comments

Comments
 (0)