svn:eol-style native

This commit is contained in:
Alexandre Emsenhuber 2012-02-23 19:11:34 +00:00
parent b6ff0d5c02
commit 8c26b32e01
Notes: Gabriel Wicke 2012-02-27 16:40:01 +00:00
2 changed files with 220 additions and 220 deletions

View file

@ -1,21 +1,21 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Playground</title> <title>Playground</title>
<style> <style>
#editor { #editor {
width: 300px; width: 300px;
height: 300px; height: 300px;
border: solid 1px; border: solid 1px;
} }
</style> </style>
<script src="../modules/jquery/jquery.js"></script> <script src="../modules/jquery/jquery.js"></script>
<script src="../modules/rangy/rangy-core.js"></script> <script src="../modules/rangy/rangy-core.js"></script>
<script src="playground.js"></script> <script src="playground.js"></script>
</head> </head>
<body> <body>
<div contenteditable="true" id="editor"> <div contenteditable="true" id="editor">
</div> </div>
</body> </body>
</html> </html>

View file

@ -1,200 +1,200 @@
app = function () { app = function () {
var _this = this, var _this = this,
$document = $( document ); $document = $( document );
this.$editor = $('#editor'); this.$editor = $('#editor');
this.$editor.bind( { this.$editor.bind( {
'focus': function( e ) { 'focus': function( e ) {
$document.unbind( '.surfaceView' ); $document.unbind( '.surfaceView' );
$document.bind( { $document.bind( {
'keydown.surfaceView': function( e ) { 'keydown.surfaceView': function( e ) {
return _this.onKeyDown( e ); return _this.onKeyDown( e );
}, },
'keyup.surfaceView': function( e ) { 'keyup.surfaceView': function( e ) {
return _this.onKeyUp( e ); return _this.onKeyUp( e );
}, },
'keypress.surfaceView': function( e ) { 'keypress.surfaceView': function( e ) {
return _this.onKeyPress( e ); return _this.onKeyPress( e );
} }
} ); } );
}, },
'blur': function( e ) { 'blur': function( e ) {
$document.unbind( '.surfaceView' ); $document.unbind( '.surfaceView' );
} }
} ); } );
this.$editor.mousedown( function(e) { this.$editor.mousedown( function(e) {
return _this.onMouseDown( e ); return _this.onMouseDown( e );
} ); } );
// Set initial content for the "editor" // Set initial content for the "editor"
this.$editor.html("<b>Lorem Ipsum is simply dummy text</b> of the printing and typesetting industry. <b>Lorem Ipsum has been the <i>industry's</i> standard</b> dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it <u>to <b>make <i>a type</i> specimen</b> book.</u>"); this.$editor.html("<b>Lorem Ipsum is simply dummy text</b> of the printing and typesetting industry. <b>Lorem Ipsum has been the <i>industry's</i> standard</b> dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it <u>to <b>make <i>a type</i> specimen</b> book.</u>");
this.$editor.addClass('leafNode'); this.$editor.addClass('leafNode');
this.keypress = false; this.keypress = false;
this.keyup = false; this.keyup = false;
this.keydown = false; this.keydown = false;
this.mousedown = false; this.mousedown = false;
this.inime = false; this.inime = false;
this.prevText = app.getDOMText(this.$editor[0]); this.prevText = app.getDOMText(this.$editor[0]);
setInterval(function() { setInterval(function() {
_this.loopFunc(); _this.loopFunc();
}, 100); }, 100);
}; };
app.prototype.onKeyPress = function() { app.prototype.onKeyPress = function() {
//console.log("onKeyPress"); //console.log("onKeyPress");
this.keypress = true; this.keypress = true;
if ( e.which === 229 ) { if ( e.which === 229 ) {
this.inime = true; this.inime = true;
} }
}; };
app.prototype.onKeyUp = function() { app.prototype.onKeyUp = function() {
//console.log("onKeyUp"); //console.log("onKeyUp");
this.keyup = true; this.keyup = true;
if ( this.inime ) { if ( this.inime ) {
this.inime = false; this.inime = false;
} }
}; };
app.prototype.onKeyDown = function( e ) { app.prototype.onKeyDown = function( e ) {
//console.log("onKeyDown"); //console.log("onKeyDown");
this.keydown = true; this.keydown = true;
if ( e.which === 229 ) { if ( e.which === 229 ) {
this.inime = true; this.inime = true;
} }
}; };
app.prototype.onMouseDown = function() { app.prototype.onMouseDown = function() {
this.mousedown = true; this.mousedown = true;
if ( this.inime ) { if ( this.inime ) {
this.inime = false; this.inime = false;
} }
}; };
app.prototype.loopFunc = function() { app.prototype.loopFunc = function() {
var text = app.getDOMText(this.$editor[0]); var text = app.getDOMText(this.$editor[0]);
if(text != this.prevText) { if(text != this.prevText) {
var selection = rangy.getSelection(); var selection = rangy.getSelection();
// keyup in IE // keyup in IE
// keypress and keydown in FF and Chrome // keypress and keydown in FF and Chrome
if ( (($.browser.msie && !this.keyup) || (!$.browser.msie && !this.keypress && !this.mousedown)) && !this.inime ) { if ( (($.browser.msie && !this.keyup) || (!$.browser.msie && !this.keypress && !this.mousedown)) && !this.inime ) {
console.log(this.inime); console.log(this.inime);
console.log('SPELLCHECK'); console.log('SPELLCHECK');
this.prevText = text; this.prevText = text;
this.keypress = false; this.keypress = false;
this.keyup = false; this.keyup = false;
this.mousedown = false; this.mousedown = false;
return; return;
} }
console.log("keyboard"); console.log("keyboard");
// we are going to need a cursor position // we are going to need a cursor position
var offset = this.getOffset( selection.anchorNode, selection.anchorOffset ); var offset = this.getOffset( selection.anchorNode, selection.anchorOffset );
var diffLength = text.length - this.prevText.length; var diffLength = text.length - this.prevText.length;
//console.log("diffLength: " + diffLength); //console.log("diffLength: " + diffLength);
if ( diffLength > 0 ) { if ( diffLength > 0 ) {
//console.log( text.substring(offset - diffLength, offset) ); //console.log( text.substring(offset - diffLength, offset) );
} else if ( diffLength === 0 ) { } else if ( diffLength === 0 ) {
//console.log( text.substring(offset - 1, offset) ); //console.log( text.substring(offset - 1, offset) );
} }
this.prevText = text; this.prevText = text;
} }
this.keypress = false; this.keypress = false;
this.keyup = false; this.keyup = false;
this.keydown = false; this.keydown = false;
this.mousedown = false; this.mousedown = false;
}; };
app.getDOMText = function( elem ) { app.getDOMText = function( elem ) {
var nodeType = elem.nodeType, var nodeType = elem.nodeType,
ret = ''; ret = '';
if ( nodeType === 1 || nodeType === 9 ) { if ( nodeType === 1 || nodeType === 9 ) {
// Use textContent || innerText for elements // Use textContent || innerText for elements
if ( typeof elem.textContent === 'string' ) { if ( typeof elem.textContent === 'string' ) {
return elem.textContent; return elem.textContent;
} else if ( typeof elem.innerText === 'string' ) { } else if ( typeof elem.innerText === 'string' ) {
// Replace IE's carriage returns // Replace IE's carriage returns
return elem.innerText.replace( /\r\n/g, '' ); return elem.innerText.replace( /\r\n/g, '' );
} else { } else {
// Traverse it's children // Traverse it's children
for ( elem = elem.firstChild; elem; elem = elem.nextSibling) { for ( elem = elem.firstChild; elem; elem = elem.nextSibling) {
ret += app.getDOMText( elem ); ret += app.getDOMText( elem );
} }
} }
} else if ( nodeType === 3 || nodeType === 4 ) { } else if ( nodeType === 3 || nodeType === 4 ) {
return elem.nodeValue; return elem.nodeValue;
} }
return ret; return ret;
}; };
app.prototype.getOffset = function( localNode, localOffset ) { app.prototype.getOffset = function( localNode, localOffset ) {
var $node = $( localNode ); var $node = $( localNode );
if ( $node.hasClass( 'leafNode' ) ) { if ( $node.hasClass( 'leafNode' ) ) {
return localOffset; return localOffset;
} }
while( !$node.hasClass( 'leafNode' ) ) { while( !$node.hasClass( 'leafNode' ) ) {
$node = $node.parent(); $node = $node.parent();
} }
var current = [$node.contents(), 0]; var current = [$node.contents(), 0];
var stack = [current]; var stack = [current];
var offset = 0; var offset = 0;
while ( stack.length > 0 ) { while ( stack.length > 0 ) {
if ( current[1] >= current[0].length ) { if ( current[1] >= current[0].length ) {
stack.pop(); stack.pop();
current = stack[ stack.length - 1 ]; current = stack[ stack.length - 1 ];
continue; continue;
} }
var item = current[0][current[1]]; var item = current[0][current[1]];
var $item = current[0].eq( current[1] ); var $item = current[0].eq( current[1] );
if ( item.nodeType === 3 ) { if ( item.nodeType === 3 ) {
if ( item === localNode ) { if ( item === localNode ) {
offset += localOffset; offset += localOffset;
break; break;
} else { } else {
offset += item.textContent.length; offset += item.textContent.length;
} }
} else if ( item.nodeType === 1 ) { } else if ( item.nodeType === 1 ) {
if ( $( item ).attr('contentEditable') === "false" ) { if ( $( item ).attr('contentEditable') === "false" ) {
offset += 1; offset += 1;
} else { } else {
if ( item === localNode ) { if ( item === localNode ) {
offset += localOffset; offset += localOffset;
break; break;
} }
stack.push( [$item.contents(), 0] ); stack.push( [$item.contents(), 0] );
current[1]++; current[1]++;
current = stack[stack.length-1]; current = stack[stack.length-1];
continue; continue;
} }
} }
current[1]++; current[1]++;
} }
return offset; return offset;
}; };
$(function() { $(function() {
new app(); new app();
}); });