@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,428 @@
1
+ <view>
2
+ <div class="exp" role="region" $aria-label="'Peek: ' + state.label" $style="state.arrowStyle" @keydown="onKey">
3
+ <div class="stage">
4
+ <img class="big" $src="state.preview" $style="state.bigStyle" alt="" />
5
+ </div>
6
+ <div class="info">
7
+ <div class="px-row head">
8
+ <div class="titles">
9
+ <b class="name">{{ state.label }}</b>
10
+ <p class="px-subtle">{{ state.meta }}</p>
11
+ </div>
12
+ <button class="px-btn px-btn-ghost px-btn-icon px-btn-sm" type="button" aria-label="Previous picture" $disabled="!props.prev" @click="go(props.prev)"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m15 18-6-6 6-6"></path></svg></button>
13
+ <button class="px-btn px-btn-ghost px-btn-icon px-btn-sm" type="button" aria-label="Next picture" $disabled="!props.next" @click="go(props.next)"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m9 18 6-6-6-6"></path></svg></button>
14
+ <button class="px-btn px-btn-ghost px-btn-icon px-btn-sm" type="button" aria-label="Close peek" $show="!props.picking" @click="close"><svg width="16" height="16" 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>
15
+ </div>
16
+
17
+ <div class="px-callout px-callout-danger" $show="state.binned">{{ state.binnedNote }}</div>
18
+
19
+ <div class="px-callout" $for="flag in state.flags" $key="flag.label" $class="{ 'px-callout-danger': flag.tone === 'danger', 'px-callout-warn': flag.tone === 'warn' }">
20
+ <span><b>{{ flag.label }}.</b> {{ flag.detail }} {{ flag.fix }}</span>
21
+ </div>
22
+
23
+ <div class="px-field" $show="!state.binned">
24
+ <label class="px-label" $htmlFor="'peek-alt-' + state.id">Alt text</label>
25
+ <textarea class="px-textarea alt" $id="'peek-alt-' + state.id" $if="state.editable" placeholder="What is in this picture?" $ref="alt" @input="onAlt" @blur="saveAlt"></textarea>
26
+ <p class="read" $if="!state.editable">{{ state.alt || 'No alt text yet.' }}</p>
27
+ <span class="px-hint" $class="{ 'text-danger': state.saveFailed }">{{ state.hint }}</span>
28
+ </div>
29
+
30
+ <div class="crops" $show="state.cuts.length">
31
+ <span class="lbl">Crops</span>
32
+ <div class="minis">
33
+ <figure $for="cut in state.cuts" $key="cut.name">
34
+ <span class="cfr" $style="cut.style"></span>
35
+ {{ cut.label }}
36
+ </figure>
37
+ </div>
38
+ </div>
39
+
40
+ <p class="used" $show="state.usageKnown">
41
+ <span class="px-subtle">{{ state.usageLead }}</span>
42
+ <span $for="page in state.pages" $key="page.key"><a $if="page.url" $href="page.url" target="_blank" rel="noopener">{{ page.title }}</a><span $if="!page.url">{{ page.title }}</span>{{ page.after }}</span>
43
+ </p>
44
+
45
+ <div class="px-row acts">
46
+ <span class="px-badge px-badge-accent" $show="props.picking">Chosen</span>
47
+ <button class="px-btn px-btn-sm" type="button" $class="{ 'px-btn-primary': !props.picking, 'px-btn-secondary': props.picking }" @click="edit">{{ state.binned ? 'Open' : 'Open editor' }} <span class="px-kbd key" $show="!props.picking">Enter</span></button>
48
+ <button class="px-btn px-btn-secondary px-btn-sm" type="button" $show="!state.binned" @click="copy">{{ state.copyLabel }}</button>
49
+ <button class="px-btn px-btn-ghost px-btn-sm" type="button" $show="!state.binned" @click="getLink">Get a link…</button>
50
+ <button class="px-btn px-btn-ghost px-btn-sm" type="button" $show="!props.picking" $aria-pressed="props.picked" @click="pick">{{ props.picked ? 'Picked' : 'Pick' }}</button>
51
+ </div>
52
+ </div>
53
+ </div>
54
+ </view>
55
+
56
+ <logic lang="ts">
57
+ import { reactive, onMounted, onUpdate, shared, defineProps, defineRefs } from '@lightspeed-js/core'
58
+ import { presetRect } from '@pseudolux-ui/client'
59
+ import { getLibrary, previewUrl, canDescribeView, SELECTED, PEEK, PICKED, PATCHED, PRESETS, LINK } from '../config.ts'
60
+ import { cutStyle } from '../cuts.ts'
61
+
62
+ /**
63
+ * A closer look at one picture without leaving the wall: it opens under the
64
+ * row the picture is in. Enough to check it, give it words and move on; the
65
+ * editor is one step further for everything else.
66
+ */
67
+ const props = defineProps({
68
+ /** The picture, as the wall presents it. */
69
+ asset: { default: null },
70
+ prev: { default: '' },
71
+ next: { default: '' },
72
+ picked: { default: false },
73
+ /** Where the picture's middle is, in pixels from the left of the wall. */
74
+ arrow: { default: 80 },
75
+ /** Inside a picker, where the picture peeked at is the one chosen. */
76
+ picking: { default: false },
77
+ })
78
+
79
+ const refs = defineRefs()
80
+
81
+ const state = reactive({
82
+ id: '',
83
+ label: '',
84
+ meta: '',
85
+ preview: '',
86
+ bigStyle: '',
87
+ arrowStyle: '',
88
+ alt: '',
89
+ editable: false,
90
+ hint: '',
91
+ saveFailed: false,
92
+ binned: false,
93
+ binnedNote: '',
94
+ cuts: [],
95
+ flags: [],
96
+ usageKnown: false,
97
+ usageLead: '',
98
+ pages: [],
99
+ copyLabel: 'Copy link',
100
+ })
101
+
102
+ const selected = shared(SELECTED)
103
+ const peek = shared(PEEK)
104
+ const pickedCell = shared(PICKED)
105
+ const patched = shared(PATCHED)
106
+ const presets = shared(PRESETS)
107
+ const link = shared(LINK)
108
+
109
+ const plural = (n, one, many) => n + ' ' + (n === 1 ? one : many)
110
+
111
+ let saved = ''
112
+ let usageFor = ''
113
+
114
+ function show() {
115
+ const asset = props.asset
116
+ state.arrowStyle = '--arrow: ' + Math.max(18, Math.round(props.arrow) - 8) + 'px'
117
+ if (!asset) return
118
+ const fresh = asset.id !== state.id
119
+ state.id = asset.id
120
+ state.label = asset.label
121
+ state.binned = Boolean(asset.binned)
122
+ state.flags = asset.flags || []
123
+ state.binnedNote = 'In the bin' + (asset.deletedReason ? ' · “' + asset.deletedReason + '”' : '') + '. Open it to restore it.'
124
+ state.meta = [
125
+ asset.width && asset.height ? asset.width + ' × ' + asset.height : '',
126
+ asset.format ? String(asset.format).toUpperCase() : '',
127
+ (asset.tags || []).join(', '),
128
+ ].filter(Boolean).join(' · ')
129
+ state.preview = previewUrl(asset.id)
130
+ state.bigStyle = 'aspect-ratio: ' + (asset.width && asset.height ? asset.width + ' / ' + asset.height : '3 / 2')
131
+ state.editable = !asset.whyViewOnly && !asset.binned && (!asset.altView || canDescribeView(asset.altView))
132
+ if (fresh) {
133
+ state.alt = asset.altText || ''
134
+ saved = state.alt
135
+ // Written to the box itself: a textarea keeps what was typed into it
136
+ // over whatever a new render says its value is.
137
+ requestAnimationFrame(() => {
138
+ if (refs.alt && state.id === asset.id) refs.alt.value = state.alt
139
+ })
140
+ state.saveFailed = false
141
+ state.copyLabel = 'Copy link'
142
+ loadUsage(asset.id)
143
+ describeHint(asset)
144
+ }
145
+ drawCuts(asset)
146
+ }
147
+
148
+ function describeHint(asset) {
149
+ state.hint = asset.whyViewOnly
150
+ ? asset.whyViewOnly
151
+ : asset.altView
152
+ ? 'Just for ' + asset.altView + '. Saved when you leave the box.'
153
+ : 'Shared on every site that uses it. Saved when you leave the box.'
154
+ }
155
+
156
+ function drawCuts(asset) {
157
+ const list = presets.value || []
158
+ if (!asset.width || !asset.height || asset.binned) {
159
+ state.cuts = []
160
+ return
161
+ }
162
+ const picture = { dimensions: { width: asset.width, height: asset.height }, ...(asset.focal ? { focal: asset.focal } : {}) }
163
+ state.cuts = list.map((preset) => {
164
+ const stored = asset.crops && asset.crops[preset.name]
165
+ return {
166
+ name: preset.name,
167
+ label: preset.label + (stored ? ' · adjusted' : ''),
168
+ style: cutStyle(stored || presetRect(picture, preset), asset, state.preview, { height: 48, maxWidth: 110 }),
169
+ }
170
+ })
171
+ }
172
+
173
+ async function loadUsage(id) {
174
+ const port = getLibrary()
175
+ state.usageKnown = false
176
+ state.pages = []
177
+ if (typeof port.usage !== 'function') return
178
+ usageFor = id
179
+ try {
180
+ const usage = await port.usage(id)
181
+ if (usageFor !== id) return
182
+ const seen = new Set()
183
+ const pages = (usage.placements || []).filter((one) => {
184
+ const key = one.site + '|' + one.page
185
+ if (seen.has(key)) return false
186
+ seen.add(key)
187
+ return true
188
+ })
189
+ const some = pages.slice(0, 4)
190
+ state.pages = some.map((one, index) => ({
191
+ key: one.site + '|' + one.page,
192
+ title: one.title || one.page,
193
+ url: one.url || '',
194
+ after: index < some.length - 1 ? ', ' : pages.length > some.length ? ' and ' + (pages.length - some.length) + ' more' : '',
195
+ }))
196
+ state.usageLead = pages.length ? 'Used on ' : 'Not used on any page.'
197
+ state.usageKnown = true
198
+ } catch {
199
+ // Usage is a courtesy; the peek is still about the picture.
200
+ }
201
+ }
202
+
203
+ function onAlt(event) {
204
+ state.alt = event.target.value
205
+ }
206
+
207
+ async function saveAlt() {
208
+ const asset = props.asset
209
+ const value = state.alt.trim()
210
+ if (!asset || !state.editable || value === saved) return
211
+ const id = asset.id
212
+ const target = asset.altView || ''
213
+ state.hint = 'Saving…'
214
+ state.saveFailed = false
215
+ try {
216
+ await getLibrary().describe(id, { altText: value || null }, target ? { view: target } : undefined)
217
+ saved = value
218
+ patched.value = { id, patch: target ? { views: { [target.trim().toLowerCase()]: { altText: value } } } : { altText: value || undefined } }
219
+ if (state.id === id) state.hint = 'Saved'
220
+ } catch (err) {
221
+ const why = err && err.message ? err.message : 'no answer from the library'
222
+ if (state.id === id) {
223
+ state.hint = "Couldn't save: " + why + '. Your words are still in the box.'
224
+ state.saveFailed = true
225
+ }
226
+ }
227
+ }
228
+
229
+ function go(id) {
230
+ if (id) peek.value = id
231
+ }
232
+
233
+ function close() {
234
+ peek.value = null
235
+ }
236
+
237
+ function edit() {
238
+ if (props.asset) selected.value = JSON.parse(JSON.stringify(props.asset))
239
+ }
240
+
241
+ function pick() {
242
+ if (!props.asset) return
243
+ const id = props.asset.id
244
+ const now = pickedCell.value || []
245
+ pickedCell.value = now.includes(id) ? now.filter((one) => one !== id) : now.concat([id])
246
+ }
247
+
248
+ function getLink() {
249
+ if (props.asset) link.value = JSON.parse(JSON.stringify(props.asset))
250
+ }
251
+
252
+ async function copy() {
253
+ const asset = props.asset
254
+ if (!asset) return
255
+ const port = getLibrary()
256
+ const url = new URL(port.imageUrl(asset.id, {}), location.href).href
257
+ try {
258
+ await navigator.clipboard.writeText(url)
259
+ state.copyLabel = 'Copied'
260
+ if (typeof port.copied === 'function') port.copied(asset.id).catch(() => {})
261
+ } catch {
262
+ state.copyLabel = "Couldn't copy"
263
+ }
264
+ }
265
+
266
+ function onKey(event) {
267
+ // In a picker, Escape belongs to the dialog around the library.
268
+ if (event.key === 'Escape' && !props.picking) {
269
+ event.preventDefault()
270
+ event.stopPropagation()
271
+ close()
272
+ return
273
+ }
274
+ // Typing in the words is typing, not steering the wall.
275
+ if (event.target && event.target.tagName === 'TEXTAREA') event.stopPropagation()
276
+ }
277
+
278
+ presets.subscribe(() => {
279
+ if (props.asset) drawCuts(props.asset)
280
+ })
281
+
282
+ onUpdate(show)
283
+ onMounted(show)
284
+ </logic>
285
+
286
+ <style>
287
+ .exp {
288
+ position: relative;
289
+ display: grid;
290
+ grid-template-columns: minmax(0, 1.5fr) minmax(300px, 1fr);
291
+ gap: 28px;
292
+ padding: 22px;
293
+ margin: 10px 0 10px;
294
+ border-radius: 14px;
295
+ background: var(--px-surface);
296
+ border: 1px solid var(--px-border);
297
+ box-shadow: 0 14px 40px rgba(0, 0, 0, .12);
298
+ }
299
+
300
+ .exp::before {
301
+ content: "";
302
+ position: absolute;
303
+ top: -9px;
304
+ left: var(--arrow, 80px);
305
+ width: 16px;
306
+ height: 16px;
307
+ transform: rotate(45deg);
308
+ background: var(--px-surface);
309
+ border-left: 1px solid var(--px-border);
310
+ border-top: 1px solid var(--px-border);
311
+ }
312
+
313
+ .stage {
314
+ display: flex;
315
+ align-items: flex-start;
316
+ justify-content: center;
317
+ min-width: 0;
318
+ }
319
+
320
+ .big {
321
+ display: block;
322
+ max-width: 100%;
323
+ max-height: 420px;
324
+ border-radius: var(--px-r-m);
325
+ background: var(--px-surface-2);
326
+ object-fit: contain;
327
+ }
328
+
329
+ .info {
330
+ display: flex;
331
+ flex-direction: column;
332
+ gap: 14px;
333
+ min-width: 0;
334
+ }
335
+
336
+ .head {
337
+ gap: 4px;
338
+ align-items: flex-start;
339
+ flex-wrap: nowrap;
340
+ }
341
+
342
+ .titles {
343
+ flex: 1;
344
+ min-width: 0;
345
+ }
346
+
347
+ .name {
348
+ display: block;
349
+ font-size: 18px;
350
+ overflow-wrap: anywhere;
351
+ }
352
+
353
+ .alt {
354
+ min-height: 76px;
355
+ }
356
+
357
+ .read {
358
+ margin: 0;
359
+ color: var(--px-text-2);
360
+ }
361
+
362
+ .crops {
363
+ display: flex;
364
+ flex-direction: column;
365
+ gap: 6px;
366
+ }
367
+
368
+ .lbl {
369
+ font-size: 12px;
370
+ font-weight: 600;
371
+ color: var(--px-text-3);
372
+ letter-spacing: .04em;
373
+ text-transform: uppercase;
374
+ }
375
+
376
+ .minis {
377
+ display: flex;
378
+ flex-wrap: wrap;
379
+ gap: 8px;
380
+ align-items: flex-end;
381
+ }
382
+
383
+ .minis figure {
384
+ margin: 0;
385
+ display: flex;
386
+ flex-direction: column;
387
+ gap: 4px;
388
+ font-size: 11px;
389
+ font-weight: 600;
390
+ color: var(--px-text-2);
391
+ }
392
+
393
+ .cfr {
394
+ display: block;
395
+ border-radius: var(--px-r-s);
396
+ background-color: var(--px-surface-2);
397
+ background-repeat: no-repeat;
398
+ }
399
+
400
+ .used {
401
+ margin: 0;
402
+ font-size: var(--px-fs-s);
403
+ }
404
+
405
+ .acts {
406
+ gap: 8px;
407
+ margin-top: auto;
408
+ flex-wrap: wrap;
409
+ }
410
+
411
+ .key {
412
+ background: transparent;
413
+ color: inherit;
414
+ border-color: currentColor;
415
+ }
416
+
417
+ .text-danger {
418
+ color: var(--px-danger);
419
+ }
420
+
421
+ @media (max-width: 900px) {
422
+ .exp {
423
+ grid-template-columns: minmax(0, 1fr);
424
+ gap: 16px;
425
+ padding: 16px;
426
+ }
427
+ }
428
+ </style>