@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,227 @@
1
+ <view>
2
+ <div class="cover" $show="state.group">
3
+ <button class="px-btn px-btn-ghost px-btn-sm back" type="button" @click="back">‹ Groups</button>
4
+ <div class="banner">
5
+ <div class="mosaic" aria-hidden="true">
6
+ <img $for="pic in state.pics" $key="pic.id" $src="pic.thumb" alt="" loading="lazy" />
7
+ </div>
8
+ <div class="about">
9
+ <h1>{{ state.group }}</h1>
10
+ <p>{{ state.note }}</p>
11
+ </div>
12
+ </div>
13
+ <div class="tiles">
14
+ <button class="tile" type="button" $for="tile in state.tiles" $key="tile.key" $aria-pressed="tile.on" $disabled="!tile.part" @click="choose(tile)">
15
+ <b>{{ tile.number }}</b>
16
+ <span>{{ tile.label }}</span>
17
+ </button>
18
+ </div>
19
+ </div>
20
+ </view>
21
+
22
+ <logic lang="ts">
23
+ import { reactive, onMounted, shared } from '@lightspeed-js/core'
24
+ import { getLibrary, thumbnailUrl, cannotChange, canWrite, TAB, SENTENCE, CHANGED, PATCHED } from '../config.ts'
25
+ import { BLANK } from '../sentence.ts'
26
+ import { survey, tally, atLeast, forgetSurveys } from '../survey.ts'
27
+
28
+ /**
29
+ * The top of an open group: what it is, whether the person can change it,
30
+ * and its numbers. Each number is a way into the pictures below: choosing
31
+ * one sets the sentence, and choosing it again clears that part.
32
+ */
33
+ const state = reactive({
34
+ group: '',
35
+ note: '',
36
+ pics: [],
37
+ tiles: [],
38
+ })
39
+
40
+ const tab = shared(TAB)
41
+ const sentence = shared(SENTENCE)
42
+ const changed = shared(CHANGED)
43
+ const patched = shared(PATCHED)
44
+
45
+ let generation = 0
46
+ let counted = null
47
+
48
+ function draw() {
49
+ const now = { ...BLANK, ...(sentence.value || {}) }
50
+ if (!counted) {
51
+ state.tiles = []
52
+ return
53
+ }
54
+ const few = (n) => atLeast(n, counted.complete)
55
+ state.tiles = [
56
+ { key: 'all', number: few(counted.count), label: counted.count === 1 ? 'picture' : 'pictures', part: '', value: '', on: false },
57
+ { key: 'needs', number: few(counted.needs.length), label: counted.needs.length === 1 ? 'needs words' : 'need words', part: 'condition', value: 'needs', on: now.condition === 'needs' },
58
+ { key: 'month', number: String(counted.month.length), label: 'added this month', part: 'when', value: 'month', on: now.when === 'month' },
59
+ ]
60
+ }
61
+
62
+ async function look() {
63
+ const group = (sentence.value && sentence.value.group) || ''
64
+ if (group !== state.group) {
65
+ state.group = group
66
+ state.pics = []
67
+ counted = null
68
+ }
69
+ draw()
70
+ if (!group) return
71
+ const mine = !cannotChange([group])
72
+ state.note = !canWrite()
73
+ ? 'You can see and use these pictures.'
74
+ : mine
75
+ ? 'You can change these pictures and upload to this group.'
76
+ : 'You can see and use these pictures. Ask the group’s own team to change their words or crops.'
77
+ const run = ++generation
78
+ try {
79
+ const found = await survey(getLibrary(), group)
80
+ if (run !== generation) return
81
+ counted = tally(found, new Date())
82
+ state.pics = found.items.slice(0, 5).map((item) => ({ id: item.id, thumb: thumbnailUrl(item.id) }))
83
+ draw()
84
+ } catch {
85
+ // The pictures below still load; the numbers are a courtesy.
86
+ }
87
+ }
88
+
89
+ function choose(tile) {
90
+ if (!tile.part) return
91
+ const now = { ...BLANK, ...(sentence.value || {}) }
92
+ sentence.value = { ...now, [tile.part]: tile.on ? '' : tile.value }
93
+ }
94
+
95
+ function back() {
96
+ tab.value = 'groups'
97
+ }
98
+
99
+ sentence.subscribe(look)
100
+
101
+ let lastChange = changed.value
102
+ changed.subscribe((value) => {
103
+ if (value === lastChange) return
104
+ lastChange = value
105
+ forgetSurveys()
106
+ look()
107
+ })
108
+
109
+ patched.subscribe((change) => {
110
+ if (!change || !state.group) return
111
+ forgetSurveys()
112
+ look()
113
+ })
114
+
115
+ onMounted(look)
116
+ </logic>
117
+
118
+ <style>
119
+ .cover {
120
+ display: flex;
121
+ flex-direction: column;
122
+ gap: 14px;
123
+ }
124
+
125
+ .back {
126
+ align-self: flex-start;
127
+ }
128
+
129
+ .banner {
130
+ position: relative;
131
+ border-radius: 16px;
132
+ overflow: hidden;
133
+ min-height: 200px;
134
+ background: #1b1b1d;
135
+ display: flex;
136
+ align-items: flex-end;
137
+ }
138
+
139
+ .mosaic {
140
+ position: absolute;
141
+ inset: 0;
142
+ display: grid;
143
+ grid-template-columns: 2fr 1fr 1fr;
144
+ grid-template-rows: 1fr 1fr;
145
+ gap: 2px;
146
+ }
147
+
148
+ .mosaic img {
149
+ width: 100%;
150
+ height: 100%;
151
+ min-height: 0;
152
+ object-fit: cover;
153
+ }
154
+
155
+ .mosaic img:first-child {
156
+ grid-row: 1 / 3;
157
+ }
158
+
159
+ .about {
160
+ position: relative;
161
+ width: 100%;
162
+ padding: 60px 24px 20px;
163
+ background: linear-gradient(transparent, rgba(0, 0, 0, .78));
164
+ color: #fff;
165
+ }
166
+
167
+ .about h1 {
168
+ font-size: clamp(28px, 3vw + 10px, 44px);
169
+ font-weight: 600;
170
+ letter-spacing: -.02em;
171
+ text-transform: capitalize;
172
+ }
173
+
174
+ .about p {
175
+ font-size: var(--px-fs-m);
176
+ opacity: .9;
177
+ max-width: 60ch;
178
+ }
179
+
180
+ .tiles {
181
+ display: grid;
182
+ grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
183
+ gap: 12px;
184
+ }
185
+
186
+ .tile {
187
+ display: flex;
188
+ flex-direction: column;
189
+ gap: 2px;
190
+ padding: 14px 16px;
191
+ border: 1px solid var(--px-border);
192
+ border-radius: 12px;
193
+ background: var(--px-surface);
194
+ color: var(--px-text);
195
+ font: inherit;
196
+ text-align: left;
197
+ cursor: pointer;
198
+ }
199
+
200
+ .tile:disabled {
201
+ cursor: default;
202
+ }
203
+
204
+ .tile:not(:disabled):hover {
205
+ border-color: var(--px-accent);
206
+ }
207
+
208
+ .tile[aria-pressed="true"] {
209
+ background: var(--px-accent-soft);
210
+ border-color: var(--px-accent);
211
+ color: var(--px-accent-text);
212
+ }
213
+
214
+ .tile b {
215
+ font-size: 24px;
216
+ letter-spacing: -.01em;
217
+ }
218
+
219
+ .tile span {
220
+ font-size: var(--px-fs-s);
221
+ color: var(--px-text-2);
222
+ }
223
+
224
+ .tile[aria-pressed="true"] span {
225
+ color: inherit;
226
+ }
227
+ </style>
@@ -0,0 +1,415 @@
1
+ <view>
2
+ <div class="desk">
3
+ <div class="lede">
4
+ <h1>Your desk</h1>
5
+ <p class="px-muted" aria-live="polite">{{ state.lede }}</p>
6
+ </div>
7
+
8
+ <div class="jobs" $show="state.jobs.length">
9
+ <div class="job" $for="job in state.jobs" $key="job.key" $class="{ 'is-lead': job.lead }">
10
+ <div class="mini" aria-hidden="true">
11
+ <img $for="pic in job.pics" $key="pic.id" $src="pic.thumb" alt="" loading="lazy" />
12
+ </div>
13
+ <h3>{{ job.title }}</h3>
14
+ <span class="px-subtle">{{ job.note }}</span>
15
+ <div class="px-row job-acts">
16
+ <button class="px-btn" type="button" $class="{ 'px-btn-primary': job.lead, 'px-btn-secondary': !job.lead }" @click="act(job, 'main')">{{ job.action }}</button>
17
+ <button class="px-btn px-btn-ghost see" type="button" $show="job.second" @click="act(job, 'second')">{{ job.second }}</button>
18
+ </div>
19
+ </div>
20
+ </div>
21
+
22
+ <section class="shelf" $for="shelf in state.shelves" $key="shelf.name" $aria-label="shelf.name">
23
+ <div class="shelf-h">
24
+ <h3>{{ shelf.name }}</h3>
25
+ <span class="px-subtle">{{ shelf.meta }}</span>
26
+ <button class="px-btn px-btn-ghost px-btn-sm open" type="button" @click="openGroup(shelf.name)">Open {{ shelf.name }} ›</button>
27
+ </div>
28
+ <div class="strip">
29
+ <button type="button" $for="pic in shelf.pics" $key="pic.id" $aria-label="pic.label" @click="edit(pic)">
30
+ <img $src="pic.thumb" alt="" loading="lazy" />
31
+ <i $show="pic.noAlt" title="Needs words"></i>
32
+ </button>
33
+ </div>
34
+ <p class="px-subtle" $show="!shelf.pics.length">No pictures yet.</p>
35
+ </section>
36
+
37
+ <div class="others" $show="state.others.length">
38
+ <h3>Other groups <span class="px-subtle">· you can see and use</span></h3>
39
+ <div class="px-row chips">
40
+ <button class="px-chip go" type="button" $for="one in state.others" $key="one" @click="openGroup(one)">{{ one }}</button>
41
+ </div>
42
+ </div>
43
+
44
+ <p class="px-subtle text-danger" role="alert" $show="state.error">{{ state.error }}</p>
45
+ </div>
46
+ </view>
47
+
48
+ <logic lang="ts">
49
+ import { reactive, onMounted, shared } from '@lightspeed-js/core'
50
+ import { getLibrary, thumbnailUrl, describedFor, canWrite, TAB, SENTENCE, SELECTED, RUN, CAPABILITIES, CHANGED, PATCHED } from '../config.ts'
51
+ import { BLANK } from '../sentence.ts'
52
+ import { survey, tally, atLeast, forgetSurveys } from '../survey.ts'
53
+ import { flagsOf } from '../checks.ts'
54
+
55
+ /**
56
+ * The library's home for people who look after a group: what needs them,
57
+ * and their groups' newest pictures. Counts cover only groups the person
58
+ * can change, and say "at least" where a group is too large to have been
59
+ * read to the end.
60
+ */
61
+ const STRIP = 8
62
+
63
+ const state = reactive({
64
+ lede: 'Looking over your groups…',
65
+ jobs: [],
66
+ shelves: [],
67
+ others: [],
68
+ error: '',
69
+ })
70
+
71
+ const tab = shared(TAB)
72
+ const sentence = shared(SENTENCE)
73
+ const selected = shared(SELECTED)
74
+ const run = shared(RUN)
75
+ const capabilities = shared(CAPABILITIES)
76
+ const changed = shared(CHANGED)
77
+ const patched = shared(PATCHED)
78
+
79
+ const plural = (n, one, many) => n + ' ' + (n === 1 ? one : many)
80
+
81
+ let mine = []
82
+ let needing = []
83
+ let generation = 0
84
+
85
+ const pic = (item, group) => ({
86
+ id: item.id,
87
+ thumb: thumbnailUrl(item.id),
88
+ label: (item.filename || item.id) + (describedFor(item, group).altText ? '' : ', needs words'),
89
+ noAlt: !describedFor(item, group).altText,
90
+ record: item,
91
+ })
92
+
93
+ async function look() {
94
+ const about = capabilities.value
95
+ if (!about) return
96
+ const writes = canWrite() ? (about.writeGroups && about.writeGroups.length ? about.writeGroups : about.writeGroup ? [about.writeGroup] : []) : []
97
+ mine = writes
98
+ state.others = (about.groups || []).filter((one) => !writes.includes(one))
99
+ if (!writes.length) {
100
+ state.jobs = []
101
+ state.shelves = []
102
+ state.lede = state.others.length ? 'You can see and use pictures from these groups.' : 'Nothing needs you right now.'
103
+ return
104
+ }
105
+ const now = new Date()
106
+ const run = ++generation
107
+ try {
108
+ const found = await Promise.all(writes.map((group) => survey(getLibrary(), group)))
109
+ if (run !== generation) return
110
+ const counted = found.map((one) => ({ group: one.group, items: one.items, ...tally(one, now) }))
111
+ const complete = counted.every((one) => one.complete)
112
+ // A picture in two of the person's groups is one picture to describe.
113
+ const seen = new Set()
114
+ needing = counted.flatMap((one) => one.needs.filter((item) => !seen.has(item.id) && seen.add(item.id)).map((item) => ({ item, group: one.group })))
115
+ seen.clear()
116
+ const week = counted.flatMap((one) => one.week.filter((item) => !seen.has(item.id) && seen.add(item.id)))
117
+ const groupsNeeding = counted.filter((one) => one.needs.length).map((one) => one.group)
118
+
119
+ const jobs = []
120
+ if (needing.length) {
121
+ jobs.push({
122
+ key: 'needs',
123
+ lead: true,
124
+ title: atLeast(needing.length, complete) + (needing.length === 1 ? ' picture needs words' : ' pictures need words'),
125
+ note: 'In ' + groupsNeeding.join(' and ') + '.',
126
+ action: 'Start describing',
127
+ second: 'See them',
128
+ pics: needing.slice(0, 8).map((one) => pic(one.item, one.group)),
129
+ })
130
+ }
131
+ if (week.length) {
132
+ jobs.push({
133
+ key: 'week',
134
+ lead: false,
135
+ title: plural(week.length, 'picture', 'pictures') + ' added this week',
136
+ note: writes.length > 1 ? 'Across your groups.' : 'In ' + writes[0] + '.',
137
+ action: 'Open them',
138
+ second: '',
139
+ pics: week.slice(0, 8).map((item) => pic(item, '')),
140
+ })
141
+ }
142
+ seen.clear()
143
+ const flagged = counted.flatMap((one) => one.items.filter((item) => !seen.has(item.id) && seen.add(item.id) && flagsOf(item, about.presets || []).length))
144
+ if (flagged.length) {
145
+ jobs.push({
146
+ key: 'flagged',
147
+ lead: false,
148
+ title: atLeast(flagged.length, complete) + (flagged.length === 1 ? ' picture has a quality flag' : ' pictures have quality flags'),
149
+ note: 'Too small for a shape the sites show them at.',
150
+ action: 'Review',
151
+ second: '',
152
+ pics: flagged.slice(0, 8).map((item) => pic(item, '')),
153
+ })
154
+ }
155
+ state.jobs = jobs
156
+ state.lede = needing.length
157
+ ? (needing.length === 1 ? 'One thing needs you' : 'A few things need you') + ' in ' + writes.join(' and ') + '.'
158
+ : 'Nothing needs you right now.'
159
+ state.shelves = counted.map((one) => ({
160
+ name: one.group,
161
+ meta: [
162
+ atLeast(one.count, one.complete) + (one.count === 1 ? ' picture' : ' pictures'),
163
+ one.needs.length ? atLeast(one.needs.length, one.complete) + (one.needs.length === 1 ? ' needs words' : ' need words') : '',
164
+ ].filter(Boolean).join(' · '),
165
+ pics: one.items.slice(0, STRIP).map((item) => pic(item, one.group)),
166
+ }))
167
+ state.error = ''
168
+ } catch (err) {
169
+ if (run !== generation) return
170
+ state.lede = ''
171
+ state.error = "Your groups didn't load: " + (err && err.message ? err.message : 'no answer from the library')
172
+ }
173
+ }
174
+
175
+ function showAll(parts) {
176
+ sentence.value = { ...BLANK, ...parts }
177
+ tab.value = 'all'
178
+ }
179
+
180
+ function act(job, which) {
181
+ if (job.key === 'needs' && which === 'main') {
182
+ run.value = {
183
+ items: needing.map(({ item, group }) => ({
184
+ ...item,
185
+ label: item.filename || item.id,
186
+ altText: describedFor(item, group).altText || '',
187
+ altView: '',
188
+ })),
189
+ }
190
+ return
191
+ }
192
+ if (job.key === 'needs') return showAll({ condition: 'needs', ...(mine.length === 1 ? { group: mine[0] } : {}) })
193
+ if (job.key === 'flagged') return showAll({ condition: 'flagged', ...(mine.length === 1 ? { group: mine[0] } : {}) })
194
+ showAll({ when: 'week', ...(mine.length === 1 ? { group: mine[0] } : {}) })
195
+ }
196
+
197
+ function openGroup(name) {
198
+ showAll({ group: name })
199
+ }
200
+
201
+ function edit(one) {
202
+ selected.value = JSON.parse(JSON.stringify(one.record))
203
+ }
204
+
205
+ let lastChange = changed.value
206
+ changed.subscribe((value) => {
207
+ if (value === lastChange) return
208
+ lastChange = value
209
+ forgetSurveys()
210
+ if (tab.value === 'desk') look()
211
+ })
212
+
213
+ // Words written anywhere change what the desk counts.
214
+ patched.subscribe((change) => {
215
+ if (!change) return
216
+ forgetSurveys()
217
+ if (tab.value === 'desk') look()
218
+ })
219
+
220
+ tab.subscribe((value) => {
221
+ if (value === 'desk') look()
222
+ })
223
+
224
+ capabilities.subscribe(() => {
225
+ if (tab.value === 'desk') look()
226
+ })
227
+
228
+ onMounted(() => {
229
+ if (tab.value === 'desk') look()
230
+ })
231
+ </logic>
232
+
233
+ <style>
234
+ .desk {
235
+ display: flex;
236
+ flex-direction: column;
237
+ gap: 28px;
238
+ }
239
+
240
+ .lede {
241
+ display: flex;
242
+ flex-direction: column;
243
+ gap: 6px;
244
+ }
245
+
246
+ .lede h1 {
247
+ font-size: clamp(26px, 2vw + 12px, 34px);
248
+ font-weight: 600;
249
+ letter-spacing: -.02em;
250
+ }
251
+
252
+ .lede p {
253
+ font-size: 16px;
254
+ }
255
+
256
+ .jobs {
257
+ display: grid;
258
+ grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
259
+ gap: 16px;
260
+ }
261
+
262
+ .job {
263
+ display: flex;
264
+ flex-direction: column;
265
+ gap: 12px;
266
+ padding: 18px;
267
+ border: 1px solid var(--px-border);
268
+ border-radius: 16px;
269
+ background: var(--px-surface);
270
+ }
271
+
272
+ .job.is-lead {
273
+ background: var(--px-text);
274
+ color: var(--px-bg);
275
+ border-color: var(--px-text);
276
+ }
277
+
278
+ .job.is-lead .px-subtle {
279
+ color: inherit;
280
+ opacity: .7;
281
+ }
282
+
283
+ .job.is-lead .see {
284
+ color: inherit;
285
+ }
286
+
287
+ .job h3 {
288
+ font-size: 19px;
289
+ letter-spacing: -.01em;
290
+ line-height: 1.25;
291
+ }
292
+
293
+ .job-acts {
294
+ gap: 8px;
295
+ margin-top: auto;
296
+ flex-wrap: wrap;
297
+ }
298
+
299
+ .mini {
300
+ display: grid;
301
+ grid-template-columns: repeat(4, 1fr);
302
+ grid-auto-rows: 46px;
303
+ gap: 4px;
304
+ height: 96px;
305
+ overflow: hidden;
306
+ }
307
+
308
+ .mini img {
309
+ width: 100%;
310
+ height: 100%;
311
+ object-fit: cover;
312
+ border-radius: var(--px-r-s);
313
+ background: var(--px-surface-2);
314
+ }
315
+
316
+ .shelf {
317
+ display: flex;
318
+ flex-direction: column;
319
+ gap: 12px;
320
+ }
321
+
322
+ .shelf-h {
323
+ display: flex;
324
+ align-items: baseline;
325
+ flex-wrap: wrap;
326
+ gap: 6px 12px;
327
+ }
328
+
329
+ .shelf-h h3 {
330
+ font-size: 18px;
331
+ }
332
+
333
+ .open {
334
+ margin-left: auto;
335
+ }
336
+
337
+ .strip {
338
+ display: grid;
339
+ grid-template-columns: repeat(8, 1fr);
340
+ gap: 8px;
341
+ }
342
+
343
+ .strip button {
344
+ position: relative;
345
+ aspect-ratio: 1;
346
+ border: 0;
347
+ padding: 0;
348
+ border-radius: var(--px-r-m);
349
+ overflow: hidden;
350
+ cursor: pointer;
351
+ background: var(--px-surface-2);
352
+ }
353
+
354
+ .strip img {
355
+ position: absolute;
356
+ inset: 0;
357
+ width: 100%;
358
+ height: 100%;
359
+ object-fit: cover;
360
+ }
361
+
362
+ .strip i {
363
+ position: absolute;
364
+ right: 6px;
365
+ top: 6px;
366
+ width: 8px;
367
+ height: 8px;
368
+ border-radius: 50%;
369
+ background: #f2bd57;
370
+ box-shadow: 0 0 0 2px rgba(0, 0, 0, .4);
371
+ }
372
+
373
+ .others {
374
+ display: flex;
375
+ flex-direction: column;
376
+ gap: 10px;
377
+ }
378
+
379
+ .others h3 {
380
+ font-size: var(--px-fs-m);
381
+ }
382
+
383
+ .others h3 .px-subtle {
384
+ font-weight: 400;
385
+ }
386
+
387
+ .chips {
388
+ gap: 8px;
389
+ flex-wrap: wrap;
390
+ }
391
+
392
+ .go {
393
+ padding: 0 14px;
394
+ min-height: 36px;
395
+ font: inherit;
396
+ font-size: var(--px-fs-s);
397
+ font-weight: 500;
398
+ color: var(--px-text);
399
+ cursor: pointer;
400
+ }
401
+
402
+ .go:hover {
403
+ border-color: var(--px-accent);
404
+ }
405
+
406
+ .text-danger {
407
+ color: var(--px-danger);
408
+ }
409
+
410
+ @media (max-width: 760px) {
411
+ .strip {
412
+ grid-template-columns: repeat(4, 1fr);
413
+ }
414
+ }
415
+ </style>