@elementor/wp-media 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +45 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +42 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/errors.ts +5 -0
- package/src/hooks/use-wp-media-frame.ts +40 -1
- package/src/types/elementor-common.ts +9 -0
- package/src/types/plupload.ts +18 -0
- package/src/types/wp-media.ts +5 -0
- package/src/wp-plupload-settings.ts +12 -0
package/CHANGELOG.md
CHANGED
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -18,12 +18,12 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
20
|
// src/index.ts
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
23
|
useWpMediaAttachment: () => useWpMediaAttachment,
|
|
24
24
|
useWpMediaFrame: () => useWpMediaFrame
|
|
25
25
|
});
|
|
26
|
-
module.exports = __toCommonJS(
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
27
|
|
|
28
28
|
// src/hooks/use-wp-media-attachment.ts
|
|
29
29
|
var import_query = require("@elementor/query");
|
|
@@ -34,6 +34,10 @@ var WpMediaNotAvailableError = (0, import_utils.createError)({
|
|
|
34
34
|
code: "wp_media_not_available",
|
|
35
35
|
message: "`wp.media` is not available, make sure the `media-models` handle is set in the dependencies array"
|
|
36
36
|
});
|
|
37
|
+
var WpPluploadSettingsNotAvailableError = (0, import_utils.createError)({
|
|
38
|
+
code: "wp_plupload_settings_not_available",
|
|
39
|
+
message: "`_wpPluploadSettings` is not available, make sure a wp media uploader is open"
|
|
40
|
+
});
|
|
37
41
|
|
|
38
42
|
// src/media.ts
|
|
39
43
|
var wpMediaWindow = window;
|
|
@@ -87,6 +91,17 @@ async function getAttachment({ queryKey: [, id] }) {
|
|
|
87
91
|
|
|
88
92
|
// src/hooks/use-wp-media-frame.ts
|
|
89
93
|
var import_react = require("react");
|
|
94
|
+
|
|
95
|
+
// src/wp-plupload-settings.ts
|
|
96
|
+
var wpPluploadSettingsWindow = window;
|
|
97
|
+
var wp_plupload_settings_default = () => {
|
|
98
|
+
if (!wpPluploadSettingsWindow._wpPluploadSettings) {
|
|
99
|
+
throw new WpPluploadSettingsNotAvailableError();
|
|
100
|
+
}
|
|
101
|
+
return wpPluploadSettingsWindow._wpPluploadSettings;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// src/hooks/use-wp-media-frame.ts
|
|
90
105
|
function useWpMediaFrame(options) {
|
|
91
106
|
const frame = (0, import_react.useRef)();
|
|
92
107
|
const open = (openOptions = {}) => {
|
|
@@ -111,9 +126,11 @@ function createFrame({ onSelect, multiple, types, selected, title, mode = "brows
|
|
|
111
126
|
type: types
|
|
112
127
|
}
|
|
113
128
|
}).on("open", () => {
|
|
129
|
+
setTypeCaller(frame);
|
|
114
130
|
applyMode(frame, mode);
|
|
115
131
|
applySelection(frame, selected);
|
|
116
132
|
}).on("close", () => cleanupFrame(frame)).on("insert select", () => select(frame, multiple, onSelect));
|
|
133
|
+
handleSvgUpload(frame);
|
|
117
134
|
return frame;
|
|
118
135
|
}
|
|
119
136
|
function cleanupFrame(frame) {
|
|
@@ -132,6 +149,31 @@ function select(frame, multiple, onSelect) {
|
|
|
132
149
|
const onSelectFn = onSelect;
|
|
133
150
|
onSelectFn(multiple ? attachments : attachments[0]);
|
|
134
151
|
}
|
|
152
|
+
function setTypeCaller(frame) {
|
|
153
|
+
frame.uploader.uploader.param("uploadTypeCaller", "elementor-wp-media-upload");
|
|
154
|
+
}
|
|
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;
|
|
170
|
+
frame.on("ready", () => {
|
|
171
|
+
allowedMimeTypes.extensions = allowedExtensions + ",svg";
|
|
172
|
+
});
|
|
173
|
+
frame.on("close", () => {
|
|
174
|
+
wp_plupload_settings_default().defaults.filters.mime_types = [{ extensions: allowedExtensions }];
|
|
175
|
+
});
|
|
176
|
+
}
|
|
135
177
|
// Annotate the CommonJS export names for ESM import in node:
|
|
136
178
|
0 && (module.exports = {
|
|
137
179
|
useWpMediaAttachment,
|
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"],"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","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 MediaFrame } from '../types/wp-media';\n\ntype OpenOptions = {\n\tmode?: 'upload' | 'browse';\n};\n\ntype Options = {\n\ttypes: Array< 'image' >;\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\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\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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAyB;;;ACAzB,mBAA4B;AAErB,IAAM,+BAA2B,0BAAa;AAAA,EACpD,MAAM;AAAA,EACN,SAAS;AACV,CAAE;;;
|
|
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 wpPluploadSettingsWindow._wpPluploadSettings;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;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;AAAA,EACD;AACD;;;AHZe,SAAR,qBAAuC,IAAoB;AACjE,aAAO,uBAAU;AAAA,IAChB,UAAU,CAAE,iBAAiB,EAAG;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,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":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -7,6 +7,10 @@ var WpMediaNotAvailableError = createError({
|
|
|
7
7
|
code: "wp_media_not_available",
|
|
8
8
|
message: "`wp.media` is not available, make sure the `media-models` handle is set in the dependencies array"
|
|
9
9
|
});
|
|
10
|
+
var WpPluploadSettingsNotAvailableError = createError({
|
|
11
|
+
code: "wp_plupload_settings_not_available",
|
|
12
|
+
message: "`_wpPluploadSettings` is not available, make sure a wp media uploader is open"
|
|
13
|
+
});
|
|
10
14
|
|
|
11
15
|
// src/media.ts
|
|
12
16
|
var wpMediaWindow = window;
|
|
@@ -60,6 +64,17 @@ async function getAttachment({ queryKey: [, id] }) {
|
|
|
60
64
|
|
|
61
65
|
// src/hooks/use-wp-media-frame.ts
|
|
62
66
|
import { useEffect, useRef } from "react";
|
|
67
|
+
|
|
68
|
+
// src/wp-plupload-settings.ts
|
|
69
|
+
var wpPluploadSettingsWindow = window;
|
|
70
|
+
var wp_plupload_settings_default = () => {
|
|
71
|
+
if (!wpPluploadSettingsWindow._wpPluploadSettings) {
|
|
72
|
+
throw new WpPluploadSettingsNotAvailableError();
|
|
73
|
+
}
|
|
74
|
+
return wpPluploadSettingsWindow._wpPluploadSettings;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/hooks/use-wp-media-frame.ts
|
|
63
78
|
function useWpMediaFrame(options) {
|
|
64
79
|
const frame = useRef();
|
|
65
80
|
const open = (openOptions = {}) => {
|
|
@@ -84,9 +99,11 @@ function createFrame({ onSelect, multiple, types, selected, title, mode = "brows
|
|
|
84
99
|
type: types
|
|
85
100
|
}
|
|
86
101
|
}).on("open", () => {
|
|
102
|
+
setTypeCaller(frame);
|
|
87
103
|
applyMode(frame, mode);
|
|
88
104
|
applySelection(frame, selected);
|
|
89
105
|
}).on("close", () => cleanupFrame(frame)).on("insert select", () => select(frame, multiple, onSelect));
|
|
106
|
+
handleSvgUpload(frame);
|
|
90
107
|
return frame;
|
|
91
108
|
}
|
|
92
109
|
function cleanupFrame(frame) {
|
|
@@ -105,6 +122,31 @@ function select(frame, multiple, onSelect) {
|
|
|
105
122
|
const onSelectFn = onSelect;
|
|
106
123
|
onSelectFn(multiple ? attachments : attachments[0]);
|
|
107
124
|
}
|
|
125
|
+
function setTypeCaller(frame) {
|
|
126
|
+
frame.uploader.uploader.param("uploadTypeCaller", "elementor-wp-media-upload");
|
|
127
|
+
}
|
|
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;
|
|
143
|
+
frame.on("ready", () => {
|
|
144
|
+
allowedMimeTypes.extensions = allowedExtensions + ",svg";
|
|
145
|
+
});
|
|
146
|
+
frame.on("close", () => {
|
|
147
|
+
wp_plupload_settings_default().defaults.filters.mime_types = [{ extensions: allowedExtensions }];
|
|
148
|
+
});
|
|
149
|
+
}
|
|
108
150
|
export {
|
|
109
151
|
useWpMediaAttachment,
|
|
110
152
|
useWpMediaFrame
|
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/hooks/use-wp-media-frame.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","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 MediaFrame } from '../types/wp-media';\n\ntype OpenOptions = {\n\tmode?: 'upload' | 'browse';\n};\n\ntype Options = {\n\ttypes: Array< 'image' >;\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\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\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"],"mappings":";AAAA,SAAS,gBAAgB;;;ACAzB,SAAS,mBAAmB;AAErB,IAAM,2BAA2B,YAAa;AAAA,EACpD,MAAM;AAAA,EACN,SAAS;AACV,CAAE;;;
|
|
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,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,EAC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Ac,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":[]}
|
package/package.json
CHANGED
package/src/errors.ts
CHANGED
|
@@ -4,3 +4,8 @@ export const WpMediaNotAvailableError = createError( {
|
|
|
4
4
|
code: 'wp_media_not_available',
|
|
5
5
|
message: '`wp.media` is not available, make sure the `media-models` handle is set in the dependencies array',
|
|
6
6
|
} );
|
|
7
|
+
|
|
8
|
+
export const WpPluploadSettingsNotAvailableError = createError( {
|
|
9
|
+
code: 'wp_plupload_settings_not_available',
|
|
10
|
+
message: '`_wpPluploadSettings` is not available, make sure a wp media uploader is open',
|
|
11
|
+
} );
|
|
@@ -3,14 +3,16 @@ import { useEffect, useRef } from 'react';
|
|
|
3
3
|
import media from '../media';
|
|
4
4
|
import normalize from '../normalize';
|
|
5
5
|
import { type Attachment } from '../types/attachment';
|
|
6
|
+
import { type ElementorCommonWindow } from '../types/elementor-common';
|
|
6
7
|
import { type MediaFrame } from '../types/wp-media';
|
|
8
|
+
import wpPluploadSettings from '../wp-plupload-settings';
|
|
7
9
|
|
|
8
10
|
type OpenOptions = {
|
|
9
11
|
mode?: 'upload' | 'browse';
|
|
10
12
|
};
|
|
11
13
|
|
|
12
14
|
type Options = {
|
|
13
|
-
types: Array< 'image' >;
|
|
15
|
+
types: Array< 'image' | 'image/svg+xml' >;
|
|
14
16
|
title?: string;
|
|
15
17
|
} & (
|
|
16
18
|
| {
|
|
@@ -56,12 +58,15 @@ function createFrame( { onSelect, multiple, types, selected, title, mode = 'brow
|
|
|
56
58
|
},
|
|
57
59
|
} )
|
|
58
60
|
.on( 'open', () => {
|
|
61
|
+
setTypeCaller( frame );
|
|
59
62
|
applyMode( frame, mode );
|
|
60
63
|
applySelection( frame, selected );
|
|
61
64
|
} )
|
|
62
65
|
.on( 'close', () => cleanupFrame( frame ) )
|
|
63
66
|
.on( 'insert select', () => select( frame, multiple, onSelect ) );
|
|
64
67
|
|
|
68
|
+
handleSvgUpload( frame );
|
|
69
|
+
|
|
65
70
|
return frame;
|
|
66
71
|
}
|
|
67
72
|
|
|
@@ -92,3 +97,37 @@ function select( frame: MediaFrame, multiple: boolean, onSelect: Options[ 'onSel
|
|
|
92
97
|
|
|
93
98
|
onSelectFn( multiple ? attachments : attachments[ 0 ] );
|
|
94
99
|
}
|
|
100
|
+
|
|
101
|
+
function setTypeCaller( frame: MediaFrame ) {
|
|
102
|
+
frame.uploader.uploader.param( 'uploadTypeCaller', 'elementor-wp-media-upload' );
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function handleSvgUpload( frame: MediaFrame ) {
|
|
106
|
+
if ( isSvgUploadEnabled() ) {
|
|
107
|
+
enableSvgUpload( frame );
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function isSvgUploadEnabled(): boolean {
|
|
112
|
+
const extendedWindow = window as unknown as ElementorCommonWindow;
|
|
113
|
+
|
|
114
|
+
return Boolean( extendedWindow.elementorCommon.config.filesUpload.unfilteredFiles );
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function enableSvgUpload( frame: MediaFrame ) {
|
|
118
|
+
const allowedMimeTypes = wpPluploadSettings().defaults.filters.mime_types?.[ 0 ];
|
|
119
|
+
if ( ! allowedMimeTypes ) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const allowedExtensions = allowedMimeTypes.extensions;
|
|
124
|
+
// Add svg to the allowed extensions
|
|
125
|
+
frame.on( 'ready', () => {
|
|
126
|
+
allowedMimeTypes.extensions = allowedExtensions + ',svg';
|
|
127
|
+
} );
|
|
128
|
+
|
|
129
|
+
// Restore allowed upload extensions
|
|
130
|
+
frame.on( 'close', () => {
|
|
131
|
+
wpPluploadSettings().defaults.filters.mime_types = [ { extensions: allowedExtensions } ];
|
|
132
|
+
} );
|
|
133
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
type PluploadFilters = {
|
|
2
|
+
mime_types?: PluploadFiltersMimeTypes[];
|
|
3
|
+
max_file_size?: number | string;
|
|
4
|
+
prevent_duplicates?: boolean;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
type PluploadFiltersMimeTypes = {
|
|
8
|
+
title?: string;
|
|
9
|
+
extensions: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type WpPluploadSettingsWindow = Window & {
|
|
13
|
+
_wpPluploadSettings?: {
|
|
14
|
+
defaults: {
|
|
15
|
+
filters: PluploadFilters;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
};
|
package/src/types/wp-media.ts
CHANGED
|
@@ -21,6 +21,11 @@ export type MediaFrame = {
|
|
|
21
21
|
content: {
|
|
22
22
|
mode: ( mode: string ) => void;
|
|
23
23
|
};
|
|
24
|
+
uploader: {
|
|
25
|
+
uploader: {
|
|
26
|
+
param: ( key: string, value: string ) => void;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
24
29
|
state: () => {
|
|
25
30
|
get: ( key: 'selection' ) => {
|
|
26
31
|
set: ( attachments: BackboneAttachmentModel[] ) => void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { WpPluploadSettingsNotAvailableError } from './errors';
|
|
2
|
+
import { type WpPluploadSettingsWindow } from './types/plupload';
|
|
3
|
+
|
|
4
|
+
const wpPluploadSettingsWindow = window as unknown as WpPluploadSettingsWindow;
|
|
5
|
+
|
|
6
|
+
export default () => {
|
|
7
|
+
if ( ! wpPluploadSettingsWindow._wpPluploadSettings ) {
|
|
8
|
+
throw new WpPluploadSettingsNotAvailableError();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return wpPluploadSettingsWindow._wpPluploadSettings;
|
|
12
|
+
};
|