2012-02-07 19:13:19 +00:00
|
|
|
/**
|
2012-03-06 22:39:43 +00:00
|
|
|
* Creates an ve.ce.Surface object.
|
2012-03-07 19:33:00 +00:00
|
|
|
*
|
2012-02-07 19:13:19 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {jQuery} $container DOM Container to render surface into
|
|
|
|
* @param {ve.dm.Surface} model Surface model to view
|
|
|
|
*/
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface = function( $container, model ) {
|
2012-02-07 19:13:19 +00:00
|
|
|
// Inheritance
|
|
|
|
ve.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// References for use in closures
|
|
|
|
var _this = this,
|
|
|
|
$document = $( document ),
|
|
|
|
$window = $( window );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.model = model;
|
2012-03-12 21:50:22 +00:00
|
|
|
this.currentSelection = new ve.Range();
|
2012-04-18 18:24:38 +00:00
|
|
|
this.selectionDirection = null;
|
2012-03-06 22:39:43 +00:00
|
|
|
this.documentView = new ve.ce.DocumentNode( this.model.getDocument(), this );
|
2012-02-07 19:13:19 +00:00
|
|
|
this.contextView = null;
|
|
|
|
this.$ = $container
|
2012-04-04 18:53:23 +00:00
|
|
|
.addClass( 've-ce-surface' )
|
2012-02-07 19:13:19 +00:00
|
|
|
.append( this.documentView.$ );
|
2012-03-07 19:37:17 +00:00
|
|
|
this.insertionAnnotations = [];
|
2012-02-07 19:13:19 +00:00
|
|
|
this.emitUpdateTimeout = undefined;
|
2012-03-01 22:27:23 +00:00
|
|
|
this.clipboard = {};
|
2012-03-07 08:13:12 +00:00
|
|
|
this.autoRender = false;
|
2012-02-07 19:13:19 +00:00
|
|
|
|
|
|
|
// Events
|
2012-03-16 22:01:09 +00:00
|
|
|
this.documentView.$.on( {
|
2012-02-13 22:45:18 +00:00
|
|
|
'focus': function( e ) {
|
2012-03-01 01:28:39 +00:00
|
|
|
_this.documentOnFocus();
|
2012-04-04 18:53:23 +00:00
|
|
|
$document.off( '.ve-ce-surface' );
|
2012-03-16 22:01:09 +00:00
|
|
|
$document.on( {
|
2012-04-06 15:10:30 +00:00
|
|
|
'keydown.ce-surfaceView': function( e ) {
|
2012-03-02 00:10:08 +00:00
|
|
|
return _this.onKeyDown( e );
|
2012-03-14 00:04:24 +00:00
|
|
|
},
|
2012-04-06 15:10:30 +00:00
|
|
|
'mousemove.ce-surfaceView': function( 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-04-04 18:53:23 +00:00
|
|
|
$document.on( '.ve-ce-surface' );
|
2012-02-13 22:45:18 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2012-03-16 22:01:09 +00:00
|
|
|
this.$.on( {
|
|
|
|
'cut copy': function( e ) {
|
2012-03-06 23:51:31 +00:00
|
|
|
_this.onCutCopy( e );
|
2012-03-16 22:01:09 +00:00
|
|
|
},
|
|
|
|
'paste': function( e ) {
|
2012-03-06 23:51:31 +00:00
|
|
|
_this.onPaste( e );
|
2012-03-16 22:01:09 +00:00
|
|
|
},
|
|
|
|
'mousedown': function( e ) {
|
2012-03-14 00:04:24 +00:00
|
|
|
return _this.onMouseDown( e );
|
2012-03-16 22:01:09 +00:00
|
|
|
},
|
|
|
|
'compositionstart': function( e ) {
|
2012-03-02 01:12:18 +00:00
|
|
|
_this.onCompositionStart( e );
|
2012-03-16 22:01:09 +00:00
|
|
|
},
|
|
|
|
'compositionend': function( e ) {
|
2012-03-02 01:12:18 +00:00
|
|
|
_this.onCompositionEnd( e );
|
2012-03-16 22:01:09 +00:00
|
|
|
},
|
|
|
|
'dragover drop': function( e ) {
|
2012-03-02 01:12:18 +00:00
|
|
|
e.preventDefault();
|
2012-03-16 22:01:09 +00:00
|
|
|
}
|
|
|
|
} );
|
2012-02-13 22:45:18 +00:00
|
|
|
|
2012-03-01 01:28:39 +00:00
|
|
|
// Initialization
|
|
|
|
this.documentView.renderContent();
|
|
|
|
|
|
|
|
this.poll = {
|
2012-04-06 15:10:30 +00:00
|
|
|
polling: false,
|
|
|
|
timeout: null,
|
|
|
|
frequency: 100,
|
|
|
|
rangy: {
|
|
|
|
anchorNode: null,
|
|
|
|
anchorOffset: null,
|
|
|
|
focusNode: null,
|
|
|
|
focusOffset: null
|
|
|
|
},
|
|
|
|
range: null,
|
|
|
|
node: null,
|
|
|
|
text: null,
|
|
|
|
hash: null,
|
|
|
|
composition: {
|
|
|
|
start: null,
|
|
|
|
end: null
|
|
|
|
}
|
2012-03-01 01:28:39 +00:00
|
|
|
};
|
2012-04-06 15:10:30 +00:00
|
|
|
|
|
|
|
this.on( 'textChange', function( e ) {
|
|
|
|
_this.onTextChange( e );
|
|
|
|
} );
|
|
|
|
|
2012-04-06 15:43:14 +00:00
|
|
|
this.on( 'rangeChange', function( e ) {
|
2012-04-18 18:24:38 +00:00
|
|
|
|
|
|
|
/* Save selection direction only
|
|
|
|
if selection start and end have changed */
|
|
|
|
if( e.new && e.old ) {
|
|
|
|
if( e.new.start !== e.old.start || e.new.end !== e.old.end ) {
|
|
|
|
this.selectionDirection = ( e.new.to < e.new.from ) ? -1 : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-06 15:43:14 +00:00
|
|
|
if ( e.new !== null ) {
|
2012-04-18 18:24:38 +00:00
|
|
|
/* Set selection in the model */
|
2012-04-06 15:43:14 +00:00
|
|
|
_this.model.setSelection( e.new );
|
2012-04-18 18:24:38 +00:00
|
|
|
|
|
|
|
/* Hide or show context view icon */
|
|
|
|
_this.toggleContextView();
|
|
|
|
|
|
|
|
/* Load or clear annotations */
|
2012-04-06 15:43:14 +00:00
|
|
|
if ( e.new.getLength() === 0 ) {
|
|
|
|
_this.loadInsertionAnnotations();
|
|
|
|
} else {
|
|
|
|
_this.clearInsertionAnnotations();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2012-03-12 23:06:47 +00:00
|
|
|
//Prevent native contentedtiable tools
|
|
|
|
try {
|
|
|
|
document.execCommand("enableInlineTableEditing", false, false);
|
|
|
|
document.execCommand("enableObjectResizing", false, false);
|
|
|
|
} catch(e) {
|
|
|
|
}
|
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-04-06 15:10:30 +00:00
|
|
|
ve.ce.Surface.prototype.onCompositionStart = function( e ) {
|
|
|
|
var rangySel = rangy.getSelection();
|
|
|
|
this.poll.composition.start = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset );
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.onCompositionEnd = function( e ) {
|
|
|
|
var rangySel = rangy.getSelection();
|
|
|
|
this.poll.composition.end = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset );
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.onTextChange = function( e ) {
|
|
|
|
var oldOffset = null,
|
|
|
|
newOffset = null;
|
|
|
|
|
|
|
|
if (
|
|
|
|
e.old.range !== null &&
|
|
|
|
e.new.range !== null &&
|
|
|
|
e.old.range.getLength() === 0 &&
|
|
|
|
e.new.range.getLength() === 0
|
|
|
|
) {
|
|
|
|
oldOffset = e.old.range.start;
|
|
|
|
newOffset = e.new.range.start;
|
|
|
|
}
|
|
|
|
|
|
|
|
var lengthDiff = e.new.text.length - e.old.text.length,
|
|
|
|
offsetDiff = ( newOffset !== null && oldOffset !== null ) ? newOffset - oldOffset : null,
|
|
|
|
nodeOffset = this.documentView.getOffsetFromNode( $( e.node ).data( 'view' ) );
|
|
|
|
|
|
|
|
if (
|
|
|
|
lengthDiff === offsetDiff &&
|
|
|
|
e.old.text.substring( 0, oldOffset - nodeOffset - 1 ) === e.new.text.substring( 0, oldOffset - nodeOffset - 1 )
|
|
|
|
) {
|
|
|
|
var data = e.new.text.substring( oldOffset - nodeOffset - 1, newOffset - nodeOffset - 1).split( '' );
|
|
|
|
var annotations = this.model.getDocument().getAnnotationsFromOffset( oldOffset - 1, true );
|
|
|
|
ve.dm.DocumentNode.addAnnotationsToData( data, annotations );
|
|
|
|
this.model.transact( this.documentView.model.prepareInsertion( oldOffset, data ) );
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
len -= fromLeft;
|
|
|
|
while (
|
|
|
|
fromRight < len &&
|
|
|
|
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, true
|
|
|
|
);
|
|
|
|
if ( fromLeft + fromRight < e.old.text.length ) {
|
|
|
|
this.model.transact( this.documentView.model.prepareRemoval( new ve.Range(
|
|
|
|
nodeOffset + 1 + fromLeft,
|
|
|
|
nodeOffset + 1 + e.old.text.length - fromRight
|
|
|
|
) ) );
|
|
|
|
}
|
|
|
|
var data = e.new.text.substring( fromLeft, e.new.text.length - fromRight ).split( '' );
|
|
|
|
ve.dm.DocumentNode.addAnnotationsToData( data, annotations );
|
|
|
|
this.model.transact(
|
|
|
|
this.documentView.model.prepareInsertion( nodeOffset + 1 + fromLeft, data )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.Surface.prototype.startPolling = function( async ) {
|
2012-04-06 15:43:14 +00:00
|
|
|
if ( this.poll.polling === false ) {
|
|
|
|
this.poll.polling = true;
|
|
|
|
this.pollChanges( async );
|
|
|
|
}
|
2012-04-06 15:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.Surface.prototype.stopPolling = function() {
|
|
|
|
if ( this.poll.polling === true ) {
|
|
|
|
this.poll.polling = false;
|
|
|
|
clearTimeout( this.poll.timeout );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.Surface.prototype.pollChanges = function( async ) {
|
|
|
|
var _this = this;
|
|
|
|
var delay = function( now ) {
|
|
|
|
if ( _this.poll.polling === true ) {
|
|
|
|
_this.poll.timeout = setTimeout(
|
|
|
|
function() {
|
|
|
|
_this.pollChanges();
|
|
|
|
},
|
|
|
|
now === true ? 0 : _this.poll.frequency
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( async === true ) {
|
|
|
|
delay( true );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.poll.composition.start !== null ) {
|
|
|
|
if ( this.poll.composition.end !== null ) {
|
|
|
|
this.poll.composition.start = this.poll.composition.end = null;
|
|
|
|
} else {
|
|
|
|
delay();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var rangySel = rangy.getSelection(),
|
|
|
|
range = this.poll.range,
|
|
|
|
node = this.poll.node;
|
|
|
|
|
|
|
|
if (
|
|
|
|
rangySel.anchorNode !== this.poll.rangy.anchorNode ||
|
|
|
|
rangySel.anchorOffset !== this.poll.rangy.anchorOffset ||
|
|
|
|
rangySel.focusNode !== this.poll.rangy.focusNode ||
|
|
|
|
rangySel.focusOffset !== this.poll.rangy.focusOffset
|
|
|
|
) {
|
|
|
|
this.poll.rangy.anchorNode = rangySel.anchorNode;
|
|
|
|
this.poll.rangy.anchorOffset = rangySel.anchorOffset;
|
|
|
|
this.poll.rangy.focusNode = rangySel.focusNode;
|
|
|
|
this.poll.rangy.focusOffset = rangySel.focusOffset;
|
|
|
|
|
|
|
|
if ( !this.documentView.$.is(':focus') ) {
|
|
|
|
range = null;
|
|
|
|
node = null;
|
|
|
|
} else {
|
|
|
|
if ( rangySel.isCollapsed ) {
|
|
|
|
range = new ve.Range(
|
|
|
|
this.getOffset( rangySel.anchorNode, rangySel.anchorOffset )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
range = new ve.Range(
|
|
|
|
this.getOffset( rangySel.anchorNode, rangySel.anchorOffset ),
|
|
|
|
this.getOffset( rangySel.focusNode, rangySel.focusOffset )
|
|
|
|
);
|
|
|
|
}
|
2012-04-06 15:13:43 +00:00
|
|
|
node = ve.ce.Surface.getLeafNode( rangySel.anchorNode )[0];
|
2012-04-06 15:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.poll.node !== node ) {
|
|
|
|
// TODO: emit nodeChange event?
|
|
|
|
this.poll.node = node;
|
|
|
|
this.poll.text = ve.ce.Surface.getDOMText2( node );
|
|
|
|
this.poll.hash = ve.ce.Surface.getDOMHash( node );
|
|
|
|
} else {
|
|
|
|
if ( node !== null ) {
|
|
|
|
var text = ve.ce.Surface.getDOMText2( node ),
|
|
|
|
hash = ve.ce.Surface.getDOMHash( node );
|
|
|
|
if ( this.poll.text !== text || this.poll.hash !== hash ) {
|
|
|
|
this.emit( 'textChange', {
|
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.poll.range !== range ) {
|
|
|
|
// TODO: find a proper/better name for this event
|
|
|
|
this.emit( 'rangeChange', { 'old': this.poll.range, 'new': range } );
|
|
|
|
this.poll.range = range;
|
|
|
|
}
|
|
|
|
|
|
|
|
delay();
|
2012-04-06 15:43:14 +00:00
|
|
|
return;
|
2012-04-06 15:10:30 +00:00
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.annotate = function( method, annotation ) {
|
2012-04-12 23:37:23 +00:00
|
|
|
var selection = this.model.getSelection(),
|
2012-03-07 21:06:07 +00:00
|
|
|
_this = this;
|
|
|
|
|
2012-03-05 22:08:35 +00:00
|
|
|
if ( method === 'toggle' ) {
|
|
|
|
var annotations = this.getAnnotations();
|
|
|
|
if ( ve.dm.DocumentNode.getIndexOfAnnotation( annotations.full, annotation ) !== -1 ) {
|
|
|
|
method = 'clear';
|
|
|
|
} else {
|
|
|
|
method = 'set';
|
|
|
|
}
|
|
|
|
}
|
2012-04-12 23:37:23 +00:00
|
|
|
if ( selection.getLength() ) {
|
2012-03-05 22:08:35 +00:00
|
|
|
var tx = this.model.getDocument().prepareContentAnnotation(
|
2012-04-12 23:37:23 +00:00
|
|
|
selection, method, annotation
|
2012-03-05 22:08:35 +00:00
|
|
|
);
|
2012-03-07 21:06:07 +00:00
|
|
|
|
|
|
|
// transact with autoRender
|
2012-03-07 19:33:00 +00:00
|
|
|
this.autoRender = true;
|
2012-03-05 22:08:35 +00:00
|
|
|
this.model.transact( tx );
|
2012-03-07 08:13:12 +00:00
|
|
|
this.autoRender = false;
|
2012-03-07 21:06:07 +00:00
|
|
|
|
|
|
|
this.clearPollData();
|
|
|
|
|
2012-04-12 23:37:23 +00:00
|
|
|
_this.showSelection( selection );
|
2012-03-05 22:08:35 +00:00
|
|
|
} else {
|
|
|
|
if ( method === 'set' ) {
|
|
|
|
this.addInsertionAnnotation( annotation );
|
|
|
|
} else if ( method === 'clear' ) {
|
|
|
|
this.removeInsertionAnnotation( annotation );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-07 19:37:17 +00:00
|
|
|
ve.ce.Surface.prototype.getAnnotations = function() {
|
2012-03-12 20:39:08 +00:00
|
|
|
return this.getSelectionRange().getLength() ?
|
|
|
|
this.model.getDocument().getAnnotationsFromRange( this.getSelectionRange() ) :
|
2012-03-07 19:37:17 +00:00
|
|
|
{
|
|
|
|
'full': this.insertionAnnotations,
|
|
|
|
'partial': [],
|
|
|
|
'all': this.insertionAnnotations
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.getInsertionAnnotations = function() {
|
|
|
|
return this.insertionAnnotations;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.addInsertionAnnotation = function( annotation ) {
|
|
|
|
this.insertionAnnotations.push( annotation );
|
|
|
|
};
|
|
|
|
|
2012-04-06 15:43:14 +00:00
|
|
|
ve.ce.Surface.prototype.loadInsertionAnnotations = function() {
|
|
|
|
this.insertionAnnotations = this.model.getDocument().getAnnotationsFromOffset( this.model.getSelection().to - 1 );
|
2012-03-07 19:37:17 +00:00
|
|
|
// Filter out annotations that aren't textStyles or links
|
|
|
|
for ( var i = 0; i < this.insertionAnnotations.length; i++ ) {
|
|
|
|
if ( !this.insertionAnnotations[i].type.match( /(textStyle\/|link\/)/ ) ) {
|
|
|
|
this.insertionAnnotations.splice( i, 1 );
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.removeInsertionAnnotation = function( annotation ) {
|
|
|
|
var index = ve.dm.DocumentNode.getIndexOfAnnotation( this.insertionAnnotations, annotation );
|
|
|
|
if ( index !== -1 ) {
|
|
|
|
this.insertionAnnotations.splice( index, 1 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.Surface.prototype.clearInsertionAnnotations = function() {
|
|
|
|
this.insertionAnnotations = [];
|
2012-03-05 22:08:35 +00:00
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.onCutCopy = function( e ) {
|
2012-03-01 22:27:23 +00:00
|
|
|
var _this = this,
|
2012-03-06 23:51:31 +00:00
|
|
|
sel = rangy.getSelection(),
|
|
|
|
key = sel.getRangeAt(0).toString().replace( /\s/gm, '' );
|
2012-03-01 22:27:23 +00:00
|
|
|
|
2012-03-09 22:51:00 +00:00
|
|
|
_this.clipboard[key] = ve.copyArray(
|
|
|
|
_this.documentView.model.getData( _this.getSelectionRange() )
|
|
|
|
);
|
2012-03-01 22:27:23 +00:00
|
|
|
|
|
|
|
if ( event.type == 'cut' ) {
|
|
|
|
setTimeout( function() {
|
2012-03-02 00:37:01 +00:00
|
|
|
// we don't like how browsers cut, so let's undo it and do it ourselves.
|
2012-03-01 22:27:23 +00:00
|
|
|
document.execCommand('undo', false, false);
|
2012-03-02 00:37:01 +00:00
|
|
|
|
2012-03-07 21:41:53 +00:00
|
|
|
var selection = _this.getSelectionRange();
|
2012-03-02 00:37:01 +00:00
|
|
|
|
|
|
|
// transact
|
2012-03-01 22:27:23 +00:00
|
|
|
var tx = _this.model.getDocument().prepareRemoval( selection );
|
2012-03-07 21:41:53 +00:00
|
|
|
|
|
|
|
_this.autoRender = true;
|
2012-03-01 22:27:23 +00:00
|
|
|
_this.model.transact( tx );
|
2012-03-07 21:41:53 +00:00
|
|
|
_this.autoRender = false;
|
2012-03-02 00:37:01 +00:00
|
|
|
|
2012-03-07 21:06:07 +00:00
|
|
|
_this.clearPollData();
|
2012-03-02 00:37:01 +00:00
|
|
|
|
|
|
|
// place cursor
|
2012-03-07 23:33:41 +00:00
|
|
|
_this.showCursor( selection.start );
|
2012-03-01 22:27:23 +00:00
|
|
|
}, 1 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.onPaste = function( e ) {
|
2012-04-06 15:10:30 +00:00
|
|
|
return;
|
2012-03-02 00:10:08 +00:00
|
|
|
var _this = this,
|
2012-03-07 21:41:53 +00:00
|
|
|
insertionPoint = _this.getSelectionRange().start;
|
2012-03-02 00:10:08 +00:00
|
|
|
|
2012-03-06 23:51:31 +00:00
|
|
|
$('#paste')
|
|
|
|
.html('')
|
|
|
|
.show()
|
|
|
|
.css( 'top', $(window).scrollTop() )
|
2012-03-07 21:06:07 +00:00
|
|
|
.css( 'left', $(window).scrollLeft() )
|
2012-03-06 23:51:31 +00:00
|
|
|
.focus();
|
2012-03-01 22:27:23 +00:00
|
|
|
|
|
|
|
setTimeout( function() {
|
2012-03-06 23:51:31 +00:00
|
|
|
var key = $('#paste').hide().text().replace( /\s/gm, '' );
|
2012-03-01 22:27:23 +00:00
|
|
|
|
2012-03-07 19:33:00 +00:00
|
|
|
if ( _this.clipboard[key] ) {
|
2012-03-01 22:27:23 +00:00
|
|
|
// transact
|
2012-03-06 23:51:31 +00:00
|
|
|
var tx = _this.documentView.model.prepareInsertion(
|
|
|
|
insertionPoint, _this.clipboard[key]
|
|
|
|
);
|
2012-03-07 08:13:12 +00:00
|
|
|
_this.autoRender = true;
|
2012-03-01 22:27:23 +00:00
|
|
|
_this.model.transact( tx );
|
2012-03-07 08:13:12 +00:00
|
|
|
_this.autoRender = false;
|
2012-03-02 00:27:04 +00:00
|
|
|
|
2012-03-07 21:06:07 +00:00
|
|
|
_this.clearPollData();
|
2012-03-01 22:27:23 +00:00
|
|
|
|
|
|
|
// place cursor
|
2012-03-07 23:33:41 +00:00
|
|
|
_this.showCursor( insertionPoint + _this.clipboard[key].length );
|
2012-03-01 22:27:23 +00:00
|
|
|
} else {
|
|
|
|
alert('i can only handle copy/paste from hybrid surface. sorry. :(');
|
|
|
|
}
|
|
|
|
}, 1 );
|
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.attachContextView = function( contextView ) {
|
2012-02-13 22:45:18 +00:00
|
|
|
this.contextView = contextView;
|
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.getModel = function() {
|
2012-02-13 22:45:18 +00:00
|
|
|
return this.model;
|
|
|
|
};
|
|
|
|
|
2012-04-18 18:24:38 +00:00
|
|
|
ve.ce.Surface.prototype.toggleContextView = function( delay ) {
|
2012-04-12 21:02:59 +00:00
|
|
|
var _this = this,
|
|
|
|
selection = this.model.getSelection();
|
|
|
|
|
2012-03-12 21:50:22 +00:00
|
|
|
function update() {
|
2012-04-12 21:02:59 +00:00
|
|
|
if ( selection.getLength() > 0 ) {
|
2012-03-12 21:50:22 +00:00
|
|
|
_this.clearInsertionAnnotations();
|
|
|
|
}
|
|
|
|
if ( _this.contextView ) {
|
2012-04-12 21:02:59 +00:00
|
|
|
if ( selection.getLength() > 0 ) {
|
2012-03-12 21:50:22 +00:00
|
|
|
_this.contextView.set();
|
|
|
|
} else {
|
|
|
|
_this.contextView.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_this.updateSelectionTimeout = undefined;
|
2012-04-11 20:19:58 +00:00
|
|
|
|
2012-03-12 21:50:22 +00:00
|
|
|
}
|
|
|
|
if ( delay ) {
|
|
|
|
if ( this.updateSelectionTimeout !== undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.updateSelectionTimeout = setTimeout( update, delay );
|
|
|
|
} else {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.documentOnFocus = function() {
|
2012-04-06 15:10:30 +00:00
|
|
|
this.startPolling( true );
|
2012-02-13 22:45:18 +00:00
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.documentOnBlur = function() {
|
2012-03-01 01:28:39 +00:00
|
|
|
this.stopPolling();
|
2012-02-13 22:45:18 +00:00
|
|
|
};
|
|
|
|
|
2012-03-07 21:06:07 +00:00
|
|
|
ve.ce.Surface.prototype.clearPollData = function() {
|
2012-04-06 15:10:30 +00:00
|
|
|
/*
|
2012-03-07 21:06:07 +00:00
|
|
|
this.stopPolling();
|
|
|
|
this.poll.prevText =
|
|
|
|
this.poll.prevHash =
|
|
|
|
this.poll.prevOffset =
|
|
|
|
this.poll.node = null;
|
|
|
|
this.startPolling();
|
2012-04-06 15:10:30 +00:00
|
|
|
*/
|
2012-03-01 01:28:39 +00:00
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.onMouseDown = function( e ) {
|
2012-04-06 15:10:30 +00:00
|
|
|
//this.pollChanges();
|
2012-04-06 15:43:14 +00:00
|
|
|
//this.pollChanges( true );
|
2012-04-06 15:10:30 +00:00
|
|
|
return;
|
2012-03-02 02:07:55 +00:00
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.onKeyDown = function( e ) {
|
2012-04-06 15:43:14 +00:00
|
|
|
//this.pollChanges();
|
2012-04-06 15:10:30 +00:00
|
|
|
//this.pollChanges( true );
|
|
|
|
return;
|
|
|
|
|
2012-03-02 02:07:55 +00:00
|
|
|
if ( this.poll.interval !== null ) {
|
|
|
|
this.stopPolling();
|
|
|
|
this.pollContent();
|
|
|
|
this.startPolling();
|
|
|
|
}
|
2012-03-06 23:51:31 +00:00
|
|
|
var sel,
|
|
|
|
globalOffset,
|
|
|
|
newOffset,
|
|
|
|
node,
|
|
|
|
nodeOffset;
|
2012-03-02 00:10:08 +00:00
|
|
|
switch ( e.keyCode ) {
|
2012-03-02 01:35:34 +00:00
|
|
|
// Enter
|
|
|
|
case 13:
|
|
|
|
this.handleEnter();
|
|
|
|
e.preventDefault();
|
|
|
|
break;
|
|
|
|
// Backspace
|
|
|
|
case 8:
|
|
|
|
this.handleDelete( true );
|
|
|
|
e.preventDefault();
|
|
|
|
break;
|
|
|
|
// Delete
|
|
|
|
case 46:
|
|
|
|
this.handleDelete( false );
|
|
|
|
e.preventDefault();
|
|
|
|
break;
|
2012-03-02 00:10:08 +00:00
|
|
|
// Left arrow
|
|
|
|
case 37:
|
2012-03-06 23:51:31 +00:00
|
|
|
sel = rangy.getSelection();
|
|
|
|
if ( sel.anchorOffset === 0 ) {
|
|
|
|
globalOffset = this.getOffset( sel.anchorNode, sel.anchorOffset, true );
|
|
|
|
node = this.documentView.getNodeFromOffset( globalOffset );
|
2012-04-02 20:43:03 +00:00
|
|
|
nodeOffset = this.documentView.getOffsetFromNode( node );
|
2012-03-02 00:10:08 +00:00
|
|
|
if ( nodeOffset + 1 === globalOffset ) {
|
2012-03-06 23:51:31 +00:00
|
|
|
newOffset = this.documentView.model.getRelativeContentOffset(
|
|
|
|
globalOffset, -1
|
|
|
|
);
|
2012-03-07 23:33:41 +00:00
|
|
|
this.showCursor(newOffset);
|
2012-03-02 00:10:08 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Right arrow
|
|
|
|
case 39:
|
2012-03-06 23:51:31 +00:00
|
|
|
sel = rangy.getSelection();
|
|
|
|
if ( sel.anchorOffset === sel.anchorNode.length ) {
|
|
|
|
globalOffset = this.getOffset( sel.anchorNode, sel.anchorOffset, true );
|
|
|
|
node = this.documentView.getNodeFromOffset( globalOffset );
|
2012-04-02 20:43:03 +00:00
|
|
|
nodeOffset = this.documentView.getOffsetFromNode( node );
|
2012-03-02 00:34:31 +00:00
|
|
|
if ( nodeOffset + 1 + node.getContentLength() === globalOffset ) {
|
2012-03-06 23:51:31 +00:00
|
|
|
newOffset = this.documentView.model.getRelativeContentOffset(
|
|
|
|
globalOffset, 1
|
|
|
|
);
|
2012-03-07 23:33:41 +00:00
|
|
|
this.showCursor(newOffset);
|
2012-03-02 00:34:31 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
2012-03-02 00:10:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-03-07 21:37:39 +00:00
|
|
|
var range = this.getSelectionRange();
|
2012-03-12 20:39:08 +00:00
|
|
|
if ( range.getLength() !== 0 && this.poll.compositionStart === null) {
|
2012-03-02 01:35:34 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
2012-03-02 00:10:08 +00:00
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.getOffset = function( elem, offset, global ) {
|
2012-04-06 15:10:30 +00:00
|
|
|
var global = true;
|
2012-04-06 15:13:43 +00:00
|
|
|
var $leafNode = ve.ce.Surface.getLeafNode( elem );
|
2012-04-06 15:10:30 +00:00
|
|
|
if ( $leafNode === null ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-16 22:16:15 +00:00
|
|
|
var current = [$leafNode.contents(), 0],
|
2012-03-01 01:28:39 +00:00
|
|
|
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-06 23:51:31 +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
|
|
|
};
|
|
|
|
|
2012-03-07 21:37:39 +00:00
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
* @returns {ve.Range} Current selection range
|
|
|
|
*/
|
|
|
|
ve.ce.Surface.prototype.getSelectionRange = function() {
|
|
|
|
var rangySel = rangy.getSelection();
|
|
|
|
|
|
|
|
if ( rangySel.isCollapsed ) {
|
|
|
|
var offset = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, true );
|
|
|
|
return new ve.Range( offset, offset );
|
2012-03-01 20:49:21 +00:00
|
|
|
} else {
|
2012-03-07 21:37:39 +00:00
|
|
|
return new ve.Range(
|
|
|
|
this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, true ),
|
|
|
|
this.getOffset( rangySel.focusNode, rangySel.focusOffset, true )
|
2012-03-01 20:49:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-12 21:50:22 +00:00
|
|
|
ve.ce.Surface.prototype.getSelectionRect = function() {
|
2012-03-15 22:06:40 +00:00
|
|
|
var rangySel = rangy.getSelection();
|
|
|
|
return {
|
2012-03-16 21:52:17 +00:00
|
|
|
start: rangySel.getStartDocumentPos(),
|
|
|
|
end: rangySel.getEndDocumentPos()
|
2012-03-15 22:06:40 +00:00
|
|
|
};
|
2012-03-12 21:50:22 +00:00
|
|
|
};
|
|
|
|
|
2012-03-07 23:32:26 +00:00
|
|
|
ve.ce.Surface.prototype.getDOMNodeAndOffset = function( offset ) {
|
|
|
|
var $node = this.documentView.getNodeFromOffset( offset ).$,
|
|
|
|
nodeOffset = this.documentView.getOffsetFromNode( $node.data('view') ) + 1,
|
|
|
|
current = [$node.contents(), 0],
|
|
|
|
stack = [current],
|
|
|
|
localNode,
|
|
|
|
localOffset;
|
|
|
|
|
|
|
|
while ( stack.length > 0 ) {
|
|
|
|
if ( current[1] >= current[0].length ) {
|
|
|
|
stack.pop();
|
|
|
|
current = stack[ stack.length - 1 ];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var item = current[0][current[1]],
|
|
|
|
$item = current[0].eq( current[1] );
|
|
|
|
|
|
|
|
if ( item.nodeType === 3 ) {
|
|
|
|
var length = item.textContent.length;
|
|
|
|
if ( offset >= nodeOffset && offset <= nodeOffset + length ) {
|
|
|
|
return {
|
|
|
|
node: item,
|
|
|
|
offset: offset - nodeOffset
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
nodeOffset += length;
|
|
|
|
}
|
|
|
|
} else if ( item.nodeType === 1 ) {
|
|
|
|
if ( $( item ).attr('contentEditable') === 'false' ) {
|
|
|
|
nodeOffset += 1;
|
|
|
|
} else {
|
|
|
|
stack.push( [$item.contents(), 0] );
|
|
|
|
current[1]++;
|
|
|
|
current = stack[stack.length-1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current[1]++;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.Surface.prototype.showCursor = function( offset ) {
|
|
|
|
this.showSelection( new ve.Range( offset ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.Surface.prototype.showSelection = function( range ) {
|
|
|
|
var start = this.getDOMNodeAndOffset( range.start ),
|
|
|
|
stop = this.getDOMNodeAndOffset( range.end ),
|
|
|
|
sel = rangy.getSelection();
|
2012-04-18 18:24:38 +00:00
|
|
|
|
2012-03-09 22:55:49 +00:00
|
|
|
// FIXME: Shadowing range, really?
|
|
|
|
range = rangy.createRange();
|
2012-03-07 23:32:26 +00:00
|
|
|
range.setStart( start.node, start.offset );
|
|
|
|
range.setEnd( stop.node, stop.offset );
|
|
|
|
sel.setSingleRange( range );
|
|
|
|
};
|
|
|
|
|
2012-03-14 00:04:24 +00:00
|
|
|
ve.ce.Surface.getLeafNode = function( elem ) {
|
2012-04-04 18:53:23 +00:00
|
|
|
var $leaf = $( elem ).closest( '.ve-ce-leafNode' );
|
|
|
|
return $leaf.length ? $leaf : null;
|
2012-02-09 22:11:33 +00:00
|
|
|
};
|
2012-04-04 18:53:23 +00:00
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.getDOMText2 = function( elem ) {
|
2012-03-01 01:28:39 +00:00
|
|
|
// TODO: there must be some better way to write this regex replace
|
2012-03-07 19:33:00 +00:00
|
|
|
var regex = new RegExp('[' + String.fromCharCode(32) + String.fromCharCode(160) + ']', 'g');
|
2012-03-06 23:51:31 +00:00
|
|
|
return ve.ce.Surface.getDOMText( elem ).replace( regex, ' ' );
|
2012-02-09 22:11:33 +00:00
|
|
|
};
|
2012-02-09 00:51:59 +00:00
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.getDOMText = function( elem ) {
|
2012-03-06 23:51:31 +00:00
|
|
|
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.ce.Surface.getDOMText( elem );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ( nodeType === 3 || nodeType === 4 ) {
|
|
|
|
return elem.nodeValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-02-07 19:13:19 +00:00
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.getDOMHash = function( elem ) {
|
2012-03-06 23:51:31 +00:00
|
|
|
var nodeType = elem.nodeType,
|
2012-03-01 22:14:14 +00:00
|
|
|
nodeName = elem.nodeName,
|
2012-03-06 23:51:31 +00:00
|
|
|
ret = '';
|
2012-03-01 01:28:39 +00:00
|
|
|
|
|
|
|
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-06 22:39:43 +00:00
|
|
|
ret += ve.ce.Surface.getDOMHash( elem );
|
2012-03-06 23:51:31 +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-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.handleDelete = function( backspace, isPartial ) {
|
2012-03-02 01:35:34 +00:00
|
|
|
this.stopPolling();
|
2012-03-07 23:35:38 +00:00
|
|
|
this.clearPollData();
|
2012-03-07 21:37:39 +00:00
|
|
|
var selection = this.getSelectionRange().clone(),
|
2012-03-02 01:35:34 +00:00
|
|
|
sourceOffset,
|
|
|
|
targetOffset,
|
|
|
|
sourceSplitableNode,
|
|
|
|
targetSplitableNode,
|
|
|
|
tx,
|
|
|
|
cursorAt;
|
|
|
|
// this.resetText();
|
|
|
|
if ( selection.from === selection.to ) {
|
|
|
|
if ( backspace ) {
|
|
|
|
sourceOffset = selection.to;
|
|
|
|
targetOffset = this.model.getDocument().getRelativeContentOffset(
|
|
|
|
sourceOffset,
|
|
|
|
-1
|
|
|
|
);
|
|
|
|
} 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.model.getElementType() === targetNode.model.getElementType() ) {
|
2012-03-06 22:39:43 +00:00
|
|
|
sourceSplitableNode = ve.ce.Node.getSplitableNode( sourceNode );
|
|
|
|
targetSplitableNode = ve.ce.Node.getSplitableNode( targetNode );
|
2012-03-02 01:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cursorAt = targetOffset;
|
|
|
|
|
|
|
|
if ( sourceNode === targetNode ||
|
|
|
|
( typeof sourceSplitableNode !== 'undefined' &&
|
|
|
|
sourceSplitableNode.getParent() === targetSplitableNode.getParent() ) ) {
|
|
|
|
tx = this.model.getDocument().prepareRemoval(
|
|
|
|
new ve.Range( targetOffset, sourceOffset )
|
|
|
|
);
|
|
|
|
this.model.transact( tx );
|
|
|
|
} else {
|
|
|
|
tx = this.model.getDocument().prepareInsertion(
|
|
|
|
targetOffset, sourceNode.model.getContentData()
|
|
|
|
);
|
|
|
|
this.model.transact( tx );
|
|
|
|
|
|
|
|
var nodeToDelete = sourceNode;
|
|
|
|
ve.Node.traverseUpstream( nodeToDelete, function( node ) {
|
|
|
|
if ( node.getParent().children.length === 1 ) {
|
|
|
|
nodeToDelete = node.getParent();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
var range = new ve.Range();
|
|
|
|
range.from = this.documentView.getOffsetFromNode( nodeToDelete, false );
|
|
|
|
range.to = range.from + nodeToDelete.getElementLength();
|
|
|
|
tx = this.model.getDocument().prepareRemoval( range );
|
|
|
|
this.model.transact( tx );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// selection removal
|
|
|
|
tx = this.model.getDocument().prepareRemoval( selection );
|
|
|
|
this.model.transact( tx );
|
|
|
|
cursorAt = selection.start;
|
|
|
|
}
|
|
|
|
this.documentView.renderContent();
|
2012-03-13 18:12:57 +00:00
|
|
|
this.showCursor(cursorAt);
|
2012-03-02 01:35:34 +00:00
|
|
|
var _this = this;
|
|
|
|
setTimeout( function() {
|
|
|
|
_this.poll.prevText = _this.poll.prevHash = _this.poll.prevOffset = _this.poll.node = null;
|
|
|
|
_this.startPolling();
|
|
|
|
}, 0 );
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.Surface.prototype.handleEnter = function() {
|
2012-03-02 01:35:34 +00:00
|
|
|
this.stopPolling();
|
2012-03-07 21:37:39 +00:00
|
|
|
var selection = this.getSelectionRange().clone(),
|
2012-03-02 01:35:34 +00:00
|
|
|
tx;
|
|
|
|
if ( selection.from !== selection.to ) {
|
|
|
|
this.handleDelete( false, true );
|
|
|
|
}
|
|
|
|
var node = this.documentView.getNodeFromOffset( selection.to, false ),
|
|
|
|
nodeOffset = this.documentView.getOffsetFromNode( node, false );
|
|
|
|
|
|
|
|
if (
|
|
|
|
nodeOffset + node.getContentLength() + 1 === selection.to &&
|
2012-03-06 22:39:43 +00:00
|
|
|
node === ve.ce.Node.getSplitableNode( node )
|
2012-03-02 01:35:34 +00:00
|
|
|
) {
|
|
|
|
tx = this.documentView.model.prepareInsertion(
|
|
|
|
nodeOffset + node.getElementLength(),
|
|
|
|
[ { 'type': 'paragraph' }, { 'type': '/paragraph' } ]
|
|
|
|
);
|
|
|
|
this.model.transact( tx );
|
|
|
|
selection.from = selection.to = nodeOffset + node.getElementLength() + 1;
|
|
|
|
} else {
|
|
|
|
var stack = [],
|
|
|
|
splitable = false;
|
|
|
|
|
|
|
|
ve.Node.traverseUpstream( node, function( node ) {
|
|
|
|
var elementType = node.model.getElementType();
|
|
|
|
if (
|
|
|
|
splitable === true &&
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce.DocumentNode.splitRules[ elementType ].children === true
|
2012-03-02 01:35:34 +00:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
stack.splice(
|
|
|
|
stack.length / 2,
|
|
|
|
0,
|
|
|
|
{ 'type': '/' + elementType },
|
|
|
|
{
|
|
|
|
'type': elementType,
|
|
|
|
'attributes': ve.copyObject( node.model.element.attributes )
|
|
|
|
}
|
|
|
|
);
|
2012-03-06 22:39:43 +00:00
|
|
|
splitable = ve.ce.DocumentNode.splitRules[ elementType ].self;
|
2012-03-02 01:35:34 +00:00
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
tx = this.documentView.model.prepareInsertion( selection.to, stack );
|
|
|
|
this.model.transact( tx );
|
|
|
|
selection.from = selection.to =
|
|
|
|
this.model.getDocument().getRelativeContentOffset( selection.to, 1 );
|
|
|
|
}
|
|
|
|
this.documentView.renderContent();
|
2012-03-13 18:12:57 +00:00
|
|
|
this.showCursor(selection.to);
|
2012-03-02 01:35:34 +00:00
|
|
|
var _this = this;
|
|
|
|
setTimeout( function() {
|
|
|
|
_this.poll.prevText = _this.poll.prevHash = _this.poll.prevOffset = _this.poll.node = null;
|
|
|
|
_this.startPolling();
|
|
|
|
}, 0 );
|
|
|
|
};
|
|
|
|
|
2012-02-07 19:13:19 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.extendClass( ve.ce.Surface, ve.EventEmitter );
|