2011-11-02 21:00:55 +00:00
|
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
|
* Creates an ve.es.Surface object.
|
2012-02-23 23:20:47 +00:00
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
|
* @class
|
|
|
|
|
* @constructor
|
|
|
|
|
* @param {jQuery} $container DOM Container to render surface into
|
2012-02-06 23:50:56 +00:00
|
|
|
|
* @param {ve.dm.Surface} model Surface model to view
|
2011-11-02 21:00:55 +00:00
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface = function( $container, model ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Inheritance
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.EventEmitter.call( this );
|
2011-11-10 01:28:01 +00:00
|
|
|
|
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// References for use in closures
|
|
|
|
|
var _this = this,
|
|
|
|
|
$document = $( document ),
|
|
|
|
|
$window = $( window );
|
|
|
|
|
|
|
|
|
|
// Properties
|
2011-11-02 21:00:55 +00:00
|
|
|
|
this.model = model;
|
2012-02-06 23:50:56 +00:00
|
|
|
|
this.currentSelection = new ve.Range();
|
|
|
|
|
this.documentView = new ve.es.DocumentNode( this.model.getDocument(), this );
|
2011-12-09 01:28:44 +00:00
|
|
|
|
this.contextView = null;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.$ = $container
|
|
|
|
|
.addClass( 'es-surfaceView' )
|
|
|
|
|
.append( this.documentView.$ );
|
2011-12-14 18:54:36 +00:00
|
|
|
|
this.$input = $( '<textarea class="es-surfaceView-textarea" autocapitalize="off" />' )
|
2011-11-29 21:32:41 +00:00
|
|
|
|
.appendTo( 'body' );
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.$cursor = $( '<div class="es-surfaceView-cursor"></div>' )
|
2011-11-28 21:58:46 +00:00
|
|
|
|
.appendTo( 'body' );
|
2011-12-01 00:16:50 +00:00
|
|
|
|
this.insertionAnnotations = [];
|
2011-11-23 22:21:46 +00:00
|
|
|
|
this.updateSelectionTimeout = undefined;
|
2011-11-30 23:33:07 +00:00
|
|
|
|
this.emitUpdateTimeout = undefined;
|
2011-12-02 23:01:21 +00:00
|
|
|
|
this.emitCursorTimeout = undefined;
|
2011-11-02 21:00:55 +00:00
|
|
|
|
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Interaction states
|
2011-11-14 19:51:04 +00:00
|
|
|
|
|
2011-11-23 00:36:46 +00:00
|
|
|
|
/*
|
|
|
|
|
* There are three different selection modes available for mouse. Selection of:
|
|
|
|
|
* 1 - chars
|
|
|
|
|
* 2 - words
|
|
|
|
|
* 3 - nodes (e.g. paragraph, listitem)
|
|
|
|
|
*
|
|
|
|
|
* In case of 2 and 3 selectedRange stores the range of original selection caused by double
|
|
|
|
|
* or triple mousedowns.
|
|
|
|
|
*/
|
2011-11-02 21:00:55 +00:00
|
|
|
|
this.mouse = {
|
2011-11-10 01:13:56 +00:00
|
|
|
|
selectingMode: null,
|
|
|
|
|
selectedRange: null
|
2011-11-02 21:00:55 +00:00
|
|
|
|
};
|
|
|
|
|
this.cursor = {
|
|
|
|
|
interval: null,
|
|
|
|
|
initialLeft: null,
|
|
|
|
|
initialBias: false
|
|
|
|
|
};
|
|
|
|
|
this.keyboard = {
|
|
|
|
|
selecting: false,
|
|
|
|
|
cursorAnchor: null,
|
|
|
|
|
keydownTimeout: null,
|
2012-02-23 23:20:47 +00:00
|
|
|
|
keys: { shift: false },
|
|
|
|
|
readInterval: null,
|
|
|
|
|
prevInput: '',
|
|
|
|
|
undoCount: 0,
|
|
|
|
|
chunkSize: 3
|
2011-11-02 21:00:55 +00:00
|
|
|
|
};
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.dimensions = {
|
|
|
|
|
width: this.$.width(),
|
|
|
|
|
height: $window.height(),
|
|
|
|
|
scrollTop: $window.scrollTop(),
|
|
|
|
|
// XXX: This is a dirty hack!
|
|
|
|
|
toolbarHeight: $( '#es-toolbar' ).height()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Events
|
2011-11-02 21:00:55 +00:00
|
|
|
|
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.model.on( 'select', function( selection ) {
|
|
|
|
|
// Keep a copy of the current selection on hand
|
|
|
|
|
_this.currentSelection = selection.clone();
|
|
|
|
|
// Respond to selection changes
|
2011-11-23 23:57:54 +00:00
|
|
|
|
_this.updateSelection();
|
2011-12-01 00:55:33 +00:00
|
|
|
|
if ( selection.getLength() ) {
|
2011-12-05 20:32:05 +00:00
|
|
|
|
_this.$input.val( _this.documentView.model.getContentText( selection ) ).select();
|
2011-12-01 00:55:33 +00:00
|
|
|
|
_this.clearInsertionAnnotations();
|
|
|
|
|
} else {
|
|
|
|
|
_this.loadInsertionAnnotations();
|
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
|
} );
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.model.getDocument().on( 'update', function() {
|
2011-11-23 23:42:41 +00:00
|
|
|
|
_this.emitUpdate( 25 );
|
2011-11-30 23:36:44 +00:00
|
|
|
|
} );
|
|
|
|
|
this.on( 'update', function() {
|
2011-11-23 23:42:41 +00:00
|
|
|
|
_this.updateSelection( 25 );
|
2011-11-23 00:36:46 +00:00
|
|
|
|
} );
|
|
|
|
|
this.$.mousedown( function(e) {
|
|
|
|
|
return _this.onMouseDown( e );
|
|
|
|
|
} );
|
2012-02-23 23:20:47 +00:00
|
|
|
|
|
2011-11-28 20:28:28 +00:00
|
|
|
|
this.$input.bind( {
|
2012-02-23 23:20:47 +00:00
|
|
|
|
'focus': function( e ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Make sure we aren't double-binding
|
2011-11-28 20:28:28 +00:00
|
|
|
|
$document.unbind( '.es-surfaceView' );
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Bind mouse and key events to the document to ensure we don't miss anything
|
2011-11-28 20:28:28 +00:00
|
|
|
|
$document.bind( {
|
2012-02-23 23:20:47 +00:00
|
|
|
|
'mousemove.es-surfaceView': function( e ) {
|
2011-11-18 05:44:24 +00:00
|
|
|
|
return _this.onMouseMove( e );
|
2011-11-02 21:00:55 +00:00
|
|
|
|
},
|
2012-02-23 23:20:47 +00:00
|
|
|
|
'mouseup.es-surfaceView': function( e ) {
|
2011-11-18 05:44:24 +00:00
|
|
|
|
return _this.onMouseUp( e );
|
2011-11-02 21:00:55 +00:00
|
|
|
|
},
|
|
|
|
|
'keydown.es-surfaceView': function( e ) {
|
2012-02-23 23:20:47 +00:00
|
|
|
|
return _this.onKeyDown( e );
|
2011-11-02 21:00:55 +00:00
|
|
|
|
},
|
|
|
|
|
'keyup.es-surfaceView': function( e ) {
|
2012-02-23 23:20:47 +00:00
|
|
|
|
return _this.onKeyUp( e );
|
2011-12-07 00:51:26 +00:00
|
|
|
|
},
|
|
|
|
|
'copy.es-surfaceView': function( e ) {
|
2012-02-23 23:20:47 +00:00
|
|
|
|
return _this.onCopy( e );
|
2011-12-07 00:51:26 +00:00
|
|
|
|
},
|
|
|
|
|
'cut.es-surfaceView': function( e ) {
|
2012-02-23 23:20:47 +00:00
|
|
|
|
return _this.onCut( e );
|
2011-12-07 00:51:26 +00:00
|
|
|
|
},
|
|
|
|
|
'paste.es-surfaceView': function( e ) {
|
2012-02-23 23:20:47 +00:00
|
|
|
|
return _this.onPaste( e );
|
2011-11-02 21:00:55 +00:00
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
} );
|
2012-02-23 23:20:47 +00:00
|
|
|
|
return _this.onFocus( e );
|
2011-11-02 21:00:55 +00:00
|
|
|
|
},
|
|
|
|
|
'blur': function( e ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Release our event handlers when not focused
|
2011-11-28 20:28:28 +00:00
|
|
|
|
$document.unbind( '.es-surfaceView' );
|
2011-11-18 05:44:24 +00:00
|
|
|
|
_this.hideCursor();
|
2012-02-23 23:20:47 +00:00
|
|
|
|
return _this.onBlur( e );
|
2011-12-03 01:30:44 +00:00
|
|
|
|
},
|
|
|
|
|
'paste': function() {
|
|
|
|
|
setTimeout( function() {
|
2011-12-10 00:06:37 +00:00
|
|
|
|
_this.model.breakpoint();
|
2011-12-03 01:30:44 +00:00
|
|
|
|
_this.insertFromInput();
|
2011-12-10 00:06:37 +00:00
|
|
|
|
_this.model.breakpoint();
|
2011-12-03 01:30:44 +00:00
|
|
|
|
}, 0 );
|
2011-11-02 21:00:55 +00:00
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
} );
|
2011-12-13 23:22:19 +00:00
|
|
|
|
$window.bind( {
|
|
|
|
|
'resize': function() {
|
|
|
|
|
// Re-render when resizing horizontally
|
|
|
|
|
// TODO: Instead of re-rendering on every single 'resize' event wait till user is done
|
|
|
|
|
// with resizing - can be implemented with setTimeout
|
|
|
|
|
_this.hideCursor();
|
|
|
|
|
_this.dimensions.height = $window.height();
|
|
|
|
|
// XXX: This is a dirty hack!
|
|
|
|
|
_this.dimensions.toolbarHeight = $( '#es-toolbar' ).height();
|
|
|
|
|
var width = _this.$.width();
|
|
|
|
|
if ( _this.dimensions.width !== width ) {
|
|
|
|
|
_this.dimensions.width = width;
|
|
|
|
|
_this.documentView.renderContent();
|
|
|
|
|
_this.emitUpdate( 25 );
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'scroll': function() {
|
|
|
|
|
_this.dimensions.scrollTop = $window.scrollTop();
|
|
|
|
|
if ( _this.contextView ) {
|
|
|
|
|
if ( _this.currentSelection.getLength() && !_this.mouse.selectingMode ) {
|
|
|
|
|
_this.contextView.set();
|
|
|
|
|
} else {
|
|
|
|
|
_this.contextView.clear();
|
|
|
|
|
}
|
2011-12-09 01:28:44 +00:00
|
|
|
|
}
|
2011-12-13 23:22:19 +00:00
|
|
|
|
},
|
|
|
|
|
'blur': function() {
|
|
|
|
|
_this.keyboard.keys.shift = false;
|
2011-12-01 22:50:55 +00:00
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
|
} );
|
2011-11-23 00:36:46 +00:00
|
|
|
|
|
|
|
|
|
// Configuration
|
|
|
|
|
this.mac = navigator.userAgent.match(/mac/i) ? true : false; // (yes it's evil, for keys only!)
|
2011-12-09 20:29:37 +00:00
|
|
|
|
this.ie8 = $.browser.msie && $.browser.version === "8.0";
|
2011-11-23 00:36:46 +00:00
|
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
|
this.$input.focus();
|
|
|
|
|
this.documentView.renderContent();
|
2011-11-02 21:00:55 +00:00
|
|
|
|
};
|
|
|
|
|
|
2011-11-14 23:47:07 +00:00
|
|
|
|
/* Methods */
|
2011-12-06 23:48:47 +00:00
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.attachContextView = function( contextView ) {
|
2011-12-09 01:28:44 +00:00
|
|
|
|
this.contextView = contextView;
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.getContextView = function() {
|
2011-12-09 01:28:44 +00:00
|
|
|
|
return this.contextView ;
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.annotate = function( method, annotation ) {
|
2011-12-06 23:48:47 +00:00
|
|
|
|
if ( method === 'toggle' ) {
|
|
|
|
|
var annotations = this.getAnnotations();
|
2012-02-06 23:50:56 +00:00
|
|
|
|
if ( ve.dm.DocumentNode.getIndexOfAnnotation( annotations.full, annotation ) !== -1 ) {
|
2011-12-06 23:48:47 +00:00
|
|
|
|
method = 'clear';
|
|
|
|
|
} else {
|
|
|
|
|
method = 'set';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( this.currentSelection.getLength() ) {
|
|
|
|
|
var tx = this.model.getDocument().prepareContentAnnotation(
|
|
|
|
|
this.currentSelection, method, annotation
|
|
|
|
|
);
|
|
|
|
|
this.model.transact( tx );
|
|
|
|
|
} else {
|
|
|
|
|
if ( method === 'set' ) {
|
|
|
|
|
this.addInsertionAnnotation( annotation );
|
|
|
|
|
} else if ( method === 'clear' ) {
|
|
|
|
|
this.removeInsertionAnnotation( annotation );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.getAnnotations = function() {
|
2011-12-06 23:48:47 +00:00
|
|
|
|
return this.currentSelection.getLength() ?
|
|
|
|
|
this.model.getDocument().getAnnotationsFromRange( this.currentSelection ) :
|
|
|
|
|
{
|
|
|
|
|
'full': this.insertionAnnotations,
|
|
|
|
|
'partial': [],
|
|
|
|
|
'all': this.insertionAnnotations
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.emitCursor = function() {
|
2011-12-02 23:01:21 +00:00
|
|
|
|
if ( this.emitCursorTimeout ) {
|
|
|
|
|
clearTimeout( this.emitCursorTimeout );
|
|
|
|
|
}
|
|
|
|
|
var _this = this;
|
|
|
|
|
this.emitCursorTimeout = setTimeout( function() {
|
2011-12-06 23:48:47 +00:00
|
|
|
|
var annotations = _this.getAnnotations(),
|
2011-12-02 23:01:21 +00:00
|
|
|
|
nodes = [],
|
|
|
|
|
model = _this.documentView.model;
|
2011-12-06 23:48:47 +00:00
|
|
|
|
if ( _this.currentSelection.from === _this.currentSelection.to ) {
|
2011-12-02 23:01:21 +00:00
|
|
|
|
nodes.push( model.getNodeFromOffset( _this.currentSelection.from ) );
|
|
|
|
|
} else {
|
|
|
|
|
var startNode = model.getNodeFromOffset( _this.currentSelection.start ),
|
|
|
|
|
endNode = model.getNodeFromOffset( _this.currentSelection.end );
|
|
|
|
|
if ( startNode === endNode ) {
|
|
|
|
|
nodes.push( startNode );
|
|
|
|
|
} else {
|
|
|
|
|
model.traverseLeafNodes( function( node ) {
|
|
|
|
|
nodes.push( node );
|
|
|
|
|
if( node === endNode ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-02-23 23:20:47 +00:00
|
|
|
|
}, startNode );
|
2011-12-02 23:01:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_this.emit( 'cursor', annotations, nodes );
|
|
|
|
|
}, 50 );
|
|
|
|
|
};
|
2011-11-14 23:47:07 +00:00
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.getInsertionAnnotations = function() {
|
2011-12-01 00:16:50 +00:00
|
|
|
|
return this.insertionAnnotations;
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.addInsertionAnnotation = function( annotation ) {
|
2011-12-01 00:16:50 +00:00
|
|
|
|
this.insertionAnnotations.push( annotation );
|
2011-12-02 23:01:21 +00:00
|
|
|
|
this.emitCursor();
|
2011-12-01 00:16:50 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.loadInsertionAnnotations = function( annotation ) {
|
2011-12-01 00:21:59 +00:00
|
|
|
|
this.insertionAnnotations =
|
2011-12-01 00:58:51 +00:00
|
|
|
|
this.model.getDocument().getAnnotationsFromOffset( this.currentSelection.to - 1 );
|
2011-12-01 00:21:59 +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--;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-02 23:01:21 +00:00
|
|
|
|
this.emitCursor();
|
2011-12-01 00:21:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.removeInsertionAnnotation = function( annotation ) {
|
|
|
|
|
var index = ve.dm.DocumentNode.getIndexOfAnnotation( this.insertionAnnotations, annotation );
|
2011-12-01 00:16:50 +00:00
|
|
|
|
if ( index !== -1 ) {
|
|
|
|
|
this.insertionAnnotations.splice( index, 1 );
|
|
|
|
|
}
|
2011-12-02 23:01:21 +00:00
|
|
|
|
this.emitCursor();
|
2011-12-01 00:16:50 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.clearInsertionAnnotations = function() {
|
2011-12-01 00:16:50 +00:00
|
|
|
|
this.insertionAnnotations = [];
|
2011-12-02 23:01:21 +00:00
|
|
|
|
this.emitCursor();
|
2011-12-01 00:16:50 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.getModel = function() {
|
2011-11-29 23:29:02 +00:00
|
|
|
|
return this.model;
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.updateSelection = function( delay ) {
|
2011-12-01 00:16:50 +00:00
|
|
|
|
var _this = this;
|
|
|
|
|
function update() {
|
2011-12-01 23:00:35 +00:00
|
|
|
|
if ( _this.currentSelection.getLength() ) {
|
2011-12-01 00:21:59 +00:00
|
|
|
|
_this.clearInsertionAnnotations();
|
2011-12-01 00:16:50 +00:00
|
|
|
|
_this.hideCursor();
|
|
|
|
|
_this.documentView.drawSelection( _this.currentSelection );
|
|
|
|
|
} else {
|
|
|
|
|
_this.showCursor();
|
|
|
|
|
_this.documentView.clearSelection( _this.currentSelection );
|
2011-12-01 23:00:35 +00:00
|
|
|
|
}
|
2011-12-09 01:28:44 +00:00
|
|
|
|
if ( _this.contextView ) {
|
|
|
|
|
if ( _this.currentSelection.getLength() && !_this.mouse.selectingMode ) {
|
|
|
|
|
_this.contextView.set();
|
|
|
|
|
} else {
|
|
|
|
|
_this.contextView.clear();
|
|
|
|
|
}
|
2011-12-01 00:16:50 +00:00
|
|
|
|
}
|
|
|
|
|
_this.updateSelectionTimeout = undefined;
|
|
|
|
|
}
|
2011-11-23 23:57:54 +00:00
|
|
|
|
if ( delay ) {
|
|
|
|
|
if ( this.updateSelectionTimeout !== undefined ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-12-01 00:16:50 +00:00
|
|
|
|
this.updateSelectionTimeout = setTimeout( update, delay );
|
2011-11-23 23:57:54 +00:00
|
|
|
|
} else {
|
2011-12-01 00:16:50 +00:00
|
|
|
|
update();
|
2011-11-23 23:57:54 +00:00
|
|
|
|
}
|
2011-11-23 23:42:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.emitUpdate = function( delay ) {
|
2011-11-23 23:57:54 +00:00
|
|
|
|
if ( delay ) {
|
|
|
|
|
if ( this.emitUpdateTimeout !== undefined ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var _this = this;
|
|
|
|
|
this.emitUpdateTimeout = setTimeout( function() {
|
2012-02-23 23:20:47 +00:00
|
|
|
|
_this.emit( 'update' );
|
2011-11-23 23:57:54 +00:00
|
|
|
|
_this.emitUpdateTimeout = undefined;
|
2011-11-24 00:01:11 +00:00
|
|
|
|
}, delay );
|
2011-11-23 23:57:54 +00:00
|
|
|
|
} else {
|
2012-02-23 23:20:47 +00:00
|
|
|
|
this.emit( 'update' );
|
2011-11-23 23:42:41 +00:00
|
|
|
|
}
|
2011-11-23 22:21:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.onMouseDown = function( e ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Only for left mouse button
|
2011-12-05 22:01:06 +00:00
|
|
|
|
if ( e.which === 1 ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
var selection = this.currentSelection.clone(),
|
|
|
|
|
offset = this.documentView.getOffsetFromEvent( e );
|
|
|
|
|
// Single click
|
2011-12-09 20:29:37 +00:00
|
|
|
|
if ( this.ie8 || e.originalEvent.detail === 1 ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
|
// @see {ve.es.Surface.prototype.onMouseMove}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.mouse.selectingMode = 1;
|
|
|
|
|
|
|
|
|
|
if ( this.keyboard.keys.shift && offset !== selection.from ) {
|
|
|
|
|
// Extend current or create new selection
|
|
|
|
|
selection.to = offset;
|
2011-11-10 23:30:29 +00:00
|
|
|
|
} else {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.from = selection.to = offset;
|
2011-11-15 01:30:39 +00:00
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
var position = ve.Position.newFromEventPagePosition( e ),
|
2011-11-10 23:30:29 +00:00
|
|
|
|
nodeView = this.documentView.getNodeFromOffset( offset, false );
|
|
|
|
|
this.cursor.initialBias = position.left > nodeView.contentView.$.offset().left;
|
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
}
|
|
|
|
|
// Double click
|
|
|
|
|
else if ( e.originalEvent.detail === 2 ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
|
// @see {ve.es.Surface.prototype.onMouseMove}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.mouse.selectingMode = 2;
|
2011-11-15 01:30:39 +00:00
|
|
|
|
|
2011-11-22 22:59:05 +00:00
|
|
|
|
var wordRange = this.model.getDocument().getWordBoundaries( offset );
|
2011-11-10 23:30:29 +00:00
|
|
|
|
if( wordRange ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection = wordRange;
|
|
|
|
|
this.mouse.selectedRange = selection.clone();
|
2011-11-10 23:30:29 +00:00
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
}
|
|
|
|
|
// Triple click
|
|
|
|
|
else if ( e.originalEvent.detail >= 3 ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
|
// @see {ve.es.Surface.prototype.onMouseMove}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.mouse.selectingMode = 3;
|
2011-11-18 08:10:56 +00:00
|
|
|
|
|
|
|
|
|
var node = this.documentView.getNodeFromOffset( offset ),
|
|
|
|
|
nodeOffset = this.documentView.getOffsetFromNode( node, false );
|
|
|
|
|
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.from = this.model.getDocument().getRelativeContentOffset( nodeOffset, 1 );
|
|
|
|
|
selection.to = this.model.getDocument().getRelativeContentOffset(
|
|
|
|
|
nodeOffset + node.getElementLength(), -1
|
2011-11-21 21:50:14 +00:00
|
|
|
|
);
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.mouse.selectedRange = selection.clone();
|
2011-11-10 23:30:29 +00:00
|
|
|
|
}
|
2012-02-24 00:32:44 +00:00
|
|
|
|
this.resetText();
|
2011-11-03 20:27:57 +00:00
|
|
|
|
}
|
2011-12-09 20:29:37 +00:00
|
|
|
|
|
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
|
|
function select() {
|
|
|
|
|
if ( e.which === 1 ) {
|
|
|
|
|
// Reset the initial left position
|
|
|
|
|
_this.cursor.initialLeft = null;
|
|
|
|
|
// Apply new selection
|
|
|
|
|
_this.model.select( selection, true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the inut isn't already focused, focus it and select it's contents
|
|
|
|
|
if ( !_this.$input.is( ':focus' ) ) {
|
|
|
|
|
_this.$input.focus().select();
|
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
|
}
|
2011-12-09 20:29:37 +00:00
|
|
|
|
|
|
|
|
|
if ( this.ie8 ) {
|
|
|
|
|
setTimeout( select, 0 );
|
|
|
|
|
} else {
|
|
|
|
|
select();
|
|
|
|
|
}
|
2012-02-24 00:04:21 +00:00
|
|
|
|
//end ime
|
|
|
|
|
this.$input.blur();
|
|
|
|
|
this.$input.focus();
|
|
|
|
|
this.showCursor();
|
2011-11-02 21:00:55 +00:00
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.onMouseMove = function( e ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Only with the left mouse button while in selecting mode
|
2011-12-05 22:01:06 +00:00
|
|
|
|
if ( e.which === 1 && this.mouse.selectingMode ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
var selection = this.currentSelection.clone(),
|
|
|
|
|
offset = this.documentView.getOffsetFromEvent( e );
|
|
|
|
|
|
|
|
|
|
// Character selection
|
|
|
|
|
if ( this.mouse.selectingMode === 1 ) {
|
|
|
|
|
selection.to = offset;
|
|
|
|
|
}
|
|
|
|
|
// Word selection
|
|
|
|
|
else if ( this.mouse.selectingMode === 2 ) {
|
2011-11-22 22:59:05 +00:00
|
|
|
|
var wordRange = this.model.getDocument().getWordBoundaries( offset );
|
2011-11-10 23:30:29 +00:00
|
|
|
|
if ( wordRange ) {
|
|
|
|
|
if ( wordRange.to <= this.mouse.selectedRange.from ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.from = wordRange.from;
|
|
|
|
|
selection.to = this.mouse.selectedRange.to;
|
2011-11-10 23:30:29 +00:00
|
|
|
|
} else {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.from = this.mouse.selectedRange.from;
|
|
|
|
|
selection.to = wordRange.to;
|
2011-11-10 23:30:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
}
|
|
|
|
|
// Node selection
|
|
|
|
|
else if ( this.mouse.selectingMode === 3 ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
|
// @see {ve.es.Surface.prototype.onMouseMove}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.mouse.selectingMode = 3;
|
2011-11-18 08:10:56 +00:00
|
|
|
|
|
2011-11-15 01:30:39 +00:00
|
|
|
|
var nodeRange = this.documentView.getRangeFromNode(
|
|
|
|
|
this.documentView.getNodeFromOffset( offset )
|
|
|
|
|
);
|
2011-11-10 23:30:29 +00:00
|
|
|
|
if ( nodeRange.to <= this.mouse.selectedRange.from ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.from = this.model.getDocument().getRelativeContentOffset(
|
|
|
|
|
nodeRange.from, 1
|
2011-11-21 21:50:14 +00:00
|
|
|
|
);
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.to = this.mouse.selectedRange.to;
|
2011-11-10 01:13:56 +00:00
|
|
|
|
} else {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.from = this.mouse.selectedRange.from;
|
|
|
|
|
selection.to = this.model.getDocument().getRelativeContentOffset(
|
|
|
|
|
nodeRange.to, -1
|
2011-11-21 21:50:14 +00:00
|
|
|
|
);
|
2012-02-23 23:20:47 +00:00
|
|
|
|
}
|
2011-11-10 23:30:29 +00:00
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Apply new selection
|
2011-12-06 01:52:34 +00:00
|
|
|
|
this.model.select( selection, true );
|
2011-11-03 20:27:57 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.onMouseUp = function( e ) {
|
2012-02-23 23:20:47 +00:00
|
|
|
|
if ( e.which === 1 ) { // left mouse button
|
2011-11-10 01:13:56 +00:00
|
|
|
|
this.mouse.selectingMode = this.mouse.selectedRange = null;
|
2011-12-06 01:52:38 +00:00
|
|
|
|
this.model.select( this.currentSelection, true );
|
2011-12-09 01:28:44 +00:00
|
|
|
|
if ( this.contextView ) {
|
|
|
|
|
// We have to manually call this because the selection will not have changed between the
|
|
|
|
|
// most recent mousemove and this mouseup
|
|
|
|
|
this.contextView.set();
|
|
|
|
|
}
|
2011-11-03 20:27:57 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.onCopy = function( e ) {
|
2011-12-07 00:51:26 +00:00
|
|
|
|
// TODO: Keep a data copy around
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.onCut = function( e ) {
|
2011-12-07 00:51:26 +00:00
|
|
|
|
var _this = this;
|
|
|
|
|
setTimeout( function() {
|
|
|
|
|
_this.handleDelete();
|
|
|
|
|
}, 10 );
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.onPaste = function( e ) {
|
2011-12-07 00:51:26 +00:00
|
|
|
|
// TODO: Check if the data copy is the same as what got pasted, and use that instead if so
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-23 23:20:47 +00:00
|
|
|
|
ve.es.Surface.prototype.onFocus = function( e ) {
|
|
|
|
|
var _this = this;
|
|
|
|
|
this.keyboard.prevInput = _this.$input.val();
|
|
|
|
|
//start polling
|
|
|
|
|
this.keyboard.readInterval = setInterval( function(){
|
|
|
|
|
_this.readInput( _this );
|
|
|
|
|
}, 10 );
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ve.es.Surface.prototype.onBlur = function( e ) {
|
|
|
|
|
//stop polling
|
|
|
|
|
if ( this.keyboard.readInterval ) {
|
2012-02-24 17:41:22 +00:00
|
|
|
|
clearInterval( this.keyboard.readInterval );
|
2012-02-23 23:20:47 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ve.es.Surface.prototype.readInput = function( _this ) {
|
|
|
|
|
var selection = _this.currentSelection.clone(),
|
|
|
|
|
text = _this.$input.val();
|
|
|
|
|
|
|
|
|
|
//Do nothing if the text is the same.
|
|
|
|
|
if ( text == _this.keyboard.prevInput ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//transfer text
|
|
|
|
|
_this.keyboard.prevInput = text;
|
|
|
|
|
_this.handleInsert();
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-28 00:35:17 +00:00
|
|
|
|
/* TODO: need to complete the unicode expression for all RTL characters
|
|
|
|
|
Currently have Hebrew & and all Arabic except numbers
|
|
|
|
|
*/
|
|
|
|
|
ve.es.Surface.prototype.isTextRTL = function( text ) {
|
|
|
|
|
return /[\u0590–\u05FF\u0600-\u06FF\u0750—\u077F\u08A0—\u08FF\uFB50—\uFDFF\uFE70—\uFEFF]/.test( text );
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-23 23:20:47 +00:00
|
|
|
|
ve.es.Surface.prototype.resetText = function( e ) {
|
|
|
|
|
this.$input.val( '' );
|
|
|
|
|
this.keyboard.undoCount = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.onKeyDown = function( e ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
|
switch ( e.keyCode ) {
|
2011-12-07 01:10:25 +00:00
|
|
|
|
// Tab
|
|
|
|
|
case 9:
|
|
|
|
|
if ( !e.metaKey && !e.ctrlKey && !e.altKey ) {
|
|
|
|
|
this.$input.val( '\t' );
|
|
|
|
|
this.handleInsert();
|
2012-02-23 23:20:47 +00:00
|
|
|
|
this.resetText();
|
2011-12-07 01:10:25 +00:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Shift
|
|
|
|
|
case 16:
|
2011-11-02 21:00:55 +00:00
|
|
|
|
this.keyboard.keys.shift = true;
|
2011-11-03 22:42:20 +00:00
|
|
|
|
this.keyboard.selecting = true;
|
2011-11-02 21:00:55 +00:00
|
|
|
|
break;
|
2011-12-03 01:30:44 +00:00
|
|
|
|
// Ctrl
|
|
|
|
|
case 17:
|
|
|
|
|
break;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Home
|
|
|
|
|
case 36:
|
2011-11-15 01:30:39 +00:00
|
|
|
|
this.moveCursor( 'left', 'line' );
|
2011-11-02 21:00:55 +00:00
|
|
|
|
break;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// End
|
|
|
|
|
case 35:
|
2011-11-15 01:30:39 +00:00
|
|
|
|
this.moveCursor( 'right', 'line' );
|
2011-11-02 21:00:55 +00:00
|
|
|
|
break;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Left arrow
|
|
|
|
|
case 37:
|
2011-11-16 21:59:22 +00:00
|
|
|
|
if ( !this.mac ) {
|
|
|
|
|
if ( e.ctrlKey ) {
|
|
|
|
|
this.moveCursor( 'left', 'word' );
|
|
|
|
|
} else {
|
|
|
|
|
this.moveCursor( 'left', 'char' );
|
|
|
|
|
}
|
2011-11-15 01:30:39 +00:00
|
|
|
|
} else {
|
2011-11-16 21:59:22 +00:00
|
|
|
|
if ( e.metaKey || e.ctrlKey ) {
|
|
|
|
|
this.moveCursor( 'left', 'line' );
|
|
|
|
|
} else if ( e.altKey ) {
|
|
|
|
|
this.moveCursor( 'left', 'word' );
|
|
|
|
|
} else {
|
|
|
|
|
this.moveCursor( 'left', 'char' );
|
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Up arrow
|
|
|
|
|
case 38:
|
2011-11-16 21:59:22 +00:00
|
|
|
|
if ( !this.mac ) {
|
|
|
|
|
if ( e.ctrlKey ) {
|
|
|
|
|
this.moveCursor( 'up', 'unit' );
|
|
|
|
|
} else {
|
|
|
|
|
this.moveCursor( 'up', 'char' );
|
|
|
|
|
}
|
2011-11-15 01:30:39 +00:00
|
|
|
|
} else {
|
2011-11-16 21:59:22 +00:00
|
|
|
|
if ( e.altKey ) {
|
|
|
|
|
this.moveCursor( 'up', 'unit' );
|
|
|
|
|
} else {
|
|
|
|
|
this.moveCursor( 'up', 'char' );
|
|
|
|
|
}
|
2011-11-15 01:30:39 +00:00
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
|
break;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Right arrow
|
|
|
|
|
case 39:
|
2011-11-16 21:59:22 +00:00
|
|
|
|
if ( !this.mac ) {
|
|
|
|
|
if ( e.ctrlKey ) {
|
|
|
|
|
this.moveCursor( 'right', 'word' );
|
|
|
|
|
} else {
|
|
|
|
|
this.moveCursor( 'right', 'char' );
|
|
|
|
|
}
|
2011-11-15 01:30:39 +00:00
|
|
|
|
} else {
|
2011-11-16 21:59:22 +00:00
|
|
|
|
if ( e.metaKey || e.ctrlKey ) {
|
|
|
|
|
this.moveCursor( 'right', 'line' );
|
|
|
|
|
} else if ( e.altKey ) {
|
|
|
|
|
this.moveCursor( 'right', 'word' );
|
|
|
|
|
} else {
|
|
|
|
|
this.moveCursor( 'right', 'char' );
|
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Down arrow
|
|
|
|
|
case 40:
|
2011-11-16 21:59:22 +00:00
|
|
|
|
if ( !this.mac ) {
|
|
|
|
|
if ( e.ctrlKey ) {
|
|
|
|
|
this.moveCursor( 'down', 'unit' );
|
|
|
|
|
} else {
|
|
|
|
|
this.moveCursor( 'down', 'char' );
|
|
|
|
|
}
|
2011-11-15 01:30:39 +00:00
|
|
|
|
} else {
|
2011-11-16 21:59:22 +00:00
|
|
|
|
if ( e.altKey ) {
|
|
|
|
|
this.moveCursor( 'down', 'unit' );
|
|
|
|
|
} else {
|
|
|
|
|
this.moveCursor( 'down', 'char' );
|
|
|
|
|
}
|
2011-11-15 01:30:39 +00:00
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
|
break;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Backspace
|
|
|
|
|
case 8:
|
2011-11-21 21:50:14 +00:00
|
|
|
|
this.handleDelete( true );
|
2011-11-02 21:00:55 +00:00
|
|
|
|
break;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Delete
|
|
|
|
|
case 46:
|
2011-11-21 21:50:14 +00:00
|
|
|
|
this.handleDelete();
|
2011-11-16 23:16:02 +00:00
|
|
|
|
break;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
// Enter
|
|
|
|
|
case 13:
|
2011-12-07 01:10:25 +00:00
|
|
|
|
if ( this.keyboard.keys.shift ) {
|
|
|
|
|
this.$input.val( '\n' );
|
|
|
|
|
this.handleInsert();
|
2012-02-23 23:20:47 +00:00
|
|
|
|
this.resetText();
|
2011-12-07 01:10:25 +00:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-11-21 22:55:48 +00:00
|
|
|
|
this.handleEnter();
|
2012-02-23 23:20:47 +00:00
|
|
|
|
this.resetText();
|
2011-11-18 06:17:14 +00:00
|
|
|
|
e.preventDefault();
|
2011-11-02 21:00:55 +00:00
|
|
|
|
break;
|
2011-12-06 23:48:47 +00:00
|
|
|
|
// Insert content (maybe)
|
|
|
|
|
default:
|
|
|
|
|
// Control/command + character combos
|
2011-11-21 23:54:57 +00:00
|
|
|
|
if ( e.metaKey || e.ctrlKey ) {
|
2011-12-06 23:48:47 +00:00
|
|
|
|
switch ( e.keyCode ) {
|
2011-12-10 00:02:47 +00:00
|
|
|
|
// y (redo)
|
|
|
|
|
case 89:
|
|
|
|
|
this.model.redo();
|
|
|
|
|
return false;
|
2011-12-06 23:48:47 +00:00
|
|
|
|
// z (undo/redo)
|
|
|
|
|
case 90:
|
|
|
|
|
if ( this.keyboard.keys.shift ) {
|
2011-12-10 00:02:47 +00:00
|
|
|
|
this.model.redo();
|
2011-12-06 23:48:47 +00:00
|
|
|
|
} else {
|
2011-12-10 00:02:47 +00:00
|
|
|
|
this.model.undo();
|
2012-02-23 23:20:47 +00:00
|
|
|
|
this.resetText();
|
2011-12-06 23:48:47 +00:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
// a (select all)
|
2012-03-05 17:57:54 +00:00
|
|
|
|
case 91:
|
|
|
|
|
return false;
|
2011-12-06 23:48:47 +00:00
|
|
|
|
case 65:
|
2012-02-06 23:50:56 +00:00
|
|
|
|
this.model.select( new ve.Range(
|
2011-12-06 23:48:47 +00:00
|
|
|
|
this.model.getDocument().getRelativeContentOffset( 0, 1 ),
|
|
|
|
|
this.model.getDocument().getRelativeContentOffset(
|
|
|
|
|
this.model.getDocument().getContentLength(), -1
|
|
|
|
|
)
|
|
|
|
|
), true );
|
|
|
|
|
return false;
|
|
|
|
|
// b (bold)
|
|
|
|
|
case 66:
|
|
|
|
|
this.annotate( 'toggle', {'type': 'textStyle/bold' } );
|
|
|
|
|
return false;
|
|
|
|
|
// i (italic)
|
|
|
|
|
case 73:
|
|
|
|
|
this.annotate( 'toggle', {'type': 'textStyle/italic' } );
|
|
|
|
|
return false;
|
2011-12-09 21:16:42 +00:00
|
|
|
|
// k (hyperlink)
|
|
|
|
|
case 75:
|
2011-12-09 23:23:43 +00:00
|
|
|
|
if ( this.currentSelection.getLength() ) {
|
|
|
|
|
this.contextView.openInspector( 'link' );
|
2011-12-13 23:12:27 +00:00
|
|
|
|
} else {
|
|
|
|
|
var range = this.model.getDocument().getAnnotationBoundaries(
|
|
|
|
|
this.currentSelection.from, { 'type': 'link/internal' }, true
|
|
|
|
|
);
|
|
|
|
|
if ( range ) {
|
|
|
|
|
this.model.select( range );
|
|
|
|
|
this.contextView.openInspector( 'link' );
|
|
|
|
|
}
|
2011-12-09 23:23:43 +00:00
|
|
|
|
}
|
2011-12-09 21:16:42 +00:00
|
|
|
|
return false;
|
2011-11-22 00:26:50 +00:00
|
|
|
|
}
|
2011-11-21 23:25:43 +00:00
|
|
|
|
}
|
2012-02-23 23:20:47 +00:00
|
|
|
|
// Ignore chrome 229 IME event.
|
|
|
|
|
if (e.which !== 229) {
|
|
|
|
|
// Chunked insert
|
2012-02-24 17:41:22 +00:00
|
|
|
|
this.handleInsert( this.keyboard.chunkSize );
|
2012-02-23 23:20:47 +00:00
|
|
|
|
}
|
2011-11-23 00:59:57 +00:00
|
|
|
|
break;
|
2011-11-02 21:00:55 +00:00
|
|
|
|
}
|
2011-11-05 00:49:48 +00:00
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.onKeyUp = function( e ) {
|
2011-12-06 23:48:47 +00:00
|
|
|
|
if ( e.keyCode === 16 ) {
|
|
|
|
|
this.keyboard.keys.shift = false;
|
|
|
|
|
if ( this.keyboard.selecting ) {
|
|
|
|
|
this.keyboard.selecting = false;
|
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-23 23:20:47 +00:00
|
|
|
|
ve.es.Surface.prototype.handleInsert = function( chunkSize ) {
|
2011-12-07 00:51:26 +00:00
|
|
|
|
var _this = this;
|
|
|
|
|
if ( _this.keyboard.keydownTimeout ) {
|
|
|
|
|
clearTimeout( _this.keyboard.keydownTimeout );
|
|
|
|
|
}
|
|
|
|
|
_this.keyboard.keydownTimeout = setTimeout( function () {
|
2012-02-23 23:20:47 +00:00
|
|
|
|
_this.insertFromInput( chunkSize );
|
2011-12-07 00:51:26 +00:00
|
|
|
|
}, 10 );
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.handleDelete = function( backspace, isPartial ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
var selection = this.currentSelection.clone(),
|
|
|
|
|
sourceOffset,
|
2011-11-21 22:43:56 +00:00
|
|
|
|
targetOffset,
|
|
|
|
|
sourceSplitableNode,
|
|
|
|
|
targetSplitableNode,
|
2011-12-08 03:14:10 +00:00
|
|
|
|
tx;
|
2012-02-23 23:20:47 +00:00
|
|
|
|
this.resetText();
|
2011-11-23 00:36:46 +00:00
|
|
|
|
if ( selection.from === selection.to ) {
|
2011-11-21 21:50:14 +00:00
|
|
|
|
if ( backspace ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
sourceOffset = selection.to;
|
2011-11-22 22:59:05 +00:00
|
|
|
|
targetOffset = this.model.getDocument().getRelativeContentOffset(
|
2011-11-21 22:43:56 +00:00
|
|
|
|
sourceOffset,
|
|
|
|
|
-1
|
|
|
|
|
);
|
2011-11-21 21:50:14 +00:00
|
|
|
|
} else {
|
2011-11-22 22:59:05 +00:00
|
|
|
|
sourceOffset = this.model.getDocument().getRelativeContentOffset(
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.to,
|
2011-11-21 22:43:56 +00:00
|
|
|
|
1
|
|
|
|
|
);
|
2011-11-23 00:36:46 +00:00
|
|
|
|
targetOffset = selection.to;
|
2011-11-21 21:50:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sourceNode = this.documentView.getNodeFromOffset( sourceOffset, false ),
|
|
|
|
|
targetNode = this.documentView.getNodeFromOffset( targetOffset, false );
|
|
|
|
|
|
|
|
|
|
if ( sourceNode.model.getElementType() === targetNode.model.getElementType() ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
|
sourceSplitableNode = ve.es.Node.getSplitableNode( sourceNode );
|
|
|
|
|
targetSplitableNode = ve.es.Node.getSplitableNode( targetNode );
|
2011-11-21 21:50:14 +00:00
|
|
|
|
}
|
2011-11-22 22:59:05 +00:00
|
|
|
|
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.from = selection.to = targetOffset;
|
2011-12-06 01:52:38 +00:00
|
|
|
|
this.model.select( selection );
|
2011-11-22 22:59:05 +00:00
|
|
|
|
|
2011-11-21 21:50:14 +00:00
|
|
|
|
if ( sourceNode === targetNode ||
|
|
|
|
|
( typeof sourceSplitableNode !== 'undefined' &&
|
|
|
|
|
sourceSplitableNode.getParent() === targetSplitableNode.getParent() ) ) {
|
2011-12-08 03:14:10 +00:00
|
|
|
|
tx = this.model.getDocument().prepareRemoval(
|
2012-02-06 23:50:56 +00:00
|
|
|
|
new ve.Range( targetOffset, sourceOffset )
|
2011-12-08 03:14:10 +00:00
|
|
|
|
);
|
|
|
|
|
this.model.transact( tx, isPartial );
|
2011-11-21 21:50:14 +00:00
|
|
|
|
} else {
|
2011-12-08 03:14:10 +00:00
|
|
|
|
tx = this.model.getDocument().prepareInsertion(
|
2011-12-05 19:41:04 +00:00
|
|
|
|
targetOffset, sourceNode.model.getContentData()
|
2011-12-08 03:14:10 +00:00
|
|
|
|
);
|
|
|
|
|
this.model.transact( tx, isPartial );
|
2011-11-21 21:50:14 +00:00
|
|
|
|
|
|
|
|
|
var nodeToDelete = sourceNode;
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.Node.traverseUpstream( nodeToDelete, function( node ) {
|
2011-11-21 21:50:14 +00:00
|
|
|
|
if ( node.getParent().children.length === 1 ) {
|
2011-11-22 20:11:23 +00:00
|
|
|
|
nodeToDelete = node.getParent();
|
2011-12-01 19:08:32 +00:00
|
|
|
|
return true;
|
2011-11-21 21:50:14 +00:00
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} );
|
2012-02-06 23:50:56 +00:00
|
|
|
|
var range = new ve.Range();
|
2011-11-21 21:50:14 +00:00
|
|
|
|
range.from = this.documentView.getOffsetFromNode( nodeToDelete, false );
|
|
|
|
|
range.to = range.from + nodeToDelete.getElementLength();
|
2011-12-08 03:14:10 +00:00
|
|
|
|
tx = this.model.getDocument().prepareRemoval( range );
|
|
|
|
|
this.model.transact( tx, isPartial );
|
2011-11-21 21:50:14 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// selection removal
|
2011-11-23 00:36:46 +00:00
|
|
|
|
tx = this.model.getDocument().prepareRemoval( selection );
|
2011-12-08 03:14:10 +00:00
|
|
|
|
this.model.transact( tx, isPartial );
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.from = selection.to = selection.start;
|
2011-12-06 01:52:38 +00:00
|
|
|
|
this.model.select( selection );
|
2011-11-21 21:50:14 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.handleEnter = function() {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
var selection = this.currentSelection.clone(),
|
2011-12-08 03:14:10 +00:00
|
|
|
|
tx;
|
2011-11-23 00:36:46 +00:00
|
|
|
|
if ( selection.from !== selection.to ) {
|
2011-12-08 03:14:10 +00:00
|
|
|
|
this.handleDelete( false, true );
|
2011-11-21 22:55:48 +00:00
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
var node = this.documentView.getNodeFromOffset( selection.to, false ),
|
2011-11-21 22:55:48 +00:00
|
|
|
|
nodeOffset = this.documentView.getOffsetFromNode( node, false );
|
|
|
|
|
|
|
|
|
|
if (
|
2011-11-23 00:36:46 +00:00
|
|
|
|
nodeOffset + node.getContentLength() + 1 === selection.to &&
|
2012-02-06 23:50:56 +00:00
|
|
|
|
node === ve.es.Node.getSplitableNode( node )
|
2011-11-21 22:55:48 +00:00
|
|
|
|
) {
|
2011-12-08 03:14:10 +00:00
|
|
|
|
tx = this.documentView.model.prepareInsertion(
|
2011-11-21 22:55:48 +00:00
|
|
|
|
nodeOffset + node.getElementLength(),
|
|
|
|
|
[ { 'type': 'paragraph' }, { 'type': '/paragraph' } ]
|
2011-12-08 03:14:10 +00:00
|
|
|
|
);
|
|
|
|
|
this.model.transact( tx );
|
2012-02-23 23:20:47 +00:00
|
|
|
|
selection.from = selection.to = nodeOffset + node.getElementLength() + 1;
|
2011-11-21 22:55:48 +00:00
|
|
|
|
} else {
|
|
|
|
|
var stack = [],
|
|
|
|
|
splitable = false;
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.Node.traverseUpstream( node, function( node ) {
|
2011-11-21 22:55:48 +00:00
|
|
|
|
var elementType = node.model.getElementType();
|
|
|
|
|
if (
|
|
|
|
|
splitable === true &&
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.DocumentNode.splitRules[ elementType ].children === true
|
2011-11-21 22:55:48 +00:00
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
stack.splice(
|
|
|
|
|
stack.length / 2,
|
|
|
|
|
0,
|
|
|
|
|
{ 'type': '/' + elementType },
|
|
|
|
|
{
|
|
|
|
|
'type': elementType,
|
2012-02-06 23:50:56 +00:00
|
|
|
|
'attributes': ve.copyObject( node.model.element.attributes )
|
2011-11-21 22:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
);
|
2012-02-06 23:50:56 +00:00
|
|
|
|
splitable = ve.es.DocumentNode.splitRules[ elementType ].self;
|
2011-12-01 19:08:32 +00:00
|
|
|
|
return true;
|
2011-11-21 22:55:48 +00:00
|
|
|
|
} );
|
2011-12-08 03:14:10 +00:00
|
|
|
|
tx = this.documentView.model.prepareInsertion( selection.to, stack );
|
|
|
|
|
this.model.transact( tx );
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.from = selection.to =
|
|
|
|
|
this.model.getDocument().getRelativeContentOffset( selection.to, 1 );
|
2011-11-21 22:55:48 +00:00
|
|
|
|
}
|
2011-12-06 01:52:38 +00:00
|
|
|
|
this.model.select( selection );
|
2011-11-21 22:55:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-02-23 23:20:47 +00:00
|
|
|
|
ve.es.Surface.prototype.insertFromInput = function( chunkSize ) {
|
|
|
|
|
var selection = this.currentSelection.clone(),
|
|
|
|
|
val = this.$input.val(),
|
|
|
|
|
chunkThis = '',
|
|
|
|
|
chunked = [],
|
|
|
|
|
slice,
|
|
|
|
|
data,
|
|
|
|
|
tx;
|
|
|
|
|
|
2011-11-18 05:44:24 +00:00
|
|
|
|
if ( val.length > 0 ) {
|
2011-12-05 20:40:35 +00:00
|
|
|
|
// Check if there was any effective input
|
|
|
|
|
var input = this.$input[0],
|
|
|
|
|
// Internet Explorer
|
|
|
|
|
range = document.selection && document.selection.createRange();
|
|
|
|
|
if (
|
2012-02-23 23:20:47 +00:00
|
|
|
|
// Be sure text is being selected so IME updates are not blocked
|
|
|
|
|
( this.mouse.selectingMode || this.keyboard.selection ) &&
|
2011-12-05 20:40:35 +00:00
|
|
|
|
// DOM 3.0
|
2012-02-23 23:20:47 +00:00
|
|
|
|
(( 'selectionStart' in input && input.selectionEnd - input.selectionStart ) ||
|
2011-12-05 20:40:35 +00:00
|
|
|
|
// Internet Explorer
|
2012-02-23 23:20:47 +00:00
|
|
|
|
( range && range.text.length ))
|
2011-12-05 20:40:35 +00:00
|
|
|
|
) {
|
|
|
|
|
// The input is still selected, so the key must not have inserted anything
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-23 23:20:47 +00:00
|
|
|
|
//process undo count
|
|
|
|
|
while ( this.keyboard.undoCount > 0 ) {
|
|
|
|
|
this.keyboard.undoCount--;
|
|
|
|
|
this.model.undo();
|
|
|
|
|
}
|
|
|
|
|
//reset selection
|
|
|
|
|
selection = this.currentSelection.clone();
|
2011-12-05 20:40:35 +00:00
|
|
|
|
|
2012-02-23 23:20:47 +00:00
|
|
|
|
// Prepare and process a selection removal.
|
2011-11-23 00:36:46 +00:00
|
|
|
|
if ( selection.from != selection.to ) {
|
2011-12-08 03:14:10 +00:00
|
|
|
|
tx = this.model.getDocument().prepareRemoval( selection );
|
|
|
|
|
this.model.transact( tx, true );
|
|
|
|
|
selection.from = selection.to =
|
|
|
|
|
Math.min( selection.from, selection.to );
|
2011-11-18 05:51:50 +00:00
|
|
|
|
}
|
2012-02-23 23:20:47 +00:00
|
|
|
|
//Chunking text only on keyDown.
|
|
|
|
|
if (chunkSize !== null && val.length > chunkSize && chunkSize > 1) {
|
|
|
|
|
chunkThis = val;
|
|
|
|
|
//build a chunked array
|
|
|
|
|
while ( chunkThis.length > 0 ) {
|
|
|
|
|
slice = chunkThis.substring( 0, chunkSize );
|
|
|
|
|
chunkThis = chunkThis.substring( chunkSize );
|
|
|
|
|
chunked.push( slice );
|
|
|
|
|
}
|
|
|
|
|
//chunked transactions
|
|
|
|
|
for ( var chunk in chunked ) {
|
|
|
|
|
this.model.breakpoint();
|
|
|
|
|
data = chunked[chunk].split('');
|
|
|
|
|
selection = this.currentSelection.clone();
|
|
|
|
|
ve.dm.DocumentNode.addAnnotationsToData( data, this.getInsertionAnnotations() );
|
|
|
|
|
tx = this.model.getDocument().prepareInsertion( selection.from, data );
|
|
|
|
|
this.model.transact( tx );
|
|
|
|
|
// Move the selection
|
|
|
|
|
selection.from += chunked[chunk].length;
|
|
|
|
|
selection.to += chunked[chunk].length;
|
|
|
|
|
this.model.select( selection );
|
|
|
|
|
this.model.breakpoint();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//set the working text to the last chunk
|
|
|
|
|
this.$input.val( chunked[chunked.length - 1] );
|
|
|
|
|
this.keyboard.undoCount = 1;
|
|
|
|
|
} else {
|
|
|
|
|
this.model.breakpoint();
|
|
|
|
|
data = val.split('');
|
|
|
|
|
ve.dm.DocumentNode.addAnnotationsToData( data, this.getInsertionAnnotations() );
|
|
|
|
|
tx = this.model.getDocument().prepareInsertion( selection.from, data );
|
|
|
|
|
this.model.transact( tx );
|
|
|
|
|
// Move the selection
|
|
|
|
|
selection.from += val.length;
|
|
|
|
|
selection.to += val.length;
|
|
|
|
|
this.model.select( selection );
|
|
|
|
|
this.keyboard.undoCount++;
|
|
|
|
|
this.model.breakpoint();
|
2012-02-28 00:35:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Move the cursor left if RTL
|
2012-02-28 00:46:23 +00:00
|
|
|
|
TODO:
|
|
|
|
|
1) Create method to detect end of RTL and move cursor the the right
|
|
|
|
|
2) moveCursor is breaking IME for the RTL language, need to add mode switching
|
2012-02-28 00:35:17 +00:00
|
|
|
|
*/
|
2012-02-28 01:37:38 +00:00
|
|
|
|
if( this.isTextRTL( val.charAt( val.length-1 ) ) ) {
|
2012-02-28 00:35:17 +00:00
|
|
|
|
this.moveCursor('left', 'char');
|
|
|
|
|
}
|
2012-02-23 23:20:47 +00:00
|
|
|
|
}
|
2011-12-01 01:10:38 +00:00
|
|
|
|
|
2011-11-18 05:44:24 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2011-11-15 01:30:39 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param {String} direction up | down | left | right
|
|
|
|
|
* @param {String} unit char | word | line | node | page
|
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.moveCursor = function( direction, unit ) {
|
2011-11-15 01:30:39 +00:00
|
|
|
|
if ( direction !== 'up' && direction !== 'down' ) {
|
2011-11-04 23:28:40 +00:00
|
|
|
|
this.cursor.initialLeft = null;
|
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
var selection = this.currentSelection.clone(),
|
|
|
|
|
to,
|
2011-11-21 22:43:56 +00:00
|
|
|
|
offset;
|
2011-11-15 01:30:39 +00:00
|
|
|
|
switch ( direction ) {
|
|
|
|
|
case 'left':
|
|
|
|
|
case 'right':
|
|
|
|
|
switch ( unit ) {
|
|
|
|
|
case 'char':
|
|
|
|
|
case 'word':
|
2011-11-23 00:36:46 +00:00
|
|
|
|
if ( this.keyboard.keys.shift || selection.from === selection.to ) {
|
|
|
|
|
offset = selection.to;
|
2011-11-15 01:30:39 +00:00
|
|
|
|
} else {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
offset = direction === 'left' ? selection.start : selection.end;
|
2011-11-15 01:30:39 +00:00
|
|
|
|
}
|
2011-11-22 22:59:05 +00:00
|
|
|
|
to = this.model.getDocument().getRelativeContentOffset(
|
2011-11-15 01:30:39 +00:00
|
|
|
|
offset,
|
|
|
|
|
direction === 'left' ? -1 : 1
|
|
|
|
|
);
|
|
|
|
|
if ( unit === 'word' ) {
|
2011-11-22 22:59:05 +00:00
|
|
|
|
var wordRange = this.model.getDocument().getWordBoundaries(
|
2011-11-15 01:30:39 +00:00
|
|
|
|
direction === 'left' ? to : offset
|
|
|
|
|
);
|
|
|
|
|
if ( wordRange ) {
|
|
|
|
|
to = direction === 'left' ? wordRange.start : wordRange.end;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-11-04 23:49:50 +00:00
|
|
|
|
break;
|
2011-11-15 01:30:39 +00:00
|
|
|
|
case 'line':
|
2011-11-21 22:43:56 +00:00
|
|
|
|
offset = this.cursor.initialBias ?
|
2011-11-22 22:59:05 +00:00
|
|
|
|
this.model.getDocument().getRelativeContentOffset(
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.to,
|
2011-11-15 01:30:39 +00:00
|
|
|
|
-1) :
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.to;
|
2011-11-15 01:30:39 +00:00
|
|
|
|
var range = this.documentView.getRenderedLineRangeFromOffset( offset );
|
|
|
|
|
to = direction === 'left' ? range.start : range.end;
|
2011-11-04 23:28:40 +00:00
|
|
|
|
break;
|
2011-12-01 19:08:32 +00:00
|
|
|
|
default:
|
|
|
|
|
throw new Error( 'unrecognized cursor movement unit' );
|
2011-11-15 01:30:39 +00:00
|
|
|
|
}
|
2011-11-04 23:28:40 +00:00
|
|
|
|
break;
|
2011-11-15 01:30:39 +00:00
|
|
|
|
case 'up':
|
|
|
|
|
case 'down':
|
|
|
|
|
switch ( unit ) {
|
|
|
|
|
case 'unit':
|
2011-11-16 19:51:02 +00:00
|
|
|
|
var toNode = null;
|
2011-11-22 22:59:05 +00:00
|
|
|
|
this.model.getDocument().traverseLeafNodes(
|
2011-12-01 19:08:32 +00:00
|
|
|
|
function( node ) {
|
|
|
|
|
var doNextChild = toNode === null;
|
|
|
|
|
toNode = node;
|
|
|
|
|
return doNextChild;
|
2011-11-16 19:51:02 +00:00
|
|
|
|
},
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.documentView.getNodeFromOffset( selection.to, false ).getModel(),
|
2011-11-16 19:51:02 +00:00
|
|
|
|
direction === 'up' ? true : false
|
|
|
|
|
);
|
2011-11-22 22:59:05 +00:00
|
|
|
|
to = this.model.getDocument().getOffsetFromNode( toNode, false ) + 1;
|
2011-11-16 19:51:02 +00:00
|
|
|
|
break;
|
2011-11-15 01:30:39 +00:00
|
|
|
|
case 'char':
|
|
|
|
|
/*
|
|
|
|
|
* Looks for the in-document character position that would match up with the
|
|
|
|
|
* same horizontal position - jumping a few pixels up/down at a time until we
|
|
|
|
|
* reach the next/previous line
|
|
|
|
|
*/
|
|
|
|
|
var position = this.documentView.getRenderedPositionFromOffset(
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.to,
|
2011-11-18 19:48:43 +00:00
|
|
|
|
this.cursor.initialBias
|
2011-11-15 01:30:39 +00:00
|
|
|
|
);
|
2011-11-19 03:07:27 +00:00
|
|
|
|
|
2011-11-15 01:30:39 +00:00
|
|
|
|
if ( this.cursor.initialLeft === null ) {
|
|
|
|
|
this.cursor.initialLeft = position.left;
|
|
|
|
|
}
|
2012-02-06 23:50:56 +00:00
|
|
|
|
var fakePosition = new ve.Position( this.cursor.initialLeft, position.top ),
|
2011-11-15 01:30:39 +00:00
|
|
|
|
i = 0,
|
|
|
|
|
step = direction === 'up' ? -5 : 5,
|
|
|
|
|
top = this.$.position().top;
|
2011-11-19 03:07:27 +00:00
|
|
|
|
|
2011-11-21 22:43:56 +00:00
|
|
|
|
this.cursor.initialBias = position.left > this.documentView.getNodeFromOffset(
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.to, false
|
2011-11-21 22:43:56 +00:00
|
|
|
|
).contentView.$.offset().left;
|
2011-11-19 03:07:27 +00:00
|
|
|
|
|
2011-11-15 01:30:39 +00:00
|
|
|
|
do {
|
2011-12-01 19:08:32 +00:00
|
|
|
|
i++;
|
|
|
|
|
fakePosition.top += i * step;
|
2011-11-15 01:30:39 +00:00
|
|
|
|
if ( fakePosition.top < top ) {
|
|
|
|
|
break;
|
2011-11-21 22:43:56 +00:00
|
|
|
|
} else if (
|
|
|
|
|
fakePosition.top > top + this.dimensions.height +
|
|
|
|
|
this.dimensions.scrollTop
|
|
|
|
|
) {
|
2011-11-15 01:30:39 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2011-11-19 03:07:27 +00:00
|
|
|
|
fakePosition = this.documentView.getRenderedPositionFromOffset(
|
|
|
|
|
this.documentView.getOffsetFromRenderedPosition( fakePosition ),
|
|
|
|
|
this.cursor.initialBias
|
2011-11-15 01:30:39 +00:00
|
|
|
|
);
|
|
|
|
|
fakePosition.left = this.cursor.initialLeft;
|
|
|
|
|
} while ( position.top === fakePosition.top );
|
|
|
|
|
to = this.documentView.getOffsetFromRenderedPosition( fakePosition );
|
|
|
|
|
break;
|
2011-12-01 19:08:32 +00:00
|
|
|
|
default:
|
|
|
|
|
throw new Error( 'unrecognized cursor movement unit' );
|
2011-11-15 01:30:39 +00:00
|
|
|
|
}
|
2012-02-23 23:20:47 +00:00
|
|
|
|
break;
|
2011-12-01 19:08:32 +00:00
|
|
|
|
default:
|
|
|
|
|
throw new Error( 'unrecognized cursor direction' );
|
2011-11-04 21:40:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 23:21:33 +00:00
|
|
|
|
if( direction != 'up' && direction != 'down' ) {
|
2011-11-19 03:07:27 +00:00
|
|
|
|
this.cursor.initialBias = direction === 'right' && unit === 'line' ? true : false;
|
2011-11-18 23:21:33 +00:00
|
|
|
|
}
|
2011-11-18 19:48:43 +00:00
|
|
|
|
|
2011-11-23 00:36:46 +00:00
|
|
|
|
if ( this.keyboard.keys.shift && selection.from !== to) {
|
|
|
|
|
selection.to = to;
|
2011-11-04 23:32:46 +00:00
|
|
|
|
} else {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
selection.from = selection.to = to;
|
2011-11-02 21:00:55 +00:00
|
|
|
|
}
|
2011-12-06 01:52:38 +00:00
|
|
|
|
this.model.select( selection, true );
|
2012-02-23 23:20:47 +00:00
|
|
|
|
this.resetText();
|
2011-11-02 21:00:55 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shows the cursor in a new position.
|
2012-02-23 23:20:47 +00:00
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
|
* @method
|
|
|
|
|
* @param offset {Integer} Position to show the cursor at
|
|
|
|
|
*/
|
2012-02-23 23:20:47 +00:00
|
|
|
|
ve.es.Surface.prototype.showCursor = function() {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
var $window = $( window ),
|
|
|
|
|
position = this.documentView.getRenderedPositionFromOffset(
|
|
|
|
|
this.currentSelection.to, this.cursor.initialBias
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.$cursor.css( {
|
2011-11-04 00:21:29 +00:00
|
|
|
|
'left': position.left,
|
|
|
|
|
'top': position.top,
|
|
|
|
|
'height': position.bottom - position.top
|
|
|
|
|
} ).show();
|
|
|
|
|
this.$input.css({
|
|
|
|
|
'top': position.top,
|
2012-02-27 19:44:02 +00:00
|
|
|
|
'left': position.left,
|
2011-11-04 00:21:29 +00:00
|
|
|
|
'height': position.bottom - position.top
|
|
|
|
|
});
|
2011-11-02 21:00:55 +00:00
|
|
|
|
|
2011-11-21 22:55:48 +00:00
|
|
|
|
// Auto scroll to cursor
|
|
|
|
|
var inputTop = this.$input.offset().top,
|
2012-02-23 23:20:47 +00:00
|
|
|
|
inputBottom = inputTop + position.bottom - position.top;
|
2011-11-22 07:17:08 +00:00
|
|
|
|
if ( inputTop - this.dimensions.toolbarHeight < this.dimensions.scrollTop ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
$window.scrollTop( inputTop - this.dimensions.toolbarHeight );
|
2011-11-21 22:55:48 +00:00
|
|
|
|
} else if ( inputBottom > ( this.dimensions.scrollTop + this.dimensions.height ) ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
$window.scrollTop( inputBottom - this.dimensions.height );
|
2011-11-21 22:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-02 21:00:55 +00:00
|
|
|
|
// cursor blinking
|
|
|
|
|
if ( this.cursor.interval ) {
|
|
|
|
|
clearInterval( this.cursor.interval );
|
|
|
|
|
}
|
2011-11-22 19:59:18 +00:00
|
|
|
|
|
|
|
|
|
var _this = this;
|
2011-11-02 21:00:55 +00:00
|
|
|
|
this.cursor.interval = setInterval( function( surface ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
|
_this.$cursor.css( 'display', function( index, value ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
|
return value === 'block' ? 'none' : 'block';
|
|
|
|
|
} );
|
2011-11-22 19:59:18 +00:00
|
|
|
|
}, 500 );
|
2011-11-02 21:00:55 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Hides the cursor.
|
2012-02-23 23:20:47 +00:00
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
|
* @method
|
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.es.Surface.prototype.hideCursor = function() {
|
2011-11-02 21:00:55 +00:00
|
|
|
|
if( this.cursor.interval ) {
|
|
|
|
|
clearInterval( this.cursor.interval );
|
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
|
this.$cursor.hide();
|
2011-11-02 21:00:55 +00:00
|
|
|
|
};
|
2011-11-10 01:28:01 +00:00
|
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
|
ve.extendClass( ve.es.Surface, ve.EventEmitter );
|