forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.js
98 lines (91 loc) · 2.44 KB
/
edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import {
AlignmentControl,
useBlockProps,
BlockControls,
HeadingLevelDropdown,
RichText,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
const HEADING_LEVELS = [ 0, 1, 2, 3, 4, 5, 6 ];
export default function SiteTaglineEdit( {
attributes,
setAttributes,
insertBlocksAfter,
} ) {
const { textAlign, level } = attributes;
const { canUserEdit, tagline } = useSelect( ( select ) => {
const { canUser, getEntityRecord, getEditedEntityRecord } =
select( coreStore );
const canEdit = canUser( 'update', 'settings' );
const settings = canEdit ? getEditedEntityRecord( 'root', 'site' ) : {};
const readOnlySettings = getEntityRecord( 'root', '__unstableBase' );
return {
canUserEdit: canUser( 'update', 'settings' ),
tagline: canEdit
? settings?.description
: readOnlySettings?.description,
};
}, [] );
const TagName = level === 0 ? 'p' : `h${ level }`;
const { editEntityRecord } = useDispatch( coreStore );
function setTagline( newTagline ) {
editEntityRecord( 'root', 'site', undefined, {
description: newTagline,
} );
}
const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
'wp-block-site-tagline__placeholder': ! canUserEdit && ! tagline,
} ),
} );
const siteTaglineContent = canUserEdit ? (
<RichText
allowedFormats={ [] }
onChange={ setTagline }
aria-label={ __( 'Site tagline text' ) }
placeholder={ __( 'Write site tagline…' ) }
tagName={ TagName }
value={ tagline }
disableLineBreaks
__unstableOnSplitAtEnd={ () =>
insertBlocksAfter( createBlock( getDefaultBlockName() ) )
}
{ ...blockProps }
/>
) : (
<TagName { ...blockProps }>
{ tagline || __( 'Site Tagline placeholder' ) }
</TagName>
);
return (
<>
<BlockControls group="block">
<HeadingLevelDropdown
options={ HEADING_LEVELS }
value={ level }
onChange={ ( newLevel ) =>
setAttributes( { level: newLevel } )
}
/>
<AlignmentControl
onChange={ ( newAlign ) =>
setAttributes( { textAlign: newAlign } )
}
value={ textAlign }
/>
</BlockControls>
{ siteTaglineContent }
</>
);
}