CoreFiles by mySites.guru
editor-dark-mode-support.js81 lines, 2,251 bytes, from wordpress-5.6-beta4.zip
Raw History Compare with 5.6-beta3
md5
de594e468105fbdc8a0831d7972cf177
sha1
b4b005366ec3801fd8f53ab314795c3df02bc3d0
sha256
0b1d3d365f4c96ca685e7a09a7bf05acc89e917141e8197c9ab56bf6b26b92df
/* global ajaxurl, XMLHttpRequest, darkModeInitialLoad, setTimeout */

// Check the body class to determine if we want to add the toggler and handle dark-mode or not.
if ( document.body.classList.contains( 'twentytwentyone-supports-dark-theme' ) ) {
	// Add the toggler.
	twentytwentyoneDarkModeEditorToggle();
}

/**
 * Make an AJAX request, inject the toggle and call any functions that need to run.
 *
 * @since 1.0.0
 *
 * @return {void}
 */
function twentytwentyoneDarkModeEditorToggle() {
	var request = new XMLHttpRequest();

	// Define the request.
	request.open( 'POST', ajaxurl, true );

	// Add headers.
	request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' );

	// On success call funtions that need to run.
	request.onload = function() {
		var selector = '.editor-styles-wrapper,.edit-post-visual-editor',
			editor,
			attemptDelay = 25,
			attempt = 0,
			maxAttempts = 8;

		if ( 200 <= this.status && 400 > this.status ) {
			editor = document.querySelector( selector );

			if ( null === editor ) {
				// Try again.
				if ( attempt < maxAttempts ) {
					setTimeout( function() {
						twentytwentyoneDarkModeEditorToggle();
					}, attemptDelay );

					// Increment the attempts counter.
					attempt++;

					// Double the delay, give the server some time to breathe.
					attemptDelay *= 2;
				}
				return;
			}
			// Inject the toggle.
			document.querySelector( selector ).insertAdjacentHTML( 'afterbegin', this.response );

			// Run toggler script.
			darkModeInitialLoad();

			// Switch editor styles if needed.
			twentytwentyoneDarkModeEditorToggleEditorStyles();
		}
	};

	// Send the request.
	request.send( 'action=tt1_dark_mode_editor_switch' );
}

/**
 * Toggle the editor dark styles depending on the user's preferences in the toggler.
 *
 * @since 1.0.0
 *
 * @return {void}
 */
function twentytwentyoneDarkModeEditorToggleEditorStyles() {
	var toggler = document.getElementById( 'dark-mode-toggler' );

	if ( 'true' === toggler.getAttribute( 'aria-pressed' ) ) {
		document.body.classList.add( 'is-dark-theme' );
		document.documentElement.classList.add( 'is-dark-theme' );
		document.querySelector( '.block-editor__typewriter' ).classList.add( 'is-dark-theme' );
	}
}

No other release has this exact content. See every version of this file.