-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
mover-description.js
275 lines (250 loc) · 6.88 KB
/
mover-description.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* WordPress dependencies
*/
import { __, _n, sprintf } from '@wordpress/i18n';
/**
* Return a label for the block movement controls depending on block position.
*
* @param {number} selectedCount Number of blocks selected.
* @param {string} type Block type - in the case of a single block, should
* define its 'type'. I.e. 'Text', 'Heading', 'Image' etc.
* @param {number} firstIndex The index (position - 1) of the first block selected.
* @param {boolean} isFirst This is the first block.
* @param {boolean} isLast This is the last block.
* @param {number} dir Direction of movement (> 0 is considered to be going
* down, < 0 is up).
* @param {string} orientation The orientation of the block movers, vertical or
* horizontal.
* @param {boolean} isRTL True if current writing system is right to left.
*
* @return {string} Label for the block movement controls.
*/
export function getBlockMoverDescription(
selectedCount,
type,
firstIndex,
isFirst,
isLast,
dir,
orientation,
isRTL
) {
const position = firstIndex + 1;
const getMovementDirection = ( moveDirection ) => {
if ( moveDirection === 'up' ) {
if ( orientation === 'horizontal' ) {
return isRTL ? 'right' : 'left';
}
return 'up';
} else if ( moveDirection === 'down' ) {
if ( orientation === 'horizontal' ) {
return isRTL ? 'left' : 'right';
}
return 'down';
}
return null;
};
if ( selectedCount > 1 ) {
return getMultiBlockMoverDescription(
selectedCount,
firstIndex,
isFirst,
isLast,
dir
);
}
if ( isFirst && isLast ) {
// translators: %s: Type of block (i.e. Text, Image etc)
return sprintf(
__( 'Block %s is the only block, and cannot be moved' ),
type
);
}
if ( dir > 0 && ! isLast ) {
// moving down
const movementDirection = getMovementDirection( 'down' );
if ( movementDirection === 'down' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc), 2: Position of selected block, 3: New position
__(
'Move %1$s block from position %2$d down to position %3$d'
),
type,
position,
position + 1
);
}
if ( movementDirection === 'left' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc), 2: Position of selected block, 3: New position
__(
'Move %1$s block from position %2$d left to position %3$d'
),
type,
position,
position + 1
);
}
if ( movementDirection === 'right' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc), 2: Position of selected block, 3: New position
__(
'Move %1$s block from position %2$d right to position %3$d'
),
type,
position,
position + 1
);
}
}
if ( dir > 0 && isLast ) {
// moving down, and is the last item
const movementDirection = getMovementDirection( 'down' );
if ( movementDirection === 'down' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc)
__(
'Block %1$s is at the end of the content and can’t be moved down'
),
type
);
}
if ( movementDirection === 'left' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc)
__(
'Block %1$s is at the end of the content and can’t be moved left'
),
type
);
}
if ( movementDirection === 'right' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc)
__(
'Block %1$s is at the end of the content and can’t be moved right'
),
type
);
}
}
if ( dir < 0 && ! isFirst ) {
// moving up
const movementDirection = getMovementDirection( 'up' );
if ( movementDirection === 'up' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc), 2: Position of selected block, 3: New position
__( 'Move %1$s block from position %2$d up to position %3$d' ),
type,
position,
position - 1
);
}
if ( movementDirection === 'left' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc), 2: Position of selected block, 3: New position
__(
'Move %1$s block from position %2$d left to position %3$d'
),
type,
position,
position - 1
);
}
if ( movementDirection === 'right' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc), 2: Position of selected block, 3: New position
__(
'Move %1$s block from position %2$d right to position %3$d'
),
type,
position,
position - 1
);
}
}
if ( dir < 0 && isFirst ) {
// moving up, and is the first item
const movementDirection = getMovementDirection( 'up' );
if ( movementDirection === 'up' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc)
__(
'Block %1$s is at the beginning of the content and can’t be moved up'
),
type
);
}
if ( movementDirection === 'left' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc)
__(
'Block %1$s is at the beginning of the content and can’t be moved left'
),
type
);
}
if ( movementDirection === 'right' ) {
return sprintf(
// translators: 1: Type of block (i.e. Text, Image etc)
__(
'Block %1$s is at the beginning of the content and can’t be moved right'
),
type
);
}
}
}
/**
* Return a label for the block movement controls depending on block position.
*
* @param {number} selectedCount Number of blocks selected.
* @param {number} firstIndex The index (position - 1) of the first block selected.
* @param {boolean} isFirst This is the first block.
* @param {boolean} isLast This is the last block.
* @param {number} dir Direction of movement (> 0 is considered to be going
* down, < 0 is up).
*
* @return {string} Label for the block movement controls.
*/
export function getMultiBlockMoverDescription(
selectedCount,
firstIndex,
isFirst,
isLast,
dir
) {
const position = firstIndex + 1;
if ( dir < 0 && isFirst ) {
return __( 'Blocks cannot be moved up as they are already at the top' );
}
if ( dir > 0 && isLast ) {
return __(
'Blocks cannot be moved down as they are already at the bottom'
);
}
if ( dir < 0 && ! isFirst ) {
return sprintf(
// translators: 1: Number of selected blocks, 2: Position of selected blocks
_n(
'Move %1$d block from position %2$d up by one place',
'Move %1$d blocks from position %2$d up by one place',
selectedCount
),
selectedCount,
position
);
}
if ( dir > 0 && ! isLast ) {
return sprintf(
// translators: 1: Number of selected blocks, 2: Position of selected blocks
_n(
'Move %1$d block from position %2$d down by one place',
'Move %1$d blocks from position %2$d down by one place',
selectedCount
),
selectedCount,
position
);
}
}