2012-02-07 19:13:19 +00:00
|
|
|
/**
|
|
|
|
* Creates an ve.es.Surface object.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {jQuery} $container DOM Container to render surface into
|
|
|
|
* @param {ve.dm.Surface} model Surface model to view
|
|
|
|
*/
|
|
|
|
ve.es.Surface = function( $container, model ) {
|
|
|
|
// Inheritance
|
|
|
|
ve.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// References for use in closures
|
|
|
|
var _this = this,
|
|
|
|
$document = $( document ),
|
|
|
|
$window = $( window );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.model = model;
|
|
|
|
this.documentView = new ve.es.DocumentNode( this.model.getDocument(), this );
|
|
|
|
this.contextView = null;
|
|
|
|
this.$ = $container
|
|
|
|
.addClass( 'es-surfaceView' )
|
|
|
|
.append( this.documentView.$ );
|
|
|
|
this.emitUpdateTimeout = undefined;
|
|
|
|
|
|
|
|
// Events
|
2012-02-13 22:45:18 +00:00
|
|
|
this.documentView.$.bind( {
|
|
|
|
'focus': function( e ) {
|
2012-03-01 01:28:39 +00:00
|
|
|
_this.documentOnFocus();
|
2012-02-13 22:45:18 +00:00
|
|
|
$document.unbind( '.ce-surfaceView' );
|
|
|
|
$document.bind( {
|
|
|
|
'keydown.ce-surfaceView': function( e ) {
|
2012-03-01 01:28:39 +00:00
|
|
|
// return _this.onKeyDown( e );
|
2012-03-01 22:14:14 +00:00
|
|
|
}
|
2012-02-13 22:45:18 +00:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
'blur': function( e ) {
|
2012-03-01 01:28:39 +00:00
|
|
|
_this.documentOnBlur();
|
2012-02-13 22:45:18 +00:00
|
|
|
$document.unbind( '.ce-surfaceView' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.$.mousedown( function(e) {
|
2012-03-01 01:28:39 +00:00
|
|
|
// return _this.onMouseDown( e );
|
2012-02-13 22:45:18 +00:00
|
|
|
} );
|
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
// Initialization
|
|
|
|
this.documentView.renderContent();
|
|
|
|
|
2012-02-27 21:56:56 +00:00
|
|
|
// Prevent dragging text
|
|
|
|
this.$.bind('dragover drop', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
this.poll = {
|
|
|
|
interval: null,
|
|
|
|
frequency: 100,
|
|
|
|
node: null,
|
|
|
|
prevText: null,
|
|
|
|
prevHash: null,
|
|
|
|
prevOffset: null,
|
|
|
|
compositionStart: null,
|
|
|
|
compositionEnd: null
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener( 'compositionstart', function( e ) {
|
|
|
|
_this.onCompositionStart( e );
|
2012-02-07 19:13:19 +00:00
|
|
|
} );
|
2012-03-01 01:28:39 +00:00
|
|
|
document.addEventListener( 'compositionend', function( e ) {
|
|
|
|
_this.onCompositionEnd( e );
|
2012-02-09 22:11:33 +00:00
|
|
|
} );
|
2012-03-01 01:28:39 +00:00
|
|
|
};
|
2012-02-08 06:28:38 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
/* Methods */
|
2012-02-08 02:12:21 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.es.Surface.prototype.onCompositionStart = function( e ) {
|
|
|
|
this.stopPolling();
|
|
|
|
var rangySel = rangy.getSelection();
|
|
|
|
this.poll.compositionStart = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, false );
|
2012-02-13 22:45:18 +00:00
|
|
|
};
|
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.es.Surface.prototype.onCompositionEnd = function( e ) {
|
|
|
|
var rangySel = rangy.getSelection();
|
|
|
|
this.poll.compositionEnd = this.getOffset( rangySel.focusNode, rangySel.focusOffset, false );
|
|
|
|
this.startPolling();
|
|
|
|
};
|
2012-02-13 22:45:18 +00:00
|
|
|
|
|
|
|
ve.es.Surface.prototype.attachContextView = function( contextView ) {
|
|
|
|
this.contextView = contextView;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.es.Surface.prototype.getModel = function() {
|
|
|
|
return this.model;
|
|
|
|
};
|
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.es.Surface.prototype.documentOnFocus = function() {
|
|
|
|
this.startPolling();
|
2012-02-13 22:45:18 +00:00
|
|
|
};
|
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.es.Surface.prototype.documentOnBlur = function() {
|
|
|
|
this.stopPolling();
|
2012-02-13 22:45:18 +00:00
|
|
|
};
|
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.es.Surface.prototype.startPolling = function() {
|
|
|
|
if ( this.poll.interval === null ) {
|
|
|
|
var _this = this;
|
|
|
|
this.poll.interval = setInterval( function() {
|
|
|
|
_this.pollContent();
|
|
|
|
}, this.poll.frequency );
|
|
|
|
this.pollContent();
|
|
|
|
}
|
|
|
|
};
|
2012-02-13 22:45:18 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.es.Surface.prototype.stopPolling = function() {
|
|
|
|
if ( this.poll.interval !== null ) {
|
|
|
|
clearInterval( this.poll.interval );
|
|
|
|
this.poll.interval = null;
|
|
|
|
}
|
|
|
|
};
|
2012-02-13 22:45:18 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.es.Surface.prototype.pollContent = function() {
|
2012-03-01 22:14:14 +00:00
|
|
|
var localOffset, text, hash;
|
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
if ( this.poll.compositionStart !== null && this.poll.compositionEnd !== null ) {
|
2012-03-01 20:49:21 +00:00
|
|
|
|
2012-03-01 22:14:14 +00:00
|
|
|
text = ve.es.Surface.getDOMText2( this.poll.node );
|
|
|
|
hash = ve.es.Surface.getDOMHash( this.poll.node );
|
|
|
|
localOffset = this.poll.compositionEnd;
|
2012-03-01 01:28:39 +00:00
|
|
|
this.poll.compositionStart = null;
|
|
|
|
this.poll.compositionEnd = null;
|
2012-03-01 20:49:21 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
} else {
|
|
|
|
var rangySel = rangy.getSelection();
|
2012-02-10 18:18:35 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
if ( rangySel.anchorNode === null ) {
|
2012-02-13 22:45:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-01 22:14:14 +00:00
|
|
|
var node = this.getLeafNode( rangySel.anchorNode )[0];
|
|
|
|
text = ve.es.Surface.getDOMText2( node );
|
|
|
|
hash = ve.es.Surface.getDOMHash( node );
|
2012-02-07 19:13:19 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
if ( rangySel.anchorNode !== rangySel.focusNode || rangySel.anchorOffset !== rangySel.focusOffset ) {
|
2012-03-01 22:14:14 +00:00
|
|
|
localOffset = null;
|
2012-03-01 01:28:39 +00:00
|
|
|
} else {
|
2012-03-01 22:14:14 +00:00
|
|
|
localOffset = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, false );
|
2012-03-01 01:28:39 +00:00
|
|
|
}
|
2012-02-13 22:45:18 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
if ( node !== this.poll.node ) {
|
|
|
|
this.poll.node = node;
|
|
|
|
this.poll.prevText = text;
|
|
|
|
this.poll.prevHash = hash;
|
|
|
|
this.poll.prevOffset = localOffset;
|
|
|
|
return;
|
|
|
|
}
|
2012-02-13 22:45:18 +00:00
|
|
|
}
|
2012-03-01 22:14:14 +00:00
|
|
|
|
|
|
|
var newData, annotations;
|
2012-02-13 22:45:18 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
if ( text !== this.poll.prevText ) {
|
|
|
|
var nodeOffset = this.documentView.getOffsetFromNode( $( this.poll.node ).data( 'view' ) ),
|
|
|
|
lengthDiff = text.length - this.poll.prevText.length,
|
|
|
|
offsetDiff = ( localOffset !== null && this.poll.prevOffset !== null ) ? localOffset - this.poll.prevOffset : null;
|
|
|
|
|
|
|
|
if ( lengthDiff === offsetDiff && this.poll.prevText.substring( 0, this.poll.prevOffset ) === text.substring( 0, this.poll.prevOffset ) ) {
|
2012-03-01 22:14:14 +00:00
|
|
|
newData = text.substring( this.poll.prevOffset, localOffset ).split( '' );
|
|
|
|
annotations = this.model.getDocument().getAnnotationsFromOffset( nodeOffset + 1 + this.poll.prevOffset - 1 );
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.dm.DocumentNode.addAnnotationsToData( newData, annotations );
|
|
|
|
this.model.transact( this.documentView.model.prepareInsertion(
|
|
|
|
nodeOffset + 1 + this.poll.prevOffset,
|
|
|
|
newData
|
|
|
|
) );
|
|
|
|
} else {
|
|
|
|
var sameFromLeft = 0,
|
|
|
|
sameFromRight = 0,
|
|
|
|
l = text.length > this.poll.prevText.length ? this.poll.prevText.length : text.length;
|
2012-03-01 22:14:14 +00:00
|
|
|
while ( sameFromLeft < l && this.poll.prevText[sameFromLeft] === text[sameFromLeft] ) {
|
2012-03-01 01:28:39 +00:00
|
|
|
++sameFromLeft;
|
|
|
|
}
|
|
|
|
l = l - sameFromLeft;
|
2012-03-01 22:14:14 +00:00
|
|
|
while ( sameFromRight < l && this.poll.prevText[this.poll.prevText.length - 1 - sameFromRight] === text[text.length - 1 - sameFromRight] ) {
|
2012-03-01 01:28:39 +00:00
|
|
|
++sameFromRight;
|
|
|
|
}
|
|
|
|
this.model.transact( this.documentView.model.prepareRemoval( new ve.Range(
|
|
|
|
nodeOffset + 1 + sameFromLeft,
|
|
|
|
nodeOffset + 1 + this.poll.prevText.length - sameFromRight
|
|
|
|
) ) );
|
2012-03-01 22:14:14 +00:00
|
|
|
newData = text.substring( sameFromLeft, text.length - sameFromRight ).split( '' );
|
|
|
|
annotations = this.model.getDocument().getAnnotationsFromOffset( nodeOffset + 1 + sameFromLeft );
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.dm.DocumentNode.addAnnotationsToData( newData, annotations );
|
|
|
|
this.model.transact( this.documentView.model.prepareInsertion(
|
|
|
|
nodeOffset + 1 + sameFromLeft,
|
|
|
|
newData
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
this.poll.prevText = text;
|
2012-02-13 22:45:18 +00:00
|
|
|
}
|
2012-03-01 01:28:39 +00:00
|
|
|
if ( hash !== this.poll.prevHash ) {
|
|
|
|
// TODO: redisplay cursor in correct position (with setTimeout)
|
|
|
|
this.getLeafNode( this.poll.node ).data( 'view' ).renderContent();
|
|
|
|
this.poll.prevHash = hash;
|
2012-02-13 22:45:18 +00:00
|
|
|
}
|
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
this.poll.prevOffset = localOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.es.Surface.prototype.getOffset = function( elem, offset, global ) {
|
|
|
|
var $leafNode = this.getLeafNode( elem ),
|
|
|
|
current = [$leafNode.contents(), 0],
|
|
|
|
stack = [current],
|
|
|
|
localOffset = 0;
|
|
|
|
|
2012-02-13 22:45:18 +00:00
|
|
|
while ( stack.length > 0 ) {
|
|
|
|
if ( current[1] >= current[0].length ) {
|
|
|
|
stack.pop();
|
|
|
|
current = stack[ stack.length - 1 ];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var item = current[0][current[1]];
|
|
|
|
var $item = current[0].eq( current[1] );
|
|
|
|
|
|
|
|
if ( item.nodeType === 3 ) {
|
2012-03-01 01:28:39 +00:00
|
|
|
if ( item === elem ) {
|
|
|
|
localOffset += offset;
|
2012-02-13 22:45:18 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2012-03-01 01:28:39 +00:00
|
|
|
localOffset += item.textContent.length;
|
2012-02-13 22:45:18 +00:00
|
|
|
}
|
|
|
|
} else if ( item.nodeType === 1 ) {
|
2012-03-01 01:28:39 +00:00
|
|
|
if ( $( item ).attr( 'contentEditable' ) === "false" ) {
|
2012-02-13 22:45:18 +00:00
|
|
|
offset += 1;
|
|
|
|
} else {
|
2012-03-01 01:28:39 +00:00
|
|
|
if ( item === elem ) {
|
|
|
|
localOffset += offset;
|
2012-02-13 22:45:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack.push( [$item.contents(), 0] );
|
|
|
|
current[1]++;
|
|
|
|
current = stack[stack.length-1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current[1]++;
|
|
|
|
}
|
2012-03-01 01:28:39 +00:00
|
|
|
if ( global === true ) {
|
|
|
|
return this.documentView.getOffsetFromNode( $leafNode.data( 'view' ) ) + 1 + localOffset;
|
|
|
|
} else {
|
|
|
|
return localOffset;
|
|
|
|
}
|
2012-02-13 22:45:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.es.Surface.prototype.showCursorAt = function( offset ) {
|
2012-03-01 22:14:14 +00:00
|
|
|
var $node = this.documentView.getNodeFromOffset( offset ).$,
|
|
|
|
current = [$node.contents(), 0],
|
|
|
|
stack = [current],
|
|
|
|
node,
|
|
|
|
localOffset,
|
|
|
|
index = this.documentView.getOffsetFromNode( $node.data('view') ) + 1;
|
|
|
|
|
2012-02-13 22:45:18 +00:00
|
|
|
while ( stack.length > 0 ) {
|
|
|
|
if ( current[1] >= current[0].length ) {
|
|
|
|
stack.pop();
|
|
|
|
current = stack[ stack.length - 1 ];
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-01 22:14:14 +00:00
|
|
|
var item = current[0][current[1]],
|
|
|
|
$item = current[0].eq( current[1] );
|
2012-02-13 22:45:18 +00:00
|
|
|
|
|
|
|
if ( item.nodeType === 3 ) {
|
|
|
|
var length = item.textContent.length;
|
|
|
|
if ( offset >= index && offset <= index + length ) {
|
|
|
|
node = item;
|
|
|
|
localOffset = offset - index;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
index += length;
|
|
|
|
}
|
|
|
|
} else if ( item.nodeType === 1 ) {
|
|
|
|
if ( $( item ).attr('contentEditable') === "false" ) {
|
|
|
|
index += 1;
|
|
|
|
} else {
|
|
|
|
stack.push( [$item.contents(), 0] );
|
|
|
|
current[1]++;
|
|
|
|
current = stack[stack.length-1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current[1]++;
|
|
|
|
}
|
|
|
|
var range = document.createRange();
|
|
|
|
range.collapsed = true;
|
2012-03-01 22:14:14 +00:00
|
|
|
range.setStart( node, localOffset );
|
2012-02-13 22:45:18 +00:00
|
|
|
var sel = window.getSelection();
|
|
|
|
sel.removeAllRanges();
|
2012-03-01 22:14:14 +00:00
|
|
|
sel.addRange( range );
|
2012-02-13 22:45:18 +00:00
|
|
|
};
|
|
|
|
|
2012-03-01 20:49:21 +00:00
|
|
|
ve.es.Surface.prototype.getSelection = function() {
|
2012-03-01 20:52:22 +00:00
|
|
|
var rangySel = rangy.getSelection();
|
2012-03-01 20:49:21 +00:00
|
|
|
|
2012-03-01 20:52:22 +00:00
|
|
|
if ( rangySel.anchorNode === rangySel.focusNode && rangySel.anchorOffset === rangySel.focusOffset ) {
|
|
|
|
var offset = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, true );
|
2012-03-01 20:49:21 +00:00
|
|
|
return new ve.Range( offset, offset );
|
|
|
|
} else {
|
|
|
|
return new ve.Range(
|
2012-03-01 20:52:22 +00:00
|
|
|
this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, true ),
|
|
|
|
this.getOffset( rangySel.focusNode, rangySel.focusOffset, true )
|
2012-03-01 20:49:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.es.Surface.prototype.getLeafNode = function( elem ) {
|
|
|
|
var $node = $( elem );
|
|
|
|
while( !$node.hasClass( 'ce-leafNode' ) ) {
|
|
|
|
$node = $node.parent();
|
2012-02-09 22:11:33 +00:00
|
|
|
}
|
2012-03-01 01:28:39 +00:00
|
|
|
return $node;
|
2012-02-09 22:11:33 +00:00
|
|
|
};
|
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.es.Surface.getDOMText2 = function( elem ) {
|
|
|
|
// TODO: there must be some better way to write this regex replace
|
|
|
|
var regex = new RegExp("[" + String.fromCharCode(32) + String.fromCharCode(160) + "]", "g");
|
|
|
|
return ve.es.Surface.getDOMText( elem ).replace( regex, " " );
|
2012-02-09 22:11:33 +00:00
|
|
|
};
|
2012-02-09 00:51:59 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.es.Surface.getDOMText = function( elem ) {
|
|
|
|
var nodeType = elem.nodeType,
|
|
|
|
ret = '';
|
|
|
|
|
|
|
|
if ( nodeType === 1 || nodeType === 9 ) {
|
|
|
|
// Use textContent || innerText for elements
|
|
|
|
if ( typeof elem.textContent === 'string' ) {
|
|
|
|
return elem.textContent;
|
|
|
|
} else if ( typeof elem.innerText === 'string' ) {
|
|
|
|
// Replace IE's carriage returns
|
|
|
|
return elem.innerText.replace( /\r\n/g, '' );
|
|
|
|
} else {
|
|
|
|
// Traverse it's children
|
|
|
|
for ( elem = elem.firstChild; elem; elem = elem.nextSibling) {
|
|
|
|
ret += ve.es.Surface.getDOMText( elem );
|
2012-02-09 00:51:59 +00:00
|
|
|
}
|
2012-03-01 01:28:39 +00:00
|
|
|
}
|
|
|
|
} else if ( nodeType === 3 || nodeType === 4 ) {
|
|
|
|
return elem.nodeValue;
|
|
|
|
}
|
2012-02-09 00:51:59 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
return ret;
|
2012-02-07 19:13:19 +00:00
|
|
|
};
|
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
ve.es.Surface.getDOMHash = function( elem ) {
|
|
|
|
var nodeType = elem.nodeType,
|
2012-03-01 22:14:14 +00:00
|
|
|
nodeName = elem.nodeName,
|
2012-03-01 01:28:39 +00:00
|
|
|
ret = '';
|
|
|
|
|
|
|
|
if ( nodeType === 3 || nodeType === 4 ) {
|
|
|
|
return '#';
|
|
|
|
} else if ( nodeType === 1 || nodeType === 9 ) {
|
|
|
|
ret += '<' + nodeName + '>';
|
2012-03-01 22:14:14 +00:00
|
|
|
// Traverse it's children
|
2012-03-01 01:28:39 +00:00
|
|
|
for ( elem = elem.firstChild; elem; elem = elem.nextSibling) {
|
2012-03-01 22:14:14 +00:00
|
|
|
ret += ve.es.Surface.getDOMHash( elem );
|
2012-03-01 01:28:39 +00:00
|
|
|
}
|
|
|
|
ret += '</' + nodeName + '>';
|
2012-02-08 00:30:40 +00:00
|
|
|
}
|
2012-03-01 01:28:39 +00:00
|
|
|
return ret;
|
2012-02-08 00:30:40 +00:00
|
|
|
};
|
|
|
|
|
2012-02-07 19:13:19 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
2012-03-01 22:14:14 +00:00
|
|
|
ve.extendClass( ve.es.Surface, ve.EventEmitter );
|