@wordpress/list-reusable-blocks 5.54.2-next.v.202608281122.0 → 5.55.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 +1 -1
- package/package.json +10 -11
- package/src/components/import-dropdown/index.tsx +0 -30
- package/src/components/import-dropdown/style.scss +0 -8
- package/src/components/import-form/index.tsx +0 -95
- package/src/components/import-form/style.scss +0 -9
- package/src/index.tsx +0 -54
- package/src/style.scss +0 -10
- package/src/utils/export.ts +0 -41
- package/src/utils/file.ts +0 -15
- package/src/utils/import.ts +0 -59
- package/src/utils/types.ts +0 -47
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/list-reusable-blocks",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.55.0",
|
|
4
4
|
"description": "Adding Export/Import support to the reusable blocks listing.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -24,7 +24,6 @@
|
|
|
24
24
|
"npm": ">=8.19.2"
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
|
-
"src",
|
|
28
27
|
"build",
|
|
29
28
|
"build-module",
|
|
30
29
|
"build-style",
|
|
@@ -45,14 +44,14 @@
|
|
|
45
44
|
"wpScript": true,
|
|
46
45
|
"types": "build-types",
|
|
47
46
|
"dependencies": {
|
|
48
|
-
"@wordpress/api-fetch": "^7.
|
|
49
|
-
"@wordpress/base-styles": "^13.
|
|
50
|
-
"@wordpress/blob": "^4.
|
|
51
|
-
"@wordpress/components": "^40.1.
|
|
52
|
-
"@wordpress/compose": "^8.
|
|
53
|
-
"@wordpress/core-data": "^
|
|
54
|
-
"@wordpress/element": "^8.
|
|
55
|
-
"@wordpress/i18n": "^6.
|
|
47
|
+
"@wordpress/api-fetch": "^7.55.0",
|
|
48
|
+
"@wordpress/base-styles": "^13.1.0",
|
|
49
|
+
"@wordpress/blob": "^4.55.0",
|
|
50
|
+
"@wordpress/components": "^40.1.0",
|
|
51
|
+
"@wordpress/compose": "^8.8.0",
|
|
52
|
+
"@wordpress/core-data": "^8.0.0",
|
|
53
|
+
"@wordpress/element": "^8.7.0",
|
|
54
|
+
"@wordpress/i18n": "^6.28.0",
|
|
56
55
|
"change-case": "^4.1.2",
|
|
57
56
|
"remove-accents": "^0.5.0"
|
|
58
57
|
},
|
|
@@ -69,5 +68,5 @@
|
|
|
69
68
|
"publishConfig": {
|
|
70
69
|
"access": "public"
|
|
71
70
|
},
|
|
72
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "485f42ae8a1c58ceea18371a507fd4acfa86fbd8"
|
|
73
72
|
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { Button, Dropdown } from '@wordpress/components';
|
|
2
|
-
import { pipe } from '@wordpress/compose';
|
|
3
|
-
import { __ } from '@wordpress/i18n';
|
|
4
|
-
import type { ImportDropdownProps } from '../../utils/types';
|
|
5
|
-
import ImportForm from '../import-form';
|
|
6
|
-
|
|
7
|
-
function ImportDropdown( { onUpload }: ImportDropdownProps ) {
|
|
8
|
-
return (
|
|
9
|
-
<Dropdown
|
|
10
|
-
popoverProps={ { placement: 'bottom-start' } }
|
|
11
|
-
contentClassName="list-reusable-blocks-import-dropdown__content"
|
|
12
|
-
renderToggle={ ( { isOpen, onToggle } ) => (
|
|
13
|
-
<Button
|
|
14
|
-
size="compact"
|
|
15
|
-
className="list-reusable-blocks-import-dropdown__button"
|
|
16
|
-
aria-expanded={ isOpen }
|
|
17
|
-
onClick={ onToggle }
|
|
18
|
-
variant="primary"
|
|
19
|
-
>
|
|
20
|
-
{ __( 'Import from JSON' ) }
|
|
21
|
-
</Button>
|
|
22
|
-
) }
|
|
23
|
-
renderContent={ ( { onClose } ) => (
|
|
24
|
-
<ImportForm onUpload={ pipe( onClose, onUpload ) } />
|
|
25
|
-
) }
|
|
26
|
-
/>
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export default ImportDropdown;
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { Button, Notice } from '@wordpress/components';
|
|
2
|
-
import { withInstanceId } from '@wordpress/compose';
|
|
3
|
-
import { useRef, useState } from '@wordpress/element';
|
|
4
|
-
import { __, _x } from '@wordpress/i18n';
|
|
5
|
-
import importReusableBlock from '../../utils/import';
|
|
6
|
-
import type { ImportFormProps, ReusableBlock } from '../../utils/types';
|
|
7
|
-
|
|
8
|
-
function ImportForm( { instanceId, onUpload }: ImportFormProps ) {
|
|
9
|
-
const inputId = 'list-reusable-blocks-import-form-' + instanceId;
|
|
10
|
-
|
|
11
|
-
const formRef = useRef< HTMLFormElement >( null );
|
|
12
|
-
const [ isLoading, setIsLoading ] = useState( false );
|
|
13
|
-
const [ error, setError ] = useState< string | null >( null );
|
|
14
|
-
const [ file, setFile ] = useState< File | null >( null );
|
|
15
|
-
|
|
16
|
-
const onChangeFile = ( event: React.ChangeEvent< HTMLInputElement > ) => {
|
|
17
|
-
setFile( event.target.files?.[ 0 ] || null );
|
|
18
|
-
setError( null );
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const onSubmit = ( event: React.FormEvent< HTMLFormElement > ) => {
|
|
22
|
-
event.preventDefault();
|
|
23
|
-
if ( ! file ) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
setIsLoading( true );
|
|
27
|
-
importReusableBlock( file )
|
|
28
|
-
.then( ( reusableBlock: ReusableBlock ) => {
|
|
29
|
-
if ( ! formRef.current ) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
setIsLoading( false );
|
|
34
|
-
onUpload( reusableBlock );
|
|
35
|
-
} )
|
|
36
|
-
.catch( ( errors: Error ) => {
|
|
37
|
-
if ( ! formRef.current ) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
let uiMessage: string;
|
|
42
|
-
switch ( errors.message ) {
|
|
43
|
-
case 'Invalid JSON file':
|
|
44
|
-
uiMessage = __( 'Invalid JSON file' );
|
|
45
|
-
break;
|
|
46
|
-
case 'Invalid pattern JSON file':
|
|
47
|
-
uiMessage = __( 'Invalid pattern JSON file' );
|
|
48
|
-
break;
|
|
49
|
-
default:
|
|
50
|
-
uiMessage = __( 'Unknown error' );
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
setIsLoading( false );
|
|
54
|
-
setError( uiMessage );
|
|
55
|
-
} );
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const onDismissError = () => {
|
|
59
|
-
setError( null );
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
return (
|
|
63
|
-
<form
|
|
64
|
-
className="list-reusable-blocks-import-form"
|
|
65
|
-
onSubmit={ onSubmit }
|
|
66
|
-
ref={ formRef }
|
|
67
|
-
>
|
|
68
|
-
{ error && (
|
|
69
|
-
<Notice status="error" onRemove={ () => onDismissError() }>
|
|
70
|
-
{ error }
|
|
71
|
-
</Notice>
|
|
72
|
-
) }
|
|
73
|
-
<label
|
|
74
|
-
htmlFor={ inputId }
|
|
75
|
-
className="list-reusable-blocks-import-form__label"
|
|
76
|
-
>
|
|
77
|
-
{ __( 'File' ) }
|
|
78
|
-
</label>
|
|
79
|
-
<input id={ inputId } type="file" onChange={ onChangeFile } />
|
|
80
|
-
<Button
|
|
81
|
-
__next40pxDefaultSize
|
|
82
|
-
type="submit"
|
|
83
|
-
isBusy={ isLoading }
|
|
84
|
-
accessibleWhenDisabled
|
|
85
|
-
disabled={ ! file || isLoading }
|
|
86
|
-
variant="secondary"
|
|
87
|
-
className="list-reusable-blocks-import-form__button"
|
|
88
|
-
>
|
|
89
|
-
{ _x( 'Import', 'button label' ) }
|
|
90
|
-
</Button>
|
|
91
|
-
</form>
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export default withInstanceId( ImportForm );
|
package/src/index.tsx
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { createRoot, StrictMode } from '@wordpress/element';
|
|
2
|
-
import { __ } from '@wordpress/i18n';
|
|
3
|
-
import ImportDropdown from './components/import-dropdown';
|
|
4
|
-
import exportReusableBlock from './utils/export';
|
|
5
|
-
|
|
6
|
-
// Setup Export Links.
|
|
7
|
-
document.body.addEventListener( 'click', ( event: MouseEvent ) => {
|
|
8
|
-
const target = event.target as HTMLElement;
|
|
9
|
-
|
|
10
|
-
if ( ! target.classList.contains( 'wp-list-reusable-blocks__export' ) ) {
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
event.preventDefault();
|
|
15
|
-
|
|
16
|
-
const blockId = target.dataset.id;
|
|
17
|
-
if ( blockId ) {
|
|
18
|
-
exportReusableBlock( Number( blockId ) );
|
|
19
|
-
}
|
|
20
|
-
} );
|
|
21
|
-
|
|
22
|
-
// Setup Import Form.
|
|
23
|
-
document.addEventListener( 'DOMContentLoaded', () => {
|
|
24
|
-
const button = document.querySelector( '.page-title-action' );
|
|
25
|
-
if ( ! button ) {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const showNotice = () => {
|
|
30
|
-
const notice = document.createElement( 'div' );
|
|
31
|
-
notice.className = 'notice notice-success is-dismissible';
|
|
32
|
-
notice.innerHTML = `<p>${ __( 'Pattern imported successfully!' ) }</p>`;
|
|
33
|
-
|
|
34
|
-
const headerEnd = document.querySelector( '.wp-header-end' );
|
|
35
|
-
if ( ! headerEnd || ! headerEnd.parentNode ) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
headerEnd.parentNode.insertBefore( notice, headerEnd );
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const container = document.createElement( 'div' );
|
|
42
|
-
container.className = 'list-reusable-blocks__container';
|
|
43
|
-
|
|
44
|
-
if ( ! button.parentNode ) {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
button.parentNode.insertBefore( container, button );
|
|
49
|
-
createRoot( container ).render(
|
|
50
|
-
<StrictMode>
|
|
51
|
-
<ImportDropdown onUpload={ showNotice } />
|
|
52
|
-
</StrictMode>
|
|
53
|
-
);
|
|
54
|
-
} );
|
package/src/style.scss
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
@use "@wordpress/base-styles/mixins" as *;
|
|
2
|
-
@use "./components/import-dropdown/style.scss" as *;
|
|
3
|
-
@use "./components/import-form/style.scss" as *;
|
|
4
|
-
|
|
5
|
-
.list-reusable-blocks__container {
|
|
6
|
-
display: inline-flex;
|
|
7
|
-
align-items: center;
|
|
8
|
-
position: relative;
|
|
9
|
-
top: -3px;
|
|
10
|
-
}
|
package/src/utils/export.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { paramCase as kebabCase } from 'change-case';
|
|
2
|
-
import removeAccents from 'remove-accents';
|
|
3
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
4
|
-
import { downloadBlob } from '@wordpress/blob';
|
|
5
|
-
import type { Post, PostType } from './types';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Export a reusable block as a JSON file.
|
|
9
|
-
*
|
|
10
|
-
* @param id - The ID of the reusable block to export
|
|
11
|
-
*/
|
|
12
|
-
async function exportReusableBlock( id: number ): Promise< void > {
|
|
13
|
-
const postType = await apiFetch< PostType >( {
|
|
14
|
-
path: `/wp/v2/types/wp_block`,
|
|
15
|
-
} );
|
|
16
|
-
|
|
17
|
-
const post = await apiFetch< Post >( {
|
|
18
|
-
path: `/wp/v2/${ postType.rest_base }/${ id }?context=edit`,
|
|
19
|
-
} );
|
|
20
|
-
|
|
21
|
-
const title = post.title.raw;
|
|
22
|
-
const content = post.content.raw;
|
|
23
|
-
const syncStatus = post.wp_pattern_sync_status;
|
|
24
|
-
|
|
25
|
-
const fileContent = JSON.stringify(
|
|
26
|
-
{
|
|
27
|
-
__file: 'wp_block',
|
|
28
|
-
title,
|
|
29
|
-
content,
|
|
30
|
-
syncStatus,
|
|
31
|
-
},
|
|
32
|
-
null,
|
|
33
|
-
2
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
const fileName = kebabCase( removeAccents( title ) ) + '.json';
|
|
37
|
-
|
|
38
|
-
downloadBlob( fileName, fileContent, 'application/json' );
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export default exportReusableBlock;
|
package/src/utils/file.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reads the textual content of the given file.
|
|
3
|
-
*
|
|
4
|
-
* @param file - File to read
|
|
5
|
-
* @return Promise that resolves to the content of the file
|
|
6
|
-
*/
|
|
7
|
-
export function readTextFile( file: File ): Promise< string > {
|
|
8
|
-
const reader = new window.FileReader();
|
|
9
|
-
return new Promise( ( resolve ) => {
|
|
10
|
-
reader.onload = () => {
|
|
11
|
-
resolve( reader.result as string );
|
|
12
|
-
};
|
|
13
|
-
reader.readAsText( file );
|
|
14
|
-
} );
|
|
15
|
-
}
|
package/src/utils/import.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
2
|
-
import { readTextFile } from './file';
|
|
3
|
-
import type {
|
|
4
|
-
ParsedContent,
|
|
5
|
-
PostType,
|
|
6
|
-
ReusableBlock,
|
|
7
|
-
ReusableBlockData,
|
|
8
|
-
} from './types';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Import a reusable block from a JSON file.
|
|
12
|
-
*
|
|
13
|
-
* @param file - File to import
|
|
14
|
-
* @return Promise returning the imported reusable block
|
|
15
|
-
*/
|
|
16
|
-
async function importReusableBlock( file: File ): Promise< ReusableBlock > {
|
|
17
|
-
const fileContent = await readTextFile( file );
|
|
18
|
-
let parsedContent: ParsedContent;
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
parsedContent = JSON.parse( fileContent );
|
|
22
|
-
} catch {
|
|
23
|
-
throw new Error( 'Invalid JSON file' );
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (
|
|
27
|
-
parsedContent.__file !== 'wp_block' ||
|
|
28
|
-
! parsedContent.title ||
|
|
29
|
-
! parsedContent.content ||
|
|
30
|
-
typeof parsedContent.title !== 'string' ||
|
|
31
|
-
typeof parsedContent.content !== 'string' ||
|
|
32
|
-
( parsedContent.syncStatus &&
|
|
33
|
-
typeof parsedContent.syncStatus !== 'string' )
|
|
34
|
-
) {
|
|
35
|
-
throw new Error( 'Invalid pattern JSON file' );
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const postType = await apiFetch< PostType >( {
|
|
39
|
-
path: `/wp/v2/types/wp_block`,
|
|
40
|
-
} );
|
|
41
|
-
|
|
42
|
-
const reusableBlock = await apiFetch< ReusableBlock >( {
|
|
43
|
-
path: `/wp/v2/${ postType.rest_base }`,
|
|
44
|
-
data: {
|
|
45
|
-
title: parsedContent.title,
|
|
46
|
-
content: parsedContent.content,
|
|
47
|
-
status: 'publish',
|
|
48
|
-
meta:
|
|
49
|
-
parsedContent.syncStatus === 'unsynced'
|
|
50
|
-
? { wp_pattern_sync_status: parsedContent.syncStatus }
|
|
51
|
-
: undefined,
|
|
52
|
-
} satisfies ReusableBlockData,
|
|
53
|
-
method: 'POST',
|
|
54
|
-
} );
|
|
55
|
-
|
|
56
|
-
return reusableBlock;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export default importReusableBlock;
|
package/src/utils/types.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { Post as CorePost, Type } from '@wordpress/core-data';
|
|
2
|
-
|
|
3
|
-
export interface ImportDropdownProps {
|
|
4
|
-
onUpload: ( data: ReusableBlock ) => void;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface ImportFormProps {
|
|
8
|
-
instanceId: string | number;
|
|
9
|
-
onUpload: ( reusableBlock: ReusableBlock ) => void;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type PostType = Type;
|
|
13
|
-
|
|
14
|
-
export type Post = CorePost & { wp_pattern_sync_status?: string };
|
|
15
|
-
|
|
16
|
-
export interface ParsedContent {
|
|
17
|
-
__file: string;
|
|
18
|
-
title: string;
|
|
19
|
-
content: string;
|
|
20
|
-
syncStatus?: string;
|
|
21
|
-
[ key: string ]: unknown;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface ReusableBlockMeta {
|
|
25
|
-
wp_pattern_sync_status?: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface ReusableBlockData {
|
|
29
|
-
title: string;
|
|
30
|
-
content: string;
|
|
31
|
-
status: string;
|
|
32
|
-
meta?: ReusableBlockMeta;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface ReusableBlock {
|
|
36
|
-
id: number;
|
|
37
|
-
title: {
|
|
38
|
-
raw: string;
|
|
39
|
-
rendered: string;
|
|
40
|
-
};
|
|
41
|
-
content: {
|
|
42
|
-
raw: string;
|
|
43
|
-
rendered: string;
|
|
44
|
-
};
|
|
45
|
-
status: string;
|
|
46
|
-
[ key: string ]: unknown;
|
|
47
|
-
}
|