WordPress/wp-content/themes/twentytwentyone/assets/js/editor-dark-mode-support.js
desrosj 967d29d83d Twenty Twenty-One: Merge the latest changes changes from GitHub for Beta 4.
In addition to syncing the latest changes, this change also merges the theme’s `.scss` files and other related build tool configurations required to compile the theme’s CSS.

This will allow development of the theme to continue on Trac after 5.6 is released and the GitHub repository is archived.

For a full list of changes since [], see e7d5991...aa284fd.

Props poena, luminuu kjellr, aristath, justinahinon.
See #51526.
Built from https://develop.svn.wordpress.org/trunk@49574


git-svn-id: http://core.svn.wordpress.org/trunk@49312 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-11-12 18:36:15 +00:00

82 lines
2.2 KiB
JavaScript

/* 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' );
}
}