-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.js
155 lines (146 loc) · 3.73 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
/**
* WordPress dependencies
*/
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import {
Disabled,
RangeControl,
ToggleControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import ServerSideRender from '@wordpress/server-side-render';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
/**
* Minimum number of comments a user can show using this block.
*
* @type {number}
*/
const MIN_COMMENTS = 1;
/**
* Maximum number of comments a user can show using this block.
*
* @type {number}
*/
const MAX_COMMENTS = 100;
export default function LatestComments( { attributes, setAttributes } ) {
const { commentsToShow, displayAvatar, displayDate, displayExcerpt } =
attributes;
const serverSideAttributes = {
...attributes,
style: {
...attributes?.style,
spacing: undefined,
},
};
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
return (
<div { ...useBlockProps() }>
<InspectorControls>
<ToolsPanel
label={ __( 'Settings' ) }
resetAll={ () => {
setAttributes( {
commentsToShow: 5,
displayAvatar: true,
displayDate: true,
displayExcerpt: true,
} );
} }
dropdownMenuProps={ dropdownMenuProps }
>
<ToolsPanelItem
hasValue={ () => ! displayAvatar }
label={ __( 'Display avatar' ) }
onDeselect={ () =>
setAttributes( { displayAvatar: true } )
}
isShownByDefault
>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Display avatar' ) }
checked={ displayAvatar }
onChange={ () =>
setAttributes( {
displayAvatar: ! displayAvatar,
} )
}
/>
</ToolsPanelItem>
<ToolsPanelItem
hasValue={ () => ! displayDate }
label={ __( 'Display date' ) }
onDeselect={ () =>
setAttributes( { displayDate: true } )
}
isShownByDefault
>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Display date' ) }
checked={ displayDate }
onChange={ () =>
setAttributes( { displayDate: ! displayDate } )
}
/>
</ToolsPanelItem>
<ToolsPanelItem
hasValue={ () => ! displayExcerpt }
label={ __( 'Display excerpt' ) }
onDeselect={ () =>
setAttributes( { displayExcerpt: true } )
}
isShownByDefault
>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Display excerpt' ) }
checked={ displayExcerpt }
onChange={ () =>
setAttributes( {
displayExcerpt: ! displayExcerpt,
} )
}
/>
</ToolsPanelItem>
<ToolsPanelItem
hasValue={ () => commentsToShow !== 5 }
label={ __( 'Number of comments' ) }
onDeselect={ () =>
setAttributes( { commentsToShow: 5 } )
}
isShownByDefault
>
<RangeControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Number of comments' ) }
value={ commentsToShow }
onChange={ ( value ) =>
setAttributes( { commentsToShow: value } )
}
min={ MIN_COMMENTS }
max={ MAX_COMMENTS }
required
/>
</ToolsPanelItem>
</ToolsPanel>
</InspectorControls>
<Disabled>
<ServerSideRender
block="core/latest-comments"
attributes={ serverSideAttributes }
// The preview uses the site's locale to make it more true to how
// the block appears on the frontend. Setting the locale
// explicitly prevents any middleware from setting it to 'user'.
urlQueryArgs={ { _locale: 'site' } }
/>
</Disabled>
</div>
);
}