@elementor/wp-media 4.1.0-797 → 4.1.0-799
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/dist/index.d.mts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +46 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +46 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/hooks/use-wp-media-frame.ts +58 -7
- package/src/types/wp-media.ts +9 -2
package/dist/index.d.mts
CHANGED
|
@@ -28,12 +28,14 @@ type Attachment = {
|
|
|
28
28
|
};
|
|
29
29
|
|
|
30
30
|
type OpenOptions = {
|
|
31
|
-
mode?: 'upload' | 'browse';
|
|
31
|
+
mode?: 'upload' | 'browse' | 'url';
|
|
32
32
|
};
|
|
33
33
|
type MediaType = 'image' | 'svg' | 'video';
|
|
34
34
|
type Options = {
|
|
35
35
|
mediaTypes: MediaType[];
|
|
36
36
|
title?: string;
|
|
37
|
+
allowUrlImport?: boolean;
|
|
38
|
+
onSelectUrl?: (url: string, alt?: string) => void;
|
|
37
39
|
} & ({
|
|
38
40
|
multiple: true;
|
|
39
41
|
selected: Array<number | null>;
|
package/dist/index.d.ts
CHANGED
|
@@ -28,12 +28,14 @@ type Attachment = {
|
|
|
28
28
|
};
|
|
29
29
|
|
|
30
30
|
type OpenOptions = {
|
|
31
|
-
mode?: 'upload' | 'browse';
|
|
31
|
+
mode?: 'upload' | 'browse' | 'url';
|
|
32
32
|
};
|
|
33
33
|
type MediaType = 'image' | 'svg' | 'video';
|
|
34
34
|
type Options = {
|
|
35
35
|
mediaTypes: MediaType[];
|
|
36
36
|
title?: string;
|
|
37
|
+
allowUrlImport?: boolean;
|
|
38
|
+
onSelectUrl?: (url: string, alt?: string) => void;
|
|
37
39
|
} & ({
|
|
38
40
|
multiple: true;
|
|
39
41
|
selected: Array<number | null>;
|
package/dist/index.js
CHANGED
|
@@ -121,18 +121,33 @@ function useWpMediaFrame(options) {
|
|
|
121
121
|
open
|
|
122
122
|
};
|
|
123
123
|
}
|
|
124
|
-
function createFrame({
|
|
124
|
+
function createFrame({
|
|
125
|
+
onSelect,
|
|
126
|
+
onSelectUrl,
|
|
127
|
+
allowUrlImport,
|
|
128
|
+
multiple,
|
|
129
|
+
mediaTypes,
|
|
130
|
+
selected,
|
|
131
|
+
title,
|
|
132
|
+
mode = "browse"
|
|
133
|
+
}) {
|
|
125
134
|
const frame = media_default()({
|
|
126
135
|
title,
|
|
127
136
|
multiple,
|
|
128
137
|
library: {
|
|
129
138
|
type: getMimeTypes(mediaTypes)
|
|
130
|
-
}
|
|
139
|
+
},
|
|
140
|
+
...allowUrlImport ? { frame: "post" } : {}
|
|
131
141
|
}).on("open", () => {
|
|
132
142
|
setTypeCaller(frame);
|
|
133
143
|
applyMode(frame, mode);
|
|
134
|
-
|
|
135
|
-
|
|
144
|
+
if (mode !== "url") {
|
|
145
|
+
applySelection(frame, selected);
|
|
146
|
+
}
|
|
147
|
+
}).on("close", () => cleanupFrame(frame)).on("insert select", () => select(frame, multiple, onSelect, onSelectUrl));
|
|
148
|
+
if (allowUrlImport) {
|
|
149
|
+
frame.on("ready open", () => restrictFrameMenu(frame));
|
|
150
|
+
}
|
|
136
151
|
handleExtensions(frame, mediaTypes);
|
|
137
152
|
return frame;
|
|
138
153
|
}
|
|
@@ -141,17 +156,41 @@ function cleanupFrame(frame) {
|
|
|
141
156
|
frame?.remove();
|
|
142
157
|
}
|
|
143
158
|
function applyMode(frame, mode = "browse") {
|
|
144
|
-
|
|
159
|
+
if (mode === "url") {
|
|
160
|
+
frame.setState("embed");
|
|
161
|
+
} else {
|
|
162
|
+
frame.content.mode(mode);
|
|
163
|
+
}
|
|
145
164
|
}
|
|
146
165
|
function applySelection(frame, selected) {
|
|
147
166
|
const selectedAttachments = (typeof selected === "number" ? [selected] : selected)?.filter((id) => !!id).map((id) => media_default().attachment(id));
|
|
148
167
|
frame.state().get("selection").set(selectedAttachments || []);
|
|
149
168
|
}
|
|
150
|
-
function select(frame, multiple, onSelect) {
|
|
151
|
-
const
|
|
169
|
+
function select(frame, multiple, onSelect, onSelectUrl) {
|
|
170
|
+
const state = frame.state();
|
|
171
|
+
if (state.get("id") === "embed") {
|
|
172
|
+
if (onSelectUrl) {
|
|
173
|
+
const url = state.props?.get("url");
|
|
174
|
+
const alt = state.props?.get("alt");
|
|
175
|
+
if (url) {
|
|
176
|
+
onSelectUrl(url, alt);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const attachments = state.get("selection").toJSON().map(normalize);
|
|
152
182
|
const onSelectFn = onSelect;
|
|
153
183
|
onSelectFn(multiple ? attachments : attachments[0]);
|
|
154
184
|
}
|
|
185
|
+
var FRAME_MENU_ITEMS_TO_REMOVE = [
|
|
186
|
+
"#menu-item-gallery",
|
|
187
|
+
"#menu-item-featured-image",
|
|
188
|
+
"#menu-item-playlist",
|
|
189
|
+
"#menu-item-video-playlist"
|
|
190
|
+
].join(",");
|
|
191
|
+
function restrictFrameMenu(frame) {
|
|
192
|
+
frame.$el?.find(FRAME_MENU_ITEMS_TO_REMOVE)?.remove();
|
|
193
|
+
}
|
|
155
194
|
function setTypeCaller(frame) {
|
|
156
195
|
frame.uploader.uploader.param("uploadTypeCaller", "elementor-wp-media-upload");
|
|
157
196
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/hooks/use-wp-media-attachment.ts","../src/errors.ts","../src/media.ts","../src/normalize.ts","../src/get-media-attachment.ts","../src/hooks/use-wp-media-frame.ts","../src/wp-plupload-settings.ts"],"sourcesContent":["export type { Attachment } from './types/attachment';\nexport type { OpenOptions, MediaType } from './hooks/use-wp-media-frame';\n\nexport { default as useWpMediaAttachment } from './hooks/use-wp-media-attachment';\nexport { default as useWpMediaFrame } from './hooks/use-wp-media-frame';\n\nexport { getMediaAttachment } from './get-media-attachment';\n","import { useQuery } from '@elementor/query';\n\nimport { getMediaAttachment } from '../get-media-attachment';\n\nexport default function useWpMediaAttachment( id: number | null ) {\n\treturn useQuery( {\n\t\tqueryKey: [ 'wp-attachment', id ],\n\t\tqueryFn: () => getMediaAttachment( { id } ),\n\t\tenabled: !! id,\n\t} );\n}\n","import { createError } from '@elementor/utils';\n\nexport const WpMediaNotAvailableError = createError( {\n\tcode: 'wp_media_not_available',\n\tmessage: '`wp.media` is not available, make sure the `media-models` handle is set in the dependencies array',\n} );\n\nexport const WpPluploadSettingsNotAvailableError = createError( {\n\tcode: 'wp_plupload_settings_not_available',\n\tmessage: '`_wpPluploadSettings` is not available, make sure a wp media uploader is open',\n} );\n","import { WpMediaNotAvailableError } from './errors';\nimport { type WpMediaWindow } from './types/wp-media';\n\nconst wpMediaWindow = window as unknown as WpMediaWindow;\n\nexport default () => {\n\tif ( ! wpMediaWindow.wp?.media ) {\n\t\tthrow new WpMediaNotAvailableError();\n\t}\n\n\treturn wpMediaWindow.wp.media;\n};\n","import { type Attachment } from './types/attachment';\nimport { type WpAttachmentJSON } from './types/wp-media';\n\nexport default function normalize( attachment: WpAttachmentJSON ): Attachment {\n\tconst { filesizeInBytes, filesizeHumanReadable, author, authorName, ...rest } = attachment;\n\n\treturn {\n\t\t...rest,\n\t\tfilesize: {\n\t\t\tinBytes: filesizeInBytes,\n\t\t\thumanReadable: filesizeHumanReadable,\n\t\t},\n\t\tauthor: {\n\t\t\tid: parseInt( author ),\n\t\t\tname: authorName,\n\t\t},\n\t};\n}\n","import media from './media';\nimport normalize from './normalize';\n\nexport async function getMediaAttachment( { id }: { id: number | null } ) {\n\tif ( ! id ) {\n\t\treturn null;\n\t}\n\n\tconst model = media().attachment( id );\n\tconst wpAttachment = model.toJSON();\n\n\tconst isFetched = 'url' in wpAttachment;\n\n\tif ( isFetched ) {\n\t\treturn normalize( wpAttachment );\n\t}\n\n\ttry {\n\t\treturn normalize( await model.fetch() );\n\t} catch {\n\t\treturn null;\n\t}\n}\n","import { useEffect, useRef } from 'react';\n\nimport media from '../media';\nimport normalize from '../normalize';\nimport { type Attachment } from '../types/attachment';\nimport { type MediaFrame } from '../types/wp-media';\nimport wpPluploadSettings from '../wp-plupload-settings';\n\nexport type OpenOptions = {\n\tmode?: 'upload' | 'browse';\n};\n\nexport type MediaType = 'image' | 'svg' | 'video';\n\ntype Options = {\n\tmediaTypes: MediaType[];\n\ttitle?: string;\n} & (\n\t| {\n\t\t\tmultiple: true;\n\t\t\tselected: Array< number | null >;\n\t\t\tonSelect: ( val: Attachment[] ) => void;\n\t }\n\t| {\n\t\t\tmultiple: false;\n\t\t\tselected: number | null;\n\t\t\tonSelect: ( val: Attachment ) => void;\n\t }\n);\n\nexport default function useWpMediaFrame( options: Options ) {\n\tconst frame = useRef< MediaFrame >();\n\n\tconst open = ( openOptions: OpenOptions = {} ) => {\n\t\tcleanupFrame( frame.current );\n\n\t\tframe.current = createFrame( { ...options, ...openOptions } );\n\n\t\tframe.current?.open();\n\t};\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tcleanupFrame( frame.current );\n\t\t};\n\t}, [] );\n\n\treturn {\n\t\topen,\n\t};\n}\n\nfunction createFrame( { onSelect, multiple, mediaTypes, selected, title, mode = 'browse' }: Options & OpenOptions ) {\n\tconst frame: MediaFrame = media()( {\n\t\ttitle,\n\t\tmultiple,\n\t\tlibrary: {\n\t\t\ttype: getMimeTypes( mediaTypes ),\n\t\t},\n\t} )\n\t\t.on( 'open', () => {\n\t\t\tsetTypeCaller( frame );\n\t\t\tapplyMode( frame, mode );\n\t\t\tapplySelection( frame, selected );\n\t\t} )\n\t\t.on( 'close', () => cleanupFrame( frame ) )\n\t\t.on( 'insert select', () => select( frame, multiple, onSelect ) );\n\n\thandleExtensions( frame, mediaTypes );\n\n\treturn frame;\n}\n\nfunction cleanupFrame( frame?: MediaFrame ) {\n\tframe?.detach();\n\tframe?.remove();\n}\n\nfunction applyMode( frame: MediaFrame, mode: OpenOptions[ 'mode' ] = 'browse' ) {\n\tframe.content.mode( mode );\n}\n\nfunction applySelection( frame: MediaFrame, selected: number | null | Array< number | null > ) {\n\tconst selectedAttachments = ( typeof selected === 'number' ? [ selected ] : selected )\n\t\t?.filter( ( id ) => !! id )\n\t\t.map( ( id ) => media().attachment( id as number ) );\n\n\tframe\n\t\t.state()\n\t\t.get( 'selection' )\n\t\t.set( selectedAttachments || [] );\n}\n\nfunction select( frame: MediaFrame, multiple: boolean, onSelect: Options[ 'onSelect' ] ) {\n\tconst attachments = frame.state().get( 'selection' ).toJSON().map( normalize );\n\n\tconst onSelectFn = onSelect as ( val: Attachment | Attachment[] ) => void;\n\n\tonSelectFn( multiple ? attachments : attachments[ 0 ] );\n}\n\nfunction setTypeCaller( frame: MediaFrame ) {\n\tframe.uploader.uploader.param( 'uploadTypeCaller', 'elementor-wp-media-upload' );\n}\n\nfunction handleExtensions( frame: MediaFrame, mediaTypes: MediaType[] ) {\n\tconst defaultExtensions = wpPluploadSettings().defaults.filters.mime_types?.[ 0 ]?.extensions;\n\n\t// Set extensions by media types\n\tframe.on( 'ready', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = [ { extensions: getExtensions( mediaTypes ) } ];\n\t} );\n\n\t// Restore default upload extensions\n\tframe.on( 'close', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = defaultExtensions\n\t\t\t? [ { extensions: defaultExtensions } ]\n\t\t\t: [];\n\t} );\n}\n\nconst imageExtensions = [ 'avif', 'bmp', 'gif', 'ico', 'jpe', 'jpeg', 'jpg', 'png', 'webp' ];\nconst videoExtensions = [ 'mp4', 'webm', 'ogg', 'mov', 'm4v', 'avi', 'wmv', 'mpg', 'mpeg', '3gp', '3g2' ];\n\nfunction getMimeTypes( mediaTypes: MediaType[] ) {\n\tconst mimeTypesPerType: Record< MediaType, string[] > = {\n\t\timage: imageExtensions.map( ( extension ) => `image/${ extension }` ),\n\t\tsvg: [ 'image/svg+xml' ],\n\t\tvideo: [\n\t\t\t'video/mp4',\n\t\t\t'video/webm',\n\t\t\t'video/ogg',\n\t\t\t'video/quicktime',\n\t\t\t'video/x-m4v',\n\t\t\t'video/avi',\n\t\t\t'video/x-ms-wmv',\n\t\t\t'video/mpeg',\n\t\t\t'video/3gpp',\n\t\t\t'video/3gpp2',\n\t\t],\n\t};\n\n\treturn mediaTypes.reduce( ( prev, currentType ) => {\n\t\treturn prev.concat( mimeTypesPerType[ currentType ] );\n\t}, [] as string[] );\n}\n\nfunction getExtensions( mediaTypes: MediaType[] ) {\n\tconst extensionsPerType: Record< MediaType, string[] > = {\n\t\timage: imageExtensions,\n\t\tsvg: [ 'svg' ],\n\t\tvideo: videoExtensions,\n\t};\n\n\tconst extensions = mediaTypes.reduce( ( prev, currentType ) => {\n\t\treturn prev.concat( extensionsPerType[ currentType ] );\n\t}, [] as string[] );\n\n\treturn extensions.join( ',' );\n}\n","import { WpPluploadSettingsNotAvailableError } from './errors';\nimport { type WpPluploadSettingsWindow } from './types/plupload';\n\nconst wpPluploadSettingsWindow = window as unknown as WpPluploadSettingsWindow;\n\nexport default () => {\n\tif ( ! wpPluploadSettingsWindow._wpPluploadSettings ) {\n\t\tthrow new WpPluploadSettingsNotAvailableError();\n\t}\n\n\treturn wpPluploadSettingsWindow._wpPluploadSettings;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAyB;;;ACAzB,mBAA4B;AAErB,IAAM,+BAA2B,0BAAa;AAAA,EACpD,MAAM;AAAA,EACN,SAAS;AACV,CAAE;AAEK,IAAM,0CAAsC,0BAAa;AAAA,EAC/D,MAAM;AAAA,EACN,SAAS;AACV,CAAE;;;ACPF,IAAM,gBAAgB;AAEtB,IAAO,gBAAQ,MAAM;AACpB,MAAK,CAAE,cAAc,IAAI,OAAQ;AAChC,UAAM,IAAI,yBAAyB;AAAA,EACpC;AAEA,SAAO,cAAc,GAAG;AACzB;;;ACRe,SAAR,UAA4B,YAA2C;AAC7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AAM,GAAI;AAC7B;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/hooks/use-wp-media-attachment.ts","../src/errors.ts","../src/media.ts","../src/normalize.ts","../src/get-media-attachment.ts","../src/hooks/use-wp-media-frame.ts","../src/wp-plupload-settings.ts"],"sourcesContent":["export type { Attachment } from './types/attachment';\nexport type { OpenOptions, MediaType } from './hooks/use-wp-media-frame';\n\nexport { default as useWpMediaAttachment } from './hooks/use-wp-media-attachment';\nexport { default as useWpMediaFrame } from './hooks/use-wp-media-frame';\n\nexport { getMediaAttachment } from './get-media-attachment';\n","import { useQuery } from '@elementor/query';\n\nimport { getMediaAttachment } from '../get-media-attachment';\n\nexport default function useWpMediaAttachment( id: number | null ) {\n\treturn useQuery( {\n\t\tqueryKey: [ 'wp-attachment', id ],\n\t\tqueryFn: () => getMediaAttachment( { id } ),\n\t\tenabled: !! id,\n\t} );\n}\n","import { createError } from '@elementor/utils';\n\nexport const WpMediaNotAvailableError = createError( {\n\tcode: 'wp_media_not_available',\n\tmessage: '`wp.media` is not available, make sure the `media-models` handle is set in the dependencies array',\n} );\n\nexport const WpPluploadSettingsNotAvailableError = createError( {\n\tcode: 'wp_plupload_settings_not_available',\n\tmessage: '`_wpPluploadSettings` is not available, make sure a wp media uploader is open',\n} );\n","import { WpMediaNotAvailableError } from './errors';\nimport { type WpMediaWindow } from './types/wp-media';\n\nconst wpMediaWindow = window as unknown as WpMediaWindow;\n\nexport default () => {\n\tif ( ! wpMediaWindow.wp?.media ) {\n\t\tthrow new WpMediaNotAvailableError();\n\t}\n\n\treturn wpMediaWindow.wp.media;\n};\n","import { type Attachment } from './types/attachment';\nimport { type WpAttachmentJSON } from './types/wp-media';\n\nexport default function normalize( attachment: WpAttachmentJSON ): Attachment {\n\tconst { filesizeInBytes, filesizeHumanReadable, author, authorName, ...rest } = attachment;\n\n\treturn {\n\t\t...rest,\n\t\tfilesize: {\n\t\t\tinBytes: filesizeInBytes,\n\t\t\thumanReadable: filesizeHumanReadable,\n\t\t},\n\t\tauthor: {\n\t\t\tid: parseInt( author ),\n\t\t\tname: authorName,\n\t\t},\n\t};\n}\n","import media from './media';\nimport normalize from './normalize';\n\nexport async function getMediaAttachment( { id }: { id: number | null } ) {\n\tif ( ! id ) {\n\t\treturn null;\n\t}\n\n\tconst model = media().attachment( id );\n\tconst wpAttachment = model.toJSON();\n\n\tconst isFetched = 'url' in wpAttachment;\n\n\tif ( isFetched ) {\n\t\treturn normalize( wpAttachment );\n\t}\n\n\ttry {\n\t\treturn normalize( await model.fetch() );\n\t} catch {\n\t\treturn null;\n\t}\n}\n","import { useEffect, useRef } from 'react';\n\nimport media from '../media';\nimport normalize from '../normalize';\nimport { type Attachment } from '../types/attachment';\nimport { type MediaFrame } from '../types/wp-media';\nimport wpPluploadSettings from '../wp-plupload-settings';\n\nexport type OpenOptions = {\n\tmode?: 'upload' | 'browse' | 'url';\n};\n\nexport type MediaType = 'image' | 'svg' | 'video';\n\ntype Options = {\n\tmediaTypes: MediaType[];\n\ttitle?: string;\n\tallowUrlImport?: boolean;\n\tonSelectUrl?: ( url: string, alt?: string ) => void;\n} & (\n\t| {\n\t\t\tmultiple: true;\n\t\t\tselected: Array< number | null >;\n\t\t\tonSelect: ( val: Attachment[] ) => void;\n\t }\n\t| {\n\t\t\tmultiple: false;\n\t\t\tselected: number | null;\n\t\t\tonSelect: ( val: Attachment ) => void;\n\t }\n);\n\nexport default function useWpMediaFrame( options: Options ) {\n\tconst frame = useRef< MediaFrame >();\n\n\tconst open = ( openOptions: OpenOptions = {} ) => {\n\t\tcleanupFrame( frame.current );\n\n\t\tframe.current = createFrame( { ...options, ...openOptions } );\n\n\t\tframe.current?.open();\n\t};\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tcleanupFrame( frame.current );\n\t\t};\n\t}, [] );\n\n\treturn {\n\t\topen,\n\t};\n}\n\nfunction createFrame( {\n\tonSelect,\n\tonSelectUrl,\n\tallowUrlImport,\n\tmultiple,\n\tmediaTypes,\n\tselected,\n\ttitle,\n\tmode = 'browse',\n}: Options & OpenOptions ) {\n\tconst frame: MediaFrame = media()( {\n\t\ttitle,\n\t\tmultiple,\n\t\tlibrary: {\n\t\t\ttype: getMimeTypes( mediaTypes ),\n\t\t},\n\t\t...( allowUrlImport ? { frame: 'post' } : {} ),\n\t} )\n\t\t.on( 'open', () => {\n\t\t\tsetTypeCaller( frame );\n\t\t\tapplyMode( frame, mode );\n\t\t\tif ( mode !== 'url' ) {\n\t\t\t\tapplySelection( frame, selected );\n\t\t\t}\n\t\t} )\n\t\t.on( 'close', () => cleanupFrame( frame ) )\n\t\t.on( 'insert select', () => select( frame, multiple, onSelect, onSelectUrl ) );\n\n\tif ( allowUrlImport ) {\n\t\tframe.on( 'ready open', () => restrictFrameMenu( frame ) );\n\t}\n\n\thandleExtensions( frame, mediaTypes );\n\n\treturn frame;\n}\n\nfunction cleanupFrame( frame?: MediaFrame ) {\n\tframe?.detach();\n\tframe?.remove();\n}\n\nfunction applyMode( frame: MediaFrame, mode: OpenOptions[ 'mode' ] = 'browse' ) {\n\tif ( mode === 'url' ) {\n\t\tframe.setState( 'embed' );\n\t} else {\n\t\tframe.content.mode( mode );\n\t}\n}\n\nfunction applySelection( frame: MediaFrame, selected: number | null | Array< number | null > ) {\n\tconst selectedAttachments = ( typeof selected === 'number' ? [ selected ] : selected )\n\t\t?.filter( ( id ) => !! id )\n\t\t.map( ( id ) => media().attachment( id as number ) );\n\n\tframe\n\t\t.state()\n\t\t.get( 'selection' )\n\t\t.set( selectedAttachments || [] );\n}\n\nfunction select(\n\tframe: MediaFrame,\n\tmultiple: boolean,\n\tonSelect: Options[ 'onSelect' ],\n\tonSelectUrl?: Options[ 'onSelectUrl' ]\n) {\n\tconst state = frame.state();\n\n\tif ( state.get( 'id' ) === 'embed' ) {\n\t\tif ( onSelectUrl ) {\n\t\t\tconst url = state.props?.get( 'url' );\n\t\t\tconst alt = state.props?.get( 'alt' );\n\t\t\tif ( url ) {\n\t\t\t\tonSelectUrl( url, alt );\n\t\t\t}\n\t\t}\n\t\treturn;\n\t}\n\n\tconst attachments = state.get( 'selection' ).toJSON().map( normalize );\n\n\tconst onSelectFn = onSelect as ( val: Attachment | Attachment[] ) => void;\n\n\tonSelectFn( multiple ? attachments : attachments[ 0 ] );\n}\n\nconst FRAME_MENU_ITEMS_TO_REMOVE = [\n\t'#menu-item-gallery',\n\t'#menu-item-featured-image',\n\t'#menu-item-playlist',\n\t'#menu-item-video-playlist',\n].join( ',' );\n\nfunction restrictFrameMenu( frame: MediaFrame ) {\n\tframe.$el?.find( FRAME_MENU_ITEMS_TO_REMOVE )?.remove();\n}\n\nfunction setTypeCaller( frame: MediaFrame ) {\n\tframe.uploader.uploader.param( 'uploadTypeCaller', 'elementor-wp-media-upload' );\n}\n\nfunction handleExtensions( frame: MediaFrame, mediaTypes: MediaType[] ) {\n\tconst defaultExtensions = wpPluploadSettings().defaults.filters.mime_types?.[ 0 ]?.extensions;\n\n\t// Set extensions by media types\n\tframe.on( 'ready', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = [ { extensions: getExtensions( mediaTypes ) } ];\n\t} );\n\n\t// Restore default upload extensions\n\tframe.on( 'close', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = defaultExtensions\n\t\t\t? [ { extensions: defaultExtensions } ]\n\t\t\t: [];\n\t} );\n}\n\nconst imageExtensions = [ 'avif', 'bmp', 'gif', 'ico', 'jpe', 'jpeg', 'jpg', 'png', 'webp' ];\nconst videoExtensions = [ 'mp4', 'webm', 'ogg', 'mov', 'm4v', 'avi', 'wmv', 'mpg', 'mpeg', '3gp', '3g2' ];\n\nfunction getMimeTypes( mediaTypes: MediaType[] ) {\n\tconst mimeTypesPerType: Record< MediaType, string[] > = {\n\t\timage: imageExtensions.map( ( extension ) => `image/${ extension }` ),\n\t\tsvg: [ 'image/svg+xml' ],\n\t\tvideo: [\n\t\t\t'video/mp4',\n\t\t\t'video/webm',\n\t\t\t'video/ogg',\n\t\t\t'video/quicktime',\n\t\t\t'video/x-m4v',\n\t\t\t'video/avi',\n\t\t\t'video/x-ms-wmv',\n\t\t\t'video/mpeg',\n\t\t\t'video/3gpp',\n\t\t\t'video/3gpp2',\n\t\t],\n\t};\n\n\treturn mediaTypes.reduce( ( prev, currentType ) => {\n\t\treturn prev.concat( mimeTypesPerType[ currentType ] );\n\t}, [] as string[] );\n}\n\nfunction getExtensions( mediaTypes: MediaType[] ) {\n\tconst extensionsPerType: Record< MediaType, string[] > = {\n\t\timage: imageExtensions,\n\t\tsvg: [ 'svg' ],\n\t\tvideo: videoExtensions,\n\t};\n\n\tconst extensions = mediaTypes.reduce( ( prev, currentType ) => {\n\t\treturn prev.concat( extensionsPerType[ currentType ] );\n\t}, [] as string[] );\n\n\treturn extensions.join( ',' );\n}\n","import { WpPluploadSettingsNotAvailableError } from './errors';\nimport { type WpPluploadSettingsWindow } from './types/plupload';\n\nconst wpPluploadSettingsWindow = window as unknown as WpPluploadSettingsWindow;\n\nexport default () => {\n\tif ( ! wpPluploadSettingsWindow._wpPluploadSettings ) {\n\t\tthrow new WpPluploadSettingsNotAvailableError();\n\t}\n\n\treturn wpPluploadSettingsWindow._wpPluploadSettings;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAyB;;;ACAzB,mBAA4B;AAErB,IAAM,+BAA2B,0BAAa;AAAA,EACpD,MAAM;AAAA,EACN,SAAS;AACV,CAAE;AAEK,IAAM,0CAAsC,0BAAa;AAAA,EAC/D,MAAM;AAAA,EACN,SAAS;AACV,CAAE;;;ACPF,IAAM,gBAAgB;AAEtB,IAAO,gBAAQ,MAAM;AACpB,MAAK,CAAE,cAAc,IAAI,OAAQ;AAChC,UAAM,IAAI,yBAAyB;AAAA,EACpC;AAEA,SAAO,cAAc,GAAG;AACzB;;;ACRe,SAAR,UAA4B,YAA2C;AAC7E,QAAM,EAAE,iBAAiB,uBAAuB,QAAQ,YA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G,SAAS,aAAc,YAA0B;AAChD,QAAM,mBAAkD;AAAA,IACvD,OAAO,gBAAgB,IAAK,CAAE,cAAe,SAAU,SAAU,EAAG;AAAA,IACpE,KAAK,CAAE,eAAgB;AAAA,IACvB,OAAO;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAEA,SAAO,WAAW,OAAQ,CAAE,MAAM,gBAAiB;AAClD,WAAO,KAAK,OAAQ,iBAAkB,WAAY,CAAE;AAAA,EACrD,GAAG,CAAC,CAAc;AACnB;AAEA,SAAS,cAAe,YAA0B;AACjD,QAAM,oBAAmD;AAAA,IACxD,OAAO;AAAA,IACP,KAAK,CAAE,KAAM;AAAA,IACb,OAAO;AAAA,EACR;AAEA,QAAM,aAAa,WAAW,OAAQ,CAAE,MAAM,gBAAiB;AAC9D,WAAO,KAAK,OAAQ,kBAAmB,WAAY,CAAE;AAAA,EACtD,GAAG,CAAC,CAAc;AAElB,SAAO,WAAW,KAAM,GAAI;AAC7B;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -93,18 +93,33 @@ function useWpMediaFrame(options) {
|
|
|
93
93
|
open
|
|
94
94
|
};
|
|
95
95
|
}
|
|
96
|
-
function createFrame({
|
|
96
|
+
function createFrame({
|
|
97
|
+
onSelect,
|
|
98
|
+
onSelectUrl,
|
|
99
|
+
allowUrlImport,
|
|
100
|
+
multiple,
|
|
101
|
+
mediaTypes,
|
|
102
|
+
selected,
|
|
103
|
+
title,
|
|
104
|
+
mode = "browse"
|
|
105
|
+
}) {
|
|
97
106
|
const frame = media_default()({
|
|
98
107
|
title,
|
|
99
108
|
multiple,
|
|
100
109
|
library: {
|
|
101
110
|
type: getMimeTypes(mediaTypes)
|
|
102
|
-
}
|
|
111
|
+
},
|
|
112
|
+
...allowUrlImport ? { frame: "post" } : {}
|
|
103
113
|
}).on("open", () => {
|
|
104
114
|
setTypeCaller(frame);
|
|
105
115
|
applyMode(frame, mode);
|
|
106
|
-
|
|
107
|
-
|
|
116
|
+
if (mode !== "url") {
|
|
117
|
+
applySelection(frame, selected);
|
|
118
|
+
}
|
|
119
|
+
}).on("close", () => cleanupFrame(frame)).on("insert select", () => select(frame, multiple, onSelect, onSelectUrl));
|
|
120
|
+
if (allowUrlImport) {
|
|
121
|
+
frame.on("ready open", () => restrictFrameMenu(frame));
|
|
122
|
+
}
|
|
108
123
|
handleExtensions(frame, mediaTypes);
|
|
109
124
|
return frame;
|
|
110
125
|
}
|
|
@@ -113,17 +128,41 @@ function cleanupFrame(frame) {
|
|
|
113
128
|
frame?.remove();
|
|
114
129
|
}
|
|
115
130
|
function applyMode(frame, mode = "browse") {
|
|
116
|
-
|
|
131
|
+
if (mode === "url") {
|
|
132
|
+
frame.setState("embed");
|
|
133
|
+
} else {
|
|
134
|
+
frame.content.mode(mode);
|
|
135
|
+
}
|
|
117
136
|
}
|
|
118
137
|
function applySelection(frame, selected) {
|
|
119
138
|
const selectedAttachments = (typeof selected === "number" ? [selected] : selected)?.filter((id) => !!id).map((id) => media_default().attachment(id));
|
|
120
139
|
frame.state().get("selection").set(selectedAttachments || []);
|
|
121
140
|
}
|
|
122
|
-
function select(frame, multiple, onSelect) {
|
|
123
|
-
const
|
|
141
|
+
function select(frame, multiple, onSelect, onSelectUrl) {
|
|
142
|
+
const state = frame.state();
|
|
143
|
+
if (state.get("id") === "embed") {
|
|
144
|
+
if (onSelectUrl) {
|
|
145
|
+
const url = state.props?.get("url");
|
|
146
|
+
const alt = state.props?.get("alt");
|
|
147
|
+
if (url) {
|
|
148
|
+
onSelectUrl(url, alt);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const attachments = state.get("selection").toJSON().map(normalize);
|
|
124
154
|
const onSelectFn = onSelect;
|
|
125
155
|
onSelectFn(multiple ? attachments : attachments[0]);
|
|
126
156
|
}
|
|
157
|
+
var FRAME_MENU_ITEMS_TO_REMOVE = [
|
|
158
|
+
"#menu-item-gallery",
|
|
159
|
+
"#menu-item-featured-image",
|
|
160
|
+
"#menu-item-playlist",
|
|
161
|
+
"#menu-item-video-playlist"
|
|
162
|
+
].join(",");
|
|
163
|
+
function restrictFrameMenu(frame) {
|
|
164
|
+
frame.$el?.find(FRAME_MENU_ITEMS_TO_REMOVE)?.remove();
|
|
165
|
+
}
|
|
127
166
|
function setTypeCaller(frame) {
|
|
128
167
|
frame.uploader.uploader.param("uploadTypeCaller", "elementor-wp-media-upload");
|
|
129
168
|
}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hooks/use-wp-media-attachment.ts","../src/errors.ts","../src/media.ts","../src/normalize.ts","../src/get-media-attachment.ts","../src/hooks/use-wp-media-frame.ts","../src/wp-plupload-settings.ts"],"sourcesContent":["import { useQuery } from '@elementor/query';\n\nimport { getMediaAttachment } from '../get-media-attachment';\n\nexport default function useWpMediaAttachment( id: number | null ) {\n\treturn useQuery( {\n\t\tqueryKey: [ 'wp-attachment', id ],\n\t\tqueryFn: () => getMediaAttachment( { id } ),\n\t\tenabled: !! id,\n\t} );\n}\n","import { createError } from '@elementor/utils';\n\nexport const WpMediaNotAvailableError = createError( {\n\tcode: 'wp_media_not_available',\n\tmessage: '`wp.media` is not available, make sure the `media-models` handle is set in the dependencies array',\n} );\n\nexport const WpPluploadSettingsNotAvailableError = createError( {\n\tcode: 'wp_plupload_settings_not_available',\n\tmessage: '`_wpPluploadSettings` is not available, make sure a wp media uploader is open',\n} );\n","import { WpMediaNotAvailableError } from './errors';\nimport { type WpMediaWindow } from './types/wp-media';\n\nconst wpMediaWindow = window as unknown as WpMediaWindow;\n\nexport default () => {\n\tif ( ! wpMediaWindow.wp?.media ) {\n\t\tthrow new WpMediaNotAvailableError();\n\t}\n\n\treturn wpMediaWindow.wp.media;\n};\n","import { type Attachment } from './types/attachment';\nimport { type WpAttachmentJSON } from './types/wp-media';\n\nexport default function normalize( attachment: WpAttachmentJSON ): Attachment {\n\tconst { filesizeInBytes, filesizeHumanReadable, author, authorName, ...rest } = attachment;\n\n\treturn {\n\t\t...rest,\n\t\tfilesize: {\n\t\t\tinBytes: filesizeInBytes,\n\t\t\thumanReadable: filesizeHumanReadable,\n\t\t},\n\t\tauthor: {\n\t\t\tid: parseInt( author ),\n\t\t\tname: authorName,\n\t\t},\n\t};\n}\n","import media from './media';\nimport normalize from './normalize';\n\nexport async function getMediaAttachment( { id }: { id: number | null } ) {\n\tif ( ! id ) {\n\t\treturn null;\n\t}\n\n\tconst model = media().attachment( id );\n\tconst wpAttachment = model.toJSON();\n\n\tconst isFetched = 'url' in wpAttachment;\n\n\tif ( isFetched ) {\n\t\treturn normalize( wpAttachment );\n\t}\n\n\ttry {\n\t\treturn normalize( await model.fetch() );\n\t} catch {\n\t\treturn null;\n\t}\n}\n","import { useEffect, useRef } from 'react';\n\nimport media from '../media';\nimport normalize from '../normalize';\nimport { type Attachment } from '../types/attachment';\nimport { type MediaFrame } from '../types/wp-media';\nimport wpPluploadSettings from '../wp-plupload-settings';\n\nexport type OpenOptions = {\n\tmode?: 'upload' | 'browse';\n};\n\nexport type MediaType = 'image' | 'svg' | 'video';\n\ntype Options = {\n\tmediaTypes: MediaType[];\n\ttitle?: string;\n} & (\n\t| {\n\t\t\tmultiple: true;\n\t\t\tselected: Array< number | null >;\n\t\t\tonSelect: ( val: Attachment[] ) => void;\n\t }\n\t| {\n\t\t\tmultiple: false;\n\t\t\tselected: number | null;\n\t\t\tonSelect: ( val: Attachment ) => void;\n\t }\n);\n\nexport default function useWpMediaFrame( options: Options ) {\n\tconst frame = useRef< MediaFrame >();\n\n\tconst open = ( openOptions: OpenOptions = {} ) => {\n\t\tcleanupFrame( frame.current );\n\n\t\tframe.current = createFrame( { ...options, ...openOptions } );\n\n\t\tframe.current?.open();\n\t};\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tcleanupFrame( frame.current );\n\t\t};\n\t}, [] );\n\n\treturn {\n\t\topen,\n\t};\n}\n\nfunction createFrame( { onSelect, multiple, mediaTypes, selected, title, mode = 'browse' }: Options & OpenOptions ) {\n\tconst frame: MediaFrame = media()( {\n\t\ttitle,\n\t\tmultiple,\n\t\tlibrary: {\n\t\t\ttype: getMimeTypes( mediaTypes ),\n\t\t},\n\t} )\n\t\t.on( 'open', () => {\n\t\t\tsetTypeCaller( frame );\n\t\t\tapplyMode( frame, mode );\n\t\t\tapplySelection( frame, selected );\n\t\t} )\n\t\t.on( 'close', () => cleanupFrame( frame ) )\n\t\t.on( 'insert select', () => select( frame, multiple, onSelect ) );\n\n\thandleExtensions( frame, mediaTypes );\n\n\treturn frame;\n}\n\nfunction cleanupFrame( frame?: MediaFrame ) {\n\tframe?.detach();\n\tframe?.remove();\n}\n\nfunction applyMode( frame: MediaFrame, mode: OpenOptions[ 'mode' ] = 'browse' ) {\n\tframe.content.mode( mode );\n}\n\nfunction applySelection( frame: MediaFrame, selected: number | null | Array< number | null > ) {\n\tconst selectedAttachments = ( typeof selected === 'number' ? [ selected ] : selected )\n\t\t?.filter( ( id ) => !! id )\n\t\t.map( ( id ) => media().attachment( id as number ) );\n\n\tframe\n\t\t.state()\n\t\t.get( 'selection' )\n\t\t.set( selectedAttachments || [] );\n}\n\nfunction select( frame: MediaFrame, multiple: boolean, onSelect: Options[ 'onSelect' ] ) {\n\tconst attachments = frame.state().get( 'selection' ).toJSON().map( normalize );\n\n\tconst onSelectFn = onSelect as ( val: Attachment | Attachment[] ) => void;\n\n\tonSelectFn( multiple ? attachments : attachments[ 0 ] );\n}\n\nfunction setTypeCaller( frame: MediaFrame ) {\n\tframe.uploader.uploader.param( 'uploadTypeCaller', 'elementor-wp-media-upload' );\n}\n\nfunction handleExtensions( frame: MediaFrame, mediaTypes: MediaType[] ) {\n\tconst defaultExtensions = wpPluploadSettings().defaults.filters.mime_types?.[ 0 ]?.extensions;\n\n\t// Set extensions by media types\n\tframe.on( 'ready', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = [ { extensions: getExtensions( mediaTypes ) } ];\n\t} );\n\n\t// Restore default upload extensions\n\tframe.on( 'close', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = defaultExtensions\n\t\t\t? [ { extensions: defaultExtensions } ]\n\t\t\t: [];\n\t} );\n}\n\nconst imageExtensions = [ 'avif', 'bmp', 'gif', 'ico', 'jpe', 'jpeg', 'jpg', 'png', 'webp' ];\nconst videoExtensions = [ 'mp4', 'webm', 'ogg', 'mov', 'm4v', 'avi', 'wmv', 'mpg', 'mpeg', '3gp', '3g2' ];\n\nfunction getMimeTypes( mediaTypes: MediaType[] ) {\n\tconst mimeTypesPerType: Record< MediaType, string[] > = {\n\t\timage: imageExtensions.map( ( extension ) => `image/${ extension }` ),\n\t\tsvg: [ 'image/svg+xml' ],\n\t\tvideo: [\n\t\t\t'video/mp4',\n\t\t\t'video/webm',\n\t\t\t'video/ogg',\n\t\t\t'video/quicktime',\n\t\t\t'video/x-m4v',\n\t\t\t'video/avi',\n\t\t\t'video/x-ms-wmv',\n\t\t\t'video/mpeg',\n\t\t\t'video/3gpp',\n\t\t\t'video/3gpp2',\n\t\t],\n\t};\n\n\treturn mediaTypes.reduce( ( prev, currentType ) => {\n\t\treturn prev.concat( mimeTypesPerType[ currentType ] );\n\t}, [] as string[] );\n}\n\nfunction getExtensions( mediaTypes: MediaType[] ) {\n\tconst extensionsPerType: Record< MediaType, string[] > = {\n\t\timage: imageExtensions,\n\t\tsvg: [ 'svg' ],\n\t\tvideo: videoExtensions,\n\t};\n\n\tconst extensions = mediaTypes.reduce( ( prev, currentType ) => {\n\t\treturn prev.concat( extensionsPerType[ currentType ] );\n\t}, [] as string[] );\n\n\treturn extensions.join( ',' );\n}\n","import { WpPluploadSettingsNotAvailableError } from './errors';\nimport { type WpPluploadSettingsWindow } from './types/plupload';\n\nconst wpPluploadSettingsWindow = window as unknown as WpPluploadSettingsWindow;\n\nexport default () => {\n\tif ( ! wpPluploadSettingsWindow._wpPluploadSettings ) {\n\t\tthrow new WpPluploadSettingsNotAvailableError();\n\t}\n\n\treturn wpPluploadSettingsWindow._wpPluploadSettings;\n};\n"],"mappings":";AAAA,SAAS,gBAAgB;;;ACAzB,SAAS,mBAAmB;AAErB,IAAM,2BAA2B,YAAa;AAAA,EACpD,MAAM;AAAA,EACN,SAAS;AACV,CAAE;AAEK,IAAM,sCAAsC,YAAa;AAAA,EAC/D,MAAM;AAAA,EACN,SAAS;AACV,CAAE;;;ACPF,IAAM,gBAAgB;AAEtB,IAAO,gBAAQ,MAAM;AACpB,MAAK,CAAE,cAAc,IAAI,OAAQ;AAChC,UAAM,IAAI,yBAAyB;AAAA,EACpC;AAEA,SAAO,cAAc,GAAG;AACzB;;;ACRe,SAAR,UAA4B,YAA2C;AAC7E,QAAM,EAAE,iBAAiB,uBAAuB,QAAQ,YAAY,GAAG,KAAK,IAAI;AAEhF,SAAO;AAAA,IACN,GAAG;AAAA,IACH,UAAU;AAAA,MACT,SAAS;AAAA,MACT,eAAe;AAAA,IAChB;AAAA,IACA,QAAQ;AAAA,MACP,IAAI,SAAU,MAAO;AAAA,MACrB,MAAM;AAAA,IACP;AAAA,EACD;AACD;;;ACdA,eAAsB,mBAAoB,EAAE,GAAG,GAA2B;AACzE,MAAK,CAAE,IAAK;AACX,WAAO;AAAA,EACR;AAEA,QAAM,QAAQ,cAAM,EAAE,WAAY,EAAG;AACrC,QAAM,eAAe,MAAM,OAAO;AAElC,QAAM,YAAY,SAAS;AAE3B,MAAK,WAAY;AAChB,WAAO,UAAW,YAAa;AAAA,EAChC;AAEA,MAAI;AACH,WAAO,UAAW,MAAM,MAAM,MAAM,CAAE;AAAA,EACvC,QAAQ;AACP,WAAO;AAAA,EACR;AACD;;;AJlBe,SAAR,qBAAuC,IAAoB;AACjE,SAAO,SAAU;AAAA,IAChB,UAAU,CAAE,iBAAiB,EAAG;AAAA,IAChC,SAAS,MAAM,mBAAoB,EA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O;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAEA,SAAO,WAAW,OAAQ,CAAE,MAAM,gBAAiB;AAClD,WAAO,KAAK,OAAQ,iBAAkB,WAAY,CAAE;AAAA,EACrD,GAAG,CAAC,CAAc;AACnB;AAEA,SAAS,cAAe,YAA0B;AACjD,QAAM,oBAAmD;AAAA,IACxD,OAAO;AAAA,IACP,KAAK,CAAE,KAAM;AAAA,IACb,OAAO;AAAA,EACR;AAEA,QAAM,aAAa,WAAW,OAAQ,CAAE,MAAM,gBAAiB;AAC9D,WAAO,KAAK,OAAQ,kBAAmB,WAAY,CAAE;AAAA,EACtD,GAAG,CAAC,CAAc;AAElB,SAAO,WAAW,KAAM,GAAI;AAC7B;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-wp-media-attachment.ts","../src/errors.ts","../src/media.ts","../src/normalize.ts","../src/get-media-attachment.ts","../src/hooks/use-wp-media-frame.ts","../src/wp-plupload-settings.ts"],"sourcesContent":["import { useQuery } from '@elementor/query';\n\nimport { getMediaAttachment } from '../get-media-attachment';\n\nexport default function useWpMediaAttachment( id: number | null ) {\n\treturn useQuery( {\n\t\tqueryKey: [ 'wp-attachment', id ],\n\t\tqueryFn: () => getMediaAttachment( { id } ),\n\t\tenabled: !! id,\n\t} );\n}\n","import { createError } from '@elementor/utils';\n\nexport const WpMediaNotAvailableError = createError( {\n\tcode: 'wp_media_not_available',\n\tmessage: '`wp.media` is not available, make sure the `media-models` handle is set in the dependencies array',\n} );\n\nexport const WpPluploadSettingsNotAvailableError = createError( {\n\tcode: 'wp_plupload_settings_not_available',\n\tmessage: '`_wpPluploadSettings` is not available, make sure a wp media uploader is open',\n} );\n","import { WpMediaNotAvailableError } from './errors';\nimport { type WpMediaWindow } from './types/wp-media';\n\nconst wpMediaWindow = window as unknown as WpMediaWindow;\n\nexport default () => {\n\tif ( ! wpMediaWindow.wp?.media ) {\n\t\tthrow new WpMediaNotAvailableError();\n\t}\n\n\treturn wpMediaWindow.wp.media;\n};\n","import { type Attachment } from './types/attachment';\nimport { type WpAttachmentJSON } from './types/wp-media';\n\nexport default function normalize( attachment: WpAttachmentJSON ): Attachment {\n\tconst { filesizeInBytes, filesizeHumanReadable, author, authorName, ...rest } = attachment;\n\n\treturn {\n\t\t...rest,\n\t\tfilesize: {\n\t\t\tinBytes: filesizeInBytes,\n\t\t\thumanReadable: filesizeHumanReadable,\n\t\t},\n\t\tauthor: {\n\t\t\tid: parseInt( author ),\n\t\t\tname: authorName,\n\t\t},\n\t};\n}\n","import media from './media';\nimport normalize from './normalize';\n\nexport async function getMediaAttachment( { id }: { id: number | null } ) {\n\tif ( ! id ) {\n\t\treturn null;\n\t}\n\n\tconst model = media().attachment( id );\n\tconst wpAttachment = model.toJSON();\n\n\tconst isFetched = 'url' in wpAttachment;\n\n\tif ( isFetched ) {\n\t\treturn normalize( wpAttachment );\n\t}\n\n\ttry {\n\t\treturn normalize( await model.fetch() );\n\t} catch {\n\t\treturn null;\n\t}\n}\n","import { useEffect, useRef } from 'react';\n\nimport media from '../media';\nimport normalize from '../normalize';\nimport { type Attachment } from '../types/attachment';\nimport { type MediaFrame } from '../types/wp-media';\nimport wpPluploadSettings from '../wp-plupload-settings';\n\nexport type OpenOptions = {\n\tmode?: 'upload' | 'browse' | 'url';\n};\n\nexport type MediaType = 'image' | 'svg' | 'video';\n\ntype Options = {\n\tmediaTypes: MediaType[];\n\ttitle?: string;\n\tallowUrlImport?: boolean;\n\tonSelectUrl?: ( url: string, alt?: string ) => void;\n} & (\n\t| {\n\t\t\tmultiple: true;\n\t\t\tselected: Array< number | null >;\n\t\t\tonSelect: ( val: Attachment[] ) => void;\n\t }\n\t| {\n\t\t\tmultiple: false;\n\t\t\tselected: number | null;\n\t\t\tonSelect: ( val: Attachment ) => void;\n\t }\n);\n\nexport default function useWpMediaFrame( options: Options ) {\n\tconst frame = useRef< MediaFrame >();\n\n\tconst open = ( openOptions: OpenOptions = {} ) => {\n\t\tcleanupFrame( frame.current );\n\n\t\tframe.current = createFrame( { ...options, ...openOptions } );\n\n\t\tframe.current?.open();\n\t};\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tcleanupFrame( frame.current );\n\t\t};\n\t}, [] );\n\n\treturn {\n\t\topen,\n\t};\n}\n\nfunction createFrame( {\n\tonSelect,\n\tonSelectUrl,\n\tallowUrlImport,\n\tmultiple,\n\tmediaTypes,\n\tselected,\n\ttitle,\n\tmode = 'browse',\n}: Options & OpenOptions ) {\n\tconst frame: MediaFrame = media()( {\n\t\ttitle,\n\t\tmultiple,\n\t\tlibrary: {\n\t\t\ttype: getMimeTypes( mediaTypes ),\n\t\t},\n\t\t...( allowUrlImport ? { frame: 'post' } : {} ),\n\t} )\n\t\t.on( 'open', () => {\n\t\t\tsetTypeCaller( frame );\n\t\t\tapplyMode( frame, mode );\n\t\t\tif ( mode !== 'url' ) {\n\t\t\t\tapplySelection( frame, selected );\n\t\t\t}\n\t\t} )\n\t\t.on( 'close', () => cleanupFrame( frame ) )\n\t\t.on( 'insert select', () => select( frame, multiple, onSelect, onSelectUrl ) );\n\n\tif ( allowUrlImport ) {\n\t\tframe.on( 'ready open', () => restrictFrameMenu( frame ) );\n\t}\n\n\thandleExtensions( frame, mediaTypes );\n\n\treturn frame;\n}\n\nfunction cleanupFrame( frame?: MediaFrame ) {\n\tframe?.detach();\n\tframe?.remove();\n}\n\nfunction applyMode( frame: MediaFrame, mode: OpenOptions[ 'mode' ] = 'browse' ) {\n\tif ( mode === 'url' ) {\n\t\tframe.setState( 'embed' );\n\t} else {\n\t\tframe.content.mode( mode );\n\t}\n}\n\nfunction applySelection( frame: MediaFrame, selected: number | null | Array< number | null > ) {\n\tconst selectedAttachments = ( typeof selected === 'number' ? [ selected ] : selected )\n\t\t?.filter( ( id ) => !! id )\n\t\t.map( ( id ) => media().attachment( id as number ) );\n\n\tframe\n\t\t.state()\n\t\t.get( 'selection' )\n\t\t.set( selectedAttachments || [] );\n}\n\nfunction select(\n\tframe: MediaFrame,\n\tmultiple: boolean,\n\tonSelect: Options[ 'onSelect' ],\n\tonSelectUrl?: Options[ 'onSelectUrl' ]\n) {\n\tconst state = frame.state();\n\n\tif ( state.get( 'id' ) === 'embed' ) {\n\t\tif ( onSelectUrl ) {\n\t\t\tconst url = state.props?.get( 'url' );\n\t\t\tconst alt = state.props?.get( 'alt' );\n\t\t\tif ( url ) {\n\t\t\t\tonSelectUrl( url, alt );\n\t\t\t}\n\t\t}\n\t\treturn;\n\t}\n\n\tconst attachments = state.get( 'selection' ).toJSON().map( normalize );\n\n\tconst onSelectFn = onSelect as ( val: Attachment | Attachment[] ) => void;\n\n\tonSelectFn( multiple ? attachments : attachments[ 0 ] );\n}\n\nconst FRAME_MENU_ITEMS_TO_REMOVE = [\n\t'#menu-item-gallery',\n\t'#menu-item-featured-image',\n\t'#menu-item-playlist',\n\t'#menu-item-video-playlist',\n].join( ',' );\n\nfunction restrictFrameMenu( frame: MediaFrame ) {\n\tframe.$el?.find( FRAME_MENU_ITEMS_TO_REMOVE )?.remove();\n}\n\nfunction setTypeCaller( frame: MediaFrame ) {\n\tframe.uploader.uploader.param( 'uploadTypeCaller', 'elementor-wp-media-upload' );\n}\n\nfunction handleExtensions( frame: MediaFrame, mediaTypes: MediaType[] ) {\n\tconst defaultExtensions = wpPluploadSettings().defaults.filters.mime_types?.[ 0 ]?.extensions;\n\n\t// Set extensions by media types\n\tframe.on( 'ready', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = [ { extensions: getExtensions( mediaTypes ) } ];\n\t} );\n\n\t// Restore default upload extensions\n\tframe.on( 'close', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = defaultExtensions\n\t\t\t? [ { extensions: defaultExtensions } ]\n\t\t\t: [];\n\t} );\n}\n\nconst imageExtensions = [ 'avif', 'bmp', 'gif', 'ico', 'jpe', 'jpeg', 'jpg', 'png', 'webp' ];\nconst videoExtensions = [ 'mp4', 'webm', 'ogg', 'mov', 'm4v', 'avi', 'wmv', 'mpg', 'mpeg', '3gp', '3g2' ];\n\nfunction getMimeTypes( mediaTypes: MediaType[] ) {\n\tconst mimeTypesPerType: Record< MediaType, string[] > = {\n\t\timage: imageExtensions.map( ( extension ) => `image/${ extension }` ),\n\t\tsvg: [ 'image/svg+xml' ],\n\t\tvideo: [\n\t\t\t'video/mp4',\n\t\t\t'video/webm',\n\t\t\t'video/ogg',\n\t\t\t'video/quicktime',\n\t\t\t'video/x-m4v',\n\t\t\t'video/avi',\n\t\t\t'video/x-ms-wmv',\n\t\t\t'video/mpeg',\n\t\t\t'video/3gpp',\n\t\t\t'video/3gpp2',\n\t\t],\n\t};\n\n\treturn mediaTypes.reduce( ( prev, currentType ) => {\n\t\treturn prev.concat( mimeTypesPerType[ currentType ] );\n\t}, [] as string[] );\n}\n\nfunction getExtensions( mediaTypes: MediaType[] ) {\n\tconst extensionsPerType: Record< MediaType, string[] > = {\n\t\timage: imageExtensions,\n\t\tsvg: [ 'svg' ],\n\t\tvideo: videoExtensions,\n\t};\n\n\tconst extensions = mediaTypes.reduce( ( prev, currentType ) => {\n\t\treturn prev.concat( extensionsPerType[ currentType ] );\n\t}, [] as string[] );\n\n\treturn extensions.join( ',' );\n}\n","import { WpPluploadSettingsNotAvailableError } from './errors';\nimport { type WpPluploadSettingsWindow } from './types/plupload';\n\nconst wpPluploadSettingsWindow = window as unknown as WpPluploadSettingsWindow;\n\nexport default () => {\n\tif ( ! wpPluploadSettingsWindow._wpPluploadSettings ) {\n\t\tthrow new WpPluploadSettingsNotAvailableError();\n\t}\n\n\treturn wpPluploadSettingsWindow._wpPluploadSettings;\n};\n"],"mappings":";AAAA,SAAS,gBAAgB;;;ACAzB,SAAS,mBAAmB;AAErB,IAAM,2BAA2B,YAAa;AAAA,EACpD,MAAM;AAAA,EACN,SAAS;AACV,CAAE;AAEK,IAAM,sCAAsC,YA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AAmB,EAAE,SAAS,QAAQ,aAAa,oBAChD,CAAE,EAAE,YAAY,kBAAkB,CAAE,IACpC,CAAC;AAAA,EACL,CAAE;AACH;AAEA,IAAM,kBAAkB,CAAE,QAAQ,OAAO,OAAO,OAAO,OAAO,QAAQ,OAAO,OAAO,MAAO;AAC3F,IAAM,kBAAkB,CAAE,OAAO,QAAQ,OAAO,OAAO,OAAO,OAAO,OAAO,OAAO,QAAQ,OAAO,KAAM;AAExG,SAAS,aAAc,YAA0B;AAChD,QAAM,mBAAkD;AAAA,IACvD,OAAO,gBAAgB,IAAK,CAAE,cAAe,SAAU,SAAU,EAAG;AAAA,IACpE,KAAK,CAAE,eAAgB;AAAA,IACvB,OAAO;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAEA,SAAO,WAAW,OAAQ,CAAE,MAAM,gBAAiB;AAClD,WAAO,KAAK,OAAQ,iBAAkB,WAAY,CAAE;AAAA,EACrD,GAAG,CAAC,CAAc;AACnB;AAEA,SAAS,cAAe,YAA0B;AACjD,QAAM,oBAAmD;AAAA,IACxD,OAAO;AAAA,IACP,KAAK,CAAE,KAAM;AAAA,IACb,OAAO;AAAA,EACR;AAEA,QAAM,aAAa,WAAW,OAAQ,CAAE,MAAM,gBAAiB;AAC9D,WAAO,KAAK,OAAQ,kBAAmB,WAAY,CAAE;AAAA,EACtD,GAAG,CAAC,CAAc;AAElB,SAAO,WAAW,KAAM,GAAI;AAC7B;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/wp-media",
|
|
3
3
|
"description": "An adapter for WordPress' media utils",
|
|
4
|
-
"version": "4.1.0-
|
|
4
|
+
"version": "4.1.0-799",
|
|
5
5
|
"private": false,
|
|
6
6
|
"author": "Elementor Team",
|
|
7
7
|
"homepage": "https://elementor.com/",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"dev": "tsup --config=../../tsup.dev.ts"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@elementor/query": "4.1.0-
|
|
44
|
-
"@elementor/utils": "4.1.0-
|
|
43
|
+
"@elementor/query": "4.1.0-799",
|
|
44
|
+
"@elementor/utils": "4.1.0-799"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"react": "^18.3.1"
|
|
@@ -7,7 +7,7 @@ import { type MediaFrame } from '../types/wp-media';
|
|
|
7
7
|
import wpPluploadSettings from '../wp-plupload-settings';
|
|
8
8
|
|
|
9
9
|
export type OpenOptions = {
|
|
10
|
-
mode?: 'upload' | 'browse';
|
|
10
|
+
mode?: 'upload' | 'browse' | 'url';
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
export type MediaType = 'image' | 'svg' | 'video';
|
|
@@ -15,6 +15,8 @@ export type MediaType = 'image' | 'svg' | 'video';
|
|
|
15
15
|
type Options = {
|
|
16
16
|
mediaTypes: MediaType[];
|
|
17
17
|
title?: string;
|
|
18
|
+
allowUrlImport?: boolean;
|
|
19
|
+
onSelectUrl?: ( url: string, alt?: string ) => void;
|
|
18
20
|
} & (
|
|
19
21
|
| {
|
|
20
22
|
multiple: true;
|
|
@@ -50,21 +52,37 @@ export default function useWpMediaFrame( options: Options ) {
|
|
|
50
52
|
};
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
function createFrame( {
|
|
55
|
+
function createFrame( {
|
|
56
|
+
onSelect,
|
|
57
|
+
onSelectUrl,
|
|
58
|
+
allowUrlImport,
|
|
59
|
+
multiple,
|
|
60
|
+
mediaTypes,
|
|
61
|
+
selected,
|
|
62
|
+
title,
|
|
63
|
+
mode = 'browse',
|
|
64
|
+
}: Options & OpenOptions ) {
|
|
54
65
|
const frame: MediaFrame = media()( {
|
|
55
66
|
title,
|
|
56
67
|
multiple,
|
|
57
68
|
library: {
|
|
58
69
|
type: getMimeTypes( mediaTypes ),
|
|
59
70
|
},
|
|
71
|
+
...( allowUrlImport ? { frame: 'post' } : {} ),
|
|
60
72
|
} )
|
|
61
73
|
.on( 'open', () => {
|
|
62
74
|
setTypeCaller( frame );
|
|
63
75
|
applyMode( frame, mode );
|
|
64
|
-
|
|
76
|
+
if ( mode !== 'url' ) {
|
|
77
|
+
applySelection( frame, selected );
|
|
78
|
+
}
|
|
65
79
|
} )
|
|
66
80
|
.on( 'close', () => cleanupFrame( frame ) )
|
|
67
|
-
.on( 'insert select', () => select( frame, multiple, onSelect ) );
|
|
81
|
+
.on( 'insert select', () => select( frame, multiple, onSelect, onSelectUrl ) );
|
|
82
|
+
|
|
83
|
+
if ( allowUrlImport ) {
|
|
84
|
+
frame.on( 'ready open', () => restrictFrameMenu( frame ) );
|
|
85
|
+
}
|
|
68
86
|
|
|
69
87
|
handleExtensions( frame, mediaTypes );
|
|
70
88
|
|
|
@@ -77,7 +95,11 @@ function cleanupFrame( frame?: MediaFrame ) {
|
|
|
77
95
|
}
|
|
78
96
|
|
|
79
97
|
function applyMode( frame: MediaFrame, mode: OpenOptions[ 'mode' ] = 'browse' ) {
|
|
80
|
-
|
|
98
|
+
if ( mode === 'url' ) {
|
|
99
|
+
frame.setState( 'embed' );
|
|
100
|
+
} else {
|
|
101
|
+
frame.content.mode( mode );
|
|
102
|
+
}
|
|
81
103
|
}
|
|
82
104
|
|
|
83
105
|
function applySelection( frame: MediaFrame, selected: number | null | Array< number | null > ) {
|
|
@@ -91,14 +113,43 @@ function applySelection( frame: MediaFrame, selected: number | null | Array< num
|
|
|
91
113
|
.set( selectedAttachments || [] );
|
|
92
114
|
}
|
|
93
115
|
|
|
94
|
-
function select(
|
|
95
|
-
|
|
116
|
+
function select(
|
|
117
|
+
frame: MediaFrame,
|
|
118
|
+
multiple: boolean,
|
|
119
|
+
onSelect: Options[ 'onSelect' ],
|
|
120
|
+
onSelectUrl?: Options[ 'onSelectUrl' ]
|
|
121
|
+
) {
|
|
122
|
+
const state = frame.state();
|
|
123
|
+
|
|
124
|
+
if ( state.get( 'id' ) === 'embed' ) {
|
|
125
|
+
if ( onSelectUrl ) {
|
|
126
|
+
const url = state.props?.get( 'url' );
|
|
127
|
+
const alt = state.props?.get( 'alt' );
|
|
128
|
+
if ( url ) {
|
|
129
|
+
onSelectUrl( url, alt );
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const attachments = state.get( 'selection' ).toJSON().map( normalize );
|
|
96
136
|
|
|
97
137
|
const onSelectFn = onSelect as ( val: Attachment | Attachment[] ) => void;
|
|
98
138
|
|
|
99
139
|
onSelectFn( multiple ? attachments : attachments[ 0 ] );
|
|
100
140
|
}
|
|
101
141
|
|
|
142
|
+
const FRAME_MENU_ITEMS_TO_REMOVE = [
|
|
143
|
+
'#menu-item-gallery',
|
|
144
|
+
'#menu-item-featured-image',
|
|
145
|
+
'#menu-item-playlist',
|
|
146
|
+
'#menu-item-video-playlist',
|
|
147
|
+
].join( ',' );
|
|
148
|
+
|
|
149
|
+
function restrictFrameMenu( frame: MediaFrame ) {
|
|
150
|
+
frame.$el?.find( FRAME_MENU_ITEMS_TO_REMOVE )?.remove();
|
|
151
|
+
}
|
|
152
|
+
|
|
102
153
|
function setTypeCaller( frame: MediaFrame ) {
|
|
103
154
|
frame.uploader.uploader.param( 'uploadTypeCaller', 'elementor-wp-media-upload' );
|
|
104
155
|
}
|
package/src/types/wp-media.ts
CHANGED
|
@@ -10,6 +10,7 @@ export type BackboneAttachmentModel = {
|
|
|
10
10
|
export type CreateMediaFrameOptions = {
|
|
11
11
|
title?: string;
|
|
12
12
|
multiple?: boolean;
|
|
13
|
+
frame?: string;
|
|
13
14
|
library?: {
|
|
14
15
|
type?: string[] | string;
|
|
15
16
|
};
|
|
@@ -26,12 +27,18 @@ export type MediaFrame = {
|
|
|
26
27
|
param: ( key: string, value: string ) => void;
|
|
27
28
|
};
|
|
28
29
|
};
|
|
30
|
+
$el?: {
|
|
31
|
+
find: ( selector: string ) => { remove: () => void };
|
|
32
|
+
};
|
|
29
33
|
state: () => {
|
|
30
|
-
get: ( key: 'selection' ) => {
|
|
34
|
+
get: ( ( key: 'selection' ) => {
|
|
31
35
|
set: ( attachments: BackboneAttachmentModel[] ) => void;
|
|
32
36
|
toJSON: () => WpAttachmentJSON[];
|
|
33
|
-
}
|
|
37
|
+
} ) &
|
|
38
|
+
( ( key: 'id' ) => string );
|
|
39
|
+
props?: { get: ( key: string ) => string | undefined };
|
|
34
40
|
};
|
|
41
|
+
setState: ( id: string ) => MediaFrame;
|
|
35
42
|
on: ( event: string, callback: () => void ) => MediaFrame;
|
|
36
43
|
open: () => void;
|
|
37
44
|
detach: () => void;
|