@wordpress/block-library 9.33.6 → 9.33.7
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.
- package/build/heading/index.js +3 -1
- package/build/heading/index.js.map +3 -3
- package/build/heading/variations.js +53 -0
- package/build/heading/variations.js.map +7 -0
- package/build/navigation-link/edit.js +25 -10
- package/build/navigation-link/edit.js.map +2 -2
- package/build/navigation-link/link-ui/index.js +8 -3
- package/build/navigation-link/link-ui/index.js.map +2 -2
- package/build/navigation-link/shared/controls.js +76 -21
- package/build/navigation-link/shared/controls.js.map +2 -2
- package/build/navigation-link/shared/use-entity-binding.js +31 -2
- package/build/navigation-link/shared/use-entity-binding.js.map +3 -3
- package/build/paragraph/index.js +3 -1
- package/build/paragraph/index.js.map +3 -3
- package/build/paragraph/variations.js +55 -0
- package/build/paragraph/variations.js.map +7 -0
- package/build-module/heading/index.js +3 -1
- package/build-module/heading/index.js.map +2 -2
- package/build-module/heading/variations.js +33 -0
- package/build-module/heading/variations.js.map +7 -0
- package/build-module/navigation-link/edit.js +25 -10
- package/build-module/navigation-link/edit.js.map +2 -2
- package/build-module/navigation-link/link-ui/index.js +8 -3
- package/build-module/navigation-link/link-ui/index.js.map +2 -2
- package/build-module/navigation-link/shared/controls.js +77 -22
- package/build-module/navigation-link/shared/controls.js.map +2 -2
- package/build-module/navigation-link/shared/use-entity-binding.js +35 -3
- package/build-module/navigation-link/shared/use-entity-binding.js.map +2 -2
- package/build-module/paragraph/index.js +3 -1
- package/build-module/paragraph/index.js.map +2 -2
- package/build-module/paragraph/variations.js +35 -0
- package/build-module/paragraph/variations.js.map +7 -0
- package/build-style/accordion-heading/style-rtl.css +19 -3
- package/build-style/accordion-heading/style.css +19 -3
- package/build-style/accordion-panel/style-rtl.css +4 -1
- package/build-style/accordion-panel/style.css +4 -1
- package/build-style/editor-rtl.css +7 -0
- package/build-style/editor.css +7 -0
- package/build-style/navigation-link/editor-rtl.css +7 -0
- package/build-style/navigation-link/editor.css +7 -0
- package/build-style/style-rtl.css +23 -4
- package/build-style/style.css +23 -4
- package/package.json +11 -11
- package/src/accordion-heading/style.scss +40 -7
- package/src/accordion-panel/style.scss +6 -1
- package/src/heading/index.js +2 -0
- package/src/heading/variations.js +37 -0
- package/src/navigation-link/edit.js +44 -13
- package/src/navigation-link/editor.scss +7 -0
- package/src/navigation-link/link-ui/index.js +9 -8
- package/src/navigation-link/shared/controls.js +123 -25
- package/src/navigation-link/shared/test/controls.js +19 -9
- package/src/navigation-link/shared/test/use-entity-binding.js +14 -1
- package/src/navigation-link/shared/use-entity-binding.js +57 -9
- package/src/paragraph/index.js +2 -0
- package/src/paragraph/variations.js +39 -0
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
TextareaControl,
|
|
12
12
|
} from '@wordpress/components';
|
|
13
13
|
import { __, sprintf } from '@wordpress/i18n';
|
|
14
|
-
import { useRef } from '@wordpress/element';
|
|
14
|
+
import { useRef, useEffect, useState } from '@wordpress/element';
|
|
15
15
|
import { useInstanceId } from '@wordpress/compose';
|
|
16
16
|
import { safeDecodeURI } from '@wordpress/url';
|
|
17
17
|
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
|
|
@@ -72,19 +72,31 @@ export function Controls( { attributes, setAttributes, clientId } ) {
|
|
|
72
72
|
const { label, url, description, rel, opensInNewTab } = attributes;
|
|
73
73
|
const lastURLRef = useRef( url );
|
|
74
74
|
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
|
|
75
|
+
const urlInputRef = useRef();
|
|
76
|
+
const shouldFocusURLInputRef = useRef( false );
|
|
75
77
|
const inputId = useInstanceId( Controls, 'link-input' );
|
|
76
78
|
const helpTextId = `${ inputId }__help`;
|
|
77
79
|
|
|
80
|
+
// Local state to control the input value
|
|
81
|
+
const [ inputValue, setInputValue ] = useState( url );
|
|
82
|
+
|
|
83
|
+
// Sync local state when url prop changes (e.g., from undo/redo or external updates)
|
|
84
|
+
useEffect( () => {
|
|
85
|
+
setInputValue( url );
|
|
86
|
+
lastURLRef.current = url;
|
|
87
|
+
}, [ url ] );
|
|
88
|
+
|
|
78
89
|
// Use the entity binding hook internally
|
|
79
|
-
const { hasUrlBinding, clearBinding } =
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
90
|
+
const { hasUrlBinding, isBoundEntityAvailable, clearBinding } =
|
|
91
|
+
useEntityBinding( {
|
|
92
|
+
clientId,
|
|
93
|
+
attributes,
|
|
94
|
+
} );
|
|
83
95
|
|
|
84
96
|
// Get direct store dispatch to bypass setBoundAttributes wrapper
|
|
85
97
|
const { updateBlockAttributes } = useDispatch( blockEditorStore );
|
|
86
98
|
|
|
87
|
-
const
|
|
99
|
+
const unsyncBoundLink = () => {
|
|
88
100
|
// Clear the binding first
|
|
89
101
|
clearBinding();
|
|
90
102
|
|
|
@@ -93,9 +105,22 @@ export function Controls( { attributes, setAttributes, clientId } ) {
|
|
|
93
105
|
// setAttributes is actually setBoundAttributes, a wrapper function that
|
|
94
106
|
// processes attributes through the binding system.
|
|
95
107
|
// See: packages/block-editor/src/components/block-edit/edit.js
|
|
96
|
-
updateBlockAttributes( clientId, {
|
|
108
|
+
updateBlockAttributes( clientId, {
|
|
109
|
+
url: lastURLRef.current, // set the lastURLRef as the new editable value so we avoid bugs from empty link states
|
|
110
|
+
id: undefined,
|
|
111
|
+
} );
|
|
97
112
|
};
|
|
98
113
|
|
|
114
|
+
useEffect( () => {
|
|
115
|
+
// Only want to focus the input if the url is not bound to an entity.
|
|
116
|
+
if ( ! hasUrlBinding && shouldFocusURLInputRef.current ) {
|
|
117
|
+
// focuses and highlights the url input value, giving the user
|
|
118
|
+
// the ability to delete the value quickly or edit it.
|
|
119
|
+
urlInputRef.current?.select();
|
|
120
|
+
}
|
|
121
|
+
shouldFocusURLInputRef.current = false;
|
|
122
|
+
}, [ hasUrlBinding ] );
|
|
123
|
+
|
|
99
124
|
return (
|
|
100
125
|
<ToolsPanel
|
|
101
126
|
label={ __( 'Settings' ) }
|
|
@@ -135,56 +160,100 @@ export function Controls( { attributes, setAttributes, clientId } ) {
|
|
|
135
160
|
isShownByDefault
|
|
136
161
|
>
|
|
137
162
|
<InputControl
|
|
163
|
+
ref={ urlInputRef }
|
|
138
164
|
__nextHasNoMarginBottom
|
|
139
165
|
__next40pxDefaultSize
|
|
140
166
|
id={ inputId }
|
|
141
167
|
label={ __( 'Link' ) }
|
|
142
|
-
value={
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return; // Prevent editing when URL is bound
|
|
168
|
+
value={ ( () => {
|
|
169
|
+
if ( hasUrlBinding && ! isBoundEntityAvailable ) {
|
|
170
|
+
return '';
|
|
146
171
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
} );
|
|
150
|
-
} }
|
|
172
|
+
return inputValue ? safeDecodeURI( inputValue ) : '';
|
|
173
|
+
} )() }
|
|
151
174
|
autoComplete="off"
|
|
152
175
|
type="url"
|
|
153
176
|
disabled={ hasUrlBinding }
|
|
177
|
+
aria-invalid={
|
|
178
|
+
hasUrlBinding && ! isBoundEntityAvailable
|
|
179
|
+
? 'true'
|
|
180
|
+
: undefined
|
|
181
|
+
}
|
|
182
|
+
aria-describedby={ helpTextId }
|
|
183
|
+
className={
|
|
184
|
+
hasUrlBinding && ! isBoundEntityAvailable
|
|
185
|
+
? 'navigation-link-control__input-with-error-suffix'
|
|
186
|
+
: undefined
|
|
187
|
+
}
|
|
188
|
+
onChange={ ( newValue ) => {
|
|
189
|
+
if ( isBoundEntityAvailable ) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Defer updating the url attribute until onBlur to prevent the canvas from
|
|
194
|
+
// treating a temporary empty value as a committed value, which replaces the
|
|
195
|
+
// label with placeholder text.
|
|
196
|
+
setInputValue( newValue );
|
|
197
|
+
} }
|
|
154
198
|
onFocus={ () => {
|
|
155
|
-
if (
|
|
199
|
+
if ( isBoundEntityAvailable ) {
|
|
156
200
|
return;
|
|
157
201
|
}
|
|
158
202
|
lastURLRef.current = url;
|
|
159
203
|
} }
|
|
160
204
|
onBlur={ () => {
|
|
161
|
-
if (
|
|
205
|
+
if ( isBoundEntityAvailable ) {
|
|
162
206
|
return;
|
|
163
207
|
}
|
|
208
|
+
|
|
209
|
+
const finalValue = ! inputValue
|
|
210
|
+
? lastURLRef.current
|
|
211
|
+
: inputValue;
|
|
212
|
+
|
|
213
|
+
// Update local state immediately so input reflects the reverted value if the value was cleared
|
|
214
|
+
setInputValue( finalValue );
|
|
215
|
+
|
|
164
216
|
// Defer the updateAttributes call to ensure entity connection isn't severed by accident.
|
|
165
|
-
updateAttributes(
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
);
|
|
217
|
+
updateAttributes( { url: finalValue }, setAttributes, {
|
|
218
|
+
...attributes,
|
|
219
|
+
url: lastURLRef.current,
|
|
220
|
+
} );
|
|
170
221
|
} }
|
|
171
222
|
help={
|
|
172
|
-
hasUrlBinding && (
|
|
173
|
-
<
|
|
223
|
+
hasUrlBinding && ! isBoundEntityAvailable ? (
|
|
224
|
+
<MissingEntityHelpText
|
|
225
|
+
id={ helpTextId }
|
|
174
226
|
type={ attributes.type }
|
|
175
227
|
kind={ attributes.kind }
|
|
176
228
|
/>
|
|
229
|
+
) : (
|
|
230
|
+
isBoundEntityAvailable && (
|
|
231
|
+
<BindingHelpText
|
|
232
|
+
type={ attributes.type }
|
|
233
|
+
kind={ attributes.kind }
|
|
234
|
+
/>
|
|
235
|
+
)
|
|
177
236
|
)
|
|
178
237
|
}
|
|
179
238
|
suffix={
|
|
180
239
|
hasUrlBinding && (
|
|
181
240
|
<Button
|
|
182
241
|
icon={ unlinkIcon }
|
|
183
|
-
onClick={
|
|
242
|
+
onClick={ () => {
|
|
243
|
+
unsyncBoundLink();
|
|
244
|
+
// Focus management to send focus to the URL input
|
|
245
|
+
// on next render after disabled state is removed.
|
|
246
|
+
shouldFocusURLInputRef.current = true;
|
|
247
|
+
} }
|
|
184
248
|
aria-describedby={ helpTextId }
|
|
185
249
|
showTooltip
|
|
186
250
|
label={ __( 'Unsync and edit' ) }
|
|
187
251
|
__next40pxDefaultSize
|
|
252
|
+
className={
|
|
253
|
+
hasUrlBinding && ! isBoundEntityAvailable
|
|
254
|
+
? 'navigation-link-control__error-suffix-button'
|
|
255
|
+
: undefined
|
|
256
|
+
}
|
|
188
257
|
/>
|
|
189
258
|
)
|
|
190
259
|
}
|
|
@@ -266,3 +335,32 @@ function BindingHelpText( { type, kind } ) {
|
|
|
266
335
|
entityType
|
|
267
336
|
);
|
|
268
337
|
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Component to display error help text for missing entity bindings.
|
|
341
|
+
*
|
|
342
|
+
* @param {Object} props - Component props
|
|
343
|
+
* @param {string} props.id - ID for the help text element (for aria-describedby)
|
|
344
|
+
* @param {string} props.type - The entity type
|
|
345
|
+
* @param {string} props.kind - The entity kind
|
|
346
|
+
* @return {JSX.Element} Error help text component
|
|
347
|
+
*/
|
|
348
|
+
function MissingEntityHelpText( { id, type, kind } ) {
|
|
349
|
+
const entityType = getEntityTypeName( type, kind );
|
|
350
|
+
return (
|
|
351
|
+
<span
|
|
352
|
+
id={ id }
|
|
353
|
+
className="navigation-link-control__error-text"
|
|
354
|
+
role="alert"
|
|
355
|
+
aria-live="polite"
|
|
356
|
+
>
|
|
357
|
+
{ sprintf(
|
|
358
|
+
/* translators: %s is the entity type (e.g., "page", "post", "category") */
|
|
359
|
+
__(
|
|
360
|
+
'Synced %s is missing. Please update or remove this link.'
|
|
361
|
+
),
|
|
362
|
+
entityType
|
|
363
|
+
) }
|
|
364
|
+
</span>
|
|
365
|
+
);
|
|
366
|
+
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* External dependencies
|
|
7
7
|
*/
|
|
8
8
|
import { render, screen, fireEvent } from '@testing-library/react';
|
|
9
|
+
import userEvent from '@testing-library/user-event';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Internal dependencies
|
|
@@ -27,6 +28,7 @@ jest.mock( '../../../utils/hooks', () => ( {
|
|
|
27
28
|
jest.mock( '../use-entity-binding', () => ( {
|
|
28
29
|
useEntityBinding: jest.fn( () => ( {
|
|
29
30
|
hasUrlBinding: false,
|
|
31
|
+
isBoundEntityAvailable: false,
|
|
30
32
|
clearBinding: jest.fn(),
|
|
31
33
|
} ) ),
|
|
32
34
|
} ) );
|
|
@@ -95,18 +97,22 @@ describe( 'Controls', () => {
|
|
|
95
97
|
expect( urlInput.value ).toBe( 'https://example.com/test page' );
|
|
96
98
|
} );
|
|
97
99
|
|
|
98
|
-
it( '
|
|
100
|
+
it( 'calls updateAttributes with new URL on blur', async () => {
|
|
101
|
+
const user = userEvent.setup();
|
|
99
102
|
render( <Controls { ...defaultProps } /> );
|
|
100
103
|
|
|
101
104
|
const urlInput = screen.getByLabelText( 'Link' );
|
|
102
105
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
+
await user.click( urlInput );
|
|
107
|
+
await user.clear( urlInput );
|
|
108
|
+
await user.type( urlInput, 'https://example.com/test page' );
|
|
109
|
+
await user.tab();
|
|
106
110
|
|
|
107
|
-
expect(
|
|
108
|
-
url: 'https://example.com/test
|
|
109
|
-
|
|
111
|
+
expect( mockUpdateAttributes ).toHaveBeenCalledWith(
|
|
112
|
+
{ url: 'https://example.com/test page' },
|
|
113
|
+
defaultProps.setAttributes,
|
|
114
|
+
{ ...defaultProps.attributes, url: 'https://example.com' }
|
|
115
|
+
);
|
|
110
116
|
} );
|
|
111
117
|
|
|
112
118
|
it( 'calls updateAttributes on URL blur', () => {
|
|
@@ -143,11 +149,11 @@ describe( 'Controls', () => {
|
|
|
143
149
|
target: { value: 'https://new.com' },
|
|
144
150
|
} );
|
|
145
151
|
|
|
146
|
-
// Blur should call updateAttributes with the
|
|
152
|
+
// Blur should call updateAttributes with the new URL value from the input
|
|
147
153
|
fireEvent.blur( urlInput );
|
|
148
154
|
|
|
149
155
|
expect( mockUpdateAttributes ).toHaveBeenCalledWith(
|
|
150
|
-
{ url: 'https://
|
|
156
|
+
{ url: 'https://new.com' }, // New URL from input value
|
|
151
157
|
defaultProps.setAttributes,
|
|
152
158
|
{
|
|
153
159
|
...propsWithDifferentUrl.attributes,
|
|
@@ -197,6 +203,7 @@ describe( 'Controls', () => {
|
|
|
197
203
|
const { useEntityBinding } = require( '../use-entity-binding' );
|
|
198
204
|
useEntityBinding.mockReturnValue( {
|
|
199
205
|
hasUrlBinding: true,
|
|
206
|
+
isBoundEntityAvailable: true,
|
|
200
207
|
clearBinding: jest.fn(),
|
|
201
208
|
} );
|
|
202
209
|
|
|
@@ -220,6 +227,7 @@ describe( 'Controls', () => {
|
|
|
220
227
|
const { useEntityBinding } = require( '../use-entity-binding' );
|
|
221
228
|
useEntityBinding.mockReturnValue( {
|
|
222
229
|
hasUrlBinding: true,
|
|
230
|
+
isBoundEntityAvailable: true,
|
|
223
231
|
clearBinding: jest.fn(),
|
|
224
232
|
} );
|
|
225
233
|
|
|
@@ -257,6 +265,7 @@ describe( 'Controls', () => {
|
|
|
257
265
|
const { useEntityBinding } = require( '../use-entity-binding' );
|
|
258
266
|
useEntityBinding.mockReturnValue( {
|
|
259
267
|
hasUrlBinding: true,
|
|
268
|
+
isBoundEntityAvailable: true,
|
|
260
269
|
clearBinding: jest.fn(),
|
|
261
270
|
} );
|
|
262
271
|
|
|
@@ -280,6 +289,7 @@ describe( 'Controls', () => {
|
|
|
280
289
|
const { useEntityBinding } = require( '../use-entity-binding' );
|
|
281
290
|
useEntityBinding.mockReturnValue( {
|
|
282
291
|
hasUrlBinding: true,
|
|
292
|
+
isBoundEntityAvailable: true,
|
|
283
293
|
clearBinding: jest.fn(),
|
|
284
294
|
} );
|
|
285
295
|
|
|
@@ -18,12 +18,23 @@ import {
|
|
|
18
18
|
// Mock the entire @wordpress/block-editor module
|
|
19
19
|
jest.mock( '@wordpress/block-editor', () => ( {
|
|
20
20
|
useBlockBindingsUtils: jest.fn(),
|
|
21
|
+
useBlockEditingMode: jest.fn(),
|
|
21
22
|
} ) );
|
|
22
23
|
|
|
24
|
+
// Mock useSelect specifically to avoid needing to set up full data store
|
|
25
|
+
jest.mock( '@wordpress/data/src/components/use-select', () => {
|
|
26
|
+
const mock = jest.fn();
|
|
27
|
+
return mock;
|
|
28
|
+
} );
|
|
29
|
+
|
|
23
30
|
/**
|
|
24
31
|
* WordPress dependencies
|
|
25
32
|
*/
|
|
26
|
-
import {
|
|
33
|
+
import {
|
|
34
|
+
useBlockBindingsUtils,
|
|
35
|
+
useBlockEditingMode,
|
|
36
|
+
} from '@wordpress/block-editor';
|
|
37
|
+
import { useSelect } from '@wordpress/data';
|
|
27
38
|
|
|
28
39
|
describe( 'useEntityBinding', () => {
|
|
29
40
|
const mockUpdateBlockBindings = jest.fn();
|
|
@@ -33,6 +44,8 @@ describe( 'useEntityBinding', () => {
|
|
|
33
44
|
useBlockBindingsUtils.mockReturnValue( {
|
|
34
45
|
updateBlockBindings: mockUpdateBlockBindings,
|
|
35
46
|
} );
|
|
47
|
+
useBlockEditingMode.mockReturnValue( 'default' );
|
|
48
|
+
useSelect.mockReturnValue( true );
|
|
36
49
|
} );
|
|
37
50
|
|
|
38
51
|
describe( 'hasUrlBinding', () => {
|
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
4
|
import { useCallback } from '@wordpress/element';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
useBlockBindingsUtils,
|
|
7
|
+
useBlockEditingMode,
|
|
8
|
+
} from '@wordpress/block-editor';
|
|
9
|
+
import { useSelect } from '@wordpress/data';
|
|
10
|
+
import { store as coreStore } from '@wordpress/core-data';
|
|
6
11
|
|
|
7
12
|
/**
|
|
8
13
|
* Builds entity binding configuration for navigation link URLs.
|
|
@@ -16,7 +21,7 @@ import { useBlockBindingsUtils } from '@wordpress/block-editor';
|
|
|
16
21
|
* @throws {Error} If kind is not 'post-type' or 'taxonomy'
|
|
17
22
|
*/
|
|
18
23
|
export function buildNavigationLinkEntityBinding( kind ) {
|
|
19
|
-
// Validate kind parameter exists
|
|
24
|
+
// Validate kind parameter exists.
|
|
20
25
|
if ( kind === undefined ) {
|
|
21
26
|
throw new Error(
|
|
22
27
|
'buildNavigationLinkEntityBinding requires a kind parameter. ' +
|
|
@@ -24,7 +29,7 @@ export function buildNavigationLinkEntityBinding( kind ) {
|
|
|
24
29
|
);
|
|
25
30
|
}
|
|
26
31
|
|
|
27
|
-
// Validate kind parameter value
|
|
32
|
+
// Validate kind parameter value.
|
|
28
33
|
if ( kind !== 'post-type' && kind !== 'taxonomy' ) {
|
|
29
34
|
throw new Error(
|
|
30
35
|
`Invalid kind "${ kind }" provided to buildNavigationLinkEntityBinding. ` +
|
|
@@ -57,7 +62,8 @@ export function buildNavigationLinkEntityBinding( kind ) {
|
|
|
57
62
|
*/
|
|
58
63
|
export function useEntityBinding( { clientId, attributes } ) {
|
|
59
64
|
const { updateBlockBindings } = useBlockBindingsUtils( clientId );
|
|
60
|
-
const { metadata, id, kind } = attributes;
|
|
65
|
+
const { metadata, id, kind, type } = attributes;
|
|
66
|
+
const blockEditingMode = useBlockEditingMode();
|
|
61
67
|
|
|
62
68
|
const hasUrlBinding = !! metadata?.bindings?.url && !! id;
|
|
63
69
|
const expectedSource =
|
|
@@ -65,6 +71,47 @@ export function useEntityBinding( { clientId, attributes } ) {
|
|
|
65
71
|
const hasCorrectBinding =
|
|
66
72
|
hasUrlBinding && metadata?.bindings?.url?.source === expectedSource;
|
|
67
73
|
|
|
74
|
+
// Check if the bound entity is available (not deleted).
|
|
75
|
+
const isBoundEntityAvailable = useSelect(
|
|
76
|
+
( select ) => {
|
|
77
|
+
// First check: metadata/binding must exist
|
|
78
|
+
if ( ! hasCorrectBinding || ! id ) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const isPostType = kind === 'post-type';
|
|
83
|
+
const isTaxonomy = kind === 'taxonomy';
|
|
84
|
+
|
|
85
|
+
// Only check entity availability for post types and taxonomies.
|
|
86
|
+
if ( ! isPostType && ! isTaxonomy ) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Skip check in disabled contexts to avoid unnecessary requests.
|
|
91
|
+
if ( blockEditingMode === 'disabled' ) {
|
|
92
|
+
return true; // Assume available in disabled contexts.
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Second check: entity must exist
|
|
96
|
+
const { getEntityRecord, hasFinishedResolution } =
|
|
97
|
+
select( coreStore );
|
|
98
|
+
|
|
99
|
+
// Use the correct entity type based on kind.
|
|
100
|
+
const entityType = isTaxonomy ? 'taxonomy' : 'postType';
|
|
101
|
+
const entityRecord = getEntityRecord( entityType, type, id );
|
|
102
|
+
const hasResolved = hasFinishedResolution( 'getEntityRecord', [
|
|
103
|
+
entityType,
|
|
104
|
+
type,
|
|
105
|
+
id,
|
|
106
|
+
] );
|
|
107
|
+
|
|
108
|
+
// If resolution has finished and entityRecord is undefined, the entity was deleted.
|
|
109
|
+
// Return true if entity exists, false if deleted.
|
|
110
|
+
return hasResolved ? entityRecord !== undefined : true;
|
|
111
|
+
},
|
|
112
|
+
[ kind, type, id, hasCorrectBinding, blockEditingMode ]
|
|
113
|
+
);
|
|
114
|
+
|
|
68
115
|
const clearBinding = useCallback( () => {
|
|
69
116
|
if ( hasUrlBinding ) {
|
|
70
117
|
updateBlockBindings( { url: undefined } );
|
|
@@ -73,11 +120,11 @@ export function useEntityBinding( { clientId, attributes } ) {
|
|
|
73
120
|
|
|
74
121
|
const createBinding = useCallback(
|
|
75
122
|
( updatedAttributes ) => {
|
|
76
|
-
// Use updated attributes if provided, otherwise fall back to closure attributes
|
|
77
|
-
// updatedAttributes needed to access the most up-to-date data when called synchronously
|
|
123
|
+
// Use updated attributes if provided, otherwise fall back to closure attributes.
|
|
124
|
+
// updatedAttributes needed to access the most up-to-date data when called synchronously.
|
|
78
125
|
const kindToUse = updatedAttributes?.kind ?? kind;
|
|
79
126
|
|
|
80
|
-
// Avoid creating binding if no kind is provided
|
|
127
|
+
// Avoid creating binding if no kind is provided.
|
|
81
128
|
if ( ! kindToUse ) {
|
|
82
129
|
return;
|
|
83
130
|
}
|
|
@@ -91,14 +138,15 @@ export function useEntityBinding( { clientId, attributes } ) {
|
|
|
91
138
|
'Failed to create entity binding:',
|
|
92
139
|
error.message
|
|
93
140
|
);
|
|
94
|
-
// Don't create binding if validation fails
|
|
141
|
+
// Don't create binding if validation fails.
|
|
95
142
|
}
|
|
96
143
|
},
|
|
97
|
-
[ updateBlockBindings, kind
|
|
144
|
+
[ updateBlockBindings, kind ]
|
|
98
145
|
);
|
|
99
146
|
|
|
100
147
|
return {
|
|
101
148
|
hasUrlBinding: hasCorrectBinding,
|
|
149
|
+
isBoundEntityAvailable,
|
|
102
150
|
clearBinding,
|
|
103
151
|
createBinding,
|
|
104
152
|
};
|
package/src/paragraph/index.js
CHANGED
|
@@ -13,6 +13,7 @@ import edit from './edit';
|
|
|
13
13
|
import metadata from './block.json';
|
|
14
14
|
import save from './save';
|
|
15
15
|
import transforms from './transforms';
|
|
16
|
+
import variations from './variations';
|
|
16
17
|
|
|
17
18
|
const { name } = metadata;
|
|
18
19
|
|
|
@@ -54,6 +55,7 @@ export const settings = {
|
|
|
54
55
|
},
|
|
55
56
|
edit,
|
|
56
57
|
save,
|
|
58
|
+
variations,
|
|
57
59
|
};
|
|
58
60
|
|
|
59
61
|
export const init = () => initBlock( { name, metadata, settings } );
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { __ } from '@wordpress/i18n';
|
|
5
|
+
import { Path, SVG } from '@wordpress/primitives';
|
|
6
|
+
import { paragraph } from '@wordpress/icons';
|
|
7
|
+
|
|
8
|
+
const variations = [
|
|
9
|
+
{
|
|
10
|
+
name: 'paragraph',
|
|
11
|
+
title: __( 'Paragraph' ),
|
|
12
|
+
description: __(
|
|
13
|
+
'Start with the basic building block of all narrative.'
|
|
14
|
+
),
|
|
15
|
+
isDefault: true,
|
|
16
|
+
scope: [ 'block', 'inserter', 'transform' ],
|
|
17
|
+
attributes: { fitText: undefined },
|
|
18
|
+
icon: paragraph,
|
|
19
|
+
},
|
|
20
|
+
// There is a hardcoded workaround in packages/block-editor/src/store/selectors.js
|
|
21
|
+
// to make Stretchy variations appear as the last of their sections in the inserter.
|
|
22
|
+
{
|
|
23
|
+
name: 'stretchy-paragraph',
|
|
24
|
+
title: __( 'Stretchy Paragraph' ),
|
|
25
|
+
description: __( 'Paragraph that resizes to fit its container.' ),
|
|
26
|
+
icon: (
|
|
27
|
+
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
|
|
28
|
+
<Path d="M3 9c0 2.8 2.2 5 5 5v-.2V20h1.5V5.5H12V20h1.5V5.5h3V4H8C5.2 4 3 6.2 3 9Zm16.2-.2v1.5h2.2L17.7 14l1.1 1.1 3.7-3.7v2.2H24V8.8h-4.8Z" />
|
|
29
|
+
</SVG>
|
|
30
|
+
),
|
|
31
|
+
attributes: {
|
|
32
|
+
fitText: true,
|
|
33
|
+
},
|
|
34
|
+
scope: [ 'inserter', 'transform' ],
|
|
35
|
+
isActive: ( blockAttributes ) => blockAttributes.fitText === true,
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
export default variations;
|