@pseudolux-ui/media-library 0.0.0-stage → 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,526 @@
1
+ <view>
2
+ <div class="px-scrim scrim" $show="state.open" @click="backdrop">
3
+ <div class="px-dialog bar" role="dialog" aria-modal="true" aria-label="Find or do anything" @keydown="onKey">
4
+ <div class="ask">
5
+ <button class="px-chip ctx" type="button" $show="state.context" title="Step back: act on nothing picked" @click="dropContext">{{ state.context }} <span aria-hidden="true">×</span></button>
6
+ <input
7
+ class="q"
8
+ type="text"
9
+ role="combobox"
10
+ aria-autocomplete="list"
11
+ aria-controls="command-list"
12
+ aria-expanded="true"
13
+ $aria-activedescendant="state.activeId"
14
+ $aria-label="state.prompt"
15
+ $placeholder="state.prompt"
16
+ autocomplete="off"
17
+ spellcheck="false"
18
+ $ref="input"
19
+ @input="onInput"
20
+ />
21
+ <span class="px-kbd">Esc</span>
22
+ </div>
23
+
24
+ <div class="list" id="command-list" role="listbox" aria-label="Commands" $show="!state.keysOpen" $ref="list">
25
+ <div class="sec" role="group" $for="section in state.sections" $key="section.title" $aria-label="section.title">
26
+ <div class="sec-h" aria-hidden="true">{{ section.title }}</div>
27
+ <div
28
+ class="cmd"
29
+ role="option"
30
+ $for="one in section.items"
31
+ $key="one.id"
32
+ $id="one.id"
33
+ $aria-selected="one.id === state.activeId"
34
+ @click="runById(one.id)"
35
+ @mousemove="hover(one.id)"
36
+ >
37
+ <span class="cmd-label">{{ one.label }}</span>
38
+ <span class="px-subtle" $show="one.hint">{{ one.hint }}</span>
39
+ <span class="px-kbd" $show="one.key">{{ one.key }}</span>
40
+ </div>
41
+ </div>
42
+ <p class="px-muted none" $show="!state.sections.length">Nothing to do with that.</p>
43
+ </div>
44
+
45
+ <div class="keys" $show="state.keysOpen">
46
+ <table>
47
+ <caption>Keys</caption>
48
+ <tbody>
49
+ <tr $for="row in state.keyRows" $key="row.what"><th scope="row">{{ row.what }}</th><td>{{ row.keys }}</td></tr>
50
+ </tbody>
51
+ </table>
52
+ <label class="letters"><input type="checkbox" $checked="state.lettersOn" @change="toggleLetters" /> Single-letter keys (they only work outside text boxes)</label>
53
+ <button class="px-btn px-btn-ghost px-btn-sm" type="button" @click="closeKeys">Back to commands</button>
54
+ </div>
55
+
56
+ <div class="foot" aria-hidden="true">
57
+ <span><span class="px-kbd">↑</span> <span class="px-kbd">↓</span> move</span>
58
+ <span><span class="px-kbd">Enter</span> do it</span>
59
+ <span $show="state.context"><span class="px-kbd">⌫</span> step back</span>
60
+ </div>
61
+ <span class="sr-only" role="status">{{ state.announce }}</span>
62
+ </div>
63
+ </div>
64
+ </view>
65
+
66
+ <logic lang="ts">
67
+ import { reactive, onMounted, onUnmounted, shared, defineRefs } from '@lightspeed-js/core'
68
+ import { canWrite, letterKeys, setLetterKeys, TAB, SENTENCE, SELECTED, PICKED, SET, CAPABILITIES, COMMAND, UPLOAD, OWN_BIN } from '../config.ts'
69
+ import { BLANK, parse, said } from '../sentence.ts'
70
+
71
+ /**
72
+ * One box that finds a picture or does a thing, from anywhere in the
73
+ * library. With pictures picked it acts on them first; Backspace in the
74
+ * empty box steps back to acting on nothing.
75
+ */
76
+ const refs = defineRefs()
77
+
78
+ const SAVED_KEY = 'pseudolux:sentences'
79
+ const REASONS = ['Duplicate', 'Poor quality', 'Rights have ended', 'No longer needed']
80
+
81
+ const KEY_ROWS = [
82
+ { what: 'Find or do anything', keys: '⌘K or Ctrl K' },
83
+ { what: 'Search here', keys: '/' },
84
+ { what: 'Move between pictures', keys: 'Arrow keys, Home, End' },
85
+ { what: 'Peek, quick look, open the editor', keys: 'Click, Space, Enter' },
86
+ { what: 'Pick, pick a range, pick all', keys: 'X, Shift X, ⌘A' },
87
+ { what: 'Label the picked', keys: 'L' },
88
+ { what: 'Describe one by one', keys: 'D' },
89
+ { what: 'Get a link', keys: 'Shift L' },
90
+ { what: 'Move the picked to the bin', keys: 'Backspace' },
91
+ { what: 'Upload', keys: 'U' },
92
+ { what: 'Go to Desk, All pictures, Groups', keys: 'G then D, A, G' },
93
+ { what: 'Close, or clear picks', keys: 'Esc' },
94
+ ]
95
+
96
+ const state = reactive({
97
+ open: false,
98
+ context: '',
99
+ prompt: 'Find a picture or do something',
100
+ sections: [],
101
+ activeId: '',
102
+ keysOpen: false,
103
+ keyRows: KEY_ROWS,
104
+ lettersOn: true,
105
+ announce: '',
106
+ })
107
+
108
+ const tab = shared(TAB)
109
+ const sentence = shared(SENTENCE)
110
+ const selected = shared(SELECTED)
111
+ const picked = shared(PICKED)
112
+ const set = shared(SET)
113
+ const capabilities = shared(CAPABILITIES)
114
+ const command = shared(COMMAND)
115
+ const upload = shared(UPLOAD)
116
+ const ownBin = shared(OWN_BIN)
117
+
118
+ const plural = (n, one, many) => n + ' ' + (n === 1 ? one : many)
119
+
120
+ let query = ''
121
+ // A command that needs one more answer: 'label' or 'bin'.
122
+ let step = ''
123
+ let useContext = true
124
+ let actions = new Map()
125
+ let order = []
126
+ let opener = null
127
+
128
+ const pickedNow = () => (useContext ? picked.value || [] : [])
129
+
130
+ function savedSentences() {
131
+ try {
132
+ const kept = JSON.parse(localStorage.getItem(SAVED_KEY) || '[]')
133
+ return Array.isArray(kept) ? kept.filter((one) => one && one.name && one.sentence) : []
134
+ } catch {
135
+ return []
136
+ }
137
+ }
138
+
139
+ function ask(name, arg) {
140
+ command.value = { name, arg, at: Date.now() }
141
+ }
142
+
143
+ function go(where) {
144
+ tab.value = where
145
+ }
146
+
147
+ function build() {
148
+ const about = capabilities.value || {}
149
+ const groups = about.groups || []
150
+ const ids = pickedNow()
151
+ const n = ids.length
152
+ const words = query.trim().toLowerCase()
153
+ const sections = []
154
+ actions = new Map()
155
+ let serial = 0
156
+ const item = (label, run, extra = {}) => {
157
+ const id = 'command-' + ++serial
158
+ actions.set(id, run)
159
+ return { id, label, hint: '', key: '', ...extra }
160
+ }
161
+ const matches = (label) => !words || words.split(/\s+/).every((word) => label.toLowerCase().includes(word))
162
+
163
+ state.context = n ? n + ' picked' : ''
164
+
165
+ if (step === 'label') {
166
+ state.prompt = 'Label for ' + plural(n, 'picture', 'pictures')
167
+ const term = query.trim()
168
+ sections.push({ title: 'Add a label', items: term ? [item('Add label “' + term + '” to ' + plural(n, 'picture', 'pictures'), () => ask('label', term))] : [] })
169
+ } else if (step === 'bin') {
170
+ state.prompt = 'Why are they going in the bin?'
171
+ sections.push({ title: 'Move ' + plural(n, 'picture', 'pictures') + ' to the bin', items: REASONS.filter(matches).map((reason) => item(reason, () => ask('bin', reason))) })
172
+ } else {
173
+ state.prompt = n ? 'Do something with the ' + n + ' picked, or find a picture' : 'Find a picture or do something'
174
+ // "label eofy" is the whole command in one line.
175
+ const shorthand = /^label\s+(.+)$/i.exec(query.trim())
176
+
177
+ if (n && canWrite()) {
178
+ const forPicked = []
179
+ if (shorthand && about.describe) forPicked.push(item('Add label “' + shorthand[1].trim() + '” to ' + plural(n, 'picture', 'pictures'), () => ask('label', shorthand[1].trim())))
180
+ if (about.describe && matches('describe one by one')) forPicked.push(item('Describe one by one', () => ask('describe'), { key: 'D' }))
181
+ if (about.describe && matches('add a label') && !shorthand) forPicked.push(item('Add a label…', () => enter('label'), { key: 'L' }))
182
+ if (about.remove && matches('move to bin')) forPicked.push(item('Move to bin…', () => enter('bin'), { key: '⌫' }))
183
+ if (matches('clear picks')) forPicked.push(item('Clear picks', () => ask('clear')))
184
+ if (forPicked.length) sections.push({ title: 'For the ' + n + ' picked', items: forPicked })
185
+ }
186
+
187
+ const places = [
188
+ ['Desk', () => go('desk'), 'G D'],
189
+ ['All pictures', () => { sentence.value = { ...BLANK }; go('all') }, 'G A'],
190
+ ['Groups', () => go('groups'), 'G G'],
191
+ ]
192
+ if (about.remove && canWrite() && ownBin.value) places.push(['Bin', () => go('bin'), ''])
193
+ const goTo = places.filter(([label]) => matches(label)).map(([label, run, key]) => item(label, run, { key }))
194
+ for (const group of groups) {
195
+ if (matches(group)) goTo.push(item(group, () => { sentence.value = { ...BLANK, group }; go('all') }, { hint: 'Group' }))
196
+ }
197
+ for (const kept of savedSentences()) {
198
+ if (matches(kept.name)) goTo.push(item(kept.name, () => { sentence.value = { ...BLANK, ...kept.sentence }; go('all') }, { hint: 'Saved sentence' }))
199
+ }
200
+ if (goTo.length) sections.push({ title: 'Go to', items: goTo.slice(0, 9) })
201
+
202
+ const doing = []
203
+ if (about.upload && canWrite() && matches('upload pictures')) doing.push(item('Upload pictures…', () => (upload.value = (upload.value || 0) + 1), { key: 'U' }))
204
+ if (canWrite() && matches('pictures that need words')) doing.push(item('See the pictures that need words', () => { sentence.value = { ...BLANK, condition: 'needs' }; go('all') }))
205
+ if (matches('keyboard shortcuts keys')) doing.push(item('Keyboard shortcuts', () => openKeys(), {}))
206
+ if (doing.length) sections.push({ title: 'Do', items: doing })
207
+
208
+ if (words) {
209
+ const found = ((set.value && set.value.items) || []).filter((one) => matches(one.label)).slice(0, 6)
210
+ if (found.length) {
211
+ sections.push({ title: 'Pictures', items: found.map((one) => item(one.label, () => (selected.value = { id: one.id, ...(one.record || {}) }), { hint: one.noAlt ? 'Needs words' : '' })) })
212
+ }
213
+ const asked = parse(query, { groups })
214
+ const only = asked.about && !asked.shape && !asked.group && !asked.condition && !asked.when
215
+ const label = only ? 'Search pictures for “' + asked.about + '”' : 'Show: ' + said(asked).replace(/\.$/, '')
216
+ sections.push({ title: 'Search', items: [item(label, () => { sentence.value = asked; go('all') })] })
217
+ }
218
+ }
219
+
220
+ state.sections = sections.filter((one) => one.items.length)
221
+ order = state.sections.flatMap((one) => one.items.map((each) => each.id))
222
+ if (!order.includes(state.activeId)) state.activeId = order[0] || ''
223
+ }
224
+
225
+ function enter(next) {
226
+ step = next
227
+ query = ''
228
+ if (refs.input) refs.input.value = ''
229
+ build()
230
+ // Stays open for the second answer.
231
+ return true
232
+ }
233
+
234
+ function runById(id) {
235
+ const run = actions.get(id)
236
+ if (!run) return
237
+ const stay = run()
238
+ if (stay !== true) close()
239
+ }
240
+
241
+ function hover(id) {
242
+ if (state.activeId !== id) state.activeId = id
243
+ }
244
+
245
+ function onInput(event) {
246
+ query = event.target.value
247
+ state.activeId = ''
248
+ build()
249
+ }
250
+
251
+ function dropContext() {
252
+ useContext = false
253
+ step = ''
254
+ build()
255
+ if (refs.input) refs.input.focus()
256
+ }
257
+
258
+ function openKeys() {
259
+ state.lettersOn = letterKeys()
260
+ state.keysOpen = true
261
+ return true
262
+ }
263
+
264
+ function closeKeys() {
265
+ state.keysOpen = false
266
+ if (refs.input) refs.input.focus()
267
+ }
268
+
269
+ function toggleLetters(event) {
270
+ setLetterKeys(event.target.checked)
271
+ state.lettersOn = event.target.checked
272
+ }
273
+
274
+ function move(by) {
275
+ if (!order.length) return
276
+ const at = order.indexOf(state.activeId)
277
+ state.activeId = order[(at + by + order.length) % order.length]
278
+ requestAnimationFrame(() => {
279
+ const row = document.getElementById(state.activeId)
280
+ if (row && row.scrollIntoView) row.scrollIntoView({ block: 'nearest' })
281
+ })
282
+ }
283
+
284
+ function onKey(event) {
285
+ event.stopPropagation()
286
+ if (event.key === 'Escape') {
287
+ event.preventDefault()
288
+ if (state.keysOpen) return closeKeys()
289
+ if (step) {
290
+ step = ''
291
+ build()
292
+ return
293
+ }
294
+ close()
295
+ } else if (state.keysOpen) {
296
+ return
297
+ } else if (event.key === 'ArrowDown') {
298
+ event.preventDefault()
299
+ move(1)
300
+ } else if (event.key === 'ArrowUp') {
301
+ event.preventDefault()
302
+ move(-1)
303
+ } else if (event.key === 'Enter') {
304
+ event.preventDefault()
305
+ if (state.activeId) runById(state.activeId)
306
+ } else if (event.key === 'Backspace' && !query) {
307
+ if (step) {
308
+ event.preventDefault()
309
+ step = ''
310
+ build()
311
+ } else if (pickedNow().length) {
312
+ event.preventDefault()
313
+ dropContext()
314
+ }
315
+ } else if (event.key === 'Tab') {
316
+ // One box and its list; focus stays in the box.
317
+ if (!state.keysOpen) event.preventDefault()
318
+ }
319
+ }
320
+
321
+ function open() {
322
+ if (state.open) return
323
+ opener = document.activeElement
324
+ query = ''
325
+ step = ''
326
+ useContext = true
327
+ state.keysOpen = false
328
+ state.activeId = ''
329
+ state.open = true
330
+ build()
331
+ requestAnimationFrame(() => {
332
+ if (!refs.input) return
333
+ refs.input.value = ''
334
+ refs.input.focus()
335
+ })
336
+ }
337
+
338
+ function close() {
339
+ if (!state.open) return
340
+ state.open = false
341
+ // Whatever the command opened takes focus itself; otherwise it goes back.
342
+ requestAnimationFrame(() => {
343
+ const taken = document.activeElement && document.activeElement !== document.body && !document.activeElement.closest('.bar')
344
+ if (!taken && opener && document.contains(opener) && opener.focus) opener.focus()
345
+ opener = null
346
+ })
347
+ }
348
+
349
+ function backdrop(event) {
350
+ if (event.target && event.target.classList && event.target.classList.contains('scrim')) close()
351
+ }
352
+
353
+ function onShortcut(event) {
354
+ if ((event.metaKey || event.ctrlKey) && (event.key === 'k' || event.key === 'K')) {
355
+ event.preventDefault()
356
+ if (state.open) close()
357
+ else open()
358
+ }
359
+ }
360
+
361
+ onMounted(() => {
362
+ document.addEventListener('keydown', onShortcut)
363
+ })
364
+
365
+ onUnmounted(() => {
366
+ document.removeEventListener('keydown', onShortcut)
367
+ })
368
+ </logic>
369
+
370
+ <style>
371
+ .scrim {
372
+ position: fixed;
373
+ inset: 0;
374
+ z-index: 70;
375
+ display: flex;
376
+ align-items: flex-start;
377
+ justify-content: center;
378
+ padding: 12vh 16px 16px;
379
+ background: var(--px-scrim);
380
+ }
381
+
382
+ .bar {
383
+ width: min(640px, 100%);
384
+ max-height: 70vh;
385
+ color: var(--px-text);
386
+ }
387
+
388
+ .ask {
389
+ display: flex;
390
+ align-items: center;
391
+ gap: 10px;
392
+ padding: 12px 16px;
393
+ border-bottom: 1px solid var(--px-border);
394
+ }
395
+
396
+ .ctx {
397
+ flex: none;
398
+ padding: 0 10px;
399
+ background: var(--px-accent-soft);
400
+ border-color: var(--px-accent);
401
+ color: var(--px-accent-text);
402
+ font: inherit;
403
+ font-size: var(--px-fs-s);
404
+ font-weight: 600;
405
+ cursor: pointer;
406
+ }
407
+
408
+ .q {
409
+ flex: 1;
410
+ min-width: 0;
411
+ min-height: var(--px-target);
412
+ border: 0;
413
+ background: transparent;
414
+ font: inherit;
415
+ font-size: 17px;
416
+ color: var(--px-text);
417
+ outline: none;
418
+ }
419
+
420
+ .list {
421
+ overflow: auto;
422
+ padding: 6px;
423
+ }
424
+
425
+ .sec-h {
426
+ padding: 10px 10px 4px;
427
+ font-size: var(--px-fs-xs);
428
+ font-weight: 600;
429
+ letter-spacing: .06em;
430
+ text-transform: uppercase;
431
+ color: var(--px-text-3);
432
+ }
433
+
434
+ .cmd {
435
+ display: flex;
436
+ align-items: center;
437
+ gap: 10px;
438
+ min-height: var(--px-target);
439
+ padding: 0 10px;
440
+ border-radius: var(--px-r-m);
441
+ cursor: pointer;
442
+ }
443
+
444
+ .cmd[aria-selected="true"] {
445
+ background: var(--px-accent-soft);
446
+ color: var(--px-accent-text);
447
+ }
448
+
449
+ .cmd-label {
450
+ flex: 1;
451
+ min-width: 0;
452
+ overflow: hidden;
453
+ text-overflow: ellipsis;
454
+ white-space: nowrap;
455
+ }
456
+
457
+ .none {
458
+ padding: 16px 10px;
459
+ }
460
+
461
+ .keys {
462
+ display: flex;
463
+ flex-direction: column;
464
+ gap: 12px;
465
+ padding: 14px 16px;
466
+ overflow: auto;
467
+ }
468
+
469
+ .keys table {
470
+ width: 100%;
471
+ border-collapse: collapse;
472
+ font-size: var(--px-fs-s);
473
+ }
474
+
475
+ .keys caption {
476
+ text-align: left;
477
+ font-weight: 600;
478
+ font-size: var(--px-fs-m);
479
+ padding-bottom: 6px;
480
+ }
481
+
482
+ .keys th {
483
+ text-align: left;
484
+ font-weight: 400;
485
+ padding: 6px 12px 6px 0;
486
+ }
487
+
488
+ .keys td {
489
+ text-align: right;
490
+ color: var(--px-text-2);
491
+ white-space: nowrap;
492
+ }
493
+
494
+ .keys tr + tr {
495
+ border-top: 1px solid var(--px-border);
496
+ }
497
+
498
+ .letters {
499
+ display: flex;
500
+ align-items: center;
501
+ gap: 8px;
502
+ font-size: var(--px-fs-s);
503
+ }
504
+
505
+ .keys .px-btn {
506
+ align-self: flex-start;
507
+ }
508
+
509
+ .foot {
510
+ display: flex;
511
+ gap: 16px;
512
+ padding: 8px 16px;
513
+ border-top: 1px solid var(--px-border);
514
+ font-size: var(--px-fs-xs);
515
+ color: var(--px-text-3);
516
+ }
517
+
518
+ .sr-only {
519
+ position: absolute;
520
+ width: 1px;
521
+ height: 1px;
522
+ overflow: hidden;
523
+ clip-path: inset(50%);
524
+ white-space: nowrap;
525
+ }
526
+ </style>