Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2defdf5f82
Fetching contributors…

Cannot retrieve contributors at this time

file 96 lines (73 sloc) 3.259 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<!DOCTYPE html>
<html>
    <head>
        <script src="../modules/jquery/jquery.js"></script>
        <script>
            $( function() {
                
                var $editor = $('#ce-editor');
                var editorInterval = null;
                var sel = {
                            from: 0,
                            to: 0
                };
                var prevInput = '';

                $editor.blur(function() {
                    if ( editorInterval !== null ) {
                        clearInterval( editorInterval );
                    }
                });

                $editor.focus(function() {
                    prevInput = $editor.html();
                    editorInterval = setInterval( readInput, 100 );
                    sel.from = sel.to = window.getSelection().getRangeAt(0).startOffset;
                });
                
                var readInput = function() {
                    var text = $editor.html();
                    if (text === prevInput) {
                        return false;
                    }
                    var same = 0,
                        l = Math.min(prevInput.length, text.length);
                    while ( same < l && prevInput[same] === text[same] ) {
                        ++same;
                    }

                    sel.from = sel.to = window.getSelection().getRangeAt(0).startOffset - 1;


                    if (same < prevInput.length) {
                        console.log(prevInput.length - same);
                    }
                    

                    var newText = text.slice(same);

                    console.log(sel);
                    console.log(newText);

                    prevInput = text;
                }

            } );

            var oldText = 'Test';
            var newText = 'Teeeeeest';
            
            var sameFromLeft = 0,
                sameFromRight = 0,
                l = oldText.length;

            while ( sameFromLeft < l && oldText[sameFromLeft] == newText[sameFromLeft] ) {
                ++sameFromLeft;
            }
            if ( oldText.length > sameFromLeft ) {
                l = l - sameFromLeft;
                while ( sameFromRight < l && oldText[oldText.length - 1 - sameFromRight] == newText[newText.length - 1 - sameFromRight] ) {
                    ++sameFromRight;
                }
            }
            
            console.log('sameFromLeft: ' + sameFromLeft);
            console.log('sameFromRight: ' + sameFromRight);
            
            if ( sameFromLeft + sameFromRight !== oldText.length ) {
                var toDelete = oldText.split('');
                console.log(toDelete.slice(sameFromLeft,oldText.length - sameFromRight).join(''));
            }
            
            if ( sameFromLeft + sameFromRight !== newText.length ) {
                var toAdd = newText.split('');
                console.log(toAdd.slice(sameFromLeft, newText.length - sameFromRight).join(''));
            }
            
            
            
                        

        </script>
    </head>
    <body>
        <div style="width: 500px; height: 200px; border: solid 1px;" contenteditable="true" id="ce-editor"></div>
    </body>
</html>
Something went wrong with that request. Please try again.