Repository navigation
Command Palette: Make every admin screen faster by loading the palette only when it is opened #15871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rene-hermenau
wants to merge
7
commits into
WordPress:trunk
Choose a base branch
from
rene-hermenau:66642-command-palette-on-demand
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+857
−17
Open
Command Palette: Make every admin screen faster by loading the palette only when it is opened #15871
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7a7d8b3
Script Loader: Load the Command Palette on demand.
rene-hermenau 8b749f6
Script Loader: Add an action for Command Palette assets.
rene-hermenau f7409ae
Merge branch 'trunk' into 66642-command-palette-on-demand
rene-hermenau 5d3b0d4
Build/Test Tools: Type-check the Command Palette loader.
rene-hermenau 6822400
Command Palette: Prefetch the palette once the page is idle and show …
rene-hermenau 4f43015
Command Palette: Add the palette on idle for people who have used it.
rene-hermenau d0f48f9
Command Palette: Declare the user settings globals for type checking.
rene-hermenau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,319 @@ | ||
| /** | ||
| * @output wp-includes/js/wp-command-palette-loader.js | ||
| */ | ||
|
|
||
| /** | ||
| * Opens the Command Palette, loading its assets on first use. | ||
| * | ||
| * On screens that do not load the Command Palette up front, its scripts and | ||
| * styles are printed into an inert template by | ||
| * wp_print_command_palette_assets_template(). For someone who has not used the | ||
| * palette yet, they are only downloaded once the page is idle, and added to the | ||
| * page when the palette is first requested, with the keyboard shortcut or the | ||
| * toolbar button, so that they do not delay every admin page load. For someone | ||
| * who has used it before, they are added to the page once it is idle, so that | ||
| * the palette opens right away. | ||
| * | ||
| * @since 7.3.0 | ||
| */ | ||
| ( function ( window, document ) { | ||
| const TEMPLATE_ID = 'wp-command-palette-assets'; | ||
| const TOOLBAR_ITEM_ID = 'wp-admin-bar-command-palette'; | ||
| const TOOLBAR_LINK_SELECTOR = '#' + TOOLBAR_ITEM_ID + ' > .ab-item'; | ||
| const USED_PALETTE_SETTING = 'commandpalette'; | ||
| const IS_APPLE_OS = /Mac|iPod|iPhone|iPad/.test( window.navigator.platform ); | ||
|
|
||
| /** @type {Promise<void>|null} */ | ||
| let paletteAssetsLoading = null; | ||
| let arePaletteAssetsLoaded = false; | ||
| let isWaitingToOpen = false; | ||
|
|
||
| /** | ||
| * Tells whether a key press is the shortcut that opens the Command Palette. | ||
| * | ||
| * @param {KeyboardEvent} event The key press. | ||
| * @return {boolean} Whether the key press is Cmd+K on Apple devices or Ctrl+K elsewhere. | ||
| */ | ||
| function isOpenPaletteShortcut( event ) { | ||
| const primaryModifier = IS_APPLE_OS ? event.metaKey : event.ctrlKey; | ||
|
|
||
| return primaryModifier && ! event.altKey && ! event.shiftKey && 'k' === event.key?.toLowerCase(); | ||
| } | ||
|
|
||
| /** | ||
| * Tells whether the Command Palette assets have not finished loading into the page yet. | ||
| * | ||
| * @return {boolean} Whether the assets are still in their template or still loading. | ||
| */ | ||
| function arePaletteAssetsPending() { | ||
| return ! arePaletteAssetsLoaded && ( null !== paletteAssetsLoading || null !== document.getElementById( TEMPLATE_ID ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tells whether the current user has opened the Command Palette before. | ||
| * | ||
| * @return {boolean} Whether the user setting records an earlier use. | ||
| */ | ||
| function hasUserUsedPalette() { | ||
| return 'function' === typeof window.getUserSetting && 'used' === window.getUserSetting( USED_PALETTE_SETTING ); | ||
| } | ||
|
|
||
| /** | ||
| * Records in the user settings that the current user has opened the Command Palette. | ||
| */ | ||
| function rememberUserUsedPalette() { | ||
| if ( ! hasUserUsedPalette() && 'function' === typeof window.setUserSetting ) { | ||
| window.setUserSetting( USED_PALETTE_SETTING, 'used' ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Adds a script to the page and waits until it has run. | ||
| * | ||
| * A script added with the DOM runs as soon as it is inserted when it is | ||
| * inline, but only after it has loaded when it has a source. Waiting for | ||
| * each one keeps the order in which they were printed. | ||
| * | ||
| * @param {HTMLScriptElement} templateScript The script as printed in the template. | ||
| * @return {Promise<void>} Resolves once the script has run or failed to load. | ||
| */ | ||
| function insertScript( templateScript ) { | ||
| const script = document.createElement( 'script' ); | ||
|
|
||
| for ( const attribute of templateScript.attributes ) { | ||
| script.setAttribute( attribute.name, attribute.value ); | ||
| } | ||
| script.text = templateScript.text; | ||
|
|
||
| if ( ! script.hasAttribute( 'src' ) ) { | ||
| document.body.appendChild( script ); | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| return new Promise( ( resolve ) => { | ||
| script.onload = () => resolve(); | ||
| script.onerror = () => resolve(); | ||
| document.body.appendChild( script ); | ||
| } ); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a stylesheet to the page and waits until it has loaded. | ||
| * | ||
| * @param {HTMLLinkElement|HTMLStyleElement} templateStyle The style as printed in the template. | ||
| * @return {Promise<void>} Resolves once the stylesheet has loaded or failed to load. | ||
| */ | ||
| function insertStyle( templateStyle ) { | ||
| const style = document.importNode( templateStyle, true ); | ||
|
|
||
| if ( 'LINK' !== style.tagName ) { | ||
| document.head.appendChild( style ); | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| return new Promise( ( resolve ) => { | ||
| style.onload = () => resolve(); | ||
| style.onerror = () => resolve(); | ||
| document.head.appendChild( style ); | ||
| } ); | ||
| } | ||
|
|
||
| /** | ||
| * Asks the browser to start downloading a script before it is inserted. | ||
| * | ||
| * @param {string} url The script URL. | ||
| */ | ||
| function preloadScript( url ) { | ||
| const link = document.createElement( 'link' ); | ||
|
|
||
| link.rel = 'preload'; | ||
| link.as = 'script'; | ||
| link.href = url; | ||
| document.head.appendChild( link ); | ||
| } | ||
|
|
||
| /** | ||
| * Asks the browser to download a file into its cache without running it. | ||
| * | ||
| * @param {string} url The file URL. | ||
| */ | ||
| function prefetchFile( url ) { | ||
| const link = document.createElement( 'link' ); | ||
|
|
||
| link.rel = 'prefetch'; | ||
| link.href = url; | ||
| document.head.appendChild( link ); | ||
| } | ||
|
|
||
| /** | ||
| * Downloads the Command Palette's scripts and styles without running them. | ||
| * | ||
| * This takes the download off the first time the palette is opened, while the page itself | ||
| * still does not parse or run the palette's scripts until then. | ||
| */ | ||
| function prefetchPaletteAssets() { | ||
| const template = /** @type {HTMLTemplateElement|null} */ ( document.getElementById( TEMPLATE_ID ) ); | ||
|
|
||
| if ( ! template ) { | ||
| return; | ||
| } | ||
|
|
||
| for ( const asset of template.content.querySelectorAll( 'script[src], link[rel="stylesheet"][href]' ) ) { | ||
| const url = asset.getAttribute( 'src' ) || asset.getAttribute( 'href' ); | ||
|
|
||
| if ( url ) { | ||
| prefetchFile( url ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Runs a callback once the page has loaded and the browser is idle. | ||
| * | ||
| * @param {Function} callback The callback. | ||
| */ | ||
| function whenPageIsIdle( callback ) { | ||
| const scheduleWhenIdle = () => { | ||
| if ( 'requestIdleCallback' in window ) { | ||
| window.requestIdleCallback( () => callback() ); | ||
| } else { | ||
| setTimeout( callback, 1 ); | ||
| } | ||
| }; | ||
|
|
||
| if ( 'complete' === document.readyState ) { | ||
| scheduleWhenIdle(); | ||
| } else { | ||
| window.addEventListener( 'load', scheduleWhenIdle, { once: true } ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Shows that the Command Palette is loading, or that it has finished loading. | ||
| * | ||
| * @param {boolean} isPaletteLoading Whether the palette is loading. | ||
| */ | ||
| function showPaletteLoading( isPaletteLoading ) { | ||
| const toolbarItem = document.getElementById( TOOLBAR_ITEM_ID ); | ||
|
|
||
| if ( toolbarItem ) { | ||
| toolbarItem.classList.toggle( 'is-loading', isPaletteLoading ); | ||
| toolbarItem.querySelector( '.ab-item' )?.setAttribute( 'aria-busy', isPaletteLoading ? 'true' : 'false' ); | ||
| } | ||
|
|
||
| if ( isPaletteLoading ) { | ||
| window.wp.a11y.speak( window.wp.i18n.__( 'Loading the command palette.' ) ); | ||
| } | ||
|
rene-hermenau marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Adds the Command Palette's scripts and styles from the template to the page. | ||
| * | ||
| * All scripts are preloaded at once and then run one after the other. | ||
| * | ||
| * @param {DocumentFragment} assets The template content. | ||
| * @return {Promise<void>} Resolves once every script has run and every stylesheet has loaded. | ||
| */ | ||
| async function insertPaletteAssets( assets ) { | ||
| const scripts = Array.from( assets.querySelectorAll( 'script' ) ); | ||
| const styles = Array.from( | ||
| assets.querySelectorAll( 'link[rel="stylesheet"], style' ), | ||
| ( style ) => /** @type {HTMLLinkElement|HTMLStyleElement} */ ( style ) | ||
| ); | ||
|
|
||
| for ( const script of scripts ) { | ||
| const url = script.getAttribute( 'src' ); | ||
|
|
||
| if ( url ) { | ||
| preloadScript( url ); | ||
| } | ||
| } | ||
|
|
||
| const stylesLoaded = Promise.all( styles.map( insertStyle ) ); | ||
|
|
||
| for ( const script of scripts ) { | ||
| await insertScript( script ); | ||
| } | ||
|
|
||
| await stylesLoaded; | ||
| } | ||
|
|
||
| /** | ||
| * Moves the Command Palette's assets from the template into the page, once. | ||
| * | ||
| * @return {Promise<void>} Resolves once the assets are in the page. | ||
| */ | ||
| function loadPaletteAssets() { | ||
| if ( ! paletteAssetsLoading ) { | ||
| const template = /** @type {HTMLTemplateElement|null} */ ( document.getElementById( TEMPLATE_ID ) ); | ||
|
|
||
| if ( template ) { | ||
| template.remove(); | ||
| paletteAssetsLoading = insertPaletteAssets( template.content ); | ||
| } else { | ||
| paletteAssetsLoading = Promise.resolve(); | ||
| } | ||
|
|
||
| paletteAssetsLoading.then( () => { | ||
| arePaletteAssetsLoaded = true; | ||
| } ); | ||
| } | ||
|
|
||
| return paletteAssetsLoading; | ||
| } | ||
|
|
||
| /** | ||
| * Opens the Command Palette, adding its assets to the page first if needed. | ||
| */ | ||
| async function openPalette() { | ||
| if ( isWaitingToOpen ) { | ||
| return; | ||
| } | ||
|
|
||
| rememberUserUsedPalette(); | ||
|
|
||
| if ( ! arePaletteAssetsLoaded ) { | ||
| isWaitingToOpen = true; | ||
| showPaletteLoading( true ); | ||
| await loadPaletteAssets(); | ||
| showPaletteLoading( false ); | ||
| isWaitingToOpen = false; | ||
| } | ||
|
|
||
| window.wp.data.dispatch( 'core/commands' ).open(); | ||
| } | ||
|
|
||
| /** | ||
| * Prepares the Command Palette once the page is idle. | ||
| * | ||
| * Someone who has used the palette before gets it added to the page, so it opens | ||
| * right away. Everyone else only gets its files downloaded. | ||
| */ | ||
| function preparePaletteWhenIdle() { | ||
| if ( hasUserUsedPalette() ) { | ||
| loadPaletteAssets(); | ||
| } else { | ||
| prefetchPaletteAssets(); | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener( 'keydown', ( event ) => { | ||
| if ( event.defaultPrevented || ! isOpenPaletteShortcut( event ) || ! arePaletteAssetsPending() ) { | ||
| return; | ||
| } | ||
|
|
||
| event.preventDefault(); | ||
| openPalette(); | ||
| } ); | ||
|
|
||
| document.addEventListener( 'click', ( event ) => { | ||
| if ( ! ( event.target instanceof Element ) || ! event.target.closest( TOOLBAR_LINK_SELECTOR ) ) { | ||
| return; | ||
| } | ||
|
|
||
| event.preventDefault(); | ||
| openPalette(); | ||
| } ); | ||
|
|
||
| whenPageIsIdle( preparePaletteWhenIdle ); | ||
| }( window, document ) ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.