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-06-13 21:31:33 +00:00
|
|
|
this.documentView = null; // See initialization below
|
|
|
|
this.contextView = null; // See initialization below
|
2012-06-15 17:45:53 +00:00
|
|
|
this.selectionTimeout = null;
|
2012-05-04 00:19:01 +00:00
|
|
|
this.$ = $container;
|
2012-06-14 23:41:03 +00:00
|
|
|
this.$document = $( document );
|
2012-06-07 21:03:48 +00:00
|
|
|
this.clipboard = {};
|
2012-06-14 04:46:29 +00:00
|
|
|
this.render = true; // Used in ve.ce.TextNode
|
|
|
|
|
|
|
|
this.poll = {
|
|
|
|
text: null,
|
|
|
|
hash: null,
|
|
|
|
node: null,
|
|
|
|
range: null,
|
|
|
|
rangySelection: {
|
|
|
|
anchorNode: null,
|
|
|
|
anchorOffset: null,
|
|
|
|
focusNode: null,
|
|
|
|
focusOffset: null
|
|
|
|
},
|
|
|
|
polling: false,
|
|
|
|
timeout: null,
|
|
|
|
frequency: 100
|
2012-06-07 18:03:22 +00:00
|
|
|
};
|
2012-06-14 04:46:29 +00:00
|
|
|
|
2012-05-25 03:55:45 +00:00
|
|
|
// Events
|
2012-06-18 20:19:07 +00:00
|
|
|
this.model.on( 'change', ve.proxy( this.onChange, this ) );
|
|
|
|
//this.model.on( 'select', ve.proxy( this.onSelect, this ) );
|
|
|
|
//this.model.on( 'transact', ve.proxy( this.onTransact, this ) );
|
2012-06-14 04:46:29 +00:00
|
|
|
this.on( 'contentChange', ve.proxy( this.onContentChange, this ) );
|
2012-05-25 03:55:45 +00:00
|
|
|
this.$.on( {
|
2012-06-11 23:11:49 +00:00
|
|
|
'cut copy': ve.proxy( this.onCutCopy, this ),
|
2012-06-12 17:23:58 +00:00
|
|
|
'paste': ve.proxy( this.onPaste, this ),
|
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-13 21:31:33 +00:00
|
|
|
if ( $.browser.msie ) {
|
|
|
|
this.$.on( 'beforepaste', ve.proxy( this.onPaste, this ) );
|
2012-06-12 20:26:50 +00:00
|
|
|
}
|
2012-06-12 17:23:58 +00:00
|
|
|
|
2012-05-04 00:19:01 +00:00
|
|
|
// Initialization
|
2012-05-18 18:40:22 +00:00
|
|
|
try {
|
2012-06-13 21:31:33 +00:00
|
|
|
document.execCommand( 'enableObjectResizing', false, false );
|
|
|
|
document.execCommand( 'enableInlineTableEditing', false, false );
|
|
|
|
} catch ( e ) {
|
|
|
|
// Silently ignore
|
|
|
|
}
|
|
|
|
rangy.init();
|
|
|
|
// Must be initialized after select and transact listeners are added to model so respond first
|
2012-06-14 04:46:29 +00:00
|
|
|
this.documentView = new ve.ce.Document( model.getDocument(), this );
|
2012-06-13 21:31:33 +00:00
|
|
|
this.contextView = new ve.ui.Context( this );
|
2012-06-14 04:46:29 +00:00
|
|
|
this.$.append( this.documentView.getDocumentNode().$ );
|
|
|
|
|
|
|
|
// DocumentNode Events
|
|
|
|
this.documentView.getDocumentNode().$.on( {
|
2012-06-14 23:41:03 +00:00
|
|
|
'focus': ve.proxy( this.documentOnFocus, this ),
|
|
|
|
'blur': ve.proxy( this.documentOnBlur, this ),
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2012-06-18 20:19:07 +00:00
|
|
|
ve.ce.Surface.prototype.onChange = function( transaction, selection ) {
|
|
|
|
if ( selection ) {
|
|
|
|
this.showSelection( selection );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-14 23:41:03 +00:00
|
|
|
ve.ce.Surface.prototype.documentOnFocus = function() {
|
|
|
|
ve.log( 'documentOnFocus' );
|
|
|
|
|
|
|
|
this.$document.on( {
|
|
|
|
// key
|
2012-06-14 04:46:29 +00:00
|
|
|
'keydown.ve-ce-Surface': ve.proxy( this.onKeyDown, this ),
|
2012-06-14 23:41:03 +00:00
|
|
|
// mouse
|
2012-06-14 04:46:29 +00:00
|
|
|
'mousedown.ve-ce-Surface': ve.proxy( this.onMouseDown, this ),
|
2012-06-14 23:41:03 +00:00
|
|
|
'mouseup.ve-ce-Surface': ve.proxy( this.onMouseUp, this ),
|
2012-06-14 04:46:29 +00:00
|
|
|
} );
|
2012-06-15 00:24:15 +00:00
|
|
|
this.startPolling( true );
|
2012-05-03 20:21:13 +00:00
|
|
|
};
|
2012-05-03 23:02:21 +00:00
|
|
|
|
2012-06-14 23:41:03 +00:00
|
|
|
ve.ce.Surface.prototype.documentOnBlur = function() {
|
|
|
|
ve.log( 'documentOnBlur' );
|
|
|
|
|
|
|
|
this.$document.off( '.ve-ce-Surface' );
|
|
|
|
this.stopPolling();
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.onMouseDown = function( e ) {
|
|
|
|
ve.log( 'onMouseDown' );
|
|
|
|
|
|
|
|
if ( this.poll.polling === true ) {
|
|
|
|
this.pollChanges();
|
|
|
|
if ( $( e.target ).closest( '.ve-ce-documentNode' ).length === 0 ) {
|
|
|
|
this.stopPolling();
|
|
|
|
} else {
|
|
|
|
this.pollChanges( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block / prevent triple click
|
|
|
|
if ( e.originalEvent.detail > 2 ) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.onMouseUp = function( e ) {
|
|
|
|
ve.log( 'onMouseUp' );
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.stopPolling = function() {
|
|
|
|
ve.log( 'stopPolling' );
|
|
|
|
|
|
|
|
this.poll.polling = false;
|
|
|
|
clearTimeout( this.poll.timeout );
|
|
|
|
};
|
2012-05-18 22:19:31 +00:00
|
|
|
|
2012-06-15 00:19:05 +00:00
|
|
|
ve.ce.Surface.prototype.startPolling = function( async ) {
|
|
|
|
ve.log( 'startPolling' );
|
|
|
|
|
|
|
|
this.poll.polling = true;
|
|
|
|
this.pollChanges( async );
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.clearPollData = function() {
|
|
|
|
ve.log( 'clearPollData' );
|
|
|
|
|
|
|
|
this.poll.text = null;
|
|
|
|
this.poll.hash = null;
|
|
|
|
this.poll.node = null;
|
2012-06-15 00:21:50 +00:00
|
|
|
this.poll.rangySelection = {
|
2012-06-15 00:19:05 +00:00
|
|
|
anchorNode: null,
|
|
|
|
anchorOffset: null,
|
|
|
|
focusNode: null,
|
|
|
|
focusOffset: null
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-06-15 17:45:53 +00:00
|
|
|
/* Responsible for Debouncing the ContextView Icon
|
|
|
|
until select events are finished being fired. */
|
2012-06-13 21:31:33 +00:00
|
|
|
ve.ce.Surface.prototype.onSelect = function( range ) {
|
2012-06-15 17:45:53 +00:00
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
clearTimeout(this.selectionTimeout);
|
|
|
|
this.selectionTimeout = setTimeout(function(){
|
|
|
|
var selection = _this.model.getSelection();
|
2012-06-15 04:06:16 +00:00
|
|
|
if ( _this.contextView ) {
|
|
|
|
if ( selection.getLength() > 0 ) {
|
|
|
|
_this.contextView.set();
|
|
|
|
} else {
|
|
|
|
_this.contextView.clear();
|
|
|
|
}
|
2012-06-14 21:26:06 +00:00
|
|
|
}
|
2012-06-15 17:45:53 +00:00
|
|
|
}, 500);
|
2012-06-13 21:31:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.onTransact = function( tx ) {
|
|
|
|
this.showSelection( this.model.getSelection() );
|
2012-06-07 18:03:22 +00:00
|
|
|
};
|
|
|
|
|
2012-06-14 04:46:29 +00:00
|
|
|
ve.ce.Surface.prototype.onKeyDown = function( e ) {
|
2012-06-14 19:17:42 +00:00
|
|
|
switch ( e.keyCode ) {
|
|
|
|
// Left arrow
|
|
|
|
case 37:
|
|
|
|
break;
|
|
|
|
// Right arrow
|
|
|
|
case 39:
|
|
|
|
break;
|
2012-06-12 02:40:09 +00:00
|
|
|
// Enter
|
|
|
|
case 13:
|
|
|
|
e.preventDefault();
|
|
|
|
this.handleEnter();
|
|
|
|
break;
|
2012-06-14 19:17:42 +00:00
|
|
|
// Backspace
|
|
|
|
case 8:
|
|
|
|
this.handleDelete( true );
|
|
|
|
e.preventDefault();
|
|
|
|
break;
|
|
|
|
// Delete
|
|
|
|
case 46:
|
|
|
|
this.handleDelete( false );
|
|
|
|
e.preventDefault();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ( this.poll.polling === false ) {
|
|
|
|
this.poll.polling = true;
|
|
|
|
this.pollChanges();
|
|
|
|
}
|
2012-06-12 02:40:09 +00:00
|
|
|
}
|
2012-06-07 18:03:22 +00:00
|
|
|
};
|
|
|
|
|
2012-06-14 19:17:42 +00:00
|
|
|
ve.ce.Surface.prototype.onKeyPress = function( e ) {
|
2012-06-18 20:30:14 +00:00
|
|
|
ve.log('onKeyPress');
|
2012-06-14 19:17:42 +00:00
|
|
|
};
|
|
|
|
|
2012-06-14 05:19:10 +00:00
|
|
|
|
2012-06-14 04:46:29 +00:00
|
|
|
|
|
|
|
ve.ce.Surface.prototype.pollChanges = function( async ) {
|
2012-06-14 19:17:42 +00:00
|
|
|
var delay = ve.proxy( function( async ) {
|
2012-06-14 04:46:29 +00:00
|
|
|
if ( this.poll.polling ) {
|
|
|
|
if ( this.poll.timeout !== null ) {
|
|
|
|
clearTimeout( this.poll.timeout );
|
|
|
|
}
|
|
|
|
this.poll.timeout = setTimeout( ve.proxy( this.pollChanges, this ), async ? 0 : this.poll.frequency );
|
|
|
|
}
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
if ( async ) {
|
|
|
|
delay( true );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var node = this.poll.node,
|
|
|
|
range = this.poll.range,
|
|
|
|
rangySelection = rangy.getSelection();
|
|
|
|
|
|
|
|
if (
|
|
|
|
rangySelection.anchorNode !== this.poll.rangySelection.anchorNode ||
|
|
|
|
rangySelection.anchorOffset !== this.poll.rangySelection.anchorOffset ||
|
|
|
|
rangySelection.focusNode !== this.poll.rangySelection.focusNode ||
|
|
|
|
rangySelection.focusOffset !== this.poll.rangySelection.focusOffset
|
|
|
|
) {
|
|
|
|
this.poll.rangySelection.anchorNode = rangySelection.anchorNode;
|
|
|
|
this.poll.rangySelection.anchorOffset = rangySelection.anchorOffset;
|
|
|
|
this.poll.rangySelection.focusNode = rangySelection.focusNode;
|
|
|
|
this.poll.rangySelection.focusOffset = rangySelection.focusOffset;
|
|
|
|
|
|
|
|
// TODO: Optimize for the case of collapsed (rangySelection.isCollapsed) range
|
|
|
|
|
|
|
|
var $anchorNode = $( rangySelection.anchorNode ).closest( '.ve-ce-branchNode' ),
|
|
|
|
$focusNode = $( rangySelection.focusNode ).closest( '.ve-ce-branchNode' );
|
|
|
|
|
|
|
|
if ( $anchorNode[0] === $focusNode[0] ) {
|
|
|
|
node = $anchorNode[0]
|
|
|
|
} else {
|
|
|
|
node = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Do we really need to figure out range even if node is null? (YES)
|
|
|
|
|
|
|
|
range = new ve.Range(
|
|
|
|
this.getOffset( rangySelection.anchorNode, rangySelection.anchorOffset ),
|
|
|
|
this.getOffset( rangySelection.focusNode, rangySelection.focusOffset )
|
|
|
|
);
|
2012-05-26 06:52:52 +00:00
|
|
|
}
|
2012-06-14 04:46:29 +00:00
|
|
|
|
2012-06-14 05:23:59 +00:00
|
|
|
// TODO: Invastigate more when and why node is null and what to do in those cases
|
|
|
|
|
2012-06-14 04:46:29 +00:00
|
|
|
if ( this.poll.node !== node ) {
|
|
|
|
if ( node === null ) {
|
2012-06-14 05:23:59 +00:00
|
|
|
this.poll.text = this.poll.hash = this.poll.node = null;
|
|
|
|
} else {
|
|
|
|
this.poll.text = ve.ce.getDomText( node );
|
|
|
|
this.poll.hash = ve.ce.getDomHash( node );
|
|
|
|
this.poll.node = node;
|
2012-06-14 04:46:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-06-14 05:23:59 +00:00
|
|
|
if ( node !== null ) {
|
|
|
|
var text = ve.ce.getDomText( node ),
|
|
|
|
hash = ve.ce.getDomHash( node );
|
|
|
|
if ( this.poll.text !== text || this.poll.hash !== hash ) {
|
|
|
|
this.emit( 'contentChange', {
|
|
|
|
'node': node,
|
|
|
|
'old': {
|
|
|
|
'text': this.poll.text,
|
|
|
|
'hash': this.poll.hash,
|
|
|
|
'range': this.poll.range,
|
|
|
|
},
|
|
|
|
'new': {
|
|
|
|
'text': text,
|
|
|
|
'hash': hash,
|
|
|
|
'range': range
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
this.poll.text = text;
|
|
|
|
this.poll.hash = hash;
|
|
|
|
}
|
2012-06-14 04:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.poll.range !== range ) {
|
2012-06-18 20:19:07 +00:00
|
|
|
|
|
|
|
console.log('old', range.start);
|
|
|
|
if ( range.getLength() === 0 ) {
|
|
|
|
range = new ve.Range( this.getNearestCorrectOffset( range.start, 1 ) );
|
|
|
|
}
|
|
|
|
console.log('new', range.start);
|
|
|
|
|
|
|
|
// TODO: Fix range
|
|
|
|
this.model.change( null, range );
|
2012-06-14 04:46:29 +00:00
|
|
|
this.poll.range = range;
|
|
|
|
}
|
|
|
|
|
|
|
|
delay();
|
2012-05-26 06:52:52 +00:00
|
|
|
};
|
|
|
|
|
2012-06-14 19:17:42 +00:00
|
|
|
ve.ce.Surface.prototype.clearPollData = function() {
|
|
|
|
this.poll.text = null;
|
|
|
|
this.poll.hash = null;
|
|
|
|
this.poll.node = null;
|
|
|
|
this.poll.rangySelection = {
|
|
|
|
anchorNode: null,
|
|
|
|
anchorOffset: null,
|
|
|
|
focusNode: null,
|
|
|
|
focusOffset: null
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-06-14 04:46:29 +00:00
|
|
|
ve.ce.Surface.prototype.onContentChange = function( e ) {
|
|
|
|
var nodeOffset = $( e.node ).data( 'node' ).model.getOffset(),
|
|
|
|
offsetDiff = (
|
|
|
|
e.old.range !== null &&
|
|
|
|
e.new.range !== null &&
|
|
|
|
e.old.range.getLength() === 0 &&
|
|
|
|
e.new.range.getLength() === 0
|
|
|
|
) ? e.new.range.start - e.old.range.start : null,
|
|
|
|
lengthDiff = e.new.text.length - e.old.text.length;
|
|
|
|
|
|
|
|
if (
|
|
|
|
offsetDiff === lengthDiff &&
|
|
|
|
e.old.text.substring( 0, e.old.range.start - nodeOffset - 1 ) ===
|
|
|
|
e.new.text.substring( 0, e.old.range.start - nodeOffset - 1 )
|
|
|
|
) {
|
|
|
|
var data = e.new.text.substring(
|
|
|
|
e.old.range.start - nodeOffset - 1,
|
|
|
|
e.new.range.start - nodeOffset - 1
|
|
|
|
).split( '' ),
|
|
|
|
annotations = this.model.getDocument().getAnnotationsFromOffset( e.old.range.start - 1 );
|
|
|
|
|
2012-06-15 02:24:44 +00:00
|
|
|
if ( !ve.isEmptyObject( annotations ) ) {
|
|
|
|
ve.dm.Document.addAnnotationsToData( data, annotations );
|
|
|
|
}
|
2012-06-14 04:46:29 +00:00
|
|
|
|
|
|
|
this.render = false;
|
|
|
|
|
2012-06-18 20:19:07 +00:00
|
|
|
this.model.change(
|
|
|
|
ve.dm.Transaction.newFromInsertion( this.documentView.model, e.old.range.start, data ),
|
|
|
|
e.new.range
|
|
|
|
);
|
2012-06-14 04:46:29 +00:00
|
|
|
|
|
|
|
this.render = true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
var fromLeft = 0,
|
|
|
|
fromRight = 0,
|
|
|
|
len = Math.min( e.old.text.length, e.new.text.length );
|
|
|
|
|
|
|
|
while ( fromLeft < len && e.old.text[fromLeft] === e.new.text[fromLeft] ) {
|
|
|
|
++fromLeft;
|
|
|
|
}
|
|
|
|
while (
|
|
|
|
fromRight < len - fromLeft &&
|
|
|
|
e.old.text[e.old.text.length - 1 - fromRight] ===
|
|
|
|
e.new.text[e.new.text.length - 1 - fromRight]
|
|
|
|
) {
|
|
|
|
++fromRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
var annotations = this.model.getDocument().getAnnotationsFromOffset( nodeOffset + 1 + fromLeft ),
|
|
|
|
data = e.new.text.substring( fromLeft, e.new.text.length - fromRight ).split( '' );
|
|
|
|
|
2012-06-15 02:24:44 +00:00
|
|
|
if ( !ve.isEmptyObject( annotations ) ) {
|
|
|
|
ve.dm.Document.addAnnotationsToData( data, annotations );
|
|
|
|
}
|
2012-06-14 04:46:29 +00:00
|
|
|
|
2012-06-14 19:17:42 +00:00
|
|
|
|
2012-06-14 04:46:29 +00:00
|
|
|
this.hideSelection();
|
2012-06-18 20:19:07 +00:00
|
|
|
this.clearPollData();
|
2012-06-14 19:17:42 +00:00
|
|
|
|
2012-06-14 04:46:29 +00:00
|
|
|
// TODO: combine newFromRemoval and newFromInsertion into one newFromReplacement
|
|
|
|
if ( fromLeft + fromRight < e.old.text.length ) {
|
2012-06-18 20:19:07 +00:00
|
|
|
this.model.change(
|
|
|
|
ve.dm.Transaction.newFromRemoval(
|
|
|
|
this.documentView.model,
|
|
|
|
new ve.Range( nodeOffset + 1 + fromLeft, nodeOffset + 1 + e.old.text.length - fromRight )
|
|
|
|
),
|
|
|
|
e.new.range
|
|
|
|
);
|
2012-06-14 04:46:29 +00:00
|
|
|
}
|
2012-06-18 20:19:07 +00:00
|
|
|
this.model.change(
|
|
|
|
ve.dm.Transaction.newFromInsertion( this.documentView.model, nodeOffset + 1 + fromLeft, data ),
|
|
|
|
e.new.range
|
|
|
|
);
|
2012-06-14 04:46:29 +00:00
|
|
|
}
|
2012-06-07 18:03:22 +00:00
|
|
|
};
|
2012-05-25 03:55:45 +00:00
|
|
|
|
2012-06-14 04:46:29 +00:00
|
|
|
/*
|
2012-06-07 18:03:22 +00:00
|
|
|
ve.ce.Surface.prototype.onMouseUp = function( e ) {
|
|
|
|
var handler = function() {
|
2012-06-11 23:11:49 +00:00
|
|
|
var rangySel = rangy.getSelection(),
|
|
|
|
offset1,
|
|
|
|
offset2;
|
2012-05-25 03:55:45 +00:00
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
if ( rangySel.anchorNode === null && rangySel.focusNode === null ) {
|
2012-06-11 23:11:49 +00:00
|
|
|
setTimeout( ve.proxy( handler, this ), 100 );
|
2012-06-07 18:03:22 +00:00
|
|
|
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() ) {
|
2012-06-11 23:11:49 +00:00
|
|
|
offset1 = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset );
|
|
|
|
offset2 = this.getOffset( rangySel.focusNode, rangySel.focusOffset );
|
2012-06-07 18:03:22 +00:00
|
|
|
} else {
|
2012-06-11 23:11:49 +00:00
|
|
|
offset1 = this.getOffset( rangySel.focusNode, rangySel.focusOffset );
|
|
|
|
offset2 = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset );
|
2012-06-07 18:03:22 +00:00
|
|
|
}
|
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;
|
2012-06-07 21:13:55 +00:00
|
|
|
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;
|
|
|
|
};
|
2012-06-11 23:11:49 +00:00
|
|
|
setTimeout( ve.proxy( handler, this ), 0 );
|
2012-05-25 03:55:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.onMouseMove = function( e ) {
|
|
|
|
if ( this.isMouseDown === true ) {
|
|
|
|
}
|
|
|
|
};
|
2012-06-14 04:46:29 +00:00
|
|
|
*/
|
2012-05-25 03:55:45 +00:00
|
|
|
|
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-18 20:19:07 +00:00
|
|
|
this.stopPolling();
|
|
|
|
|
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(
|
2012-06-07 21:03:48 +00:00
|
|
|
this.documentView.model.getData( this.model.getSelection() )
|
2012-05-25 19:02:56 +00:00
|
|
|
);
|
|
|
|
|
2012-05-25 22:46:58 +00:00
|
|
|
if ( e.type == 'cut' ) {
|
2012-06-15 18:47:05 +00:00
|
|
|
this.stopPolling();
|
|
|
|
|
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-07 21:03:48 +00:00
|
|
|
selection = _this.model.getSelection();
|
2012-05-25 19:02:56 +00:00
|
|
|
|
2012-06-01 22:52:51 +00:00
|
|
|
// Transact
|
2012-06-14 04:46:29 +00:00
|
|
|
_this.autoRender = true;
|
2012-06-01 23:38:27 +00:00
|
|
|
tx = ve.dm.Transaction.newFromRemoval( _this.documentView.model, selection );
|
2012-06-18 20:19:07 +00:00
|
|
|
_this.model.change( tx, new ve.Range( selection.start ) );
|
2012-06-14 04:46:29 +00:00
|
|
|
_this.autoRender = false;
|
2012-05-25 19:02:56 +00:00
|
|
|
|
2012-06-15 18:47:05 +00:00
|
|
|
_this.clearPollData();
|
|
|
|
_this.startPolling();
|
2012-05-25 19:02:56 +00:00
|
|
|
}, 1 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.Surface.prototype.onPaste = function( e ) {
|
|
|
|
var _this = this,
|
2012-06-07 21:03:48 +00:00
|
|
|
selection = this.model.getSelection(),
|
2012-06-01 23:38:27 +00:00
|
|
|
tx = null;
|
|
|
|
|
2012-06-15 18:47:05 +00:00
|
|
|
this.stopPolling();
|
|
|
|
|
2012-06-07 21:13:55 +00:00
|
|
|
// Pasting into a range? Remove first.
|
2012-06-01 23:38:27 +00:00
|
|
|
if (!rangy.getSelection().isCollapsed) {
|
|
|
|
tx = ve.dm.Transaction.newFromRemoval( _this.documentView.model, selection );
|
2012-06-18 20:19:07 +00:00
|
|
|
_this.model.change( 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-18 20:19:07 +00:00
|
|
|
_this.model.change( tx, new ve.Range( selection.start + pasteData.length ) );
|
2012-05-25 23:26:46 +00:00
|
|
|
_this.documentView.documentNode.$.focus();
|
2012-06-15 18:47:05 +00:00
|
|
|
|
|
|
|
_this.clearPollData();
|
|
|
|
_this.startPolling();
|
2012-05-25 19:02:56 +00:00
|
|
|
}, 1 );
|
|
|
|
};
|
|
|
|
|
2012-06-12 02:40:09 +00:00
|
|
|
ve.ce.Surface.prototype.handleEnter = function() {
|
|
|
|
this.stopPolling();
|
2012-06-15 00:21:05 +00:00
|
|
|
|
2012-06-18 20:19:07 +00:00
|
|
|
var selection = this.model.getSelection();
|
|
|
|
|
2012-06-12 02:40:09 +00:00
|
|
|
if ( selection.from !== selection.to ) {
|
2012-06-18 20:19:07 +00:00
|
|
|
this.model.change(
|
|
|
|
ve.dm.Transaction.newFromRemoval( this.documentView.model, selection )
|
|
|
|
);
|
2012-06-12 02:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var node = this.documentView.getNodeFromOffset( selection.to ),
|
|
|
|
nodeOffset = this.documentView.getDocumentNode().getOffsetFromNode( node ),
|
|
|
|
stack = [];
|
|
|
|
|
|
|
|
// Build stack
|
|
|
|
ve.Node.traverseUpstream( node, function( node ) {
|
|
|
|
var elementType = node.type;
|
|
|
|
if ( !node.canBeSplit() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
stack.splice(
|
|
|
|
stack.length / 2,
|
|
|
|
0,
|
|
|
|
{ 'type': '/' + elementType },
|
|
|
|
{
|
|
|
|
'type': elementType,
|
|
|
|
'attributes': ve.copyObject( node.model.attributes )
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
|
2012-06-18 20:19:07 +00:00
|
|
|
this.model.change(
|
|
|
|
ve.dm.Transaction.newFromInsertion( this.documentView.model, selection.from, stack ),
|
|
|
|
new ve.Range ( this.model.getDocument().getRelativeContentOffset( selection.from, 1 )
|
|
|
|
) );
|
2012-06-12 02:40:09 +00:00
|
|
|
|
2012-06-15 00:21:05 +00:00
|
|
|
this.clearPollData();
|
|
|
|
this.startPolling();
|
2012-06-12 02:40:09 +00:00
|
|
|
};
|
|
|
|
|
2012-06-14 19:17:42 +00:00
|
|
|
ve.ce.Surface.prototype.handleDelete = function( backspace ) {
|
|
|
|
this.stopPolling();
|
|
|
|
|
|
|
|
var selection = this.model.getSelection(),
|
|
|
|
sourceOffset,
|
|
|
|
targetOffset,
|
|
|
|
sourceSplitableNode,
|
|
|
|
targetSplitableNode,
|
|
|
|
tx,
|
|
|
|
cursorAt;
|
|
|
|
|
|
|
|
if ( selection.from === selection.to ) {
|
|
|
|
if ( backspace ) {
|
|
|
|
sourceOffset = selection.to;
|
|
|
|
targetOffset = this.model.getDocument().getRelativeContentOffset( sourceOffset, -1 );
|
2012-06-18 20:19:07 +00:00
|
|
|
//this.model.setSelection( new ve.Range( targetOffset, targetOffset ) );
|
2012-06-14 19:17:42 +00:00
|
|
|
} else {
|
|
|
|
sourceOffset = this.model.getDocument().getRelativeContentOffset( selection.to, 1 );
|
|
|
|
targetOffset = selection.to;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sourceNode = this.documentView.getNodeFromOffset( sourceOffset, false ),
|
|
|
|
targetNode = this.documentView.getNodeFromOffset( targetOffset, false );
|
|
|
|
|
|
|
|
if ( sourceNode.type === targetNode.type ) {
|
|
|
|
sourceSplitableNode = ve.ce.Node.getSplitableNode( sourceNode );
|
|
|
|
targetSplitableNode = ve.ce.Node.getSplitableNode( targetNode );
|
|
|
|
}
|
2012-06-18 20:30:14 +00:00
|
|
|
//ve.log(sourceSplitableNode, targetSplitableNode);
|
2012-06-14 19:17:42 +00:00
|
|
|
|
|
|
|
cursorAt = targetOffset;
|
|
|
|
|
|
|
|
|
|
|
|
if ( sourceNode === targetNode ||
|
|
|
|
( typeof sourceSplitableNode !== 'undefined' &&
|
|
|
|
sourceSplitableNode.getParent() === targetSplitableNode.getParent() ) ) {
|
|
|
|
// Source and target are the same node or have the same parent (list items)
|
|
|
|
|
|
|
|
// Transact
|
|
|
|
tx = ve.dm.Transaction.newFromRemoval( this.documentView.model, new ve.Range( targetOffset, sourceOffset ) );
|
2012-06-18 20:19:07 +00:00
|
|
|
this.model.change( tx, new ve.Range( cursorAt ) );
|
2012-06-14 19:17:42 +00:00
|
|
|
} else {
|
|
|
|
// Source and target are different nodes and do not share a parent. Perform tricky merge.
|
|
|
|
|
|
|
|
// !!!This portion still in development
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Transact
|
|
|
|
/*
|
|
|
|
tx = ve.dm.Transaction.newFromInsertion(
|
|
|
|
this.documentView.model, targetOffset, sourceNode.model.getContentData()
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
|
2012-06-18 20:30:14 +00:00
|
|
|
ve.log('sourceNode', sourceNode);
|
|
|
|
ve.log('sourceOffset', sourceOffset);
|
|
|
|
ve.log('targetNode', targetNode);
|
|
|
|
ve.log('targetOffset', targetOffset);
|
2012-06-14 19:17:42 +00:00
|
|
|
return;
|
2012-06-18 20:30:14 +00:00
|
|
|
ve.log('length: ', sourceNode.model.getLength());
|
|
|
|
ve.log('outerlength: ', sourceNode.model.getOuterLength());
|
|
|
|
ve.log('offset', this.documentView.getDocumentNode().getOffsetFromNode( sourceNode ));
|
|
|
|
ve.log('some data: ', this.documentView.model.getData( this.model.getSelection() ) )
|
2012-06-14 19:17:42 +00:00
|
|
|
|
|
|
|
// Find the node that should be completely removed
|
|
|
|
var nodeToDelete = sourceNode;
|
|
|
|
ve.Node.traverseUpstream( nodeToDelete, function( node ) {
|
|
|
|
if ( node.getParent().children.length === 1 ) {
|
|
|
|
nodeToDelete = node.getParent();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Create range surrounding the entire sourceNode and remove
|
2012-06-15 02:34:30 +00:00
|
|
|
/*var range = new ve.Range();
|
2012-06-14 19:17:42 +00:00
|
|
|
range.from = this.documentView.getDocumentNode().getOffsetFromNode( sourceNode )
|
2012-06-15 02:34:30 +00:00
|
|
|
range.to = range.from + sourceNode.model.getOuterLength();*/
|
|
|
|
// Just setting .from and .to produces an invalid range with .start and .end unset --Roan
|
|
|
|
var from = this.documentView.getDocumentNode().getOffsetFromNode( sourceNode ),
|
|
|
|
range = new ve.Range(
|
|
|
|
from,
|
|
|
|
from + sourceNode.model.getOuterLength()
|
|
|
|
);
|
2012-06-14 19:17:42 +00:00
|
|
|
tx = ve.dm.Transaction.newFromRemoval( this.documentView.model, range );
|
|
|
|
this.model.transact( tx );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// selection removal
|
|
|
|
tx = ve.dm.Transaction.newFromRemoval( this.documentView.model, selection );
|
2012-06-18 20:19:07 +00:00
|
|
|
this.model.change( tx, new ve.Range( selection.start ) );
|
2012-06-14 19:17:42 +00:00
|
|
|
}
|
|
|
|
|
2012-06-15 00:21:05 +00:00
|
|
|
this.clearPollData();
|
2012-06-14 19:17:42 +00:00
|
|
|
this.startPolling();
|
|
|
|
};
|
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
ve.ce.Surface.prototype.showCursor = function( offset ) {
|
|
|
|
this.showSelection( new ve.Range( offset ) );
|
2012-06-07 21:13:55 +00:00
|
|
|
};
|
2012-06-07 18:03:22 +00:00
|
|
|
|
|
|
|
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 );
|
2012-06-14 21:33:33 +00:00
|
|
|
rangySel.removeAllRanges();
|
|
|
|
rangySel.addRange( rangyRange, range.start !== range.from );
|
2012-06-07 18:03:22 +00:00
|
|
|
} else {
|
|
|
|
start = end = this.getNodeAndOffset( range.start );
|
|
|
|
rangyRange.setStart( start.node, start.offset );
|
|
|
|
rangySel.setSingleRange( rangyRange );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-14 04:46:29 +00:00
|
|
|
ve.ce.Surface.prototype.hideSelection = function() {
|
|
|
|
rangy.getSelection().removeAllRanges();
|
|
|
|
};
|
|
|
|
|
2012-06-07 18:03:22 +00:00
|
|
|
// 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;
|
|
|
|
|
2012-06-11 23:11:49 +00:00
|
|
|
if (
|
|
|
|
ve.dm.Document.isContentOffset( this.documentView.model.data, offset ) ||
|
|
|
|
this.hasSlugAtOffset( offset )
|
|
|
|
) {
|
2012-06-07 18:03:22 +00:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
var contentOffset = this.documentView.model.getNearestContentOffset( offset, direction ),
|
2012-06-11 23:11:49 +00:00
|
|
|
structuralOffset =
|
|
|
|
this.documentView.model.getNearestStructuralOffset( offset, direction, true );
|
2012-06-07 18:03:22 +00:00
|
|
|
|
|
|
|
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() ) {
|
2012-06-12 01:09:43 +00:00
|
|
|
return node.hasSlugAtOffset( offset );
|
2012-06-07 18:03:22 +00:00
|
|
|
} 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-11 23:11:49 +00:00
|
|
|
* @param domNode {DOM Node} DOM node
|
|
|
|
* @param domOffset {Integer} DOM offset within the DOM Element
|
2012-06-07 18:03:22 +00:00
|
|
|
* @returns {Integer} Linear model offset
|
2012-05-30 22:19:39 +00:00
|
|
|
*/
|
2012-06-11 23:11:49 +00:00
|
|
|
ve.ce.Surface.prototype.getOffset = function( domNode, domOffset ) {
|
|
|
|
if ( domNode.nodeType === Node.TEXT_NODE ) {
|
|
|
|
return this.getOffsetFromTextNode( domNode, domOffset );
|
2012-06-07 18:03:22 +00:00
|
|
|
} else {
|
2012-06-11 23:11:49 +00:00
|
|
|
return this.getOffsetFromElementNode( domNode, domOffset );
|
2012-06-07 18:03:22 +00:00
|
|
|
}
|
2012-05-18 22:19:31 +00:00
|
|
|
};
|
|
|
|
|
2012-06-11 23:11:49 +00:00
|
|
|
ve.ce.Surface.prototype.getOffsetFromTextNode = function( domNode, domOffset ) {
|
|
|
|
var $node = $( domNode ).closest(
|
|
|
|
'.ve-ce-branchNode, .ve-ce-alienBlockNode, .ve-ce-alienInlineNode'
|
|
|
|
),
|
2012-06-07 18:03:22 +00:00
|
|
|
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 ) {
|
2012-06-11 23:11:49 +00:00
|
|
|
if ( item === domNode ) {
|
|
|
|
offset += domOffset;
|
2012-06-07 18:03:22 +00:00
|
|
|
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' ) ) {
|
2012-06-11 23:11:49 +00:00
|
|
|
if ( $item.contents()[0] === domNode ) {
|
2012-06-07 18:03:22 +00:00
|
|
|
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-11 23:11:49 +00:00
|
|
|
ve.ce.Surface.prototype.getOffsetFromElementNode = function( domNode, domOffset, addOuterLength ) {
|
|
|
|
var $domNode = $( domNode ),
|
2012-06-07 18:03:22 +00:00
|
|
|
nodeModel,
|
|
|
|
node;
|
2012-05-18 22:19:31 +00:00
|
|
|
|
2012-06-11 23:11:49 +00:00
|
|
|
if ( $domNode.hasClass( 've-ce-slug' ) ) {
|
|
|
|
if ( $domNode.prev().length > 0 ) {
|
|
|
|
nodeModel = $domNode.prev().data( 'node' ).getModel();
|
2012-06-07 18:03:22 +00:00
|
|
|
return nodeModel.getOffset() + nodeModel.getOuterLength();
|
|
|
|
}
|
2012-06-11 23:11:49 +00:00
|
|
|
if ( $domNode.next().length > 0 ) {
|
|
|
|
nodeModel = $domNode.next().data( 'node' ).getModel();
|
2012-06-07 18:03:22 +00:00
|
|
|
return nodeModel.getOffset();
|
|
|
|
}
|
|
|
|
}
|
2012-05-25 19:02:56 +00:00
|
|
|
|
2012-06-11 23:11:49 +00:00
|
|
|
if ( domOffset === 0 ) {
|
|
|
|
node = $domNode.data( 'node' );
|
2012-06-07 18:03:22 +00:00
|
|
|
if ( node ) {
|
2012-06-11 23:11:49 +00:00
|
|
|
nodeModel = $domNode.data( 'node' ).getModel();
|
2012-06-07 18:03:22 +00:00
|
|
|
if ( addOuterLength === true ) {
|
|
|
|
return nodeModel.getOffset() + nodeModel.getOuterLength();
|
|
|
|
} else {
|
|
|
|
return nodeModel.getOffset();
|
|
|
|
}
|
|
|
|
} else {
|
2012-06-11 23:11:49 +00:00
|
|
|
node = $domNode.contents().last()[0];
|
2012-06-07 18:03:22 +00:00
|
|
|
}
|
2012-05-25 19:02:56 +00:00
|
|
|
} else {
|
2012-06-11 23:11:49 +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-14 21:26:06 +00:00
|
|
|
ve.ce.Surface.prototype.updateContextIcon = function() {
|
|
|
|
var _this = this,
|
|
|
|
selection = this.model.getSelection();
|
|
|
|
|
|
|
|
if ( this.contextView ) {
|
|
|
|
if ( selection.getLength() > 0 ) {
|
|
|
|
this.contextView.set();
|
|
|
|
} else {
|
|
|
|
this.contextView.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-06-07 21:13:55 +00:00
|
|
|
/* Supplies the selection anchor coordinates to contextView */
|
|
|
|
ve.ce.Surface.prototype.getSelectionRect = function() {
|
|
|
|
var rangySel = rangy.getSelection();
|
|
|
|
return {
|
|
|
|
start: rangySel.getStartDocumentPos(),
|
|
|
|
end: rangySel.getEndDocumentPos()
|
|
|
|
};
|
|
|
|
};
|
2012-06-07 00:49:41 +00:00
|
|
|
|
2012-06-14 05:05:38 +00:00
|
|
|
ve.ce.Surface.prototype.getModel = function() {
|
|
|
|
return this.model;
|
2012-06-14 04:46:29 +00:00
|
|
|
};
|
|
|
|
|
2012-05-03 23:02:21 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.ce.Surface, ve.EventEmitter );
|