-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
375 lines (330 loc) · 13.1 KB
/
index.html
File metadata and controls
375 lines (330 loc) · 13.1 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<html>
<head>
<title>Together</title>
<meta name="description" content="Learn together">
<meta name="author" content="Kris">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:title" content="Together">
<meta property="og:description" content="Learn together">
<meta property="og:image" content="https://krisl.github.io/together/chat.png">
<meta name="twitter:text:title" content="Together">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://krisl.github.io/together/chat.png">
</head>
<style>
#url {
min-width: 70%;
}
.imgpreview {
width: 40%;
border: 1px solid black;
vertical-align: top;
}
picture {
position: relative;
}
picture.story:after {
content: "";
border-top: 25px solid red;
border-left: 25px solid transparent;
position: absolute;
right: 0;
}
@media screen and (max-width: 600px) {
.desktop {
visibility: hidden;
display: none;
}
}
</style>
<link rel="stylesheet" href="css/style.css">
<script>
const peerImages = {}
function broadcastAllImages () {
getOurImagesPromise().then((images) => {
images = images.filter(image => (image.url || '').startsWith('http'))
console.log('Sending Images to client', images)
TogetherJS.send({type: 'setImages', images})
})
}
var TogetherJSConfig_hubBase = "https://togetherjs-hub.glitch.me/"
var TogetherJSConfig_dontShowClicks = ".ignore-clicks"
var TogetherJSConfig_ignoreForms = ['input[type=file]']
var TogetherJSConfig_cloneClicks = "#loadUrl" // clone clicks on load image button only
var TogetherJSConfig_autoStart = true
var TogetherJSConfig_suppressJoinConfirmation = true
var TogetherJSConfig_suppressInvite = true
var TogetherJSConfig_on_ready = function () {
var session = TogetherJS.require("session");
console.log({session});
broadcastAllImages()
TogetherJS.hub.on('setImageUrl', msg => setImageUrl(msg.url, true))
TogetherJS.hub.on('imagesSet', msg => console.log('imagesSet', {msg}))
TogetherJS.hub.on('togetherjs.form-init', loadMainImage)
TogetherJS.hub.on('togetherjs.peer-update', msg => {
const peerInfo = peerImages[msg.clientId]
peerImages[msg.clientId] = { images: peerInfo.images, name: msg.name }
})
TogetherJS.hub.on('setImages', msg => {
peerImages[msg.clientId] = { images: msg.images, name: msg.peer.name }
console.log('setImages', {msg})
TogetherJS.send({type: 'imagesSet', qty: msg.images.length});
})
TogetherJS.hub.on('togetherjs.hello', msg => {
// FIXME sent only to hello client, can be done?
broadcastAllImages()
})
const shareButton = document.getElementById('togetherjs-share-button')
shareButton.dataset.intro = "Click here to get link to share"
shareButton.dataset.position = "left"
$('body').chardinJs('start')
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/chardin.js/0.1.3/chardinjs.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/chardin.js/0.1.3/chardinjs.js"></script>
<script async src="https://togetherjs.com/togetherjs-min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/lozad"></script>
<script defer src="./panel.js"></script>
<body>
<main class="cd-main-content">
<div calss="tools">
<input checked type="checkbox" id="proxy" title="proxy" class="ignore-clicks">
<input type="url" name="url" id="url"
value="https://dafideen.files.wordpress.com/2014/11/1911807_10205199837057666_925714740730337757_n.jpg"
data-intro="Enter URL of image here. Try searching Pinterest or other image sites for great content"
data-position="bottom"
placeholder="https://site.com/path/to/image.jpg"
required>
<a href="#2" class="button-link" id="loadUrl" data-intro="Load image. START HERE" data-position="bottom">Load</a>
<a href="#0" class="js-cd-panel-trigger desktop button-link" data-panel="main" data-intro="Show image library" data-position="bottom">
Images
</a>
<a href="#1" class="js-cd-panel-trigger desktop button-link" data-panel="story" id="storybutton" style="visibility: hidden">
Story
</a>
</div>
<template id="image-template">
<img src="" data-src="">
</template>
<div id="images" />
</main>
<div class="ignore-clicks cd-panel cd-panel--from-right js-cd-panel-main">
<header class="cd-panel__header">
<h1>
Images
<input style='margin-left:30px;' id="browse" type="file" onchange="addFiles()" multiple>
<a href="#3" class="button-link" onclick="loadBidstrup()" style="font-size: initial">Bidstrup</a>
</h1>
<a href="#0" class="cd-panel__close js-cd-close">Close</a>
</header>
<div class="cd-panel__container">
<div id="previews" class="cd-panel__content">
<p>Loading...</p>
</div>
</div>
</div>
<div class="ignore-clicks cd-panel cd-panel--from-right js-cd-panel-story">
<header class="cd-panel__header">
<h1>Story</h1>
<a href="#1" class="cd-panel__close js-cd-close">Close</a>
</header>
<div class="cd-panel__container">
<div id="story" class="cd-panel__content">
<p>Loading...</p>
</div>
</div>
</div>
<script src="https://unpkg.com/dexie@latest/dist/dexie.js"></script>
<script>
const timeStamp = (e = new Event('e')) => e.timeStamp.toString()
function addFiles() {
const input = document.querySelector('input[type=file]')
const files = input.files
function readAndPreview(file) {
// Make sure `file.name` matches our extensions criteria
if ( /\.(jpe?g|png|gif)$/i.test(file.name) ) {
const reader = new FileReader();
reader.onload = function (e) {
const name = timeStamp(e)
const src = this.result;
console.log({name, src})
add(name, src).then(
// TODO previewAllImages only after all have been saved
() => previewAllImages()
)
console.log({e})
}
reader.readAsDataURL(file);
}
}
if (files) {
[].forEach.call(files, readAndPreview);
}
input.value = null
}
function setStoryButtonVisibility (visible) {
const imageInput = document.getElementById("storybutton")
imageInput.setAttribute('style', `visibility: ${visible ? 'visible' : 'hidden'}`)
}
function recordHistory (url) {
return getImagesPromise().then(
(images) => {
const existingImage = images.find(image => image.url == url)
if (!existingImage) {
add(timeStamp(), url)
console.log('added new history item', url)
} else {
console.log('already exists', url)
}
}
)
}
function makeImageNode () {
const template = document.getElementById('image-template')
const images = document.getElementById("images")
const clone = document.importNode(template.content, true)
return clone.querySelector('img')
}
function getImgurAlbum (url) {
const prefix = 'https://imgur.com/a/'
if (url.startsWith(prefix))
return url.substr(prefix.length)
}
function getUrls (url) {
const imgurAlbum = getImgurAlbum(url)
if (imgurAlbum) {
const headers = new Headers({Authorization: 'Client-ID fbf77ff49c42c8a'})
return fetch(new Request(`https://api.imgur.com/3/album/${imgurAlbum}`, { headers }))
.then(x => x.json())
.then(resp => resp.data.images.map(image => image.link))
}
return Promise.resolve([url])
}
function loadMainImage () {
const imageInput = document.getElementById("url");
const images = document.getElementById("images");
images.textContent = "" // remove existing images
loadStories(imageInput.dataset.src).then(setStoryButtonVisibility)
getUrls(imageInput.value).then(values => { for (const value of values) {
const image = makeImageNode()
image.onload = () => TogetherJS.send({type: 'imageLoaded', url: imageInput.value});
// FIXME this is bogus if the url is manually entered
image.dataset.src = imageInput.dataset.src // story determination comes from here
image.src = proxyUrl(value);
images.appendChild(image)
}})
if (imageInput.value) {
recordHistory(imageInput.value)
}
}
// add handler to img button
document.getElementById("loadUrl").addEventListener("click", e => {
$('body').chardinJs('stop')
e.preventDefault()
loadMainImage()
});
function loadBidstrup () {
// contents of the bidstrup.json made by going to http://www.bidstrup.ru/content.html
// JSON.stringify($$('table table table a').map(x => (console.log(x), {name: x.innerHTML, url: x.href.replace('content', 'images/comicses').replace('html', 'gif')})))
fetch('bidstrup.json')
.then(resp => resp.json())
.then(images => {
peerImages['bidstrup'] = { name: 'bidstrup', images }
previewAllImages()
})
}
// initialise dexie
var db = new Dexie("images");
db.version(1).stores({
images: 'name,url'
});
// db.images.put({name: 'Банковские операции', url:'http://bidstrup.ru/images/comicses/0908.gif'})
// db.images.put({name: 'Плата за мир', url:'http://bidstrup.ru/images/comicses/0920.gif'})
// db.images.put({name: 'car', url:'https://i.pinimg.com/474x/ed/35/fb/ed35fbefa01c7ea3015a0ef7c9201eb8--adult-coloring-coloring-books.jpg'})
const useProxy = () => document.getElementById("proxy").checked
const proxyUrl = (url, cb = (id) => id) => (useProxy() && url.startsWith('http'))
? cb(`https://images.weserv.nl/?url=${encodeURIComponent(url.replace('https://', 'ssl:').replace('http://', ''))}`)
: url
function setImageUrl (url, surpress) {
const imageInput = document.getElementById("url");
imageInput.dataset.src = url // no need for this is web preserve src
imageInput.value = url;
loadMainImage()
if (!surpress)
TogetherJS.send({type: 'setImageUrl', url})
}
/* console functions */
function load (name) {
db.images.get(name).then(
image => {
if (!image) {
console.log('Not found [' + name + ']')
return
}
setImageUrl(image.url)
}
)
}
function each () {
db.images.each(row => console.log(row.name, row.url))
}
function add (name, url) {
return db.images.get(name).then(
image => {
if (image) {
console.log(`image [${name}] already exists with url [${image.url}]`)
console.log({image})
} else {
image = {name}
}
image.url = url
return db.images.put(image)
}
)
}
function addStory (name, lang, story) {
db.images.get(name).then(
image => {
if (!image) {
console.log('image does not exist [' + name + ']')
return
}
image.story = (image.story || {})
image.story[lang] = story
db.images.put(image)
}
)
}
/* end console functions */
function imageClicked (el) {
console.log({el})
if (el) setImageUrl(el.dataset.src)
return false
}
function loadStories (src) {
return getImagesPromise().then(
(images) => {
const storyImage = images.find(image => image.url == src) || {}
if (storyImage.story)
return storyImage.story
const peerId = Object.keys(peerImages).find(peerId => peerImages[peerId].images.find(image => image.url === src))
if (peerId) {
return peerImages[peerId].images.find(image => image.url === src).story
}
}
)
}
// OurImages | TheirImages :: [Image]
// getImagesPromise :: global -> OurImages | TheirImages
function getImagesPromise () {
return getOurImagesPromise()
}
// getOurImagesPromise :: global -> OurImages
function getOurImagesPromise () {
const images = []
return db.images.each(row => images.push(row)).then(() => images)
}
</script>
</body>
</html>