diff --git a/Gruntfile.js b/Gruntfile.js index 05bdfda0a58c1..71663a1a16860 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -541,6 +541,8 @@ module.exports = function(grunt) { [ WORKING_DIR + 'wp-admin/js/common.js' ]: [ './src/js/_enqueues/admin/common.js' ], [ WORKING_DIR + 'wp-admin/js/custom-background.js' ]: [ './src/js/_enqueues/admin/custom-background.js' ], [ WORKING_DIR + 'wp-admin/js/custom-header.js' ]: [ './src/js/_enqueues/admin/custom-header.js' ], + [ WORKING_DIR + 'wp-admin/js/teletype.js' ]: [ './src/js/_enqueues/admin/teletype.js' ], + [ WORKING_DIR + 'wp-admin/js/teletype-loader.js' ]: [ './src/js/_enqueues/admin/teletype-loader.js' ], [ WORKING_DIR + 'wp-admin/js/customize-controls.js' ]: [ './src/js/_enqueues/wp/customize/controls.js' ], [ WORKING_DIR + 'wp-admin/js/customize-nav-menus.js' ]: [ './src/js/_enqueues/wp/customize/nav-menus.js' ], [ WORKING_DIR + 'wp-admin/js/customize-widgets.js' ]: [ './src/js/_enqueues/wp/customize/widgets.js' ], diff --git a/src/js/_enqueues/admin/teletype-loader.js b/src/js/_enqueues/admin/teletype-loader.js new file mode 100644 index 0000000000000..f06ec63279139 --- /dev/null +++ b/src/js/_enqueues/admin/teletype-loader.js @@ -0,0 +1,87 @@ +/** + * Listens to the command palette for the teletype. + * + * @output wp-admin/js/teletype-loader.js + */ + +/** + * Watches Enter in the command palette and loads the teletype on a match. + * + * @param {any} wp The global WordPress JS object. + * @param {WPTeletypeSettings|undefined} settings Settings printed by wp_enqueue_command_palette_assets(). + */ +( ( wp, settings ) => { + 'use strict'; + + if ( ! wp || ! wp.data || ! settings || ! settings.i18n ) { + return; + } + + /** + * Lowercases text and strips its whitespace and punctuation, in any script. + * + * @param {string} value Text to normalize. + * @return {string} Normalized text. + */ + const normalize = ( value ) => + value.normalize( 'NFKC' ).toLowerCase().replace( /[\s\p{P}]/gu, '' ); + + const { name, src, i18n } = settings; + const trigger = normalize( i18n.trigger || '' ); + + /** + * Loads the teletype if needed, then runs it. + */ + const play = () => { + if ( wp.teletype ) { + wp.teletype.run( name, i18n ); + return; + } + + const script = document.createElement( 'script' ); + script.src = src; + script.addEventListener( 'load', () => { + wp.teletype?.run( name, i18n ); + } ); + + document.head.appendChild( script ); + }; + + document.addEventListener( + 'keydown', + ( event ) => { + const input = event.target; + + if ( 'Enter' !== event.key || ! ( input instanceof HTMLInputElement ) || ! input.value ) { + return; + } + + const store = wp.data.select( 'core/commands' ); + + if ( ! store?.isOpen() ) { + return; + } + + // Leave Enter to the palette whenever one of its results is active. + if ( + 'combobox' !== input.getAttribute( 'role' ) || + input.getAttribute( 'aria-activedescendant' ) + ) { + return; + } + + const value = normalize( input.value ); + + if ( ! value || value !== trigger ) { + return; + } + + event.preventDefault(); + event.stopPropagation(); + + wp.data.dispatch( 'core/commands' ).close(); + play(); + }, + true + ); +} )( window.wp, window.wpTeletype ); diff --git a/src/js/_enqueues/admin/teletype.js b/src/js/_enqueues/admin/teletype.js new file mode 100644 index 0000000000000..9c674a5a782a5 --- /dev/null +++ b/src/js/_enqueues/admin/teletype.js @@ -0,0 +1,363 @@ +/** + * The teletype. See #65907. + * + * @output wp-admin/js/teletype.js + */ + +/** + * Defines wp.teletype, which plays the scene. + * + * @param {any} wp The global WordPress JS object. + * @param {WPTeletypeSettings|undefined} settings Settings printed by wp_enqueue_command_palette_assets(). + */ +( ( wp, settings ) => { + 'use strict'; + + const TYPE_SPEED = 100, // Milliseconds per character. + LINE_PAUSE = 2000, // Between lines. + START_DELAY = 3000, // Before the first line. + FADE_TIME = 3000, // Cursor fade-in, once the lights go out. + ACT_PAUSE = 4000, // Between the two acts, and before the exit. + RAIN_SIZE = 16, // Glyph size of the falling rain, in pixels. + RAIN_SPEED = 60; // Milliseconds between rain rows. + + // Katakana and digits. + const GLYPHS = 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン0123456789'; + + /** + * Runs the falling glyph rain behind the scene. + * + * @param {HTMLElement} parent Element to render into. + * @return {() => void} Stops the rain and removes it. + */ + const rain = ( parent ) => { + const canvas = document.createElement( 'canvas' ); + const context = canvas.getContext( '2d' ); + + /** @type {number[]} */ + const columns = []; + let width = 0; + let height = 0; + let frame = 0; + let last = 0; + + canvas.className = 'rain'; + canvas.setAttribute( 'aria-hidden', 'true' ); + + /** + * Sizes the canvas to its parent and adds or drops columns to fit. + */ + const resize = () => { + const ratio = window.devicePixelRatio || 1; + + width = parent.clientWidth; + height = parent.clientHeight; + + canvas.width = width * ratio; + canvas.height = height * ratio; + canvas.style.width = `${ width }px`; + canvas.style.height = `${ height }px`; + + if ( context ) { + context.setTransform( ratio, 0, 0, ratio, 0, 0 ); + context.font = `${ RAIN_SIZE }px courier, monospace`; + } + + const count = Math.ceil( width / RAIN_SIZE ); + + // New columns start at a random height so they do not arrive as a straight line. + while ( columns.length < count ) { + columns.push( Math.random() * height ); + } + + columns.length = count; + }; + + /** + * Draws one row of glyphs and advances each column. + */ + const draw = () => { + if ( ! context ) { + return; + } + + // Painting over the last frame rather than clearing it leaves the trails. + context.fillStyle = 'rgba(0,0,0,.08)'; + context.fillRect( 0, 0, width, height ); + context.fillStyle = '#0f0'; + + columns.forEach( ( y, i ) => { + context.fillText( + GLYPHS.charAt( Math.floor( Math.random() * GLYPHS.length ) ), + i * RAIN_SIZE, + y + ); + + columns[ i ] = y > height && Math.random() > 0.975 ? 0 : y + RAIN_SIZE; + } ); + }; + + /** + * Draws a row once every RAIN_SPEED milliseconds. + * + * @param {DOMHighResTimeStamp} now Current frame time. + */ + const tick = ( now ) => { + frame = window.requestAnimationFrame( tick ); + + if ( now - last < RAIN_SPEED ) { + return; + } + + last = now; + draw(); + }; + + resize(); + parent.appendChild( canvas ); + window.addEventListener( 'resize', resize ); + frame = window.requestAnimationFrame( tick ); + + return () => { + window.cancelAnimationFrame( frame ); + window.removeEventListener( 'resize', resize ); + canvas.remove(); + }; + }; + + /** + * Plays the scene. + * + * @param {string} displayName The current user's display name. + * @param {WPTeletypeI18n} i18n Translated strings. + */ + const run = ( displayName, i18n ) => { + let aborted = false; + + /** @type {number|undefined} */ + let timer; + + /** @type {(() => void)|null} */ + let stopRain = null; + + const previousFocus = document.activeElement; + const previousOverflow = document.documentElement.style.overflow; + const actOneLines = [ ...i18n.actOne, ...i18n.countdown ]; + + const stylesheet = document.createElement( 'link' ); + stylesheet.rel = 'stylesheet'; + stylesheet.href = settings?.styles ?? ''; + + const overlay = document.createElement( 'div' ); + overlay.className = 'wp-teletype'; + overlay.setAttribute( 'role', 'dialog' ); + overlay.setAttribute( 'aria-modal', 'true' ); + overlay.setAttribute( 'aria-label', i18n.label ); + overlay.setAttribute( 'tabindex', '-1' ); + overlay.style.setProperty( '--wp-teletype-transition-duration', `${ FADE_TIME / 1000 }s` ); + + // Hidden from screen readers, which get whole lines from the live region. + const line = document.createElement( 'p' ); + line.setAttribute( 'aria-hidden', 'true' ); + + const narration = document.createElement( 'div' ); + narration.className = 'narration'; + narration.setAttribute( 'aria-live', 'polite' ); + narration.setAttribute( 'aria-atomic', 'true' ); + + const exit = document.createElement( 'button' ); + exit.type = 'button'; + exit.className = 'exit'; + exit.textContent = i18n.exit; + + const cursor = document.createElement( 'span' ); + cursor.className = 'cursor'; + cursor.textContent = '▌'; + + line.append( cursor ); + overlay.append( line, narration, exit ); + + /** + * Hands a whole line to the live region. + * + * @param {string} text Line to announce. + */ + const say = ( text ) => { + narration.textContent = text; + }; + + /** + * Calls a function after a delay unless the scene has ended. + * + * @param {number} ms Milliseconds to wait. + * @param {() => void} next Function to call. + */ + const wait = ( ms, next ) => { + timer = window.setTimeout( () => { + if ( ! aborted ) { + next(); + } + }, ms ); + }; + + /** + * Removes the typed text, leaving the cursor. + */ + const clear = () => { + while ( line.firstChild && line.firstChild !== cursor ) { + line.firstChild.remove(); + } + }; + + /** + * Starts a new line ahead of the cursor. + */ + const newline = () => { + cursor.before( document.createElement( 'br' ) ); + }; + + /** + * Types one line, a character at a time, ahead of the cursor. + * + * @param {string} text Text to type. + * @param {() => void} done Called once the line is typed. + */ + const type = ( text, done ) => { + const chars = [ ...text ]; + + const next = () => { + if ( aborted ) { + return; + } + + const char = chars.shift(); + + if ( undefined === char ) { + done(); + return; + } + + cursor.before( char ); + wait( TYPE_SPEED, next ); + }; + + next(); + }; + + /** + * Types act one on the white screen, each line under the last. + * + * @param {number} index Index of the line to type. + */ + const actOne = ( index ) => { + if ( index >= actOneLines.length ) { + lightsOut(); + return; + } + + say( actOneLines[ index ] ); + type( actOneLines[ index ], () => { + newline(); + wait( LINE_PAUSE, () => actOne( index + 1 ) ); + } ); + }; + + /** + * Switches the screen to black, starts the rain, and fades the cursor in. + */ + const lightsOut = () => { + overlay.className = 'wp-teletype is-dark'; + clear(); + cursor.className = 'cursor is-visible'; + + // The rain is decoration, so it sits out a reduced-motion preference. + if ( ! window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches ) { + stopRain = rain( overlay ); + } + + // Describe the rain once for screen readers. + say( i18n.rain ); + + wait( ACT_PAUSE, () => actTwo( 0 ) ); + }; + + /** + * Types act two one line at a time, then ends the scene after the last line. + * + * @param {number} index Index of the line to type. + */ + const actTwo = ( index ) => { + const text = i18n.actTwo[ index ].replace( '%s', () => displayName ); + + say( text ); + type( text, () => { + if ( index + 1 >= i18n.actTwo.length ) { + wait( ACT_PAUSE, abort ); + return; + } + + wait( LINE_PAUSE, () => { + clear(); + actTwo( index + 1 ); + } ); + } ); + }; + + /** + * Exits on Escape and keeps Tab on the Exit button. + * + * @param {KeyboardEvent} event The keydown event. + */ + const onKeydown = ( event ) => { + if ( 'Escape' === event.key ) { + abort(); + return; + } + + // The scene has one control, so the trap has one stop. + if ( 'Tab' === event.key ) { + event.preventDefault(); + exit.focus(); + } + }; + + /** + * Ends the scene and restores the page, its scrolling, and focus. + */ + const abort = () => { + if ( aborted ) { + return; + } + + aborted = true; + window.clearTimeout( timer ); + stopRain?.(); + + document.removeEventListener( 'keydown', onKeydown ); + document.documentElement.style.overflow = previousOverflow; + overlay.remove(); + stylesheet.remove(); + + if ( previousFocus instanceof HTMLElement ) { + previousFocus.focus(); + } + }; + + overlay.addEventListener( 'click', abort ); + + // Show the scene once its styles are in, so it never flashes unstyled. + stylesheet.addEventListener( 'load', () => { + document.addEventListener( 'keydown', onKeydown ); + document.body.append( overlay ); + document.documentElement.style.overflow = 'hidden'; + overlay.focus(); + + wait( START_DELAY, () => actOne( 0 ) ); + } ); + stylesheet.addEventListener( 'error', abort ); + + document.head.append( stylesheet ); + }; + + wp.teletype = { run }; +} )( /** @type {any} */ ( window.wp = window.wp || {} ), window.wpTeletype ); diff --git a/src/wp-admin/css/teletype.css b/src/wp-admin/css/teletype.css new file mode 100644 index 0000000000000..cef057934f850 --- /dev/null +++ b/src/wp-admin/css/teletype.css @@ -0,0 +1,103 @@ +/* Styles for the teletype, loaded by teletype.js when it runs. See #65907. */ +.wp-teletype { + position: fixed; + inset: 0; + z-index: 2147483647; + margin: 0; + padding: 2.5em; + font-family: courier, monospace; + font-size: 16px; + line-height: 1.6; + background: #fff; + color: #000; + overflow: hidden; + transition: background-color 0.6s linear, color 0.6s linear; +} + +.wp-teletype.is-dark { + background: #000; + color: #0f0; +} + +/* Focused only to move the reading position in, never by tabbing to it. */ +.wp-teletype:focus { + outline: none; +} + +.wp-teletype p { + position: relative; + z-index: 1; + margin: 0; + white-space: pre-wrap; +} + +.wp-teletype .rain { + position: absolute; + inset: 0; + opacity: 0.5; +} + +.wp-teletype .narration { + position: absolute; + width: 1px; + height: 1px; + margin: -1px; + padding: 0; + overflow: hidden; + clip-path: inset(50%); + white-space: nowrap; + border: 0; +} + +.wp-teletype .exit { + position: absolute; + z-index: 2; + top: 1.5em; + inset-inline-end: 1.5em; + padding: 0.7em 1.8em; + border: 0; + border-radius: 999px; + background: #2271b1; + color: #fff; + font-family: inherit; + font-size: 14px; + line-height: 1; + cursor: pointer; +} + +.wp-teletype .exit:hover { + background: #135e96; +} + +.wp-teletype .exit:focus-visible { + outline: 2px solid #fff; + outline-offset: 2px; +} + +.wp-teletype .cursor { + opacity: 0; + transition: opacity linear; + transition-duration: var(--wp-teletype-transition-duration); /* Set in teletype.js. */ +} + +.wp-teletype .cursor.is-visible { + opacity: 1; + animation: wp-teletype-blink 1s step-end infinite; +} + +@keyframes wp-teletype-blink { + 50% { + opacity: 0; + } +} + +@media (prefers-reduced-motion: reduce) { + .wp-teletype .cursor.is-visible { + animation: none; + } + + .wp-teletype, + .wp-teletype .cursor { + transition: none; + } +} diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index b37f2d5717e0e..38dbaf52aa57d 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1224,6 +1224,8 @@ function wp_default_scripts( $scripts ) { $scripts->add( 'password-toggle', "/wp-admin/js/password-toggle$suffix.js", array(), false, 1 ); $scripts->set_translations( 'password-toggle' ); + $scripts->add( 'wp-teletype-loader', "/wp-admin/js/teletype-loader$suffix.js", array( 'wp-data', 'wp-core-commands' ), false, 1 ); + $scripts->add( 'application-passwords', "/wp-admin/js/application-passwords$suffix.js", array( 'jquery', 'wp-util', 'wp-api-request', 'wp-date', 'wp-i18n', 'wp-hooks' ), false, 1 ); $scripts->set_translations( 'application-passwords' ); @@ -3961,6 +3963,68 @@ function wp_enqueue_command_palette_assets() { wp_json_encode( $command_palette_settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ) ); + + // Someone has to keep an eye on the terminal. + wp_enqueue_script( 'wp-teletype-loader' ); + wp_add_inline_script( + 'wp-teletype-loader', + sprintf( + 'window.wpTeletype = %s;', + wp_json_encode( + array( + 'src' => add_query_arg( + 'ver', + wp_get_wp_version(), + admin_url( 'js/teletype' . wp_scripts_get_suffix() . '.js' ) + ), + 'name' => wp_get_current_user()->display_name, + 'styles' => add_query_arg( + 'ver', + wp_get_wp_version(), + admin_url( 'css/teletype' . wp_scripts_get_suffix() . '.css' ) + ), + /* + * The teletype is an easter egg: typing the trigger phrase into the + * command palette plays a short scene. See #65907 for the details. + */ + 'i18n' => array( + /* translators: Easter egg trigger, typed into the command palette. Case, spaces and punctuation are ignored. Feel free to pick a phrase loved by your local community. See https://core.trac.wordpress.org/ticket/65907 */ + 'trigger' => _x( 'Code is poetry', 'easter egg trigger' ), + /* translators: Accessible name of the easter egg dialog. See https://core.trac.wordpress.org/ticket/65907 */ + 'label' => _x( 'A WordPress easter egg. Press Escape to leave.', 'easter egg' ), + /* translators: Button that closes the easter egg. See https://core.trac.wordpress.org/ticket/65907 */ + 'exit' => _x( 'Exit', 'easter egg' ), + /* translators: Read aloud to screen readers when the easter egg screen goes black and Matrix-style code rain starts. See https://core.trac.wordpress.org/ticket/65907 */ + 'rain' => _x( 'The screen goes black. Green code rains down it.', 'easter egg' ), + 'actOne' => array( + /* translators: Easter egg, typed out like a computer terminal. See https://core.trac.wordpress.org/ticket/65907 */ + _x( 'Self-reference detected.', 'easter egg' ), + /* translators: Easter egg, typed out like a computer terminal. See https://core.trac.wordpress.org/ticket/65907 */ + _x( 'Initiating infinite loop eschewal protocol.', 'easter egg' ), + /* translators: Easter egg, typed out like a computer terminal. The 3 starts a countdown that continues 2, 1. See https://core.trac.wordpress.org/ticket/65907 */ + _x( 'Running a routine maintenance cycle. Back in... 3', 'easter egg' ), + ), + 'countdown' => array( + /* translators: Easter egg countdown, typed after "Back in... 3". Translate only if your locale writes numbers with other digits. See https://core.trac.wordpress.org/ticket/65907 */ + _x( '2', 'easter egg countdown' ), + /* translators: Easter egg countdown, typed after "2". Translate only if your locale writes numbers with other digits. See https://core.trac.wordpress.org/ticket/65907 */ + _x( '1', 'easter egg countdown' ), + ), + 'actTwo' => array( + /* translators: %s: User display name. Easter egg quoting The Matrix (1999); use the line from your local release of the film if there is one. See https://core.trac.wordpress.org/ticket/65907 */ + _x( 'Wake up, %s...', 'easter egg' ), + /* translators: Easter egg quoting The Matrix (1999); use the line from your local release of the film if there is one. See https://core.trac.wordpress.org/ticket/65907 */ + _x( 'The Matrix has you...', 'easter egg' ), + /* translators: Easter egg quoting The Matrix (1999); use the line from your local release of the film if there is one. See https://core.trac.wordpress.org/ticket/65907 */ + _x( 'Follow the white rabbit.', 'easter egg' ), + ), + ), + ), + JSON_HEX_TAG | JSON_UNESCAPED_SLASHES + ) + ), + 'before' + ); } /** diff --git a/tsconfig.json b/tsconfig.json index 2a72ff4c550c2..216ce08bb4cb9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -26,6 +26,7 @@ "media-uploads", "browser-globals", "wp-emoji", + "wp-teletype", "codemirror/addon/lint/lint", "codemirror/addon/hint/show-hint" ] @@ -35,6 +36,8 @@ "src/js/_enqueues/admin/media-frame-upload.js", "src/js/_enqueues/admin/media-new-upload.js", "src/js/_enqueues/admin/media-upload-pipeline.js", + "src/js/_enqueues/admin/teletype-loader.js", + "src/js/_enqueues/admin/teletype.js", "src/js/_enqueues/admin/xfn.js", "src/js/_enqueues/lib/accordion.js", "src/js/_enqueues/lib/codemirror/htmlhint-kses.js", diff --git a/typings/wp-teletype/index.d.ts b/typings/wp-teletype/index.d.ts new file mode 100644 index 0000000000000..5ff7ea9eef9fe --- /dev/null +++ b/typings/wp-teletype/index.d.ts @@ -0,0 +1,45 @@ +/** + * Types for the teletype settings, printed by `wp_enqueue_command_palette_assets()`. + * + * These live here rather than beside either script because two files share them: the loader + * (js/_enqueues/admin/teletype-loader.js) reads the trigger, and the teletype + * (js/_enqueues/admin/teletype.js) loads the styles and plays the strings. + */ + +/** + * Translated strings for the teletype. + */ +interface WPTeletypeI18n { + /** Phrase that starts the scene when entered in the command palette. */ + trigger: string; + /** Accessible name of the dialog. */ + label: string; + /** Text of the Exit button. */ + exit: string; + /** Description of the rain for screen readers. */ + rain: string; + /** Lines typed on the white screen. */ + actOne: string[]; + /** The 2 and 1 typed after the white screen's last line. */ + countdown: string[]; + /** Lines typed on the black screen; %s is the display name. */ + actTwo: string[]; +} + +/** + * Teletype settings. + */ +interface WPTeletypeSettings { + /** URL of the teletype script, loaded on demand. */ + src: string; + /** The current user's display name. */ + name: string; + /** URL of the teletype stylesheet, loaded on demand. */ + styles: string; + /** Translated strings. */ + i18n: WPTeletypeI18n; +} + +interface Window { + wpTeletype?: WPTeletypeSettings; +}