|
1 | 1 | /**
|
2 | 2 | * WordPress dependencies
|
3 | 3 | */
|
4 |
| -import { |
5 |
| - store, |
6 |
| - privateApis, |
7 |
| - getConfig, |
8 |
| - getElement, |
9 |
| -} from '@wordpress/interactivity'; |
| 4 | +import { store, privateApis, getConfig } from '@wordpress/interactivity'; |
10 | 5 |
|
11 | 6 | /**
|
12 | 7 | * Internal dependencies
|
@@ -200,34 +195,23 @@ export const { state, actions } = store( 'core/router', {
|
200 | 195 | * needed, and updates any interactive regions whose contents have
|
201 | 196 | * changed. It also creates a new entry in the browser session history.
|
202 | 197 | *
|
203 |
| - * @param {string|Event} eventOrUrl The page href or the event handler in case it is used directly in a directive. |
204 |
| - * @param {Object} [options] Options object. |
205 |
| - * @param {boolean} [options.force] If true, it forces re-fetching the URL. |
206 |
| - * @param {string} [options.html] HTML string to be used instead of fetching the requested URL. |
207 |
| - * @param {boolean} [options.replace] If true, it replaces the current entry in the browser session history. |
208 |
| - * @param {number} [options.timeout] Time until the navigation is aborted, in milliseconds. Default is 10000. |
209 |
| - * @param {boolean} [options.loadingAnimation] Whether an animation should be shown while navigating. Default to `true`. |
210 |
| - * @param {boolean} [options.screenReaderAnnouncement] Whether a message for screen readers should be announced while navigating. Default to `true`. |
| 198 | + * @param {string} href The page href. |
| 199 | + * @param {Object} [options] Options object. |
| 200 | + * @param {boolean} [options.force] If true, it forces re-fetching the URL. |
| 201 | + * @param {string} [options.html] HTML string to be used instead of fetching the requested URL. |
| 202 | + * @param {boolean} [options.replace] If true, it replaces the current entry in the browser session history. |
| 203 | + * @param {number} [options.timeout] Time until the navigation is aborted, in milliseconds. Default is 10000. |
| 204 | + * @param {boolean} [options.loadingAnimation] Whether an animation should be shown while navigating. Default to `true`. |
| 205 | + * @param {boolean} [options.screenReaderAnnouncement] Whether a message for screen readers should be announced while navigating. Default to `true`. |
211 | 206 | *
|
212 | 207 | * @return {Promise} Promise that resolves once the navigation is completed or aborted.
|
213 | 208 | */
|
214 |
| - *navigate( eventOrUrl, options = {} ) { |
| 209 | + *navigate( href, options = {} ) { |
215 | 210 | const { clientNavigationDisabled } = getConfig();
|
216 |
| - const url = ! ( eventOrUrl instanceof Event ) && eventOrUrl; |
217 |
| - const event = eventOrUrl instanceof Event && eventOrUrl; |
218 |
| - let ref; |
219 |
| - // The getElement() function can only be called when it is an event. |
220 |
| - if ( event ) { |
221 |
| - ref = getElement().ref; |
222 |
| - } |
223 |
| - if ( |
224 |
| - clientNavigationDisabled || |
225 |
| - ! ( url || ( isValidLink( ref ) && isValidEvent( event ) ) ) |
226 |
| - ) { |
227 |
| - yield forcePageReload( url ); |
| 211 | + if ( clientNavigationDisabled ) { |
| 212 | + yield forcePageReload( href ); |
228 | 213 | }
|
229 |
| - if ( event ) event.preventDefault(); |
230 |
| - const href = url ? url : ref.href; |
| 214 | + |
231 | 215 | const pagePath = getPagePath( href );
|
232 | 216 | const { navigation } = state;
|
233 | 217 | const {
|
@@ -316,29 +300,50 @@ export const { state, actions } = store( 'core/router', {
|
316 | 300 | * The function normalizes the URL and stores internally the fetch
|
317 | 301 | * promise, to avoid triggering a second fetch for an ongoing request.
|
318 | 302 | *
|
319 |
| - * @param {string|Event} eventOrUrl The page href or the event handler in case it is used directly in a directive. |
320 |
| - * @param {Object} [options] Options object. |
321 |
| - * @param {boolean} [options.force] Force fetching the URL again. |
322 |
| - * @param {string} [options.html] HTML string to be used instead of fetching the requested URL. |
| 303 | + * @param {string} url The page URL. |
| 304 | + * @param {Object} [options] Options object. |
| 305 | + * @param {boolean} [options.force] Force fetching the URL again. |
| 306 | + * @param {string} [options.html] HTML string to be used instead of fetching the requested URL. |
323 | 307 | */
|
324 |
| - prefetch( eventOrUrl, options = {} ) { |
325 |
| - const url = ! ( eventOrUrl instanceof Event ) && eventOrUrl; |
326 |
| - const event = eventOrUrl instanceof Event && eventOrUrl; |
327 |
| - let ref; |
328 |
| - // The getElement() function can only be called when it is an event. |
329 |
| - if ( event ) { |
330 |
| - ref = getElement().ref; |
331 |
| - } |
| 308 | + prefetch( url, options = {} ) { |
332 | 309 | const { clientNavigationDisabled } = getConfig();
|
333 |
| - if ( |
334 |
| - clientNavigationDisabled || |
335 |
| - ! ( url || ( isValidLink( ref ) && isValidEvent( event ) ) ) |
336 |
| - ) |
337 |
| - return; |
338 |
| - const pagePath = getPagePath( url ? url : ref.href ); |
| 310 | + if ( clientNavigationDisabled ) return; |
| 311 | + |
| 312 | + const pagePath = getPagePath( url ); |
339 | 313 | if ( options.force || ! pages.has( pagePath ) ) {
|
340 | 314 | pages.set( pagePath, fetchPage( pagePath, options ) );
|
341 | 315 | }
|
342 | 316 | },
|
343 | 317 | },
|
344 | 318 | } );
|
| 319 | + |
| 320 | +// Add click and prefetch to all links. |
| 321 | +if ( process.env.IS_GUTENBERG_PLUGIN ) { |
| 322 | + if ( navigationMode === 'fullPage' ) { |
| 323 | + // Navigate on click. |
| 324 | + document.addEventListener( |
| 325 | + 'click', |
| 326 | + function ( event ) { |
| 327 | + const ref = event.target.closest( 'a' ); |
| 328 | + if ( isValidLink( ref ) && isValidEvent( event ) ) { |
| 329 | + event.preventDefault(); |
| 330 | + actions.navigate( ref.href ); |
| 331 | + } |
| 332 | + }, |
| 333 | + true |
| 334 | + ); |
| 335 | + // Prefetch on hover. |
| 336 | + document.addEventListener( |
| 337 | + 'mouseenter', |
| 338 | + function ( event ) { |
| 339 | + if ( event.target?.nodeName === 'A' ) { |
| 340 | + const ref = event.target.closest( 'a' ); |
| 341 | + if ( isValidLink( ref ) && isValidEvent( event ) ) { |
| 342 | + actions.prefetch( ref.href ); |
| 343 | + } |
| 344 | + } |
| 345 | + }, |
| 346 | + true |
| 347 | + ); |
| 348 | + } |
| 349 | +} |
0 commit comments