-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathui.html
658 lines (636 loc) · 16.5 KB
/
ui.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
<style>
#app {
margin: 0 8px;
}
.d-inline {
display: inline-block;
max-width: 50px;
}
.footer .button {
display: inline-flex;
}
.icon-button {
padding: 0;
}
.row {
display: block;
margin: 0 -8px;
overflow: auto;
}
.col {
padding: 8px;
float: left;
width: 50%;
}
.row.field > .col {
padding: 16px;
line-height: 18px;
}
.field select {
display: block;
max-width: 100%;
}
.step-fields {
padding-bottom: 100px;
}
.label {
margin-top: -1em;
}
</style>
<link
rel="stylesheet"
href="https://unpkg.com/figma-plugin-ds@0.1.8/dist/figma-plugin-ds.css"
/>
<style>
body {
font-family: Inter, system-ui, roboto, sans-serif;
font-size: 11px;
}
[v-cloak] {
display: none;
}
.grid {
overflow: auto;
padding: 0 8px 8px;
}
label > .left + select {
display: block;
margin: 0 0 0 180px;
width: 180px;
}
.footer {
position: fixed;
bottom: 0;
right: 0;
left: auto;
padding: 16px;
}
.footer .btn + .btn {
margin-left: 10px;
}
ol {
margin-top: 0;
padding-left: 20px;
}
li {
line-height: 18px;
}
svg {
vertical-align: top;
}
.step-msg {
line-height: 200px;
text-align: center;
font-size: 15px;
}
</style>
<div id="app" v-cloak>
<div class="step step-msg step-loading" v-if="selection.loading">
Select a component or a frame
</div>
<div class="step step-msg step-loading" v-if="step === 'loading'">
Reading data from Airtable
</div>
<div
class="step step-msg step-select"
v-else-if="!selection.length"
@click="checkSelection"
>
Select a component or a frame and click here
</div>
<div class="step step-msg step-texts" v-else-if="!texts.length">
Selection must contain at least 1 <b>TEXT</b> layer
</div>
<form
class="step step-fields"
v-else-if="data.columns.length && !mappingConfirmed && !changeLink"
@submit.prevent="apply"
>
<div class="row">
<div class="col">
<div class="section-title">Text node</div>
</div>
<div class="col">
<div class="section-title">Airtable field</div>
</div>
</div>
<label class="row field" v-for="text in config.mapping">
<div class="col">
<div class="node-name">{{text.name}}</div>
</div>
<div class="col">
<select class="" v-model="text.airtableField">
<option value="" selected>--- Select an Airtable field ---</option>
<option v-for="col in textColumns">{{col.name}}</option>
</select>
</div>
</label>
<div v-if="imageColumns.length && config.images && config.images.length">
<div class="row">
<div class="col">
<div class="section-title">Image node</div>
</div>
<div class="col">
<div class="section-title">Airtable field</div>
</div>
</div>
<label class="row field" v-for="text in config.images">
<div class="col">
<div class="node-name">{{text.name}}</div>
</div>
<div class="col">
<select class="" v-model="text.airtableField">
<option value="" selected>--- Select an Airtable field ---</option>
<option v-for="col in imageColumns">{{col.name}}</option>
</select>
</div>
</label>
</div>
<div class="footer">
<button
type="button"
class="button button--secondary"
@click.prevent="changeLink = true"
>
Change link
</button>
<button
type="button"
class="button button--primary"
@click.prevent="mappingConfirmed = true"
>
Next
</button>
</div>
</form>
<form
class="step step-fields"
v-else-if="data.columns.length && !changeLink"
@submit.prevent="apply"
>
<div class="row">
<div class="col">
<div class="section-title">Direction</div>
<div class="grid">
<div class="d-inline">
<button
type="button"
@click="config.grid = ''"
class="icon-button"
:class="{'icon-button--selected': !config.grid }"
>
<div class="icon icon--layout-grid-rows"></div>
</button>
</div>
<div class="d-inline">
<button
type="button"
@click="config.grid = 'columns'"
class="icon-button"
:class="{'icon-button--selected': config.grid === 'columns' }"
>
<div class="icon icon--layout-grid-columns"></div>
</button>
</div>
<div class="d-inline">
<button
type="button"
@click="config.grid = 'grid'"
class="icon-button"
:class="{'icon-button--selected': config.grid === 'grid' }"
>
<div class="icon icon--layout-grid-uniform"></div>
</button>
</div>
</div>
</div>
<div class="col" v-if="config.grid === 'grid'">
<label class="input">
<div class="section-title">Columns</div>
<input
type="number"
class="input__field"
v-model="config.columns"
placeholder="Default: 3"
min="1"
max="100"
/>
</label>
</div>
</div>
<div class="row">
<div class="col">
<label class="input">
<div class="section-title">Spacing</div>
<input
type="number"
class="input__field"
v-model="config.spacing"
placeholder="Default: 20px"
min="0"
max="1000"
/>
</label>
</div>
<div class="col">
<label class="input">
<div class="section-title">Max items</div>
<input
type="number"
class="input__field"
v-model="config.limit"
placeholder="Default: 100 items"
min="1"
max="1000"
/>
</label>
</div>
</div>
<div class="footer">
<button
type="button"
class="button button--secondary"
@click.prevent="mappingConfirmed = false"
>
Change field mapping
</button>
<button
type="button"
class="button button--primary"
@click.prevent="postMessage('close')"
>
Done
</button>
</div>
</form>
<form @submit.prevent="submitURL" v-else>
<div class="section-title">Getting started</div>
<ol>
<li>
Go to <a href="https://airtable.com">airtable.com</a> and build or
select your own table
</li>
<li>In your Airtable of choice, click ‘Share’</li>
<li>Scroll down and click ‘Create a shared view’</li>
<li>Click ‘Share the current view’</li>
<li>Next, click ‘Create a shareable grid view’.</li>
<li>Copy the link and paste it below.</li>
</ol>
<label class="input">
<div class="section-title">Shared view link</div>
<input
class="input__field"
type="text"
v-model="url"
placeholder="https://airtable.com/shr..."
autofocus
required
/>
<span class="label" v-if="data.url === url"
>Found {{data.rows.length}} rows and {{data.columns.length}}
columns</span
>
</label>
<div class="footer">
<button
type="button"
class="button button--secondary"
@click.prevent="url = 'https://airtable.com/shr0hyEnBae0AKKfQ'"
>
Example
</button>
<button
type="submit"
class="button button--primary"
:disabled="!validURL"
>
Load Airtable data
</button>
</div>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
<script>
const emptyArray = []
const noData = { rows: emptyArray, columns: emptyArray, url: '' }
const render = debounce(async state => {
// console.log('render', state)
const ok = await requestMessage('renderAirtable', state)
if (ok === 'sync:selection') {
app.checkSelection(true)
}
// console.log('rendered', ok)
}, 200)
const app = new Vue({
el: '#app',
data() {
return {
config: {
grid: '',
spacing: null,
limit: null,
columns: null,
mapping: emptyArray,
images: emptyArray,
},
mappingConfirmed: false,
changeLink: false,
data: noData,
selection: emptyArray,
step: '',
url: '',
}
},
computed: {
validURL() {
return this.url
},
texts() {
return (this.selection[0] && this.selection[0].texts) || emptyArray
},
textColumns() {
return this.data.columns.filter(col => !col.type.includes('ttachment'))
},
images() {
return (this.selection[0] && this.selection[0].images) || emptyArray
},
imageColumns() {
return this.data.columns.filter(col => col.type.includes('ttachment'))
},
rows() {
if (!this.data.rows) {
return emptyArray
}
return this.data.rows.map(row => {
const out = {}
this.data.columns.forEach(col => {
out[col.name] =
(row.cellValuesByColumnId && row.cellValuesByColumnId[col.id]) ||
''
})
return out
})
},
state() {
return {
airtableConfig: this.config,
airtableLink: this.url,
airtableRows: this.rows,
}
},
},
methods: {
postMessage,
log() {
postMessage('debug')
},
async checkSelection(noConfig) {
this.selection = emptyArray
this.selection.loading = true
this.selection = await requestMessage('selection')
if (noConfig) {
return
}
if (!this.selection.length && this.data.rows.length) {
console.log('selection no len')
this.data = noData
return
}
if (!this.texts.length && this.data.rows.length) {
console.log('selection no text')
this.data = noData
return
}
// Get URL
const withUrl = this.selection.find(s => s.airtableLink)
if (!withUrl) {
return
}
console.log('current airtableLink', withUrl.airtableLink)
this.url = withUrl.airtableLink
// Get data
const withData = this.selection.find(s => s.airtableData)
if (!withData) {
return
}
console.log(
'current airtableData rows',
withData.airtableData.rows.length
)
this.data = withData.airtableData
// Get data
const withConfig = this.selection.find(s => s.airtableConfig)
if (!withConfig) {
return
}
console.log('current airtableConfig', inert(withConfig.airtableConfig))
this.config = withConfig.airtableConfig
},
async submitURL() {
// Fetch data from API
this.step = 'loading'
this.changeLink = false
this.data = noData
const data = await scrape(this.url)
console.log('got', data.url)
this.step = ''
if (!data.rows.length) {
return notify('could not fetch data from Airtable')
}
if (!data.columns.length) {
return notify('could not fetch data from Airtable')
}
// Selection validation (deprecated?)
if (!this.selection.length) {
return msg('select', 'url')
}
const texts = this.selection[0].texts
if (!texts || !texts.length) {
return msg('texts', 'url')
}
// Prepare an empty mapping
this.data = data
this.config.mapping = inert(this.getMapping())
this.config.images = inert(this.getImages())
// Alright, let's remember for next time
requestMessage('saveAirtableLink', this.url)
requestMessage('saveAirtableData', this.data)
// document.querySelector('#fields').innerHTML = texts
// .map(
// text =>
// `<label><div class="left">${text.name}</div><select><option value="" selected>--- Select an Airtable field ---</option>${options}</select></label>`
// )
// .join('')
},
async apply() {
const v = this.state
if (!v || !v.airtableLink || !v.airtableConfig.mapping.length) {
return
}
// debounced
render(v)
},
getMapping() {
if (
this.selection[0] &&
this.selection[0].airtableConfig &&
this.selection[0].airtableConfig.mapping
) {
return this.selection[0].airtableConfig.mapping
}
return this.texts.map(t => {
t.airtableField = ''
return t
})
},
getImages() {
if (
this.selection[0] &&
this.selection[0].airtableConfig &&
this.selection[0].airtableConfig.images
) {
return this.selection[0].airtableConfig.images
}
return this.images.map(t => {
t.airtableField = ''
return t
})
},
},
mounted() {
this.checkSelection()
},
watch: {
rows() {
this.apply()
},
config: {
deep: true,
handler(v) {
this.apply()
},
},
},
})
function notify(msg) {
postMessage('notify', msg)
}
function postMessage(type, data) {
const id = Math.random().toString(36)
window.parent.postMessage({ pluginMessage: { id, type, data } }, '*')
return id
}
function requestMessage(type, data) {
return new Promise(resolve => {
const id = postMessage('request:' + type, JSON.stringify(data))
window.addEventListener('message', onmessage)
function onmessage(evt) {
const message = evt.data.pluginMessage
if (message && message.id === id) {
resolve(JSON.parse(message.data))
// console.log('response message', message, JSON.parse(message.data))
window.removeEventListener('message', onmessage)
}
}
})
}
function responseMessage(msg, data) {
window.parent.postMessage(
{
pluginMessage: {
id: msg.id,
type: msg.type.replace('request:', 'response:'),
data: JSON.stringify(data),
},
},
'*'
)
}
function inert(data) {
return JSON.parse(JSON.stringify(data))
}
function debounce(fn, wait) {
var timeout
return function () {
const args = arguments
clearTimeout(timeout)
timeout = setTimeout(function () {
fn.apply(this, args)
}, wait || 1)
}
}
function scrape(url) {
if (ls(url)) {
console.log('localStorage scrape')
return ls(url)
}
return fetch(
'https://airtable-shr.vercel.app/api?' +
serialize({
url,
})
)
.then(r => r.json())
.then(data => {
if (data && data.data) {
data = data.data
}
if (!data || !data.rows || !data.columns) {
return noData
}
ls(url, data)
return data
})
.catch(e => {
e.message && notify(e.message)
return noData
})
.then(data => {
data.url = url
return data
})
}
function serialize(obj) {
const str = []
for (const p in obj) {
if (obj.hasOwnProperty(p) && obj[p]) {
str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]))
}
}
return str.join('&')
}
function ls(key, value) {
try {
const storage =
typeof localStorage !== 'undefined' ? localStorage : figma.clientStorage
if (typeof value === 'undefined') {
return JSON.parse(storage[key] || 'null')
} else {
storage[key] = JSON.stringify(value)
return value
}
} catch (e) {}
}
function msg(id, after) {
alert(id)
}
imageDownloadService()
function imageDownloadService() {
window.addEventListener('message', onmessage)
async function onmessage(evt) {
const msg = evt.data.pluginMessage
if (msg && msg.type === 'request:downloadImage') {
const url = JSON.parse(msg.data)
responseMessage(msg, await urlToImage(url))
}
}
}
function urlToImage(imgUrl) {
return fetch(imgUrl)
.then(r => r.arrayBuffer())
.then(a => {
return Array.from(new Uint8Array(a))
})
}
</script>