@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
@@ -3,17 +3,18 @@ import { createElement } from "@wordpress/element";
3
3
  /**
4
4
  * WordPress dependencies
5
5
  */
6
- import { useState, useRef } from '@wordpress/element';
7
- import { __ } from '@wordpress/i18n';
6
+ import { useState, useRef, createInterpolateElement } from '@wordpress/element';
7
+ import { __, sprintf } from '@wordpress/i18n';
8
8
  import { withSpokenMessages, Popover } from '@wordpress/components';
9
9
  import { prependHTTP } from '@wordpress/url';
10
- import { create, insert, isCollapsed, applyFormat, useAnchorRef, removeFormat } from '@wordpress/rich-text';
11
- import { __experimentalLinkControl as LinkControl } from '@wordpress/block-editor';
10
+ import { create, insert, isCollapsed, applyFormat, useAnchorRef, removeFormat, slice, replace } from '@wordpress/rich-text';
11
+ import { __experimentalLinkControl as LinkControl, store as blockEditorStore } from '@wordpress/block-editor';
12
+ import { useSelect } from '@wordpress/data';
12
13
  /**
13
14
  * Internal dependencies
14
15
  */
15
16
 
16
- import { createLinkFormat, isValidHref } from './utils';
17
+ import { createLinkFormat, isValidHref, getFormatBoundary } from './utils';
17
18
  import { link as settings } from './index';
18
19
 
