-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
edit.js
162 lines (147 loc) · 3.99 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
162
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { _x, _n, __, sprintf } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import {
AlignmentControl,
BlockControls,
useBlockProps,
RichText,
} from '@wordpress/block-editor';
import {
createBlock,
getDefaultBlockName,
__unstableSerializeAndClean,
} from '@wordpress/blocks';
import { useEntityProp, useEntityBlockEditor } from '@wordpress/core-data';
import { count as wordCount } from '@wordpress/wordcount';
/**
* Average reading rate - based on average taken from
* https://irisreading.com/average-reading-speed-in-various-languages/
* (Characters/minute used for Chinese rather than words).
*/
const AVERAGE_READING_RATE = 189;
// Allowed formats for the prefix and suffix fields.
const ALLOWED_FORMATS = [
'core/bold',
'core/image',
'core/italic',
'core/link',
'core/strikethrough',
'core/text-color',
];
function PostTimeToReadEdit( {
attributes,
setAttributes,
insertBlocksAfter,
isSelected,
context,
} ) {
const { textAlign, prefix, suffix } = attributes;
const { postId, postType } = context;
const [ contentStructure ] = useEntityProp(
'postType',
postType,
'content',
postId
);
const [ blocks ] = useEntityBlockEditor( 'postType', postType, {
id: postId,
} );
const minutesToReadString = useMemo( () => {
// Replicates the logic found in getEditedPostContent().
let content;
if ( contentStructure instanceof Function ) {
content = contentStructure( { blocks } );
} else if ( blocks ) {
// If we have parsed blocks already, they should be our source of truth.
// Parsing applies block deprecations and legacy block conversions that
// unparsed content will not have.
content = __unstableSerializeAndClean( blocks );
} else {
content = contentStructure;
}
/*
* translators: If your word count is based on single characters (e.g. East Asian characters),
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
* Do not translate into your own language.
*/
const wordCountType = _x(
'words',
'Word count type. Do not translate!'
);
const minutesToRead = Math.round(
wordCount( content, wordCountType ) / AVERAGE_READING_RATE
);
if ( minutesToRead === undefined ) {
return __( 'There is no content.' );
}
if ( minutesToRead !== 0 ) {
return sprintf(
/* translators: %d is the number of minutes the post will take to read. */
_n( '%d minute', '%d minutes', minutesToRead ),
minutesToRead
);
}
return prefix ? __( 'less than a minute' ) : __( 'Less than a minute' );
}, [ contentStructure, blocks ] );
const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );
return (
<>
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
<p { ...blockProps }>
{ ( isSelected || prefix ) && (
<RichText
allowedFormats={ ALLOWED_FORMATS }
className="wp-block-post-time-to-read__prefix"
multiline={ false }
aria-label={ __( 'Prefix' ) }
placeholder={ __( 'Prefix' ) + ' ' }
value={ prefix }
onChange={ ( value ) =>
setAttributes( { prefix: value } )
}
tagName="span"
/>
) }
{ minutesToReadString }
{ ( isSelected || suffix ) && (
<RichText
allowedFormats={ ALLOWED_FORMATS }
className="wp-block-post-time-to-read__suffix"
multiline={ false }
aria-label={ __( 'Suffix' ) }
placeholder={ ' ' + __( 'Suffix' ) }
value={ suffix }
onChange={ ( value ) =>
setAttributes( { suffix: value } )
}
tagName="span"
__unstableOnSplitAtEnd={ () =>
insertBlocksAfter(
createBlock( getDefaultBlockName() )
)
}
/>
) }
</p>
</>
);
}
export default PostTimeToReadEdit;