@wordpress/format-library 3.0.4 → 3.0.8

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 (91) hide show
  1. package/build/bold/index.js +13 -8
  2. package/build/bold/index.js.map +1 -1
  3. package/build/code/index.js +12 -8
  4. package/build/code/index.js.map +1 -1
  5. package/build/image/index.js +27 -23
  6. package/build/image/index.js.map +1 -1
  7. package/build/index.js +7 -4
  8. package/build/index.js.map +1 -1
  9. package/build/italic/index.js +13 -8
  10. package/build/italic/index.js.map +1 -1
  11. package/build/keyboard/index.js +12 -8
  12. package/build/keyboard/index.js.map +1 -1
  13. package/build/link/index.js +15 -12
  14. package/build/link/index.js.map +1 -1
  15. package/build/link/inline.js +78 -16
  16. package/build/link/inline.js.map +1 -1
  17. package/build/link/modal-screens/link-picker-screen.native.js +6 -4
  18. package/build/link/modal-screens/link-picker-screen.native.js.map +1 -1
  19. package/build/link/modal-screens/link-settings-screen.native.js +15 -12
  20. package/build/link/modal-screens/link-settings-screen.native.js.map +1 -1
  21. package/build/link/modal.native.js +5 -4
  22. package/build/link/modal.native.js.map +1 -1
  23. package/build/link/use-link-instance-key.js +40 -0
  24. package/build/link/use-link-instance-key.js.map +1 -0
  25. package/build/link/utils.js +122 -7
  26. package/build/link/utils.js.map +1 -1
  27. package/build/strikethrough/index.js +12 -8
  28. package/build/strikethrough/index.js.map +1 -1
  29. package/build/subscript/index.js +12 -8
  30. package/build/subscript/index.js.map +1 -1
  31. package/build/superscript/index.js +12 -8
  32. package/build/superscript/index.js.map +1 -1
  33. package/build/text-color/index.js +20 -14
  34. package/build/text-color/index.js.map +1 -1
  35. package/build/text-color/inline.js +31 -18
  36. package/build/text-color/inline.js.map +1 -1
  37. package/build/underline/index.js +10 -6
  38. package/build/underline/index.js.map +1 -1
  39. package/build-module/bold/index.js +13 -8
  40. package/build-module/bold/index.js.map +1 -1
  41. package/build-module/code/index.js +12 -8
  42. package/build-module/code/index.js.map +1 -1
  43. package/build-module/image/index.js +27 -23
  44. package/build-module/image/index.js.map +1 -1
  45. package/build-module/index.js +7 -4
  46. package/build-module/index.js.map +1 -1
  47. package/build-module/italic/index.js +13 -8
  48. package/build-module/italic/index.js.map +1 -1
  49. package/build-module/keyboard/index.js +12 -8
  50. package/build-module/keyboard/index.js.map +1 -1
  51. package/build-module/link/index.js +15 -12
  52. package/build-module/link/index.js.map +1 -1
  53. package/build-module/link/inline.js +77 -18
  54. package/build-module/link/inline.js.map +1 -1
  55. package/build-module/link/modal-screens/link-picker-screen.native.js +6 -4
  56. package/build-module/link/modal-screens/link-picker-screen.native.js.map +1 -1
  57. package/build-module/link/modal-screens/link-settings-screen.native.js +15 -12
  58. package/build-module/link/modal-screens/link-settings-screen.native.js.map +1 -1
  59. package/build-module/link/modal.native.js +5 -4
  60. package/build-module/link/modal.native.js.map +1 -1
  61. package/build-module/link/use-link-instance-key.js +33 -0
  62. package/build-module/link/use-link-instance-key.js.map +1 -0
  63. package/build-module/link/utils.js +119 -7
  64. package/build-module/link/utils.js.map +1 -1
  65. package/build-module/strikethrough/index.js +12 -8
  66. package/build-module/strikethrough/index.js.map +1 -1
  67. package/build-module/subscript/index.js +12 -8
  68. package/build-module/subscript/index.js.map +1 -1
  69. package/build-module/superscript/index.js +12 -8
  70. package/build-module/superscript/index.js.map +1 -1
  71. package/build-module/text-color/index.js +16 -12
  72. package/build-module/text-color/index.js.map +1 -1
  73. package/build-module/text-color/inline.js +31 -18
  74. package/build-module/text-color/inline.js.map +1 -1
  75. package/build-module/underline/index.js +12 -6
  76. package/build-module/underline/index.js.map +1 -1
  77. package/package.json +16 -16
  78. package/src/bold/index.js +2 -2
  79. package/src/code/index.js +2 -1
  80. package/src/italic/index.js +2 -2
  81. package/src/keyboard/index.js +2 -1
  82. package/src/link/inline.js +75 -5
  83. package/src/link/test/utils.js +362 -1
  84. package/src/link/use-link-instance-key.js +34 -0
  85. package/src/link/utils.js +132 -1
  86. package/src/strikethrough/index.js +2 -1
  87. package/src/subscript/index.js +2 -1
  88. package/src/superscript/index.js +2 -1
  89. package/src/text-color/index.js +1 -0
  90. package/src/text-color/inline.js +12 -1
  91. package/src/underline/index.js +3 -1
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' );
@@ -16,7 +16,7 @@ export const strikethrough = {
16
16
  className: null,
17
17
  edit( { isActive, value, onChange, onFocus } ) {
18
18
  function onClick() {
19
- onChange( toggleFormat( value, { type: name } ) );
19
+ onChange( toggleFormat( value, { type: name, title } ) );
20
20
  onFocus();
21
21
  }
22
22
 
@@ -26,6 +26,7 @@ export const strikethrough = {
26
26
  title={ title }
27
27
  onClick={ onClick }
28
28
  isActive={ isActive }
29
+ role="menuitemcheckbox"
29
30
  />
30
31
  );
31
32
  },
@@ -16,7 +16,7 @@ export const subscript = {
16
16
  className: null,
17
17
  edit( { isActive, value, onChange, onFocus } ) {
18
18
  function onToggle() {
19
- onChange( toggleFormat( value, { type: name } ) );
19
+ onChange( toggleFormat( value, { type: name, title } ) );
20
20
  }
21
21
 
22
22
  function onClick() {
@@ -30,6 +30,7 @@ export const subscript = {
30
30
  title={ title }
31
31
  onClick={ onClick }
32
32
  isActive={ isActive }
33
+ role="menuitemcheckbox"
33
34
  />
34
35
  );
35
36
  },
@@ -16,7 +16,7 @@ export const superscript = {
16
16
  className: null,
17
17
  edit( { isActive, value, onChange, onFocus } ) {
18
18
  function onToggle() {
19
- onChange( toggleFormat( value, { type: name } ) );
19
+ onChange( toggleFormat( value, { type: name, title } ) );
20
20
  }
21
21
 
22
22
  function onClick() {
@@ -30,6 +30,7 @@ export const superscript = {
30
30
  title={ title }
31
31
  onClick={ onClick }
32
32
  isActive={ isActive }
33
+ role="menuitemcheckbox"
33
34
  />
34
35
  );
35
36
  },
@@ -101,6 +101,7 @@ function TextColorEdit( {
101
101
  ? enableIsAddingColor
102
102
  : () => onChange( removeFormat( value, name ) )
103
103
  }
104
+ role="menuitemcheckbox"
104
105
  />
105
106
  { isAddingColor && (
106
107
  <InlineColorUI
@@ -20,6 +20,7 @@ import {
20
20
  getColorObjectByColorValue,
21
21
  getColorObjectByAttributeValues,
22
22
  store as blockEditorStore,
23
+ useCachedTruthy,
23
24
  } from '@wordpress/block-editor';
24
25
  import { Popover, TabPanel } from '@wordpress/components';
25
26
  import { __ } from '@wordpress/i18n';
@@ -139,7 +140,17 @@ export default function InlineColorUI( {
139
140
  onClose,
140
141
  contentRef,
141
142
  } ) {
142
- const anchorRef = useAnchorRef( { ref: contentRef, value, settings } );
143
+ /*
144
+ As you change the text color by typing a HEX value into a field,
145
+ the return value of document.getSelection jumps to the field you're editing,
146
+ not the highlighted text. Given that useAnchorRef uses document.getSelection,
147
+ it will return null, since it can't find the <mark> element within the HEX input.
148
+ This caches the last truthy value of the selection anchor reference.
149
+ */
150
+ const anchorRef = useCachedTruthy(
151
+ useAnchorRef( { ref: contentRef, value, settings } )
152
+ );
153
+
143
154
  return (
144
155
  <Popover
145
156
  onClose={ onClose }
@@ -9,10 +9,11 @@ import {
9
9
  } from '@wordpress/block-editor';
10
10
 
11
11
  const name = 'core/underline';
12
+ const title = __( 'Underline' );
12
13
 
13
14
  export const underline = {
14
15
  name,
15
- title: __( 'Underline' ),
16
+ title,
16
17
  tagName: 'span',
17
18
  className: null,
18
19
  attributes: {
@@ -26,6 +27,7 @@ export const underline = {
26
27
  attributes: {
27
28
  style: 'text-decoration: underline;',
28
29
  },
30
+ title,
29
31
  } )
30
32
  );
31
33
  };