2012-05-03 20:21:13 +00:00
|
|
|
/**
|
|
|
|
* ContentEditable surface.
|
2012-05-03 23:02:21 +00:00
|
|
|
*
|
2012-05-03 20:21:13 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param model {ve.dm.Surface} Model to observe
|
|
|
|
*/
|
2012-05-03 21:02:35 +00:00
|
|
|
ve.ce.Surface = function( $container, model ) {
|
2012-05-03 23:02:21 +00:00
|
|
|
// Inheritance
|
|
|
|
ve.EventEmitter.call( this );
|
2012-05-04 00:19:01 +00:00
|
|
|
|
2012-05-03 20:21:13 +00:00
|
|
|
// Properties
|
2012-05-03 23:14:54 +00:00
|
|
|
this.model = model;
|
2012-05-04 22:20:14 +00:00
|
|
|
this.documentView = new ve.ce.Document( model.getDocument() );
|
2012-05-21 23:06:30 +00:00
|
|
|
this.contextView = new ve.ui.Context( this );
|
2012-05-04 00:19:01 +00:00
|
|
|
this.$ = $container;
|
2012-06-07 18:03:22 +00:00
|
|
|
|
|
|
|
// Driven by mousedown and mouseup events
|
2012-05-25 03:55:45 +00:00
|
|
|
this.isMouseDown = false;
|
2012-06-07 18:03:22 +00:00
|
|
|
this.range = {
|
|
|
|
anchorNode: null,
|
|
|
|
anchorOffset: null,
|
|
|
|
focusNode: null,
|
|
|
|
focusOffset: null
|
|
|
|
};
|
|
|
|
|
2012-05-25 03:55:45 +00:00
|
|
|
// Events
|
|
|
|
this.$.on( {
|
2012-06-07 18:03:22 +00:00
|
|
|
'keypress': this.proxy( this.onKeyPress ),
|
2012-05-26 06:52:52 +00:00
|
|
|
'keydown': this.proxy( this.onKeyDown ),
|
2012-05-25 03:55:45 +00:00
|
|
|
'mousedown': this.proxy( this.onMouseDown ),
|
|
|
|
'mouseup': this.proxy( this.onMouseUp ),
|
|
|
|
'mousemove': this.proxy( this.onMouseMove ),
|
2012-06-07 18:10:08 +00:00
|
|
|
'cut copy': this.proxy( this.onCutCopy ),
|
|
|
|
'beforepaste paste': this.proxy( this.onPaste ),
|
2012-06-07 18:03:22 +00:00
|
|
|
'dragover drop': function( e ) {
|
|
|
|
// Prevent content drag & drop
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-25 03:55:45 +00:00
|
|
|
} );
|
2012-06-07 18:03:22 +00:00
|
|
|
this.model.on( 'select', this.proxy( this.onSelect ) );
|
2012-05-04 00:19:01 +00:00
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.$.append( this.documentView.documentNode.$ );
|
2012-05-18 18:40:22 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
document.execCommand( "enableObjectResizing", false, false );
|
2012-05-21 23:06:30 +00:00
|
|
|
document.execCommand( "enableInlineTableEditing", false, false );
|
2012-05-18 18:40:22 +00:00
|
|
|
} catch (e) { }
|
2012-05-03 20:21:13 +00:00
|
|
|
};
|
2012-05-03 23:02:21 +00:00
|
|
|
|
2012-05-18 22:19:31 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2012-05-23 20:34:52 +00:00
|
|
|
ve.ce.Surface.prototype.proxy = function( func ) {
|
|
|
|
var _this = this;
|
|
|
|
return( function() {
|
|
|
|
return func.apply( _this, arguments );
|
|
|
|
});
|
|
|
|
};
|
2012-05-18 22:19:31 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
ve.ce.Surface.prototype.onSelect = function( e ) {
|
|
|
|
console.log("onSelect", e);
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.onKeyPress = function( e ) {
|
|
|
|
console.log('onKeyPress');
|
|
|
|
};
|
|
|
|
|
2012-05-26 06:52:52 +00:00
|
|
|
ve.ce.Surface.prototype.onKeyDown = function( e ) {
|
2012-06-07 18:03:22 +00:00
|
|
|
console.log('onKeyDown');
|
|
|
|
// Prevent all interactions coming from keyboard when mouse is down (possibly selecting)
|
|
|
|
if ( this.isMouseDown === true ) {
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( e.keyCode ) {
|
|
|
|
// Left arrow
|
|
|
|
case 37:
|
|
|
|
break;
|
|
|
|
// Right arrow
|
|
|
|
case 39:
|
|
|
|
break;
|
|
|
|
// Backspace
|
|
|
|
case 8:
|
|
|
|
|
|
|
|
tx = ve.dm.Transaction.newFromRemoval( this.documentView.model, this.model.getSelection() );
|
|
|
|
ve.dm.TransactionProcessor.commit( this.documentView.model, tx );
|
|
|
|
this.showCursor(this.model.getSelection().start);
|
|
|
|
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
break;
|
|
|
|
// Delete
|
|
|
|
case 46:
|
|
|
|
e.preventDefault();
|
|
|
|
break;
|
2012-05-26 06:52:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-25 03:55:45 +00:00
|
|
|
ve.ce.Surface.prototype.onMouseDown = function( e ) {
|
|
|
|
this.isMouseDown = true;
|
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
// TODO: Add special handling for clicking on images and alien nodes
|
|
|
|
var $leaf = $( e.target ).closest( '.ve-ce-leafNode' );
|
|
|
|
};
|
2012-05-25 03:55:45 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
ve.ce.Surface.prototype.onMouseUp = function( e ) {
|
|
|
|
var handler = function() {
|
2012-05-26 06:52:52 +00:00
|
|
|
var rangySel = rangy.getSelection();
|
2012-05-25 03:55:45 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
if ( rangySel.anchorNode === null && rangySel.focusNode === null ) {
|
|
|
|
setTimeout( this.proxy( handler ), 100 );
|
|
|
|
return;
|
|
|
|
}
|
2012-05-25 03:55:45 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
if (
|
|
|
|
rangySel.anchorNode !== this.range.anchorNode ||
|
|
|
|
rangySel.anchorOffset !== this.range.anchorOffset ||
|
|
|
|
rangySel.focusNode !== this.range.focusNode ||
|
|
|
|
rangySel.focusOffset !== this.range.focusOffset
|
|
|
|
) {
|
|
|
|
if ( rangySel.isCollapsed ) {
|
|
|
|
var offset = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset );
|
|
|
|
this.model.setSelection( new ve.Range( offset ) );
|
|
|
|
} else {
|
|
|
|
if ( !rangySel.isBackwards() ) {
|
|
|
|
var offset1 = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset ),
|
|
|
|
offset2 = this.getOffset( rangySel.focusNode, rangySel.focusOffset );
|
|
|
|
} else {
|
|
|
|
var offset1 = this.getOffset( rangySel.focusNode, rangySel.focusOffset ),
|
|
|
|
offset2 = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset );
|
|
|
|
}
|
2012-05-25 03:55:45 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
var offset1Fixed = this.getNearestCorrectOffset( offset1, 1 ),
|
|
|
|
offset2Fixed = this.getNearestCorrectOffset( offset2, -1 );
|
2012-05-25 03:55:45 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
var range;
|
|
|
|
if ( !rangySel.isBackwards() ) {
|
|
|
|
range = new ve.Range( offset1Fixed, offset2Fixed );
|
|
|
|
} else {
|
|
|
|
range = new ve.Range( offset2Fixed, offset1Fixed );
|
|
|
|
}
|
2012-05-25 03:55:45 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
this.model.setSelection( range );
|
2012-05-25 03:55:45 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
if ( offset1 !== offset1Fixed || offset2 !== offset2Fixed ) {
|
|
|
|
this.showSelection( range );
|
|
|
|
}
|
2012-05-26 06:52:52 +00:00
|
|
|
}
|
2012-06-07 18:03:22 +00:00
|
|
|
this.range.anchorNode = rangySel.anchorNode;
|
|
|
|
this.range.anchorOffset = rangySel.anchorOffset;
|
|
|
|
this.range.focusNode = rangySel.focusNode;
|
|
|
|
this.range.focusOffset = rangySel.focusOffset;
|
2012-05-26 06:52:52 +00:00
|
|
|
}
|
2012-05-25 03:55:45 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
this.isMouseDown = false;
|
|
|
|
};
|
|
|
|
setTimeout( this.proxy( handler ), 0 );
|
2012-05-25 03:55:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.onMouseMove = function( e ) {
|
|
|
|
if ( this.isMouseDown === true ) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-25 19:02:56 +00:00
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.Surface.prototype.onCutCopy = function( e ) {
|
|
|
|
var _this = this,
|
|
|
|
sel = rangy.getSelection(),
|
2012-06-01 22:52:51 +00:00
|
|
|
$frag = null,
|
|
|
|
key = '';
|
2012-05-25 19:02:56 +00:00
|
|
|
|
2012-06-01 22:52:51 +00:00
|
|
|
// Create key from text and element names
|
|
|
|
$frag = $(sel.getRangeAt(0).cloneContents());
|
|
|
|
$frag.contents().each(function() {
|
|
|
|
key += this.textContent || this.nodeName;
|
|
|
|
});
|
|
|
|
key = key.replace( /\s/gm, '' );
|
|
|
|
|
|
|
|
// Set surface clipboard
|
2012-05-25 19:02:56 +00:00
|
|
|
this.clipboard[key] = ve.copyArray(
|
|
|
|
this.documentView.model.getData( this.getSelectionRange() )
|
|
|
|
);
|
|
|
|
|
2012-05-25 22:46:58 +00:00
|
|
|
if ( e.type == 'cut' ) {
|
2012-05-25 19:02:56 +00:00
|
|
|
setTimeout( function() {
|
2012-06-01 22:52:51 +00:00
|
|
|
var selection = null,
|
|
|
|
tx = null;
|
|
|
|
|
|
|
|
// We don't like how browsers cut, so let's undo it and do it ourselves.
|
2012-05-25 19:02:56 +00:00
|
|
|
document.execCommand('undo', false, false);
|
|
|
|
|
2012-06-01 22:52:51 +00:00
|
|
|
selection = _this.getSelectionRange();
|
2012-05-25 19:02:56 +00:00
|
|
|
|
2012-06-01 22:52:51 +00:00
|
|
|
// Transact
|
2012-06-01 23:38:27 +00:00
|
|
|
tx = ve.dm.Transaction.newFromRemoval( _this.documentView.model, selection );
|
2012-06-07 03:28:06 +00:00
|
|
|
_this.model.transact( tx );
|
2012-05-25 19:02:56 +00:00
|
|
|
|
2012-06-01 22:52:51 +00:00
|
|
|
// Place cursor
|
2012-05-25 19:02:56 +00:00
|
|
|
_this.showCursor( selection.start );
|
|
|
|
}, 1 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.Surface.prototype.onPaste = function( e ) {
|
|
|
|
var _this = this,
|
2012-06-01 23:38:27 +00:00
|
|
|
selection = this.getSelectionRange(),
|
|
|
|
tx = null;
|
|
|
|
|
|
|
|
// Pasting into a range? Remove first.
|
|
|
|
if (!rangy.getSelection().isCollapsed) {
|
|
|
|
tx = ve.dm.Transaction.newFromRemoval( _this.documentView.model, selection );
|
2012-06-07 03:28:06 +00:00
|
|
|
_this.model.transact( tx );
|
2012-06-01 23:38:27 +00:00
|
|
|
}
|
2012-05-25 19:02:56 +00:00
|
|
|
|
2012-05-30 20:06:19 +00:00
|
|
|
$('#paste').html('').show().focus();
|
2012-05-25 19:02:56 +00:00
|
|
|
|
|
|
|
setTimeout( function() {
|
2012-06-01 22:52:51 +00:00
|
|
|
var key = '',
|
|
|
|
pasteData = null,
|
|
|
|
tx = null;
|
|
|
|
|
|
|
|
// Create key from text and element names
|
|
|
|
$('#paste').hide().contents().each(function() {
|
|
|
|
key += this.textContent || this.nodeName;
|
|
|
|
});
|
|
|
|
key = key.replace( /\s/gm, '' );
|
|
|
|
|
|
|
|
// Get linear model from surface clipboard or create array from unknown pasted content
|
|
|
|
pasteData = ( _this.clipboard[key] ) ? _this.clipboard[key] : $('#paste').text().split('');
|
|
|
|
|
|
|
|
// Transact
|
|
|
|
tx = ve.dm.Transaction.newFromInsertion(
|
2012-06-01 23:38:27 +00:00
|
|
|
_this.documentView.model, selection.start, pasteData
|
2012-06-01 00:11:01 +00:00
|
|
|
);
|
2012-06-07 03:28:06 +00:00
|
|
|
_this.model.transact( tx );
|
2012-05-25 19:02:56 +00:00
|
|
|
|
2012-06-01 22:52:51 +00:00
|
|
|
// Place cursor
|
2012-06-01 23:38:27 +00:00
|
|
|
_this.showCursor( selection.start + pasteData.length );
|
2012-05-25 23:26:46 +00:00
|
|
|
_this.documentView.documentNode.$.focus();
|
2012-05-25 19:02:56 +00:00
|
|
|
}, 1 );
|
|
|
|
};
|
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
ve.ce.Surface.prototype.showCursor = function( offset ) {
|
|
|
|
this.showSelection( new ve.Range( offset ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.showSelection = function( range ) {
|
|
|
|
var rangySel = rangy.getSelection(),
|
|
|
|
rangyRange = rangy.createRange(),
|
|
|
|
start,
|
|
|
|
end;
|
|
|
|
|
|
|
|
if ( range.start !== range.end ) {
|
|
|
|
start = this.getNodeAndOffset( range.start );
|
|
|
|
end = this.getNodeAndOffset( range.end ),
|
|
|
|
rangyRange.setStart( start.node, start.offset );
|
|
|
|
rangyRange.setEnd( end.node, end.offset );
|
|
|
|
rangySel.setSingleRange( rangyRange, range.start !== range.from );
|
|
|
|
} else {
|
|
|
|
start = end = this.getNodeAndOffset( range.start );
|
|
|
|
rangyRange.setStart( start.node, start.offset );
|
|
|
|
rangySel.setSingleRange( rangyRange );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Find a better name and a better place for this method
|
|
|
|
ve.ce.Surface.prototype.getNearestCorrectOffset = function( offset, direction ) {
|
|
|
|
direction = direction > 0 ? 1 : -1;
|
|
|
|
|
|
|
|
if ( ve.dm.Document.isContentOffset( this.documentView.model.data, offset ) || this.hasSlugAtOffset( offset ) ) {
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
var contentOffset = this.documentView.model.getNearestContentOffset( offset, direction ),
|
|
|
|
structuralOffset = this.documentView.model.getNearestStructuralOffset( offset, direction, true );
|
|
|
|
|
|
|
|
if ( !this.hasSlugAtOffset( structuralOffset ) ) {
|
|
|
|
return contentOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( direction === 1 ) {
|
|
|
|
if ( contentOffset < offset ) {
|
|
|
|
return structuralOffset;
|
|
|
|
} else {
|
|
|
|
return Math.min( contentOffset, structuralOffset );
|
2012-05-25 03:55:45 +00:00
|
|
|
}
|
2012-06-07 18:03:22 +00:00
|
|
|
} else {
|
|
|
|
if ( contentOffset > offset ) {
|
|
|
|
return structuralOffset;
|
2012-05-25 03:55:45 +00:00
|
|
|
} else {
|
2012-06-07 18:03:22 +00:00
|
|
|
return Math.max( contentOffset, structuralOffset );
|
2012-05-25 03:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
// TODO: Find a better name and a better place for this method - probably in a document view?
|
|
|
|
ve.ce.Surface.prototype.hasSlugAtOffset = function( offset ) {
|
|
|
|
var node = this.documentView.documentNode.getNodeFromOffset( offset );
|
|
|
|
if ( node.canHaveChildren() ) {
|
|
|
|
return this.documentView.documentNode.getNodeFromOffset( offset ).hasSlugAtOffset( offset );
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2012-05-25 19:02:56 +00:00
|
|
|
|
|
|
|
/**
|
2012-06-01 00:11:01 +00:00
|
|
|
* Based on a given offset returns DOM node and offset that can be used to place a cursor.
|
|
|
|
*
|
|
|
|
* The results of this function are meant to be used with rangy.
|
2012-05-30 22:19:39 +00:00
|
|
|
*
|
2012-05-25 19:02:56 +00:00
|
|
|
* @method
|
|
|
|
* @param offset {Integer} Linear model offset
|
2012-06-01 00:11:01 +00:00
|
|
|
* @returns {Object} Object containing a node and offset property where node is an HTML element and
|
|
|
|
* offset is the position within the element
|
2012-05-18 22:19:31 +00:00
|
|
|
*/
|
2012-05-30 22:19:39 +00:00
|
|
|
ve.ce.Surface.prototype.getNodeAndOffset = function( offset ) {
|
|
|
|
var node = this.documentView.getNodeFromOffset( offset ),
|
2012-06-01 00:11:01 +00:00
|
|
|
startOffset = this.documentView.getDocumentNode().getOffsetFromNode( node ) +
|
|
|
|
( ( node.isWrapped() ) ? 1 : 0 ),
|
2012-05-30 22:19:39 +00:00
|
|
|
current = [node.$.contents(), 0],
|
|
|
|
stack = [current],
|
|
|
|
item,
|
|
|
|
$item,
|
|
|
|
length;
|
2012-05-25 03:55:45 +00:00
|
|
|
|
|
|
|
while ( stack.length > 0 ) {
|
|
|
|
if ( current[1] >= current[0].length ) {
|
|
|
|
stack.pop();
|
|
|
|
current = stack[ stack.length - 1 ];
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-30 22:19:39 +00:00
|
|
|
item = current[0][current[1]];
|
2012-05-30 18:41:31 +00:00
|
|
|
if ( item.nodeType === Node.TEXT_NODE ) {
|
2012-05-30 22:19:39 +00:00
|
|
|
length = item.textContent.length;
|
2012-05-25 03:55:45 +00:00
|
|
|
if ( offset >= startOffset && offset <= startOffset + length ) {
|
2012-05-30 22:19:39 +00:00
|
|
|
return {
|
|
|
|
node: item,
|
|
|
|
offset: offset - startOffset
|
|
|
|
};
|
2012-05-25 03:55:45 +00:00
|
|
|
} else {
|
|
|
|
startOffset += length;
|
|
|
|
}
|
2012-05-30 18:41:31 +00:00
|
|
|
} else if ( item.nodeType === Node.ELEMENT_NODE ) {
|
2012-05-30 22:19:39 +00:00
|
|
|
$item = current[0].eq( current[1] );
|
2012-05-25 03:55:45 +00:00
|
|
|
if ( $item.hasClass('ve-ce-slug') ) {
|
2012-05-26 06:52:52 +00:00
|
|
|
if ( offset === startOffset ) {
|
2012-05-30 22:19:39 +00:00
|
|
|
return {
|
|
|
|
node: $item[0],
|
|
|
|
offset: 1
|
|
|
|
};
|
2012-05-26 06:52:52 +00:00
|
|
|
}
|
2012-05-25 03:55:45 +00:00
|
|
|
} else if ( $item.is( '.ve-ce-branchNode, .ve-ce-leafNode' ) ) {
|
2012-05-30 22:19:39 +00:00
|
|
|
length = $item.data( 'node' ).model.getOuterLength();
|
2012-05-25 03:55:45 +00:00
|
|
|
if ( offset >= startOffset && offset < startOffset + length ) {
|
|
|
|
stack.push( [$item.contents(), 0] );
|
|
|
|
current[1]++;
|
|
|
|
current = stack[stack.length-1];
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
startOffset += length;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stack.push( [$item.contents(), 0] );
|
|
|
|
current[1]++;
|
|
|
|
current = stack[stack.length-1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
current[1]++;
|
|
|
|
}
|
2012-05-30 22:19:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-07 18:03:22 +00:00
|
|
|
* Gets the linear offset based on a given DOM node and DOM offset
|
2012-05-30 22:19:39 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-06-07 18:03:22 +00:00
|
|
|
* @param DOMnode {DOM Node} DOM node
|
|
|
|
* @param DOMoffset {Integer} DOM offset within the DOM Element
|
|
|
|
* @returns {Integer} Linear model offset
|
2012-05-30 22:19:39 +00:00
|
|
|
*/
|
2012-06-07 18:03:22 +00:00
|
|
|
ve.ce.Surface.prototype.getOffset = function( DOMnode, DOMoffset ) {
|
|
|
|
if ( DOMnode.nodeType === Node.TEXT_NODE ) {
|
|
|
|
return this.getOffsetFromTextNode( DOMnode, DOMoffset );
|
|
|
|
} else {
|
|
|
|
return this.getOffsetFromElementNode( DOMnode, DOMoffset );
|
|
|
|
}
|
2012-05-18 22:19:31 +00:00
|
|
|
};
|
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
ve.ce.Surface.prototype.getOffsetFromTextNode = function( DOMnode, DOMoffset ) {
|
|
|
|
var $node = $( DOMnode ).closest( '.ve-ce-branchNode, .ve-ce-alienBlockNode, .ve-ce-alienInlineNode' ),
|
|
|
|
nodeModel = $node.data( 'node' ).getModel();
|
|
|
|
if ( ! $node.hasClass( 've-ce-branchNode' ) ) {
|
|
|
|
return nodeModel.getOffset();
|
|
|
|
}
|
|
|
|
var current = [$node.contents(), 0],
|
|
|
|
stack = [current],
|
|
|
|
offset = 0,
|
|
|
|
item,
|
|
|
|
$item;
|
|
|
|
while ( stack.length > 0 ) {
|
|
|
|
if ( current[1] >= current[0].length ) {
|
|
|
|
stack.pop();
|
|
|
|
current = stack[ stack.length - 1 ];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
item = current[0][current[1]];
|
|
|
|
if ( item.nodeType === Node.TEXT_NODE ) {
|
|
|
|
if ( item === DOMnode ) {
|
|
|
|
offset += DOMoffset;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
offset += item.textContent.length;
|
|
|
|
}
|
|
|
|
} else if ( item.nodeType === Node.ELEMENT_NODE ) {
|
|
|
|
$item = current[0].eq( current[1] );
|
|
|
|
if ( $item.hasClass( 've-ce-slug' ) ) {
|
|
|
|
if ( $item.contents()[0] === DOMnode ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if ( $item.hasClass( 've-ce-leafNode' ) ) {
|
|
|
|
offset += 2;
|
|
|
|
} else if ( $item.hasClass( 've-ce-branchNode' ) ) {
|
|
|
|
offset += $item.data( 'node' ).getOuterLength();
|
|
|
|
} else {
|
|
|
|
stack.push( [$item.contents(), 0 ] );
|
|
|
|
current[1]++;
|
|
|
|
current = stack[stack.length-1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current[1]++;
|
|
|
|
}
|
|
|
|
return offset + nodeModel.getOffset() + ( nodeModel.isWrapped() ? 1 : 0 );
|
2012-05-18 22:19:31 +00:00
|
|
|
};
|
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
ve.ce.Surface.prototype.getOffsetFromElementNode = function( DOMnode, DOMoffset, addOuterLength ) {
|
|
|
|
var $DOMnode = $( DOMnode ),
|
|
|
|
nodeModel,
|
|
|
|
node;
|
2012-05-18 22:19:31 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
if ( $DOMnode.hasClass( 've-ce-slug' ) ) {
|
|
|
|
if ( $DOMnode.prev().length > 0 ) {
|
|
|
|
nodeModel = $DOMnode.prev().data( 'node' ).getModel();
|
|
|
|
return nodeModel.getOffset() + nodeModel.getOuterLength();
|
|
|
|
}
|
|
|
|
if ( $DOMnode.next().length > 0 ) {
|
|
|
|
nodeModel = $DOMnode.next().data( 'node' ).getModel();
|
|
|
|
return nodeModel.getOffset();
|
|
|
|
}
|
|
|
|
}
|
2012-05-25 19:02:56 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
if ( DOMoffset === 0 ) {
|
|
|
|
node = $DOMnode.data( 'node' );
|
|
|
|
if ( node ) {
|
|
|
|
nodeModel = $DOMnode.data( 'node' ).getModel();
|
|
|
|
if ( addOuterLength === true ) {
|
|
|
|
return nodeModel.getOffset() + nodeModel.getOuterLength();
|
|
|
|
} else {
|
|
|
|
return nodeModel.getOffset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
node = $DOMnode.contents().last()[0];
|
|
|
|
}
|
2012-05-25 19:02:56 +00:00
|
|
|
} else {
|
2012-06-07 18:03:22 +00:00
|
|
|
node = $DOMnode.contents()[ DOMoffset - 1 ];
|
2012-05-25 19:02:56 +00:00
|
|
|
}
|
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
if ( node.nodeType === Node.TEXT_NODE ) {
|
|
|
|
return this.getOffsetFromTextNode( node, node.length );
|
|
|
|
} else {
|
|
|
|
return this.getOffsetFromElementNode( node, 0, true );
|
|
|
|
}
|
2012-05-21 23:06:30 +00:00
|
|
|
};
|
|
|
|
|
2012-06-07 00:49:41 +00:00
|
|
|
ve.ce.Surface.prototype.getModel = function() {
|
|
|
|
return this.model;
|
|
|
|
};
|
|
|
|
|
2012-05-03 23:02:21 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.ce.Surface, ve.EventEmitter );
|