forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.js
161 lines (155 loc) · 3.98 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
156
157
158
159
160
161
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';
import { useRef } from '@wordpress/element';
import { __experimentalGetSettings, dateI18n } from '@wordpress/date';
import {
AlignmentControl,
BlockControls,
InspectorControls,
useBlockProps,
} from '@wordpress/block-editor';
import {
Dropdown,
ToolbarGroup,
ToolbarButton,
ToggleControl,
DateTimePicker,
PanelBody,
CustomSelectControl,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { edit } from '@wordpress/icons';
import { DOWN } from '@wordpress/keycodes';
export default function PostDateEdit( {
attributes: { textAlign, format, isLink },
context: { postId, postType, queryId },
setAttributes,
} ) {
const isDescendentOfQueryLoop = !! queryId;
const [ siteFormat ] = useEntityProp( 'root', 'site', 'date_format' );
const [ date, setDate ] = useEntityProp(
'postType',
postType,
'date',
postId
);
const settings = __experimentalGetSettings();
// To know if the current time format is a 12 hour time, look for "a".
// Also make sure this "a" is not escaped by a "/".
const is12Hour = /a(?!\\)/i.test(
settings.formats.time
.toLowerCase() // Test only for the lower case "a".
.replace( /\\\\/g, '' ) // Replace "//" with empty strings.
.split( '' )
.reverse()
.join( '' ) // Reverse the string and test for "a" not followed by a slash.
);
const formatOptions = Object.values( settings.formats ).map(
( formatOption ) => ( {
key: formatOption,
name: dateI18n( formatOption, date ),
} )
);
const resolvedFormat = format || siteFormat || settings.formats.date;
const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );
const timeRef = useRef();
let postDate = date ? (
<time dateTime={ dateI18n( 'c', date ) } ref={ timeRef }>
{ dateI18n( resolvedFormat, date ) }
</time>
) : (
__( 'No Date' )
);
if ( isLink && date ) {
postDate = (
<a
href="#post-date-pseudo-link"
onClick={ ( event ) => event.preventDefault() }
>
{ postDate }
</a>
);
}
return (
<>
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
{ date && ! isDescendentOfQueryLoop && (
<ToolbarGroup>
<Dropdown
popoverProps={ { anchorRef: timeRef.current } }
renderContent={ () => (
<DateTimePicker
currentDate={ date }
onChange={ setDate }
is12Hour={ is12Hour }
/>
) }
renderToggle={ ( { isOpen, onToggle } ) => {
const openOnArrowDown = ( event ) => {
if ( ! isOpen && event.keyCode === DOWN ) {
event.preventDefault();
onToggle();
}
};
return (
<ToolbarButton
aria-expanded={ isOpen }
icon={ edit }
title={ __( 'Change Date' ) }
onClick={ onToggle }
onKeyDown={ openOnArrowDown }
/>
);
} }
/>
</ToolbarGroup>
) }
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Format settings' ) }>
<CustomSelectControl
hideLabelFromVision
label={ __( 'Date Format' ) }
options={ formatOptions }
onChange={ ( { selectedItem } ) =>
setAttributes( {
format: selectedItem.key,
} )
}
value={ formatOptions.find(
( option ) => option.key === resolvedFormat
) }
/>
</PanelBody>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ sprintf(
// translators: %s: Name of the post type e.g: "post".
__( 'Link to %s' ),
postType
) }
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
</PanelBody>
</InspectorControls>
<div { ...blockProps }>{ postDate }</div>
</>
);
}