forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.js
More file actions
255 lines (242 loc) · 7.03 KB
/
Copy pathedit.js
File metadata and controls
255 lines (242 loc) · 7.03 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* WordPress dependencies
*/
import { store as coreStore } from '@wordpress/core-data';
import { useEffect, useMemo, useState } from '@wordpress/element';
import {
dateI18n,
humanTimeDiff,
getSettings as getDateSettings,
} from '@wordpress/date';
import {
BlockControls,
InspectorControls,
store as blockEditorStore,
useBlockProps,
useBlockEditingMode,
__experimentalDateFormatPicker as DateFormatPicker,
__experimentalPublishDateTimePicker as PublishDateTimePicker,
} from '@wordpress/block-editor';
import {
Dropdown,
ToolbarGroup,
ToolbarButton,
ToggleControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { __, _x, sprintf } from '@wordpress/i18n';
import { pencil } from '@wordpress/icons';
import { DOWN } from '@wordpress/keycodes';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as blocksStore } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
import useDeprecatedTextAlign from '../utils/deprecated-text-align-attributes';
export default function PostDateEdit( props ) {
const {
attributes,
context: { postType: postTypeSlug, queryId },
setAttributes,
name,
} = props;
useDeprecatedTextAlign( props );
const { datetime, format, isLink } = attributes;
const blockProps = useBlockProps();
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
// Use internal state instead of a ref to make sure that the component
// re-renders when the popover's anchor updates.
const [ popoverAnchor, setPopoverAnchor ] = useState( null );
// Memoize popoverProps to avoid returning a new object every time.
const popoverProps = useMemo(
() => ( { anchor: popoverAnchor } ),
[ popoverAnchor ]
);
const { __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
// We need to set the datetime to a default value upon first loading
// to discern the block from its legacy version (which would default
// to the containing post's publish date).
useEffect( () => {
if ( datetime === undefined ) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { datetime: new Date() } );
}
}, [ datetime ] );
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const dateSettings = getDateSettings();
const {
postType,
siteFormat = dateSettings.formats.date,
siteTimeFormat = dateSettings.formats.time,
} = useSelect(
( select ) => {
const { getPostType, getEntityRecord } = select( coreStore );
const siteSettings = getEntityRecord( 'root', 'site' );
return {
siteFormat: siteSettings?.date_format,
siteTimeFormat: siteSettings?.time_format,
postType: postTypeSlug ? getPostType( postTypeSlug ) : null,
};
},
[ postTypeSlug ]
);
const activeBlockVariationName = useSelect(
( select ) =>
select( blocksStore ).getActiveBlockVariation( name, attributes )
?.name,
[ name, attributes ]
);
const blockEditingMode = useBlockEditingMode();
const validDatetime = datetime || new Date();
let postDate = (
<time
dateTime={ dateI18n( 'c', validDatetime ) }
ref={ setPopoverAnchor }
>
{ format === 'human-diff'
? humanTimeDiff( validDatetime )
: dateI18n( format || siteFormat, validDatetime ) }
</time>
);
if ( isLink && datetime ) {
postDate = (
<a
href="#post-date-pseudo-link"
onClick={ ( event ) => event.preventDefault() }
>
{ postDate }
</a>
);
}
return (
<>
{ ( blockEditingMode === 'default' || ! isDescendentOfQueryLoop ) &&
activeBlockVariationName !== 'post-date-modified' &&
( ! isDescendentOfQueryLoop || ! activeBlockVariationName ) && (
<BlockControls group="block">
<ToolbarGroup>
<Dropdown
popoverProps={ popoverProps }
renderContent={ ( { onClose } ) => (
<PublishDateTimePicker
title={
activeBlockVariationName ===
'post-date'
? __( 'Publish Date' )
: __( 'Date' )
}
currentDate={ datetime }
onChange={ ( newDatetime ) =>
setAttributes( {
datetime: newDatetime,
} )
}
is12Hour={ is12HourFormat(
siteTimeFormat
) }
onClose={ onClose }
dateOrder={
/* translators: Order of day, month, and year. Available formats are 'dmy', 'mdy', and 'ymd'. */
_x( 'dmy', 'date order' )
}
/>
) }
renderToggle={ ( { isOpen, onToggle } ) => {
const openOnArrowDown = ( event ) => {
if (
! isOpen &&
event.keyCode === DOWN
) {
event.preventDefault();
onToggle();
}
};
return (
<ToolbarButton
aria-expanded={ isOpen }
icon={ pencil }
title={ __( 'Change Date' ) }
onClick={ onToggle }
onKeyDown={ openOnArrowDown }
/>
);
} }
/>
</ToolbarGroup>
</BlockControls>
) }
<InspectorControls>
<ToolsPanel
label={ __( 'Settings' ) }
resetAll={ () => {
setAttributes( {
datetime: undefined,
format: undefined,
isLink: false,
} );
} }
dropdownMenuProps={ dropdownMenuProps }
>
<ToolsPanelItem
hasValue={ () => !! format }
label={ __( 'Date Format' ) }
onDeselect={ () =>
setAttributes( { format: undefined } )
}
isShownByDefault
>
<DateFormatPicker
format={ format }
defaultFormat={ siteFormat }
onChange={ ( nextFormat ) =>
setAttributes( { format: nextFormat } )
}
/>
</ToolsPanelItem>
<ToolsPanelItem
hasValue={ () => isLink !== false }
label={
postType?.labels?.singular_name
? sprintf(
// translators: %s: Name of the post type e.g: "post".
__( 'Link to %s' ),
postType.labels.singular_name.toLowerCase()
)
: __( 'Link to post' )
}
onDeselect={ () => setAttributes( { isLink: false } ) }
isShownByDefault
>
<ToggleControl
label={
postType?.labels?.singular_name
? sprintf(
// translators: %s: Name of the post type e.g: "post".
__( 'Link to %s' ),
postType.labels.singular_name.toLowerCase()
)
: __( 'Link to post' )
}
onChange={ () =>
setAttributes( { isLink: ! isLink } )
}
checked={ isLink }
/>
</ToolsPanelItem>
</ToolsPanel>
</InspectorControls>
<div { ...blockProps }>{ postDate }</div>
</>
);
}
export function is12HourFormat( format ) {
// To know if the time format is a 12 hour time, look for any of the 12 hour
// format characters: 'a', 'A', 'g', and 'h'. The character must be
// unescaped, i.e. not preceded by a '\'. Coincidentally, 'aAgh' is how I
// feel when working with regular expressions.
// https://www.php.net/manual/en/datetime.format.php
return /(?:^|[^\\])[aAgh]/.test( format );
}