@pseudolux-ui/media-library 0.0.0-stage → 0.1.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,616 @@
1
+ <view>
2
+ <dialog class="px-dialog crop-dialog" $ref="dialog" aria-labelledby="px-crop-title" @close="closed">
3
+ <div class="px-dialog-head">
4
+ <h3 id="px-crop-title" class="title">Crop · {{ state.label }}</h3>
5
+ <span class="px-subtle" $show="state.pagesLabel">{{ state.pagesLabel }}</span>
6
+ </div>
7
+
8
+ <div class="stage">
9
+ <div class="px-seg shapes" role="radiogroup" aria-label="Shape">
10
+ <button
11
+ type="button"
12
+ role="radio"
13
+ $for="one in state.shapes"
14
+ $key="one.name"
15
+ $aria-checked="one.name === state.shape"
16
+ $tabindex="one.name === state.shape ? 0 : -1"
17
+ $data-shape="one.name"
18
+ @click="pickShape(one.name)"
19
+ @keydown="shapeKey($event)"
20
+ >{{ one.label }}</button>
21
+ </div>
22
+
23
+ <div
24
+ class="px-cropper frame"
25
+ $ref="frame"
26
+ $style="state.frameStyle"
27
+ $class="{ dragging: state.dragging }"
28
+ @pointerdown="pointerDown"
29
+ @pointermove="pointerMove"
30
+ @pointerup="pointerUp"
31
+ @pointercancel="pointerUp"
32
+ >
33
+ <img class="picture" $src="state.src" alt="" draggable="false" />
34
+ <span class="px-focal focal" $show="state.focalStyle" $style="state.focalStyle" aria-hidden="true"></span>
35
+ <div
36
+ class="px-cropbox box"
37
+ $ref="box"
38
+ tabindex="0"
39
+ role="slider"
40
+ aria-label="Crop area. Arrow keys move it, Shift for bigger steps, Alt and arrows resize it."
41
+ aria-valuemin="1"
42
+ $aria-valuemax="state.width"
43
+ $aria-valuenow="state.valueNow"
44
+ $aria-valuetext="state.valueText"
45
+ $style="state.boxStyle"
46
+ $class="{ 'too-small': state.tooSmall }"
47
+ data-part="box"
48
+ @keydown="boxKey"
49
+ >
50
+ <span class="g v1"></span><span class="g v2"></span><span class="g h1"></span><span class="g h2"></span>
51
+ <span class="h nw" data-part="nw"></span>
52
+ <span class="h ne" data-part="ne"></span>
53
+ <span class="h sw" data-part="sw"></span>
54
+ <span class="h se" data-part="se"></span>
55
+ <span class="readout">{{ state.sizeLabel }}</span>
56
+ </div>
57
+ </div>
58
+
59
+ <p class="keys">
60
+ <span class="px-kbd">←↑→↓</span> move · <span class="px-kbd">Shift</span> bigger steps ·
61
+ <span class="px-kbd">Alt</span> + arrows resize · <span class="px-kbd">Enter</span> save ·
62
+ <span class="px-kbd">Esc</span> cancel
63
+ </p>
64
+ </div>
65
+
66
+ <div class="notes" $show="state.isNew || state.tooSmall || state.followsNote || state.error">
67
+ <label class="px-field" $show="state.isNew">
68
+ <span class="px-label">Name <span class="px-subtle">Other sites ask for the crop by this name</span></span>
69
+ <input
70
+ class="px-input"
71
+ type="text"
72
+ placeholder="banner-2026"
73
+ pattern="[a-z0-9]+(-[a-z0-9]+)*"
74
+ $model="state.nameDraft"
75
+ />
76
+ <span class="px-hint">Lowercase letters, digits and hyphens.</span>
77
+ </label>
78
+ <div class="px-callout px-callout-warn" role="status" $show="state.tooSmall">
79
+ <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" aria-hidden="true"><circle cx="12" cy="12" r="9"></circle><path d="M12 8v5M12 16h.01"></path></svg>
80
+ <span><b>{{ state.warnTitle }}</b> {{ state.warnText }}</span>
81
+ </div>
82
+ <p class="px-subtle" $show="state.followsNote">{{ state.followsNote }}</p>
83
+ <div class="px-callout px-callout-danger" role="alert" $show="state.error"><span>{{ state.error }}</span></div>
84
+ </div>
85
+
86
+ <div class="px-dialog-foot">
87
+ <button class="px-btn px-btn-ghost" type="button" $show="state.stored" $disabled="state.saving" @click="useDefault">{{ state.resetLabel }}</button>
88
+ <span class="spacer"></span>
89
+ <button class="px-btn px-btn-secondary" type="button" $disabled="state.saving" @click="cancel">Cancel</button>
90
+ <button class="px-btn px-btn-primary" type="button" $disabled="state.saving" @click="save">{{ state.saveLabel }}</button>
91
+ </div>
92
+ </dialog>
93
+ </view>
94
+
95
+ <logic lang="ts">
96
+ import { reactive, shared, defineRefs, onMounted } from '@lightspeed-js/core'
97
+ import { getLibrary, previewUrl, CROP_EDITOR, SELECTED } from '../config.ts'
98
+ import { cropRect } from '@pseudolux-ui/client'
99
+
100
+ const FREE = '*free'
101
+ const NAME_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/
102
+ const MIN_SIZE = 1
103
+
104
+ const state = reactive({
105
+ label: '',
106
+ src: '',
107
+ width: 0,
108
+ height: 0,
109
+ shapes: [],
110
+ shape: '',
111
+ isNew: false,
112
+ nameDraft: '',
113
+ frameStyle: '',
114
+ boxStyle: '',
115
+ focalStyle: '',
116
+ sizeLabel: '',
117
+ valueNow: 0,
118
+ valueText: '',
119
+ tooSmall: false,
120
+ warnTitle: '',
121
+ warnText: '',
122
+ followsNote: '',
123
+ stored: false,
124
+ resetLabel: 'Use the default',
125
+ pagesLabel: '',
126
+ error: '',
127
+ saving: false,
128
+ saveLabel: 'Save crop',
129
+ dragging: false,
130
+ })
131
+
132
+ const request = shared(CROP_EDITOR)
133
+ const selected = shared(SELECTED)
134
+ const refs = defineRefs()
135
+
136
+ let asked = null
137
+ let opener = null
138
+ let crops = {}
139
+ let box = { x: 0, y: 0, w: 100, h: 100 }
140
+ let lockedRatio = null
141
+ let customName = ''
142
+ let drag = null
143
+
144
+ function round(value) {
145
+ return Math.round(value * 1000) / 1000
146
+ }
147
+
148
+ function presets() {
149
+ return (asked && asked.presets) || []
150
+ }
151
+
152
+ function picture() {
153
+ return {
154
+ dimensions: { width: state.width, height: state.height },
155
+ ...(asked && asked.focal ? { focal: asked.focal } : {}),
156
+ crops,
157
+ }
158
+ }
159
+
160
+ function frameAspect() {
161
+ return state.width && state.height ? state.width / state.height : 1
162
+ }
163
+
164
+ function heightFor(w) {
165
+ return (w * frameAspect()) / lockedRatio
166
+ }
167
+
168
+ function widthFor(h) {
169
+ return (h * lockedRatio) / frameAspect()
170
+ }
171
+
172
+ function rectToBox(rect) {
173
+ box = {
174
+ x: (rect.x / state.width) * 100,
175
+ y: (rect.y / state.height) * 100,
176
+ w: (rect.width / state.width) * 100,
177
+ h: (rect.height / state.height) * 100,
178
+ }
179
+ }
180
+
181
+ function boxToRect() {
182
+ const W = state.width
183
+ const H = state.height
184
+ const width = Math.max(1, Math.min(W, Math.round((box.w / 100) * W)))
185
+ let height = Math.max(1, Math.min(H, Math.round((box.h / 100) * H)))
186
+ if (lockedRatio) height = Math.max(1, Math.min(H, Math.round(width / lockedRatio)))
187
+ const x = Math.min(Math.max(0, Math.round((box.x / 100) * W)), W - width)
188
+ const y = Math.min(Math.max(0, Math.round((box.y / 100) * H)), H - height)
189
+ return { x, y, width, height }
190
+ }
191
+
192
+ function currentPreset() {
193
+ return presets().find((one) => one.name === state.shape)
194
+ }
195
+
196
+ function cropName() {
197
+ if (state.shape !== FREE) return state.shape
198
+ return state.isNew ? state.nameDraft.trim().toLowerCase() : customName
199
+ }
200
+
201
+ function draw() {
202
+ state.boxStyle =
203
+ 'left:' + round(box.x) + '%;top:' + round(box.y) + '%;width:' + round(box.w) + '%;height:' + round(box.h) + '%'
204
+ const rect = boxToRect()
205
+ state.sizeLabel = rect.width + ' × ' + rect.height + ' px'
206
+ state.valueNow = rect.width
207
+ state.valueText =
208
+ rect.width + ' by ' + rect.height + ' pixels, starting ' + rect.x + ' from the left, ' + rect.y + ' from the top'
209
+ const preset = currentPreset()
210
+ state.tooSmall = Boolean(preset && preset.minWidth && rect.width < preset.minWidth)
211
+ if (state.tooSmall) {
212
+ state.warnTitle = 'May look blurry as a ' + preset.label.toLowerCase() + '.'
213
+ state.warnText =
214
+ preset.label + ' crops are shown up to ' + preset.minWidth + ' px wide, and this box is ' + rect.width +
215
+ ' px. Make the box bigger, or save anyway for smaller uses.'
216
+ }
217
+ }
218
+
219
+ function describeShape() {
220
+ const preset = currentPreset()
221
+ const name = cropName()
222
+ state.stored = Boolean(name && crops[name])
223
+ state.resetLabel = preset ? 'Use the default' : 'Delete this crop'
224
+ state.followsNote =
225
+ preset && !state.stored
226
+ ? preset.label + ' ' + preset.ratioLabel + ' follows the focal point. Moving the box here makes it a hand-set crop, marked “Adjusted” in the strip. Use the default to undo that.'
227
+ : ''
228
+ const pages = (asked && asked.pages && name && asked.pages[name]) || 0
229
+ state.pagesLabel = pages ? 'Shared: ' + pages + (pages === 1 ? ' page uses' : ' pages use') + ' this crop' : ''
230
+ state.error = ''
231
+ }
232
+
233
+ function pickShape(name) {
234
+ if (!asked) return
235
+ state.shape = name
236
+ const preset = currentPreset()
237
+ if (preset) {
238
+ lockedRatio = preset.ratio
239
+ state.isNew = false
240
+ rectToBox(cropRect(picture(), preset.name, presets()))
241
+ } else {
242
+ lockedRatio = null
243
+ const existing = customName && crops[customName]
244
+ state.isNew = !existing
245
+ rectToBox(existing || { x: 0, y: 0, width: state.width, height: state.height })
246
+ }
247
+ describeShape()
248
+ draw()
249
+ }
250
+
251
+ function shapeKey(event) {
252
+ if (!event || (event.key !== 'ArrowRight' && event.key !== 'ArrowLeft')) return
253
+ event.preventDefault()
254
+ const names = state.shapes.map((one) => one.name)
255
+ const at = names.indexOf(state.shape)
256
+ const next = names[(at + (event.key === 'ArrowRight' ? 1 : names.length - 1)) % names.length]
257
+ pickShape(next)
258
+ const group = event.currentTarget && event.currentTarget.parentElement
259
+ const button = group && group.querySelector('[data-shape="' + next + '"]')
260
+ if (button) button.focus()
261
+ }
262
+
263
+ function clampMove() {
264
+ box.x = Math.min(Math.max(0, box.x), 100 - box.w)
265
+ box.y = Math.min(Math.max(0, box.y), 100 - box.h)
266
+ }
267
+
268
+ function resize(dw, dh) {
269
+ let w = box.w
270
+ let h = box.h
271
+ if (lockedRatio) {
272
+ w = Math.max(MIN_SIZE, Math.min(100 - box.x, w + (dw || dh)))
273
+ h = heightFor(w)
274
+ if (box.y + h > 100) {
275
+ h = 100 - box.y
276
+ w = widthFor(h)
277
+ }
278
+ } else {
279
+ w = Math.max(MIN_SIZE, Math.min(100 - box.x, w + dw))
280
+ h = Math.max(MIN_SIZE, Math.min(100 - box.y, h + dh))
281
+ }
282
+ box = { ...box, w, h }
283
+ }
284
+
285
+ function boxKey(event) {
286
+ if (!event) return
287
+ if (event.key === 'Enter') {
288
+ event.preventDefault()
289
+ save()
290
+ return
291
+ }
292
+ const arrows = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }
293
+ const move = arrows[event.key]
294
+ if (!move) return
295
+ event.preventDefault()
296
+ const step = event.shiftKey ? 10 : 1
297
+ if (event.altKey) {
298
+ resize(move[0] * step, move[1] * step)
299
+ } else {
300
+ box = { ...box, x: box.x + move[0] * step, y: box.y + move[1] * step }
301
+ clampMove()
302
+ }
303
+ draw()
304
+ }
305
+
306
+ function framePoint(event) {
307
+ const frame = refs.frame
308
+ if (!frame || !frame.getBoundingClientRect) return null
309
+ const r = frame.getBoundingClientRect()
310
+ if (!r.width || !r.height) return null
311
+ return { x: ((event.clientX - r.left) / r.width) * 100, y: ((event.clientY - r.top) / r.height) * 100 }
312
+ }
313
+
314
+ function pointerDown(event) {
315
+ if (!event || event.button) return
316
+ const part = event.target && event.target.getAttribute ? event.target.getAttribute('data-part') : null
317
+ const at = framePoint(event)
318
+ if (!at) return
319
+ event.preventDefault()
320
+ drag = { part: part || 'draw', from: at, start: { ...box } }
321
+ if (drag.part === 'draw') {
322
+ box = { x: at.x, y: at.y, w: 0, h: 0 }
323
+ drag.anchor = { x: at.x, y: at.y }
324
+ }
325
+ state.dragging = true
326
+ if (refs.box && refs.box.focus) refs.box.focus({ preventScroll: true })
327
+ const frame = refs.frame
328
+ if (frame && frame.setPointerCapture && event.pointerId !== undefined) {
329
+ try { frame.setPointerCapture(event.pointerId) } catch { /* not pointer-capable */ }
330
+ }
331
+ }
332
+
333
+ function pointerMove(event) {
334
+ if (!drag) return
335
+ const at = framePoint(event)
336
+ if (!at) return
337
+ const start = drag.start
338
+ if (drag.part === 'box') {
339
+ box = { ...start, x: start.x + at.x - drag.from.x, y: start.y + at.y - drag.from.y }
340
+ clampMove()
341
+ } else {
342
+ const anchor =
343
+ drag.part === 'draw'
344
+ ? drag.anchor
345
+ : {
346
+ x: drag.part === 'nw' || drag.part === 'sw' ? start.x + start.w : start.x,
347
+ y: drag.part === 'nw' || drag.part === 'ne' ? start.y + start.h : start.y,
348
+ }
349
+ const px = Math.min(Math.max(0, at.x), 100)
350
+ const py = Math.min(Math.max(0, at.y), 100)
351
+ let w = Math.abs(px - anchor.x)
352
+ let h = Math.abs(py - anchor.y)
353
+ const left = px < anchor.x
354
+ const up = py < anchor.y
355
+ if (lockedRatio) {
356
+ h = heightFor(w)
357
+ const maxW = left ? anchor.x : 100 - anchor.x
358
+ const maxH = up ? anchor.y : 100 - anchor.y
359
+ if (w > maxW) { w = maxW; h = heightFor(w) }
360
+ if (h > maxH) { h = maxH; w = widthFor(h) }
361
+ }
362
+ box = { x: left ? anchor.x - w : anchor.x, y: up ? anchor.y - h : anchor.y, w, h }
363
+ }
364
+ draw()
365
+ }
366
+
367
+ function pointerUp(event) {
368
+ if (!drag) return
369
+ const wasDrawing = drag.part === 'draw'
370
+ const start = drag.start
371
+ drag = null
372
+ state.dragging = false
373
+ const frame = refs.frame
374
+ if (frame && frame.releasePointerCapture && event && event.pointerId !== undefined) {
375
+ try { frame.releasePointerCapture(event.pointerId) } catch { /* already released */ }
376
+ }
377
+ if (wasDrawing && (box.w < 0.5 || box.h < 0.5)) box = start
378
+ draw()
379
+ }
380
+
381
+ function keepCrops(next) {
382
+ crops = next
383
+ const row = selected.value
384
+ if (row && asked && row.id === asked.id) selected.value = { ...row, crops: next }
385
+ }
386
+
387
+ async function save() {
388
+ if (!asked || state.saving) return
389
+ const name = cropName()
390
+ if (!name || !NAME_PATTERN.test(name)) {
391
+ state.error = 'Give the crop a name: lowercase letters, digits and hyphens.'
392
+ return
393
+ }
394
+ if (state.isNew && presets().some((one) => one.name === name)) {
395
+ state.error = '“' + name + '” is one of the shapes. Choose it above to adjust it.'
396
+ return
397
+ }
398
+ const rect = boxToRect()
399
+ state.saving = true
400
+ state.saveLabel = 'Saving…'
401
+ state.error = ''
402
+ try {
403
+ const result = await getLibrary().setCrop(asked.id, name, rect)
404
+ keepCrops((result && result.crops) || { ...crops, [name]: rect })
405
+ close()
406
+ } catch (err) {
407
+ state.error = err && err.message ? err.message : 'Could not save the crop.'
408
+ } finally {
409
+ state.saving = false
410
+ state.saveLabel = 'Save crop'
411
+ }
412
+ }
413
+
414
+ async function useDefault() {
415
+ if (!asked || state.saving) return
416
+ const name = cropName()
417
+ if (!name || !crops[name]) return
418
+ state.saving = true
419
+ state.error = ''
420
+ try {
421
+ const result = await getLibrary().removeCrop(asked.id, name)
422
+ const rest = { ...crops }
423
+ delete rest[name]
424
+ keepCrops((result && result.crops) || rest)
425
+ close()
426
+ } catch (err) {
427
+ state.error = err && err.message ? err.message : 'Could not go back to the default.'
428
+ } finally {
429
+ state.saving = false
430
+ }
431
+ }
432
+
433
+ function cancel() {
434
+ close()
435
+ }
436
+
437
+ function close() {
438
+ const dialog = refs.dialog
439
+ if (dialog && dialog.open) dialog.close()
440
+ else closed()
441
+ }
442
+
443
+ function closed() {
444
+ if (opener && opener.isConnected && opener.focus) opener.focus()
445
+ opener = null
446
+ drag = null
447
+ state.dragging = false
448
+ asked = null
449
+ if (request.value) request.value = null
450
+ }
451
+
452
+ function open(next) {
453
+ if (!next) {
454
+ const dialog = refs.dialog
455
+ if (dialog && dialog.open) dialog.close()
456
+ return
457
+ }
458
+ asked = next
459
+ crops = { ...(next.crops || {}) }
460
+ state.label = next.label || next.id
461
+ state.width = next.width
462
+ state.height = next.height
463
+ state.src = previewUrl(next.id)
464
+ const aspect = next.width / next.height
465
+ state.frameStyle =
466
+ 'aspect-ratio: ' + aspect + '; width: min(100%, calc(58vh * ' + aspect + '))'
467
+ state.focalStyle = next.focal ? 'left:' + next.focal.x + '%;top:' + next.focal.y + '%' : ''
468
+ state.shapes = presets()
469
+ .map((one) => ({ name: one.name, label: one.label + ' ' + one.ratioLabel }))
470
+ .concat([{ name: FREE, label: 'Free' }])
471
+ const isPreset = presets().some((one) => one.name === next.name)
472
+ customName = isPreset ? '' : next.name || ''
473
+ state.nameDraft = ''
474
+ state.saving = false
475
+ state.saveLabel = 'Save crop'
476
+ pickShape(isPreset ? next.name : FREE)
477
+ const dialog = refs.dialog
478
+ if (dialog && !dialog.open && dialog.showModal) {
479
+ opener = document.activeElement
480
+ dialog.showModal()
481
+ }
482
+ setTimeout(() => {
483
+ if (refs.box && refs.box.focus) refs.box.focus({ preventScroll: true })
484
+ }, 0)
485
+ }
486
+
487
+ request.subscribe(open)
488
+
489
+ onMounted(() => {
490
+ if (request.value) open(request.value)
491
+ })
492
+ </logic>
493
+
494
+ <style>
495
+ .crop-dialog {
496
+ margin: 4vh auto auto;
497
+ width: min(960px, calc(100vw - 32px));
498
+ max-width: none;
499
+ max-height: 92vh;
500
+ padding: 0;
501
+ color: var(--px-text);
502
+ overflow: auto;
503
+ }
504
+
505
+ .crop-dialog:not([open]) {
506
+ display: none;
507
+ }
508
+
509
+ .crop-dialog::backdrop {
510
+ background: var(--px-scrim);
511
+ }
512
+
513
+ .title {
514
+ flex: 1;
515
+ min-width: 0;
516
+ overflow-wrap: anywhere;
517
+ }
518
+
519
+ .stage {
520
+ display: flex;
521
+ flex-direction: column;
522
+ align-items: center;
523
+ gap: 14px;
524
+ padding: 16px;
525
+ background: var(--px-surface-2);
526
+ }
527
+
528
+ .shapes {
529
+ flex-wrap: wrap;
530
+ justify-content: center;
531
+ background: var(--px-surface);
532
+ }
533
+
534
+ .frame {
535
+ touch-action: none;
536
+ user-select: none;
537
+ cursor: crosshair;
538
+ background: var(--px-surface);
539
+ }
540
+
541
+ .frame.dragging {
542
+ cursor: grabbing;
543
+ }
544
+
545
+ .picture {
546
+ display: block;
547
+ width: 100%;
548
+ height: 100%;
549
+ object-fit: cover;
550
+ pointer-events: none;
551
+ }
552
+
553
+ .focal {
554
+ pointer-events: none;
555
+ }
556
+
557
+ .box {
558
+ cursor: grab;
559
+ }
560
+
561
+ .box.too-small {
562
+ border-color: var(--px-warn);
563
+ }
564
+
565
+ .box .g,
566
+ .readout {
567
+ pointer-events: none;
568
+ }
569
+
570
+ .g.v1 { left: 33.333%; top: 0; bottom: 0; width: 1px; }
571
+ .g.v2 { left: 66.667%; top: 0; bottom: 0; width: 1px; }
572
+ .g.h1 { top: 33.333%; left: 0; right: 0; height: 1px; }
573
+ .g.h2 { top: 66.667%; left: 0; right: 0; height: 1px; }
574
+
575
+ .box .h::before {
576
+ content: "";
577
+ position: absolute;
578
+ inset: -12px;
579
+ }
580
+
581
+ .h.nw { left: -8px; top: -8px; cursor: nwse-resize; }
582
+ .h.ne { right: -8px; top: -8px; cursor: nesw-resize; }
583
+ .h.sw { left: -8px; bottom: -8px; cursor: nesw-resize; }
584
+ .h.se { right: -8px; bottom: -8px; cursor: nwse-resize; }
585
+
586
+ .readout {
587
+ position: absolute;
588
+ left: 8px;
589
+ bottom: 8px;
590
+ padding: 3px 8px;
591
+ border-radius: 4px;
592
+ background: var(--px-text);
593
+ color: var(--px-surface);
594
+ font-size: var(--px-fs-xs);
595
+ font-weight: 600;
596
+ white-space: nowrap;
597
+ }
598
+
599
+ .keys {
600
+ margin: 0;
601
+ font-size: var(--px-fs-s);
602
+ color: var(--px-text-2);
603
+ text-align: center;
604
+ }
605
+
606
+ .notes {
607
+ display: flex;
608
+ flex-direction: column;
609
+ gap: 10px;
610
+ padding: 14px 16px;
611
+ }
612
+
613
+ .spacer {
614
+ flex: 1;
615
+ }
616
+ </style>