19
20
  function InlineLinkUI({
@@ -26,6 +27,9 @@ function InlineLinkUI({
26
27
  stopAddingLink,
27
28
  contentRef
28
29
  }) {
30
+ const richLinkTextValue = getRichTextValueFromSelection(value, isActive); // Get the text content minus any HTML tags.
31
+
32
+ const richTextText = richLinkTextValue.text;
29
33
  /**
30
34
  * Pending settings to be applied to the next link. When inserting a new
31
35
  * link, toggle values cannot be applied immediately, because there is not
@@ -34,12 +38,29 @@ function InlineLinkUI({
34
38
  *
35
39
  * @type {[Object|undefined,Function]}
36
40
  */
41
+
37
42
  const [nextLinkValue, setNextLinkValue] = useState();
43
+ const {
44
+ createPageEntity,
45
+ userCanCreatePages
46
+ } = useSelect(select => {
47
+ const {
48
+ getSettings
49
+ } = select(blockEditorStore);
50
+
51
+ const _settings = getSettings();
52
+
53
+ return {
54
+ createPageEntity: _settings.__experimentalCreatePageEntity,
55
+ userCanCreatePages: _settings.__experimentalUserCanCreatePages
56
+ };
57
+ }, []);
38
58
  const linkValue = {
39
59
  url: activeAttributes.url,
40
60
  type: activeAttributes.type,
41
61
  id: activeAttributes.id,
42
62
  opensInNewTab: activeAttributes.target === '_blank',
63
+ title: richTextText,
43
64
  ...nextLinkValue
44
65
  };
45
66
 
@@ -72,21 +93,45 @@ function InlineLinkUI({
72
93
  }
73
94
 
74
95
  const newUrl = prependHTTP(nextValue.url);
75
- const format = createLinkFormat({
96
+ const linkFormat = createLinkFormat({
76
97
  url: newUrl,
77
98
  type: nextValue.type,
78
99
  id: nextValue.id !== undefined && nextValue.id !== null ? String(nextValue.id) : undefined,
79
100
  opensInNewWindow: nextValue.opensInNewTab
80
101
  });
102
+ const newText = nextValue.title || newUrl;
81
103
 
82
104
  if (isCollapsed(value) && !isActive) {
83
- const newText = nextValue.title || newUrl;
105
+ // Scenario: we don't have any actively selected text or formats.
84
106
  const toInsert = applyFormat(create({
85
107
  text: newText
86
- }), format, 0, newText.length);
108
+ }), linkFormat, 0, newText.length);
87
109
  onChange(insert(value, toInsert));
88
110
  } else {
89
- const newValue = applyFormat(value, format);
111
+ // Scenario: we have any active text selection or an active format
112
+ let newValue;
113
+
114
+ if (newText === richTextText) {
115
+ // If we're not updating the text then ignore
116
+ newValue = applyFormat(value, linkFormat);
117
+ } else {
118
+ // Create new RichText value for the new text in order that we
119
+ // can apply formats to it.
120
+ newValue = create({
121
+ text: newText
122
+ }); // Apply the new Link format to this new text value.
123
+
124
+ newValue = applyFormat(newValue, linkFormat, 0, newText.length); // Update the original (full) RichTextValue replacing the
125
+ // target text with the *new* RichTextValue containing:
126
+ // 1. The new text content.
127
+ // 2. The new link format.
128
+ // Note original formats will be lost when applying this change.
129
+ // That is expected behaviour.
130
+ // See: https://github.com/WordPress/gutenberg/pull/33849#issuecomment-936134179.
131
+
132
+ newValue = replace(value, richTextText, newValue);
133
+ }
134
+
90
135
  newValue.start = newValue.end;
91
136
  newValue.activeFormats = [];
92
137
  onChange(newValue);
@@ -115,6 +160,29 @@ function InlineLinkUI({
115
160
  // otherwise it causes a render of the content.
116
161
 
117
162
  const focusOnMount = useRef(addingLink ? 'firstElement' : false);
163
+
164
+ async function handleCreate(pageTitle) {
165
+ const page = await createPageEntity({
166
+ title: pageTitle,
167
+ status: 'draft'
168
+ });
169
+ return {
170
+ id: page.id,
171
+ type: page.type,
172
+ title: page.title.rendered,
173
+ url: page.link,
174
+ kind: 'post-type'
175
+ };
176
+ }
177
+
178
+ function createButtonText(searchTerm) {
179
+ return createInterpolateElement(sprintf(
180
+ /* translators: %s: search term. */
181
+ __('Create Page: <mark>%s</mark>'), searchTerm), {
182
+ mark: createElement("mark", null)
183
+ });
184
+ }
185
+
118
186
  return createElement(Popover, {
119
187
  anchorRef: anchorRef,
120
188
  focusOnMount: focusOnMount.current,
@@ -125,9 +193,34 @@ function InlineLinkUI({
125
193
  onChange: onChangeLink,
126
194
  onRemove: removeLink,
127
195
  forceIsEditingLink: addingLink,
128
- hasRichPreviews: true
196
+ hasRichPreviews: true,
197
+ createSuggestion: createPageEntity && handleCreate,
198
+ withCreateSuggestion: userCanCreatePages,
199
+ createSuggestionButtonText: createButtonText,
200
+ hasTextControl: true
129
201
  }));
130
202
  }
131
203
 
204
+ function getRichTextValueFromSelection(value, isActive) {
205
+ // Default to the selection ranges on the RichTextValue object.
206
+ let textStart = value.start;
207
+ let textEnd = value.end; // If the format is currently active then the rich text value
208
+ // should always be taken from the bounds of the active format
209
+ // and not the selected text.
210
+
211
+ if (isActive) {
212
+ const boundary = getFormatBoundary(value, {
213
+ type: 'core/link'
214
+ });
215
+ textStart = boundary.start; // Text *selection* always extends +1 beyond the edge of the format.
216
+ // We account for that here.
217
+
218
+ textEnd = boundary.end + 1;
219
+ } // Get a RichTextValue containing the selected text content.
220
+
221
+
222
+ return slice(value, textStart, textEnd);
223
+ }
224
+
132
225
  export default withSpokenMessages(InlineLinkUI);
133
226
  //# sourceMappingURL=inline.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/format-library/src/link/inline.js"],"names":["useState","useRef","__","withSpokenMessages","Popover","prependHTTP","create","insert","isCollapsed","applyFormat","useAnchorRef","removeFormat","__experimentalLinkControl","LinkControl","createLinkFormat","isValidHref","link","settings","InlineLinkUI","isActive","activeAttributes","addingLink","value","onChange","speak","stopAddingLink","contentRef","nextLinkValue","setNextLinkValue","linkValue","url","type","id","opensInNewTab","target","removeLink","newValue","onChangeLink","nextValue","didToggleSetting","didToggleSettingForNewLink","undefined","newUrl","format","String","opensInNewWindow","newText","title","toInsert","text","length","start","end","activeFormats","anchorRef","ref","focusOnMount","current"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,QAAT,EAAmBC,MAAnB,QAAiC,oBAAjC;AACA,SAASC,EAAT,QAAmB,iBAAnB;AACA,SAASC,kBAAT,EAA6BC,OAA7B,QAA4C,uBAA5C;AACA,SAASC,WAAT,QAA4B,gBAA5B;AACA,SACCC,MADD,EAECC,MAFD,EAGCC,WAHD,EAICC,WAJD,EAKCC,YALD,EAMCC,YAND,QAOO,sBAPP;AAQA,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A9B,MAAAA,QAAQ,CAAEa,QAAF,CAAR;AACA,KArDiC,CAuDlC;AACA;;;AACA,QAAK,CAAEG,gBAAP,EAA0B;AACzBd,MAAAA,cAAc;AACd;;AAED,QAAK,CAAEV,WAAW,CAAE2B,MAAF,CAAlB,EAA+B;AAC9BlB,MAAAA,KAAK,CACJtB,EAAE,CACD,0EADC,CADE,EAIJ,WAJI,CAAL;AAMA,KAPD,MAOO,IAAKiB,QAAL,EAAgB;AACtBK,MAAAA,KAAK,CAAEtB,EAAE,CAAE,cAAF,CAAJ,EAAwB,WAAxB,CAAL;AACA,KAFM,MAEA;AACNsB,MAAAA,KAAK,CAAEtB,EAAE,CAAE,gBAAF,CAAJ,EAA0B,WAA1B,CAAL;AACA;AACD;;AAED,QAAMoD,SAAS,GAAG5C,YAAY,CAAE;AAAE6C,IAAAA,GAAG,EAAE7B,UAAP;AAAmBJ,IAAAA,KAAnB;AAA0BL,IAAAA;AAA1B,GAAF,CAA9B,CArGG,CAuGH;AACA;;AACA,QAAMuC,YAAY,GAAGvD,MAAM,CAAEoB,UAAU,GAAG,cAAH,GAAoB,KAAhC,CAA3B;AAEA,SACC,cAAC,OAAD;AACC,IAAA,SAAS,EAAGiC,SADb;AAEC,IAAA,YAAY,EAAGE,YAAY,CAACC,OAF7B;AAGC,IAAA,OAAO,EAAGhC,cAHX;AAIC,IAAA,QAAQ,EAAC;AAJV,KAMC,cAAC,WAAD;AACC,IAAA,KAAK,EAAGI,SADT;AAEC,IAAA,QAAQ,EAAGQ,YAFZ;AAGC,IAAA,QAAQ,EAAGF,UAHZ;AAIC,IAAA,kBAAkB,EAAGd,UAJtB;AAKC,IAAA,eAAe;AALhB,IAND,CADD;AAgBA;;AAED,eAAelB,kBAAkB,CAAEe,YAAF,CAAjC","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useState, useRef } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport { withSpokenMessages, Popover } from '@wordpress/components';\nimport { prependHTTP } from '@wordpress/url';\nimport {\n\tcreate,\n\tinsert,\n\tisCollapsed,\n\tapplyFormat,\n\tuseAnchorRef,\n\tremoveFormat,\n} from '@wordpress/rich-text';\nimport { __experimentalLinkControl as LinkControl } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport { createLinkFormat, isValidHref } from './utils';\nimport { link as settings } from './index';\n\nfunction InlineLinkUI( {\n\tisActive,\n\tactiveAttributes,\n\taddingLink,\n\tvalue,\n\tonChange,\n\tspeak,\n\tstopAddingLink,\n\tcontentRef,\n} ) {\n\t/**\n\t * Pending settings to be applied to the next link. When inserting a new\n\t * link, toggle values cannot be applied immediately, because there is not\n\t * yet a link for them to apply to. Thus, they are maintained in a state\n\t * value until the time that the link can be inserted or edited.\n\t *\n\t * @type {[Object|undefined,Function]}\n\t */\n\tconst [ nextLinkValue, setNextLinkValue ] = useState();\n\n\tconst linkValue = {\n\t\turl: activeAttributes.url,\n\t\ttype: activeAttributes.type,\n\t\tid: activeAttributes.id,\n\t\topensInNewTab: activeAttributes.target === '_blank',\n\t\t...nextLinkValue,\n\t};\n\n\tfunction removeLink() {\n\t\tconst newValue = removeFormat( value, 'core/link' );\n\t\tonChange( newValue );\n\t\tstopAddingLink();\n\t\tspeak( __( 'Link removed.' ), 'assertive' );\n\t}\n\n\tfunction onChangeLink( nextValue ) {\n\t\t// Merge with values from state, both for the purpose of assigning the\n\t\t// next state value, and for use in constructing the new link format if\n\t\t// the link is ready to be applied.\n\t\tnextValue = {\n\t\t\t...nextLinkValue,\n\t\t\t...nextValue,\n\t\t};\n\n\t\t// LinkControl calls `onChange` immediately upon the toggling a setting.\n\t\tconst didToggleSetting =\n\t\t\tlinkValue.opensInNewTab !== nextValue.opensInNewTab &&\n\t\t\tlinkValue.url === nextValue.url;\n\n\t\t// If change handler was called as a result of a settings change during\n\t\t// link insertion, it must be held in state until the link is ready to\n\t\t// be applied.\n\t\tconst didToggleSettingForNewLink =\n\t\t\tdidToggleSetting && nextValue.url === undefined;\n\n\t\t// If link will be assigned, the state value can be considered flushed.\n\t\t// Otherwise, persist the pending changes.\n\t\tsetNextLinkValue( didToggleSettingForNewLink ? nextValue : undefined );\n\n\t\tif ( didToggleSettingForNewLink ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst newUrl = prependHTTP( nextValue.url );\n\t\tconst format = createLinkFormat( {\n\t\t\turl: newUrl,\n\t\t\ttype: nextValue.type,\n\t\t\tid:\n\t\t\t\tnextValue.id !== undefined && nextValue.id !== null\n\t\t\t\t\t? String( nextValue.id )\n\t\t\t\t\t: undefined,\n\t\t\topensInNewWindow: nextValue.opensInNewTab,\n\t\t} );\n\n\t\tif ( isCollapsed( value ) && ! isActive ) {\n\t\t\tconst newText = nextValue.title || newUrl;\n\t\t\tconst toInsert = applyFormat(\n\t\t\t\tcreate( { text: newText } ),\n\t\t\t\tformat,\n\t\t\t\t0,\n\t\t\t\tnewText.length\n\t\t\t);\n\t\t\tonChange( insert( value, toInsert ) );\n\t\t} else {\n\t\t\tconst newValue = applyFormat( value, format );\n\t\t\tnewValue.start = newValue.end;\n\t\t\tnewValue.activeFormats = [];\n\t\t\tonChange( newValue );\n\t\t}\n\n\t\t// Focus should only be shifted back to the formatted segment when the\n\t\t// URL is submitted.\n\t\tif ( ! didToggleSetting ) {\n\t\t\tstopAddingLink();\n\t\t}\n\n\t\tif ( ! isValidHref( newUrl ) ) {\n\t\t\tspeak(\n\t\t\t\t__(\n\t\t\t\t\t'Warning: the link has been inserted but may have errors. Please test it.'\n\t\t\t\t),\n\t\t\t\t'assertive'\n\t\t\t);\n\t\t} else if ( isActive ) {\n\t\t\tspeak( __( 'Link edited.' ), 'assertive' );\n\t\t} else {\n\t\t\tspeak( __( 'Link inserted.' ), 'assertive' );\n\t\t}\n\t}\n\n\tconst anchorRef = useAnchorRef( { ref: contentRef, value, settings } );\n\n\t// The focusOnMount prop shouldn't evolve during render of a Popover\n\t// otherwise it causes a render of the content.\n\tconst focusOnMount = useRef( addingLink ? 'firstElement' : false );\n\n\treturn (\n\t\t<Popover\n\t\t\tanchorRef={ anchorRef }\n\t\t\tfocusOnMount={ focusOnMount.current }\n\t\t\tonClose={ stopAddingLink }\n\t\t\tposition=\"bottom center\"\n\t\t>\n\t\t\t<LinkControl\n\t\t\t\tvalue={ linkValue }\n\t\t\t\tonChange={ onChangeLink }\n\t\t\t\tonRemove={ removeLink }\n\t\t\t\tforceIsEditingLink={ addingLink }\n\t\t\t\thasRichPreviews\n\t\t\t/>\n\t\t</Popover>\n\t);\n}\n\nexport default withSpokenMessages( InlineLinkUI );\n"]}
1
+ {"version":3,"sources":["@wordpress/format-library/src/link/inline.js"],"names":["useState","useRef","createInterpolateElement","__","sprintf","withSpokenMessages","Popover","prependHTTP","create","insert","isCollapsed","applyFormat","useAnchorRef","removeFormat","slice","replace","__experimentalLinkControl","LinkControl","store","blockEditorStore","useSelect","createLinkFormat","isValidHref","getFormatBoundary","link","settings","InlineLinkUI","isActive","activeAttributes","addingLink","value","onChange","speak","stopAddingLink","contentRef","richLinkTextValue","getRichTextValueFromSelection","richTextText","text","nextLinkValue","setNextLinkValue","createPageEntity","userCanCreatePages","select","getSettings","_settings","__experimentalCreatePageEntity","__experimentalUserCanCreatePages","linkValue","url","type","id","opensInNewTab","target","title","removeLink","newValue","onChangeLink","nextValue","didToggleSetting","didToggleSettingForNewLink","undefined","newUrl","linkFormat","String","opensInNewWindow","newText","toInsert","length","start","end","activeFormats","anchorRef","ref","focusOnMount","handleCreate","pageTitle","page","status","rendered","kind","createButtonText","searchTerm","mark","current","textStart","textEnd","boundary"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,QAAT,EAAmBC,MAAnB,EAA2BC,wBAA3B,QAA2D,oBAA3D;AACA,SAASC,EAAT,EAAaC,OAAb,QAA4B,iBAA5B;AACA,SAASC,kBAAT,EAA6BC,OAA7B,QAA4C,uBAA5C;AACA,SAASC,WAAT,QAA4B,gBAA5B;AACA,SACCC,MADD,EAECC,MAFD,EAGCC,WAHD,EAICC,WAJD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,KAN8B,CAA/B;AAQA;;AAED,SACC,cAAC,OAAD;AACC,IAAA,SAAS,EAAGX,SADb;AAEC,IAAA,YAAY,EAAGE,YAAY,CAACU,OAF7B;AAGC,IAAA,OAAO,EAAGnD,cAHX;AAIC,IAAA,QAAQ,EAAC;AAJV,KAMC,cAAC,WAAD;AACC,IAAA,KAAK,EAAGe,SADT;AAEC,IAAA,QAAQ,EAAGS,YAFZ;AAGC,IAAA,QAAQ,EAAGF,UAHZ;AAIC,IAAA,kBAAkB,EAAG1B,UAJtB;AAKC,IAAA,eAAe,MALhB;AAMC,IAAA,gBAAgB,EAAGY,gBAAgB,IAAIkC,YANxC;AAOC,IAAA,oBAAoB,EAAGjC,kBAPxB;AAQC,IAAA,0BAA0B,EAAGuC,gBAR9B;AASC,IAAA,cAAc;AATf,IAND,CADD;AAoBA;;AAED,SAAS7C,6BAAT,CAAwCN,KAAxC,EAA+CH,QAA/C,EAA0D;AACzD;AACA,MAAI0D,SAAS,GAAGvD,KAAK,CAACuC,KAAtB;AACA,MAAIiB,OAAO,GAAGxD,KAAK,CAACwC,GAApB,CAHyD,CAKzD;AACA;AACA;;AACA,MAAK3C,QAAL,EAAgB;AACf,UAAM4D,QAAQ,GAAGhE,iBAAiB,CAAEO,KAAF,EAAS;AAC1CoB,MAAAA,IAAI,EAAE;AADoC,KAAT,CAAlC;AAIAmC,IAAAA,SAAS,GAAGE,QAAQ,CAAClB,KAArB,CALe,CAOf;AACA;;AACAiB,IAAAA,OAAO,GAAGC,QAAQ,CAACjB,GAAT,GAAe,CAAzB;AACA,GAlBwD,CAoBzD;;;AACA,SAAOxD,KAAK,CAAEgB,KAAF,EAASuD,SAAT,EAAoBC,OAApB,CAAZ;AACA;;AAED,eAAejF,kBAAkB,CAAEqB,YAAF,CAAjC","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useState, useRef, createInterpolateElement } from '@wordpress/element';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { withSpokenMessages, Popover } from '@wordpress/components';\nimport { prependHTTP } from '@wordpress/url';\nimport {\n\tcreate,\n\tinsert,\n\tisCollapsed,\n\tapplyFormat,\n\tuseAnchorRef,\n\tremoveFormat,\n\tslice,\n\treplace,\n} from '@wordpress/rich-text';\nimport {\n\t__experimentalLinkControl as LinkControl,\n\tstore as blockEditorStore,\n} from '@wordpress/block-editor';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { createLinkFormat, isValidHref, getFormatBoundary } from './utils';\nimport { link as settings } from './index';\n\nfunction InlineLinkUI( {\n\tisActive,\n\tactiveAttributes,\n\taddingLink,\n\tvalue,\n\tonChange,\n\tspeak,\n\tstopAddingLink,\n\tcontentRef,\n} ) {\n\tconst richLinkTextValue = getRichTextValueFromSelection( value, isActive );\n\n\t// Get the text content minus any HTML tags.\n\tconst richTextText = richLinkTextValue.text;\n\n\t/**\n\t * Pending settings to be applied to the next link. When inserting a new\n\t * link, toggle values cannot be applied immediately, because there is not\n\t * yet a link for them to apply to. Thus, they are maintained in a state\n\t * value until the time that the link can be inserted or edited.\n\t *\n\t * @type {[Object|undefined,Function]}\n\t */\n\tconst [ nextLinkValue, setNextLinkValue ] = useState();\n\n\tconst { createPageEntity, userCanCreatePages } = useSelect( ( select ) => {\n\t\tconst { getSettings } = select( blockEditorStore );\n\t\tconst _settings = getSettings();\n\n\t\treturn {\n\t\t\tcreatePageEntity: _settings.__experimentalCreatePageEntity,\n\t\t\tuserCanCreatePages: _settings.__experimentalUserCanCreatePages,\n\t\t};\n\t}, [] );\n\n\tconst linkValue = {\n\t\turl: activeAttributes.url,\n\t\ttype: activeAttributes.type,\n\t\tid: activeAttributes.id,\n\t\topensInNewTab: activeAttributes.target === '_blank',\n\t\ttitle: richTextText,\n\t\t...nextLinkValue,\n\t};\n\n\tfunction removeLink() {\n\t\tconst newValue = removeFormat( value, 'core/link' );\n\t\tonChange( newValue );\n\t\tstopAddingLink();\n\t\tspeak( __( 'Link removed.' ), 'assertive' );\n\t}\n\n\tfunction onChangeLink( nextValue ) {\n\t\t// Merge with values from state, both for the purpose of assigning the\n\t\t// next state value, and for use in constructing the new link format if\n\t\t// the link is ready to be applied.\n\t\tnextValue = {\n\t\t\t...nextLinkValue,\n\t\t\t...nextValue,\n\t\t};\n\n\t\t// LinkControl calls `onChange` immediately upon the toggling a setting.\n\t\tconst didToggleSetting =\n\t\t\tlinkValue.opensInNewTab !== nextValue.opensInNewTab &&\n\t\t\tlinkValue.url === nextValue.url;\n\n\t\t// If change handler was called as a result of a settings change during\n\t\t// link insertion, it must be held in state until the link is ready to\n\t\t// be applied.\n\t\tconst didToggleSettingForNewLink =\n\t\t\tdidToggleSetting && nextValue.url === undefined;\n\n\t\t// If link will be assigned, the state value can be considered flushed.\n\t\t// Otherwise, persist the pending changes.\n\t\tsetNextLinkValue( didToggleSettingForNewLink ? nextValue : undefined );\n\n\t\tif ( didToggleSettingForNewLink ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst newUrl = prependHTTP( nextValue.url );\n\t\tconst linkFormat = createLinkFormat( {\n\t\t\turl: newUrl,\n\t\t\ttype: nextValue.type,\n\t\t\tid:\n\t\t\t\tnextValue.id !== undefined && nextValue.id !== null\n\t\t\t\t\t? String( nextValue.id )\n\t\t\t\t\t: undefined,\n\t\t\topensInNewWindow: nextValue.opensInNewTab,\n\t\t} );\n\n\t\tconst newText = nextValue.title || newUrl;\n\t\tif ( isCollapsed( value ) && ! isActive ) {\n\t\t\t// Scenario: we don't have any actively selected text or formats.\n\t\t\tconst toInsert = applyFormat(\n\t\t\t\tcreate( { text: newText } ),\n\t\t\t\tlinkFormat,\n\t\t\t\t0,\n\t\t\t\tnewText.length\n\t\t\t);\n\t\t\tonChange( insert( value, toInsert ) );\n\t\t} else {\n\t\t\t// Scenario: we have any active text selection or an active format\n\t\t\tlet newValue;\n\n\t\t\tif ( newText === richTextText ) {\n\t\t\t\t// If we're not updating the text then ignore\n\t\t\t\tnewValue = applyFormat( value, linkFormat );\n\t\t\t} else {\n\t\t\t\t// Create new RichText value for the new text in order that we\n\t\t\t\t// can apply formats to it.\n\t\t\t\tnewValue = create( { text: newText } );\n\n\t\t\t\t// Apply the new Link format to this new text value.\n\t\t\t\tnewValue = applyFormat(\n\t\t\t\t\tnewValue,\n\t\t\t\t\tlinkFormat,\n\t\t\t\t\t0,\n\t\t\t\t\tnewText.length\n\t\t\t\t);\n\n\t\t\t\t// Update the original (full) RichTextValue replacing the\n\t\t\t\t// target text with the *new* RichTextValue containing:\n\t\t\t\t// 1. The new text content.\n\t\t\t\t// 2. The new link format.\n\t\t\t\t// Note original formats will be lost when applying this change.\n\t\t\t\t// That is expected behaviour.\n\t\t\t\t// See: https://github.com/WordPress/gutenberg/pull/33849#issuecomment-936134179.\n\t\t\t\tnewValue = replace( value, richTextText, newValue );\n\t\t\t}\n\n\t\t\tnewValue.start = newValue.end;\n\t\t\tnewValue.activeFormats = [];\n\t\t\tonChange( newValue );\n\t\t}\n\n\t\t// Focus should only be shifted back to the formatted segment when the\n\t\t// URL is submitted.\n\t\tif ( ! didToggleSetting ) {\n\t\t\tstopAddingLink();\n\t\t}\n\n\t\tif ( ! isValidHref( newUrl ) ) {\n\t\t\tspeak(\n\t\t\t\t__(\n\t\t\t\t\t'Warning: the link has been inserted but may have errors. Please test it.'\n\t\t\t\t),\n\t\t\t\t'assertive'\n\t\t\t);\n\t\t} else if ( isActive ) {\n\t\t\tspeak( __( 'Link edited.' ), 'assertive' );\n\t\t} else {\n\t\t\tspeak( __( 'Link inserted.' ), 'assertive' );\n\t\t}\n\t}\n\n\tconst anchorRef = useAnchorRef( { ref: contentRef, value, settings } );\n\n\t// The focusOnMount prop shouldn't evolve during render of a Popover\n\t// otherwise it causes a render of the content.\n\tconst focusOnMount = useRef( addingLink ? 'firstElement' : false );\n\n\tasync function handleCreate( pageTitle ) {\n\t\tconst page = await createPageEntity( {\n\t\t\ttitle: pageTitle,\n\t\t\tstatus: 'draft',\n\t\t} );\n\n\t\treturn {\n\t\t\tid: page.id,\n\t\t\ttype: page.type,\n\t\t\ttitle: page.title.rendered,\n\t\t\turl: page.link,\n\t\t\tkind: 'post-type',\n\t\t};\n\t}\n\n\tfunction createButtonText( searchTerm ) {\n\t\treturn createInterpolateElement(\n\t\t\tsprintf(\n\t\t\t\t/* translators: %s: search term. */\n\t\t\t\t__( 'Create Page: <mark>%s</mark>' ),\n\t\t\t\tsearchTerm\n\t\t\t),\n\t\t\t{ mark: <mark /> }\n\t\t);\n\t}\n\n\treturn (\n\t\t<Popover\n\t\t\tanchorRef={ anchorRef }\n\t\t\tfocusOnMount={ focusOnMount.current }\n\t\t\tonClose={ stopAddingLink }\n\t\t\tposition=\"bottom center\"\n\t\t>\n\t\t\t<LinkControl\n\t\t\t\tvalue={ linkValue }\n\t\t\t\tonChange={ onChangeLink }\n\t\t\t\tonRemove={ removeLink }\n\t\t\t\tforceIsEditingLink={ addingLink }\n\t\t\t\thasRichPreviews\n\t\t\t\tcreateSuggestion={ createPageEntity && handleCreate }\n\t\t\t\twithCreateSuggestion={ userCanCreatePages }\n\t\t\t\tcreateSuggestionButtonText={ createButtonText }\n\t\t\t\thasTextControl\n\t\t\t/>\n\t\t</Popover>\n\t);\n}\n\nfunction getRichTextValueFromSelection( value, isActive ) {\n\t// Default to the selection ranges on the RichTextValue object.\n\tlet textStart = value.start;\n\tlet textEnd = value.end;\n\n\t// If the format is currently active then the rich text value\n\t// should always be taken from the bounds of the active format\n\t// and not the selected text.\n\tif ( isActive ) {\n\t\tconst boundary = getFormatBoundary( value, {\n\t\t\ttype: 'core/link',\n\t\t} );\n\n\t\ttextStart = boundary.start;\n\n\t\t// Text *selection* always extends +1 beyond the edge of the format.\n\t\t// We account for that here.\n\t\ttextEnd = boundary.end + 1;\n\t}\n\n\t// Get a RichTextValue containing the selected text content.\n\treturn slice( value, textStart, textEnd );\n}\n\nexport default withSpokenMessages( InlineLinkUI );\n"]}
@@ -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
  * WordPress dependencies
7
7
  */
@@ -106,4 +106,113 @@ export function createLinkFormat({
106
106
 
107
107
  return format;
108
108
  }
109
+ /* eslint-disable jsdoc/no-undefined-types */
110
+
111
+ /**
112
+ * Get the start and end boundaries of a given format from a rich text value.
113
+ *
114
+ *
115
+ * @param {RichTextValue} value the rich text value to interrogate.
116
+ * @param {string} format the identifier for the target format (e.g. `core/link`, `core/bold`).
117
+ * @param {number?} startIndex optional startIndex to seek from.
118
+ * @param {number?} endIndex optional endIndex to seek from.
119
+ * @return {Object} object containing start and end values for the given format.
120
+ */
121
+
122
+ /* eslint-enable jsdoc/no-undefined-types */
123
+
124
+ export function getFormatBoundary(value, format, startIndex = value.start, endIndex = value.end) {
125
+ const EMPTY_BOUNDARIES = {
126
+ start: null,
127
+ end: null
128
+ };
129
+ const {
130
+ formats
131
+ } = value;
132
+ let targetFormat;
133
+ let initialIndex;
134
+
135
+ if (!(formats !== null && formats !== void 0 && formats.length)) {
136
+ return EMPTY_BOUNDARIES;
137
+ } // Clone formats to avoid modifying source formats.
138
+
139
+
140
+ const newFormats = formats.slice();
141
+ const formatAtStart = find(newFormats[startIndex], {
142
+ type: format.type
143
+ });
144
+ const formatAtEnd = find(newFormats[endIndex], {
145
+ type: format.type
146
+ });
147
+ const formatAtEndMinusOne = find(newFormats[endIndex - 1], {
148
+ type: format.type
149
+ });
150
+
151
+ if (!!formatAtStart) {
152
+ // Set values to conform to "start"
153
+ targetFormat = formatAtStart;
154
+ initialIndex = startIndex;
155
+ } else if (!!formatAtEnd) {
156
+ // Set values to conform to "end"
157
+ targetFormat = formatAtEnd;
158
+ initialIndex = endIndex;
159
+ } else if (!!formatAtEndMinusOne) {
160
+ // This is an edge case which will occur if you create a format, then place
161
+ // the caret just before the format and hit the back ARROW key. The resulting
162
+ // value object will have start and end +1 beyond the edge of the format boundary.
163
+ targetFormat = formatAtEndMinusOne;
164
+ initialIndex = endIndex - 1;
165
+ } else {
166
+ return EMPTY_BOUNDARIES;
167
+ }
168
+
169
+ const index = newFormats[initialIndex].indexOf(targetFormat);
170
+ const walkingArgs = [newFormats, initialIndex, targetFormat, index]; // Walk the startIndex "backwards" to the leading "edge" of the matching format.
171
+
172
+ startIndex = walkToStart(...walkingArgs); // Walk the endIndex "forwards" until the trailing "edge" of the matching format.
173
+
174
+ endIndex = walkToEnd(...walkingArgs); // Safe guard: start index cannot be less than 0
175
+
176
+ startIndex = startIndex < 0 ? 0 : startIndex; // // Return the indicies of the "edges" as the boundaries.
177
+
178
+ return {
179
+ start: startIndex,
180
+ end: endIndex
181
+ };
182
+ }
183
+ /**
184
+ * Walks forwards/backwards towards the boundary of a given format within an
185
+ * array of format objects. Returns the index of the boundary.
186
+ *
187
+ * @param {Array} formats the formats to search for the given format type.
188
+ * @param {number} initialIndex the starting index from which to walk.
189
+ * @param {Object} targetFormatRef a reference to the format type object being sought.
190
+ * @param {number} formatIndex the index at which we expect the target format object to be.
191
+ * @param {string} direction either 'forwards' or 'backwards' to indicate the direction.
192
+ * @return {number} the index of the boundary of the given format.
193
+ */
194
+
195
+ function walkToBoundary(formats, initialIndex, targetFormatRef, formatIndex, direction) {
196
+ let index = initialIndex;
197
+ const directions = {
198
+ forwards: 1,
199
+ backwards: -1
200
+ };
201
+ const directionIncrement = directions[direction] || 1; // invalid direction arg default to forwards
202
+
203
+ const inverseDirectionIncrement = directionIncrement * -1;
204
+
205
+ while (formats[index] && formats[index][formatIndex] === targetFormatRef) {
206
+ // Increment/decrement in the direction of operation.
207
+ index = index + directionIncrement;
208
+ } // Restore by one in inverse direction of operation
209
+ // to avoid out of bounds.
210
+
211
+
212
+ index = index + inverseDirectionIncrement;
213
+ return index;
214
+ }
215
+
216
+ const walkToStart = partialRight(walkToBoundary, 'backwards');
217
+ const walkToEnd = partialRight(walkToBoundary, 'forwards');
109
218
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/format-library/src/link/utils.js"],"names":["startsWith","getProtocol","isValidProtocol","getAuthority","isValidAuthority","getPath","isValidPath","getQueryString","isValidQueryString","getFragment","isValidFragment","isValidHref","href","trimmedHref","trim","test","protocol","authority","path","queryString","fragment","createLinkFormat","url","type","id","opensInNewWindow","format","attributes","target","rel"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAT,QAA2B,QAA3B;AAEA;AACA;AACA;;AACA,SACCC,WADD,EAECC,eAFD,EAGCC,YAHD,EAICC,gBAJD,EAKCC,OALD,EAMCC,WAND,EAOCC,cAPD,EAQCC,kBARD,EASCC,WATD,EAUCC,eAVD,QAWO,gBAXP;AAaA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,WAAT,CAAsBC,IAAtB,EAA6B;AACnC,MAAK,CAAEA,IAAP,EAAc;AACb,WAAO,KAAP;AACA;;AAED,QAAMC,WAAW,GAAGD,IAAI,CAACE,IAAL,EAApB;;AAEA,MAAK,CAAED,WAAP,EAAqB;AACpB,WAAO,KAAP;AACA,GATkC,CAWnC;;;AACA,MAAK,QAAQE,IAAR,CAAcF,WAAd,CAAL,EAAmC;AAClC,UAAMG,QAAQ,GAAGf,WAAW,CAAEY,WAAF,CAA5B;;AACA,QAAK,CAAEX,eAAe,CAAEc,QAAF,CAAtB,EAAqC;AACpC,aAAO,KAAP;AACA,KAJiC,CAMlC;AACA;;;AACA,QACChB,UAAU,CAAEgB,QAAF,EAAY,MAAZ,CAAV,IACA,CAAE,uBAAuBD,IAAvB,CAA6BF,WAA7B,CAFH,EAGE;AACD,aAAO,KAAP;AACA;;AAED,UAAMI,SAAS,GAAGd,YAAY,CAAEU,WAAF,CAA9B;;AACA,QAAK,CAAET,gBAAgB,CAAEa,SAAF,CAAvB,EAAuC;AACtC,aAAO,KAAP;AACA;;AAED,UAAMC,IAAI,GAAGb,OAAO,CAAEQ,WAAF,CAApB;;AACA,QAAKK,IAAI,IAAI,CAAEZ,WAAW,CAAEY,IAAF,CAA1B,EAAqC;AACpC,aAAO,KAAP;AACA;;AAED,UAAMC,WAAW,GAAGZ,cAAc,CAAEM,WAAF,CAAlC;;AACA,QAAKM,WAAW,IAAI,CAAEX,kBAAkB,CAAEW,WAAF,CAAxC,EAA0D;AACzD,aAAO,KAAP;AACA;;AAED,UAAMC,QAAQ,GAAGX,WAAW,CAAEI,WAAF,CAA5B;;AACA,QAAKO,QAAQ,IAAI,CAAEV,eAAe,CAAEU,QAAF,CAAlC,EAAiD;AAChD,aAAO,KAAP;AACA;AACD,GA9CkC,CAgDnC;;;AACA,MAAKpB,UAAU,CAAEa,WAAF,EAAe,GAAf,CAAV,IAAkC,CAAEH,eAAe,CAAEG,WAAF,CAAxD,EAA0E;AACzE,WAAO,KAAP;AACA;;AAED,SAAO,IAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASQ,gBAAT,CAA2B;AAAEC,EAAAA,GAAF;AAAOC,EAAAA,IAAP;AAAaC,EAAAA,EAAb;AAAiBC,EAAAA;AAAjB,CAA3B,EAAiE;AACvE,QAAMC,MAAM,GAAG;AACdH,IAAAA,IAAI,EAAE,WADQ;AAEdI,IAAAA,UAAU,EAAE;AACXL,MAAAA;AADW;AAFE,GAAf;AAOA,MAAKC,IAAL,EAAYG,MAAM,CAACC,UAAP,CAAkBJ,IAAlB,GAAyBA,IAAzB;AACZ,MAAKC,EAAL,EAAUE,MAAM,CAACC,UAAP,CAAkBH,EAAlB,GAAuBA,EAAvB;;AAEV,MAAKC,gBAAL,EAAwB;AACvBC,IAAAA,MAAM,CAACC,UAAP,CAAkBC,MAAlB,GAA2B,QAA3B;AACAF,IAAAA,MAAM,CAACC,UAAP,CAAkBE,GAAlB,GAAwB,qBAAxB;AACA;;AAED,SAAOH,MAAP;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { startsWith } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tgetProtocol,\n\tisValidProtocol,\n\tgetAuthority,\n\tisValidAuthority,\n\tgetPath,\n\tisValidPath,\n\tgetQueryString,\n\tisValidQueryString,\n\tgetFragment,\n\tisValidFragment,\n} from '@wordpress/url';\n\n/**\n * Check for issues with the provided href.\n *\n * @param {string} href The href.\n *\n * @return {boolean} Is the href invalid?\n */\nexport function isValidHref( href ) {\n\tif ( ! href ) {\n\t\treturn false;\n\t}\n\n\tconst trimmedHref = href.trim();\n\n\tif ( ! trimmedHref ) {\n\t\treturn false;\n\t}\n\n\t// Does the href start with something that looks like a URL protocol?\n\tif ( /^\\S+:/.test( trimmedHref ) ) {\n\t\tconst protocol = getProtocol( trimmedHref );\n\t\tif ( ! isValidProtocol( protocol ) ) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Add some extra checks for http(s) URIs, since these are the most common use-case.\n\t\t// This ensures URIs with an http protocol have exactly two forward slashes following the protocol.\n\t\tif (\n\t\t\tstartsWith( protocol, 'http' ) &&\n\t\t\t! /^https?:\\/\\/[^\\/\\s]/i.test( trimmedHref )\n\t\t) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst authority = getAuthority( trimmedHref );\n\t\tif ( ! isValidAuthority( authority ) ) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst path = getPath( trimmedHref );\n\t\tif ( path && ! isValidPath( path ) ) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst queryString = getQueryString( trimmedHref );\n\t\tif ( queryString && ! isValidQueryString( queryString ) ) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst fragment = getFragment( trimmedHref );\n\t\tif ( fragment && ! isValidFragment( fragment ) ) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t// Validate anchor links.\n\tif ( startsWith( trimmedHref, '#' ) && ! isValidFragment( trimmedHref ) ) {\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n\n/**\n * Generates the format object that will be applied to the link text.\n *\n * @param {Object} options\n * @param {string} options.url The href of the link.\n * @param {string} options.type The type of the link.\n * @param {string} options.id The ID of the link.\n * @param {boolean} options.opensInNewWindow Whether this link will open in a new window.\n *\n * @return {Object} The final format object.\n */\nexport function createLinkFormat( { url, type, id, opensInNewWindow } ) {\n\tconst format = {\n\t\ttype: 'core/link',\n\t\tattributes: {\n\t\t\turl,\n\t\t},\n\t};\n\n\tif ( type ) format.attributes.type = type;\n\tif ( id ) format.attributes.id = id;\n\n\tif ( opensInNewWindow ) {\n\t\tformat.attributes.target = '_blank';\n\t\tformat.attributes.rel = 'noreferrer noopener';\n\t}\n\n\treturn format;\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/format-library/src/link/utils.js"],"names":["startsWith","find","partialRight","getProtocol","isValidProtocol","getAuthority","isValidAuthority","getPath","isValidPath","getQueryString","isValidQueryString","getFragment","isValidFragment","isValidHref","href","trimmedHref","trim","test","protocol","authority","path","queryString","fragment","createLinkFormat","url","type","id","opensInNewWindow","format","attributes","target","rel","getFormatBoundary","value","startIndex","start","endIndex","end","EMPTY_BOUNDARIES","formats","targetFormat","initialIndex","length","newFormats","slice","formatAtStart","formatAtEnd","formatAtEndMinusOne","index","indexOf","walkingArgs","walkToStart","walkToEnd","walkToBoundary","targetFormatRef","formatIndex","direction","directions","forwards","backwards","directionIncrement","inverseDirectionIncrement"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAT,EAAqBC,IAArB,EAA2BC,YAA3B,QAA+C,QAA/C;AAEA;AACA;AACA;;AACA,SACCC,WADD,EAECC,eAFD,EAGCC,YAHD,EAICC,gBAJD,EAKCC,OALD,EAMCC,WAND,EAOCC,cAPD,EAQCC,kBARD,EASCC,WATD,EAUCC,eAVD,QAWO,gBAXP;AAaA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,WAAT,CAAsBC,IAAtB,EAA6B;AACnC,MAAK,CAAEA,IAAP,EAAc;AACb,WAAO,KAAP;AACA;;AAED,QAAMC,WAAW,GAAGD,IAAI,CAACE,IAAL,EAApB;;AAEA,MAAK,CAAED,WAAP,EAAqB;AACpB,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;AACA,SAASiB,cAAT,CACCd,OADD,EAECE,YAFD,EAGCa,eAHD,EAICC,WAJD,EAKCC,SALD,EAME;AACD,MAAIR,KAAK,GAAGP,YAAZ;AAEA,QAAMgB,UAAU,GAAG;AAClBC,IAAAA,QAAQ,EAAE,CADQ;AAElBC,IAAAA,SAAS,EAAE,CAAC;AAFM,GAAnB;AAKA,QAAMC,kBAAkB,GAAGH,UAAU,CAAED,SAAF,CAAV,IAA2B,CAAtD,CARC,CAQwD;;AACzD,QAAMK,yBAAyB,GAAGD,kBAAkB,GAAG,CAAC,CAAxD;;AAEA,SACCrB,OAAO,CAAES,KAAF,CAAP,IACAT,OAAO,CAAES,KAAF,CAAP,CAAkBO,WAAlB,MAAoCD,eAFrC,EAGE;AACD;AACAN,IAAAA,KAAK,GAAGA,KAAK,GAAGY,kBAAhB;AACA,GAjBA,CAmBD;AACA;;;AACAZ,EAAAA,KAAK,GAAGA,KAAK,GAAGa,yBAAhB;AAEA,SAAOb,KAAP;AACA;;AAED,MAAMG,WAAW,GAAGjD,YAAY,CAAEmD,cAAF,EAAkB,WAAlB,CAAhC;AAEA,MAAMD,SAAS,GAAGlD,YAAY,CAAEmD,cAAF,EAAkB,UAAlB,CAA9B","sourcesContent":["/**\n * External dependencies\n */\nimport { startsWith, find, partialRight } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tgetProtocol,\n\tisValidProtocol,\n\tgetAuthority,\n\tisValidAuthority,\n\tgetPath,\n\tisValidPath,\n\tgetQueryString,\n\tisValidQueryString,\n\tgetFragment,\n\tisValidFragment,\n} from '@wordpress/url';\n\n/**\n * Check for issues with the provided href.\n *\n * @param {string} href The href.\n *\n * @return {boolean} Is the href invalid?\n */\nexport function isValidHref( href ) {\n\tif ( ! href ) {\n\t\treturn false;\n\t}\n\n\tconst trimmedHref = href.trim();\n\n\tif ( ! trimmedHref ) {\n\t\treturn false;\n\t}\n\n\t// Does the href start with something that looks like a URL protocol?\n\tif ( /^\\S+:/.test( trimmedHref ) ) {\n\t\tconst protocol = getProtocol( trimmedHref );\n\t\tif ( ! isValidProtocol( protocol ) ) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Add some extra checks for http(s) URIs, since these are the most common use-case.\n\t\t// This ensures URIs with an http protocol have exactly two forward slashes following the protocol.\n\t\tif (\n\t\t\tstartsWith( protocol, 'http' ) &&\n\t\t\t! /^https?:\\/\\/[^\\/\\s]/i.test( trimmedHref )\n\t\t) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst authority = getAuthority( trimmedHref );\n\t\tif ( ! isValidAuthority( authority ) ) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst path = getPath( trimmedHref );\n\t\tif ( path && ! isValidPath( path ) ) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst queryString = getQueryString( trimmedHref );\n\t\tif ( queryString && ! isValidQueryString( queryString ) ) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst fragment = getFragment( trimmedHref );\n\t\tif ( fragment && ! isValidFragment( fragment ) ) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t// Validate anchor links.\n\tif ( startsWith( trimmedHref, '#' ) && ! isValidFragment( trimmedHref ) ) {\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n\n/**\n * Generates the format object that will be applied to the link text.\n *\n * @param {Object} options\n * @param {string} options.url The href of the link.\n * @param {string} options.type The type of the link.\n * @param {string} options.id The ID of the link.\n * @param {boolean} options.opensInNewWindow Whether this link will open in a new window.\n *\n * @return {Object} The final format object.\n */\nexport function createLinkFormat( { url, type, id, opensInNewWindow } ) {\n\tconst format = {\n\t\ttype: 'core/link',\n\t\tattributes: {\n\t\t\turl,\n\t\t},\n\t};\n\n\tif ( type ) format.attributes.type = type;\n\tif ( id ) format.attributes.id = id;\n\n\tif ( opensInNewWindow ) {\n\t\tformat.attributes.target = '_blank';\n\t\tformat.attributes.rel = 'noreferrer noopener';\n\t}\n\n\treturn format;\n}\n\n/* eslint-disable jsdoc/no-undefined-types */\n/**\n * Get the start and end boundaries of a given format from a rich text value.\n *\n *\n * @param {RichTextValue} value the rich text value to interrogate.\n * @param {string} format the identifier for the target format (e.g. `core/link`, `core/bold`).\n * @param {number?} startIndex optional startIndex to seek from.\n * @param {number?} endIndex optional endIndex to seek from.\n * @return {Object}\tobject containing start and end values for the given format.\n */\n/* eslint-enable jsdoc/no-undefined-types */\nexport function getFormatBoundary(\n\tvalue,\n\tformat,\n\tstartIndex = value.start,\n\tendIndex = value.end\n) {\n\tconst EMPTY_BOUNDARIES = {\n\t\tstart: null,\n\t\tend: null,\n\t};\n\n\tconst { formats } = value;\n\tlet targetFormat;\n\tlet initialIndex;\n\n\tif ( ! formats?.length ) {\n\t\treturn EMPTY_BOUNDARIES;\n\t}\n\n\t// Clone formats to avoid modifying source formats.\n\tconst newFormats = formats.slice();\n\n\tconst formatAtStart = find( newFormats[ startIndex ], {\n\t\ttype: format.type,\n\t} );\n\n\tconst formatAtEnd = find( newFormats[ endIndex ], {\n\t\ttype: format.type,\n\t} );\n\n\tconst formatAtEndMinusOne = find( newFormats[ endIndex - 1 ], {\n\t\ttype: format.type,\n\t} );\n\n\tif ( !! formatAtStart ) {\n\t\t// Set values to conform to \"start\"\n\t\ttargetFormat = formatAtStart;\n\t\tinitialIndex = startIndex;\n\t} else if ( !! formatAtEnd ) {\n\t\t// Set values to conform to \"end\"\n\t\ttargetFormat = formatAtEnd;\n\t\tinitialIndex = endIndex;\n\t} else if ( !! formatAtEndMinusOne ) {\n\t\t// This is an edge case which will occur if you create a format, then place\n\t\t// the caret just before the format and hit the back ARROW key. The resulting\n\t\t// value object will have start and end +1 beyond the edge of the format boundary.\n\t\ttargetFormat = formatAtEndMinusOne;\n\t\tinitialIndex = endIndex - 1;\n\t} else {\n\t\treturn EMPTY_BOUNDARIES;\n\t}\n\n\tconst index = newFormats[ initialIndex ].indexOf( targetFormat );\n\n\tconst walkingArgs = [ newFormats, initialIndex, targetFormat, index ];\n\n\t// Walk the startIndex \"backwards\" to the leading \"edge\" of the matching format.\n\tstartIndex = walkToStart( ...walkingArgs );\n\n\t// Walk the endIndex \"forwards\" until the trailing \"edge\" of the matching format.\n\tendIndex = walkToEnd( ...walkingArgs );\n\n\t// Safe guard: start index cannot be less than 0\n\tstartIndex = startIndex < 0 ? 0 : startIndex;\n\n\t// // Return the indicies of the \"edges\" as the boundaries.\n\treturn {\n\t\tstart: startIndex,\n\t\tend: endIndex,\n\t};\n}\n\n/**\n * Walks forwards/backwards towards the boundary of a given format within an\n * array of format objects. Returns the index of the boundary.\n *\n * @param {Array} formats the formats to search for the given format type.\n * @param {number} initialIndex the starting index from which to walk.\n * @param {Object} targetFormatRef a reference to the format type object being sought.\n * @param {number} formatIndex the index at which we expect the target format object to be.\n * @param {string} direction either 'forwards' or 'backwards' to indicate the direction.\n * @return {number} the index of the boundary of the given format.\n */\nfunction walkToBoundary(\n\tformats,\n\tinitialIndex,\n\ttargetFormatRef,\n\tformatIndex,\n\tdirection\n) {\n\tlet index = initialIndex;\n\n\tconst directions = {\n\t\tforwards: 1,\n\t\tbackwards: -1,\n\t};\n\n\tconst directionIncrement = directions[ direction ] || 1; // invalid direction arg default to forwards\n\tconst inverseDirectionIncrement = directionIncrement * -1;\n\n\twhile (\n\t\tformats[ index ] &&\n\t\tformats[ index ][ formatIndex ] === targetFormatRef\n\t) {\n\t\t// Increment/decrement in the direction of operation.\n\t\tindex = index + directionIncrement;\n\t}\n\n\t// Restore by one in inverse direction of operation\n\t// to avoid out of bounds.\n\tindex = index + inverseDirectionIncrement;\n\n\treturn index;\n}\n\nconst walkToStart = partialRight( walkToBoundary, 'backwards' );\n\nconst walkToEnd = partialRight( walkToBoundary, 'forwards' );\n"]}
@@ -25,7 +25,8 @@ export const strikethrough = {
25
25
  }) {
26
26
  function onClick() {
27
27
  onChange(toggleFormat(value, {
28
- type: name
28
+ type: name,
29
+ title
29
30
  }));
30
31
  onFocus();
31
32
  }
@@ -34,7 +35,8 @@ export const strikethrough = {
34
35
  icon: formatStrikethrough,
35
36
  title: title,
36
37
  onClick: onClick,
37
- isActive: isActive
38
+ isActive: isActive,
39
+ role: "menuitemcheckbox"
38
40
  });
39
41
  }
40
42
 
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/format-library/src/strikethrough/index.js"],"names":["__","toggleFormat","RichTextToolbarButton","formatStrikethrough","name","title","strikethrough","tagName","className","edit","isActive","value","onChange","onFocus","onClick","type"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,EAAT,QAAmB,iBAAnB;AACA,SAASC,YAAT,QAA6B,sBAA7B;AACA,SAASC,qBAAT,QAAsC,yBAAtC;AACA,SAASC,mBAAT,QAAoC,kBAApC;AAEA,MAAMC,IAAI,GAAG,oBAAb;;AACA,MAAMC,KAAK,GAAGL,EAAE,CAAE,eAAF,CAAhB;;AAEA,OAAO,MAAMM,aAAa,GAAG;AAC5BF,EAAAA,IAD4B;AAE5BC,EAAAA,KAF4B;AAG5BE,EAAAA,OAAO,EAAE,GAHmB;AAI5BC,EAAAA,SAAS,EAAE,IAJiB;;AAK5BC,EAAAA,IAAI,CAAE;AAAEC,IAAAA,QAAF;AAAYC,IAAAA,KAAZ;AAAmBC,IAAAA,QAAnB;AAA6BC,IAAAA;AAA7B,GAAF,EAA2C;AAC9C,aAASC,OAAT,GAAmB;AAClBF,MAAAA,QAAQ,CAAEX,YAAY,CAAEU,KAAF,EAAS;AAAEI,QAAAA,IAAI,EAAEX;AAAR,OAAT,CAAd,CAAR;AACAS,MAAAA,OAAO;AACP;;AAED,WACC,cAAC,qBAAD;AACC,MAAA,IAAI,EAAGV,mBADR;AAEC,MAAA,KAAK,EAAGE,KAFT;AAGC,MAAA,OAAO,EAAGS,OAHX;AAIC,MAAA,QAAQ,EAAGJ;AAJZ,MADD;AAQA;;AAnB2B,CAAtB","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { toggleFormat } from '@wordpress/rich-text';\nimport { RichTextToolbarButton } from '@wordpress/block-editor';\nimport { formatStrikethrough } from '@wordpress/icons';\n\nconst name = 'core/strikethrough';\nconst title = __( 'Strikethrough' );\n\nexport const strikethrough = {\n\tname,\n\ttitle,\n\ttagName: 's',\n\tclassName: null,\n\tedit( { isActive, value, onChange, onFocus } ) {\n\t\tfunction onClick() {\n\t\t\tonChange( toggleFormat( value, { type: name } ) );\n\t\t\tonFocus();\n\t\t}\n\n\t\treturn (\n\t\t\t<RichTextToolbarButton\n\t\t\t\ticon={ formatStrikethrough }\n\t\t\t\ttitle={ title }\n\t\t\t\tonClick={ onClick }\n\t\t\t\tisActive={ isActive }\n\t\t\t/>\n\t\t);\n\t},\n};\n"]}
1
+ {"version":3,"sources":["@wordpress/format-library/src/strikethrough/index.js"],"names":["__","toggleFormat","RichTextToolbarButton","formatStrikethrough","name","title","strikethrough","tagName","className","edit","isActive","value","onChange","onFocus","onClick","type"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,EAAT,QAAmB,iBAAnB;AACA,SAASC,YAAT,QAA6B,sBAA7B;AACA,SAASC,qBAAT,QAAsC,yBAAtC;AACA,SAASC,mBAAT,QAAoC,kBAApC;AAEA,MAAMC,IAAI,GAAG,oBAAb;;AACA,MAAMC,KAAK,GAAGL,EAAE,CAAE,eAAF,CAAhB;;AAEA,OAAO,MAAMM,aAAa,GAAG;AAC5BF,EAAAA,IAD4B;AAE5BC,EAAAA,KAF4B;AAG5BE,EAAAA,OAAO,EAAE,GAHmB;AAI5BC,EAAAA,SAAS,EAAE,IAJiB;;AAK5BC,EAAAA,IAAI,CAAE;AAAEC,IAAAA,QAAF;AAAYC,IAAAA,KAAZ;AAAmBC,IAAAA,QAAnB;AAA6BC,IAAAA;AAA7B,GAAF,EAA2C;AAC9C,aAASC,OAAT,GAAmB;AAClBF,MAAAA,QAAQ,CAAEX,YAAY,CAAEU,KAAF,EAAS;AAAEI,QAAAA,IAAI,EAAEX,IAAR;AAAcC,QAAAA;AAAd,OAAT,CAAd,CAAR;AACAQ,MAAAA,OAAO;AACP;;AAED,WACC,cAAC,qBAAD;AACC,MAAA,IAAI,EAAGV,mBADR;AAEC,MAAA,KAAK,EAAGE,KAFT;AAGC,MAAA,OAAO,EAAGS,OAHX;AAIC,MAAA,QAAQ,EAAGJ,QAJZ;AAKC,MAAA,IAAI,EAAC;AALN,MADD;AASA;;AApB2B,CAAtB","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { toggleFormat } from '@wordpress/rich-text';\nimport { RichTextToolbarButton } from '@wordpress/block-editor';\nimport { formatStrikethrough } from '@wordpress/icons';\n\nconst name = 'core/strikethrough';\nconst title = __( 'Strikethrough' );\n\nexport const strikethrough = {\n\tname,\n\ttitle,\n\ttagName: 's',\n\tclassName: null,\n\tedit( { isActive, value, onChange, onFocus } ) {\n\t\tfunction onClick() {\n\t\t\tonChange( toggleFormat( value, { type: name, title } ) );\n\t\t\tonFocus();\n\t\t}\n\n\t\treturn (\n\t\t\t<RichTextToolbarButton\n\t\t\t\ticon={ formatStrikethrough }\n\t\t\t\ttitle={ title }\n\t\t\t\tonClick={ onClick }\n\t\t\t\tisActive={ isActive }\n\t\t\t\trole=\"menuitemcheckbox\"\n\t\t\t/>\n\t\t);\n\t},\n};\n"]}
@@ -25,7 +25,8 @@ export const subscript = {
25
25
  }) {
26
26
  function onToggle() {
27
27
  onChange(toggleFormat(value, {
28
- type: name
28
+ type: name,
29
+ title
29
30
  }));
30
31
  }
31
32
 
@@ -38,7 +39,8 @@ export const subscript = {
38
39
  icon: subscriptIcon,
39
40
  title: title,
40
41
  onClick: onClick,
41
- isActive: isActive
42
+ isActive: isActive,
43
+ role: "menuitemcheckbox"
42
44
  });
43
45
  }
44
46
 
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/format-library/src/subscript/index.js"],"names":["__","toggleFormat","RichTextToolbarButton","subscript","subscriptIcon","name","title","tagName","className","edit","isActive","value","onChange","onFocus","onToggle","type","onClick"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,EAAT,QAAmB,iBAAnB;AACA,SAASC,YAAT,QAA6B,sBAA7B;AACA,SAASC,qBAAT,QAAsC,yBAAtC;AACA,SAASC,SAAS,IAAIC,aAAtB,QAA2C,kBAA3C;AAEA,MAAMC,IAAI,GAAG,gBAAb;;AACA,MAAMC,KAAK,GAAGN,EAAE,CAAE,WAAF,CAAhB;;AAEA,OAAO,MAAMG,SAAS,GAAG;AACxBE,EAAAA,IADwB;AAExBC,EAAAA,KAFwB;AAGxBC,EAAAA,OAAO,EAAE,KAHe;AAIxBC,EAAAA,SAAS,EAAE,IAJa;;AAKxBC,EAAAA,IAAI,CAAE;AAAEC,IAAAA,QAAF;AAAYC,IAAAA,KAAZ;AAAmBC,IAAAA,QAAnB;AAA6BC,IAAAA;AAA7B,GAAF,EAA2C;AAC9C,aAASC,QAAT,GAAoB;AACnBF,MAAAA,QAAQ,CAAEX,YAAY,CAAEU,KAAF,EAAS;AAAEI,QAAAA,IAAI,EAAEV;AAAR,OAAT,CAAd,CAAR;AACA;;AAED,aAASW,OAAT,GAAmB;AAClBF,MAAAA,QAAQ;AACRD,MAAAA,OAAO;AACP;;AAED,WACC,cAAC,qBAAD;AACC,MAAA,IAAI,EAAGT,aADR;AAEC,MAAA,KAAK,EAAGE,KAFT;AAGC,MAAA,OAAO,EAAGU,OAHX;AAIC,MAAA,QAAQ,EAAGN;AAJZ,MADD;AAQA;;AAvBuB,CAAlB","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { toggleFormat } from '@wordpress/rich-text';\nimport { RichTextToolbarButton } from '@wordpress/block-editor';\nimport { subscript as subscriptIcon } from '@wordpress/icons';\n\nconst name = 'core/subscript';\nconst title = __( 'Subscript' );\n\nexport const subscript = {\n\tname,\n\ttitle,\n\ttagName: 'sub',\n\tclassName: null,\n\tedit( { isActive, value, onChange, onFocus } ) {\n\t\tfunction onToggle() {\n\t\t\tonChange( toggleFormat( value, { type: name } ) );\n\t\t}\n\n\t\tfunction onClick() {\n\t\t\tonToggle();\n\t\t\tonFocus();\n\t\t}\n\n\t\treturn (\n\t\t\t<RichTextToolbarButton\n\t\t\t\ticon={ subscriptIcon }\n\t\t\t\ttitle={ title }\n\t\t\t\tonClick={ onClick }\n\t\t\t\tisActive={ isActive }\n\t\t\t/>\n\t\t);\n\t},\n};\n"]}
1
+ {"version":3,"sources":["@wordpress/format-library/src/subscript/index.js"],"names":["__","toggleFormat","RichTextToolbarButton","subscript","subscriptIcon","name","title","tagName","className","edit","isActive","value","onChange","onFocus","onToggle","type","onClick"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,EAAT,QAAmB,iBAAnB;AACA,SAASC,YAAT,QAA6B,sBAA7B;AACA,SAASC,qBAAT,QAAsC,yBAAtC;AACA,SAASC,SAAS,IAAIC,aAAtB,QAA2C,kBAA3C;AAEA,MAAMC,IAAI,GAAG,gBAAb;;AACA,MAAMC,KAAK,GAAGN,EAAE,CAAE,WAAF,CAAhB;;AAEA,OAAO,MAAMG,SAAS,GAAG;AACxBE,EAAAA,IADwB;AAExBC,EAAAA,KAFwB;AAGxBC,EAAAA,OAAO,EAAE,KAHe;AAIxBC,EAAAA,SAAS,EAAE,IAJa;;AAKxBC,EAAAA,IAAI,CAAE;AAAEC,IAAAA,QAAF;AAAYC,IAAAA,KAAZ;AAAmBC,IAAAA,QAAnB;AAA6BC,IAAAA;AAA7B,GAAF,EAA2C;AAC9C,aAASC,QAAT,GAAoB;AACnBF,MAAAA,QAAQ,CAAEX,YAAY,CAAEU,KAAF,EAAS;AAAEI,QAAAA,IAAI,EAAEV,IAAR;AAAcC,QAAAA;AAAd,OAAT,CAAd,CAAR;AACA;;AAED,aAASU,OAAT,GAAmB;AAClBF,MAAAA,QAAQ;AACRD,MAAAA,OAAO;AACP;;AAED,WACC,cAAC,qBAAD;AACC,MAAA,IAAI,EAAGT,aADR;AAEC,MAAA,KAAK,EAAGE,KAFT;AAGC,MAAA,OAAO,EAAGU,OAHX;AAIC,MAAA,QAAQ,EAAGN,QAJZ;AAKC,MAAA,IAAI,EAAC;AALN,MADD;AASA;;AAxBuB,CAAlB","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { toggleFormat } from '@wordpress/rich-text';\nimport { RichTextToolbarButton } from '@wordpress/block-editor';\nimport { subscript as subscriptIcon } from '@wordpress/icons';\n\nconst name = 'core/subscript';\nconst title = __( 'Subscript' );\n\nexport const subscript = {\n\tname,\n\ttitle,\n\ttagName: 'sub',\n\tclassName: null,\n\tedit( { isActive, value, onChange, onFocus } ) {\n\t\tfunction onToggle() {\n\t\t\tonChange( toggleFormat( value, { type: name, title } ) );\n\t\t}\n\n\t\tfunction onClick() {\n\t\t\tonToggle();\n\t\t\tonFocus();\n\t\t}\n\n\t\treturn (\n\t\t\t<RichTextToolbarButton\n\t\t\t\ticon={ subscriptIcon }\n\t\t\t\ttitle={ title }\n\t\t\t\tonClick={ onClick }\n\t\t\t\tisActive={ isActive }\n\t\t\t\trole=\"menuitemcheckbox\"\n\t\t\t/>\n\t\t);\n\t},\n};\n"]}
@@ -25,7 +25,8 @@ export const superscript = {
25
25
  }) {
26
26
  function onToggle() {
27
27
  onChange(toggleFormat(value, {
28
- type: name
28
+ type: name,
29
+ title
29
30
  }));
30
31
  }
31
32
 
@@ -38,7 +39,8 @@ export const superscript = {
38
39
  icon: superscriptIcon,
39
40
  title: title,
40
41
  onClick: onClick,
41
- isActive: isActive
42
+ isActive: isActive,
43
+ role: "menuitemcheckbox"
42
44
  });
43
45
  }
44
46
 
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/format-library/src/superscript/index.js"],"names":["__","toggleFormat","RichTextToolbarButton","superscript","superscriptIcon","name","title","tagName","className","edit","isActive","value","onChange","onFocus","onToggle","type","onClick"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,EAAT,QAAmB,iBAAnB;AACA,SAASC,YAAT,QAA6B,sBAA7B;AACA,SAASC,qBAAT,QAAsC,yBAAtC;AACA,SAASC,WAAW,IAAIC,eAAxB,QAA+C,kBAA/C;AAEA,MAAMC,IAAI,GAAG,kBAAb;;AACA,MAAMC,KAAK,GAAGN,EAAE,CAAE,aAAF,CAAhB;;AAEA,OAAO,MAAMG,WAAW,GAAG;AAC1BE,EAAAA,IAD0B;AAE1BC,EAAAA,KAF0B;AAG1BC,EAAAA,OAAO,EAAE,KAHiB;AAI1BC,EAAAA,SAAS,EAAE,IAJe;;AAK1BC,EAAAA,IAAI,CAAE;AAAEC,IAAAA,QAAF;AAAYC,IAAAA,KAAZ;AAAmBC,IAAAA,QAAnB;AAA6BC,IAAAA;AAA7B,GAAF,EAA2C;AAC9C,aAASC,QAAT,GAAoB;AACnBF,MAAAA,QAAQ,CAAEX,YAAY,CAAEU,KAAF,EAAS;AAAEI,QAAAA,IAAI,EAAEV;AAAR,OAAT,CAAd,CAAR;AACA;;AAED,aAASW,OAAT,GAAmB;AAClBF,MAAAA,QAAQ;AACRD,MAAAA,OAAO;AACP;;AAED,WACC,cAAC,qBAAD;AACC,MAAA,IAAI,EAAGT,eADR;AAEC,MAAA,KAAK,EAAGE,KAFT;AAGC,MAAA,OAAO,EAAGU,OAHX;AAIC,MAAA,QAAQ,EAAGN;AAJZ,MADD;AAQA;;AAvByB,CAApB","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { toggleFormat } from '@wordpress/rich-text';\nimport { RichTextToolbarButton } from '@wordpress/block-editor';\nimport { superscript as superscriptIcon } from '@wordpress/icons';\n\nconst name = 'core/superscript';\nconst title = __( 'Superscript' );\n\nexport const superscript = {\n\tname,\n\ttitle,\n\ttagName: 'sup',\n\tclassName: null,\n\tedit( { isActive, value, onChange, onFocus } ) {\n\t\tfunction onToggle() {\n\t\t\tonChange( toggleFormat( value, { type: name } ) );\n\t\t}\n\n\t\tfunction onClick() {\n\t\t\tonToggle();\n\t\t\tonFocus();\n\t\t}\n\n\t\treturn (\n\t\t\t<RichTextToolbarButton\n\t\t\t\ticon={ superscriptIcon }\n\t\t\t\ttitle={ title }\n\t\t\t\tonClick={ onClick }\n\t\t\t\tisActive={ isActive }\n\t\t\t/>\n\t\t);\n\t},\n};\n"]}
1
+ {"version":3,"sources":["@wordpress/format-library/src/superscript/index.js"],"names":["__","toggleFormat","RichTextToolbarButton","superscript","superscriptIcon","name","title","tagName","className","edit","isActive","value","onChange","onFocus","onToggle","type","onClick"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,EAAT,QAAmB,iBAAnB;AACA,SAASC,YAAT,QAA6B,sBAA7B;AACA,SAASC,qBAAT,QAAsC,yBAAtC;AACA,SAASC,WAAW,IAAIC,eAAxB,QAA+C,kBAA/C;AAEA,MAAMC,IAAI,GAAG,kBAAb;;AACA,MAAMC,KAAK,GAAGN,EAAE,CAAE,aAAF,CAAhB;;AAEA,OAAO,MAAMG,WAAW,GAAG;AAC1BE,EAAAA,IAD0B;AAE1BC,EAAAA,KAF0B;AAG1BC,EAAAA,OAAO,EAAE,KAHiB;AAI1BC,EAAAA,SAAS,EAAE,IAJe;;AAK1BC,EAAAA,IAAI,CAAE;AAAEC,IAAAA,QAAF;AAAYC,IAAAA,KAAZ;AAAmBC,IAAAA,QAAnB;AAA6BC,IAAAA;AAA7B,GAAF,EAA2C;AAC9C,aAASC,QAAT,GAAoB;AACnBF,MAAAA,QAAQ,CAAEX,YAAY,CAAEU,KAAF,EAAS;AAAEI,QAAAA,IAAI,EAAEV,IAAR;AAAcC,QAAAA;AAAd,OAAT,CAAd,CAAR;AACA;;AAED,aAASU,OAAT,GAAmB;AAClBF,MAAAA,QAAQ;AACRD,MAAAA,OAAO;AACP;;AAED,WACC,cAAC,qBAAD;AACC,MAAA,IAAI,EAAGT,eADR;AAEC,MAAA,KAAK,EAAGE,KAFT;AAGC,MAAA,OAAO,EAAGU,OAHX;AAIC,MAAA,QAAQ,EAAGN,QAJZ;AAKC,MAAA,IAAI,EAAC;AALN,MADD;AASA;;AAxByB,CAApB","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { toggleFormat } from '@wordpress/rich-text';\nimport { RichTextToolbarButton } from '@wordpress/block-editor';\nimport { superscript as superscriptIcon } from '@wordpress/icons';\n\nconst name = 'core/superscript';\nconst title = __( 'Superscript' );\n\nexport const superscript = {\n\tname,\n\ttitle,\n\ttagName: 'sup',\n\tclassName: null,\n\tedit( { isActive, value, onChange, onFocus } ) {\n\t\tfunction onToggle() {\n\t\t\tonChange( toggleFormat( value, { type: name, title } ) );\n\t\t}\n\n\t\tfunction onClick() {\n\t\t\tonToggle();\n\t\t\tonFocus();\n\t\t}\n\n\t\treturn (\n\t\t\t<RichTextToolbarButton\n\t\t\t\ticon={ superscriptIcon }\n\t\t\t\ttitle={ title }\n\t\t\t\tonClick={ onClick }\n\t\t\t\tisActive={ isActive }\n\t\t\t\trole=\"menuitemcheckbox\"\n\t\t\t/>\n\t\t);\n\t},\n};\n"]}
@@ -17,13 +17,44 @@ import { removeFormat } from '@wordpress/rich-text';
17
17
  * Internal dependencies
18
18
  */
19
19
 
20
- import { default as InlineColorUI, getActiveColor } from './inline';
20
+ import { default as InlineColorUI, getActiveColors } from './inline';
21
21
  const name = 'core/text-color';
22
22
 
23
- const title = __('Text color');
23
+ const title = __('Highlight');
24
24
 
25
25
  const EMPTY_ARRAY = [];
26
26
 
27
+ function getComputedStyleProperty(element, property) {
28
+ const {
29
+ ownerDocument
30
+ } = element;
31
+ const {
32
+ defaultView
33
+ } = ownerDocument;
34
+ const style = defaultView.getComputedStyle(element);
35
+ const value = style.getPropertyValue(property);
36
+
37
+ if (property === 'background-color' && value === 'rgba(0, 0, 0, 0)' && element.parentElement) {
38
+ return getComputedStyleProperty(element.parentElement, property);
39
+ }
40
+
41
+ return value;
42
+ }
43
+
44
+ function fillComputedColors(element, {
45
+ color,
46
+ backgroundColor
47
+ }) {
48
+ if (!color && !backgroundColor) {
49
+ return;
50
+ }
51
+
52
+ return {
53
+ color: color || getComputedStyleProperty(element, 'color'),
54
+ backgroundColor: backgroundColor === 'rgba(0, 0, 0, 0)' ? getComputedStyleProperty(element, 'background-color') : backgroundColor
55
+ };
56
+ }
57
+
27
58
  function TextColorEdit({
28
59
  value,
29
60
  onChange,
@@ -36,17 +67,7 @@ function TextColorEdit({
36
67
  const [isAddingColor, setIsAddingColor] = useState(false);
37
68
  const enableIsAddingColor = useCallback(() => setIsAddingColor(true), [setIsAddingColor]);
38
69
  const disableIsAddingColor = useCallback(() => setIsAddingColor(false), [setIsAddingColor]);
39
- const colorIndicatorStyle = useMemo(() => {
40
- const activeColor = getActiveColor(name, value, colors);
41
-
42
- if (!activeColor) {
43
- return undefined;
44
- }
45
-
46
- return {
47
- backgroundColor: activeColor
48
- };
49
- }, [value, colors]);
70
+ const colorIndicatorStyle = useMemo(() => fillComputedColors(contentRef.current, getActiveColors(value, name, colors)), [value, colors]);
50
71
  const hasColorsToChoose = !isEmpty(colors) || !allowCustomControl;
51
72
 
52
73
  if (!hasColorsToChoose && !isActive) {
@@ -54,18 +75,16 @@ function TextColorEdit({
54
75
  }
55
76
 
56
77
  return createElement(Fragment, null, createElement(RichTextToolbarButton, {
57
- key: isActive ? 'text-color' : 'text-color-not-active',
58
78
  className: "format-library-text-color-button",
59
- name: isActive ? 'text-color' : undefined,
60
- icon: createElement(Fragment, null, createElement(Icon, {
61
- icon: textColorIcon
62
- }), isActive && createElement("span", {
63
- className: "format-library-text-color-button__indicator",
79
+ isActive: isActive,
80
+ icon: createElement(Icon, {
81
+ icon: textColorIcon,
64
82
  style: colorIndicatorStyle
65
- })),
83
+ }),
66
84
  title: title // If has no colors to choose but a color is active remove the color onClick
67
85
  ,
68
- onClick: hasColorsToChoose ? enableIsAddingColor : () => onChange(removeFormat(value, name))
86
+ onClick: hasColorsToChoose ? enableIsAddingColor : () => onChange(removeFormat(value, name)),
87
+ role: "menuitemcheckbox"
69
88
  }), isAddingColor && createElement(InlineColorUI, {
70
89
  name: name,
71
90
  onClose: disableIsAddingColor,
@@ -79,12 +98,32 @@ function TextColorEdit({
79
98
  export const textColor = {
80
99
  name,
81
100
  title,
82
- tagName: 'span',
101
+ tagName: 'mark',
83
102
  className: 'has-inline-color',
84
103
  attributes: {
85
104
  style: 'style',
86
105
  class: 'class'
87
106
  },
107
+
108
+ /*
109
+ * Since this format relies on the <mark> tag, it's important to
110
+ * prevent the default yellow background color applied by most
111
+ * browsers. The solution is to detect when this format is used with a
112
+ * text color but no background color, and in such cases to override
113
+ * the default styling with a transparent background.
114
+ *
115
+ * @see https://github.com/WordPress/gutenberg/pull/35516
116
+ */
117
+ __unstableFilterAttributeValue(key, value) {
118
+ if (key !== 'style') return value; // We should not add a background-color if it's already set
119
+
120
+ if (value && value.includes('background-color')) return value;
121
+ const addedCSS = ['background-color', 'rgba(0, 0, 0, 0)'].join(':'); // Prepend `addedCSS` to avoid a double `;;` as any the existing CSS
122
+ // rules will already include a `;`.
123
+
124
+ return value ? [addedCSS, value].join(';') : addedCSS;
125
+ },
126
+
88
127
  edit: TextColorEdit
89
128
  };
90
129
  //# sourceMappingURL=index.js.map