Skip to Content
Menu

pre()

Function for handling selecting and copying text within Nebula pre tags.

JavaScript February 7, 2021

Usage

JavaScript
nebula.pre()

Parameters

This function does not accept any parameters. Is this incorrect?

Additional Notes

This function is called automatically if Nebula pre tags are used.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/helpers.js on line 366.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.pre = async function(){
        //Format non-shortcode pre tags to be styled properly
        jQuery('pre.nebula-code').each(function(){
            if ( !jQuery(this).parent('.nebula-code-con').length ){
                let lang = jQuery(this).attr('data-lang') || '';
                if ( lang === '' ){
                    let langMatches = jQuery(this).attr('class').match(/lang(?:uage)?-(\S*)/i);
                    lang = ( langMatches )? langMatches[0] : ''; //Use a class that starts with "lang-" or "language-" Ex: "lang-JavaScript"
                }
                if ( lang === '' ){
                    lang = jQuery(this).attr('class').replace('nebula-code', '').replaceAll(/(\s*)((wp|m.|p.|nebula)-\S+)(\s*)/gi, '').trim(); //Remove expected classes and use remaining class as language
                }
    
                lang = escape(lang); //Escape for reuse into the DOM
    
                jQuery(this).addClass(lang.toLowerCase()).wrap('<div class="nebula-code-con clearfix ' + lang.toLowerCase() + '"></div>');
                jQuery(this).closest('.nebula-code-con').prepend('<span class="nebula-code codetitle ' + lang.toLowerCase() + '">' + lang + '</span>');
            }
        });
    
        //Manage copying snippets to clipboard
        if ( 'clipboard' in navigator ){
            jQuery('.nebula-code-con').each(function(){
                jQuery(this).append('<a href="#" class="nebula-selectcopy-code">Copy to Clipboard</a>');
                jQuery(this).find('p:empty').remove(); //Sometimes WordPress adds extra/empty <p> tags. These mess with spacing, so we remove them.
            });
    
            nebula.dom.document.on('click', '.nebula-selectcopy-code', function(){
                let oThis = jQuery(this);
                if ( oThis.hasClass('error') ){ //If we already errored, stop trying
                    return false;
                }
    
                let text = jQuery(this).closest('.nebula-code-con').find('pre').text();
    
                navigator.clipboard.writeText(text).then(function(){
                    oThis.text('Copied!').removeClass('error').addClass('success');
                    setTimeout(function(){
                        oThis.text('Copy to clipboard').removeClass('success');
                    }, 1500);
                }).catch(function(error){ //This can happen if the user denies clipboard permissions
                    ga('send', 'exception', {'exDescription': '(JS) Clipboard API error: ' + error, 'exFatal': false});
                    oThis.text('Unable to copy.').addClass('error');
                });
    
                return false;
            });
        }
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name.

    JavaScript
    nebula.pre = function(){
        //Write your own code here, leave it blank, or return false.
    }