@foundrykit/media-plugin 0.1.0 → 0.3.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.
@@ -1,51 +1,81 @@
1
1
  'use client';
2
2
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
- import { Pill } from '@payloadcms/ui';
3
+ import { ChevronIcon, Pill, Popup, PopupList } from '@payloadcms/ui';
4
+ import { ToggleViewButtons } from '@payloadcms/ui/elements/FolderView/ToggleViewButtons';
4
5
  import { useEffect, useRef, useState } from 'react';
5
6
  import { createPortal } from 'react-dom';
6
7
  import './media-plugin.css';
7
- const VIEWS = [
8
- {
9
- id: 'list',
10
- label: 'List'
11
- },
12
- {
13
- id: 'grid',
14
- label: 'Grid'
15
- }
16
- ];
17
8
  const KEY = 'foundrykit-media-view';
9
+ const COLUMNS_KEY = 'foundrykit-media-columns';
10
+ /** Cards per row in the grid. 0 is "Auto": as many as fit at the minimum card width. */ const COLUMNS = [
11
+ 0,
12
+ 2,
13
+ 3,
14
+ 4,
15
+ 5,
16
+ 6,
17
+ 8
18
+ ];
19
+ const read = (key)=>{
20
+ try {
21
+ return localStorage.getItem(key);
22
+ } catch {
23
+ // Storage can be blocked (private window); the defaults stand.
24
+ return null;
25
+ }
26
+ };
27
+ const write = (key, value)=>{
28
+ try {
29
+ localStorage.setItem(key, value);
30
+ } catch {
31
+ // As above: the choice just isn't remembered.
32
+ }
33
+ };
18
34
  /**
19
35
  * List / Grid beside Columns and Filters on an upload collection's list, on the full page and
20
- * in the picker drawer. Payload has no slot there, so it is portalled into the search bar of
36
+ * in the picker drawer. It is Payload's own toggle from the By Folder view, so both views switch
37
+ * the same way. Payload has no slot there, so it is portalled into the search bar of
21
38
  * the list this renders in (found from its own position, so a drawer open over the list page
22
- * gets its own switch). The choice is remembered per browser and set as `data-media-view` on
23
- * the list; the grid itself is CSS over the same table (media-plugin.css).
39
+ * gets its own switch). In grid view a "Per row" menu sets how many cards sit on each row. Both
40
+ * choices are remembered per browser and set on the list as `data-media-view` and
41
+ * `--media-grid-columns`; the grid itself is CSS over the same table (media-plugin.css).
24
42
  */ export function MediaViews() {
25
43
  const anchor = useRef(null);
26
44
  const [list, setList] = useState(null);
27
45
  const [view, setView] = useState('list');
46
+ const [columns, setColumns] = useState(0);
28
47
  useEffect(()=>{
29
48
  setList(anchor.current?.closest('.collection-list') ?? null);
30
- try {
31
- if (localStorage.getItem(KEY) === 'grid') {
32
- setView('grid');
33
- }
34
- } catch {}
49
+ if (read(KEY) === 'grid') {
50
+ setView('grid');
51
+ }
52
+ const saved = Number(read(COLUMNS_KEY));
53
+ if (COLUMNS.includes(saved)) {
54
+ setColumns(saved);
55
+ }
35
56
  }, []);
36
57
  useEffect(()=>{
37
- if (list) {
38
- list.dataset.mediaView = view;
58
+ if (!list) {
59
+ return;
60
+ }
61
+ list.dataset.mediaView = view;
62
+ if (columns) {
63
+ list.style.setProperty('--media-grid-columns', String(columns));
64
+ } else {
65
+ list.style.removeProperty('--media-grid-columns');
39
66
  }
40
67
  }, [
41
68
  list,
42
- view
69
+ view,
70
+ columns
43
71
  ]);
44
72
  const choose = (v)=>{
45
73
  setView(v);
46
- try {
47
- localStorage.setItem(KEY, v);
48
- } catch {}
74
+ write(KEY, v);
75
+ };
76
+ const chooseColumns = (n)=>{
77
+ setColumns(n);
78
+ write(COLUMNS_KEY, String(n));
49
79
  };
50
80
  const target = list?.querySelector('.search-bar__actions');
51
81
  return /*#__PURE__*/ _jsxs(_Fragment, {
@@ -54,17 +84,38 @@ const KEY = 'foundrykit-media-view';
54
84
  hidden: true,
55
85
  ref: anchor
56
86
  }),
57
- target ? /*#__PURE__*/ createPortal(/*#__PURE__*/ _jsx("fieldset", {
58
- "aria-label": "View",
59
- className: "media-views",
60
- children: VIEWS.map((v)=>/*#__PURE__*/ _jsx(Pill, {
61
- "aria-pressed": view === v.id,
62
- className: view === v.id ? 'media-views__on' : undefined,
63
- onClick: ()=>choose(v.id),
64
- pillStyle: "light",
65
- size: "small",
66
- children: v.label
67
- }, v.id))
87
+ target ? /*#__PURE__*/ createPortal(/*#__PURE__*/ _jsxs(_Fragment, {
88
+ children: [
89
+ view === 'grid' ? /*#__PURE__*/ _jsx(Popup, {
90
+ button: /*#__PURE__*/ _jsxs(Pill, {
91
+ icon: /*#__PURE__*/ _jsx(ChevronIcon, {}),
92
+ pillStyle: "light",
93
+ size: "small",
94
+ children: [
95
+ "Per row: ",
96
+ columns || 'Auto'
97
+ ]
98
+ }),
99
+ buttonType: "custom",
100
+ className: "media-views__columns",
101
+ horizontalAlign: "right",
102
+ render: ({ close })=>/*#__PURE__*/ _jsx(PopupList.ButtonGroup, {
103
+ children: COLUMNS.map((n)=>/*#__PURE__*/ _jsx(PopupList.Button, {
104
+ active: n === columns,
105
+ onClick: ()=>{
106
+ chooseColumns(n);
107
+ close();
108
+ },
109
+ children: n || 'Auto'
110
+ }, n))
111
+ }),
112
+ size: "small"
113
+ }) : null,
114
+ /*#__PURE__*/ _jsx(ToggleViewButtons, {
115
+ activeView: view,
116
+ setActiveView: choose
117
+ })
118
+ ]
68
119
  }), target) : null
69
120
  ]
70
121
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/components/MediaViews.tsx"],"sourcesContent":["'use client'\n\nimport { Pill } from '@payloadcms/ui'\nimport { useEffect, useRef, useState } from 'react'\nimport { createPortal } from 'react-dom'\n\nimport './media-plugin.css'\n\nconst VIEWS = [\n { id: 'list', label: 'List' },\n { id: 'grid', label: 'Grid' },\n] as const\ntype View = (typeof VIEWS)[number]['id']\nconst KEY = 'foundrykit-media-view'\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. 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). The choice is remembered per browser and set as `data-media-view` on\n * the list; 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\n useEffect(() => {\n setList(anchor.current?.closest<HTMLElement>('.collection-list') ?? null)\n try {\n if (localStorage.getItem(KEY) === 'grid') {\n setView('grid')\n }\n } catch {}\n }, [])\n\n useEffect(() => {\n if (list) {\n list.dataset.mediaView = view\n }\n }, [list, view])\n\n const choose = (v: View) => {\n setView(v)\n try {\n localStorage.setItem(KEY, v)\n } catch {}\n }\n\n const target = list?.querySelector('.search-bar__actions')\n\n return (\n <>\n <span hidden ref={anchor} />\n {target\n ? createPortal(\n <fieldset aria-label=\"View\" className=\"media-views\">\n {VIEWS.map((v) => (\n <Pill\n aria-pressed={view === v.id}\n className={view === v.id ? 'media-views__on' : undefined}\n key={v.id}\n onClick={() => choose(v.id)}\n pillStyle=\"light\"\n size=\"small\"\n >\n {v.label}\n </Pill>\n ))}\n </fieldset>,\n target,\n )\n : null}\n </>\n )\n}\n"],"names":["Pill","useEffect","useRef","useState","createPortal","VIEWS","id","label","KEY","MediaViews","anchor","list","setList","view","setView","current","closest","localStorage","getItem","dataset","mediaView","choose","v","setItem","target","querySelector","span","hidden","ref","fieldset","aria-label","className","map","aria-pressed","undefined","onClick","pillStyle","size"],"mappings":"AAAA;;AAEA,SAASA,IAAI,QAAQ,iBAAgB;AACrC,SAASC,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,QAAO;AACnD,SAASC,YAAY,QAAQ,YAAW;AAExC,OAAO,qBAAoB;AAE3B,MAAMC,QAAQ;IACZ;QAAEC,IAAI;QAAQC,OAAO;IAAO;IAC5B;QAAED,IAAI;QAAQC,OAAO;IAAO;CAC7B;AAED,MAAMC,MAAM;AAEZ;;;;;;CAMC,GACD,OAAO,SAASC;IACd,MAAMC,SAASR,OAAwB;IACvC,MAAM,CAACS,MAAMC,QAAQ,GAAGT,SAA6B;IACrD,MAAM,CAACU,MAAMC,QAAQ,GAAGX,SAAe;IAEvCF,UAAU;QACRW,QAAQF,OAAOK,OAAO,EAAEC,QAAqB,uBAAuB;QACpE,IAAI;YACF,IAAIC,aAAaC,OAAO,CAACV,SAAS,QAAQ;gBACxCM,QAAQ;YACV;QACF,EAAE,OAAM,CAAC;IACX,GAAG,EAAE;IAELb,UAAU;QACR,IAAIU,MAAM;YACRA,KAAKQ,OAAO,CAACC,SAAS,GAAGP;QAC3B;IACF,GAAG;QAACF;QAAME;KAAK;IAEf,MAAMQ,SAAS,CAACC;QACdR,QAAQQ;QACR,IAAI;YACFL,aAAaM,OAAO,CAACf,KAAKc;QAC5B,EAAE,OAAM,CAAC;IACX;IAEA,MAAME,SAASb,MAAMc,cAAc;IAEnC,qBACE;;0BACE,KAACC;gBAAKC,MAAM;gBAACC,KAAKlB;;YACjBc,uBACGpB,2BACE,KAACyB;gBAASC,cAAW;gBAAOC,WAAU;0BACnC1B,MAAM2B,GAAG,CAAC,CAACV,kBACV,KAACtB;wBACCiC,gBAAcpB,SAASS,EAAEhB,EAAE;wBAC3ByB,WAAWlB,SAASS,EAAEhB,EAAE,GAAG,oBAAoB4B;wBAE/CC,SAAS,IAAMd,OAAOC,EAAEhB,EAAE;wBAC1B8B,WAAU;wBACVC,MAAK;kCAEJf,EAAEf,KAAK;uBALHe,EAAEhB,EAAE;gBASfkB,UAEF;;;AAGV"}
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,iBAAgB;AACpE,SAASC,iBAAiB,QAAQ,uDAAsD;AACxF,SAASC,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,QAAO;AACnD,SAASC,YAAY,QAAQ,YAAW;AAExC,OAAO,qBAAoB;AAG3B,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,M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BAWbS,MAAK;yBAEL;kCACJ,KAAC9C;wBAAkByD,YAAYtC;wBAAMuC,eAAexB;;;gBAEtDI,UAEF;;;AAGV"}
@@ -40,25 +40,7 @@
40
40
  padding-inline: var(--gutter-h);
41
41
  }
42
42
 
43
- /* List / Grid switch (MediaViews.tsx); the grid is drawn over the same table */
44
- .media-views {
45
- display: flex;
46
- gap: 0.125rem;
47
- margin: 0;
48
- padding: 0;
49
- border: 0;
50
- }
51
- .media-views .pill:first-child {
52
- border-radius: 999rem 0.25rem 0.25rem 999rem;
53
- }
54
- .media-views .pill:last-child {
55
- border-radius: 0.25rem 999rem 999rem 0.25rem;
56
- }
57
- .media-views .media-views__on {
58
- background: var(--theme-elevation-150);
59
- font-weight: 600;
60
- }
61
-
43
+ /* Grid view (MediaViews.tsx), drawn over the same table */
62
44
  .collection-list[data-media-view='grid'] .table table,
63
45
  .collection-list[data-media-view='grid'] .table tbody {
64
46
  display: block;
@@ -72,6 +54,10 @@
72
54
  grid-template-columns: repeat(auto-fill, minmax(9.5rem, 1fr));
73
55
  gap: 1rem;
74
56
  }
57
+ /* A fixed number per row, chosen from "Per row" (set on the list as a custom property). */
58
+ .collection-list[data-media-view='grid'][style*='--media-grid-columns'] .table tbody {
59
+ grid-template-columns: repeat(var(--media-grid-columns), minmax(0, 1fr));
60
+ }
75
61
  .collection-list[data-media-view='grid'] .table tr {
76
62
  position: relative;
77
63
  display: block;
@@ -128,3 +114,82 @@
128
114
  .collection-list[data-media-view='grid'] .cell-filename :is(a, button):hover .file__filename {
129
115
  text-decoration: underline;
130
116
  }
117
+
118
+ /* Folder card previews (FolderPreviews.tsx): thumbnails above the name, a count below it */
119
+ .folder-file-card--folder.media-folder-preview--on:has(.media-folder-preview__strip:not(:empty)) {
120
+ grid-template-rows: auto auto;
121
+ grid-template-areas:
122
+ 'preview'
123
+ 'details';
124
+ }
125
+ .media-folder-preview__strip {
126
+ grid-area: preview;
127
+ pointer-events: none;
128
+ container-type: inline-size;
129
+ }
130
+ .media-folder-preview__strip:empty {
131
+ display: none;
132
+ }
133
+ .media-folder-preview__thumbs {
134
+ display: grid;
135
+ grid-template-columns: repeat(3, minmax(0, 1fr));
136
+ gap: 0.25rem;
137
+ padding: 0.5rem 0.5rem 0;
138
+ }
139
+ .media-folder-preview__thumbs > * {
140
+ aspect-ratio: 1;
141
+ overflow: hidden;
142
+ border-radius: var(--style-radius-s);
143
+ background: var(--theme-elevation-100);
144
+ }
145
+ .media-folder-preview__thumbs img {
146
+ display: block;
147
+ width: 100%;
148
+ height: 100%;
149
+ object-fit: cover;
150
+ }
151
+ .media-folder-preview__file,
152
+ .media-folder-preview__more {
153
+ display: flex;
154
+ align-items: center;
155
+ justify-content: center;
156
+ font-size: 0.6875rem;
157
+ font-weight: 600;
158
+ color: var(--theme-elevation-600);
159
+ }
160
+ /* With three thumbnails, "+9" sits over the last one rather than taking a fourth slot. */
161
+ .media-folder-preview__thumbs:has(> :nth-child(4)) > :nth-child(3),
162
+ .media-folder-preview__thumbs:has(> :nth-child(4)) > .media-folder-preview__more {
163
+ grid-area: 1 / 3;
164
+ }
165
+ .media-folder-preview__none {
166
+ grid-column: 1 / -1;
167
+ aspect-ratio: auto;
168
+ display: flex;
169
+ align-items: center;
170
+ justify-content: center;
171
+ /* One thumbnail tall: a third of the strip's width, less the gaps. */
172
+ height: calc((100cqi - 1rem - 0.5rem) / 3);
173
+ font-size: 0.75rem;
174
+ color: var(--theme-elevation-500);
175
+ }
176
+ .media-folder-preview__more {
177
+ z-index: 1;
178
+ background: color-mix(in srgb, var(--theme-elevation-1000) 45%, transparent);
179
+ color: var(--theme-elevation-0);
180
+ font-size: 0.875rem;
181
+ }
182
+ .media-folder-preview__meta:empty {
183
+ display: none;
184
+ }
185
+ .media-folder-preview__meta {
186
+ margin: 0;
187
+ font-size: 0.75rem;
188
+ line-height: normal;
189
+ color: var(--assigned-collections-color, var(--theme-elevation-800));
190
+ }
191
+ /* Our line says what is inside, so the folder-type line ("Media") makes way once it loads. */
192
+ .folder-file-card__titlebar-labels:has(.media-folder-preview__meta:not(:empty))
193
+ .folder-file-card__assigned-collections {
194
+ display: none;
195
+ }
@@ -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 { CollectionSlug, 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 = typeof adminThumbnail === 'string' ? doc.sizes?.[adminThumbnail]?.url : 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 => ({ [fieldName]: { equals: id } })\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((a, b) => Number(Boolean(b.thumbnail)) - Number(Boolean(a.thumbnail)))\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 [String(id), { counts, folders: subfolders.totalDocs, previews }] 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 = 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,EAAC;AACzB,+FAA+F,GAC/F,MAAMC,UAAU;AAShB,MAAMC,cAAc,CAACC,KAAgBC;IACnC,IAAI,CAACD,IAAIE,QAAQ,EAAEC,WAAW,WAAW;QACvC,OAAOC;IACT;IACA,MAAMC,OAAO,OAAOJ,mBAAmB,WAAWD,IAAIM,KAAK,EAAE,CAACL,eAAe,EAAEM,MAAMH;IACrF,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;YAAE,CAACF,UAAU,EAAE;gBAAEG,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AMlB,EAAEkB,IAAI;QACd,CAAA;AACF,EAAC;AAED;;;;CAIC,GACD,OAAO,MAAMC,yBAAmC;IAC9CC,SAAS,OAAOpD;QACd,IAAI,CAACA,IAAIqD,IAAI,EAAE;YACb,OAAOC,SAASC,IAAI,CAAC;gBAAEC,OAAO;YAAe,GAAG;gBAAEC,QAAQ;YAAI;QAChE;QACA,MAAMV,SAAS,OAAO/C,IAAI0D,KAAK,EAAEX,WAAW,WAAW/C,IAAI0D,KAAK,CAACX,MAAM,GAAGpD;QAC1E,IAAIoD,QAAQ;YACV,MAAMY,WAAW,MAAMb,aAAa9C,KAAK+C;YACzC,MAAMZ,WAAW,MAAMpC,eACrBC,KACA2D,SAAS3C,GAAG,CAAC,CAACL,IAAMA,EAAEJ,EAAE;YAE1B,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,CAAC+C,IAAMA,EAAEC,IAAI,IACjBtD,MAAM,CAAC8B,SACPC,KAAK,CAAC,GAAGpD;QACZ,OAAOiE,SAASC,IAAI,CAAC,MAAMxD,eAAeC,KAAKC;IACjD;IACAgE,QAAQ;IACRC,MAAM;AACR,EAAC"}
@@ -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';
@@ -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'\nexport { MediaViews } from '../components/MediaViews.js'\n"],"names":["FolderPicker","MediaListView","MediaViews"],"mappings":"AAAA,SAASA,YAAY,QAAQ,gCAA+B;AAC5D,SAASC,aAAa,QAAQ,iCAAgC;AAC9D,SAASC,UAAU,QAAQ,8BAA6B"}
1
+ {"version":3,"sources":["../../src/exports/client.ts"],"sourcesContent":["export { FolderPicker } from '../components/FolderPicker.js'\nexport { FolderPreviews, FolderPreviewsProvider } 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,gCAA+B;AAC5D,SAASC,cAAc,EAAEC,sBAAsB,QAAQ,kCAAiC;AACxF,SAASC,aAAa,QAAQ,iCAAgC;AAC9D,SAASC,UAAU,QAAQ,8BAA6B"}
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[]\n /** Folders tab inside the image picker drawer. Needs `folders: true` on the collection. Default true. */\n folderPicker?: 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'\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 ({ collections, folderPicker = true, gridView = true }: MediaPluginConfig = {}) =>\n (config: Config): Config => ({\n ...config,\n collections: config.collections?.map((collection) => {\n const wanted = collections ? collections.includes(collection.slug) : Boolean(collection.upload)\n return wanted ? enhance(collection, { folderPicker, gridView }) : collection\n }),\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 ? [...beforeListTable, MEDIA_VIEWS] : beforeListTable,\n views: {\n ...views,\n list: folderPicker && !views.list ? { Component: LIST_VIEW } : views.list,\n },\n },\n },\n }\n}\n"],"names":["LIST_VIEW","MEDIA_VIEWS","mediaPlugin","collections","folderPicker","gridView","config","map","collection","wanted","includes","slug","Boolean","upload","enhance","components","admin","views","beforeListTable","list","Component"],"mappings":"AAWA,MAAMA,YAAY;AAClB,MAAMC,cAAc;AAEpB;;;;CAIC,GACD,OAAO,MAAMC,cACX,CAAC,EAAEC,WAAW,EAAEC,eAAe,IAAI,EAAEC,WAAW,IAAI,EAAqB,GAAG,CAAC,CAAC,GAC9E,CAACC,SAA4B,CAAA;YAC3B,GAAGA,MAAM;YACTH,aAAaG,OAAOH,WAAW,EAAEI,IAAI,CAACC;gBACpC,MAAMC,SAASN,cAAcA,YAAYO,QAAQ,CAACF,WAAWG,IAAI,IAAIC,QAAQJ,WAAWK,MAAM;gBAC9F,OAAOJ,SAASK,QAAQN,YAAY;oBAAEJ;oBAAcC;gBAAS,KAAKG;YACpE;QACF,CAAA,EAAE;AAEJ,SAASM,QACPN,UAA4B,EAC5B,EAAEJ,YAAY,EAAEC,QAAQ,EAAgD;IAExE,MAAMU,aAAaP,WAAWQ,KAAK,EAAED,cAAc,CAAC;IACpD,MAAME,QAAQF,WAAWE,KAAK,IAAI,CAAC;IACnC,MAAMC,kBAAkBH,WAAWG,eAAe,IAAI,EAAE;IAExD,OAAO;QACL,GAAGV,UAAU;QACbQ,OAAO;YACL,GAAGR,WAAWQ,KAAK;YACnBD,YAAY;gBACV,GAAGA,UAAU;gBACbG,iBAAiBb,WAAW;uBAAIa;oBAAiBjB;iBAAY,GAAGiB;gBAChED,OAAO;oBACL,GAAGA,KAAK;oBACRE,MAAMf,gBAAgB,CAACa,MAAME,IAAI,GAAG;wBAAEC,WAAWpB;oBAAU,IAAIiB,MAAME,IAAI;gBAC3E;YACF;QACF;IACF;AACF"}
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 = '@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 ? collections.includes(collection.slug) : Boolean(collection.upload)\n return wanted ? enhance(collection, { folderPicker, gridView }) : 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: [...(config.admin?.components?.providers ?? []), FOLDER_PREVIEWS],\n },\n }\n : config.admin,\n endpoints: folderPreviews ? [...(config.endpoints ?? []), folderPreviewsEndpoint] : 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 ? [...beforeListTable, MEDIA_VIEWS] : beforeListTable,\n views: {\n ...views,\n list: 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,gCAA+B;AAetE,MAAMC,YAA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