-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.js
155 lines (144 loc) · 3.31 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import {
AlignmentControl,
BlockControls,
InspectorControls,
useBlockProps,
Warning,
HeadingLevelDropdown,
} from '@wordpress/block-editor';
import { ToggleControl, PanelBody } from '@wordpress/components';
import { __, _x, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { useArchiveLabel } from './use-archive-label';
const SUPPORTED_TYPES = [ 'archive', 'search' ];
export default function QueryTitleEdit( {
attributes: {
type,
level,
levelOptions,
textAlign,
showPrefix,
showSearchTerm,
},
setAttributes,
} ) {
const { archiveTypeLabel, archiveNameLabel } = useArchiveLabel();
const TagName = `h${ level }`;
const blockProps = useBlockProps( {
className: clsx( 'wp-block-query-title__placeholder', {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );
if ( ! SUPPORTED_TYPES.includes( type ) ) {
return (
<div { ...blockProps }>
<Warning>{ __( 'Provided type is not supported.' ) }</Warning>
</div>
);
}
let titleElement;
if ( type === 'archive' ) {
let title;
if ( archiveTypeLabel ) {
if ( showPrefix ) {
if ( archiveNameLabel ) {
title = sprintf(
/* translators: 1: Archive type title e.g: "Category", 2: Label of the archive e.g: "Shoes" */
_x( '%1$s: %2$s', 'archive label' ),
archiveTypeLabel,
archiveNameLabel
);
} else {
title = sprintf(
/* translators: %s: Archive type title e.g: "Category", "Tag"... */
__( '%s: Name' ),
archiveTypeLabel
);
}
} else if ( archiveNameLabel ) {
title = archiveNameLabel;
} else {
title = sprintf(
/* translators: %s: Archive type title e.g: "Category", "Tag"... */
__( '%s name' ),
archiveTypeLabel
);
}
} else {
title = showPrefix
? __( 'Archive type: Name' )
: __( 'Archive title' );
}
titleElement = (
<>
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Show archive type in title' ) }
onChange={ () =>
setAttributes( { showPrefix: ! showPrefix } )
}
checked={ showPrefix }
/>
</PanelBody>
</InspectorControls>
<TagName { ...blockProps }>{ title }</TagName>
</>
);
}
if ( type === 'search' ) {
titleElement = (
<>
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Show search term in title' ) }
onChange={ () =>
setAttributes( {
showSearchTerm: ! showSearchTerm,
} )
}
checked={ showSearchTerm }
/>
</PanelBody>
</InspectorControls>
<TagName { ...blockProps }>
{ showSearchTerm
? __( 'Search results for: “search term”' )
: __( 'Search results' ) }
</TagName>
</>
);
}
return (
<>
<BlockControls group="block">
<HeadingLevelDropdown
value={ level }
options={ levelOptions }
onChange={ ( newLevel ) =>
setAttributes( { level: newLevel } )
}
/>
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
{ titleElement }
</>
);
}