@elementor/wp-media 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.4.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [9ca4eab]
8
+ - @elementor/utils@0.3.1
9
+
10
+ ## 0.4.0
11
+
12
+ ### Minor Changes
13
+
14
+ - af5fa42: Separate `getMediaAttachment` from react hook.
15
+
16
+ ### Patch Changes
17
+
18
+ - a2f5096: support restricting uploaded image type
19
+
3
20
  ## 0.3.0
4
21
 
5
22
  ### Minor Changes
package/dist/index.d.mts CHANGED
@@ -25,6 +25,8 @@ type Attachment = {
25
25
  }>;
26
26
  };
27
27
 
28
+ type ImageExtension = 'avif' | 'bmp' | 'gif' | 'ico' | 'jpe' | 'jpeg' | 'jpg' | 'png' | 'svg' | 'webp';
29
+
28
30
  declare function useWpMediaAttachment(id: number | null): _tanstack_react_query.UseQueryResult<Attachment | null, Error>;
29
31
 
30
32
  type OpenOptions = {
@@ -32,6 +34,7 @@ type OpenOptions = {
32
34
  };
33
35
  type Options = {
34
36
  types: Array<'image' | 'image/svg+xml'>;
37
+ allowedExtensions?: ImageExtension[];
35
38
  title?: string;
36
39
  } & ({
37
40
  multiple: true;
@@ -46,4 +49,8 @@ declare function useWpMediaFrame(options: Options): {
46
49
  open: (openOptions?: OpenOptions) => void;
47
50
  };
48
51
 
49
- export { type Attachment, useWpMediaAttachment, useWpMediaFrame };
52
+ declare function getMediaAttachment({ id }: {
53
+ id: number | null;
54
+ }): Promise<Attachment | null>;
55
+
56
+ export { type Attachment, type ImageExtension, getMediaAttachment, useWpMediaAttachment, useWpMediaFrame };
package/dist/index.d.ts CHANGED
@@ -25,6 +25,8 @@ type Attachment = {
25
25
  }>;
26
26
  };
27
27
 
28
+ type ImageExtension = 'avif' | 'bmp' | 'gif' | 'ico' | 'jpe' | 'jpeg' | 'jpg' | 'png' | 'svg' | 'webp';
29
+
28
30
  declare function useWpMediaAttachment(id: number | null): _tanstack_react_query.UseQueryResult<Attachment | null, Error>;
29
31
 
30
32
  type OpenOptions = {
@@ -32,6 +34,7 @@ type OpenOptions = {
32
34
  };
33
35
  type Options = {
34
36
  types: Array<'image' | 'image/svg+xml'>;
37
+ allowedExtensions?: ImageExtension[];
35
38
  title?: string;
36
39
  } & ({
37
40
  multiple: true;
@@ -46,4 +49,8 @@ declare function useWpMediaFrame(options: Options): {
46
49
  open: (openOptions?: OpenOptions) => void;
47
50
  };
48
51
 
49
- export { type Attachment, useWpMediaAttachment, useWpMediaFrame };
52
+ declare function getMediaAttachment({ id }: {
53
+ id: number | null;
54
+ }): Promise<Attachment | null>;
55
+
56
+ export { type Attachment, type ImageExtension, getMediaAttachment, useWpMediaAttachment, useWpMediaFrame };
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ getMediaAttachment: () => getMediaAttachment,
23
24
  useWpMediaAttachment: () => useWpMediaAttachment,
24
25
  useWpMediaFrame: () => useWpMediaFrame
25
26
  });
@@ -64,15 +65,8 @@ function normalize(attachment) {
64
65
  };
65
66
  }
66
67
 
67
- // src/hooks/use-wp-media-attachment.ts
68
- function useWpMediaAttachment(id) {
69
- return (0, import_query.useQuery)({
70
- queryKey: ["wp-attachment", id],
71
- queryFn: getAttachment,
72
- enabled: !!id
73
- });
74
- }
75
- async function getAttachment({ queryKey: [, id] }) {
68
+ // src/get-media-attachment.ts
69
+ async function getMediaAttachment({ id }) {
76
70
  if (!id) {
77
71
  return null;
78
72
  }
@@ -89,6 +83,15 @@ async function getAttachment({ queryKey: [, id] }) {
89
83
  }
90
84
  }
91
85
 
86
+ // src/hooks/use-wp-media-attachment.ts
87
+ function useWpMediaAttachment(id) {
88
+ return (0, import_query.useQuery)({
89
+ queryKey: ["wp-attachment", id],
90
+ queryFn: () => getMediaAttachment({ id }),
91
+ enabled: !!id
92
+ });
93
+ }
94
+
92
95
  // src/hooks/use-wp-media-frame.ts
93
96
  var import_react = require("react");
94
97
 
@@ -102,6 +105,7 @@ var wp_plupload_settings_default = () => {
102
105
  };
103
106
 
104
107
  // src/hooks/use-wp-media-frame.ts
108
+ var defaultImageExtensions = ["avif", "bmp", "gif", "ico", "jpe", "jpeg", "jpg", "png", "webp"];
105
109
  function useWpMediaFrame(options) {
106
110
  const frame = (0, import_react.useRef)();
107
111
  const open = (openOptions = {}) => {
@@ -118,7 +122,15 @@ function useWpMediaFrame(options) {
118
122
  open
119
123
  };
120
124
  }
121
- function createFrame({ onSelect, multiple, types, selected, title, mode = "browse" }) {
125
+ function createFrame({
126
+ onSelect,
127
+ multiple,
128
+ types,
129
+ selected,
130
+ title,
131
+ mode = "browse",
132
+ allowedExtensions
133
+ }) {
122
134
  const frame = media_default()({
123
135
  title,
124
136
  multiple,
@@ -130,7 +142,7 @@ function createFrame({ onSelect, multiple, types, selected, title, mode = "brows
130
142
  applyMode(frame, mode);
131
143
  applySelection(frame, selected);
132
144
  }).on("close", () => cleanupFrame(frame)).on("insert select", () => select(frame, multiple, onSelect));
133
- handleSvgUpload(frame);
145
+ handleAllowedExtensions(frame, allowedExtensions);
134
146
  return frame;
135
147
  }
136
148
  function cleanupFrame(frame) {
@@ -152,30 +164,33 @@ function select(frame, multiple, onSelect) {
152
164
  function setTypeCaller(frame) {
153
165
  frame.uploader.uploader.param("uploadTypeCaller", "elementor-wp-media-upload");
154
166
  }
155
- function handleSvgUpload(frame) {
156
- if (isSvgUploadEnabled()) {
157
- enableSvgUpload(frame);
158
- }
159
- }
160
- function isSvgUploadEnabled() {
161
- const extendedWindow = window;
162
- return Boolean(extendedWindow.elementorCommon.config.filesUpload.unfilteredFiles);
163
- }
164
- function enableSvgUpload(frame) {
165
- const allowedMimeTypes = wp_plupload_settings_default().defaults.filters.mime_types?.[0];
166
- if (!allowedMimeTypes) {
167
- return;
168
- }
169
- const allowedExtensions = allowedMimeTypes.extensions;
167
+ function handleAllowedExtensions(frame, allowedExtensions) {
168
+ const defaultExtensions = wp_plupload_settings_default().defaults.filters.mime_types?.[0]?.extensions;
170
169
  frame.on("ready", () => {
171
- allowedMimeTypes.extensions = allowedExtensions + ",svg";
170
+ wp_plupload_settings_default().defaults.filters.mime_types = [
171
+ { extensions: getAllowedExtensions(allowedExtensions) }
172
+ ];
172
173
  });
173
174
  frame.on("close", () => {
174
- wp_plupload_settings_default().defaults.filters.mime_types = [{ extensions: allowedExtensions }];
175
+ wp_plupload_settings_default().defaults.filters.mime_types = defaultExtensions ? [{ extensions: defaultExtensions }] : [];
175
176
  });
176
177
  }
178
+ function getAllowedExtensions(allowedExtensions) {
179
+ let extensions = [...defaultImageExtensions];
180
+ if (allowedExtensions) {
181
+ extensions = allowedExtensions;
182
+ } else if (isUnfilteredFilesUploadEnabled()) {
183
+ extensions.push("svg");
184
+ }
185
+ return extensions.join(",");
186
+ }
187
+ function isUnfilteredFilesUploadEnabled() {
188
+ const extendedWindow = window;
189
+ return Boolean(extendedWindow.elementorCommon.config.filesUpload.unfilteredFiles);
190
+ }
177
191
  // Annotate the CommonJS export names for ESM import in node:
178
192
  0 && (module.exports = {
193
+ getMediaAttachment,
179
194
  useWpMediaAttachment,
180
195
  useWpMediaFrame
181
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/hooks/use-wp-media-frame.ts","../src/wp-plupload-settings.ts"],"sourcesContent":["export type { Attachment } from './types/attachment';\n\nexport { default as useWpMediaAttachment } from './hooks/use-wp-media-attachment';\nexport { default as useWpMediaFrame } from './hooks/use-wp-media-frame';\n","import { useQuery } from '@elementor/query';\n\nimport media from '../media';\nimport normalize from '../normalize';\n\nexport default function useWpMediaAttachment( id: number | null ) {\n\treturn useQuery( {\n\t\tqueryKey: [ 'wp-attachment', id ],\n\t\tqueryFn: getAttachment,\n\t\tenabled: !! id,\n\t} );\n}\n\nasync function getAttachment( { queryKey: [ , id ] }: { queryKey: [ string, 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 { 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 { useEffect, useRef } from 'react';\n\nimport media from '../media';\nimport normalize from '../normalize';\nimport { type Attachment } from '../types/attachment';\nimport { type ElementorCommonWindow } from '../types/elementor-common';\nimport { type MediaFrame } from '../types/wp-media';\nimport wpPluploadSettings from '../wp-plupload-settings';\n\ntype OpenOptions = {\n\tmode?: 'upload' | 'browse';\n};\n\ntype Options = {\n\ttypes: Array< 'image' | 'image/svg+xml' >;\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, types, selected, title, mode = 'browse' }: Options & OpenOptions ) {\n\tconst frame: MediaFrame = media()( {\n\t\ttitle,\n\t\tmultiple,\n\t\tlibrary: {\n\t\t\ttype: types,\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\thandleSvgUpload( frame );\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 handleSvgUpload( frame: MediaFrame ) {\n\tif ( isSvgUploadEnabled() ) {\n\t\tenableSvgUpload( frame );\n\t}\n}\n\nfunction isSvgUploadEnabled(): boolean {\n\tconst extendedWindow = window as unknown as ElementorCommonWindow;\n\n\treturn Boolean( extendedWindow.elementorCommon.config.filesUpload.unfilteredFiles );\n}\n\nfunction enableSvgUpload( frame: MediaFrame ) {\n\tconst allowedMimeTypes = wpPluploadSettings().defaults.filters.mime_types?.[ 0 ];\n\tif ( ! allowedMimeTypes ) {\n\t\treturn;\n\t}\n\n\tconst allowedExtensions = allowedMimeTypes.extensions;\n\t// Add svg to the allowed extensions\n\tframe.on( 'ready', () => {\n\t\tallowedMimeTypes.extensions = allowedExtensions + ',svg';\n\t} );\n\n\t// Restore allowed upload extensions\n\tframe.on( 'close', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = [ { extensions: allowedExtensions } ];\n\t} );\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 wpPluploadSettingsW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E,OAAQ,CAAC,CAAE,EAAG,EACzB,IAAK,CAAE,OAAQ,cAAM,EAAE,WAAY,EAAa,CAAE;AAEpD,QACE,MAAM,EACN,IAAK,WAAY,EACjB,IAAK,uBAAuB,CAAC,CAAE;AAClC;AAEA,SAAS,OAAQ,OAAmB,UAAmB,UAAkC;AACxF,QAAM,cAAc,MAAM,MAAM,EAAE,IAAK,WAAY,EAAE,OAAO,EAAE,IAAK,SAAU;AAE7E,QAAM,aAAa;AAEnB,aAAY,WAAW,cAAc,YAAa,CAAE,CAAE;AACvD;AAEA,SAAS,cAAe,OAAoB;AAC3C,QAAM,SAAS,SAAS,MAAO,oBAAoB,2BAA4B;AAChF;AAEA,SAAS,gBAAiB,OAAoB;AAC7C,MAAK,mBAAmB,GAAI;AAC3B,oBAAiB,KAAM;AAAA,EACxB;AACD;AAEA,SAAS,qBAA8B;AACtC,QAAM,iBAAiB;AAEvB,SAAO,QAAS,eAAe,gBAAgB,OAAO,YAAY,eAAgB;AACnF;AAEA,SAAS,gBAAiB,OAAoB;AAC7C,QAAM,mBAAmB,6BAAmB,EAAE,SAAS,QAAQ,aAAc,CAAE;AAC/E,MAAK,CAAE,kBAAmB;AACzB;AAAA,EACD;AAEA,QAAM,oBAAoB,iBAAiB;AAE3C,QAAM,GAAI,SAAS,MAAM;AACxB,qBAAiB,aAAa,oBAAoB;AAAA,EACnD,CAAE;AAGF,QAAM,GAAI,SAAS,MAAM;AACxB,iCAAmB,EAAE,SAAS,QAAQ,aAAa,CAAE,EAAE,YAAY,kBAAkB,CAAE;AAAA,EACxF,CAAE;AACH;","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 { ImageExtension } from './types/image';\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 ElementorCommonWindow } from '../types/elementor-common';\nimport { type ImageExtension } from '../types/image';\nimport { type MediaFrame } from '../types/wp-media';\nimport wpPluploadSettings from '../wp-plupload-settings';\n\ntype OpenOptions = {\n\tmode?: 'upload' | 'browse';\n};\n\ntype Options = {\n\ttypes: Array< 'image' | 'image/svg+xml' >;\n\tallowedExtensions?: ImageExtension[];\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\nconst defaultImageExtensions: ImageExtension[] = [ 'avif', 'bmp', 'gif', 'ico', 'jpe', 'jpeg', 'jpg', 'png', 'webp' ];\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\tmultiple,\n\ttypes,\n\tselected,\n\ttitle,\n\tmode = 'browse',\n\tallowedExtensions,\n}: Options & OpenOptions ) {\n\tconst frame: MediaFrame = media()( {\n\t\ttitle,\n\t\tmultiple,\n\t\tlibrary: {\n\t\t\ttype: types,\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\thandleAllowedExtensions( frame, allowedExtensions );\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 handleAllowedExtensions( frame: MediaFrame, allowedExtensions?: ImageExtension[] ) {\n\tconst defaultExtensions = wpPluploadSettings().defaults.filters.mime_types?.[ 0 ]?.extensions;\n\n\t// Set the allowed extensions\n\tframe.on( 'ready', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = [\n\t\t\t{ extensions: getAllowedExtensions( allowedExtensions ) },\n\t\t];\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\nfunction getAllowedExtensions( allowedExtensions?: ImageExtension[] ) {\n\tlet extensions: ImageExtension[] = [ ...defaultImageExtensions ];\n\n\tif ( allowedExtensions ) {\n\t\textensions = allowedExtensions;\n\t} else if ( isUnfilteredFilesUploadEnabled() ) {\n\t\textensions.push( 'svg' );\n\t}\n\n\treturn extensions.join( ',' );\n}\n\nfunction isUnfilteredFilesUploadEnabled(): boolean {\n\tconst extendedWindow = window as unknown as ElementorCommonWindow;\n\n\treturn Boolean( extendedWindow.elementorCommon.config.filesUpload.unfilteredFiles );\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,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;AAA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MAAM;AACxB,iCAAmB,EAAE,SAAS,QAAQ,aAAa;AAAA,MAClD,EAAE,YAAY,qBAAsB,iBAAkB,EAAE;AAAA,IACzD;AAAA,EACD,CAAE;AAGF,QAAM,GAAI,SAAS,MAAM;AACxB,iCAAmB,EAAE,SAAS,QAAQ,aAAa,oBAChD,CAAE,EAAE,YAAY,kBAAkB,CAAE,IACpC,CAAC;AAAA,EACL,CAAE;AACH;AAEA,SAAS,qBAAsB,mBAAuC;AACrE,MAAI,aAA+B,CAAE,GAAG,sBAAuB;AAE/D,MAAK,mBAAoB;AACxB,iBAAa;AAAA,EACd,WAAY,+BAA+B,GAAI;AAC9C,eAAW,KAAM,KAAM;AAAA,EACxB;AAEA,SAAO,WAAW,KAAM,GAAI;AAC7B;AAEA,SAAS,iCAA0C;AAClD,QAAM,iBAAiB;AAEvB,SAAO,QAAS,eAAe,gBAAgB,OAAO,YAAY,eAAgB;AACnF;","names":[]}
package/dist/index.mjs CHANGED
@@ -37,15 +37,8 @@ function normalize(attachment) {
37
37
  };
38
38
  }
39
39
 
40
- // src/hooks/use-wp-media-attachment.ts
41
- function useWpMediaAttachment(id) {
42
- return useQuery({
43
- queryKey: ["wp-attachment", id],
44
- queryFn: getAttachment,
45
- enabled: !!id
46
- });
47
- }
48
- async function getAttachment({ queryKey: [, id] }) {
40
+ // src/get-media-attachment.ts
41
+ async function getMediaAttachment({ id }) {
49
42
  if (!id) {
50
43
  return null;
51
44
  }
@@ -62,6 +55,15 @@ async function getAttachment({ queryKey: [, id] }) {
62
55
  }
63
56
  }
64
57
 
58
+ // src/hooks/use-wp-media-attachment.ts
59
+ function useWpMediaAttachment(id) {
60
+ return useQuery({
61
+ queryKey: ["wp-attachment", id],
62
+ queryFn: () => getMediaAttachment({ id }),
63
+ enabled: !!id
64
+ });
65
+ }
66
+
65
67
  // src/hooks/use-wp-media-frame.ts
66
68
  import { useEffect, useRef } from "react";
67
69
 
@@ -75,6 +77,7 @@ var wp_plupload_settings_default = () => {
75
77
  };
76
78
 
77
79
  // src/hooks/use-wp-media-frame.ts
80
+ var defaultImageExtensions = ["avif", "bmp", "gif", "ico", "jpe", "jpeg", "jpg", "png", "webp"];
78
81
  function useWpMediaFrame(options) {
79
82
  const frame = useRef();
80
83
  const open = (openOptions = {}) => {
@@ -91,7 +94,15 @@ function useWpMediaFrame(options) {
91
94
  open
92
95
  };
93
96
  }
94
- function createFrame({ onSelect, multiple, types, selected, title, mode = "browse" }) {
97
+ function createFrame({
98
+ onSelect,
99
+ multiple,
100
+ types,
101
+ selected,
102
+ title,
103
+ mode = "browse",
104
+ allowedExtensions
105
+ }) {
95
106
  const frame = media_default()({
96
107
  title,
97
108
  multiple,
@@ -103,7 +114,7 @@ function createFrame({ onSelect, multiple, types, selected, title, mode = "brows
103
114
  applyMode(frame, mode);
104
115
  applySelection(frame, selected);
105
116
  }).on("close", () => cleanupFrame(frame)).on("insert select", () => select(frame, multiple, onSelect));
106
- handleSvgUpload(frame);
117
+ handleAllowedExtensions(frame, allowedExtensions);
107
118
  return frame;
108
119
  }
109
120
  function cleanupFrame(frame) {
@@ -125,29 +136,32 @@ function select(frame, multiple, onSelect) {
125
136
  function setTypeCaller(frame) {
126
137
  frame.uploader.uploader.param("uploadTypeCaller", "elementor-wp-media-upload");
127
138
  }
128
- function handleSvgUpload(frame) {
129
- if (isSvgUploadEnabled()) {
130
- enableSvgUpload(frame);
131
- }
132
- }
133
- function isSvgUploadEnabled() {
134
- const extendedWindow = window;
135
- return Boolean(extendedWindow.elementorCommon.config.filesUpload.unfilteredFiles);
136
- }
137
- function enableSvgUpload(frame) {
138
- const allowedMimeTypes = wp_plupload_settings_default().defaults.filters.mime_types?.[0];
139
- if (!allowedMimeTypes) {
140
- return;
141
- }
142
- const allowedExtensions = allowedMimeTypes.extensions;
139
+ function handleAllowedExtensions(frame, allowedExtensions) {
140
+ const defaultExtensions = wp_plupload_settings_default().defaults.filters.mime_types?.[0]?.extensions;
143
141
  frame.on("ready", () => {
144
- allowedMimeTypes.extensions = allowedExtensions + ",svg";
142
+ wp_plupload_settings_default().defaults.filters.mime_types = [
143
+ { extensions: getAllowedExtensions(allowedExtensions) }
144
+ ];
145
145
  });
146
146
  frame.on("close", () => {
147
- wp_plupload_settings_default().defaults.filters.mime_types = [{ extensions: allowedExtensions }];
147
+ wp_plupload_settings_default().defaults.filters.mime_types = defaultExtensions ? [{ extensions: defaultExtensions }] : [];
148
148
  });
149
149
  }
150
+ function getAllowedExtensions(allowedExtensions) {
151
+ let extensions = [...defaultImageExtensions];
152
+ if (allowedExtensions) {
153
+ extensions = allowedExtensions;
154
+ } else if (isUnfilteredFilesUploadEnabled()) {
155
+ extensions.push("svg");
156
+ }
157
+ return extensions.join(",");
158
+ }
159
+ function isUnfilteredFilesUploadEnabled() {
160
+ const extendedWindow = window;
161
+ return Boolean(extendedWindow.elementorCommon.config.filesUpload.unfilteredFiles);
162
+ }
150
163
  export {
164
+ getMediaAttachment,
151
165
  useWpMediaAttachment,
152
166
  useWpMediaFrame
153
167
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hooks/use-wp-media-attachment.ts","../src/errors.ts","../src/media.ts","../src/normalize.ts","../src/hooks/use-wp-media-frame.ts","../src/wp-plupload-settings.ts"],"sourcesContent":["import { useQuery } from '@elementor/query';\n\nimport media from '../media';\nimport normalize from '../normalize';\n\nexport default function useWpMediaAttachment( id: number | null ) {\n\treturn useQuery( {\n\t\tqueryKey: [ 'wp-attachment', id ],\n\t\tqueryFn: getAttachment,\n\t\tenabled: !! id,\n\t} );\n}\n\nasync function getAttachment( { queryKey: [ , id ] }: { queryKey: [ string, 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 { 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 { useEffect, useRef } from 'react';\n\nimport media from '../media';\nimport normalize from '../normalize';\nimport { type Attachment } from '../types/attachment';\nimport { type ElementorCommonWindow } from '../types/elementor-common';\nimport { type MediaFrame } from '../types/wp-media';\nimport wpPluploadSettings from '../wp-plupload-settings';\n\ntype OpenOptions = {\n\tmode?: 'upload' | 'browse';\n};\n\ntype Options = {\n\ttypes: Array< 'image' | 'image/svg+xml' >;\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, types, selected, title, mode = 'browse' }: Options & OpenOptions ) {\n\tconst frame: MediaFrame = media()( {\n\t\ttitle,\n\t\tmultiple,\n\t\tlibrary: {\n\t\t\ttype: types,\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\thandleSvgUpload( frame );\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 handleSvgUpload( frame: MediaFrame ) {\n\tif ( isSvgUploadEnabled() ) {\n\t\tenableSvgUpload( frame );\n\t}\n}\n\nfunction isSvgUploadEnabled(): boolean {\n\tconst extendedWindow = window as unknown as ElementorCommonWindow;\n\n\treturn Boolean( extendedWindow.elementorCommon.config.filesUpload.unfilteredFiles );\n}\n\nfunction enableSvgUpload( frame: MediaFrame ) {\n\tconst allowedMimeTypes = wpPluploadSettings().defaults.filters.mime_types?.[ 0 ];\n\tif ( ! allowedMimeTypes ) {\n\t\treturn;\n\t}\n\n\tconst allowedExtensions = allowedMimeTypes.extensions;\n\t// Add svg to the allowed extensions\n\tframe.on( 'ready', () => {\n\t\tallowedMimeTypes.extensions = allowedExtensions + ',svg';\n\t} );\n\n\t// Restore allowed upload extensions\n\tframe.on( 'close', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = [ { extensions: allowedExtensions } ];\n\t} );\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,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AAiB,OAAoB;AAC7C,MAAK,mBAAmB,GAAI;AAC3B,oBAAiB,KAAM;AAAA,EACxB;AACD;AAEA,SAAS,qBAA8B;AACtC,QAAM,iBAAiB;AAEvB,SAAO,QAAS,eAAe,gBAAgB,OAAO,YAAY,eAAgB;AACnF;AAEA,SAAS,gBAAiB,OAAoB;AAC7C,QAAM,mBAAmB,6BAAmB,EAAE,SAAS,QAAQ,aAAc,CAAE;AAC/E,MAAK,CAAE,kBAAmB;AACzB;AAAA,EACD;AAEA,QAAM,oBAAoB,iBAAiB;AAE3C,QAAM,GAAI,SAAS,MAAM;AACxB,qBAAiB,aAAa,oBAAoB;AAAA,EACnD,CAAE;AAGF,QAAM,GAAI,SAAS,MAAM;AACxB,iCAAmB,EAAE,SAAS,QAAQ,aAAa,CAAE,EAAE,YAAY,kBAAkB,CAAE;AAAA,EACxF,CAAE;AACH;","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 ElementorCommonWindow } from '../types/elementor-common';\nimport { type ImageExtension } from '../types/image';\nimport { type MediaFrame } from '../types/wp-media';\nimport wpPluploadSettings from '../wp-plupload-settings';\n\ntype OpenOptions = {\n\tmode?: 'upload' | 'browse';\n};\n\ntype Options = {\n\ttypes: Array< 'image' | 'image/svg+xml' >;\n\tallowedExtensions?: ImageExtension[];\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\nconst defaultImageExtensions: ImageExtension[] = [ 'avif', 'bmp', 'gif', 'ico', 'jpe', 'jpeg', 'jpg', 'png', 'webp' ];\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\tmultiple,\n\ttypes,\n\tselected,\n\ttitle,\n\tmode = 'browse',\n\tallowedExtensions,\n}: Options & OpenOptions ) {\n\tconst frame: MediaFrame = media()( {\n\t\ttitle,\n\t\tmultiple,\n\t\tlibrary: {\n\t\t\ttype: types,\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\thandleAllowedExtensions( frame, allowedExtensions );\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 handleAllowedExtensions( frame: MediaFrame, allowedExtensions?: ImageExtension[] ) {\n\tconst defaultExtensions = wpPluploadSettings().defaults.filters.mime_types?.[ 0 ]?.extensions;\n\n\t// Set the allowed extensions\n\tframe.on( 'ready', () => {\n\t\twpPluploadSettings().defaults.filters.mime_types = [\n\t\t\t{ extensions: getAllowedExtensions( allowedExtensions ) },\n\t\t];\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\nfunction getAllowedExtensions( allowedExtensions?: ImageExtension[] ) {\n\tlet extensions: ImageExtension[] = [ ...defaultImageExtensions ];\n\n\tif ( allowedExtensions ) {\n\t\textensions = allowedExtensions;\n\t} else if ( isUnfilteredFilesUploadEnabled() ) {\n\t\textensions.push( 'svg' );\n\t}\n\n\treturn extensions.join( ',' );\n}\n\nfunction isUnfilteredFilesUploadEnabled(): boolean {\n\tconst extendedWindow = window as unknown as ElementorCommonWindow;\n\n\treturn Boolean( extendedWindow.elementorCommon.config.filesUpload.unfilteredFiles );\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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apter for WordPress' media utils",
4
- "version": "0.3.0",
4
+ "version": "0.4.1",
5
5
  "private": false,
6
6
  "author": "Elementor Team",
7
7
  "homepage": "https://elementor.com/",
@@ -41,9 +41,12 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@elementor/query": "0.2.4",
44
- "@elementor/utils": "0.3.0"
44
+ "@elementor/utils": "0.3.1"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "react": "^18.3.1"
48
+ },
49
+ "devDependencies": {
50
+ "tsup": "^8.3.5"
48
51
  }
49
52
  }
@@ -0,0 +1,23 @@
1
+ import media from './media';
2
+ import normalize from './normalize';
3
+
4
+ export async function getMediaAttachment( { id }: { id: number | null } ) {
5
+ if ( ! id ) {
6
+ return null;
7
+ }
8
+
9
+ const model = media().attachment( id );
10
+ const wpAttachment = model.toJSON();
11
+
12
+ const isFetched = 'url' in wpAttachment;
13
+
14
+ if ( isFetched ) {
15
+ return normalize( wpAttachment );
16
+ }
17
+
18
+ try {
19
+ return normalize( await model.fetch() );
20
+ } catch {
21
+ return null;
22
+ }
23
+ }
@@ -1,33 +1,11 @@
1
1
  import { useQuery } from '@elementor/query';
2
2
 
3
- import media from '../media';
4
- import normalize from '../normalize';
3
+ import { getMediaAttachment } from '../get-media-attachment';
5
4
 
6
5
  export default function useWpMediaAttachment( id: number | null ) {
7
6
  return useQuery( {
8
7
  queryKey: [ 'wp-attachment', id ],
9
- queryFn: getAttachment,
8
+ queryFn: () => getMediaAttachment( { id } ),
10
9
  enabled: !! id,
11
10
  } );
12
11
  }
13
-
14
- async function getAttachment( { queryKey: [ , id ] }: { queryKey: [ string, number | null ] } ) {
15
- if ( ! id ) {
16
- return null;
17
- }
18
-
19
- const model = media().attachment( id );
20
- const wpAttachment = model.toJSON();
21
-
22
- const isFetched = 'url' in wpAttachment;
23
-
24
- if ( isFetched ) {
25
- return normalize( wpAttachment );
26
- }
27
-
28
- try {
29
- return normalize( await model.fetch() );
30
- } catch {
31
- return null;
32
- }
33
- }
@@ -4,6 +4,7 @@ import media from '../media';
4
4
  import normalize from '../normalize';
5
5
  import { type Attachment } from '../types/attachment';
6
6
  import { type ElementorCommonWindow } from '../types/elementor-common';
7
+ import { type ImageExtension } from '../types/image';
7
8
  import { type MediaFrame } from '../types/wp-media';
8
9
  import wpPluploadSettings from '../wp-plupload-settings';
9
10
 
@@ -13,6 +14,7 @@ type OpenOptions = {
13
14
 
14
15
  type Options = {
15
16
  types: Array< 'image' | 'image/svg+xml' >;
17
+ allowedExtensions?: ImageExtension[];
16
18
  title?: string;
17
19
  } & (
18
20
  | {
@@ -27,6 +29,8 @@ type Options = {
27
29
  }
28
30
  );
29
31
 
32
+ const defaultImageExtensions: ImageExtension[] = [ 'avif', 'bmp', 'gif', 'ico', 'jpe', 'jpeg', 'jpg', 'png', 'webp' ];
33
+
30
34
  export default function useWpMediaFrame( options: Options ) {
31
35
  const frame = useRef< MediaFrame >();
32
36
 
@@ -49,7 +53,15 @@ export default function useWpMediaFrame( options: Options ) {
49
53
  };
50
54
  }
51
55
 
52
- function createFrame( { onSelect, multiple, types, selected, title, mode = 'browse' }: Options & OpenOptions ) {
56
+ function createFrame( {
57
+ onSelect,
58
+ multiple,
59
+ types,
60
+ selected,
61
+ title,
62
+ mode = 'browse',
63
+ allowedExtensions,
64
+ }: Options & OpenOptions ) {
53
65
  const frame: MediaFrame = media()( {
54
66
  title,
55
67
  multiple,
@@ -65,7 +77,7 @@ function createFrame( { onSelect, multiple, types, selected, title, mode = 'brow
65
77
  .on( 'close', () => cleanupFrame( frame ) )
66
78
  .on( 'insert select', () => select( frame, multiple, onSelect ) );
67
79
 
68
- handleSvgUpload( frame );
80
+ handleAllowedExtensions( frame, allowedExtensions );
69
81
 
70
82
  return frame;
71
83
  }
@@ -102,32 +114,38 @@ function setTypeCaller( frame: MediaFrame ) {
102
114
  frame.uploader.uploader.param( 'uploadTypeCaller', 'elementor-wp-media-upload' );
103
115
  }
104
116
 
105
- function handleSvgUpload( frame: MediaFrame ) {
106
- if ( isSvgUploadEnabled() ) {
107
- enableSvgUpload( frame );
108
- }
109
- }
117
+ function handleAllowedExtensions( frame: MediaFrame, allowedExtensions?: ImageExtension[] ) {
118
+ const defaultExtensions = wpPluploadSettings().defaults.filters.mime_types?.[ 0 ]?.extensions;
110
119
 
111
- function isSvgUploadEnabled(): boolean {
112
- const extendedWindow = window as unknown as ElementorCommonWindow;
120
+ // Set the allowed extensions
121
+ frame.on( 'ready', () => {
122
+ wpPluploadSettings().defaults.filters.mime_types = [
123
+ { extensions: getAllowedExtensions( allowedExtensions ) },
124
+ ];
125
+ } );
113
126
 
114
- return Boolean( extendedWindow.elementorCommon.config.filesUpload.unfilteredFiles );
127
+ // Restore default upload extensions
128
+ frame.on( 'close', () => {
129
+ wpPluploadSettings().defaults.filters.mime_types = defaultExtensions
130
+ ? [ { extensions: defaultExtensions } ]
131
+ : [];
132
+ } );
115
133
  }
116
134
 
117
- function enableSvgUpload( frame: MediaFrame ) {
118
- const allowedMimeTypes = wpPluploadSettings().defaults.filters.mime_types?.[ 0 ];
119
- if ( ! allowedMimeTypes ) {
120
- return;
135
+ function getAllowedExtensions( allowedExtensions?: ImageExtension[] ) {
136
+ let extensions: ImageExtension[] = [ ...defaultImageExtensions ];
137
+
138
+ if ( allowedExtensions ) {
139
+ extensions = allowedExtensions;
140
+ } else if ( isUnfilteredFilesUploadEnabled() ) {
141
+ extensions.push( 'svg' );
121
142
  }
122
143
 
123
- const allowedExtensions = allowedMimeTypes.extensions;
124
- // Add svg to the allowed extensions
125
- frame.on( 'ready', () => {
126
- allowedMimeTypes.extensions = allowedExtensions + ',svg';
127
- } );
144
+ return extensions.join( ',' );
145
+ }
128
146
 
129
- // Restore allowed upload extensions
130
- frame.on( 'close', () => {
131
- wpPluploadSettings().defaults.filters.mime_types = [ { extensions: allowedExtensions } ];
132
- } );
147
+ function isUnfilteredFilesUploadEnabled(): boolean {
148
+ const extendedWindow = window as unknown as ElementorCommonWindow;
149
+
150
+ return Boolean( extendedWindow.elementorCommon.config.filesUpload.unfilteredFiles );
133
151
  }
package/src/index.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  export type { Attachment } from './types/attachment';
2
+ export type { ImageExtension } from './types/image';
2
3
 
3
4
  export { default as useWpMediaAttachment } from './hooks/use-wp-media-attachment';
4
5
  export { default as useWpMediaFrame } from './hooks/use-wp-media-frame';
6
+
7
+ export { getMediaAttachment } from './get-media-attachment';
@@ -0,0 +1 @@
1
+ export type ImageExtension = 'avif' | 'bmp' | 'gif' | 'ico' | 'jpe' | 'jpeg' | 'jpg' | 'png' | 'svg' | 'webp';