<commit_before>require(['jquery'], function($){
    
    isChanged = false; //TODO CH namespace this properly
    
    function serializeDocument(){
        var ser = new XMLSerializer();
        var mystr = ser.serializeToString(document.documentElement);
        return mystr;
    }
    
    //add behaviour for defocusing (also autosave?)
    function blurHandler(evt){
        evt.target.contentEditable=false;
        $(evt.target).unbind('blur',blurHandler);    
        editor.autosave(serializeDocument());
    }
    
    //makes DOM text editable
    $("h1,h2,h3,h4,h5,h6,p").click(function(){
        this.contentEditable=true;
        $(this).bind('blur',blurHandler);
        isChanged = true;
    });
        
    //add behaviour for handling CTRL+S
    $(window).bind('keydown', function(event) {
        if (event.ctrlKey || event.metaKey) {
            switch (String.fromCharCode(event.which).toLowerCase()) {
                case 's':
                    event.preventDefault();
                    editor.save(serializeDocument());
                    isChanged = false;
                    break;
                default:
                    break;
            }
        }
    });
    
    $(window).bind("beforeunload",function(event) {
        if (isChanged) {    
            event.returnValue = "You have unsaved changes";
            return event.returnValue
        }
        else{
            return;
        }
    });

    <commit_msg>Feature to allow editing of HTML notes files<commit_after>require(['jquery'], function($){
    
    isChanged = false; //TODO CH namespace this properly
    
    //TODO CH, this is specific to editing pdf readinglog notes
    //should be in separate javascript library from the editor logic
    $("h1 a").each(function(){
        var thisq = $(this);
        var href=thisq.attr("href");
        href = "javascript:pdf.loadpdf('" + href + "')";
        thisq.attr("href",href);
    });
    
    function serializeDocument(){
        var ser = new XMLSerializer();
        var mystr = ser.serializeToString(document.documentElement);
        return mystr;
    }
    
    //add behaviour for defocusing (also autosave?)
    function blurHandler(evt){
        $(evt.target).removeAttr("contenteditable");
        $(evt.target).unbind('blur',blurHandler);    
        editor.autosave(serializeDocument());
    }
    
    var editableselector = "h1,h2,h3,h4,h5,h6,p";
    
    //makes DOM text editable
    $(document).on("click", editableselector, function(){
        this.contentEditable=true;
        $(this).bind('blur',blurHandler);
        isChanged = true;
    });
            
    //add behaviour for handling CTRL+S
    $(window).bind('keydown', function(event) { //TODO CH detect keys intelligently to detect actual changes to text
        if (event.ctrlKey || event.metaKey) {
            switch (String.fromCharCode(event.which).toLowerCase()) {
                case 's':
                    event.preventDefault();
                    editor.save(serializeDocument());
                    isChanged = false;
                    break;
                default:
                    break;
            }
        }
        if(event.which == 13) { //ensures new paragraphs are created when enter is hit
            var selection = window.getSelection();
            var oldrange = selection.getRangeAt(0);
            if (oldrange.collapsed) { //checks that there's just an ordinary cursor position
                
                var oldtextnode = oldrange.startContainer;
                var oldeditableq = $(oldtextnode).closest(editableselector);
                oldeditableq.blur();
                oldeditableq.removeAttr("contenteditable");
                
                //split the text where the cursor is
                var text = oldtextnode.data;
                var textbefore = text.substring(0,oldrange.startOffset);
                var textafter = text.substring(oldrange.startOffset);
                
                //create para, after old element, acquire text child 
                var neweditableq = $("<p contenteditable='true'></p>");
                oldeditableq.after(neweditableq);
                neweditableq.focus();
                neweditableq.text("");
                var newtextnode = neweditableq.contents().get(0);
                
                //assign text to the two nodes
                oldtextnode.data = textbefore; //assign 'before' text to text node
                newtextnode.data = textafter; //assign 'after' text to new editable
                //move editing and focus to the new node
                //position cursor in the new text node
                var newrange = document.createRange();
                newrange.setStart(newtextnode,0); 
                newrange.setEnd(newtextnode,0);
                selection.removeAllRanges();
                selection.addRange(newrange);
                return false;
            }
        }
    });
    
    $(window).bind("beforeunload",function(event) {
        if (isChanged) {    
            event.returnValue = "You have unsaved changes";
            return event.returnValue
        }
        else{
            return;
        }
    });

    
{'fraction_non_alphanumeric': 0.10985645933014354, 'fraction_numerical': 0.003827751196172249, 'mean_word_length': 1.7162162162162162, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3351570', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1334, 'n_words': 315}