@wordpress/format-library 3.0.1 → 3.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +1 -1
  2. package/build/bold/index.js +5 -2
  3. package/build/bold/index.js.map +1 -1
  4. package/build/code/index.js +4 -2
  5. package/build/code/index.js.map +1 -1
  6. package/build/italic/index.js +5 -2
  7. package/build/italic/index.js.map +1 -1
  8. package/build/keyboard/index.js +4 -2
  9. package/build/keyboard/index.js.map +1 -1
  10. package/build/link/inline.js +99 -5
  11. package/build/link/inline.js.map +1 -1
  12. package/build/link/utils.js +112 -0
  13. package/build/link/utils.js.map +1 -1
  14. package/build/strikethrough/index.js +4 -2
  15. package/build/strikethrough/index.js.map +1 -1
  16. package/build/subscript/index.js +4 -2
  17. package/build/subscript/index.js.map +1 -1
  18. package/build/superscript/index.js +4 -2
  19. package/build/superscript/index.js.map +1 -1
  20. package/build/text-color/index.js +60 -21
  21. package/build/text-color/index.js.map +1 -1
  22. package/build/text-color/inline.js +105 -37
  23. package/build/text-color/inline.js.map +1 -1
  24. package/build/underline/index.js +4 -2
  25. package/build/underline/index.js.map +1 -1
  26. package/build-module/bold/index.js +5 -2
  27. package/build-module/bold/index.js.map +1 -1
  28. package/build-module/code/index.js +4 -2
  29. package/build-module/code/index.js.map +1 -1
  30. package/build-module/italic/index.js +5 -2
  31. package/build-module/italic/index.js.map +1 -1
  32. package/build-module/keyboard/index.js +4 -2
  33. package/build-module/keyboard/index.js.map +1 -1
  34. package/build-module/link/inline.js +103 -10
  35. package/build-module/link/inline.js.map +1 -1
  36. package/build-module/link/utils.js +110 -1
  37. package/build-module/link/utils.js.map +1 -1
  38. package/build-module/strikethrough/index.js +4 -2
  39. package/build-module/strikethrough/index.js.map +1 -1
  40. package/build-module/subscript/index.js +4 -2
  41. package/build-module/subscript/index.js.map +1 -1
  42. package/build-module/superscript/index.js +4 -2
  43. package/build-module/superscript/index.js.map +1 -1
  44. package/build-module/text-color/index.js +61 -22
  45. package/build-module/text-color/index.js.map +1 -1
  46. package/build-module/text-color/inline.js +104 -37
  47. package/build-module/text-color/inline.js.map +1 -1
  48. package/build-module/underline/index.js +6 -2
  49. package/build-module/underline/index.js.map +1 -1
  50. package/build-style/style-rtl.css +2 -25
  51. package/build-style/style.css +2 -25
  52. package/package.json +15 -15
  53. package/src/bold/index.js +2 -2
  54. package/src/code/index.js +2 -1
  55. package/src/italic/index.js +2 -2
  56. package/src/keyboard/index.js +2 -1
  57. package/src/link/inline.js +113 -8
  58. package/src/link/test/utils.js +362 -1
  59. package/src/link/utils.js +132 -1
  60. package/src/strikethrough/index.js +2 -1
  61. package/src/subscript/index.js +2 -1
  62. package/src/superscript/index.js +2 -1
  63. package/src/text-color/index.js +66 -23
  64. package/src/text-color/inline.js +125 -49
  65. package/src/text-color/style.scss +2 -24
  66. package/src/underline/index.js +3 -1
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { useState, useRef } from '@wordpress/element';
5
- import { __ } from '@wordpress/i18n';
4
+ import { useState, useRef, createInterpolateElement } from '@wordpress/element';
5
+ import { __, sprintf } from '@wordpress/i18n';
6
6
  import { withSpokenMessages, Popover } from '@wordpress/components';
7
7
  import { prependHTTP } from '@wordpress/url';
