@pseudolux-ui/media-library 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,527 @@
1
+ <view>
2
+ <div class="tri" role="dialog" aria-modal="true" aria-label="Describing pictures" $show="state.open" $ref="root" @keydown="onKey">
3
+ <div class="tri-top">
4
+ <button class="px-btn px-btn-ghost px-btn-sm" type="button" $ref="leave" @click="finish">{{ state.done ? 'Close' : 'Finish later' }}</button>
5
+ <b>Describing pictures</b>
6
+ <span class="px-subtle" aria-live="polite">{{ state.position }}</span>
7
+ <div class="prog" aria-hidden="true">
8
+ <span $for="one in state.progress" $key="one.key" $class="one.tone"></span>
9
+ </div>
10
+ <span class="px-subtle keys" $show="!state.done"><span class="px-kbd">Enter</span> save and next · <span class="px-kbd">Shift</span> <span class="px-kbd">Enter</span> new line · <span class="px-kbd">Esc</span> finish later</span>
11
+ </div>
12
+
13
+ <div class="done-card" $show="state.done">
14
+ <div class="tick" aria-hidden="true">✓</div>
15
+ <h2>{{ state.doneTitle }}</h2>
16
+ <p class="px-muted">{{ state.doneNote }}</p>
17
+ <div class="px-row done-acts">
18
+ <button class="px-btn px-btn-secondary" type="button" $show="state.skipped" @click="redoSkipped">Go back to the skipped ones</button>
19
+ <button class="px-btn px-btn-primary" type="button" $ref="doneButton" @click="finish">Done</button>
20
+ </div>
21
+ </div>
22
+
23
+ <div class="tri-body" $show="!state.done">
24
+ <div class="tri-pic">
25
+ <img class="shot" $src="state.picture" $style="state.pictureStyle" $alt="'The picture being described: ' + state.label" />
26
+ <div class="nxt" $show="state.upNext.length">
27
+ <span>Up next</span>
28
+ <img class="t" $for="one in state.upNext" $key="one.id" $src="one.thumb" alt="" />
29
+ </div>
30
+ </div>
31
+ <div class="tri-form">
32
+ <div>
33
+ <b class="name">{{ state.label }}</b>
34
+ <p class="px-subtle">{{ state.meta }}</p>
35
+ </div>
36
+ <div class="px-field">
37
+ <label class="px-label" for="run-alt">What's in this picture?</label>
38
+ <textarea class="big" id="run-alt" placeholder="Describe what matters for the pages it's on" $ref="alt" @input="onAlt" @keydown="altKey"></textarea>
39
+ <span class="chk" $for="check in state.checks" $key="check.text" $class="{ ok: check.tone === 'ok', warn: check.tone === 'warn' }">{{ check.mark }} {{ check.text }}</span>
40
+ <span class="px-hint">{{ state.wordsNote }}</span>
41
+ <span class="px-hint text-danger" role="alert" $show="state.error">{{ state.error }}</span>
42
+ </div>
43
+ <div class="suggest" $show="state.suggested.length">
44
+ <span class="px-label">Suggested labels</span>
45
+ <div class="px-row chips">
46
+ <span class="px-chip is-suggested" $for="chip in state.suggested" $key="chip.term">
47
+ {{ chip.term }}
48
+ <button type="button" $aria-label="'Accept ' + chip.term" @click="acceptLabel(chip)"><svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 6 9 17l-5-5"></path></svg></button>
49
+ <button type="button" $aria-label="'Reject ' + chip.term" @click="rejectLabel(chip)"><svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" aria-hidden="true"><path d="M18 6 6 18M6 6l12 12"></path></svg></button>
50
+ </span>
51
+ </div>
52
+ </div>
53
+ <div class="px-row acts">
54
+ <button class="px-btn px-btn-secondary" type="button" $disabled="state.saving" @click="skip">Skip for now</button>
55
+ <button class="px-btn px-btn-ghost" type="button" @click="openEditor">Open in the editor</button>
56
+ <button class="px-btn px-btn-primary next" type="button" $disabled="state.saving" @click="saveAndNext">{{ state.saving ? 'Saving…' : 'Save and next' }}</button>
57
+ </div>
58
+ </div>
59
+ </div>
60
+ </div>
61
+ </view>
62
+
63
+ <logic lang="ts">
64
+ import { reactive, onMounted, onUnmounted, shared, defineRefs } from '@lightspeed-js/core'
65
+ import { getLibrary, thumbnailUrl, previewUrl, SELECTED, PATCHED, RUN } from '../config.ts'
66
+ import { altChecks } from '../checks.ts'
67
+
68
+ /**
69
+ * Describing pictures one after another: the picture large, one box to type
70
+ * in, Enter to save and move on. For getting through a batch that needs
71
+ * words without opening and closing each one.
72
+ */
73
+ const refs = defineRefs()
74
+
75
+ const UP_NEXT = 4
76
+
77
+ const state = reactive({
78
+ open: false,
79
+ done: false,
80
+ position: '',
81
+ progress: [],
82
+ label: '',
83
+ meta: '',
84
+ picture: '',
85
+ pictureStyle: '',
86
+ upNext: [],
87
+ checks: [],
88
+ wordsNote: '',
89
+ suggested: [],
90
+ error: '',
91
+ saving: false,
92
+ skipped: 0,
93
+ doneTitle: '',
94
+ doneNote: '',
95
+ })
96
+
97
+ const run = shared(RUN)
98
+ const selected = shared(SELECTED)
99
+ const patched = shared(PATCHED)
100
+
101
+ const plural = (n, one, many) => n + ' ' + (n === 1 ? one : many)
102
+
103
+ let order = []
104
+ let at = 0
105
+ let described = []
106
+ let skipped = []
107
+ let text = ''
108
+ let rejected = []
109
+ let opener = null
110
+
111
+ const here = () => order[at] || null
112
+
113
+ function begin(items) {
114
+ order = items.slice()
115
+ at = 0
116
+ described = []
117
+ skipped = []
118
+ if (!state.open) {
119
+ opener = document.activeElement
120
+ state.open = true
121
+ document.documentElement.style.overflow = 'hidden'
122
+ }
123
+ show()
124
+ }
125
+
126
+ function show() {
127
+ const item = here()
128
+ state.done = !item
129
+ drawProgress()
130
+ if (!item) {
131
+ state.skipped = skipped.length
132
+ state.doneTitle = described.length ? plural(described.length, 'picture', 'pictures') + ' described' : 'Nothing described this time'
133
+ state.doneNote = skipped.length
134
+ ? plural(skipped.length, 'picture was', 'pictures were') + ' skipped and still ' + (skipped.length === 1 ? 'needs' : 'need') + ' words.'
135
+ : 'Every picture in this run has words now.'
136
+ state.position = ''
137
+ requestAnimationFrame(() => refs.doneButton && refs.doneButton.focus())
138
+ return
139
+ }
140
+ rejected = []
141
+ text = item.altText || ''
142
+ state.label = item.label
143
+ state.meta = [(item.tags || []).join(', '), item.width && item.height ? item.width + ' × ' + item.height : ''].filter(Boolean).join(' · ')
144
+ state.picture = previewUrl(item.id)
145
+ state.pictureStyle = 'aspect-ratio: ' + (item.width && item.height ? item.width + ' / ' + item.height : '3 / 2')
146
+ state.upNext = order.slice(at + 1, at + 1 + UP_NEXT).map((one) => ({ id: one.id, thumb: thumbnailUrl(one.id) }))
147
+ state.position = at + 1 + ' of ' + order.length
148
+ state.error = ''
149
+ drawWords(item)
150
+ drawSuggested(item)
151
+ requestAnimationFrame(() => {
152
+ if (!refs.alt) return
153
+ refs.alt.value = text
154
+ refs.alt.focus()
155
+ })
156
+ }
157
+
158
+ function drawWords(item) {
159
+ state.checks = altChecks(text).map((one) => ({ ...one, mark: one.tone === 'ok' ? '✓' : '!' }))
160
+ state.wordsNote = item.altView ? 'Just for ' + item.altView + '.' : 'Shared on every site that uses it.'
161
+ }
162
+
163
+ function drawProgress() {
164
+ state.progress = order.map((one, index) => ({
165
+ key: one.id,
166
+ tone: described.includes(one.id) ? 'done' : skipped.includes(one.id) ? 'skip' : index === at && !state.done ? 'cur' : '',
167
+ }))
168
+ }
169
+
170
+ function drawSuggested(item) {
171
+ const mine = (item.labels || []).filter((one) => !one.source || one.source.kind === 'person').map((one) => one.term)
172
+ state.suggested = (item.labels || [])
173
+ .filter((one) => one.source && one.source.kind !== 'person' && (one.status === 'generated' || one.status === 'pending'))
174
+ .filter((one) => !rejected.includes(one.term) && !mine.includes(one.term))
175
+ .map((one) => ({ term: one.term }))
176
+ }
177
+
178
+ function onAlt(event) {
179
+ text = event.target.value
180
+ const item = here()
181
+ if (item) drawWords(item)
182
+ }
183
+
184
+ function altKey(event) {
185
+ if (event.key !== 'Enter' || event.shiftKey || event.isComposing) return
186
+ event.preventDefault()
187
+ saveAndNext()
188
+ }
189
+
190
+ async function saveAndNext() {
191
+ const item = here()
192
+ if (!item || state.saving) return
193
+ const value = text.trim()
194
+ // Nothing typed is a skip: an empty save would say the picture was described.
195
+ if (!value) return skip()
196
+ state.saving = true
197
+ state.error = ''
198
+ try {
199
+ if (value !== (item.altText || '')) {
200
+ await getLibrary().describe(item.id, { altText: value }, item.altView ? { view: item.altView } : undefined)
201
+ item.altText = value
202
+ patched.value = {
203
+ id: item.id,
204
+ patch: item.altView ? { views: { [item.altView.trim().toLowerCase()]: { altText: value } } } : { altText: value },
205
+ }
206
+ }
207
+ described = described.concat([item.id])
208
+ skipped = skipped.filter((id) => id !== item.id)
209
+ at++
210
+ show()
211
+ } catch (err) {
212
+ state.error = "Couldn't save: " + (err && err.message ? err.message : 'no answer from the library') + '. Your words are still in the box.'
213
+ } finally {
214
+ state.saving = false
215
+ }
216
+ }
217
+
218
+ function skip() {
219
+ const item = here()
220
+ if (!item) return
221
+ if (!skipped.includes(item.id)) skipped = skipped.concat([item.id])
222
+ at++
223
+ show()
224
+ }
225
+
226
+ function redoSkipped() {
227
+ const again = order.filter((one) => skipped.includes(one.id))
228
+ const kept = described
229
+ begin(again)
230
+ described = kept
231
+ }
232
+
233
+ async function acceptLabel(chip) {
234
+ const item = here()
235
+ if (!item) return
236
+ const mine = (item.labels || [])
237
+ .filter((one) => !one.source || one.source.kind === 'person')
238
+ .map((one) => ({ term: one.term, ...(one.audience ? { audience: one.audience } : {}) }))
239
+ try {
240
+ await getLibrary().describe(item.id, { labels: mine.concat([{ term: chip.term }]) })
241
+ item.labels = (item.labels || []).concat([{ term: chip.term }])
242
+ patched.value = { id: item.id, replace: { labels: item.labels } }
243
+ drawSuggested(item)
244
+ } catch (err) {
245
+ state.error = "Couldn't add the label: " + (err && err.message ? err.message : 'no answer from the library')
246
+ }
247
+ }
248
+
249
+ function rejectLabel(chip) {
250
+ rejected = rejected.concat([chip.term])
251
+ const item = here()
252
+ if (item) drawSuggested(item)
253
+ }
254
+
255
+ function openEditor() {
256
+ const item = here()
257
+ if (!item) return
258
+ run.value = null
259
+ selected.value = { id: item.id, filename: item.filename, width: item.width, height: item.height, tags: item.tags }
260
+ }
261
+
262
+ function finish() {
263
+ run.value = null
264
+ }
265
+
266
+ function shut() {
267
+ state.open = false
268
+ order = []
269
+ document.documentElement.style.overflow = ''
270
+ if (opener && document.contains(opener) && opener.focus) opener.focus()
271
+ opener = null
272
+ }
273
+
274
+ function onKey(event) {
275
+ if (event.key === 'Escape') {
276
+ event.preventDefault()
277
+ finish()
278
+ }
279
+ }
280
+
281
+ function holdFocus(event) {
282
+ if (!state.open || event.key !== 'Tab' || !refs.root) return
283
+ const stops = Array.from(refs.root.querySelectorAll('button, textarea')).filter((one) => !one.disabled && one.offsetParent !== null)
284
+ if (!stops.length) return
285
+ const first = stops[0]
286
+ const last = stops[stops.length - 1]
287
+ if (!refs.root.contains(document.activeElement)) {
288
+ event.preventDefault()
289
+ first.focus()
290
+ } else if (event.shiftKey && document.activeElement === first) {
291
+ event.preventDefault()
292
+ last.focus()
293
+ } else if (!event.shiftKey && document.activeElement === last) {
294
+ event.preventDefault()
295
+ first.focus()
296
+ }
297
+ }
298
+
299
+ run.subscribe((value) => {
300
+ const items = (value && value.items) || []
301
+ if (items.length) begin(items)
302
+ else if (state.open) shut()
303
+ })
304
+
305
+ onMounted(() => {
306
+ document.addEventListener('keydown', holdFocus)
307
+ })
308
+
309
+ onUnmounted(() => {
310
+ document.removeEventListener('keydown', holdFocus)
311
+ if (state.open) document.documentElement.style.overflow = ''
312
+ })
313
+ </logic>
314
+
315
+ <style>
316
+ .tri {
317
+ position: fixed;
318
+ inset: 0;
319
+ z-index: 45;
320
+ display: flex;
321
+ flex-direction: column;
322
+ background: var(--px-bg);
323
+ color: var(--px-text);
324
+ }
325
+
326
+ .tri-top {
327
+ display: flex;
328
+ align-items: center;
329
+ flex-wrap: wrap;
330
+ gap: 8px 16px;
331
+ padding: 12px 28px;
332
+ border-bottom: 1px solid var(--px-border);
333
+ background: var(--px-surface);
334
+ }
335
+
336
+ .prog {
337
+ display: flex;
338
+ gap: 3px;
339
+ flex: 1;
340
+ min-width: 120px;
341
+ max-width: 420px;
342
+ }
343
+
344
+ .prog span {
345
+ flex: 1;
346
+ height: 6px;
347
+ border-radius: 3px;
348
+ background: var(--px-surface-2);
349
+ }
350
+
351
+ .prog span.done { background: var(--px-ok); }
352
+ .prog span.skip { background: var(--px-warn); }
353
+ .prog span.cur { background: var(--px-accent); }
354
+
355
+ .keys {
356
+ margin-left: auto;
357
+ }
358
+
359
+ .tri-body {
360
+ flex: 1;
361
+ min-height: 0;
362
+ display: grid;
363
+ grid-template-columns: minmax(0, 1fr) minmax(320px, 460px);
364
+ }
365
+
366
+ .tri-pic {
367
+ display: flex;
368
+ flex-direction: column;
369
+ align-items: center;
370
+ justify-content: center;
371
+ gap: 16px;
372
+ padding: 28px;
373
+ min-height: 0;
374
+ background: #0e0e0f;
375
+ }
376
+
377
+ .shot {
378
+ display: block;
379
+ min-height: 0;
380
+ max-width: min(100%, 820px);
381
+ max-height: calc(100% - 60px);
382
+ object-fit: contain;
383
+ border-radius: var(--px-r-s);
384
+ box-shadow: 0 30px 70px rgba(0, 0, 0, .5);
385
+ background: #1b1b1d;
386
+ }
387
+
388
+ .nxt {
389
+ display: flex;
390
+ gap: 8px;
391
+ align-items: center;
392
+ color: #bbb;
393
+ font-size: 12px;
394
+ }
395
+
396
+ .t {
397
+ width: 64px;
398
+ height: 40px;
399
+ border-radius: 4px;
400
+ object-fit: cover;
401
+ opacity: .7;
402
+ background: #1b1b1d;
403
+ }
404
+
405
+ .tri-form {
406
+ display: flex;
407
+ flex-direction: column;
408
+ gap: 18px;
409
+ padding: 28px;
410
+ background: var(--px-surface);
411
+ border-left: 1px solid var(--px-border);
412
+ overflow: auto;
413
+ }
414
+
415
+ .name {
416
+ font-size: var(--px-fs-l);
417
+ overflow-wrap: anywhere;
418
+ }
419
+
420
+ .big {
421
+ font: inherit;
422
+ font-size: 19px;
423
+ line-height: 1.5;
424
+ min-height: 150px;
425
+ padding: 14px 16px;
426
+ border: 1px solid var(--px-border-strong);
427
+ border-radius: 12px;
428
+ background: var(--px-bg);
429
+ color: var(--px-text);
430
+ resize: vertical;
431
+ }
432
+
433
+ .big:focus {
434
+ outline: none;
435
+ box-shadow: 0 0 0 3px var(--px-focus);
436
+ }
437
+
438
+ .chk {
439
+ font-size: 12px;
440
+ line-height: 1.4;
441
+ color: var(--px-text-2);
442
+ }
443
+
444
+ .chk.ok { color: var(--px-ok); }
445
+ .chk.warn { color: var(--px-warn); }
446
+
447
+ .suggest {
448
+ display: flex;
449
+ flex-direction: column;
450
+ gap: 8px;
451
+ }
452
+
453
+ .chips {
454
+ gap: 6px;
455
+ flex-wrap: wrap;
456
+ }
457
+
458
+ .acts {
459
+ gap: 8px;
460
+ margin-top: auto;
461
+ padding-top: 12px;
462
+ border-top: 1px solid var(--px-border);
463
+ flex-wrap: wrap;
464
+ }
465
+
466
+ .next {
467
+ margin-left: auto;
468
+ }
469
+
470
+ .done-card {
471
+ margin: auto;
472
+ display: flex;
473
+ flex-direction: column;
474
+ align-items: center;
475
+ gap: 14px;
476
+ text-align: center;
477
+ max-width: 460px;
478
+ padding: 24px;
479
+ }
480
+
481
+ .tick {
482
+ font-size: 44px;
483
+ }
484
+
485
+ .done-card h2 {
486
+ font-size: 26px;
487
+ }
488
+
489
+ .done-acts {
490
+ gap: 8px;
491
+ justify-content: center;
492
+ flex-wrap: wrap;
493
+ }
494
+
495
+ .text-danger {
496
+ color: var(--px-danger);
497
+ }
498
+
499
+ @media (max-width: 820px) {
500
+ .tri {
501
+ overflow: auto;
502
+ }
503
+
504
+ .tri-body {
505
+ display: flex;
506
+ flex-direction: column;
507
+ flex: none;
508
+ }
509
+
510
+ .tri-pic {
511
+ padding: 16px;
512
+ }
513
+
514
+ .shot {
515
+ max-height: 46vh;
516
+ }
517
+
518
+ .tri-form {
519
+ border-left: 0;
520
+ overflow: visible;
521
+ }
522
+
523
+ .keys {
524
+ display: none;
525
+ }
526
+ }
527
+ </style>