@wordpress/editor 14.50.0 → 14.51.1-next.v.202607070741.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 +10 -0
- package/build/components/collab-sidebar/format.cjs +83 -0
- package/build/components/collab-sidebar/format.cjs.map +7 -0
- package/build/components/collab-sidebar/hooks.cjs +117 -16
- package/build/components/collab-sidebar/hooks.cjs.map +2 -2
- package/build/components/collab-sidebar/index.cjs +18 -2
- package/build/components/collab-sidebar/index.cjs.map +2 -2
- package/build/components/collab-sidebar/note-highlight-styles.cjs +67 -0
- package/build/components/collab-sidebar/note-highlight-styles.cjs.map +7 -0
- package/build/components/collab-sidebar/utils.cjs +139 -0
- package/build/components/collab-sidebar/utils.cjs.map +2 -2
- package/build/components/collaborators-presence/use-collaborator-notifications.cjs +55 -30
- package/build/components/collaborators-presence/use-collaborator-notifications.cjs.map +2 -2
- package/build/components/commands/index.cjs +99 -3
- package/build/components/commands/index.cjs.map +2 -2
- package/build/components/document-bar/index.cjs +28 -34
- package/build/components/document-bar/index.cjs.map +2 -2
- package/build/components/entities-saved-states/index.cjs +1 -4
- package/build/components/entities-saved-states/index.cjs.map +2 -2
- package/build/components/media-categories/index.cjs +92 -6
- package/build/components/media-categories/index.cjs.map +2 -2
- package/build/components/page-attributes/parent.cjs +0 -1
- package/build/components/page-attributes/parent.cjs.map +2 -2
- package/build/components/post-author/combobox.cjs +0 -1
- package/build/components/post-author/combobox.cjs.map +2 -2
- package/build/components/post-author/select.cjs +0 -1
- package/build/components/post-author/select.cjs.map +2 -2
- package/build/components/post-preview-button/index.cjs +29 -2
- package/build/components/post-preview-button/index.cjs.map +2 -2
- package/build/components/post-publish-panel/maybe-upload-media.cjs +15 -26
- package/build/components/post-publish-panel/maybe-upload-media.cjs.map +3 -3
- package/build/components/post-revisions-preview/revisions-slider.cjs +3 -1
- package/build/components/post-revisions-preview/revisions-slider.cjs.map +2 -2
- package/build/components/post-taxonomies/flat-term-selector.cjs +0 -1
- package/build/components/post-taxonomies/flat-term-selector.cjs.map +2 -2
- package/build/components/post-taxonomies/hierarchical-term-selector.cjs +0 -2
- package/build/components/post-taxonomies/hierarchical-term-selector.cjs.map +2 -2
- package/build/components/post-template/classic-theme.cjs +0 -1
- package/build/components/post-template/classic-theme.cjs.map +2 -2
- package/build/components/preferences-modal/index.cjs +54 -28
- package/build/components/preferences-modal/index.cjs.map +2 -2
- package/build/components/preview-dropdown/index.cjs +3 -3
- package/build/components/preview-dropdown/index.cjs.map +2 -2
- package/build/components/provider/use-block-editor-settings.cjs +23 -3
- package/build/components/provider/use-block-editor-settings.cjs.map +3 -3
- package/build/components/sidebar/dataform-post-summary.cjs +5 -1
- package/build/components/sidebar/dataform-post-summary.cjs.map +3 -3
- package/build/components/sidebar/index.cjs +29 -38
- package/build/components/sidebar/index.cjs.map +3 -3
- package/build/components/sidebar/post-summary.cjs +0 -13
- package/build/components/sidebar/post-summary.cjs.map +3 -3
- package/build/dataviews/store/private-actions.cjs +24 -19
- package/build/dataviews/store/private-actions.cjs.map +2 -2
- package/build/store/private-actions.cjs +5 -2
- package/build/store/private-actions.cjs.map +2 -2
- package/build/utils/media-sideload-from-url/index.cjs +63 -0
- package/build/utils/media-sideload-from-url/index.cjs.map +7 -0
- package/build-module/components/collab-sidebar/format.mjs +57 -0
- package/build-module/components/collab-sidebar/format.mjs.map +7 -0
- package/build-module/components/collab-sidebar/hooks.mjs +121 -16
- package/build-module/components/collab-sidebar/hooks.mjs.map +2 -2
- package/build-module/components/collab-sidebar/index.mjs +21 -5
- package/build-module/components/collab-sidebar/index.mjs.map +2 -2
- package/build-module/components/collab-sidebar/note-highlight-styles.mjs +41 -0
- package/build-module/components/collab-sidebar/note-highlight-styles.mjs.map +7 -0
- package/build-module/components/collab-sidebar/utils.mjs +133 -0
- package/build-module/components/collab-sidebar/utils.mjs.map +2 -2
- package/build-module/components/collaborators-presence/use-collaborator-notifications.mjs +55 -30
- package/build-module/components/collaborators-presence/use-collaborator-notifications.mjs.map +2 -2
- package/build-module/components/commands/index.mjs +99 -3
- package/build-module/components/commands/index.mjs.map +2 -2
- package/build-module/components/document-bar/index.mjs +28 -34
- package/build-module/components/document-bar/index.mjs.map +2 -2
- package/build-module/components/entities-saved-states/index.mjs +1 -4
- package/build-module/components/entities-saved-states/index.mjs.map +2 -2
- package/build-module/components/media-categories/index.mjs +93 -7
- package/build-module/components/media-categories/index.mjs.map +2 -2
- package/build-module/components/page-attributes/parent.mjs +0 -1
- package/build-module/components/page-attributes/parent.mjs.map +2 -2
- package/build-module/components/post-author/combobox.mjs +0 -1
- package/build-module/components/post-author/combobox.mjs.map +2 -2
- package/build-module/components/post-author/select.mjs +0 -1
- package/build-module/components/post-author/select.mjs.map +2 -2
- package/build-module/components/post-preview-button/index.mjs +29 -2
- package/build-module/components/post-preview-button/index.mjs.map +2 -2
- package/build-module/components/post-publish-panel/maybe-upload-media.mjs +19 -27
- package/build-module/components/post-publish-panel/maybe-upload-media.mjs.map +2 -2
- package/build-module/components/post-revisions-preview/revisions-slider.mjs +3 -1
- package/build-module/components/post-revisions-preview/revisions-slider.mjs.map +2 -2
- package/build-module/components/post-taxonomies/flat-term-selector.mjs +0 -1
- package/build-module/components/post-taxonomies/flat-term-selector.mjs.map +2 -2
- package/build-module/components/post-taxonomies/hierarchical-term-selector.mjs +0 -2
- package/build-module/components/post-taxonomies/hierarchical-term-selector.mjs.map +2 -2
- package/build-module/components/post-template/classic-theme.mjs +0 -1
- package/build-module/components/post-template/classic-theme.mjs.map +2 -2
- package/build-module/components/preferences-modal/index.mjs +54 -28
- package/build-module/components/preferences-modal/index.mjs.map +2 -2
- package/build-module/components/preview-dropdown/index.mjs +3 -3
- package/build-module/components/preview-dropdown/index.mjs.map +2 -2
- package/build-module/components/provider/use-block-editor-settings.mjs +23 -3
- package/build-module/components/provider/use-block-editor-settings.mjs.map +2 -2
- package/build-module/components/sidebar/dataform-post-summary.mjs +6 -2
- package/build-module/components/sidebar/dataform-post-summary.mjs.map +2 -2
- package/build-module/components/sidebar/index.mjs +29 -42
- package/build-module/components/sidebar/index.mjs.map +2 -2
- package/build-module/components/sidebar/post-summary.mjs +0 -13
- package/build-module/components/sidebar/post-summary.mjs.map +2 -2
- package/build-module/dataviews/store/private-actions.mjs +26 -19
- package/build-module/dataviews/store/private-actions.mjs.map +2 -2
- package/build-module/store/private-actions.mjs +5 -2
- package/build-module/store/private-actions.mjs.map +2 -2
- package/build-module/utils/media-sideload-from-url/index.mjs +32 -0
- package/build-module/utils/media-sideload-from-url/index.mjs.map +7 -0
- package/build-style/style-rtl.css +65 -33
- package/build-style/style.css +65 -33
- package/build-types/components/collab-sidebar/format.d.ts +17 -0
- package/build-types/components/collab-sidebar/format.d.ts.map +1 -0
- package/build-types/components/collab-sidebar/hooks.d.ts.map +1 -1
- package/build-types/components/collab-sidebar/index.d.ts.map +1 -1
- package/build-types/components/collab-sidebar/note-highlight-styles.d.ts +31 -0
- package/build-types/components/collab-sidebar/note-highlight-styles.d.ts.map +1 -0
- package/build-types/components/collab-sidebar/utils.d.ts +82 -2
- package/build-types/components/collab-sidebar/utils.d.ts.map +1 -1
- package/build-types/components/collaborators-presence/use-collaborator-notifications.d.ts.map +1 -1
- package/build-types/components/commands/index.d.ts.map +1 -1
- package/build-types/components/document-bar/index.d.ts.map +1 -1
- package/build-types/components/entities-saved-states/index.d.ts.map +1 -1
- package/build-types/components/media-categories/index.d.ts +12 -3
- package/build-types/components/media-categories/index.d.ts.map +1 -1
- package/build-types/components/page-attributes/parent.d.ts.map +1 -1
- package/build-types/components/post-author/combobox.d.ts.map +1 -1
- package/build-types/components/post-author/select.d.ts.map +1 -1
- package/build-types/components/post-preview-button/index.d.ts.map +1 -1
- package/build-types/components/post-publish-panel/maybe-upload-media.d.ts.map +1 -1
- package/build-types/components/post-revisions-preview/revisions-slider.d.ts.map +1 -1
- package/build-types/components/post-taxonomies/flat-term-selector.d.ts.map +1 -1
- package/build-types/components/post-taxonomies/hierarchical-term-selector.d.ts.map +1 -1
- package/build-types/components/post-template/classic-theme.d.ts.map +1 -1
- package/build-types/components/provider/use-block-editor-settings.d.ts.map +1 -1
- package/build-types/components/sidebar/dataform-post-summary.d.ts.map +1 -1
- package/build-types/components/sidebar/index.d.ts.map +1 -1
- package/build-types/components/sidebar/post-summary.d.ts.map +1 -1
- package/build-types/dataviews/store/private-actions.d.ts.map +1 -1
- package/build-types/store/private-actions.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts.map +1 -1
- package/build-types/utils/get-template-part-icon.d.ts.map +1 -1
- package/build-types/utils/media-sideload-from-url/index.d.ts +18 -0
- package/build-types/utils/media-sideload-from-url/index.d.ts.map +1 -0
- package/package.json +48 -46
- package/src/components/collab-sidebar/format.js +72 -0
- package/src/components/collab-sidebar/hooks.js +203 -22
- package/src/components/collab-sidebar/index.js +19 -5
- package/src/components/collab-sidebar/note-highlight-styles.js +83 -0
- package/src/components/collab-sidebar/style.scss +9 -0
- package/src/components/collab-sidebar/test/note-highlight-styles.js +106 -0
- package/src/components/collab-sidebar/test/utils.js +471 -0
- package/src/components/collab-sidebar/utils.js +225 -2
- package/src/components/collaborators-presence/test/use-collaborator-notifications.ts +265 -262
- package/src/components/collaborators-presence/use-collaborator-notifications.ts +70 -34
- package/src/components/commands/index.js +142 -0
- package/src/components/document-bar/index.js +0 -9
- package/src/components/document-bar/style.scss +0 -17
- package/src/components/entities-saved-states/index.js +1 -5
- package/src/components/header/style.scss +7 -3
- package/src/components/media-categories/index.js +160 -6
- package/src/components/media-categories/test/index.js +202 -0
- package/src/components/page-attributes/parent.js +0 -1
- package/src/components/post-author/combobox.js +0 -1
- package/src/components/post-author/select.js +0 -1
- package/src/components/post-preview-button/index.js +68 -2
- package/src/components/post-publish-panel/maybe-upload-media.js +25 -30
- package/src/components/post-publish-panel/style.scss +1 -1
- package/src/components/post-publish-panel/test/__snapshots__/index.js.snap +11 -127
- package/src/components/post-revisions-preview/revisions-slider.js +4 -1
- package/src/components/post-revisions-preview/test/block-diff.js +2 -2
- package/src/components/post-taxonomies/flat-term-selector.js +0 -1
- package/src/components/post-taxonomies/hierarchical-term-selector.js +0 -2
- package/src/components/post-template/classic-theme.js +0 -1
- package/src/components/preferences-modal/index.js +40 -22
- package/src/components/preferences-modal/test/index.js +83 -0
- package/src/components/preview-dropdown/index.js +3 -3
- package/src/components/provider/use-block-editor-settings.js +42 -1
- package/src/components/sidebar/dataform-post-summary.js +13 -1
- package/src/components/sidebar/index.js +32 -47
- package/src/components/sidebar/post-summary.js +0 -17
- package/src/dataviews/store/private-actions.ts +30 -26
- package/src/store/private-actions.js +6 -2
- package/src/utils/media-sideload-from-url/index.js +54 -0
- package/src/utils/media-sideload-from-url/test/index.js +162 -0
- package/build/components/post-publish-panel/media-util.cjs +0 -72
- package/build/components/post-publish-panel/media-util.cjs.map +0 -7
- package/build-module/components/post-publish-panel/media-util.mjs +0 -46
- package/build-module/components/post-publish-panel/media-util.mjs.map +0 -7
- package/build-types/components/post-publish-panel/media-util.d.ts +0 -20
- package/build-types/components/post-publish-panel/media-util.d.ts.map +0 -1
- package/src/components/post-publish-panel/media-util.js +0 -87
- package/src/components/post-publish-panel/test/media-util.js +0 -118
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import apiFetch from '@wordpress/api-fetch';
|
|
5
|
+
import { select } from '@wordpress/data';
|
|
6
|
+
import { transformAttachment } from '@wordpress/media-utils';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Internal dependencies
|
|
10
|
+
*/
|
|
11
|
+
import { store as editorStore } from '../../store';
|
|
12
|
+
|
|
13
|
+
const noop = () => {};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Sideloads an external image into the media library from its URL.
|
|
17
|
+
*
|
|
18
|
+
* The server downloads the remote file, which avoids a cross-origin browser
|
|
19
|
+
* fetch that fails when the editor is cross-origin isolated. Since the server
|
|
20
|
+
* owns the upload it also generates sub-sizes, as with a regular upload.
|
|
21
|
+
*
|
|
22
|
+
* @param {Object} $0 Parameters object.
|
|
23
|
+
* @param {string} $0.url URL of the external image to sideload.
|
|
24
|
+
* @param {Function} $0.onSuccess Function called with the new attachment on success.
|
|
25
|
+
* @param {Function} $0.onError Function called with an error message on failure.
|
|
26
|
+
*/
|
|
27
|
+
export default function mediaSideloadFromUrl( {
|
|
28
|
+
url,
|
|
29
|
+
onSuccess,
|
|
30
|
+
onError = noop,
|
|
31
|
+
} ) {
|
|
32
|
+
const currentPost = select( editorStore ).getCurrentPost();
|
|
33
|
+
// Templates and template parts store their numerical ID in `wp_id`.
|
|
34
|
+
const currentPostId =
|
|
35
|
+
typeof currentPost?.id === 'number'
|
|
36
|
+
? currentPost.id
|
|
37
|
+
: currentPost?.wp_id;
|
|
38
|
+
const postData = currentPostId ? { post: currentPostId } : {};
|
|
39
|
+
|
|
40
|
+
apiFetch( {
|
|
41
|
+
path: '/wp/v2/media',
|
|
42
|
+
method: 'POST',
|
|
43
|
+
data: {
|
|
44
|
+
url,
|
|
45
|
+
...postData,
|
|
46
|
+
},
|
|
47
|
+
} )
|
|
48
|
+
.then( ( attachment ) => {
|
|
49
|
+
onSuccess?.( transformAttachment( attachment ) );
|
|
50
|
+
} )
|
|
51
|
+
.catch( ( error ) => {
|
|
52
|
+
onError( error?.message || error );
|
|
53
|
+
} );
|
|
54
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import apiFetch from '@wordpress/api-fetch';
|
|
5
|
+
import { select } from '@wordpress/data';
|
|
6
|
+
import { transformAttachment } from '@wordpress/media-utils';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Internal dependencies
|
|
10
|
+
*/
|
|
11
|
+
import mediaSideloadFromUrl from '..';
|
|
12
|
+
|
|
13
|
+
jest.mock( '@wordpress/api-fetch', () => jest.fn() );
|
|
14
|
+
jest.mock( '@wordpress/data', () => ( {
|
|
15
|
+
select: jest.fn(),
|
|
16
|
+
} ) );
|
|
17
|
+
/*
|
|
18
|
+
* The store and transformAttachment are mocked so this unit test does not pull
|
|
19
|
+
* in the full @wordpress/media-utils / core-data import chain (which needs the
|
|
20
|
+
* real @wordpress/data). transformAttachment's mapping is covered in
|
|
21
|
+
* @wordpress/media-utils; here we only verify mediaSideloadFromUrl forwards
|
|
22
|
+
* whatever it returns.
|
|
23
|
+
*/
|
|
24
|
+
jest.mock( '../../../store', () => ( { store: 'core/editor' } ) );
|
|
25
|
+
jest.mock( '@wordpress/media-utils', () => ( {
|
|
26
|
+
transformAttachment: jest.fn( ( attachment ) => ( {
|
|
27
|
+
id: attachment.id,
|
|
28
|
+
url: attachment.source_url,
|
|
29
|
+
alt: attachment.alt_text,
|
|
30
|
+
} ) ),
|
|
31
|
+
} ) );
|
|
32
|
+
|
|
33
|
+
const mockRestAttachment = {
|
|
34
|
+
id: 123,
|
|
35
|
+
alt_text: 'An external photo.',
|
|
36
|
+
caption: { raw: '' },
|
|
37
|
+
title: { raw: 'photo' },
|
|
38
|
+
source_url: 'https://example.com/wp-content/uploads/photo.jpg',
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Sets the post returned by `getCurrentPost()` for a test.
|
|
43
|
+
*
|
|
44
|
+
* @param {Object|undefined} post The post object, or undefined for no post.
|
|
45
|
+
*/
|
|
46
|
+
function mockCurrentPost( post ) {
|
|
47
|
+
select.mockReturnValue( {
|
|
48
|
+
getCurrentPost: () => post,
|
|
49
|
+
} );
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe( 'mediaSideloadFromUrl', () => {
|
|
53
|
+
beforeEach( () => {
|
|
54
|
+
jest.clearAllMocks();
|
|
55
|
+
mockCurrentPost( { id: 42 } );
|
|
56
|
+
} );
|
|
57
|
+
|
|
58
|
+
it( 'should POST to /wp/v2/media with the url, leaving sub-size generation to the server', async () => {
|
|
59
|
+
apiFetch.mockResolvedValue( mockRestAttachment );
|
|
60
|
+
|
|
61
|
+
await new Promise( ( resolve ) => {
|
|
62
|
+
mediaSideloadFromUrl( {
|
|
63
|
+
url: 'https://example.com/photo.jpg',
|
|
64
|
+
onSuccess: resolve,
|
|
65
|
+
} );
|
|
66
|
+
} );
|
|
67
|
+
|
|
68
|
+
expect( apiFetch ).toHaveBeenCalledWith( {
|
|
69
|
+
path: '/wp/v2/media',
|
|
70
|
+
method: 'POST',
|
|
71
|
+
data: {
|
|
72
|
+
url: 'https://example.com/photo.jpg',
|
|
73
|
+
post: 42,
|
|
74
|
+
},
|
|
75
|
+
} );
|
|
76
|
+
} );
|
|
77
|
+
|
|
78
|
+
it( 'should pass the transformed attachment to onSuccess', async () => {
|
|
79
|
+
apiFetch.mockResolvedValue( mockRestAttachment );
|
|
80
|
+
|
|
81
|
+
const media = await new Promise( ( resolve ) => {
|
|
82
|
+
mediaSideloadFromUrl( {
|
|
83
|
+
url: 'https://example.com/photo.jpg',
|
|
84
|
+
onSuccess: resolve,
|
|
85
|
+
} );
|
|
86
|
+
} );
|
|
87
|
+
|
|
88
|
+
/*
|
|
89
|
+
* The raw REST attachment is run through transformAttachment, which maps
|
|
90
|
+
* it to the editor shape the image block expects (url, alt).
|
|
91
|
+
*/
|
|
92
|
+
expect( transformAttachment ).toHaveBeenCalledWith(
|
|
93
|
+
mockRestAttachment
|
|
94
|
+
);
|
|
95
|
+
expect( media ).toMatchObject( {
|
|
96
|
+
id: 123,
|
|
97
|
+
url: 'https://example.com/wp-content/uploads/photo.jpg',
|
|
98
|
+
alt: 'An external photo.',
|
|
99
|
+
} );
|
|
100
|
+
} );
|
|
101
|
+
|
|
102
|
+
it( 'should fall back to wp_id when the post id is not numeric', async () => {
|
|
103
|
+
apiFetch.mockResolvedValue( mockRestAttachment );
|
|
104
|
+
// Templates and template parts expose their numeric id as `wp_id`.
|
|
105
|
+
mockCurrentPost( { id: 'my-theme//header', wp_id: 99 } );
|
|
106
|
+
|
|
107
|
+
await new Promise( ( resolve ) => {
|
|
108
|
+
mediaSideloadFromUrl( {
|
|
109
|
+
url: 'https://example.com/photo.jpg',
|
|
110
|
+
onSuccess: resolve,
|
|
111
|
+
} );
|
|
112
|
+
} );
|
|
113
|
+
|
|
114
|
+
expect( apiFetch ).toHaveBeenCalledWith(
|
|
115
|
+
expect.objectContaining( {
|
|
116
|
+
data: expect.objectContaining( { post: 99 } ),
|
|
117
|
+
} )
|
|
118
|
+
);
|
|
119
|
+
} );
|
|
120
|
+
|
|
121
|
+
it( 'should omit the post param when there is no current post', async () => {
|
|
122
|
+
apiFetch.mockResolvedValue( mockRestAttachment );
|
|
123
|
+
mockCurrentPost( undefined );
|
|
124
|
+
|
|
125
|
+
await new Promise( ( resolve ) => {
|
|
126
|
+
mediaSideloadFromUrl( {
|
|
127
|
+
url: 'https://example.com/photo.jpg',
|
|
128
|
+
onSuccess: resolve,
|
|
129
|
+
} );
|
|
130
|
+
} );
|
|
131
|
+
|
|
132
|
+
const { data } = apiFetch.mock.calls[ 0 ][ 0 ];
|
|
133
|
+
expect( data ).not.toHaveProperty( 'post' );
|
|
134
|
+
} );
|
|
135
|
+
|
|
136
|
+
it( 'should pass the error message to onError on failure', async () => {
|
|
137
|
+
apiFetch.mockRejectedValue(
|
|
138
|
+
new Error( 'Could not download the image.' )
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const message = await new Promise( ( resolve ) => {
|
|
142
|
+
mediaSideloadFromUrl( {
|
|
143
|
+
url: 'https://example.com/photo.jpg',
|
|
144
|
+
onSuccess: () => {},
|
|
145
|
+
onError: resolve,
|
|
146
|
+
} );
|
|
147
|
+
} );
|
|
148
|
+
|
|
149
|
+
expect( message ).toBe( 'Could not download the image.' );
|
|
150
|
+
} );
|
|
151
|
+
|
|
152
|
+
it( 'should not throw when onError is omitted and the request fails', async () => {
|
|
153
|
+
apiFetch.mockRejectedValue( new Error( 'Network error' ) );
|
|
154
|
+
|
|
155
|
+
expect( () =>
|
|
156
|
+
mediaSideloadFromUrl( { url: 'https://example.com/photo.jpg' } )
|
|
157
|
+
).not.toThrow();
|
|
158
|
+
|
|
159
|
+
// Let the rejected promise settle so the default noop onError runs.
|
|
160
|
+
await Promise.resolve();
|
|
161
|
+
} );
|
|
162
|
+
} );
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// packages/editor/src/components/post-publish-panel/media-util.js
|
|
21
|
-
var media_util_exports = {};
|
|
22
|
-
__export(media_util_exports, {
|
|
23
|
-
fetchMedia: () => fetchMedia,
|
|
24
|
-
generateUniqueBasenames: () => generateUniqueBasenames
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(media_util_exports);
|
|
27
|
-
var import_uuid = require("uuid");
|
|
28
|
-
var import_url = require("@wordpress/url");
|
|
29
|
-
function generateUniqueBasenames(urls) {
|
|
30
|
-
const basenames = /* @__PURE__ */ new Set();
|
|
31
|
-
return Object.fromEntries(
|
|
32
|
-
urls.map((url) => {
|
|
33
|
-
const filename = (0, import_url.getFilename)(url);
|
|
34
|
-
let basename = "";
|
|
35
|
-
if (filename) {
|
|
36
|
-
const parts = filename.split(".");
|
|
37
|
-
if (parts.length > 1) {
|
|
38
|
-
parts.pop();
|
|
39
|
-
}
|
|
40
|
-
basename = parts.join(".");
|
|
41
|
-
}
|
|
42
|
-
if (!basename) {
|
|
43
|
-
basename = (0, import_uuid.v4)();
|
|
44
|
-
}
|
|
45
|
-
if (basenames.has(basename)) {
|
|
46
|
-
basename = `${basename}-${(0, import_uuid.v4)()}`;
|
|
47
|
-
}
|
|
48
|
-
basenames.add(basename);
|
|
49
|
-
return [url, basename];
|
|
50
|
-
})
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
function fetchMedia(urls) {
|
|
54
|
-
return Object.fromEntries(
|
|
55
|
-
Object.entries(generateUniqueBasenames(urls)).map(
|
|
56
|
-
([url, basename]) => {
|
|
57
|
-
const filePromise = window.fetch(url.includes("?") ? url : url + "?").then((response) => response.blob()).then((blob) => {
|
|
58
|
-
return new File([blob], `${basename}.png`, {
|
|
59
|
-
type: blob.type
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
return [url, filePromise];
|
|
63
|
-
}
|
|
64
|
-
)
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
68
|
-
0 && (module.exports = {
|
|
69
|
-
fetchMedia,
|
|
70
|
-
generateUniqueBasenames
|
|
71
|
-
});
|
|
72
|
-
//# sourceMappingURL=media-util.cjs.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/components/post-publish-panel/media-util.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport { v4 as uuid } from 'uuid';\n\n/**\n * WordPress dependencies\n */\nimport { getFilename } from '@wordpress/url';\n\n/**\n * Generate a list of unique basenames given a list of URLs.\n *\n * We want all basenames to be unique, since sometimes the extension\n * doesn't reflect the mime type, and may end up getting changed by\n * the server, on upload.\n *\n * @param {string[]} urls The list of URLs\n * @return {Record< string, string >} A URL => basename record.\n */\nexport function generateUniqueBasenames( urls ) {\n\tconst basenames = new Set();\n\n\treturn Object.fromEntries(\n\t\turls.map( ( url ) => {\n\t\t\t// We prefer to match the remote filename, if possible.\n\t\t\tconst filename = getFilename( url );\n\t\t\tlet basename = '';\n\n\t\t\tif ( filename ) {\n\t\t\t\tconst parts = filename.split( '.' );\n\t\t\t\tif ( parts.length > 1 ) {\n\t\t\t\t\t// Assume the last part is the extension.\n\t\t\t\t\tparts.pop();\n\t\t\t\t}\n\t\t\t\tbasename = parts.join( '.' );\n\t\t\t}\n\n\t\t\tif ( ! basename ) {\n\t\t\t\t// It looks like we don't have a basename, so let's use a UUID.\n\t\t\t\tbasename = uuid();\n\t\t\t}\n\n\t\t\tif ( basenames.has( basename ) ) {\n\t\t\t\t// Append a UUID to deduplicate the basename.\n\t\t\t\t// The server will try to deduplicate on its own if we don't do this,\n\t\t\t\t// but it may run into a race condition\n\t\t\t\t// (see https://github.com/WordPress/gutenberg/issues/64899).\n\t\t\t\t// Deduplicating the filenames before uploading is safer.\n\t\t\t\tbasename = `${ basename }-${ uuid() }`;\n\t\t\t}\n\n\t\t\tbasenames.add( basename );\n\n\t\t\treturn [ url, basename ];\n\t\t} )\n\t);\n}\n\n/**\n * Fetch a list of URLs, turning those into promises for files with\n * unique filenames.\n *\n * @param {string[]} urls The list of URLs\n * @return {Record< string, Promise< File > >} A URL => File promise record.\n */\nexport function fetchMedia( urls ) {\n\treturn Object.fromEntries(\n\t\tObject.entries( generateUniqueBasenames( urls ) ).map(\n\t\t\t( [ url, basename ] ) => {\n\t\t\t\tconst filePromise = window\n\t\t\t\t\t.fetch( url.includes( '?' ) ? url : url + '?' )\n\t\t\t\t\t.then( ( response ) => response.blob() )\n\t\t\t\t\t.then( ( blob ) => {\n\t\t\t\t\t\t// The server will reject the upload if it doesn't have an extension,\n\t\t\t\t\t\t// even though it'll rewrite the file name to match the mime type.\n\t\t\t\t\t\t// Here we provide it with a safe extension to get it past that check.\n\t\t\t\t\t\treturn new File( [ blob ], `${ basename }.png`, {\n\t\t\t\t\t\t\ttype: blob.type,\n\t\t\t\t\t\t} );\n\t\t\t\t\t} );\n\n\t\t\t\treturn [ url, filePromise ];\n\t\t\t}\n\t\t)\n\t);\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA2B;AAK3B,iBAA4B;AAYrB,SAAS,wBAAyB,MAAO;AAC/C,QAAM,YAAY,oBAAI,IAAI;AAE1B,SAAO,OAAO;AAAA,IACb,KAAK,IAAK,CAAE,QAAS;AAEpB,YAAM,eAAW,wBAAa,GAAI;AAClC,UAAI,WAAW;AAEf,UAAK,UAAW;AACf,cAAM,QAAQ,SAAS,MAAO,GAAI;AAClC,YAAK,MAAM,SAAS,GAAI;AAEvB,gBAAM,IAAI;AAAA,QACX;AACA,mBAAW,MAAM,KAAM,GAAI;AAAA,MAC5B;AAEA,UAAK,CAAE,UAAW;AAEjB,uBAAW,YAAAA,IAAK;AAAA,MACjB;AAEA,UAAK,UAAU,IAAK,QAAS,GAAI;AAMhC,mBAAW,GAAI,QAAS,QAAK,YAAAA,IAAK,CAAE;AAAA,MACrC;AAEA,gBAAU,IAAK,QAAS;AAExB,aAAO,CAAE,KAAK,QAAS;AAAA,IACxB,CAAE;AAAA,EACH;AACD;AASO,SAAS,WAAY,MAAO;AAClC,SAAO,OAAO;AAAA,IACb,OAAO,QAAS,wBAAyB,IAAK,CAAE,EAAE;AAAA,MACjD,CAAE,CAAE,KAAK,QAAS,MAAO;AACxB,cAAM,cAAc,OAClB,MAAO,IAAI,SAAU,GAAI,IAAI,MAAM,MAAM,GAAI,EAC7C,KAAM,CAAE,aAAc,SAAS,KAAK,CAAE,EACtC,KAAM,CAAE,SAAU;AAIlB,iBAAO,IAAI,KAAM,CAAE,IAAK,GAAG,GAAI,QAAS,QAAQ;AAAA,YAC/C,MAAM,KAAK;AAAA,UACZ,CAAE;AAAA,QACH,CAAE;AAEH,eAAO,CAAE,KAAK,WAAY;AAAA,MAC3B;AAAA,IACD;AAAA,EACD;AACD;",
|
|
6
|
-
"names": ["uuid"]
|
|
7
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
// packages/editor/src/components/post-publish-panel/media-util.js
|
|
2
|
-
import { v4 as uuid } from "uuid";
|
|
3
|
-
import { getFilename } from "@wordpress/url";
|
|
4
|
-
function generateUniqueBasenames(urls) {
|
|
5
|
-
const basenames = /* @__PURE__ */ new Set();
|
|
6
|
-
return Object.fromEntries(
|
|
7
|
-
urls.map((url) => {
|
|
8
|
-
const filename = getFilename(url);
|
|
9
|
-
let basename = "";
|
|
10
|
-
if (filename) {
|
|
11
|
-
const parts = filename.split(".");
|
|
12
|
-
if (parts.length > 1) {
|
|
13
|
-
parts.pop();
|
|
14
|
-
}
|
|
15
|
-
basename = parts.join(".");
|
|
16
|
-
}
|
|
17
|
-
if (!basename) {
|
|
18
|
-
basename = uuid();
|
|
19
|
-
}
|
|
20
|
-
if (basenames.has(basename)) {
|
|
21
|
-
basename = `${basename}-${uuid()}`;
|
|
22
|
-
}
|
|
23
|
-
basenames.add(basename);
|
|
24
|
-
return [url, basename];
|
|
25
|
-
})
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
function fetchMedia(urls) {
|
|
29
|
-
return Object.fromEntries(
|
|
30
|
-
Object.entries(generateUniqueBasenames(urls)).map(
|
|
31
|
-
([url, basename]) => {
|
|
32
|
-
const filePromise = window.fetch(url.includes("?") ? url : url + "?").then((response) => response.blob()).then((blob) => {
|
|
33
|
-
return new File([blob], `${basename}.png`, {
|
|
34
|
-
type: blob.type
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
return [url, filePromise];
|
|
38
|
-
}
|
|
39
|
-
)
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
export {
|
|
43
|
-
fetchMedia,
|
|
44
|
-
generateUniqueBasenames
|
|
45
|
-
};
|
|
46
|
-
//# sourceMappingURL=media-util.mjs.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/components/post-publish-panel/media-util.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport { v4 as uuid } from 'uuid';\n\n/**\n * WordPress dependencies\n */\nimport { getFilename } from '@wordpress/url';\n\n/**\n * Generate a list of unique basenames given a list of URLs.\n *\n * We want all basenames to be unique, since sometimes the extension\n * doesn't reflect the mime type, and may end up getting changed by\n * the server, on upload.\n *\n * @param {string[]} urls The list of URLs\n * @return {Record< string, string >} A URL => basename record.\n */\nexport function generateUniqueBasenames( urls ) {\n\tconst basenames = new Set();\n\n\treturn Object.fromEntries(\n\t\turls.map( ( url ) => {\n\t\t\t// We prefer to match the remote filename, if possible.\n\t\t\tconst filename = getFilename( url );\n\t\t\tlet basename = '';\n\n\t\t\tif ( filename ) {\n\t\t\t\tconst parts = filename.split( '.' );\n\t\t\t\tif ( parts.length > 1 ) {\n\t\t\t\t\t// Assume the last part is the extension.\n\t\t\t\t\tparts.pop();\n\t\t\t\t}\n\t\t\t\tbasename = parts.join( '.' );\n\t\t\t}\n\n\t\t\tif ( ! basename ) {\n\t\t\t\t// It looks like we don't have a basename, so let's use a UUID.\n\t\t\t\tbasename = uuid();\n\t\t\t}\n\n\t\t\tif ( basenames.has( basename ) ) {\n\t\t\t\t// Append a UUID to deduplicate the basename.\n\t\t\t\t// The server will try to deduplicate on its own if we don't do this,\n\t\t\t\t// but it may run into a race condition\n\t\t\t\t// (see https://github.com/WordPress/gutenberg/issues/64899).\n\t\t\t\t// Deduplicating the filenames before uploading is safer.\n\t\t\t\tbasename = `${ basename }-${ uuid() }`;\n\t\t\t}\n\n\t\t\tbasenames.add( basename );\n\n\t\t\treturn [ url, basename ];\n\t\t} )\n\t);\n}\n\n/**\n * Fetch a list of URLs, turning those into promises for files with\n * unique filenames.\n *\n * @param {string[]} urls The list of URLs\n * @return {Record< string, Promise< File > >} A URL => File promise record.\n */\nexport function fetchMedia( urls ) {\n\treturn Object.fromEntries(\n\t\tObject.entries( generateUniqueBasenames( urls ) ).map(\n\t\t\t( [ url, basename ] ) => {\n\t\t\t\tconst filePromise = window\n\t\t\t\t\t.fetch( url.includes( '?' ) ? url : url + '?' )\n\t\t\t\t\t.then( ( response ) => response.blob() )\n\t\t\t\t\t.then( ( blob ) => {\n\t\t\t\t\t\t// The server will reject the upload if it doesn't have an extension,\n\t\t\t\t\t\t// even though it'll rewrite the file name to match the mime type.\n\t\t\t\t\t\t// Here we provide it with a safe extension to get it past that check.\n\t\t\t\t\t\treturn new File( [ blob ], `${ basename }.png`, {\n\t\t\t\t\t\t\ttype: blob.type,\n\t\t\t\t\t\t} );\n\t\t\t\t\t} );\n\n\t\t\t\treturn [ url, filePromise ];\n\t\t\t}\n\t\t)\n\t);\n}\n"],
|
|
5
|
-
"mappings": ";AAGA,SAAS,MAAM,YAAY;AAK3B,SAAS,mBAAmB;AAYrB,SAAS,wBAAyB,MAAO;AAC/C,QAAM,YAAY,oBAAI,IAAI;AAE1B,SAAO,OAAO;AAAA,IACb,KAAK,IAAK,CAAE,QAAS;AAEpB,YAAM,WAAW,YAAa,GAAI;AAClC,UAAI,WAAW;AAEf,UAAK,UAAW;AACf,cAAM,QAAQ,SAAS,MAAO,GAAI;AAClC,YAAK,MAAM,SAAS,GAAI;AAEvB,gBAAM,IAAI;AAAA,QACX;AACA,mBAAW,MAAM,KAAM,GAAI;AAAA,MAC5B;AAEA,UAAK,CAAE,UAAW;AAEjB,mBAAW,KAAK;AAAA,MACjB;AAEA,UAAK,UAAU,IAAK,QAAS,GAAI;AAMhC,mBAAW,GAAI,QAAS,IAAK,KAAK,CAAE;AAAA,MACrC;AAEA,gBAAU,IAAK,QAAS;AAExB,aAAO,CAAE,KAAK,QAAS;AAAA,IACxB,CAAE;AAAA,EACH;AACD;AASO,SAAS,WAAY,MAAO;AAClC,SAAO,OAAO;AAAA,IACb,OAAO,QAAS,wBAAyB,IAAK,CAAE,EAAE;AAAA,MACjD,CAAE,CAAE,KAAK,QAAS,MAAO;AACxB,cAAM,cAAc,OAClB,MAAO,IAAI,SAAU,GAAI,IAAI,MAAM,MAAM,GAAI,EAC7C,KAAM,CAAE,aAAc,SAAS,KAAK,CAAE,EACtC,KAAM,CAAE,SAAU;AAIlB,iBAAO,IAAI,KAAM,CAAE,IAAK,GAAG,GAAI,QAAS,QAAQ;AAAA,YAC/C,MAAM,KAAK;AAAA,UACZ,CAAE;AAAA,QACH,CAAE;AAEH,eAAO,CAAE,KAAK,WAAY;AAAA,MAC3B;AAAA,IACD;AAAA,EACD;AACD;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generate a list of unique basenames given a list of URLs.
|
|
3
|
-
*
|
|
4
|
-
* We want all basenames to be unique, since sometimes the extension
|
|
5
|
-
* doesn't reflect the mime type, and may end up getting changed by
|
|
6
|
-
* the server, on upload.
|
|
7
|
-
*
|
|
8
|
-
* @param {string[]} urls The list of URLs
|
|
9
|
-
* @return {Record< string, string >} A URL => basename record.
|
|
10
|
-
*/
|
|
11
|
-
export declare function generateUniqueBasenames(urls: string[]): Record<string, string>;
|
|
12
|
-
/**
|
|
13
|
-
* Fetch a list of URLs, turning those into promises for files with
|
|
14
|
-
* unique filenames.
|
|
15
|
-
*
|
|
16
|
-
* @param {string[]} urls The list of URLs
|
|
17
|
-
* @return {Record< string, Promise< File > >} A URL => File promise record.
|
|
18
|
-
*/
|
|
19
|
-
export declare function fetchMedia(urls: string[]): Record<string, Promise<File>>;
|
|
20
|
-
//# sourceMappingURL=media-util.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"media-util.d.ts","sourceRoot":"","sources":["../../../src/components/post-publish-panel/media-util.js"],"names":[],"mappings":"AAUA;;;;;;;;;GASG;AACH,wBAAgB,uBAAuB,CAAE,IAAI,EAHlC,MAAM,EAG4B,0BAqC5C;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAE,IAAI,EAHrB,MAAM,EAGe,iCAoB/B"}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { v4 as uuid } from 'uuid';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* WordPress dependencies
|
|
8
|
-
*/
|
|
9
|
-
import { getFilename } from '@wordpress/url';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Generate a list of unique basenames given a list of URLs.
|
|
13
|
-
*
|
|
14
|
-
* We want all basenames to be unique, since sometimes the extension
|
|
15
|
-
* doesn't reflect the mime type, and may end up getting changed by
|
|
16
|
-
* the server, on upload.
|
|
17
|
-
*
|
|
18
|
-
* @param {string[]} urls The list of URLs
|
|
19
|
-
* @return {Record< string, string >} A URL => basename record.
|
|
20
|
-
*/
|
|
21
|
-
export function generateUniqueBasenames( urls ) {
|
|
22
|
-
const basenames = new Set();
|
|
23
|
-
|
|
24
|
-
return Object.fromEntries(
|
|
25
|
-
urls.map( ( url ) => {
|
|
26
|
-
// We prefer to match the remote filename, if possible.
|
|
27
|
-
const filename = getFilename( url );
|
|
28
|
-
let basename = '';
|
|
29
|
-
|
|
30
|
-
if ( filename ) {
|
|
31
|
-
const parts = filename.split( '.' );
|
|
32
|
-
if ( parts.length > 1 ) {
|
|
33
|
-
// Assume the last part is the extension.
|
|
34
|
-
parts.pop();
|
|
35
|
-
}
|
|
36
|
-
basename = parts.join( '.' );
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if ( ! basename ) {
|
|
40
|
-
// It looks like we don't have a basename, so let's use a UUID.
|
|
41
|
-
basename = uuid();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if ( basenames.has( basename ) ) {
|
|
45
|
-
// Append a UUID to deduplicate the basename.
|
|
46
|
-
// The server will try to deduplicate on its own if we don't do this,
|
|
47
|
-
// but it may run into a race condition
|
|
48
|
-
// (see https://github.com/WordPress/gutenberg/issues/64899).
|
|
49
|
-
// Deduplicating the filenames before uploading is safer.
|
|
50
|
-
basename = `${ basename }-${ uuid() }`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
basenames.add( basename );
|
|
54
|
-
|
|
55
|
-
return [ url, basename ];
|
|
56
|
-
} )
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Fetch a list of URLs, turning those into promises for files with
|
|
62
|
-
* unique filenames.
|
|
63
|
-
*
|
|
64
|
-
* @param {string[]} urls The list of URLs
|
|
65
|
-
* @return {Record< string, Promise< File > >} A URL => File promise record.
|
|
66
|
-
*/
|
|
67
|
-
export function fetchMedia( urls ) {
|
|
68
|
-
return Object.fromEntries(
|
|
69
|
-
Object.entries( generateUniqueBasenames( urls ) ).map(
|
|
70
|
-
( [ url, basename ] ) => {
|
|
71
|
-
const filePromise = window
|
|
72
|
-
.fetch( url.includes( '?' ) ? url : url + '?' )
|
|
73
|
-
.then( ( response ) => response.blob() )
|
|
74
|
-
.then( ( blob ) => {
|
|
75
|
-
// The server will reject the upload if it doesn't have an extension,
|
|
76
|
-
// even though it'll rewrite the file name to match the mime type.
|
|
77
|
-
// Here we provide it with a safe extension to get it past that check.
|
|
78
|
-
return new File( [ blob ], `${ basename }.png`, {
|
|
79
|
-
type: blob.type,
|
|
80
|
-
} );
|
|
81
|
-
} );
|
|
82
|
-
|
|
83
|
-
return [ url, filePromise ];
|
|
84
|
-
}
|
|
85
|
-
)
|
|
86
|
-
);
|
|
87
|
-
}
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { generateUniqueBasenames } from '../media-util';
|
|
5
|
-
|
|
6
|
-
describe( 'generateUniqueBasenames', () => {
|
|
7
|
-
it( 'should prefer the original basenames', () => {
|
|
8
|
-
const urls = [
|
|
9
|
-
'https://example.com/images/image1.jpg',
|
|
10
|
-
'https://example.com/images/image2.jpg',
|
|
11
|
-
'https://example.com/images/image3.jpg',
|
|
12
|
-
'https://example.com/images/image4.jpg',
|
|
13
|
-
];
|
|
14
|
-
|
|
15
|
-
expect( generateUniqueBasenames( urls ) ).toEqual( {
|
|
16
|
-
'https://example.com/images/image1.jpg': 'image1',
|
|
17
|
-
'https://example.com/images/image2.jpg': 'image2',
|
|
18
|
-
'https://example.com/images/image3.jpg': 'image3',
|
|
19
|
-
'https://example.com/images/image4.jpg': 'image4',
|
|
20
|
-
} );
|
|
21
|
-
} );
|
|
22
|
-
|
|
23
|
-
it( 'should handle filenames with no extensions', () => {
|
|
24
|
-
const urls = [
|
|
25
|
-
'https://example.com/images/image1',
|
|
26
|
-
'https://example.com/images/image2',
|
|
27
|
-
'https://example.com/images/image3',
|
|
28
|
-
'https://example.com/images/image4',
|
|
29
|
-
];
|
|
30
|
-
|
|
31
|
-
expect( generateUniqueBasenames( urls ) ).toEqual( {
|
|
32
|
-
'https://example.com/images/image1': 'image1',
|
|
33
|
-
'https://example.com/images/image2': 'image2',
|
|
34
|
-
'https://example.com/images/image3': 'image3',
|
|
35
|
-
'https://example.com/images/image4': 'image4',
|
|
36
|
-
} );
|
|
37
|
-
} );
|
|
38
|
-
|
|
39
|
-
it( 'should handle query parameters correctly', () => {
|
|
40
|
-
const urls = [
|
|
41
|
-
'https://example.com/images/image1.jpg?a=notafile.npg',
|
|
42
|
-
'https://example.com/images/image2.jpg?a=notafile.npg',
|
|
43
|
-
'https://example.com/images/image3.jpg?a=notafile.npg',
|
|
44
|
-
'https://example.com/images/image4.jpg?a=notafile.npg',
|
|
45
|
-
];
|
|
46
|
-
|
|
47
|
-
expect( generateUniqueBasenames( urls ) ).toEqual( {
|
|
48
|
-
'https://example.com/images/image1.jpg?a=notafile.npg': 'image1',
|
|
49
|
-
'https://example.com/images/image2.jpg?a=notafile.npg': 'image2',
|
|
50
|
-
'https://example.com/images/image3.jpg?a=notafile.npg': 'image3',
|
|
51
|
-
'https://example.com/images/image4.jpg?a=notafile.npg': 'image4',
|
|
52
|
-
} );
|
|
53
|
-
} );
|
|
54
|
-
|
|
55
|
-
it( 'should deduplicate identical filenames', () => {
|
|
56
|
-
const urls = [
|
|
57
|
-
'https://example.com/image1/image.jpg',
|
|
58
|
-
'https://example.com/image2/image.jpg',
|
|
59
|
-
'https://example.com/image3/image.jpg',
|
|
60
|
-
'https://example.com/image4/image.jpg',
|
|
61
|
-
];
|
|
62
|
-
|
|
63
|
-
const results = generateUniqueBasenames( urls );
|
|
64
|
-
const resultLength = Object.entries( results ).length;
|
|
65
|
-
expect( resultLength ).toBe( urls.length );
|
|
66
|
-
|
|
67
|
-
const basenames = new Set( Object.values( results ) );
|
|
68
|
-
expect( basenames.size ).toBe( resultLength );
|
|
69
|
-
} );
|
|
70
|
-
|
|
71
|
-
it( 'should deduplicate identical basenames', () => {
|
|
72
|
-
const urls = [
|
|
73
|
-
'https://example.com/images/image.jpg',
|
|
74
|
-
'https://example.com/images/image.png',
|
|
75
|
-
'https://example.com/images/image.webp',
|
|
76
|
-
'https://example.com/images/image.avif',
|
|
77
|
-
];
|
|
78
|
-
|
|
79
|
-
const results = generateUniqueBasenames( urls );
|
|
80
|
-
const resultLength = Object.entries( results ).length;
|
|
81
|
-
expect( resultLength ).toBe( urls.length );
|
|
82
|
-
|
|
83
|
-
const basenames = new Set( Object.values( results ) );
|
|
84
|
-
expect( basenames.size ).toBe( resultLength );
|
|
85
|
-
} );
|
|
86
|
-
|
|
87
|
-
it( 'should deduplicate filenames without extensions', () => {
|
|
88
|
-
const urls = [
|
|
89
|
-
'https://example.com/image1/image',
|
|
90
|
-
'https://example.com/image2/image',
|
|
91
|
-
'https://example.com/image3/image',
|
|
92
|
-
'https://example.com/image4/image',
|
|
93
|
-
];
|
|
94
|
-
|
|
95
|
-
const results = generateUniqueBasenames( urls );
|
|
96
|
-
const resultLength = Object.entries( results ).length;
|
|
97
|
-
expect( resultLength ).toBe( urls.length );
|
|
98
|
-
|
|
99
|
-
const basenames = new Set( Object.values( results ) );
|
|
100
|
-
expect( basenames.size ).toBe( resultLength );
|
|
101
|
-
} );
|
|
102
|
-
|
|
103
|
-
it( 'should deduplicate paths with no filename', () => {
|
|
104
|
-
const urls = [
|
|
105
|
-
'https://example.com/image1/dir/',
|
|
106
|
-
'https://example.com/image2/dir/',
|
|
107
|
-
'https://example.com/image3/dir/',
|
|
108
|
-
'https://example.com/image4/dir/',
|
|
109
|
-
];
|
|
110
|
-
|
|
111
|
-
const results = generateUniqueBasenames( urls );
|
|
112
|
-
const resultLength = Object.entries( results ).length;
|
|
113
|
-
expect( resultLength ).toBe( urls.length );
|
|
114
|
-
|
|
115
|
-
const basenames = new Set( Object.values( results ) );
|
|
116
|
-
expect( basenames.size ).toBe( resultLength );
|
|
117
|
-
} );
|
|
118
|
-
} );
|