| 1 |
(function(_elementor_query, _elementor_utils, react) { |
| 2 |
|
| 3 |
//#region \0rolldown/runtime.js |
| 4 |
var __defProp = Object.defineProperty; |
| 5 |
var __name = (target, value) => __defProp(target, "name", { |
| 6 |
value, |
| 7 |
configurable: true |
| 8 |
}); |
| 9 |
var __exportAll = (all, no_symbols) => { |
| 10 |
let target = {}; |
| 11 |
for (var name in all) { |
| 12 |
__defProp(target, name, { |
| 13 |
get: all[name], |
| 14 |
enumerable: true |
| 15 |
}); |
| 16 |
} |
| 17 |
if (!no_symbols) { |
| 18 |
__defProp(target, Symbol.toStringTag, { value: "Module" }); |
| 19 |
} |
| 20 |
return target; |
| 21 |
}; |
| 22 |
|
| 23 |
//#endregion |
| 24 |
|
| 25 |
//#region packages/packages/libs/wp-media/src/errors.ts |
| 26 |
var WpMediaNotAvailableError = (0, _elementor_utils.createError)({ |
| 27 |
code: "wp_media_not_available", |
| 28 |
message: "`wp.media` is not available, make sure the `media-models` handle is set in the dependencies array" |
| 29 |
}); |
| 30 |
var WpPluploadSettingsNotAvailableError = (0, _elementor_utils.createError)({ |
| 31 |
code: "wp_plupload_settings_not_available", |
| 32 |
message: "`_wpPluploadSettings` is not available, make sure a wp media uploader is open" |
| 33 |
}); |
| 34 |
|
| 35 |
//#endregion |
| 36 |
//#region packages/packages/libs/wp-media/src/media.ts |
| 37 |
var wpMediaWindow = window; |
| 38 |
var media_default = /* @__PURE__ */ __name(() => { |
| 39 |
if (!wpMediaWindow.wp?.media) throw new WpMediaNotAvailableError(); |
| 40 |
return wpMediaWindow.wp.media; |
| 41 |
}, "default"); |
| 42 |
|
| 43 |
//#endregion |
| 44 |
//#region packages/packages/libs/wp-media/src/normalize.ts |
| 45 |
function normalize(attachment) { |
| 46 |
const { filesizeInBytes, filesizeHumanReadable, author, authorName, ...rest } = attachment; |
| 47 |
return { |
| 48 |
...rest, |
| 49 |
filesize: { |
| 50 |
inBytes: filesizeInBytes, |
| 51 |
humanReadable: filesizeHumanReadable |
| 52 |
}, |
| 53 |
author: { |
| 54 |
id: parseInt(author), |
| 55 |
name: authorName |
| 56 |
} |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
//#endregion |
| 61 |
//#region packages/packages/libs/wp-media/src/get-media-attachment.ts |
| 62 |
async function fetchAttachmentFromWP(id) { |
| 63 |
const model = media_default().attachment(id); |
| 64 |
const wpAttachment = model.toJSON(); |
| 65 |
if ("url" in wpAttachment) return normalize(wpAttachment); |
| 66 |
try { |
| 67 |
return normalize(await model.fetch()); |
| 68 |
} catch { |
| 69 |
return null; |
| 70 |
} |
| 71 |
} |
| 72 |
async function getMediaAttachment({ id }) { |
| 73 |
if (!id) return null; |
| 74 |
return (0, _elementor_query.getQueryClient)().ensureQueryData({ |
| 75 |
queryKey: ["wp-attachment", id], |
| 76 |
queryFn: () => fetchAttachmentFromWP(id) |
| 77 |
}); |
| 78 |
} |
| 79 |
|
| 80 |
//#endregion |
| 81 |
//#region packages/packages/libs/wp-media/src/hooks/use-wp-media-attachment.ts |
| 82 |
function useWpMediaAttachment(id) { |
| 83 |
return (0, _elementor_query.useQuery)({ |
| 84 |
queryKey: ["wp-attachment", id], |
| 85 |
queryFn: () => fetchAttachmentFromWP(id), |
| 86 |
enabled: !!id |
| 87 |
}); |
| 88 |
} |
| 89 |
|
| 90 |
//#endregion |
| 91 |
//#region packages/packages/libs/wp-media/src/wp-plupload-settings.ts |
| 92 |
var wpPluploadSettingsWindow = window; |
| 93 |
var wp_plupload_settings_default = /* @__PURE__ */ __name(() => { |
| 94 |
if (!wpPluploadSettingsWindow._wpPluploadSettings) throw new WpPluploadSettingsNotAvailableError(); |
| 95 |
return wpPluploadSettingsWindow._wpPluploadSettings; |
| 96 |
}, "default"); |
| 97 |
|
| 98 |
//#endregion |
| 99 |
//#region packages/packages/libs/wp-media/src/hooks/use-wp-media-frame.ts |
| 100 |
function useWpMediaFrame(options) { |
| 101 |
const frame = (0, react.useRef)(); |
| 102 |
const open = (openOptions = {}) => { |
| 103 |
cleanupFrame(frame.current); |
| 104 |
frame.current = createFrame({ |
| 105 |
...options, |
| 106 |
...openOptions |
| 107 |
}); |
| 108 |
frame.current?.open(); |
| 109 |
}; |
| 110 |
(0, react.useEffect)(() => { |
| 111 |
return () => { |
| 112 |
cleanupFrame(frame.current); |
| 113 |
}; |
| 114 |
}, []); |
| 115 |
return { open }; |
| 116 |
} |
| 117 |
function createFrame({ onSelect, onSelectUrl, allowUrlImport, multiple, mediaTypes, selected, title, mode = "browse", currentUrl, currentAlt }) { |
| 118 |
const frame = media_default()({ |
| 119 |
title, |
| 120 |
multiple, |
| 121 |
library: { type: getMimeTypes(mediaTypes) }, |
| 122 |
...allowUrlImport ? { frame: "post" } : {} |
| 123 |
}).on("open", () => { |
| 124 |
setTypeCaller(frame); |
| 125 |
applyMode(frame, mode, currentUrl, currentAlt); |
| 126 |
if (mode !== "url") applySelection(frame, selected); |
| 127 |
}).on("insert select", () => select(frame, multiple, onSelect, onSelectUrl)); |
| 128 |
if (allowUrlImport) frame.on("ready open", () => restrictFrameMenu(frame)); |
| 129 |
handleExtensions(frame, mediaTypes); |
| 130 |
return frame; |
| 131 |
} |
| 132 |
function cleanupFrame(frame) { |
| 133 |
frame?.detach(); |
| 134 |
frame?.remove(); |
| 135 |
} |
| 136 |
function applyMode(frame, mode = "browse", currentUrl, currentAlt) { |
| 137 |
if (mode === "url") { |
| 138 |
frame.setState("embed"); |
| 139 |
if (currentUrl || currentAlt) setTimeout(() => { |
| 140 |
if (currentUrl) frame.state()?.props?.set("url", currentUrl); |
| 141 |
if (currentAlt) frame.state()?.props?.set("alt", currentAlt); |
| 142 |
}, 0); |
| 143 |
} else frame.content.mode(mode); |
| 144 |
} |
| 145 |
function applySelection(frame, selected) { |
| 146 |
const selectedAttachments = (typeof selected === "number" ? [selected] : selected)?.filter((id) => !!id).map((id) => media_default().attachment(id)); |
| 147 |
frame.state().get("selection").set(selectedAttachments || []); |
| 148 |
} |
| 149 |
function select(frame, multiple, onSelect, onSelectUrl) { |
| 150 |
const state = frame.state(); |
| 151 |
if (state.get("id") === "embed") { |
| 152 |
if (onSelectUrl) { |
| 153 |
const url = state.props?.get("url"); |
| 154 |
const alt = state.props?.get("alt"); |
| 155 |
if (url) onSelectUrl(url, alt); |
| 156 |
} |
| 157 |
return; |
| 158 |
} |
| 159 |
const attachments = state.get("selection").toJSON().map(normalize); |
| 160 |
onSelect(multiple ? attachments : attachments[0]); |
| 161 |
} |
| 162 |
var FRAME_MENU_ITEMS_TO_REMOVE = [ |
| 163 |
"#menu-item-gallery", |
| 164 |
"#menu-item-featured-image", |
| 165 |
"#menu-item-playlist", |
| 166 |
"#menu-item-video-playlist" |
| 167 |
].join(","); |
| 168 |
function restrictFrameMenu(frame) { |
| 169 |
frame.$el?.find(FRAME_MENU_ITEMS_TO_REMOVE)?.remove(); |
| 170 |
} |
| 171 |
function setTypeCaller(frame) { |
| 172 |
frame.uploader.uploader.param("uploadTypeCaller", "elementor-wp-media-upload"); |
| 173 |
} |
| 174 |
function handleExtensions(frame, mediaTypes) { |
| 175 |
const defaultExtensions = wp_plupload_settings_default().defaults.filters.mime_types?.[0]?.extensions; |
| 176 |
frame.on("ready", () => { |
| 177 |
wp_plupload_settings_default().defaults.filters.mime_types = [{ extensions: getExtensions(mediaTypes) }]; |
| 178 |
}); |
| 179 |
frame.on("close", () => { |
| 180 |
wp_plupload_settings_default().defaults.filters.mime_types = defaultExtensions ? [{ extensions: defaultExtensions }] : []; |
| 181 |
}); |
| 182 |
} |
| 183 |
var imageExtensions = [ |
| 184 |
"avif", |
| 185 |
"bmp", |
| 186 |
"gif", |
| 187 |
"ico", |
| 188 |
"jpe", |
| 189 |
"jpeg", |
| 190 |
"jpg", |
| 191 |
"png", |
| 192 |
"webp" |
| 193 |
]; |
| 194 |
var videoExtensions = [ |
| 195 |
"mp4", |
| 196 |
"webm", |
| 197 |
"ogg", |
| 198 |
"mov", |
| 199 |
"m4v", |
| 200 |
"avi", |
| 201 |
"wmv", |
| 202 |
"mpg", |
| 203 |
"mpeg", |
| 204 |
"3gp", |
| 205 |
"3g2" |
| 206 |
]; |
| 207 |
function getMimeTypes(mediaTypes) { |
| 208 |
const mimeTypesPerType = { |
| 209 |
image: imageExtensions.map((extension) => `image/${extension}`), |
| 210 |
svg: ["image/svg+xml"], |
| 211 |
video: [ |
| 212 |
"video/mp4", |
| 213 |
"video/webm", |
| 214 |
"video/ogg", |
| 215 |
"video/quicktime", |
| 216 |
"video/x-m4v", |
| 217 |
"video/avi", |
| 218 |
"video/x-ms-wmv", |
| 219 |
"video/mpeg", |
| 220 |
"video/3gpp", |
| 221 |
"video/3gpp2" |
| 222 |
] |
| 223 |
}; |
| 224 |
return mediaTypes.reduce((prev, currentType) => { |
| 225 |
return prev.concat(mimeTypesPerType[currentType]); |
| 226 |
}, []); |
| 227 |
} |
| 228 |
function getExtensions(mediaTypes) { |
| 229 |
const extensionsPerType = { |
| 230 |
image: imageExtensions, |
| 231 |
svg: ["svg"], |
| 232 |
video: videoExtensions |
| 233 |
}; |
| 234 |
return mediaTypes.reduce((prev, currentType) => { |
| 235 |
return prev.concat(extensionsPerType[currentType]); |
| 236 |
}, []).join(","); |
| 237 |
} |
| 238 |
|
| 239 |
//#endregion |
| 240 |
//#region packages/packages/libs/wp-media/src/index.ts |
| 241 |
var src_exports = /* @__PURE__ */ __exportAll({ |
| 242 |
getMediaAttachment: () => getMediaAttachment, |
| 243 |
useWpMediaAttachment: () => useWpMediaAttachment, |
| 244 |
useWpMediaFrame: () => useWpMediaFrame |
| 245 |
}); |
| 246 |
|
| 247 |
//#endregion |
| 248 |
//#region \0elementor-package-library-entry |
| 249 |
(window.elementorV2 = window.elementorV2 || {}).wpMedia = src_exports; |
| 250 |
|
| 251 |
//#endregion |
| 252 |
})(elementorV2.query, elementorV2.utils, React); |
| 253 |
window.elementorV2.wpMedia?.init?.(); |
| 254 |
//# sourceMappingURL=wp-media.js.map |