forked from wandb/openui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
343 lines (334 loc) · 10.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<html>
<head>
<script src="/openui/tailwindcss.min.js"></script>
<script type="text/javascript">
window.tailwind.config.darkMode = ['class', '.darkMode']
</script>
<style type="text/css">
.selected {
position: absolute;
z-index: 49;
border: 2px dashed orange;
border-radius: 3px;
}
.inspector-element {
position: absolute;
z-index: 100;
pointer-events: none;
border: 1px dashed rgb(139 92 246);
box-shadow: 0 0 3px rgb(139 92 246);
animation: pulseGlow 1s infinite ease-in-out;
border-radius: 2px;
transition: all 250ms ease-in-out;
background-color: rgba(250, 250, 250, 0.1);
}
#wrapper {
cursor: pointer;
padding-top: 20px;
}
@keyframes pulseGlow {
0% {
box-shadow: 0 0 3px rgb(139 92 246);
}
50% {
box-shadow: 0 0 8px rgb(139 92 200);
}
100% {
box-shadow: 0 0 3px rgb(139 92 246);
}
}
</style>
</head>
<body
class="no-select items-top flex h-full w-full justify-center bg-zinc-300 dark:bg-zinc-900"
>
<div class="no-select relative w-[80vw]" id="wrapper">${body}</div>
<div
id="prompt"
class="no-select absolute z-50 origin-top scale-0 transform rounded-md border border-zinc-200 bg-zinc-100 text-zinc-700 shadow-lg transition-all duration-300 dark:border-zinc-700 dark:bg-zinc-800 dark:text-zinc-300"
>
<div class="p-4 text-xs">
<h3 class="text-base font-semibold">How do you want this to change?</h3>
<input
type="text"
class="mt-2 w-[300px] rounded-md border border-zinc-200 px-4 py-2 text-zinc-600 focus:outline-none focus:ring-2 focus:ring-blue-500 dark:focus:ring-blue-300"
/>
<button class="ml-2 mt-4 rounded-md bg-blue-500 p-2 text-white">
Submit
</button>
</div>
</div>
</body>
<script src="/openui/extra.min.js"></script>
<script type="text/javascript">
// Setup the room
window.theRoom.configure({
blockRedirection: false,
createInspector: true,
excludes: ['.no-select']
})
function clearInspector() {
const inspector = document.querySelector('.inspector-element')
if (inspector) {
inspector.style.top = '50%'
inspector.style.left = '50%'
inspector.style.width = ''
inspector.style.height = ''
inspector.style.border = 'none'
inspector.style.boxShadow = 'none'
}
}
let inspectorEnabled = false
// State TODO: maybe hydrate this from the parent
let commentIdx = 0
let selectedElements = []
// Reset the inspector every 5 seconds
let interval = null
// TODO: support more
let colors = ['blue', 'green', 'orange', 'yellow', 'red', 'purple']
let moved = false
document.addEventListener('mousemove', () => (moved = true))
document
.getElementById('wrapper')
.addEventListener('mouseleave', clearInspector)
function reset() {
interval = setInterval(() => {
if (!moved) {
clearInspector()
} else {
moved = false
}
}, 3000)
}
reset()
// TODO: might not need this if we just refactor the click handler
function reinitInspector(target, inspector) {
var pos = target.getBoundingClientRect()
var scrollTop = window.scrollY || document.documentElement.scrollTop
var scrollLeft = window.scrollX || document.documentElement.scrollLeft
var width = pos.width + 8
var height = pos.height + 8
var top = Math.max(-4, pos.top + scrollTop - 4)
var left = Math.max(-4, pos.left + scrollLeft - 4)
inspector.style.top = top + 'px'
inspector.style.left = left + 'px'
inspector.style.width = width + 'px'
inspector.style.height = height + 'px'
}
// Reset state on escape, submit on enter
document.addEventListener('keydown', e => {
if (e.key === 'Escape') {
clearInspector()
let p = document.getElementById('prompt')
if (p.classList.contains('scale-100')) {
p.querySelector('input').value = ''
let idx = p.dataset.commentIdx
document
.querySelectorAll('.selected-' + idx + ', .fix-legend-' + idx)
.forEach(selected => {
selected.parentNode.removeChild(selected)
})
p.classList.remove('scale-100')
if (inspectorEnabled) {
window.theRoom.start()
}
}
} else if (e.key === 'Enter') {
let sub = document.getElementById('prompt').querySelector('button')
sub.click()
}
})
window.addEventListener('resize', () => {
document.querySelectorAll('.selected').forEach(selected => {
let selIdx = parseInt(selected.dataset.commentIdx)
let el = selectedElements[selIdx]
reinitInspector(el, selected)
let fix = document.querySelector(`.fix-legend-${selIdx + 1}`)
fix.style.top = Math.max(3, parseFloat(selected.style.top) - 10) + 'px'
fix.style.left = parseFloat(selected.style.left) + 5 + 'px'
})
})
window.theRoom.on('mouseover', function () {
const inspector = document.querySelector('.inspector-element')
if (inspector) {
inspector.style.border = ''
inspector.style.boxShadow = ''
}
})
window.theRoom.on('click', function (element, event) {
event.preventDefault()
event.stopPropagation()
clearInterval(interval)
// TODO: reinit inspector here if it's gone
let inspector = document.querySelector('.inspector-element')
if (inspector.style.border === 'none') {
reinitInspector(element, inspector)
}
let selected = inspector.cloneNode()
let color = colors[commentIdx]
selected.classList.add('selected')
selected.classList.add('no-select')
selected.classList.add('selected-' + commentIdx)
selected.classList.remove('inspector-element')
selected.dataset.commentIdx = commentIdx
selected.style.borderColor = color
if (element.parentNode.id === 'wrapper') {
// TODO: think about this one more
selected.style.zIndex = -1
}
let prompt = document.getElementById('prompt')
prompt.dataset.commentIdx = commentIdx
prompt.classList.add('scale-100')
prompt.style.top = event.clientY + 'px'
prompt.style.left = '50%' // event.clientX + 'px'
prompt.style.marginLeft = `-200px`
let input = prompt.querySelector('input')
input.focus()
prompt.querySelector('button').addEventListener('click', () => {
addComment(input.value)
input.value = ''
prompt.classList.remove('scale-100')
})
// TODO: make it clear "esc" cancels
document.body.append(selected)
let fix = document.createElement('div')
fix.innerText = element.tagName
fix.addEventListener('mouseenter', () => {
const cmt = document.createElement('div')
cmt.innerText = text
cmt.className =
'p-2 rounded bg-white text-purple-500 italic absolute opacity-95 fix-comment'
cmt.style.top = Math.max(3, parseFloat(selected.style.top)) + 'px'
cmt.style.left = parseFloat(selected.style.left) + 'px'
cmt.style.zIndex = 50
document.body.append(cmt)
})
fix.addEventListener('mouseleave', () => {
const cmt = document.querySelector('.fix-comment')
if (cmt) {
cmt.parentNode.removeChild(cmt)
}
})
fix.className = `no-select fix fix-legend-${commentIdx} italic text-white text-center font-mono text-[8px] px-2 pt-0 z-50`
fix.classList.add('bg-' + color + '-600/[0.7]')
// TODO: use tailwind
fix.style.border = '1px dashed ' + color
fix.style.position = 'absolute'
fix.style.height = '12px'
fix.style.top = Math.max(3, parseFloat(selected.style.top) - 10) + 'px'
fix.style.left = parseFloat(selected.style.left) + 5 + 'px'
document.body.append(fix)
window.theRoom.stop()
function addComment(text) {
//let text = prompt('What would you like to fix about this?')
if (text) {
commentIdx += 1
let c = document.createComment('FIX (' + commentIdx + '): ' + text)
element.parentNode.insertBefore(c, element)
window.parent.parent.postMessage(
{
comment: text,
idx: commentIdx,
html: document.getElementById('wrapper').innerHTML
},
'*'
)
} else {
return
}
selectedElements.push(element)
clearInspector()
setTimeout(() => {
if (inspectorEnabled) {
window.theRoom.start()
}
reset()
}, 500)
}
})
window.addEventListener('load', () => {
window.parent.parent.postMessage({ action: 'ready' }, '*')
})
window._go = function (cb) {
if (document.readyState === 'complete') {
cb()
} else {
document.addEventListener('DOMContentLoaded', cb)
}
}
// handle events from parent
window.addEventListener(
'message',
function (event) {
if (event.data.action === 'take-screenshot') {
console.log('Got take screenshot event')
html2canvas(document.body, {
useCors: true,
foreignObjectRendering: true,
allowTaint: true,
windowWidth: 1524,
windowHeight: 768
}).then(function (canvas) {
const data = canvas.toDataURL('image/png')
window.parent.parent.postMessage(
{ screenshot: data, action: 'screenshot' },
'*'
)
})
} else if (event.data.action === 'hydrate') {
let wrapper = document.getElementById('wrapper')
wrapper.innerHTML = event.data.html
wrapper.querySelectorAll('img').forEach(img => {
img.crossOrigin = 'anonymous'
})
if (event.data.darkMode) {
document.documentElement.classList.add('darkMode')
}
document.querySelectorAll('.user-script').forEach(scr => {
scr.parentNode.removeChild(scr)
})
event.data.js.forEach(js => {
const script = document.createElement('script')
script.classList.add('user-script')
script.type = js.type
if (js.src) script.setAttribute('src', js.src)
// Close our JS to avoid conflicts
script.text = `(()=>{${js.text}})()`
document.body.append(script)
})
// Remove our selected elements
selectedElements = []
commentIdx = 0
var elements = document.querySelectorAll('.selected, .fix')
elements.forEach(function (element) {
element.parentNode.removeChild(element)
})
// TODO: maybe delay this a bit
window.parent.parent.postMessage(
{
preview: wrapper.hasChildNodes(),
height: document.body.scrollHeight,
action: 'loaded'
},
'*'
)
// state = event.data.state
} else if (event.data.action === 'toggle-dark-mode') {
if (document.documentElement.classList.contains('darkMode')) {
document.documentElement.classList.remove('darkMode')
} else {
document.documentElement.classList.add('darkMode')
}
} else if (event.data.action === 'toggle-inspector') {
if (inspectorEnabled) {
window.theRoom.stop()
} else {
window.theRoom.start()
}
inspectorEnabled = !inspectorEnabled
}
},
false
)
</script>
</html>