Plugin Directory

Changeset 2893419


Ignore:
Timestamp:
04/04/2023 09:27:50 AM (3 years ago)
Author:
rmstm
Message:

bug fix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • admin-ide-dark-mode/trunk/js/aidmAdmin.js

    r2879634 r2893419  
    11const changeCodeMirrorTheme = () => {
    2     // setTimeout -> codeMirror instances are not detected without it
    3     console.log( 'from const' );
    42    let codeMirrorInstance = document.querySelector( '.CodeMirror' );
     3
    54    if ( ! codeMirrorInstance ) {
    65        return false;
    76    }
     7
    88    codeMirrorInstance.CodeMirror.setOption( 'theme', aidm_theme_preference );
    99
     
    1414jQuery( document ).ready( ( $ ) => {
    1515    // this part modify the custom css IDE in the customize panel
    16     let additionalCSSTab = $( 'li#accordion-section-custom_css h3' );
    17     if ( additionalCSSTab.length > 0 ) {
    18         additionalCSSTab.on( 'click', changeCodeMirrorTheme );
     16    // Select the node that will be observed for mutations
     17    let targetNode = document.body;
    1918
    20         return;
    21     }
     19    // Options for the observer (which mutations to observe)
     20    let config = { childList: true, subtree: true };
    2221
    23     // this part change IDE througout the admin
    24     setTimeout( changeCodeMirrorTheme, 10 );
    25    
     22    // Create an observer instance
     23    let observer = new MutationObserver( () => {
     24        // Look for elements with the class name 'CodeMirror'
     25        let additionalCSSTab = $( 'li#accordion-section-custom_css > h3' );
     26        let codeMirrorElement = $( '.CodeMirror' );
     27       
     28        if ( codeMirrorElement.length > 0 ) {
     29            // Do something if the element is found
     30            changeCodeMirrorTheme();
     31            observer.disconnect();
     32                   
     33        } else if ( additionalCSSTab.length > 0 ) {
     34            additionalCSSTab.on( 'click', changeCodeMirrorTheme );
     35            observer.disconnect();
     36                   
     37        } else {
     38            setTimeout( () => {
     39                observer.disconnect();
     40            }, 3000 );
     41        }
     42    });
     43
     44    // Start observing the target node for configured mutations
     45    observer.observe(targetNode, config);
     46
    2647    return;
    2748});
Note: See TracChangeset for help on using the changeset viewer.