@seshuk/payload-media-preview 1.0.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/LICENSE +21 -0
- package/README.md +507 -0
- package/dist/components/Cell/Cell.client.d.ts +18 -0
- package/dist/components/Cell/Cell.client.js +131 -0
- package/dist/components/Cell/Cell.scss +43 -0
- package/dist/components/Cell/Cell.server.d.ts +11 -0
- package/dist/components/Cell/Cell.server.js +33 -0
- package/dist/components/ExternalLinkIcon/ExternalLinkIcon.d.ts +5 -0
- package/dist/components/ExternalLinkIcon/ExternalLinkIcon.js +17 -0
- package/dist/components/ExternalLinkIcon/ExternalLinkIcon.scss +19 -0
- package/dist/components/Field/Field.d.ts +18 -0
- package/dist/components/Field/Field.js +107 -0
- package/dist/components/Field/Field.scss +14 -0
- package/dist/components/MediaPreview.constants.d.ts +4 -0
- package/dist/components/MediaPreview.constants.js +28 -0
- package/dist/components/MediaPreview.d.ts +10 -0
- package/dist/components/MediaPreview.js +57 -0
- package/dist/components/MediaPreview.types.d.ts +2 -0
- package/dist/components/MediaPreview.types.js +1 -0
- package/dist/components/MediaPreview.utils.d.ts +6 -0
- package/dist/components/MediaPreview.utils.js +38 -0
- package/dist/components/Modal/Modal.constants.d.ts +16 -0
- package/dist/components/Modal/Modal.constants.js +16 -0
- package/dist/components/Modal/Modal.d.ts +19 -0
- package/dist/components/Modal/Modal.js +277 -0
- package/dist/components/Modal/Modal.scss +173 -0
- package/dist/components/Viewer/AudioViewer.d.ts +3 -0
- package/dist/components/Viewer/AudioViewer.js +24 -0
- package/dist/components/Viewer/IframeViewer.d.ts +3 -0
- package/dist/components/Viewer/IframeViewer.js +14 -0
- package/dist/components/Viewer/ImageViewer.d.ts +3 -0
- package/dist/components/Viewer/ImageViewer.js +10 -0
- package/dist/components/Viewer/VideoViewer.d.ts +3 -0
- package/dist/components/Viewer/VideoViewer.js +25 -0
- package/dist/components/Viewer/Viewer.d.ts +12 -0
- package/dist/components/Viewer/Viewer.js +50 -0
- package/dist/components/adapterResolver.d.ts +13 -0
- package/dist/components/adapterResolver.js +43 -0
- package/dist/exports/client.d.ts +5 -0
- package/dist/exports/client.js +5 -0
- package/dist/exports/rsc.d.ts +2 -0
- package/dist/exports/rsc.js +2 -0
- package/dist/field.d.ts +18 -0
- package/dist/field.js +36 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +90 -0
- package/dist/translations/index.d.ts +8 -0
- package/dist/translations/index.js +90 -0
- package/dist/translations/locales/ar.d.ts +2 -0
- package/dist/translations/locales/ar.js +11 -0
- package/dist/translations/locales/az.d.ts +2 -0
- package/dist/translations/locales/az.js +11 -0
- package/dist/translations/locales/bg.d.ts +2 -0
- package/dist/translations/locales/bg.js +11 -0
- package/dist/translations/locales/bnBd.d.ts +2 -0
- package/dist/translations/locales/bnBd.js +11 -0
- package/dist/translations/locales/bnIn.d.ts +2 -0
- package/dist/translations/locales/bnIn.js +11 -0
- package/dist/translations/locales/ca.d.ts +2 -0
- package/dist/translations/locales/ca.js +11 -0
- package/dist/translations/locales/cs.d.ts +2 -0
- package/dist/translations/locales/cs.js +11 -0
- package/dist/translations/locales/da.d.ts +2 -0
- package/dist/translations/locales/da.js +11 -0
- package/dist/translations/locales/de.d.ts +2 -0
- package/dist/translations/locales/de.js +11 -0
- package/dist/translations/locales/en.d.ts +2 -0
- package/dist/translations/locales/en.js +11 -0
- package/dist/translations/locales/es.d.ts +2 -0
- package/dist/translations/locales/es.js +11 -0
- package/dist/translations/locales/et.d.ts +2 -0
- package/dist/translations/locales/et.js +11 -0
- package/dist/translations/locales/fa.d.ts +2 -0
- package/dist/translations/locales/fa.js +11 -0
- package/dist/translations/locales/fr.d.ts +2 -0
- package/dist/translations/locales/fr.js +11 -0
- package/dist/translations/locales/he.d.ts +2 -0
- package/dist/translations/locales/he.js +11 -0
- package/dist/translations/locales/hr.d.ts +2 -0
- package/dist/translations/locales/hr.js +11 -0
- package/dist/translations/locales/hu.d.ts +2 -0
- package/dist/translations/locales/hu.js +11 -0
- package/dist/translations/locales/hy.d.ts +2 -0
- package/dist/translations/locales/hy.js +11 -0
- package/dist/translations/locales/id.d.ts +2 -0
- package/dist/translations/locales/id.js +11 -0
- package/dist/translations/locales/is.d.ts +2 -0
- package/dist/translations/locales/is.js +11 -0
- package/dist/translations/locales/it.d.ts +2 -0
- package/dist/translations/locales/it.js +11 -0
- package/dist/translations/locales/ja.d.ts +2 -0
- package/dist/translations/locales/ja.js +11 -0
- package/dist/translations/locales/ko.d.ts +2 -0
- package/dist/translations/locales/ko.js +11 -0
- package/dist/translations/locales/lt.d.ts +2 -0
- package/dist/translations/locales/lt.js +11 -0
- package/dist/translations/locales/lv.d.ts +2 -0
- package/dist/translations/locales/lv.js +11 -0
- package/dist/translations/locales/my.d.ts +2 -0
- package/dist/translations/locales/my.js +11 -0
- package/dist/translations/locales/nb.d.ts +2 -0
- package/dist/translations/locales/nb.js +11 -0
- package/dist/translations/locales/nl.d.ts +2 -0
- package/dist/translations/locales/nl.js +11 -0
- package/dist/translations/locales/pl.d.ts +2 -0
- package/dist/translations/locales/pl.js +11 -0
- package/dist/translations/locales/pt.d.ts +2 -0
- package/dist/translations/locales/pt.js +11 -0
- package/dist/translations/locales/ro.d.ts +2 -0
- package/dist/translations/locales/ro.js +11 -0
- package/dist/translations/locales/rs.d.ts +2 -0
- package/dist/translations/locales/rs.js +11 -0
- package/dist/translations/locales/rsLatin.d.ts +2 -0
- package/dist/translations/locales/rsLatin.js +11 -0
- package/dist/translations/locales/ru.d.ts +2 -0
- package/dist/translations/locales/ru.js +11 -0
- package/dist/translations/locales/sk.d.ts +2 -0
- package/dist/translations/locales/sk.js +11 -0
- package/dist/translations/locales/sl.d.ts +2 -0
- package/dist/translations/locales/sl.js +11 -0
- package/dist/translations/locales/sv.d.ts +2 -0
- package/dist/translations/locales/sv.js +11 -0
- package/dist/translations/locales/ta.d.ts +2 -0
- package/dist/translations/locales/ta.js +11 -0
- package/dist/translations/locales/th.d.ts +2 -0
- package/dist/translations/locales/th.js +11 -0
- package/dist/translations/locales/tr.d.ts +2 -0
- package/dist/translations/locales/tr.js +11 -0
- package/dist/translations/locales/uk.d.ts +2 -0
- package/dist/translations/locales/uk.js +11 -0
- package/dist/translations/locales/vi.d.ts +2 -0
- package/dist/translations/locales/vi.js +11 -0
- package/dist/translations/locales/zh.d.ts +2 -0
- package/dist/translations/locales/zh.js +11 -0
- package/dist/translations/locales/zhTw.d.ts +2 -0
- package/dist/translations/locales/zhTw.js +11 -0
- package/dist/translations/types.d.ts +11 -0
- package/dist/translations/types.js +1 -0
- package/dist/types.d.ts +130 -0
- package/dist/types.js +2 -0
- package/dist/utils/insertField.d.ts +3 -0
- package/dist/utils/insertField.js +97 -0
- package/package.json +121 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { Modal, useModal, useTranslation } from "@payloadcms/ui";
|
|
4
|
+
import { XIcon } from "@payloadcms/ui/icons/X";
|
|
5
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
6
|
+
import { createPortal } from "react-dom";
|
|
7
|
+
import { MediaPreviewViewer } from "../Viewer/Viewer.js";
|
|
8
|
+
import { AUDIO_DIMENSIONS, POPUP_DIMENSIONS, SPACING } from "./Modal.constants.js";
|
|
9
|
+
import "./Modal.scss";
|
|
10
|
+
export const MediaPreviewModal = ({ customViewer, media, mode, onClose, rowId, show, triggerRef })=>{
|
|
11
|
+
const { documentViewerUrl, height, mimeType, url, width } = media;
|
|
12
|
+
const [position, setPosition] = useState('bottom');
|
|
13
|
+
const [popupStyle, setPopupStyle] = useState({});
|
|
14
|
+
const popupRef = useRef(null);
|
|
15
|
+
const previousModalOpenRef = useRef(false);
|
|
16
|
+
const { closeModal, isModalOpen, openModal } = useModal();
|
|
17
|
+
const { t } = useTranslation();
|
|
18
|
+
const modalSlug = useMemo(()=>`media-preview-${rowId || 'field'}`, [
|
|
19
|
+
rowId
|
|
20
|
+
]);
|
|
21
|
+
const isAudio = useMemo(()=>mimeType?.startsWith('audio/'), [
|
|
22
|
+
mimeType
|
|
23
|
+
]);
|
|
24
|
+
const isVideo = useMemo(()=>mimeType?.startsWith('video/'), [
|
|
25
|
+
mimeType
|
|
26
|
+
]);
|
|
27
|
+
const isImage = useMemo(()=>mimeType?.startsWith('image/'), [
|
|
28
|
+
mimeType
|
|
29
|
+
]);
|
|
30
|
+
const getPopupDimensions = useCallback(()=>{
|
|
31
|
+
if (isImage && width && height) {
|
|
32
|
+
const aspectRatio = width / height;
|
|
33
|
+
let popupHeight = POPUP_DIMENSIONS.MAX_HEIGHT;
|
|
34
|
+
let popupWidth = popupHeight * aspectRatio;
|
|
35
|
+
if (popupWidth > POPUP_DIMENSIONS.MAX_WIDTH) {
|
|
36
|
+
popupWidth = POPUP_DIMENSIONS.MAX_WIDTH;
|
|
37
|
+
popupHeight = popupWidth / aspectRatio;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
height: Math.max(POPUP_DIMENSIONS.MIN_HEIGHT, popupHeight),
|
|
41
|
+
width: Math.max(POPUP_DIMENSIONS.MIN_WIDTH, popupWidth)
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (isAudio) {
|
|
45
|
+
return {
|
|
46
|
+
height: AUDIO_DIMENSIONS.HEIGHT,
|
|
47
|
+
width: AUDIO_DIMENSIONS.WIDTH
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
height: POPUP_DIMENSIONS.MAX_HEIGHT,
|
|
52
|
+
width: POPUP_DIMENSIONS.MAX_WIDTH
|
|
53
|
+
};
|
|
54
|
+
}, [
|
|
55
|
+
isImage,
|
|
56
|
+
width,
|
|
57
|
+
height,
|
|
58
|
+
isAudio
|
|
59
|
+
]);
|
|
60
|
+
const calculatePopupPosition = useCallback(()=>{
|
|
61
|
+
if (!triggerRef?.current || mode !== 'popup') {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const rect = triggerRef.current.getBoundingClientRect();
|
|
65
|
+
const { height: popupHeight, width: popupWidth } = getPopupDimensions();
|
|
66
|
+
const spacingTop = SPACING.CARET_SIZE + SPACING.TOP;
|
|
67
|
+
const spacingBottom = SPACING.CARET_SIZE + SPACING.BOTTOM;
|
|
68
|
+
const viewportWidth = document.documentElement.clientWidth;
|
|
69
|
+
const viewportHeight = document.documentElement.clientHeight;
|
|
70
|
+
let top;
|
|
71
|
+
let selectedPosition;
|
|
72
|
+
let left = rect.left + rect.width / 2 - popupWidth / 2;
|
|
73
|
+
const buttonCenterX = rect.left + rect.width / 2;
|
|
74
|
+
if (left + popupWidth > viewportWidth - SPACING.VIEWPORT_MARGIN) {
|
|
75
|
+
left = viewportWidth - popupWidth - SPACING.VIEWPORT_MARGIN;
|
|
76
|
+
}
|
|
77
|
+
if (left < SPACING.VIEWPORT_MARGIN) {
|
|
78
|
+
left = SPACING.VIEWPORT_MARGIN;
|
|
79
|
+
}
|
|
80
|
+
const caretLeft = buttonCenterX - left;
|
|
81
|
+
const spaceBelow = viewportHeight - rect.bottom;
|
|
82
|
+
const spaceAbove = rect.top;
|
|
83
|
+
if (spaceBelow >= popupHeight + spacingBottom) {
|
|
84
|
+
selectedPosition = 'bottom';
|
|
85
|
+
top = rect.bottom + spacingBottom;
|
|
86
|
+
} else if (spaceAbove >= popupHeight + spacingTop) {
|
|
87
|
+
selectedPosition = 'top';
|
|
88
|
+
top = rect.top - popupHeight - spacingTop;
|
|
89
|
+
} else {
|
|
90
|
+
if (spaceAbove > spaceBelow) {
|
|
91
|
+
selectedPosition = 'top';
|
|
92
|
+
top = rect.top - popupHeight - spacingTop;
|
|
93
|
+
} else {
|
|
94
|
+
selectedPosition = 'bottom';
|
|
95
|
+
top = rect.bottom + spacingBottom;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
setPosition(selectedPosition);
|
|
99
|
+
setPopupStyle({
|
|
100
|
+
'--caret-left': `${caretLeft}px`,
|
|
101
|
+
'--popup-height': `${popupHeight}px`,
|
|
102
|
+
'--popup-width': `${popupWidth}px`,
|
|
103
|
+
left: `${left}px`,
|
|
104
|
+
top: `${top}px`
|
|
105
|
+
});
|
|
106
|
+
}, [
|
|
107
|
+
triggerRef,
|
|
108
|
+
mode,
|
|
109
|
+
getPopupDimensions
|
|
110
|
+
]);
|
|
111
|
+
useEffect(()=>{
|
|
112
|
+
if (mode === 'fullscreen') {
|
|
113
|
+
if (show && !isModalOpen(modalSlug)) {
|
|
114
|
+
openModal(modalSlug);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}, [
|
|
118
|
+
show,
|
|
119
|
+
mode,
|
|
120
|
+
modalSlug,
|
|
121
|
+
isModalOpen,
|
|
122
|
+
openModal
|
|
123
|
+
]);
|
|
124
|
+
useEffect(()=>{
|
|
125
|
+
if (mode === 'fullscreen' && !show && isModalOpen(modalSlug)) {
|
|
126
|
+
closeModal(modalSlug);
|
|
127
|
+
}
|
|
128
|
+
}, [
|
|
129
|
+
show,
|
|
130
|
+
mode,
|
|
131
|
+
modalSlug,
|
|
132
|
+
isModalOpen,
|
|
133
|
+
closeModal
|
|
134
|
+
]);
|
|
135
|
+
useEffect(()=>{
|
|
136
|
+
if (mode === 'fullscreen') {
|
|
137
|
+
const currentModalOpen = isModalOpen(modalSlug);
|
|
138
|
+
if (previousModalOpenRef.current && !currentModalOpen && show) {
|
|
139
|
+
onClose?.();
|
|
140
|
+
}
|
|
141
|
+
previousModalOpenRef.current = currentModalOpen;
|
|
142
|
+
}
|
|
143
|
+
}, [
|
|
144
|
+
isModalOpen,
|
|
145
|
+
modalSlug,
|
|
146
|
+
mode,
|
|
147
|
+
show,
|
|
148
|
+
onClose
|
|
149
|
+
]);
|
|
150
|
+
useEffect(()=>{
|
|
151
|
+
if (show && mode === 'popup') {
|
|
152
|
+
calculatePopupPosition();
|
|
153
|
+
window.addEventListener('resize', calculatePopupPosition);
|
|
154
|
+
window.addEventListener('scroll', calculatePopupPosition, true);
|
|
155
|
+
return ()=>{
|
|
156
|
+
window.removeEventListener('resize', calculatePopupPosition);
|
|
157
|
+
window.removeEventListener('scroll', calculatePopupPosition, true);
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}, [
|
|
161
|
+
show,
|
|
162
|
+
mode,
|
|
163
|
+
calculatePopupPosition
|
|
164
|
+
]);
|
|
165
|
+
const handleClickOutside = useCallback((event)=>{
|
|
166
|
+
if (popupRef.current && !popupRef.current.contains(event.target) && triggerRef?.current && !triggerRef.current.contains(event.target)) {
|
|
167
|
+
onClose?.();
|
|
168
|
+
}
|
|
169
|
+
}, [
|
|
170
|
+
onClose,
|
|
171
|
+
triggerRef
|
|
172
|
+
]);
|
|
173
|
+
useEffect(()=>{
|
|
174
|
+
if (!show || mode !== 'popup') {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
document.addEventListener('mousedown', handleClickOutside);
|
|
178
|
+
return ()=>{
|
|
179
|
+
document.removeEventListener('mousedown', handleClickOutside);
|
|
180
|
+
};
|
|
181
|
+
}, [
|
|
182
|
+
show,
|
|
183
|
+
mode,
|
|
184
|
+
handleClickOutside
|
|
185
|
+
]);
|
|
186
|
+
const handleModalClose = useCallback(()=>{
|
|
187
|
+
closeModal(modalSlug);
|
|
188
|
+
onClose?.();
|
|
189
|
+
}, [
|
|
190
|
+
closeModal,
|
|
191
|
+
modalSlug,
|
|
192
|
+
onClose
|
|
193
|
+
]);
|
|
194
|
+
const handleBackdropClick = useCallback((e)=>{
|
|
195
|
+
if ('key' in e && e.key !== 'Enter' && e.key !== ' ') {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const target = e.target;
|
|
199
|
+
if (target.tagName === 'IMG' || target.tagName === 'VIDEO' || target.tagName === 'AUDIO' || target.tagName === 'IFRAME' || target.classList.contains('media-preview-modal__close') || target.closest('.media-preview-modal__close')) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
handleModalClose();
|
|
203
|
+
}, [
|
|
204
|
+
handleModalClose
|
|
205
|
+
]);
|
|
206
|
+
const viewerContent = useMemo(()=>{
|
|
207
|
+
if (customViewer) {
|
|
208
|
+
return customViewer;
|
|
209
|
+
}
|
|
210
|
+
return /*#__PURE__*/ _jsx(MediaPreviewViewer, {
|
|
211
|
+
className: documentViewerUrl ? 'media-preview-modal__iframe' : isVideo ? 'media-preview-modal__video' : isAudio ? 'media-preview-modal__audio' : 'media-preview-modal__image',
|
|
212
|
+
media: {
|
|
213
|
+
documentViewerUrl,
|
|
214
|
+
mimeType,
|
|
215
|
+
url
|
|
216
|
+
},
|
|
217
|
+
title: isVideo ? t('@seshuk/payload-media-preview:titleVideo') : isImage ? t('@seshuk/payload-media-preview:titleImage') : isAudio ? t('@seshuk/payload-media-preview:titleAudio') : t('@seshuk/payload-media-preview:titleDocument')
|
|
218
|
+
});
|
|
219
|
+
}, [
|
|
220
|
+
customViewer,
|
|
221
|
+
documentViewerUrl,
|
|
222
|
+
isVideo,
|
|
223
|
+
isAudio,
|
|
224
|
+
isImage,
|
|
225
|
+
mimeType,
|
|
226
|
+
url,
|
|
227
|
+
t
|
|
228
|
+
]);
|
|
229
|
+
if (mode === 'popup') {
|
|
230
|
+
if (!show) {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
return /*#__PURE__*/ createPortal(/*#__PURE__*/ _jsx("div", {
|
|
234
|
+
className: `media-preview-modal media-preview-modal--popup media-preview-modal--show media-preview-modal--position-${position}`,
|
|
235
|
+
ref: popupRef,
|
|
236
|
+
style: popupStyle,
|
|
237
|
+
children: /*#__PURE__*/ _jsx("div", {
|
|
238
|
+
className: "media-preview-modal__body",
|
|
239
|
+
children: viewerContent
|
|
240
|
+
})
|
|
241
|
+
}), document.body);
|
|
242
|
+
}
|
|
243
|
+
if (!isModalOpen(modalSlug)) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
return /*#__PURE__*/ _jsx(Modal, {
|
|
247
|
+
className: "media-preview-modal media-preview-modal--fullscreen",
|
|
248
|
+
onClick: handleBackdropClick,
|
|
249
|
+
slug: modalSlug,
|
|
250
|
+
children: /*#__PURE__*/ _jsxs("div", {
|
|
251
|
+
"aria-label": t('@seshuk/payload-media-preview:close'),
|
|
252
|
+
className: "media-preview-modal__wrapper",
|
|
253
|
+
onClick: handleBackdropClick,
|
|
254
|
+
onKeyDown: handleBackdropClick,
|
|
255
|
+
role: "button",
|
|
256
|
+
tabIndex: -1,
|
|
257
|
+
children: [
|
|
258
|
+
/*#__PURE__*/ _jsx("button", {
|
|
259
|
+
"aria-label": t('@seshuk/payload-media-preview:close'),
|
|
260
|
+
className: "drawer-close-button media-preview-modal__close",
|
|
261
|
+
onClick: handleModalClose,
|
|
262
|
+
type: "button",
|
|
263
|
+
children: /*#__PURE__*/ _jsx(XIcon, {})
|
|
264
|
+
}),
|
|
265
|
+
/*#__PURE__*/ _jsx("div", {
|
|
266
|
+
className: "media-preview-modal__content",
|
|
267
|
+
role: "dialog",
|
|
268
|
+
tabIndex: -1,
|
|
269
|
+
children: /*#__PURE__*/ _jsx("div", {
|
|
270
|
+
className: "media-preview-modal__body",
|
|
271
|
+
children: viewerContent
|
|
272
|
+
})
|
|
273
|
+
})
|
|
274
|
+
]
|
|
275
|
+
})
|
|
276
|
+
});
|
|
277
|
+
};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
@import '~@payloadcms/ui/scss';
|
|
2
|
+
|
|
3
|
+
.media-preview-modal {
|
|
4
|
+
&--popup {
|
|
5
|
+
--popup-caret-size: 10px;
|
|
6
|
+
--popup-padding: calc(var(--base) * 0.5);
|
|
7
|
+
--popup-width: 480px;
|
|
8
|
+
--popup-height: 290px;
|
|
9
|
+
|
|
10
|
+
position: fixed;
|
|
11
|
+
z-index: 9999;
|
|
12
|
+
opacity: 0;
|
|
13
|
+
visibility: hidden;
|
|
14
|
+
background: var(--theme-elevation-0);
|
|
15
|
+
border-radius: 4px;
|
|
16
|
+
padding: var(--popup-padding);
|
|
17
|
+
pointer-events: auto;
|
|
18
|
+
transition: opacity 0.2s ease-in-out;
|
|
19
|
+
will-change: opacity, transform;
|
|
20
|
+
max-height: 100vh;
|
|
21
|
+
overflow: visible;
|
|
22
|
+
|
|
23
|
+
html[data-theme='dark'] & {
|
|
24
|
+
background: var(--theme-elevation-100);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
&.media-preview-modal--show {
|
|
28
|
+
visibility: visible;
|
|
29
|
+
opacity: 1;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
&::after {
|
|
33
|
+
content: '';
|
|
34
|
+
position: absolute;
|
|
35
|
+
border: var(--popup-caret-size) solid transparent;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
&.media-preview-modal--position-top {
|
|
39
|
+
@include shadow-lg;
|
|
40
|
+
|
|
41
|
+
&::after {
|
|
42
|
+
top: calc(100% - 1px);
|
|
43
|
+
left: var(--caret-left, 50%);
|
|
44
|
+
transform: translateX(-50%);
|
|
45
|
+
border-top-color: var(--theme-elevation-0);
|
|
46
|
+
|
|
47
|
+
html[data-theme='dark'] & {
|
|
48
|
+
border-top-color: var(--theme-elevation-100);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
&.media-preview-modal--position-bottom {
|
|
54
|
+
@include shadow-lg-top;
|
|
55
|
+
|
|
56
|
+
&::after {
|
|
57
|
+
bottom: calc(100% - 1px);
|
|
58
|
+
left: var(--caret-left, 50%);
|
|
59
|
+
transform: translateX(-50%);
|
|
60
|
+
border-bottom-color: var(--theme-elevation-0);
|
|
61
|
+
|
|
62
|
+
html[data-theme='dark'] & {
|
|
63
|
+
border-bottom-color: var(--theme-elevation-100);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
&--fullscreen {
|
|
70
|
+
@include blur-bg;
|
|
71
|
+
display: flex;
|
|
72
|
+
align-items: center;
|
|
73
|
+
justify-content: center;
|
|
74
|
+
height: 100%;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
&__body {
|
|
78
|
+
position: relative;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
&--popup &__body {
|
|
82
|
+
width: var(--popup-width);
|
|
83
|
+
height: var(--popup-height);
|
|
84
|
+
max-width: 90vw;
|
|
85
|
+
max-height: calc(100vh - 2rem);
|
|
86
|
+
overflow: auto;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
&--fullscreen &__body {
|
|
90
|
+
width: 100%;
|
|
91
|
+
height: 100%;
|
|
92
|
+
border-radius: var(--border-radius-m);
|
|
93
|
+
overflow: hidden;
|
|
94
|
+
display: flex;
|
|
95
|
+
align-items: center;
|
|
96
|
+
justify-content: center;
|
|
97
|
+
flex: 1;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
&__wrapper {
|
|
101
|
+
position: absolute;
|
|
102
|
+
inset: 0;
|
|
103
|
+
display: flex;
|
|
104
|
+
align-items: center;
|
|
105
|
+
justify-content: center;
|
|
106
|
+
pointer-events: auto;
|
|
107
|
+
cursor: pointer;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
&__content {
|
|
111
|
+
position: relative;
|
|
112
|
+
z-index: 1;
|
|
113
|
+
max-width: min(90vw, calc(100vh * 1.6));
|
|
114
|
+
max-height: 90vh;
|
|
115
|
+
width: 100%;
|
|
116
|
+
height: 100%;
|
|
117
|
+
cursor: default;
|
|
118
|
+
pointer-events: auto;
|
|
119
|
+
display: flex;
|
|
120
|
+
flex-direction: column;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
&__close {
|
|
124
|
+
position: absolute;
|
|
125
|
+
top: calc(var(--base) * 0.5);
|
|
126
|
+
right: calc(var(--base) * 0.5);
|
|
127
|
+
z-index: 10;
|
|
128
|
+
pointer-events: auto;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
&--popup &__body &__iframe,
|
|
132
|
+
&--popup &__body &__video,
|
|
133
|
+
&--popup &__body &__image,
|
|
134
|
+
&--popup &__body &__audio {
|
|
135
|
+
position: absolute;
|
|
136
|
+
inset: 0;
|
|
137
|
+
width: 100%;
|
|
138
|
+
height: 100%;
|
|
139
|
+
border: none;
|
|
140
|
+
border-radius: var(--border-radius-s);
|
|
141
|
+
object-fit: contain;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
&--fullscreen &__body &__iframe {
|
|
145
|
+
width: 100%;
|
|
146
|
+
height: 100%;
|
|
147
|
+
border: none;
|
|
148
|
+
border-radius: var(--border-radius-s);
|
|
149
|
+
object-fit: contain;
|
|
150
|
+
min-height: 60vh;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
&--fullscreen &__body &__video {
|
|
154
|
+
width: 100%;
|
|
155
|
+
height: 100%;
|
|
156
|
+
border: none;
|
|
157
|
+
border-radius: var(--border-radius-s);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
&--fullscreen &__body &__image {
|
|
161
|
+
width: auto;
|
|
162
|
+
height: auto;
|
|
163
|
+
max-width: 100%;
|
|
164
|
+
max-height: 100%;
|
|
165
|
+
border-radius: var(--border-radius-s);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@media (max-width: 768px) {
|
|
169
|
+
&--popup &__body {
|
|
170
|
+
width: min(400px, calc(100vw - 64px));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import React from "react";
|
|
4
|
+
export const AudioViewer = ({ autoPlay = true, className, controls = true, loop = false, mimeType, muted = false, preload = 'metadata', src, title })=>{
|
|
5
|
+
return(// eslint-disable-next-line jsx-a11y/control-has-associated-label
|
|
6
|
+
/*#__PURE__*/ _jsxs("audio", {
|
|
7
|
+
autoPlay: autoPlay,
|
|
8
|
+
className: className,
|
|
9
|
+
controls: controls,
|
|
10
|
+
loop: loop,
|
|
11
|
+
muted: muted,
|
|
12
|
+
preload: preload,
|
|
13
|
+
title: title,
|
|
14
|
+
children: [
|
|
15
|
+
/*#__PURE__*/ _jsx("source", {
|
|
16
|
+
src: src,
|
|
17
|
+
type: mimeType
|
|
18
|
+
}),
|
|
19
|
+
/*#__PURE__*/ _jsx("track", {
|
|
20
|
+
kind: "captions"
|
|
21
|
+
})
|
|
22
|
+
]
|
|
23
|
+
}));
|
|
24
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import React from "react";
|
|
4
|
+
export const IframeViewer = ({ allow = 'accelerometer; gyroscope; autoplay; encrypted-media; picture-in-picture;', allowFullScreen = true, className, loading = 'lazy', src, title })=>{
|
|
5
|
+
return(// eslint-disable-next-line @eslint-react/dom/no-missing-iframe-sandbox
|
|
6
|
+
/*#__PURE__*/ _jsx("iframe", {
|
|
7
|
+
allow: allow,
|
|
8
|
+
allowFullScreen: allowFullScreen,
|
|
9
|
+
className: className,
|
|
10
|
+
loading: loading,
|
|
11
|
+
src: src,
|
|
12
|
+
title: title
|
|
13
|
+
}));
|
|
14
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import React from "react";
|
|
4
|
+
export const ImageViewer = ({ alt = 'Image preview', className, src })=>{
|
|
5
|
+
return /*#__PURE__*/ _jsx("img", {
|
|
6
|
+
alt: alt,
|
|
7
|
+
className: className,
|
|
8
|
+
src: src
|
|
9
|
+
});
|
|
10
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import React from "react";
|
|
4
|
+
export const VideoViewer = ({ autoPlay = true, className, controls = true, loop = false, mimeType, muted = false, preload = 'metadata', src, title })=>{
|
|
5
|
+
return(// eslint-disable-next-line jsx-a11y/control-has-associated-label
|
|
6
|
+
/*#__PURE__*/ _jsxs("video", {
|
|
7
|
+
autoPlay: autoPlay,
|
|
8
|
+
className: className,
|
|
9
|
+
controls: controls,
|
|
10
|
+
loop: loop,
|
|
11
|
+
muted: muted,
|
|
12
|
+
playsInline: autoPlay,
|
|
13
|
+
preload: preload,
|
|
14
|
+
title: title,
|
|
15
|
+
children: [
|
|
16
|
+
/*#__PURE__*/ _jsx("source", {
|
|
17
|
+
src: src,
|
|
18
|
+
type: mimeType
|
|
19
|
+
}),
|
|
20
|
+
/*#__PURE__*/ _jsx("track", {
|
|
21
|
+
kind: "captions"
|
|
22
|
+
})
|
|
23
|
+
]
|
|
24
|
+
}));
|
|
25
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
type ViewerProps = {
|
|
3
|
+
className?: string;
|
|
4
|
+
media: {
|
|
5
|
+
documentViewerUrl?: null | string;
|
|
6
|
+
mimeType?: string;
|
|
7
|
+
url?: string;
|
|
8
|
+
};
|
|
9
|
+
title?: string;
|
|
10
|
+
};
|
|
11
|
+
export declare const MediaPreviewViewer: React.FC<ViewerProps>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import React, { useMemo } from "react";
|
|
4
|
+
import { AudioViewer } from "./AudioViewer.js";
|
|
5
|
+
import { IframeViewer } from "./IframeViewer.js";
|
|
6
|
+
import { ImageViewer } from "./ImageViewer.js";
|
|
7
|
+
import { VideoViewer } from "./VideoViewer.js";
|
|
8
|
+
export const MediaPreviewViewer = ({ className, media, title })=>{
|
|
9
|
+
const { documentViewerUrl, mimeType, url } = media;
|
|
10
|
+
const isAudio = useMemo(()=>mimeType?.startsWith('audio/'), [
|
|
11
|
+
mimeType
|
|
12
|
+
]);
|
|
13
|
+
const isVideo = useMemo(()=>mimeType?.startsWith('video/'), [
|
|
14
|
+
mimeType
|
|
15
|
+
]);
|
|
16
|
+
const isImage = useMemo(()=>mimeType?.startsWith('image/'), [
|
|
17
|
+
mimeType
|
|
18
|
+
]);
|
|
19
|
+
if (isAudio && url) {
|
|
20
|
+
return /*#__PURE__*/ _jsx(AudioViewer, {
|
|
21
|
+
className: className,
|
|
22
|
+
mimeType: mimeType,
|
|
23
|
+
src: url,
|
|
24
|
+
title: title
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
if (isVideo && url) {
|
|
28
|
+
return /*#__PURE__*/ _jsx(VideoViewer, {
|
|
29
|
+
className: className,
|
|
30
|
+
mimeType: mimeType,
|
|
31
|
+
src: url,
|
|
32
|
+
title: title
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
if (isImage && url) {
|
|
36
|
+
return /*#__PURE__*/ _jsx(ImageViewer, {
|
|
37
|
+
alt: title || 'Image preview',
|
|
38
|
+
className: className,
|
|
39
|
+
src: url
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
if (documentViewerUrl) {
|
|
43
|
+
return /*#__PURE__*/ _jsx(IframeViewer, {
|
|
44
|
+
className: className,
|
|
45
|
+
src: documentViewerUrl,
|
|
46
|
+
title: title || 'Document preview'
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { MediaPreviewAdapterInlineResult, MediaPreviewAdapterNewTabResult, MediaPreviewAdapterResolveArgs } from '@/types.js';
|
|
2
|
+
import type { ImportMap, SanitizedConfig } from 'payload';
|
|
3
|
+
type PayloadLike = {
|
|
4
|
+
config: SanitizedConfig;
|
|
5
|
+
importMap: ImportMap;
|
|
6
|
+
};
|
|
7
|
+
export type AdapterMatch = {
|
|
8
|
+
adapterName: string;
|
|
9
|
+
result: MediaPreviewAdapterInlineResult | MediaPreviewAdapterNewTabResult;
|
|
10
|
+
};
|
|
11
|
+
export declare const resolveAdapter: (payloadLike: PayloadLike, adapterNames: string[] | undefined, args: MediaPreviewAdapterResolveArgs) => AdapterMatch | null;
|
|
12
|
+
export declare const resolveAdapterViewer: (payloadLike: PayloadLike, match: AdapterMatch | null) => null | React.ReactNode;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { RenderServerComponent } from "@payloadcms/ui/elements/RenderServerComponent";
|
|
2
|
+
const getAdapters = (config)=>{
|
|
3
|
+
const pluginData = config.custom?.['@seshuk/payload-media-preview'];
|
|
4
|
+
return pluginData?.adapters ?? [];
|
|
5
|
+
};
|
|
6
|
+
const filterAdapters = (allAdapters, adapterNames)=>{
|
|
7
|
+
if (!adapterNames) {
|
|
8
|
+
return allAdapters;
|
|
9
|
+
}
|
|
10
|
+
return allAdapters.filter((a)=>adapterNames.includes(a.name));
|
|
11
|
+
};
|
|
12
|
+
export const resolveAdapter = (payloadLike, adapterNames, args)=>{
|
|
13
|
+
const allAdapters = getAdapters(payloadLike.config);
|
|
14
|
+
if (allAdapters.length === 0) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const adapters = filterAdapters(allAdapters, adapterNames);
|
|
18
|
+
for (const adapter of adapters){
|
|
19
|
+
const result = adapter.resolve(args);
|
|
20
|
+
if (result) {
|
|
21
|
+
return {
|
|
22
|
+
adapterName: adapter.name,
|
|
23
|
+
result
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
};
|
|
29
|
+
export const resolveAdapterViewer = (payloadLike, match)=>{
|
|
30
|
+
if (!match || match.result.mode !== 'inline') {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const depKey = `media-preview-viewer-${match.adapterName}`;
|
|
34
|
+
const dep = payloadLike.config.admin?.dependencies?.[depKey];
|
|
35
|
+
if (!dep) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return RenderServerComponent({
|
|
39
|
+
clientProps: match.result.props,
|
|
40
|
+
Component: dep,
|
|
41
|
+
importMap: payloadLike.importMap
|
|
42
|
+
});
|
|
43
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { MediaPreviewFieldClient } from '@/components/Field/Field.js';
|
|
2
|
+
export { AudioViewer } from '@/components/Viewer/AudioViewer.js';
|
|
3
|
+
export { IframeViewer } from '@/components/Viewer/IframeViewer.js';
|
|
4
|
+
export { ImageViewer } from '@/components/Viewer/ImageViewer.js';
|
|
5
|
+
export { VideoViewer } from '@/components/Viewer/VideoViewer.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { MediaPreviewFieldClient } from "../components/Field/Field.js";
|
|
2
|
+
export { AudioViewer } from "../components/Viewer/AudioViewer.js";
|
|
3
|
+
export { IframeViewer } from "../components/Viewer/IframeViewer.js";
|
|
4
|
+
export { ImageViewer } from "../components/Viewer/ImageViewer.js";
|
|
5
|
+
export { VideoViewer } from "../components/Viewer/VideoViewer.js";
|