8
8
  import {
@@ -12,13 +12,19 @@ import {
12
12
  applyFormat,
13
13
  useAnchorRef,
14
14
  removeFormat,
15
+ slice,
16
+ replace,
15
17
  } from '@wordpress/rich-text';
16
- import { __experimentalLinkControl as LinkControl } from '@wordpress/block-editor';
18
+ import {
19
+ __experimentalLinkControl as LinkControl,
20
+ store as blockEditorStore,
21
+ } from '@wordpress/block-editor';
22
+ import { useSelect } from '@wordpress/data';
17
23
 
18
24
  /**
19
25
  * Internal dependencies
20
26
  */
21
- import { createLinkFormat, isValidHref } from './utils';
27
+ import { createLinkFormat, isValidHref, getFormatBoundary } from './utils';
22
28
  import { link as settings } from './index';
23
29
 
24
30
  function InlineLinkUI( {
@@ -31,6 +37,11 @@ function InlineLinkUI( {
31
37
  stopAddingLink,
32
38
  contentRef,
33
39
  } ) {
40
+ const richLinkTextValue = getRichTextValueFromSelection( value, isActive );
41
+
42
+ // Get the text content minus any HTML tags.
43
+ const richTextText = richLinkTextValue.text;
44
+
34
45
  /**
35
46
  * Pending settings to be applied to the next link. When inserting a new
36
47
  * link, toggle values cannot be applied immediately, because there is not
@@ -41,11 +52,22 @@ function InlineLinkUI( {
41
52
  */
42
53
  const [ nextLinkValue, setNextLinkValue ] = useState();
43
54
 
55
+ const { createPageEntity, userCanCreatePages } = useSelect( ( select ) => {
56
+ const { getSettings } = select( blockEditorStore );
57
+ const _settings = getSettings();
58
+
59
+ return {
60
+ createPageEntity: _settings.__experimentalCreatePageEntity,
61
+ userCanCreatePages: _settings.__experimentalUserCanCreatePages,
62
+ };
63
+ }, [] );
64
+
44
65
  const linkValue = {
45
66
  url: activeAttributes.url,
46
67
  type: activeAttributes.type,
47
68
  id: activeAttributes.id,
48
69
  opensInNewTab: activeAttributes.target === '_blank',
70
+ title: richTextText,
49
71
  ...nextLinkValue,
50
72
  };
51
73
 
@@ -85,7 +107,7 @@ function InlineLinkUI( {
85
107
  }
86
108
 
87
109
  const newUrl = prependHTTP( nextValue.url );
88
- const format = createLinkFormat( {
110
+ const linkFormat = createLinkFormat( {
89
111
  url: newUrl,
90
112
  type: nextValue.type,
91
113
  id:
@@ -95,17 +117,46 @@ function InlineLinkUI( {
95
117
  opensInNewWindow: nextValue.opensInNewTab,
96
118
  } );
97
119
 
120
+ const newText = nextValue.title || newUrl;
98
121
  if ( isCollapsed( value ) && ! isActive ) {
99
- const newText = nextValue.title || newUrl;
122
+ // Scenario: we don't have any actively selected text or formats.
100
123
  const toInsert = applyFormat(
101
124
  create( { text: newText } ),
102
- format,
125
+ linkFormat,
103
126
  0,
104
127
  newText.length
105
128
  );
106
129
  onChange( insert( value, toInsert ) );
107
130
  } else {
108
- const newValue = applyFormat( value, format );
131
+ // Scenario: we have any active text selection or an active format
132
+ let newValue;
133
+
134
+ if ( newText === richTextText ) {
135
+ // If we're not updating the text then ignore
136
+ newValue = applyFormat( value, linkFormat );
137
+ } else {
138
+ // Create new RichText value for the new text in order that we
139
+ // can apply formats to it.
140
+ newValue = create( { text: newText } );
141
+
142
+ // Apply the new Link format to this new text value.
143
+ newValue = applyFormat(
144
+ newValue,
145
+ linkFormat,
146
+ 0,
147
+ newText.length
148
+ );
149
+
150
+ // Update the original (full) RichTextValue replacing the
151
+ // target text with the *new* RichTextValue containing:
152
+ // 1. The new text content.
153
+ // 2. The new link format.
154
+ // Note original formats will be lost when applying this change.
155
+ // That is expected behaviour.
156
+ // See: https://github.com/WordPress/gutenberg/pull/33849#issuecomment-936134179.
157
+ newValue = replace( value, richTextText, newValue );
158
+ }
159
+
109
160
  newValue.start = newValue.end;
110
161
  newValue.activeFormats = [];
111
162
  onChange( newValue );
@@ -137,6 +188,32 @@ function InlineLinkUI( {
137
188
  // otherwise it causes a render of the content.
138
189
  const focusOnMount = useRef( addingLink ? 'firstElement' : false );
139
190
 
191
+ async function handleCreate( pageTitle ) {
192
+ const page = await createPageEntity( {
193
+ title: pageTitle,
194
+ status: 'draft',
195
+ } );
196
+
197
+ return {
198
+ id: page.id,
199
+ type: page.type,
200
+ title: page.title.rendered,
201
+ url: page.link,
202
+ kind: 'post-type',
203
+ };
204
+ }
205
+
206
+ function createButtonText( searchTerm ) {
207
+ return createInterpolateElement(
208
+ sprintf(
209
+ /* translators: %s: search term. */
210
+ __( 'Create Page: <mark>%s</mark>' ),
211
+ searchTerm
212
+ ),
213
+ { mark: <mark /> }
214
+ );
215
+ }
216
+
140
217
  return (
141
218
  <Popover
142
219
  anchorRef={ anchorRef }
@@ -150,9 +227,37 @@ function InlineLinkUI( {
150
227
  onRemove={ removeLink }
151
228
  forceIsEditingLink={ addingLink }
152
229
  hasRichPreviews
230
+ createSuggestion={ createPageEntity && handleCreate }
231
+ withCreateSuggestion={ userCanCreatePages }
232
+ createSuggestionButtonText={ createButtonText }
233
+ hasTextControl
153
234
  />
154
235
  </Popover>
155
236
  );
156
237
  }
157
238
 
239
+ function getRichTextValueFromSelection( value, isActive ) {
240
+ // Default to the selection ranges on the RichTextValue object.
241
+ let textStart = value.start;
242
+ let textEnd = value.end;
243
+
244
+ // If the format is currently active then the rich text value
245
+ // should always be taken from the bounds of the active format
246
+ // and not the selected text.
247
+ if ( isActive ) {
248
+ const boundary = getFormatBoundary( value, {
249
+ type: 'core/link',
250
+ } );
251
+
252
+ textStart = boundary.start;
253
+
254
+ // Text *selection* always extends +1 beyond the edge of the format.
255
+ // We account for that here.
256
+ textEnd = boundary.end + 1;
257
+ }
258
+
259
+ // Get a RichTextValue containing the selected text content.
260
+ return slice( value, textStart, textEnd );
261
+ }
262
+
158
263
  export default withSpokenMessages( InlineLinkUI );
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
- import { isValidHref } from '../utils';
4
+ import { isValidHref, getFormatBoundary } from '../utils';
5
5
 
6
6
  describe( 'isValidHref', () => {
7
7
  it( 'returns true if the href cannot be recognised as a url or an anchor link', () => {
@@ -96,3 +96,364 @@ describe( 'isValidHref', () => {
96
96
  } );
97
97
  } );
98
98
  } );
99
+
100
+ describe( 'getFormatBoundary', () => {
101
+ const boldFormat = {
102
+ type: 'core/bold',
103
+ };
104
+
105
+ const italicFormat = {
106
+ type: 'core/italic',
107
+ };
108
+
109
+ const linkFormat = {
110
+ type: 'core/link',
111
+ attributes: {
112
+ url: 'http://www.wordpress.org',
113
+ type: 'URL',
114
+ id: 'www.wordpress.org',
115
+ },
116
+ };
117
+
118
+ describe( 'Invalid values', () => {
119
+ it( 'should return empty bounds if value contains no formats', () => {
120
+ const record = {
121
+ formats: [], // no formats here!
122
+ text: 'Lorem ipsum dolor.',
123
+ start: 8,
124
+ end: 8,
125
+ };
126
+
127
+ expect(
128
+ getFormatBoundary( record, { type: 'core/link' } )
129
+ ).toEqual( {
130
+ start: null,
131
+ end: null,
132
+ } );
133
+ } );
134
+
135
+ it.each( [
136
+ [ 5, 12 ],
137
+ [ 11, 12 ],
138
+ ] )(
139
+ 'should return empty bounds if both start and end indexes do not contain the target format',
140
+ () => {
141
+ const record = {
142
+ formats: [
143
+ null,
144
+ [ italicFormat ],
145
+ [ italicFormat ],
146
+ [ italicFormat ],
147
+ null,
148
+ null, // 5
149
+ [ linkFormat, italicFormat ], // 6
150
+ [ linkFormat ],
151
+ [ linkFormat, boldFormat ],
152
+ [ linkFormat, boldFormat ],
153
+ [ linkFormat, italicFormat ], // 10
154
+ null, // 11
155
+ [ boldFormat ], // 12
156
+ [ boldFormat ],
157
+ [ boldFormat ],
158
+ [ boldFormat ],
159
+ null,
160
+ null,
161
+ ],
162
+
163
+ text: 'Lorem ipsum dolor.',
164
+ start: 5, // no format here
165
+ end: 12, // format here but it doesn't match the target format
166
+ };
167
+
168
+ expect(
169
+ getFormatBoundary( record, { type: 'core/link' } )
170
+ ).toEqual( {
171
+ start: null,
172
+ end: null,
173
+ } );
174
+ }
175
+ );
176
+
177
+ it.each( [
178
+ [
179
+ "start and end are beyond the length of the provided value's formats",
180
+ [ 19, 22 ],
181
+ ],
182
+ [ 'start and end are less than 0', [ -1, -2 ] ],
183
+ ] )(
184
+ 'should return empty bounds if %s',
185
+ ( _ignored, [ start, end ] ) => {
186
+ const record = {
187
+ formats: [
188
+ null,
189
+ [ italicFormat ],
190
+ [ italicFormat ],
191
+ [ italicFormat ],
192
+ null,
193
+ null, // 5
194
+ [ linkFormat, italicFormat ], // 6
195
+ [ linkFormat ],
196
+ [ linkFormat, boldFormat ],
197
+ [ linkFormat, boldFormat ],
198
+ [ linkFormat, italicFormat ], // 10
199
+ null, // 11
200
+ [ boldFormat ], // 12
201
+ [ boldFormat ],
202
+ [ boldFormat ],
203
+ [ boldFormat ],
204
+ null,
205
+ null, // 17
206
+ ],
207
+
208
+ text: 'Lorem ipsum dolor.',
209
+ start,
210
+ end,
211
+ };
212
+
213
+ expect(
214
+ getFormatBoundary( record, { type: 'core/link' } )
215
+ ).toEqual( {
216
+ start: null,
217
+ end: null,
218
+ } );
219
+ }
220
+ );
221
+ } );
222
+
223
+ describe( 'Collapsed values', () => {
224
+ it.each( [
225
+ [ 'inside', [ 8, 8 ] ],
226
+ [ 'start', [ 6, 6 ] ],
227
+ [ 'end', [ 10, 10 ] ],
228
+ [ 'just inside the start', [ 7, 7 ] ],
229
+ [ 'just inside the end', [ 9, 9 ] ],
230
+ ] )(
231
+ 'should find bounds of a format from %s of a collapsed RichTextValue',
232
+ ( _ignored, [ start, end ] ) => {
233
+ const record = {
234
+ formats: [
235
+ null,
236
+ [ italicFormat ],
237
+ [ italicFormat ],
238
+ [ italicFormat ],
239
+ null,
240
+ null,
241
+ [ linkFormat, italicFormat ], // 6
242
+ [ linkFormat ],
243
+ [ linkFormat, boldFormat ],
244
+ [ linkFormat, boldFormat ],
245
+ [ linkFormat, italicFormat ], // 10
246
+ ],
247
+
248
+ text: 'Lorem ipsum dolor.',
249
+ start,
250
+ end,
251
+ };
252
+
253
+ expect(
254
+ getFormatBoundary( record, { type: 'core/link' } )
255
+ ).toEqual( {
256
+ start: 6,
257
+ end: 10,
258
+ } );
259
+ }
260
+ );
261
+
262
+ it( 'should find bounds of a format from just outside the end of a collapsed RichTextValue', () => {
263
+ // This is an edge case which will occur if you create a format, then place
264
+ // the caret just before the format and hit the back ARROW key. The resulting
265
+ // value object will have start and end +1 beyond the edge of the format boundary.
266
+ // The code under test here has to cope with that by searching for formats -1 back
267
+ // from the end value.
268
+ const record = {
269
+ formats: [
270
+ null,
271
+ [ italicFormat ],
272
+ [ italicFormat ],
273
+ [ italicFormat ],
274
+ null,
275
+ null,
276
+ [ linkFormat, italicFormat ], // 6
277
+ [ linkFormat ],
278
+ [ linkFormat, boldFormat ],
279
+ [ linkFormat, boldFormat ],
280
+ [ linkFormat, italicFormat ], // 10
281
+ ],
282
+
283
+ text: 'Lorem ipsum dolor.',
284
+ start: 11, // +1 beyond the end of the formats
285
+ end: 11, // +1 beyond the end of the formats
286
+ };
287
+
288
+ expect(
289
+ getFormatBoundary( record, { type: 'core/link' } )
290
+ ).toEqual( {
291
+ start: 6,
292
+ end: 10,
293
+ } );
294
+ } );
295
+ } );
296
+
297
+ describe( 'Non-collapsed values', () => {
298
+ it.each( [
299
+ [ 'at the bounds', [ 6, 10 ] ],
300
+ [ 'inside the bounds', [ 7, 8 ] ],
301
+ ] )(
302
+ 'should find bounds of a format beginning %s of a non-collapsed RichTextValue',
303
+ ( _ignored, [ start, end ] ) => {
304
+ const record = {
305
+ formats: [
306
+ null,
307
+ [ italicFormat ],
308
+ [ italicFormat ],
309
+ [ italicFormat ],
310
+ null,
311
+ null,
312
+ [ linkFormat, italicFormat ], // 6
313
+ [ linkFormat ],
314
+ [ linkFormat, boldFormat ],
315
+ [ linkFormat, boldFormat ],
316
+ [ linkFormat, italicFormat ], // 10
317
+ null,
318
+ null,
319
+ [ boldFormat ],
320
+ [ boldFormat ],
321
+ [ boldFormat ],
322
+ null,
323
+ null,
324
+ ],
325
+
326
+ text: 'Lorem ipsum dolor.',
327
+ start,
328
+ end,
329
+ };
330
+
331
+ expect(
332
+ getFormatBoundary( record, { type: 'core/link' } )
333
+ ).toEqual( {
334
+ start: 6,
335
+ end: 10,
336
+ } );
337
+ }
338
+ );
339
+ } );
340
+
341
+ it( 'should find bounds of a format which starts at zeroth index where start/end pointers are at 0', () => {
342
+ const record = {
343
+ formats: [
344
+ [ linkFormat ], // 0 (th zeroth index)
345
+ [ linkFormat ],
346
+ [ linkFormat ],
347
+ [ linkFormat ],
348
+ [ linkFormat ],
349
+ [ linkFormat ], // 5
350
+ [ boldFormat ],
351
+ [ boldFormat ],
352
+ null,
353
+ null,
354
+ null,
355
+ null,
356
+ null,
357
+ null,
358
+ null,
359
+ [ boldFormat ],
360
+ [ boldFormat ],
361
+ [ boldFormat ],
362
+ null,
363
+ null,
364
+ null,
365
+ null,
366
+ null,
367
+ null,
368
+ null,
369
+ [ italicFormat ],
370
+ [ italicFormat ],
371
+ [ italicFormat ],
372
+ [ italicFormat ],
373
+ [ italicFormat ],
374
+ null,
375
+ ],
376
+
377
+ text: "Lorem ok let's try again dolor.",
378
+ start: 0,
379
+ end: 0,
380
+ };
381
+
382
+ expect( getFormatBoundary( record, { type: 'core/link' } ) ).toEqual( {
383
+ start: 0,
384
+ end: 5,
385
+ } );
386
+ } );
387
+
388
+ describe( 'Single value out of bounds', () => {
389
+ it.each( [
390
+ [ 'start is outside the boundary but end is inside', [ 1, 4 ] ],
391
+ [ 'end is outside the boundary but start is inside', [ 4, 10 ] ],
392
+ [
393
+ 'end is withins bounds but is less than start (inverted)',
394
+ [ 4, 1 ],
395
+ ],
396
+ [ 'start is negative but end is within bounds', [ -1000, 4 ] ],
397
+ [ 'end is negative but start is within bounds', [ 4, -1000 ] ],
398
+ ] )( 'should return bounds when %s', ( _ignored, [ start, end ] ) => {
399
+ const record = {
400
+ formats: [
401
+ [],
402
+ [],
403
+ [],
404
+ [],
405
+ [ boldFormat ], // 4
406
+ [ boldFormat ],
407
+ [ boldFormat ], // 6
408
+ [],
409
+ [],
410
+ ],
411
+ text: 'Lorem ipsum dolor.',
412
+ start,
413
+ end,
414
+ };
415
+
416
+ expect(
417
+ getFormatBoundary( record, { type: 'core/bold' } )
418
+ ).toEqual( {
419
+ start: 4,
420
+ end: 6,
421
+ } );
422
+ } );
423
+ } );
424
+
425
+ it( 'should respect startIndex and endIndex arguments when seeking boundary', () => {
426
+ const startIndex = 1;
427
+ const endIndex = 3;
428
+
429
+ const record = {
430
+ formats: [
431
+ [ boldFormat ],
432
+ [ boldFormat ],
433
+ [ boldFormat ],
434
+ [ boldFormat ],
435
+ [ boldFormat ],
436
+ [],
437
+ [],
438
+ [],
439
+ [],
440
+ [],
441
+ ],
442
+ text: 'Lorem ipsum dolor.',
443
+ start: 10, // the value start is outside the format
444
+ end: 10, // the value end is outside the format
445
+ };
446
+
447
+ expect(
448
+ getFormatBoundary(
449
+ record,
450
+ { type: 'core/bold' },
451
+ startIndex,
452
+ endIndex
453
+ )
454
+ ).toEqual( {
455
+ start: 0,
456
+ end: 4,
457
+ } );
458
+ } );
459
+ } );
package/src/link/utils.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
- import { startsWith } from 'lodash';
4
+ import { startsWith, find, partialRight } from 'lodash';
5
5
 
6
6
  /**
7
7
  * WordPress dependencies
@@ -111,3 +111,134 @@ export function createLinkFormat( { url, type, id, opensInNewWindow } ) {
111
111
 
112
112
  return format;
113
113
  }
114
+
115
+ /* eslint-disable jsdoc/no-undefined-types */
116
+ /**
117
+ * Get the start and end boundaries of a given format from a rich text value.
118
+ *
119
+ *
120
+ * @param {RichTextValue} value the rich text value to interrogate.
121
+ * @param {string} format the identifier for the target format (e.g. `core/link`, `core/bold`).
122
+ * @param {number?} startIndex optional startIndex to seek from.
123
+ * @param {number?} endIndex optional endIndex to seek from.
124
+ * @return {Object} object containing start and end values for the given format.
125
+ */
126
+ /* eslint-enable jsdoc/no-undefined-types */
127
+ export function getFormatBoundary(
128
+ value,
129
+ format,
130
+ startIndex = value.start,
131
+ endIndex = value.end
132
+ ) {
133
+ const EMPTY_BOUNDARIES = {
134
+ start: null,
135
+ end: null,
136
+ };
137
+
138
+ const { formats } = value;
139
+ let targetFormat;
140
+ let initialIndex;
141
+
142
+ if ( ! formats?.length ) {
143
+ return EMPTY_BOUNDARIES;
144
+ }
145
+
146
+ // Clone formats to avoid modifying source formats.
147
+ const newFormats = formats.slice();
148
+
149
+ const formatAtStart = find( newFormats[ startIndex ], {
150
+ type: format.type,
151
+ } );
152
+
153
+ const formatAtEnd = find( newFormats[ endIndex ], {
154
+ type: format.type,
155
+ } );
156
+
157
+ const formatAtEndMinusOne = find( newFormats[ endIndex - 1 ], {
158
+ type: format.type,
159
+ } );
160
+
161
+ if ( !! formatAtStart ) {
162
+ // Set values to conform to "start"
163
+ targetFormat = formatAtStart;
164
+ initialIndex = startIndex;
165
+ } else if ( !! formatAtEnd ) {
166
+ // Set values to conform to "end"
167
+ targetFormat = formatAtEnd;
168
+ initialIndex = endIndex;
169
+ } else if ( !! formatAtEndMinusOne ) {
170
+ // This is an edge case which will occur if you create a format, then place
171
+ // the caret just before the format and hit the back ARROW key. The resulting
172
+ // value object will have start and end +1 beyond the edge of the format boundary.
173
+ targetFormat = formatAtEndMinusOne;
174
+ initialIndex = endIndex - 1;
175
+ } else {
176
+ return EMPTY_BOUNDARIES;
177
+ }
178
+
179
+ const index = newFormats[ initialIndex ].indexOf( targetFormat );
180
+
181
+ const walkingArgs = [ newFormats, initialIndex, targetFormat, index ];
182
+
183
+ // Walk the startIndex "backwards" to the leading "edge" of the matching format.
184
+ startIndex = walkToStart( ...walkingArgs );
185
+
186
+ // Walk the endIndex "forwards" until the trailing "edge" of the matching format.
187
+ endIndex = walkToEnd( ...walkingArgs );
188
+
189
+ // Safe guard: start index cannot be less than 0
190
+ startIndex = startIndex < 0 ? 0 : startIndex;
191
+
192
+ // // Return the indicies of the "edges" as the boundaries.
193
+ return {
194
+ start: startIndex,
195
+ end: endIndex,
196
+ };
197
+ }
198
+
199
+ /**
200
+ * Walks forwards/backwards towards the boundary of a given format within an
201
+ * array of format objects. Returns the index of the boundary.
202
+ *
203
+ * @param {Array} formats the formats to search for the given format type.
204
+ * @param {number} initialIndex the starting index from which to walk.
205
+ * @param {Object} targetFormatRef a reference to the format type object being sought.
206
+ * @param {number} formatIndex the index at which we expect the target format object to be.
207
+ * @param {string} direction either 'forwards' or 'backwards' to indicate the direction.
208
+ * @return {number} the index of the boundary of the given format.
209
+ */
210
+ function walkToBoundary(
211
+ formats,
212
+ initialIndex,
213
+ targetFormatRef,
214
+ formatIndex,
215
+ direction
216
+ ) {
217
+ let index = initialIndex;
218
+
219
+ const directions = {
220
+ forwards: 1,
221
+ backwards: -1,
222
+ };
223
+
224
+ const directionIncrement = directions[ direction ] || 1; // invalid direction arg default to forwards
225
+ const inverseDirectionIncrement = directionIncrement * -1;
226
+
227
+ while (
228
+ formats[ index ] &&
229
+ formats[ index ][ formatIndex ] === targetFormatRef
230
+ ) {
231
+ // Increment/decrement in the direction of operation.
232
+ index = index + directionIncrement;
233
+ }
234
+
235
+ // Restore by one in inverse direction of operation
236
+ // to avoid out of bounds.
237
+ index = index + inverseDirectionIncrement;
238
+
239
+ return index;
240
+ }
241
+
242
+ const walkToStart = partialRight( walkToBoundary, 'backwards' );
243
+
244
+ const walkToEnd = partialRight( walkToBoundary, 'forwards' );