@prenta/admin 2.11.1 → 2.12.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.
- package/CHANGELOG.md +17 -0
- package/dist/__tests__/components/modal.render.test.js +30 -1
- package/dist/__tests__/components/modal.render.test.js.map +1 -1
- package/dist/__tests__/lib/media-download.test.d.ts +2 -0
- package/dist/__tests__/lib/media-download.test.d.ts.map +1 -0
- package/dist/__tests__/lib/media-download.test.js +14 -0
- package/dist/__tests__/lib/media-download.test.js.map +1 -0
- package/dist/__tests__/views/media-browser.render.test.d.ts +2 -0
- package/dist/__tests__/views/media-browser.render.test.d.ts.map +1 -0
- package/dist/__tests__/views/media-browser.render.test.js +270 -0
- package/dist/__tests__/views/media-browser.render.test.js.map +1 -0
- package/dist/__tests__/views/media-grid.test.d.ts +2 -0
- package/dist/__tests__/views/media-grid.test.d.ts.map +1 -0
- package/dist/__tests__/views/media-grid.test.js +31 -0
- package/dist/__tests__/views/media-grid.test.js.map +1 -0
- package/dist/components/MediaLightbox.d.ts +22 -0
- package/dist/components/MediaLightbox.d.ts.map +1 -0
- package/dist/components/MediaLightbox.js +38 -0
- package/dist/components/MediaLightbox.js.map +1 -0
- package/dist/components/ui/ConfirmDialog.d.ts +3 -1
- package/dist/components/ui/ConfirmDialog.d.ts.map +1 -1
- package/dist/components/ui/ConfirmDialog.js +2 -2
- package/dist/components/ui/ConfirmDialog.js.map +1 -1
- package/dist/components/ui/Modal.d.ts +7 -1
- package/dist/components/ui/Modal.d.ts.map +1 -1
- package/dist/components/ui/Modal.js +4 -2
- package/dist/components/ui/Modal.js.map +1 -1
- package/dist/lib/dialog-return-focus.d.ts +10 -0
- package/dist/lib/dialog-return-focus.d.ts.map +1 -0
- package/dist/lib/dialog-return-focus.js +26 -0
- package/dist/lib/dialog-return-focus.js.map +1 -0
- package/dist/lib/media-download.d.ts +8 -0
- package/dist/lib/media-download.d.ts.map +1 -0
- package/dist/lib/media-download.js +21 -0
- package/dist/lib/media-download.js.map +1 -0
- package/dist/prenta-admin.css +1 -1
- package/dist/views/MediaBrowser.d.ts +37 -0
- package/dist/views/MediaBrowser.d.ts.map +1 -1
- package/dist/views/MediaBrowser.js +217 -80
- package/dist/views/MediaBrowser.js.map +1 -1
- package/dist/views/MediaSeoAudit.d.ts +6 -0
- package/dist/views/MediaSeoAudit.d.ts.map +1 -1
- package/dist/views/MediaSeoAudit.js +12 -0
- package/dist/views/MediaSeoAudit.js.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/components/modal.render.test.tsx +52 -0
- package/src/__tests__/lib/media-download.test.ts +23 -0
- package/src/__tests__/views/media-browser.render.test.tsx +325 -0
- package/src/__tests__/views/media-grid.test.ts +34 -0
- package/src/components/MediaLightbox.tsx +127 -0
- package/src/components/ui/ConfirmDialog.tsx +4 -0
- package/src/components/ui/Modal.tsx +10 -0
- package/src/lib/dialog-return-focus.ts +33 -0
- package/src/lib/media-download.ts +18 -0
- package/src/views/MediaBrowser.tsx +403 -172
- package/src/views/MediaSeoAudit.tsx +11 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
3
|
+
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react'
|
|
4
|
+
|
|
5
|
+
interface Call {
|
|
6
|
+
endpoint: string
|
|
7
|
+
method: string
|
|
8
|
+
body: unknown
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const server = vi.hoisted(() => ({ calls: [] as Call[], bulkFail: [] as string[] }))
|
|
12
|
+
|
|
13
|
+
vi.mock('../../lib/api.js', () => ({
|
|
14
|
+
getApiBase: () => '/api/cms',
|
|
15
|
+
cmsApi: vi.fn(async (endpoint: string, init: RequestInit = {}) => {
|
|
16
|
+
const method = init.method ?? 'GET'
|
|
17
|
+
const body = typeof init.body === 'string' ? JSON.parse(init.body) : undefined
|
|
18
|
+
server.calls.push({ endpoint, method, body })
|
|
19
|
+
if (endpoint === '/media/bulk-delete') {
|
|
20
|
+
const ids = (body as { ids: string[] }).ids
|
|
21
|
+
return {
|
|
22
|
+
data: { results: ids.map((id) => ({ id, ok: !server.bulkFail.includes(id) })) },
|
|
23
|
+
status: 200,
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (endpoint === '/ai/media-metadata') return { error: 'AI not configured', status: 503 }
|
|
27
|
+
if (endpoint.startsWith('/folders')) return { data: [], status: 200 }
|
|
28
|
+
return { data: {}, status: 200 }
|
|
29
|
+
}),
|
|
30
|
+
}))
|
|
31
|
+
|
|
32
|
+
const toast = vi.hoisted(() =>
|
|
33
|
+
Object.assign(vi.fn(), { error: vi.fn(), success: vi.fn(), info: vi.fn() }),
|
|
34
|
+
)
|
|
35
|
+
vi.mock('sonner', () => ({ toast }))
|
|
36
|
+
|
|
37
|
+
const MEDIA = [
|
|
38
|
+
{
|
|
39
|
+
id: 'm1',
|
|
40
|
+
name: 'harbor-at-dusk.jpg',
|
|
41
|
+
type: 'image/jpeg',
|
|
42
|
+
url: 'https://x.public.blob.vercel-storage.com/harbor.jpg',
|
|
43
|
+
},
|
|
44
|
+
{ id: 'm2', name: 'team.png', type: 'image/png', url: '/u/team.png', dimensions: '800 × 600' },
|
|
45
|
+
{ id: 'm3', name: 'menu.pdf', type: 'application/pdf', url: '/u/menu.pdf' },
|
|
46
|
+
{
|
|
47
|
+
id: 'm4',
|
|
48
|
+
name: 'logo.svg',
|
|
49
|
+
type: 'image/svg+xml',
|
|
50
|
+
url: '/u/logo.svg',
|
|
51
|
+
altTag: 'Prenta logo',
|
|
52
|
+
seoScore: 85,
|
|
53
|
+
sizeBytes: 300,
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
id: 'm5',
|
|
57
|
+
name: 'brief.docx',
|
|
58
|
+
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
59
|
+
url: '/u/brief.docx',
|
|
60
|
+
seoScore: 20,
|
|
61
|
+
sizeBytes: 9000,
|
|
62
|
+
},
|
|
63
|
+
].map((item) => ({
|
|
64
|
+
size: '1 KB',
|
|
65
|
+
sizeBytes: 1024,
|
|
66
|
+
date: '2026-01-01',
|
|
67
|
+
title: item.name,
|
|
68
|
+
usedOn: [],
|
|
69
|
+
...item,
|
|
70
|
+
}))
|
|
71
|
+
|
|
72
|
+
vi.mock('../../lib/useApiData.js', () => ({
|
|
73
|
+
useApiData: (endpoint: string | null) => ({
|
|
74
|
+
data: endpoint?.startsWith('/media?pageSize=100')
|
|
75
|
+
? { docs: MEDIA, total: MEDIA.length }
|
|
76
|
+
: { docs: [], total: MEDIA.length },
|
|
77
|
+
loading: false,
|
|
78
|
+
error: null,
|
|
79
|
+
refetch: () => {},
|
|
80
|
+
}),
|
|
81
|
+
}))
|
|
82
|
+
|
|
83
|
+
const { MediaBrowser } = await import('../../views/MediaBrowser.js')
|
|
84
|
+
|
|
85
|
+
function renderBrowser() {
|
|
86
|
+
render(<MediaBrowser />)
|
|
87
|
+
return screen.findByRole('region', { name: 'Media library' })
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function openDetails(name: string) {
|
|
91
|
+
const grid = await renderBrowser()
|
|
92
|
+
fireEvent.click(within(grid).getByRole('button', { name: new RegExp(`^Open ${name}`) }))
|
|
93
|
+
return screen.findByRole('region', { name: `${name} details` })
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
beforeEach(() => {
|
|
97
|
+
server.calls = []
|
|
98
|
+
server.bulkFail = []
|
|
99
|
+
vi.clearAllMocks()
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
describe('Media grid cards', () => {
|
|
103
|
+
it('open with a real button and select with a checkbox control', async () => {
|
|
104
|
+
const grid = await renderBrowser()
|
|
105
|
+
const open = within(grid).getByRole('button', { name: 'Open team.png, missing SEO' })
|
|
106
|
+
expect(open.tagName).toBe('BUTTON')
|
|
107
|
+
|
|
108
|
+
const select = within(grid).getByRole('checkbox', { name: 'Select team.png' })
|
|
109
|
+
expect(select.getAttribute('aria-checked')).toBe('false')
|
|
110
|
+
fireEvent.click(select)
|
|
111
|
+
expect(select.getAttribute('aria-checked')).toBe('true')
|
|
112
|
+
expect(screen.getByText('1 file selected')).toBeTruthy()
|
|
113
|
+
expect(screen.queryByRole('region', { name: 'team.png details' })).toBeNull()
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
describe('Media full-size viewer', () => {
|
|
118
|
+
it('shows the whole image and steps through images with the arrow keys', async () => {
|
|
119
|
+
const panel = await openDetails('harbor-at-dusk.jpg')
|
|
120
|
+
expect(panel.querySelector('img')!.className).toContain('object-contain')
|
|
121
|
+
|
|
122
|
+
fireEvent.click(within(panel).getByRole('button', { name: 'View full size' }))
|
|
123
|
+
const viewer = await screen.findByRole('dialog', { name: 'harbor-at-dusk.jpg' })
|
|
124
|
+
expect(viewer.querySelector('img')!.className).toContain('object-contain')
|
|
125
|
+
expect(within(viewer).getByText('1 of 3')).toBeTruthy()
|
|
126
|
+
|
|
127
|
+
fireEvent.keyDown(viewer, { key: 'ArrowRight' })
|
|
128
|
+
expect(await screen.findByRole('dialog', { name: 'team.png' })).toBeTruthy()
|
|
129
|
+
expect(within(viewer).getByText('2 of 3')).toBeTruthy()
|
|
130
|
+
expect(within(viewer).getByText('800 × 600')).toBeTruthy()
|
|
131
|
+
|
|
132
|
+
// The PDF is skipped; the last image's Next does nothing but keeps focus.
|
|
133
|
+
fireEvent.keyDown(viewer, { key: 'ArrowRight' })
|
|
134
|
+
expect(await screen.findByRole('dialog', { name: 'logo.svg' })).toBeTruthy()
|
|
135
|
+
const next = within(viewer).getByRole('button', { name: 'Next image' })
|
|
136
|
+
expect(next.getAttribute('aria-disabled')).toBe('true')
|
|
137
|
+
fireEvent.click(next)
|
|
138
|
+
expect(within(viewer).getByText('3 of 3')).toBeTruthy()
|
|
139
|
+
|
|
140
|
+
fireEvent.keyDown(viewer, { key: 'ArrowLeft' })
|
|
141
|
+
fireEvent.keyDown(viewer, { key: 'ArrowLeft' })
|
|
142
|
+
expect(await screen.findByRole('dialog', { name: 'harbor-at-dusk.jpg' })).toBeTruthy()
|
|
143
|
+
|
|
144
|
+
fireEvent.click(within(viewer).getByRole('button', { name: 'Close viewer' }))
|
|
145
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('returns focus to "View full size" when it closes', async () => {
|
|
149
|
+
const panel = await openDetails('team.png')
|
|
150
|
+
const expand = within(panel).getByRole('button', { name: 'View full size' })
|
|
151
|
+
expand.focus()
|
|
152
|
+
fireEvent.click(expand)
|
|
153
|
+
const viewer = await screen.findByRole('dialog', { name: 'team.png' })
|
|
154
|
+
fireEvent.keyDown(viewer, { key: 'Escape' })
|
|
155
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
|
156
|
+
await waitFor(() => expect(document.activeElement).toBe(expand))
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
it('still opens when search has hidden the open image', async () => {
|
|
160
|
+
const panel = await openDetails('team.png')
|
|
161
|
+
fireEvent.change(screen.getByRole('searchbox', { name: 'Search files' }), {
|
|
162
|
+
target: { value: 'harbor' },
|
|
163
|
+
})
|
|
164
|
+
fireEvent.click(within(panel).getByRole('button', { name: 'View full size' }))
|
|
165
|
+
const viewer = await screen.findByRole('dialog', { name: 'team.png' })
|
|
166
|
+
expect(within(viewer).getByText('1 of 1')).toBeTruthy()
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('isn’t offered for files that aren’t images', async () => {
|
|
170
|
+
const grid = await renderBrowser()
|
|
171
|
+
fireEvent.click(within(grid).getByRole('button', { name: /^Open menu\.pdf/ }))
|
|
172
|
+
const panel = await screen.findByRole('region', { name: 'menu.pdf details' })
|
|
173
|
+
expect(within(panel).queryByRole('button', { name: 'View full size' })).toBeNull()
|
|
174
|
+
})
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
describe('Media download', () => {
|
|
178
|
+
it('links to the file as an attachment', async () => {
|
|
179
|
+
const panel = await openDetails('harbor-at-dusk.jpg')
|
|
180
|
+
const link = within(panel).getByRole('link', { name: 'Download' })
|
|
181
|
+
expect(link.getAttribute('href')).toBe(
|
|
182
|
+
'https://x.public.blob.vercel-storage.com/harbor.jpg?download=1',
|
|
183
|
+
)
|
|
184
|
+
expect(link.getAttribute('download')).toBe('harbor-at-dusk.jpg')
|
|
185
|
+
})
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
describe('Media delete', () => {
|
|
189
|
+
it('asks before deleting one file, and cancelling deletes nothing', async () => {
|
|
190
|
+
const panel = await openDetails('team.png')
|
|
191
|
+
const del = within(panel).getByRole('button', { name: 'Delete' })
|
|
192
|
+
del.focus()
|
|
193
|
+
fireEvent.click(del)
|
|
194
|
+
const confirm = await screen.findByRole('dialog', { name: 'Delete file?' })
|
|
195
|
+
fireEvent.click(within(confirm).getByRole('button', { name: 'Cancel' }))
|
|
196
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
|
197
|
+
expect(server.calls.some((c) => c.method === 'DELETE')).toBe(false)
|
|
198
|
+
await waitFor(() => expect(document.activeElement).toBe(del))
|
|
199
|
+
|
|
200
|
+
fireEvent.click(within(panel).getByRole('button', { name: 'Delete' }))
|
|
201
|
+
const again = await screen.findByRole('dialog', { name: 'Delete file?' })
|
|
202
|
+
fireEvent.click(within(again).getByRole('button', { name: 'Delete' }))
|
|
203
|
+
await waitFor(() =>
|
|
204
|
+
expect(server.calls).toContainEqual({
|
|
205
|
+
endpoint: '/media/m2',
|
|
206
|
+
method: 'DELETE',
|
|
207
|
+
body: undefined,
|
|
208
|
+
}),
|
|
209
|
+
)
|
|
210
|
+
expect(toast.success).toHaveBeenCalledWith('Media deleted')
|
|
211
|
+
// The panel (and its Delete) is gone, so focus lands on the library.
|
|
212
|
+
await waitFor(() =>
|
|
213
|
+
expect(document.activeElement).toBe(screen.getByRole('region', { name: 'Media library' })),
|
|
214
|
+
)
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
it('deletes a selection in one bulk request after confirming, and reports failures', async () => {
|
|
218
|
+
server.bulkFail = ['m3']
|
|
219
|
+
const grid = await renderBrowser()
|
|
220
|
+
fireEvent.click(within(grid).getByRole('checkbox', { name: 'Select team.png' }))
|
|
221
|
+
fireEvent.click(within(grid).getByRole('checkbox', { name: 'Select menu.pdf' }))
|
|
222
|
+
fireEvent.click(screen.getByRole('button', { name: 'Delete Selected' }))
|
|
223
|
+
|
|
224
|
+
const confirm = await screen.findByRole('dialog', { name: 'Delete 2 files?' })
|
|
225
|
+
expect(server.calls.some((c) => c.endpoint === '/media/bulk-delete')).toBe(false)
|
|
226
|
+
fireEvent.click(within(confirm).getByRole('button', { name: 'Delete' }))
|
|
227
|
+
|
|
228
|
+
await waitFor(() => expect(toast.error).toHaveBeenCalledWith("1 file couldn't be deleted"))
|
|
229
|
+
const bulk = server.calls.filter((c) => c.endpoint === '/media/bulk-delete')
|
|
230
|
+
expect(bulk).toHaveLength(1)
|
|
231
|
+
expect(bulk[0]!.body).toEqual({ ids: ['m2', 'm3'], confirm: true })
|
|
232
|
+
expect(toast.success).toHaveBeenCalledWith('Media deleted')
|
|
233
|
+
// The file that failed stays selected so it can be retried.
|
|
234
|
+
expect(screen.getByText('1 file selected')).toBeTruthy()
|
|
235
|
+
})
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
describe('Media toolbar (Media.html .mc-toolbar)', () => {
|
|
239
|
+
function pills() {
|
|
240
|
+
const group = screen.getByRole('group', { name: 'Filter by type' })
|
|
241
|
+
return within(group)
|
|
242
|
+
.getAllByRole('button')
|
|
243
|
+
.map((b) => b.textContent)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
it('shows the kit type pills with counts, plus Documents when there are any', async () => {
|
|
247
|
+
await renderBrowser()
|
|
248
|
+
expect(pills()).toEqual(['All5', 'Images2', 'Video0', 'PDF1', 'SVG1', 'Documents1'])
|
|
249
|
+
expect(screen.getByText('5 files')).toBeTruthy()
|
|
250
|
+
|
|
251
|
+
fireEvent.click(screen.getByRole('button', { name: /^SVG/ }))
|
|
252
|
+
expect(screen.getByRole('button', { name: /^SVG/ }).getAttribute('aria-pressed')).toBe('true')
|
|
253
|
+
expect(screen.getByText('1 file')).toBeTruthy()
|
|
254
|
+
const grid = screen.getByRole('region', { name: 'Media library' })
|
|
255
|
+
expect(within(grid).getByRole('button', { name: /^Open logo\.svg/ })).toBeTruthy()
|
|
256
|
+
expect(within(grid).queryByRole('button', { name: /^Open team\.png/ })).toBeNull()
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
it('sorts by the kit presets', async () => {
|
|
260
|
+
const grid = await renderBrowser()
|
|
261
|
+
const sort = screen.getByRole('combobox', { name: 'Sort media' }) as HTMLSelectElement
|
|
262
|
+
expect(sort.value).toBe('date')
|
|
263
|
+
const order = () =>
|
|
264
|
+
within(grid)
|
|
265
|
+
.getAllByRole('button', { name: /^Open / })
|
|
266
|
+
.map((b) => b.getAttribute('aria-label')!.split(',')[0]!.replace('Open ', ''))
|
|
267
|
+
|
|
268
|
+
fireEvent.change(sort, { target: { value: 'score' } })
|
|
269
|
+
expect(order().slice(0, 2)).toEqual(['logo.svg', 'brief.docx'])
|
|
270
|
+
|
|
271
|
+
fireEvent.change(sort, { target: { value: 'size' } })
|
|
272
|
+
expect(order()[0]).toBe('brief.docx')
|
|
273
|
+
expect(order().at(-1)).toBe('logo.svg')
|
|
274
|
+
|
|
275
|
+
fireEvent.change(sort, { target: { value: 'name' } })
|
|
276
|
+
expect(order()).toEqual([
|
|
277
|
+
'brief.docx',
|
|
278
|
+
'harbor-at-dusk.jpg',
|
|
279
|
+
'logo.svg',
|
|
280
|
+
'menu.pdf',
|
|
281
|
+
'team.png',
|
|
282
|
+
])
|
|
283
|
+
})
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
describe('Media grid card (Media.html .media-item)', () => {
|
|
287
|
+
it('shows the score badge, the type badge and the SEO tag', async () => {
|
|
288
|
+
const grid = await renderBrowser()
|
|
289
|
+
const card = (name: string) =>
|
|
290
|
+
within(grid).getByRole('button', { name: new RegExp(`^Open ${name.replace('.', '\\.')}`) })
|
|
291
|
+
.parentElement!
|
|
292
|
+
|
|
293
|
+
const logo = card('logo.svg')
|
|
294
|
+
expect(within(grid).getByRole('button', { name: 'Open logo.svg, SEO score 85' })).toBeTruthy()
|
|
295
|
+
expect(within(logo).getByText('85').className).toContain('var(--suc-l)')
|
|
296
|
+
expect(within(logo).getByText('svg')).toBeTruthy()
|
|
297
|
+
expect(within(logo).queryByText('SEO')).toBeNull()
|
|
298
|
+
|
|
299
|
+
const brief = card('brief.docx')
|
|
300
|
+
expect(within(brief).getByText('20').className).toContain('var(--err-l)')
|
|
301
|
+
expect(within(brief).getByText('SEO')).toBeTruthy()
|
|
302
|
+
expect(within(brief).getByText('document')).toBeTruthy()
|
|
303
|
+
|
|
304
|
+
// No score from the server: no badge.
|
|
305
|
+
expect(card('team.png').textContent).not.toMatch(/\d{2}/)
|
|
306
|
+
})
|
|
307
|
+
|
|
308
|
+
it('has no AI Content Analysis action, which nothing implements', async () => {
|
|
309
|
+
const panel = await openDetails('team.png')
|
|
310
|
+
expect(within(panel).queryByText('AI Content Analysis')).toBeNull()
|
|
311
|
+
})
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
describe('Media alt fallback', () => {
|
|
315
|
+
it('suggests the filename without an "Image of" prefix when AI is unavailable', async () => {
|
|
316
|
+
const panel = await openDetails('harbor-at-dusk.jpg')
|
|
317
|
+
const generate = within(panel).getAllByRole('button', { name: 'AI Generate' })[0]!
|
|
318
|
+
fireEvent.click(generate)
|
|
319
|
+
await waitFor(() =>
|
|
320
|
+
expect(
|
|
321
|
+
within(panel).getByPlaceholderText('Describe this image for accessibility...'),
|
|
322
|
+
).toHaveProperty('value', 'harbor at dusk'),
|
|
323
|
+
)
|
|
324
|
+
})
|
|
325
|
+
})
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { MEDIA_GRID_ROW_HEIGHT, mediaGridColumns, mediaKind } from '../../views/MediaBrowser.js'
|
|
3
|
+
|
|
4
|
+
describe('mediaKind', () => {
|
|
5
|
+
it('sorts files into the kit’s disjoint pills, with Documents for the rest', () => {
|
|
6
|
+
expect(mediaKind({ type: 'image/jpeg', name: 'a.jpg', url: '/a.jpg' })).toBe('image')
|
|
7
|
+
expect(mediaKind({ type: 'image/svg+xml', name: 'a.svg', url: '/a.svg' })).toBe('svg')
|
|
8
|
+
expect(mediaKind({ type: '', name: 'logo.SVG', url: '/x' })).toBe('svg')
|
|
9
|
+
expect(mediaKind({ type: 'video/mp4', name: 'a.mp4', url: '/a.mp4' })).toBe('video')
|
|
10
|
+
expect(mediaKind({ type: 'application/pdf', name: 'a', url: '/a' })).toBe('pdf')
|
|
11
|
+
expect(mediaKind({ type: '', name: 'menu.pdf', url: '/x' })).toBe('pdf')
|
|
12
|
+
expect(mediaKind({ type: 'application/msword', name: 'a.doc', url: '/a.doc' })).toBe('document')
|
|
13
|
+
})
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
describe('media grid layout', () => {
|
|
17
|
+
it('fits auto-fill minmax(220px, 1fr) columns with a 14px gap', () => {
|
|
18
|
+
expect(mediaGridColumns(219)).toBe(1)
|
|
19
|
+
expect(mediaGridColumns(220)).toBe(1)
|
|
20
|
+
expect(mediaGridColumns(454)).toBe(2)
|
|
21
|
+
expect(mediaGridColumns(453)).toBe(1)
|
|
22
|
+
expect(mediaGridColumns(1170)).toBe(5)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('falls back to four columns before the grid is measured', () => {
|
|
26
|
+
expect(mediaGridColumns(0)).toBe(4)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
// Rows are virtualised at this stride; cards taller than it overlap the next
|
|
30
|
+
// row (the cropped-tile bug), so the stride is the fixed card height + gap.
|
|
31
|
+
it('uses the fixed card height plus the gap as the row stride', () => {
|
|
32
|
+
expect(MEDIA_GRID_ROW_HEIGHT).toBe(172 + 14)
|
|
33
|
+
})
|
|
34
|
+
})
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as Dialog from '@radix-ui/react-dialog'
|
|
4
|
+
import { ChevronLeft, ChevronRight, X } from 'lucide-react'
|
|
5
|
+
import { adminPortalContainer } from '../lib/portal-container.js'
|
|
6
|
+
import { useDialogReturnFocus } from '../lib/dialog-return-focus.js'
|
|
7
|
+
|
|
8
|
+
interface MediaLightboxItem {
|
|
9
|
+
id: string | number
|
|
10
|
+
url: string
|
|
11
|
+
name: string
|
|
12
|
+
alt: string
|
|
13
|
+
dimensions?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface MediaLightboxProps {
|
|
17
|
+
items: MediaLightboxItem[]
|
|
18
|
+
/** Id of the item on screen; `null` closes the viewer. */
|
|
19
|
+
activeId: string | number | null
|
|
20
|
+
onActiveChange: (id: string | number) => void
|
|
21
|
+
onClose: () => void
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Full-size image viewer. The whole image is shown (`object-contain`), never
|
|
26
|
+
* cropped. ← and → (and the buttons) step through `items`; Radix supplies the
|
|
27
|
+
* focus trap, Escape, and focus return to the opener.
|
|
28
|
+
*/
|
|
29
|
+
export function MediaLightbox({ items, activeId, onActiveChange, onClose }: MediaLightboxProps) {
|
|
30
|
+
const index = activeId === null ? -1 : items.findIndex((item) => item.id === activeId)
|
|
31
|
+
const item = index >= 0 ? items[index] : undefined
|
|
32
|
+
const hasPrev = index > 0
|
|
33
|
+
const hasNext = index >= 0 && index < items.length - 1
|
|
34
|
+
const returnFocus = useDialogReturnFocus(item !== undefined)
|
|
35
|
+
|
|
36
|
+
const step = (delta: -1 | 1) => {
|
|
37
|
+
const next = items[index + delta]
|
|
38
|
+
if (next) onActiveChange(next.id)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const navButton =
|
|
42
|
+
'border-border bg-card text-foreground hover:bg-muted focus-visible:ring-ring prenta-touch-target absolute top-1/2 flex h-10 w-10 -translate-y-1/2 items-center justify-center rounded-full border shadow-sm transition-colors focus-visible:ring-2 focus-visible:outline-none aria-disabled:cursor-default aria-disabled:opacity-40'
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<Dialog.Root
|
|
46
|
+
open={item !== undefined}
|
|
47
|
+
onOpenChange={(open) => {
|
|
48
|
+
if (!open) onClose()
|
|
49
|
+
}}
|
|
50
|
+
>
|
|
51
|
+
<Dialog.Portal container={adminPortalContainer()}>
|
|
52
|
+
<Dialog.Overlay className="fixed inset-0 z-60 bg-black/70" />
|
|
53
|
+
<Dialog.Content
|
|
54
|
+
onCloseAutoFocus={returnFocus}
|
|
55
|
+
className="bg-card text-card-foreground border-border mobile:inset-0 mobile:rounded-none fixed inset-4 z-60 flex flex-col overflow-hidden rounded-[14px] border shadow-xl focus:outline-none sm:inset-8"
|
|
56
|
+
onKeyDown={(event) => {
|
|
57
|
+
if (event.key === 'ArrowLeft') {
|
|
58
|
+
event.preventDefault()
|
|
59
|
+
step(-1)
|
|
60
|
+
} else if (event.key === 'ArrowRight') {
|
|
61
|
+
event.preventDefault()
|
|
62
|
+
step(1)
|
|
63
|
+
}
|
|
64
|
+
}}
|
|
65
|
+
>
|
|
66
|
+
{item && (
|
|
67
|
+
<>
|
|
68
|
+
<div className="border-border flex shrink-0 items-center gap-3 border-b px-4 py-3">
|
|
69
|
+
<div className="min-w-0 flex-1">
|
|
70
|
+
<Dialog.Title className="text-foreground truncate text-sm font-medium">
|
|
71
|
+
{item.name}
|
|
72
|
+
</Dialog.Title>
|
|
73
|
+
<Dialog.Description className="text-muted-foreground flex gap-2 text-xs">
|
|
74
|
+
<span className="whitespace-nowrap">
|
|
75
|
+
{index + 1} of {items.length}
|
|
76
|
+
</span>
|
|
77
|
+
{item.dimensions && (
|
|
78
|
+
<span className="whitespace-nowrap">{item.dimensions}</span>
|
|
79
|
+
)}
|
|
80
|
+
</Dialog.Description>
|
|
81
|
+
</div>
|
|
82
|
+
<Dialog.Close asChild>
|
|
83
|
+
<button
|
|
84
|
+
type="button"
|
|
85
|
+
aria-label="Close viewer"
|
|
86
|
+
className="text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:ring-ring prenta-touch-target flex h-8 w-8 items-center justify-center rounded-md transition-colors focus-visible:ring-2 focus-visible:outline-none"
|
|
87
|
+
>
|
|
88
|
+
<X size={16} aria-hidden />
|
|
89
|
+
</button>
|
|
90
|
+
</Dialog.Close>
|
|
91
|
+
</div>
|
|
92
|
+
<div className="bg-muted relative flex min-h-0 flex-1 items-center justify-center p-4 sm:px-16">
|
|
93
|
+
<img
|
|
94
|
+
src={item.url}
|
|
95
|
+
alt={item.alt}
|
|
96
|
+
className="max-h-full max-w-full object-contain"
|
|
97
|
+
/>
|
|
98
|
+
{items.length > 1 && (
|
|
99
|
+
<>
|
|
100
|
+
<button
|
|
101
|
+
type="button"
|
|
102
|
+
onClick={() => step(-1)}
|
|
103
|
+
aria-disabled={!hasPrev}
|
|
104
|
+
aria-label="Previous image"
|
|
105
|
+
className={`${navButton} left-3`}
|
|
106
|
+
>
|
|
107
|
+
<ChevronLeft size={18} aria-hidden />
|
|
108
|
+
</button>
|
|
109
|
+
<button
|
|
110
|
+
type="button"
|
|
111
|
+
onClick={() => step(1)}
|
|
112
|
+
aria-disabled={!hasNext}
|
|
113
|
+
aria-label="Next image"
|
|
114
|
+
className={`${navButton} right-3`}
|
|
115
|
+
>
|
|
116
|
+
<ChevronRight size={18} aria-hidden />
|
|
117
|
+
</button>
|
|
118
|
+
</>
|
|
119
|
+
)}
|
|
120
|
+
</div>
|
|
121
|
+
</>
|
|
122
|
+
)}
|
|
123
|
+
</Dialog.Content>
|
|
124
|
+
</Dialog.Portal>
|
|
125
|
+
</Dialog.Root>
|
|
126
|
+
)
|
|
127
|
+
}
|
|
@@ -12,6 +12,8 @@ export interface ConfirmDialogProps {
|
|
|
12
12
|
confirmLabel?: string
|
|
13
13
|
cancelLabel?: string
|
|
14
14
|
destructive?: boolean
|
|
15
|
+
/** See `Modal`'s `returnFocusFallback`. */
|
|
16
|
+
returnFocusFallback?: () => HTMLElement | null
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
export function ConfirmDialog({
|
|
@@ -23,12 +25,14 @@ export function ConfirmDialog({
|
|
|
23
25
|
confirmLabel = 'Confirm',
|
|
24
26
|
cancelLabel = 'Cancel',
|
|
25
27
|
destructive = false,
|
|
28
|
+
returnFocusFallback,
|
|
26
29
|
}: ConfirmDialogProps) {
|
|
27
30
|
return (
|
|
28
31
|
<Modal
|
|
29
32
|
open={open}
|
|
30
33
|
onClose={onClose}
|
|
31
34
|
title={title}
|
|
35
|
+
returnFocusFallback={returnFocusFallback}
|
|
32
36
|
actions={
|
|
33
37
|
<>
|
|
34
38
|
<Button variant="ghost" onClick={onClose}>
|
|
@@ -4,6 +4,7 @@ import * as Dialog from '@radix-ui/react-dialog'
|
|
|
4
4
|
import { X } from 'lucide-react'
|
|
5
5
|
import { type ReactNode, type RefObject, useRef } from 'react'
|
|
6
6
|
import { adminPortalContainer } from '../../lib/portal-container.js'
|
|
7
|
+
import { useDialogReturnFocus } from '../../lib/dialog-return-focus.js'
|
|
7
8
|
|
|
8
9
|
const MODAL_SIZE_CLASS = {
|
|
9
10
|
md: 'max-w-lg',
|
|
@@ -49,6 +50,12 @@ export interface ModalProps {
|
|
|
49
50
|
* The dialog stays open until the parent calls `onClose()`.
|
|
50
51
|
*/
|
|
51
52
|
onDismiss?: () => void
|
|
53
|
+
/**
|
|
54
|
+
* Focused on close when the control that opened the dialog is gone (for
|
|
55
|
+
* example the row a confirmed delete removed). Otherwise focus returns to
|
|
56
|
+
* that control.
|
|
57
|
+
*/
|
|
58
|
+
returnFocusFallback?: () => HTMLElement | null
|
|
52
59
|
}
|
|
53
60
|
|
|
54
61
|
/**
|
|
@@ -88,9 +95,11 @@ export function Modal({
|
|
|
88
95
|
description,
|
|
89
96
|
initialFocusRef,
|
|
90
97
|
onDismiss,
|
|
98
|
+
returnFocusFallback,
|
|
91
99
|
}: ModalProps) {
|
|
92
100
|
const scrollBody = size === 'xl'
|
|
93
101
|
const contentRef = useRef<HTMLDivElement>(null)
|
|
102
|
+
const returnFocus = useDialogReturnFocus(open, returnFocusFallback)
|
|
94
103
|
|
|
95
104
|
function handleDismissAttempt() {
|
|
96
105
|
if (onDismiss) onDismiss()
|
|
@@ -125,6 +134,7 @@ export function Modal({
|
|
|
125
134
|
event.preventDefault()
|
|
126
135
|
handleDismissAttempt()
|
|
127
136
|
}}
|
|
137
|
+
onCloseAutoFocus={returnFocus}
|
|
128
138
|
onOpenAutoFocus={
|
|
129
139
|
initialFocusRef
|
|
130
140
|
? (event) => {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Focus return for a controlled Radix dialog with no `Dialog.Trigger`. Radix's
|
|
5
|
+
* `onCloseAutoFocus` only focuses its trigger, so without one, closing drops
|
|
6
|
+
* focus on `<body>`. This records the focused element when `open` turns true
|
|
7
|
+
* (a layout effect, before Radix's FocusScope moves focus in) and returns
|
|
8
|
+
* focus there on close. If that element has left the DOM (a deleted row),
|
|
9
|
+
* `fallback` is focused instead.
|
|
10
|
+
*/
|
|
11
|
+
export function useDialogReturnFocus(
|
|
12
|
+
open: boolean,
|
|
13
|
+
fallback?: () => HTMLElement | null,
|
|
14
|
+
): (event: Event) => void {
|
|
15
|
+
const opener = useRef<HTMLElement | null>(null)
|
|
16
|
+
|
|
17
|
+
useLayoutEffect(() => {
|
|
18
|
+
if (!open) return
|
|
19
|
+
const active = document.activeElement
|
|
20
|
+
opener.current = active instanceof HTMLElement && active !== document.body ? active : null
|
|
21
|
+
}, [open])
|
|
22
|
+
|
|
23
|
+
return useCallback(
|
|
24
|
+
(event: Event) => {
|
|
25
|
+
event.preventDefault()
|
|
26
|
+
const el = opener.current
|
|
27
|
+
opener.current = null
|
|
28
|
+
const target = el?.isConnected ? el : (fallback?.() ?? null)
|
|
29
|
+
target?.focus()
|
|
30
|
+
},
|
|
31
|
+
[fallback],
|
|
32
|
+
)
|
|
33
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL that saves the file instead of opening it. Browsers ignore the
|
|
3
|
+
* `download` attribute on cross-origin links, so for Vercel Blob (the default
|
|
4
|
+
* media store) the attachment disposition comes from `?download=1`. Other
|
|
5
|
+
* hosts get the URL unchanged and rely on `download` for same-origin files.
|
|
6
|
+
*/
|
|
7
|
+
export function mediaDownloadUrl(url: string): string {
|
|
8
|
+
let parsed: URL
|
|
9
|
+
try {
|
|
10
|
+
parsed = new URL(url)
|
|
11
|
+
} catch {
|
|
12
|
+
return url
|
|
13
|
+
}
|
|
14
|
+
const host = parsed.hostname
|
|
15
|
+
if (host !== 'blob.vercel-storage.com' && !host.endsWith('.blob.vercel-storage.com')) return url
|
|
16
|
+
parsed.searchParams.set('download', '1')
|
|
17
|
+
return parsed.toString()
|
|
18
|
+
}
|