• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

notify user when snippet has no changes

Messages
6
Likes
0
Points
1
#1
Hi again. Is it a good idea to incorporate in your plugin a status when current snippet has changes in it? Such as notepad that when a file has been modified add an asterisc(*) in title of window .

I hope to undestand what I mean...

I have write custom code to achieve this. Do you want to share with you? :)

Thanks.
 

Temyk

Developer & Support
Staff member
Messages
822
Likes
32
Points
28
#2
Hello.

You can share your solution, we will look at it and maybe add it.
 
Messages
6
Likes
0
Points
1
#3
The code is the following.
I would appreciate very much if you make me a contributor in your plugin in Wordpress repository :)

PHP:
add_action("admin_footer", function (){



    if( get_post_type()=="wbcr-snippets" && ($GLOBALS['pagenow']=='post-new.php' || $GLOBALS['pagenow']=='post.php') ){

       

        echo '

        <script>

            var CurrentCodeMirrorCode="first_time";

            var FirstCodeMirrorCode="first_time";

            var document_title="";

            var document_title_edited="";

           

            function decodeHtmlCharCodes(str) {

                return str.replace(/(&#(\d+);)/g, function(match, capture, charCode) {

                    return String.fromCharCode(charCode);

                });

            }





        function CodeMirrorCodeWithoutSpaces(){
            var string="";
            var children_divs=document.getElementsByClassName("CodeMirror-code").item(0).children; //itterate
            var all_spans="";
            for(var j=0; j<children_divs.length; j++){
                    
                all_spans=children_divs.item(j).getElementsByTagName("PRE").item(0).getElementsByTagName("SPAN").item(0).getElementsByTagName("SPAN");

                var flag=false;
                for(var i=0; i<all_spans.length; i++){
                    var text = all_spans.item(i).innerHTML;
                    text=text.replace(/\s/g, "");

                    
                    //the above if-loop is to not identify new empty line as new content

                    if(all_spans.item(i).innerHTML!=decodeHtmlCharCodes("& #8203;")){ //SOS Delete the space between & and #
                        string=string + text;
                        flag=true;
                    }
                }
                if(flag===true){
                        string= string + "end_of_codemirror_line";
                }
            }
            //console.log(string);
            //string=string.replace("", "");
            return string;
        }
           

            var interval_CodeMirrorCode = setInterval(function()

            {

                if(FirstCodeMirrorCode=="first_time")

                {

                    FirstCodeMirrorCode=CodeMirrorCodeWithoutSpaces();  //first content converted as string

                    CurrentCodeMirrorCode=FirstCodeMirrorCode;

                   

                    document_title=document.title;

                    document_title_edited=document_title.replace(" ", " *"); //in my code I change the first occurrence of space with *

               

                }else

                {

                    CurrentCodeMirrorCode=CodeMirrorCodeWithoutSpaces(); //current content converted as string

                }

                if(CurrentCodeMirrorCode!=FirstCodeMirrorCode){

                    document.title=document_title_edited; //change the title if content is different

                }else{

                    document.title=document_title; //if not remove the asterisc (first title)

                }

            },1000); //checks every seconds for changes

           

           

           

        </script>

        ';

    }

},20);
 
Last edited: