// Captures each template at three widths and writes shots.json for gallery.cjs. // Usage: node capture.cjs (capture.sh runs this) const { chromium } = require( process.env.PLAYWRIGHT_PATH || 'playwright' ); const sharp = require( process.env.SHARP_PATH || 'sharp' ); const fs = require( 'fs' ); const [ base, out ] = process.argv.slice( 2 ); // 1440 is a desktop. 782 and 480 are the largest widths of the editor's // tablet and mobile breakpoints. const widths = [ 1440, 782, 480 ]; // WebP can't be taller than this, so a longer page stays a PNG. const WEBP_MAX = 16383; // What to capture, as [ file name, title, path ]. The fixture's mu-plugin // (fixture/mu-plugins/theme-screenshots.php) says how the site is set up. const shotList = ( info ) => { const shots = info.mode === 'page' ? [ [ 'home', 'Homepage', '/' ], [ 'posts', 'Posts page', '/blog/' ], [ 'posts-page-2', 'Posts page, page 2', '/blog/page/2/' ], ] : [ [ 'home', 'Homepage (latest posts)', '/' ], [ 'home-page-2', 'Homepage, page 2', '/page/2/' ], ]; shots.push( [ 'archive', 'Category archive', '/category/sample/' ], [ 'search', 'Search results', '/?s=sample' ], [ 'search-no-results', 'Search with no results', '/?s=zzqqxx' ], [ 'single', 'Single post', '/alignments/' ], [ 'page', 'Page', '/sample-page/' ], [ '404', 'Page not found', '/this-page-does-not-exist/' ] ); info.custom.forEach( ( { template, path }, i ) => shots.push( [ `custom-${ i + 1 }`, `Page using ${ template }`, path ] ) ); if ( info.store ) { shots.push( [ 'shop', 'Shop', '/shop/' ], [ 'product-category', 'Product category', info.store.category ], [ 'product-search', 'Product search', '/?s=hoodie&post_type=product' ], [ 'product-simple', 'Simple product', '/product/hoodie-with-logo/' ], [ 'product-variable', 'Variable product', '/product/hoodie/' ], [ 'product-grouped', 'Grouped product', '/product/logo-collection/' ], [ 'product-external', 'External product', '/product/wordpress-pennant/' ], [ 'cart-empty', 'Empty cart', '/cart/' ], // Opening this URL adds the product to the cart, so the next shots have one. [ 'cart', 'Cart', `/cart/?add-to-cart=${ info.store.simple }` ], [ 'checkout', 'Checkout', '/checkout/' ], [ 'my-account', 'My account, logged out', '/my-account/' ], [ 'coming-soon', 'Coming soon', '/?shot-coming-soon' ] ); if ( info.store.order ) { shots.push( [ 'order-confirmation', 'Order confirmation', info.store.order ] ); } } return shots.filter( ( [ , , path ] ) => path ); }; // "pipeline//single" -> "single.html". A template the theme doesn't have says // where it came from, e.g. "single-product (WooCommerce)". const templateLabel = ( id, info ) => { if ( ! id || ! id.includes( '//' ) ) { return ''; } const [ source, slug ] = id.split( '//' ); if ( info.theme.includes( source ) ) { return `${ slug }.html`; } return `${ slug } (${ source.startsWith( 'woocommerce' ) ? 'WooCommerce' : source })`; }; const settle = ( page ) => page.waitForLoadState( 'networkidle', { timeout: 15000 } ).catch( () => {} ); const save = async ( png, file ) => { const { height } = await sharp( png ).metadata(); if ( height > WEBP_MAX ) { await sharp( png ).png( { compressionLevel: 9 } ).toFile( `${ out }/${ file }.png` ); return `${ file }.png`; } await sharp( png ).webp( { quality: 82 } ).toFile( `${ out }/${ file }.webp` ); return `${ file }.webp`; }; ( async () => { fs.mkdirSync( out, { recursive: true } ); const info = await ( await fetch( `${ base }/?shot-info` ) ).json(); const shots = shotList( info ).map( ( [ name, title, path ] ) => ( { name, title, path, template: '', files: {}, notes: [] } ) ); const browser = await chromium.launch( { executablePath: process.env.CHROME_PATH } ); for ( const width of widths ) { // A new page per width also starts a new session, so each width begins with an empty cart. const page = await browser.newPage( { viewport: { width, height: 900 } } ); for ( const shot of shots ) { // Wait for the page to load, then give the network a short time to settle. // Some pages never go fully quiet (Playground on a slow runner, polling // scripts), so a busy network is not an error. const response = await page.goto( base + shot.path, { waitUntil: 'load', timeout: 90000 } ); await settle( page ); if ( ( await page.content() ).includes( 'WordPress is not ready yet' ) ) { throw new Error( `${ shot.path }: Playground is still setting up WordPress` ); } shot.template ||= templateLabel( response.headers()[ 'x-shot-template' ], info ); // WooCommerce renders Coming soon without going through the template loader. if ( shot.name === 'coming-soon' && ! shot.template ) { shot.template = info.templates.includes( 'coming-soon' ) ? 'coming-soon.html' : 'coming-soon (WooCommerce)'; } await page.evaluate( async () => { // Load every image now, at the size it has on screen, so the full-page // capture (which resizes the window) doesn't swap in a srcset file that // isn't loaded yet and leave it blank. document.querySelectorAll( 'img' ).forEach( ( i ) => { i.loading = 'eager'; if ( i.sizes ) { i.sizes = Math.ceil( i.getBoundingClientRect().width || 1 ) + 'px'; } } ); for ( let y = 0; y < document.documentElement.scrollHeight; y += 600 ) { window.scrollTo( 0, y ); await new Promise( ( r ) => setTimeout( r, 50 ) ); } window.scrollTo( 0, 0 ); } ); await settle( page ); await page.evaluate( () => Promise.all( [ ...document.images ].map( ( i ) => i.decode().catch( () => {} ) ) ) ); shot.files[ width ] = await save( await page.screenshot( { fullPage: true } ), `${ shot.name }-${ width }` ); const overflow = await page.evaluate( () => document.documentElement.scrollWidth > window.innerWidth ); if ( overflow ) { shot.notes.push( `${ width }px: the page scrolls sideways` ); } console.log( `${ shot.name }-${ width } ${ shot.template }${ overflow ? ': HORIZONTAL OVERFLOW' : '' }` ); } await page.close(); } await browser.close(); // The theme's templates that no screenshot shows. const shown = new Set( shots.map( ( s ) => s.template ) ); const unshown = info.templates .filter( ( slug ) => ! shown.has( `${ slug }.html` ) ) .sort() .map( ( slug ) => ( slug === 'index' && info.templates.includes( 'home' ) ? 'index.html (home.html is used instead)' : `${ slug }.html` ) ); fs.writeFileSync( `${ out }/shots.json`, JSON.stringify( { widths, shots, unshown }, null, '\t' ) ); } )().catch( ( e ) => { console.error( e ); process.exit( 1 ); } );