@foundrykit/media-plugin 0.2.0 → 0.3.1
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/README.md +69 -21
- package/dist/components/FolderPicker.d.ts +1 -1
- package/dist/components/FolderPicker.js +7 -1
- package/dist/components/FolderPicker.js.map +1 -1
- package/dist/components/FolderPreviews.d.ts +32 -0
- package/dist/components/FolderPreviews.js +283 -0
- package/dist/components/FolderPreviews.js.map +1 -0
- package/dist/components/FolderPreviews.test.d.ts +1 -0
- package/dist/components/MediaListView.js.map +1 -1
- package/dist/components/MediaViews.js.map +1 -1
- package/dist/components/media-plugin.css +104 -15
- package/dist/endpoints/folderPreviews.d.ts +32 -0
- package/dist/endpoints/folderPreviews.js +152 -0
- package/dist/endpoints/folderPreviews.js.map +1 -0
- package/dist/exports/client.d.ts +1 -0
- package/dist/exports/client.js +1 -0
- package/dist/exports/client.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +20 -2
- package/dist/index.js.map +1 -1
- package/package.json +30 -45
- package/screenshots/folder-previews.png +0 -0
- package/screenshots/grid-view.png +0 -0
- package/screenshots/picker-folders.png +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/components/MediaViews.tsx"],"sourcesContent":["'use client'
|
|
1
|
+
{"version":3,"sources":["../../src/components/MediaViews.tsx"],"sourcesContent":["'use client';\n\nimport { ChevronIcon, Pill, Popup, PopupList } from '@payloadcms/ui';\nimport { ToggleViewButtons } from '@payloadcms/ui/elements/FolderView/ToggleViewButtons';\nimport { useEffect, useRef, useState } from 'react';\nimport { createPortal } from 'react-dom';\n\nimport './media-plugin.css';\n\ntype View = 'grid' | 'list';\nconst KEY = 'foundrykit-media-view';\nconst COLUMNS_KEY = 'foundrykit-media-columns';\n/** Cards per row in the grid. 0 is \"Auto\": as many as fit at the minimum card width. */\nconst COLUMNS = [0, 2, 3, 4, 5, 6, 8];\n\nconst read = (key: string) => {\n try {\n return localStorage.getItem(key);\n } catch {\n // Storage can be blocked (private window); the defaults stand.\n return null;\n }\n};\nconst write = (key: string, value: string) => {\n try {\n localStorage.setItem(key, value);\n } catch {\n // As above: the choice just isn't remembered.\n }\n};\n\n/**\n * List / Grid beside Columns and Filters on an upload collection's list, on the full page and\n * in the picker drawer. It is Payload's own toggle from the By Folder view, so both views switch\n * the same way. Payload has no slot there, so it is portalled into the search bar of\n * the list this renders in (found from its own position, so a drawer open over the list page\n * gets its own switch). In grid view a \"Per row\" menu sets how many cards sit on each row. Both\n * choices are remembered per browser and set on the list as `data-media-view` and\n * `--media-grid-columns`; the grid itself is CSS over the same table (media-plugin.css).\n */\nexport function MediaViews() {\n const anchor = useRef<HTMLSpanElement>(null);\n const [list, setList] = useState<HTMLElement | null>(null);\n const [view, setView] = useState<View>('list');\n const [columns, setColumns] = useState(0);\n\n useEffect(() => {\n setList(anchor.current?.closest<HTMLElement>('.collection-list') ?? null);\n if (read(KEY) === 'grid') {\n setView('grid');\n }\n const saved = Number(read(COLUMNS_KEY));\n if (COLUMNS.includes(saved)) {\n setColumns(saved);\n }\n }, []);\n\n useEffect(() => {\n if (!list) {\n return;\n }\n list.dataset.mediaView = view;\n if (columns) {\n list.style.setProperty('--media-grid-columns', String(columns));\n } else {\n list.style.removeProperty('--media-grid-columns');\n }\n }, [list, view, columns]);\n\n const choose = (v: View) => {\n setView(v);\n write(KEY, v);\n };\n\n const chooseColumns = (n: number) => {\n setColumns(n);\n write(COLUMNS_KEY, String(n));\n };\n\n const target = list?.querySelector('.search-bar__actions');\n\n return (\n <>\n <span hidden ref={anchor} />\n {target\n ? createPortal(\n <>\n {view === 'grid' ? (\n <Popup\n button={\n <Pill icon={<ChevronIcon />} pillStyle='light' size='small'>\n Per row: {columns || 'Auto'}\n </Pill>\n }\n buttonType='custom'\n className='media-views__columns'\n horizontalAlign='right'\n render={({ close }) => (\n <PopupList.ButtonGroup>\n {COLUMNS.map(n => (\n <PopupList.Button\n active={n === columns}\n key={n}\n onClick={() => {\n chooseColumns(n);\n close();\n }}\n >\n {n || 'Auto'}\n </PopupList.Button>\n ))}\n </PopupList.ButtonGroup>\n )}\n size='small'\n />\n ) : null}\n <ToggleViewButtons activeView={view} setActiveView={choose} />\n </>,\n target\n )\n : null}\n </>\n );\n}\n"],"names":["ChevronIcon","Pill","Popup","PopupList","ToggleViewButtons","useEffect","useRef","useState","createPortal","KEY","COLUMNS_KEY","COLUMNS","read","key","localStorage","getItem","write","value","setItem","MediaViews","anchor","list","setList","view","setView","columns","setColumns","current","closest","saved","Number","includes","dataset","mediaView","style","setProperty","String","removeProperty","choose","v","chooseColumns","n","target","querySelector","span","hidden","ref","button","icon","pillStyle","size","buttonType","className","horizontalAlign","render","close","ButtonGroup","map","Button","active","onClick","activeView","setActiveView"],"mappings":"AAAA;;AAEA,SAASA,WAAW,EAAEC,IAAI,EAAEC,KAAK,EAAEC,SAAS,QAAQ,iBAAiB;AACrE,SAASC,iBAAiB,QAAQ,uDAAuD;AACzF,SAASC,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,QAAQ;AACpD,SAASC,YAAY,QAAQ,YAAY;AAEzC,OAAO,qBAAqB;AAG5B,MAAMC,MAAM;AACZ,MAAMC,cAAc;AACpB,sFAAsF,GACtF,MAAMC,UAAU;IAAC;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;CAAE;AAErC,MAAMC,OAAO,CAACC;IACZ,IAAI;QACF,OAAOC,aAAaC,OAAO,CAACF;IAC9B,EAAE,OAAM;QACN,+DAA+D;QAC/D,OAAO;IACT;AACF;AACA,MAAMG,QAAQ,CAACH,KAAaI;IAC1B,IAAI;QACFH,aAAaI,OAAO,CAACL,KAAKI;IAC5B,EAAE,OAAM;IACN,8CAA8C;IAChD;AACF;AAEA;;;;;;;;CAQC,GACD,OAAO,SAASE;IACd,MAAMC,SAASd,OAAwB;IACvC,MAAM,CAACe,MAAMC,QAAQ,GAAGf,SAA6B;IACrD,MAAM,CAACgB,MAAM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bS,MAAK;yBAEL;kCACJ,KAAC9C;wBAAkByD,YAAYtC;wBAAMuC,eAAexB;;;gBAEtDI,UAEF;;;AAGV"}
|
|
@@ -41,42 +41,46 @@
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/* Grid view (MediaViews.tsx), drawn over the same table */
|
|
44
|
-
.collection-list[data-media-view=
|
|
45
|
-
.collection-list[data-media-view=
|
|
44
|
+
.collection-list[data-media-view="grid"] .table table,
|
|
45
|
+
.collection-list[data-media-view="grid"] .table tbody {
|
|
46
46
|
display: block;
|
|
47
47
|
}
|
|
48
|
-
.collection-list[data-media-view=
|
|
49
|
-
.collection-list[data-media-view=
|
|
48
|
+
.collection-list[data-media-view="grid"] .table thead,
|
|
49
|
+
.collection-list[data-media-view="grid"]
|
|
50
|
+
.table
|
|
51
|
+
td:not(.cell-_select, .cell-filename) {
|
|
50
52
|
display: none;
|
|
51
53
|
}
|
|
52
|
-
.collection-list[data-media-view=
|
|
54
|
+
.collection-list[data-media-view="grid"] .table tbody {
|
|
53
55
|
display: grid;
|
|
54
56
|
grid-template-columns: repeat(auto-fill, minmax(9.5rem, 1fr));
|
|
55
57
|
gap: 1rem;
|
|
56
58
|
}
|
|
57
59
|
/* A fixed number per row, chosen from "Per row" (set on the list as a custom property). */
|
|
58
|
-
.collection-list[data-media-view=
|
|
60
|
+
.collection-list[data-media-view="grid"][style*="--media-grid-columns"]
|
|
61
|
+
.table
|
|
62
|
+
tbody {
|
|
59
63
|
grid-template-columns: repeat(var(--media-grid-columns), minmax(0, 1fr));
|
|
60
64
|
}
|
|
61
|
-
.collection-list[data-media-view=
|
|
65
|
+
.collection-list[data-media-view="grid"] .table tr {
|
|
62
66
|
position: relative;
|
|
63
67
|
display: block;
|
|
64
68
|
overflow: hidden;
|
|
65
69
|
border-radius: 0.75rem;
|
|
66
70
|
background: var(--theme-elevation-50);
|
|
67
71
|
}
|
|
68
|
-
.collection-list[data-media-view=
|
|
72
|
+
.collection-list[data-media-view="grid"] .table td {
|
|
69
73
|
display: block;
|
|
70
74
|
padding: 0;
|
|
71
75
|
}
|
|
72
|
-
.collection-list[data-media-view=
|
|
76
|
+
.collection-list[data-media-view="grid"] .cell-_select {
|
|
73
77
|
position: absolute;
|
|
74
78
|
top: 0.5rem;
|
|
75
79
|
left: 0.5rem;
|
|
76
80
|
z-index: 1;
|
|
77
81
|
}
|
|
78
82
|
/* A link on the full page, a button inside the picker drawer. */
|
|
79
|
-
.collection-list[data-media-view=
|
|
83
|
+
.collection-list[data-media-view="grid"] .cell-filename :is(a, button) {
|
|
80
84
|
display: block;
|
|
81
85
|
width: 100%;
|
|
82
86
|
padding: 0;
|
|
@@ -86,10 +90,10 @@
|
|
|
86
90
|
text-decoration: none;
|
|
87
91
|
cursor: pointer;
|
|
88
92
|
}
|
|
89
|
-
.collection-list[data-media-view=
|
|
93
|
+
.collection-list[data-media-view="grid"] .file {
|
|
90
94
|
display: block;
|
|
91
95
|
}
|
|
92
|
-
.collection-list[data-media-view=
|
|
96
|
+
.collection-list[data-media-view="grid"] .file__thumbnail {
|
|
93
97
|
width: 100%;
|
|
94
98
|
max-width: none;
|
|
95
99
|
max-height: none;
|
|
@@ -97,12 +101,12 @@
|
|
|
97
101
|
aspect-ratio: 1;
|
|
98
102
|
border-radius: 0;
|
|
99
103
|
}
|
|
100
|
-
.collection-list[data-media-view=
|
|
104
|
+
.collection-list[data-media-view="grid"] .file__thumbnail img {
|
|
101
105
|
width: 100%;
|
|
102
106
|
height: 100%;
|
|
103
107
|
object-fit: cover;
|
|
104
108
|
}
|
|
105
|
-
.collection-list[data-media-view=
|
|
109
|
+
.collection-list[data-media-view="grid"] .file__filename {
|
|
106
110
|
display: block;
|
|
107
111
|
margin: 0;
|
|
108
112
|
padding: 0.625rem 0.75rem;
|
|
@@ -111,6 +115,91 @@
|
|
|
111
115
|
white-space: nowrap;
|
|
112
116
|
text-overflow: ellipsis;
|
|
113
117
|
}
|
|
114
|
-
.collection-list[data-media-view=
|
|
118
|
+
.collection-list[data-media-view="grid"]
|
|
119
|
+
.cell-filename
|
|
120
|
+
:is(a, button):hover
|
|
121
|
+
.file__filename {
|
|
115
122
|
text-decoration: underline;
|
|
116
123
|
}
|
|
124
|
+
|
|
125
|
+
/* Folder card previews (FolderPreviews.tsx): thumbnails above the name, a count below it */
|
|
126
|
+
.folder-file-card--folder.media-folder-preview--on:has(
|
|
127
|
+
.media-folder-preview__strip:not(:empty)
|
|
128
|
+
) {
|
|
129
|
+
grid-template-rows: auto auto;
|
|
130
|
+
grid-template-areas:
|
|
131
|
+
"preview"
|
|
132
|
+
"details";
|
|
133
|
+
}
|
|
134
|
+
.media-folder-preview__strip {
|
|
135
|
+
grid-area: preview;
|
|
136
|
+
pointer-events: none;
|
|
137
|
+
container-type: inline-size;
|
|
138
|
+
}
|
|
139
|
+
.media-folder-preview__strip:empty {
|
|
140
|
+
display: none;
|
|
141
|
+
}
|
|
142
|
+
.media-folder-preview__thumbs {
|
|
143
|
+
display: grid;
|
|
144
|
+
grid-template-columns: repeat(3, minmax(0, 1fr));
|
|
145
|
+
gap: 0.25rem;
|
|
146
|
+
padding: 0.5rem 0.5rem 0;
|
|
147
|
+
}
|
|
148
|
+
.media-folder-preview__thumbs > * {
|
|
149
|
+
aspect-ratio: 1;
|
|
150
|
+
overflow: hidden;
|
|
151
|
+
border-radius: var(--style-radius-s);
|
|
152
|
+
background: var(--theme-elevation-100);
|
|
153
|
+
}
|
|
154
|
+
.media-folder-preview__thumbs img {
|
|
155
|
+
display: block;
|
|
156
|
+
width: 100%;
|
|
157
|
+
height: 100%;
|
|
158
|
+
object-fit: cover;
|
|
159
|
+
}
|
|
160
|
+
.media-folder-preview__file,
|
|
161
|
+
.media-folder-preview__more {
|
|
162
|
+
display: flex;
|
|
163
|
+
align-items: center;
|
|
164
|
+
justify-content: center;
|
|
165
|
+
font-size: 0.6875rem;
|
|
166
|
+
font-weight: 600;
|
|
167
|
+
color: var(--theme-elevation-600);
|
|
168
|
+
}
|
|
169
|
+
/* With three thumbnails, "+9" sits over the last one rather than taking a fourth slot. */
|
|
170
|
+
.media-folder-preview__thumbs:has(> :nth-child(4)) > :nth-child(3),
|
|
171
|
+
.media-folder-preview__thumbs:has(> :nth-child(4))
|
|
172
|
+
> .media-folder-preview__more {
|
|
173
|
+
grid-area: 1 / 3;
|
|
174
|
+
}
|
|
175
|
+
.media-folder-preview__none {
|
|
176
|
+
grid-column: 1 / -1;
|
|
177
|
+
aspect-ratio: auto;
|
|
178
|
+
display: flex;
|
|
179
|
+
align-items: center;
|
|
180
|
+
justify-content: center;
|
|
181
|
+
/* One thumbnail tall: a third of the strip's width, less the gaps. */
|
|
182
|
+
height: calc((100cqi - 1rem - 0.5rem) / 3);
|
|
183
|
+
font-size: 0.75rem;
|
|
184
|
+
color: var(--theme-elevation-500);
|
|
185
|
+
}
|
|
186
|
+
.media-folder-preview__more {
|
|
187
|
+
z-index: 1;
|
|
188
|
+
background: color-mix(in srgb, var(--theme-elevation-1000) 45%, transparent);
|
|
189
|
+
color: var(--theme-elevation-0);
|
|
190
|
+
font-size: 0.875rem;
|
|
191
|
+
}
|
|
192
|
+
.media-folder-preview__meta:empty {
|
|
193
|
+
display: none;
|
|
194
|
+
}
|
|
195
|
+
.media-folder-preview__meta {
|
|
196
|
+
margin: 0;
|
|
197
|
+
font-size: 0.75rem;
|
|
198
|
+
line-height: normal;
|
|
199
|
+
color: var(--assigned-collections-color, var(--theme-elevation-800));
|
|
200
|
+
}
|
|
201
|
+
/* Our line says what is inside, so the folder-type line ("Media") makes way once it loads. */
|
|
202
|
+
.folder-file-card__titlebar-labels:has(.media-folder-preview__meta:not(:empty))
|
|
203
|
+
.folder-file-card__assigned-collections {
|
|
204
|
+
display: none;
|
|
205
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Endpoint, PayloadRequest } from 'payload';
|
|
2
|
+
export type FolderPreview = {
|
|
3
|
+
/** Documents directly in the folder, per collection slug. Only collections with any are listed. */
|
|
4
|
+
counts: Record<string, number>;
|
|
5
|
+
/** Subfolders directly in the folder. */
|
|
6
|
+
folders: number;
|
|
7
|
+
/** The folder's name; the client matches By Folder cards on it, as their ids aren't in the page. */
|
|
8
|
+
name?: string;
|
|
9
|
+
/** Up to `PREVIEWS` of the newest uploads in the folder, images first. */
|
|
10
|
+
previews: {
|
|
11
|
+
filename?: string;
|
|
12
|
+
mimeType?: string;
|
|
13
|
+
thumbnail?: string;
|
|
14
|
+
}[];
|
|
15
|
+
};
|
|
16
|
+
export declare const PREVIEWS = 3;
|
|
17
|
+
/**
|
|
18
|
+
* Counts and first few thumbnails for each folder id, read as the requesting user so their
|
|
19
|
+
* read access applies: a folder never previews documents they could not open.
|
|
20
|
+
*/
|
|
21
|
+
export declare const folderPreviews: (req: PayloadRequest, ids: (number | string)[]) => Promise<Record<string, FolderPreview>>;
|
|
22
|
+
/** The subfolders of `parent` (`'root'` for the top level), as `{ id, name }`. */
|
|
23
|
+
export declare const childFolders: (req: PayloadRequest, parent: string) => Promise<{
|
|
24
|
+
id: string | number;
|
|
25
|
+
name: string | undefined;
|
|
26
|
+
}[]>;
|
|
27
|
+
/**
|
|
28
|
+
* `GET /api/media-plugin/folder-previews?ids=1,2,3` previews those folders.
|
|
29
|
+
* `GET /api/media-plugin/folder-previews?parent=12` (or `parent=root`) previews every subfolder
|
|
30
|
+
* of folder 12, with names.
|
|
31
|
+
*/
|
|
32
|
+
export declare const folderPreviewsEndpoint: Endpoint;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
export const PREVIEWS = 3;
|
|
2
|
+
/** One request covers the folders on screen; more than this is a client bug, not a real page. */ const MAX_IDS = 200;
|
|
3
|
+
const thumbnailOf = (doc, adminThumbnail)=>{
|
|
4
|
+
if (!doc.mimeType?.startsWith('image/')) {
|
|
5
|
+
return undefined;
|
|
6
|
+
}
|
|
7
|
+
const size = typeof adminThumbnail === 'string' ? doc.sizes?.[adminThumbnail]?.url : undefined;
|
|
8
|
+
return size || doc.url || undefined;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Counts and first few thumbnails for each folder id, read as the requesting user so their
|
|
12
|
+
* read access applies: a folder never previews documents they could not open.
|
|
13
|
+
*/ export const folderPreviews = async (req, ids)=>{
|
|
14
|
+
const { payload } = req;
|
|
15
|
+
const folders = payload.config.folders;
|
|
16
|
+
if (!folders) {
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
19
|
+
const fieldName = folders.fieldName;
|
|
20
|
+
const inFolder = (id)=>({
|
|
21
|
+
[fieldName]: {
|
|
22
|
+
equals: id
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
const collections = payload.config.collections.filter((c)=>c.folders && c.slug !== folders.slug);
|
|
26
|
+
const entries = await Promise.all(ids.map(async (id)=>{
|
|
27
|
+
const [subfolders, perCollection] = await Promise.all([
|
|
28
|
+
payload.count({
|
|
29
|
+
collection: folders.slug,
|
|
30
|
+
overrideAccess: false,
|
|
31
|
+
req,
|
|
32
|
+
where: inFolder(id)
|
|
33
|
+
}),
|
|
34
|
+
Promise.all(collections.map(async (c)=>{
|
|
35
|
+
const slug = c.slug;
|
|
36
|
+
if (!c.upload) {
|
|
37
|
+
const { totalDocs } = await payload.count({
|
|
38
|
+
collection: slug,
|
|
39
|
+
overrideAccess: false,
|
|
40
|
+
req,
|
|
41
|
+
where: inFolder(id)
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
slug,
|
|
45
|
+
docs: [],
|
|
46
|
+
total: totalDocs
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const result = await payload.find({
|
|
50
|
+
collection: slug,
|
|
51
|
+
depth: 0,
|
|
52
|
+
limit: PREVIEWS,
|
|
53
|
+
overrideAccess: false,
|
|
54
|
+
req,
|
|
55
|
+
sort: '-createdAt',
|
|
56
|
+
where: inFolder(id)
|
|
57
|
+
});
|
|
58
|
+
const adminThumbnail = c.upload.adminThumbnail;
|
|
59
|
+
return {
|
|
60
|
+
slug,
|
|
61
|
+
docs: result.docs.map((d)=>({
|
|
62
|
+
...d,
|
|
63
|
+
thumbnail: thumbnailOf(d, adminThumbnail)
|
|
64
|
+
})),
|
|
65
|
+
total: result.totalDocs
|
|
66
|
+
};
|
|
67
|
+
}))
|
|
68
|
+
]);
|
|
69
|
+
const counts = {};
|
|
70
|
+
for (const { slug, total } of perCollection){
|
|
71
|
+
if (total) {
|
|
72
|
+
counts[slug] = total;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const previews = perCollection.flatMap(({ docs })=>docs)// Images first, so a folder of photos and PDFs leads with photos.
|
|
76
|
+
.sort((a, b)=>Number(Boolean(b.thumbnail)) - Number(Boolean(a.thumbnail))).slice(0, PREVIEWS).map((d)=>({
|
|
77
|
+
filename: d.filename ?? undefined,
|
|
78
|
+
mimeType: d.mimeType ?? undefined,
|
|
79
|
+
thumbnail: d.thumbnail
|
|
80
|
+
}));
|
|
81
|
+
return [
|
|
82
|
+
String(id),
|
|
83
|
+
{
|
|
84
|
+
counts,
|
|
85
|
+
folders: subfolders.totalDocs,
|
|
86
|
+
previews
|
|
87
|
+
}
|
|
88
|
+
];
|
|
89
|
+
}));
|
|
90
|
+
return Object.fromEntries(entries);
|
|
91
|
+
};
|
|
92
|
+
/** The subfolders of `parent` (`'root'` for the top level), as `{ id, name }`. */ export const childFolders = async (req, parent)=>{
|
|
93
|
+
const folders = req.payload.config.folders;
|
|
94
|
+
if (!folders) {
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
const { docs } = await req.payload.find({
|
|
98
|
+
collection: folders.slug,
|
|
99
|
+
depth: 0,
|
|
100
|
+
limit: MAX_IDS,
|
|
101
|
+
overrideAccess: false,
|
|
102
|
+
pagination: false,
|
|
103
|
+
req,
|
|
104
|
+
where: parent === 'root' ? {
|
|
105
|
+
[folders.fieldName]: {
|
|
106
|
+
exists: false
|
|
107
|
+
}
|
|
108
|
+
} : {
|
|
109
|
+
[folders.fieldName]: {
|
|
110
|
+
equals: parent
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
return docs.map((d)=>({
|
|
115
|
+
id: d.id,
|
|
116
|
+
name: d.name
|
|
117
|
+
}));
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* `GET /api/media-plugin/folder-previews?ids=1,2,3` previews those folders.
|
|
121
|
+
* `GET /api/media-plugin/folder-previews?parent=12` (or `parent=root`) previews every subfolder
|
|
122
|
+
* of folder 12, with names.
|
|
123
|
+
*/ export const folderPreviewsEndpoint = {
|
|
124
|
+
handler: async (req)=>{
|
|
125
|
+
if (!req.user) {
|
|
126
|
+
return Response.json({
|
|
127
|
+
error: 'Unauthorized'
|
|
128
|
+
}, {
|
|
129
|
+
status: 401
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const parent = typeof req.query?.parent === 'string' ? req.query.parent : undefined;
|
|
133
|
+
if (parent) {
|
|
134
|
+
const children = await childFolders(req, parent);
|
|
135
|
+
const previews = await folderPreviews(req, children.map((c)=>c.id));
|
|
136
|
+
for (const c of children){
|
|
137
|
+
const entry = previews[String(c.id)];
|
|
138
|
+
if (entry) {
|
|
139
|
+
entry.name = c.name;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return Response.json(previews);
|
|
143
|
+
}
|
|
144
|
+
const raw = typeof req.query?.ids === 'string' ? req.query.ids : '';
|
|
145
|
+
const ids = raw.split(',').map((s)=>s.trim()).filter(Boolean).slice(0, MAX_IDS);
|
|
146
|
+
return Response.json(await folderPreviews(req, ids));
|
|
147
|
+
},
|
|
148
|
+
method: 'get',
|
|
149
|
+
path: '/media-plugin/folder-previews'
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
//# sourceMappingURL=folderPreviews.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/endpoints/folderPreviews.ts"],"sourcesContent":["import type { Endpoint, PayloadRequest, Where } from 'payload';\n\nexport type FolderPreview = {\n /** Documents directly in the folder, per collection slug. Only collections with any are listed. */\n counts: Record<string, number>;\n /** Subfolders directly in the folder. */\n folders: number;\n /** The folder's name; the client matches By Folder cards on it, as their ids aren't in the page. */\n name?: string;\n /** Up to `PREVIEWS` of the newest uploads in the folder, images first. */\n previews: { filename?: string; mimeType?: string; thumbnail?: string }[];\n};\n\nexport const PREVIEWS = 3;\n/** One request covers the folders on screen; more than this is a client bug, not a real page. */\nconst MAX_IDS = 200;\n\ntype UploadDoc = {\n filename?: null | string;\n mimeType?: null | string;\n sizes?: Record<string, { url?: null | string } | undefined>;\n url?: null | string;\n};\n\nconst thumbnailOf = (doc: UploadDoc, adminThumbnail: unknown) => {\n if (!doc.mimeType?.startsWith('image/')) {\n return undefined;\n }\n const size =\n typeof adminThumbnail === 'string'\n ? doc.sizes?.[adminThumbnail]?.url\n : undefined;\n return size || doc.url || undefined;\n};\n\n/**\n * Counts and first few thumbnails for each folder id, read as the requesting user so their\n * read access applies: a folder never previews documents they could not open.\n */\nexport const folderPreviews = async (\n req: PayloadRequest,\n ids: (number | string)[]\n): Promise<Record<string, FolderPreview>> => {\n const { payload } = req;\n const folders = payload.config.folders;\n if (!folders) {\n return {};\n }\n const fieldName = folders.fieldName;\n const inFolder = (id: number | string): Where => ({\n [fieldName]: { equals: id },\n });\n const collections = payload.config.collections.filter(\n c => c.folders && c.slug !== folders.slug\n );\n\n const entries = await Promise.all(\n ids.map(async id => {\n const [subfolders, perCollection] = await Promise.all([\n payload.count({\n collection: folders.slug,\n overrideAccess: false,\n req,\n where: inFolder(id),\n }),\n Promise.all(\n collections.map(async c => {\n const slug = c.slug;\n if (!c.upload) {\n const { totalDocs } = await payload.count({\n collection: slug,\n overrideAccess: false,\n req,\n where: inFolder(id),\n });\n return { slug, docs: [] as UploadDoc[], total: totalDocs };\n }\n const result = await payload.find({\n collection: slug,\n depth: 0,\n limit: PREVIEWS,\n overrideAccess: false,\n req,\n sort: '-createdAt',\n where: inFolder(id),\n });\n const adminThumbnail = c.upload.adminThumbnail;\n return {\n slug,\n docs: (result.docs as UploadDoc[]).map(d => ({\n ...d,\n thumbnail: thumbnailOf(d, adminThumbnail),\n })),\n total: result.totalDocs,\n };\n })\n ),\n ]);\n\n const counts: Record<string, number> = {};\n for (const { slug, total } of perCollection) {\n if (total) {\n counts[slug] = total;\n }\n }\n const previews = perCollection\n .flatMap(({ docs }) => docs as ({ thumbnail?: string } & UploadDoc)[])\n // Images first, so a folder of photos and PDFs leads with photos.\n .sort(\n (a, b) => Number(Boolean(b.thumbnail)) - Number(Boolean(a.thumbnail))\n )\n .slice(0, PREVIEWS)\n .map(d => ({\n filename: d.filename ?? undefined,\n mimeType: d.mimeType ?? undefined,\n thumbnail: d.thumbnail,\n }));\n\n return [\n String(id),\n { counts, folders: subfolders.totalDocs, previews },\n ] as const;\n })\n );\n\n return Object.fromEntries(entries);\n};\n\n/** The subfolders of `parent` (`'root'` for the top level), as `{ id, name }`. */\nexport const childFolders = async (req: PayloadRequest, parent: string) => {\n const folders = req.payload.config.folders;\n if (!folders) {\n return [];\n }\n const { docs } = await req.payload.find({\n collection: folders.slug,\n depth: 0,\n limit: MAX_IDS,\n overrideAccess: false,\n pagination: false,\n req,\n where:\n parent === 'root'\n ? { [folders.fieldName]: { exists: false } }\n : { [folders.fieldName]: { equals: parent } },\n });\n return (docs as { id: number | string; name?: string }[]).map(d => ({\n id: d.id,\n name: d.name,\n }));\n};\n\n/**\n * `GET /api/media-plugin/folder-previews?ids=1,2,3` previews those folders.\n * `GET /api/media-plugin/folder-previews?parent=12` (or `parent=root`) previews every subfolder\n * of folder 12, with names.\n */\nexport const folderPreviewsEndpoint: Endpoint = {\n handler: async req => {\n if (!req.user) {\n return Response.json({ error: 'Unauthorized' }, { status: 401 });\n }\n const parent =\n typeof req.query?.parent === 'string' ? req.query.parent : undefined;\n if (parent) {\n const children = await childFolders(req, parent);\n const previews = await folderPreviews(\n req,\n children.map(c => c.id)\n );\n for (const c of children) {\n const entry = previews[String(c.id)];\n if (entry) {\n entry.name = c.name;\n }\n }\n return Response.json(previews);\n }\n const raw = typeof req.query?.ids === 'string' ? req.query.ids : '';\n const ids = raw\n .split(',')\n .map(s => s.trim())\n .filter(Boolean)\n .slice(0, MAX_IDS);\n return Response.json(await folderPreviews(req, ids));\n },\n method: 'get',\n path: '/media-plugin/folder-previews',\n};\n"],"names":["PREVIEWS","MAX_IDS","thumbnailOf","doc","adminThumbnail","mimeType","startsWith","undefined","size","sizes","url","folderPreviews","req","ids","payload","folders","config","fieldName","inFolder","id","equals","collections","filter","c","slug","entries","Promise","all","map","subfolders","perCollection","count","collection","overrideAccess","where","upload","totalDocs","docs","total","result","find","depth","limit","sort","d","thumbnail","counts","previews","flatMap","a","b","Number","Boolean","slice","filename","String","Object","fromEntries","childFolders","parent","pagination","exists","name","folderPreviewsEndpoint","handler","user","Response","json","error","status","query","children","entry","raw","split","s","trim","method","path"],"mappings":"AAaA,OAAO,MAAMA,WAAW,EAAE;AAC1B,+FAA+F,GAC/F,MAAMC,UAAU;AAShB,MAAMC,cAAc,CAACC,KAAgBC;IACnC,IAAI,CAACD,IAAIE,QAAQ,EAAEC,WAAW,WAAW;QACvC,OAAOC;IACT;IACA,MAAMC,OACJ,OAAOJ,mBAAmB,WACtBD,IAAIM,KAAK,EAAE,CAACL,eAAe,EAAEM,MAC7BH;IACN,OAAOC,QAAQL,IAAIO,GAAG,IAAIH;AAC5B;AAEA;;;CAGC,GACD,OAAO,MAAMI,iBAAiB,OAC5BC,KACAC;IAEA,MAAM,EAAEC,OAAO,EAAE,GAAGF;IACpB,MAAMG,UAAUD,QAAQE,MAAM,CAACD,OAAO;IACtC,IAAI,CAACA,SAAS;QACZ,OAAO,CAAC;IACV;IACA,MAAME,YAAYF,QAAQE,SAAS;IACnC,MAAMC,WAAW,CAACC,KAAgC,CAAA;YAChD,CAACF,UAAU,EAAE;gBAAEG,QAAQD;YAAG;QAC5B,CAAA;IACA,MAAME,cAAcP,QAAQE,MAAM,CAACK,WAAW,CAACC,MAAM,CACnDC,C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C9CC,SAAS,OAAMpD;QACb,IAAI,CAACA,IAAIqD,IAAI,EAAE;YACb,OAAOC,SAASC,IAAI,CAAC;gBAAEC,OAAO;YAAe,GAAG;gBAAEC,QAAQ;YAAI;QAChE;QACA,MAAMV,SACJ,OAAO/C,IAAI0D,KAAK,EAAEX,WAAW,WAAW/C,IAAI0D,KAAK,CAACX,MAAM,GAAGpD;QAC7D,IAAIoD,QAAQ;YACV,MAAMY,WAAW,MAAMb,aAAa9C,KAAK+C;YACzC,MAAMZ,WAAW,MAAMpC,eACrBC,KACA2D,SAAS3C,GAAG,CAACL,CAAAA,IAAKA,EAAEJ,EAAE;YAExB,KAAK,MAAMI,KAAKgD,SAAU;gBACxB,MAAMC,QAAQzB,QAAQ,CAACQ,OAAOhC,EAAEJ,EAAE,EAAE;gBACpC,IAAIqD,OAAO;oBACTA,MAAMV,IAAI,GAAGvC,EAAEuC,IAAI;gBACrB;YACF;YACA,OAAOI,SAASC,IAAI,CAACpB;QACvB;QACA,MAAM0B,MAAM,OAAO7D,IAAI0D,KAAK,EAAEzD,QAAQ,WAAWD,IAAI0D,KAAK,CAACzD,GAAG,GAAG;QACjE,MAAMA,MAAM4D,IACTC,KAAK,CAAC,KACN9C,GAAG,CAAC+C,CAAAA,IAAKA,EAAEC,IAAI,IACftD,MAAM,CAAC8B,SACPC,KAAK,CAAC,GAAGpD;QACZ,OAAOiE,SAASC,IAAI,CAAC,MAAMxD,eAAeC,KAAKC;IACjD;IACAgE,QAAQ;IACRC,MAAM;AACR,EAAE"}
|
package/dist/exports/client.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { FolderPicker } from '../components/FolderPicker.js';
|
|
2
|
+
export { FolderPreviews, FolderPreviewsProvider, } from '../components/FolderPreviews.js';
|
|
2
3
|
export { MediaListView } from '../components/MediaListView.js';
|
|
3
4
|
export { MediaViews } from '../components/MediaViews.js';
|
package/dist/exports/client.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { FolderPicker } from '../components/FolderPicker.js';
|
|
2
|
+
export { FolderPreviews, FolderPreviewsProvider } from '../components/FolderPreviews.js';
|
|
2
3
|
export { MediaListView } from '../components/MediaListView.js';
|
|
3
4
|
export { MediaViews } from '../components/MediaViews.js';
|
|
4
5
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/exports/client.ts"],"sourcesContent":["export { FolderPicker } from '../components/FolderPicker.js'\nexport { MediaListView } from '../components/MediaListView.js'
|
|
1
|
+
{"version":3,"sources":["../../src/exports/client.ts"],"sourcesContent":["export { FolderPicker } from '../components/FolderPicker.js';\nexport {\n FolderPreviews,\n FolderPreviewsProvider,\n} from '../components/FolderPreviews.js';\nexport { MediaListView } from '../components/MediaListView.js';\nexport { MediaViews } from '../components/MediaViews.js';\n"],"names":["FolderPicker","FolderPreviews","FolderPreviewsProvider","MediaListView","MediaViews"],"mappings":"AAAA,SAASA,YAAY,QAAQ,gCAAgC;AAC7D,SACEC,cAAc,EACdC,sBAAsB,QACjB,kCAAkC;AACzC,SAASC,aAAa,QAAQ,iCAAiC;AAC/D,SAASC,UAAU,QAAQ,8BAA8B"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { Config } from 'payload';
|
|
2
|
+
export type { FolderPreview } from './endpoints/folderPreviews.js';
|
|
2
3
|
export type MediaPluginConfig = {
|
|
3
4
|
/** Upload collections to enhance. Defaults to every upload collection. */
|
|
4
5
|
collections?: string[];
|
|
5
6
|
/** Folders tab inside the image picker drawer. Needs `folders: true` on the collection. Default true. */
|
|
6
7
|
folderPicker?: boolean;
|
|
8
|
+
/** First few thumbnails and a "what's inside" count on folder cards. Default true. */
|
|
9
|
+
folderPreviews?: boolean;
|
|
7
10
|
/** List / Grid switch on the list, full page and picker drawer. Default true. */
|
|
8
11
|
gridView?: boolean;
|
|
9
12
|
};
|
|
@@ -12,4 +15,4 @@ export type MediaPluginConfig = {
|
|
|
12
15
|
* editors can choose images from inside folders. A collection that already has its own list
|
|
13
16
|
* view keeps it.
|
|
14
17
|
*/
|
|
15
|
-
export declare const mediaPlugin: ({ collections, folderPicker, gridView }?: MediaPluginConfig) => (config: Config) => Config;
|
|
18
|
+
export declare const mediaPlugin: ({ collections, folderPicker, folderPreviews, gridView, }?: MediaPluginConfig) => (config: Config) => Config;
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { folderPreviewsEndpoint } from './endpoints/folderPreviews.js';
|
|
1
2
|
const LIST_VIEW = '@foundrykit/media-plugin/client#MediaListView';
|
|
2
3
|
const MEDIA_VIEWS = '@foundrykit/media-plugin/client#MediaViews';
|
|
4
|
+
const FOLDER_PREVIEWS = '@foundrykit/media-plugin/client#FolderPreviewsProvider';
|
|
3
5
|
/**
|
|
4
6
|
* A List / Grid switch on upload collection lists, and a Folders tab in the picker drawer so
|
|
5
7
|
* editors can choose images from inside folders. A collection that already has its own list
|
|
6
8
|
* view keeps it.
|
|
7
|
-
*/ export const mediaPlugin = ({ collections, folderPicker = true, gridView = true } = {})=>(config)=>({
|
|
9
|
+
*/ export const mediaPlugin = ({ collections, folderPicker = true, folderPreviews = true, gridView = true } = {})=>(config)=>({
|
|
8
10
|
...config,
|
|
9
11
|
collections: config.collections?.map((collection)=>{
|
|
10
12
|
const wanted = collections ? collections.includes(collection.slug) : Boolean(collection.upload);
|
|
@@ -12,7 +14,23 @@ const MEDIA_VIEWS = '@foundrykit/media-plugin/client#MediaViews';
|
|
|
12
14
|
folderPicker,
|
|
13
15
|
gridView
|
|
14
16
|
}) : collection;
|
|
15
|
-
})
|
|
17
|
+
}),
|
|
18
|
+
// By Folder renders no plugin components inside it (Payload 3.90), so the card previews
|
|
19
|
+
// there come from a provider that wraps the whole admin. The picker renders its own.
|
|
20
|
+
admin: folderPreviews ? {
|
|
21
|
+
...config.admin,
|
|
22
|
+
components: {
|
|
23
|
+
...config.admin?.components,
|
|
24
|
+
providers: [
|
|
25
|
+
...config.admin?.components?.providers ?? [],
|
|
26
|
+
FOLDER_PREVIEWS
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
} : config.admin,
|
|
30
|
+
endpoints: folderPreviews ? [
|
|
31
|
+
...config.endpoints ?? [],
|
|
32
|
+
folderPreviewsEndpoint
|
|
33
|
+
] : config.endpoints
|
|
16
34
|
});
|
|
17
35
|
function enhance(collection, { folderPicker, gridView }) {
|
|
18
36
|
const components = collection.admin?.components ?? {};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { CollectionConfig, Config } from 'payload'\n\nexport type MediaPluginConfig = {\n /** Upload collections to enhance. Defaults to every upload collection. */\n collections?: string[]
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { CollectionConfig, Config } from 'payload';\n\nimport { folderPreviewsEndpoint } from './endpoints/folderPreviews.js';\n\nexport type { FolderPreview } from './endpoints/folderPreviews.js';\n\nexport type MediaPluginConfig = {\n /** Upload collections to enhance. Defaults to every upload collection. */\n collections?: string[];\n /** Folders tab inside the image picker drawer. Needs `folders: true` on the collection. Default true. */\n folderPicker?: boolean;\n /** First few thumbnails and a \"what's inside\" count on folder cards. Default true. */\n folderPreviews?: boolean;\n /** List / Grid switch on the list, full page and picker drawer. Default true. */\n gridView?: boolean;\n};\n\nconst LIST_VIEW = '@foundrykit/media-plugin/client#MediaListView';\nconst MEDIA_VIEWS = '@foundrykit/media-plugin/client#MediaViews';\nconst FOLDER_PREVIEWS =\n '@foundrykit/media-plugin/client#FolderPreviewsProvider';\n\n/**\n * A List / Grid switch on upload collection lists, and a Folders tab in the picker drawer so\n * editors can choose images from inside folders. A collection that already has its own list\n * view keeps it.\n */\nexport const mediaPlugin =\n ({\n collections,\n folderPicker = true,\n folderPreviews = true,\n gridView = true,\n }: MediaPluginConfig = {}) =>\n (config: Config): Config => ({\n ...config,\n collections: config.collections?.map(collection => {\n const wanted = collections\n ? collections.includes(collection.slug)\n : Boolean(collection.upload);\n return wanted\n ? enhance(collection, { folderPicker, gridView })\n : collection;\n }),\n // By Folder renders no plugin components inside it (Payload 3.90), so the card previews\n // there come from a provider that wraps the whole admin. The picker renders its own.\n admin: folderPreviews\n ? {\n ...config.admin,\n components: {\n ...config.admin?.components,\n providers: [\n ...(config.admin?.components?.providers ?? []),\n FOLDER_PREVIEWS,\n ],\n },\n }\n : config.admin,\n endpoints: folderPreviews\n ? [...(config.endpoints ?? []), folderPreviewsEndpoint]\n : config.endpoints,\n });\n\nfunction enhance(\n collection: CollectionConfig,\n { folderPicker, gridView }: { folderPicker: boolean; gridView: boolean }\n): CollectionConfig {\n const components = collection.admin?.components ?? {};\n const views = components.views ?? {};\n const beforeListTable = components.beforeListTable ?? [];\n\n return {\n ...collection,\n admin: {\n ...collection.admin,\n components: {\n ...components,\n beforeListTable: gridView\n ? [...beforeListTable, MEDIA_VIEWS]\n : beforeListTable,\n views: {\n ...views,\n list:\n folderPicker && !views.list ? { Component: LIST_VIEW } : views.list,\n },\n },\n },\n };\n}\n"],"names":["folderPreviewsEndpoint","LIST_VIEW","MEDIA_VIEWS","FOLDER_PREVIEWS","mediaPlugin","collections","folderPicker","folderPreviews","gridView","config","map","collection","wanted","includes","slug","Boolean","upload","enhance","admin","components","providers","endpoints","views","beforeListTable","list","Component"],"mappings":"AAEA,SAASA,sBAAsB,QAAQ,gCAAgC;AAevE,MAAMC,YAAY;AAClB,MAAMC,cAAc;AACpB,MAAMC,kBACJ;AAEF;;;;CAIC,GACD,OAAO,MAAMC,cACX,CAAC,EACCC,WAAW,EACXC,eAAe,IAAI,EACnBC,iBAAiB,IAAI,EACrBC,WAAW,IAAI,EACG,GAAG,CAAC,CAAC,GACzB,CAACC,SAA4B,CAAA;YAC3B,GAAGA,MAAM;YACTJ,aAAaI,OAAOJ,WAAW,EAAEK,IAAIC,CAAAA;gBACnC,MAAMC,SAASP,cACXA,YAAYQ,QAAQ,CAACF,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