2011-11-02 21:00:55 +00:00
|
|
|
/**
|
|
|
|
* Creates an es.SurfaceView object.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {jQuery} $container DOM Container to render surface into
|
|
|
|
* @param {es.SurfaceModel} model Surface model to view
|
|
|
|
*/
|
|
|
|
es.SurfaceView = function( $container, model ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
// Inheritance
|
2011-11-10 01:28:01 +00:00
|
|
|
es.EventEmitter.call( this );
|
|
|
|
|
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;
|
2011-11-23 00:36:46 +00:00
|
|
|
this.currentSelection = new es.Range();
|
2011-11-02 21:00:55 +00:00
|
|
|
this.documentView = new es.DocumentView( this.model.getDocument(), this );
|
2011-11-23 00:36:46 +00:00
|
|
|
this.$ = $container
|
|
|
|
.addClass( 'es-surfaceView' )
|
|
|
|
.append( this.documentView.$ );
|
|
|
|
this.$input = $( '<textarea class="es-surfaceView-textarea" />' )
|
|
|
|
.prependTo( this.$ );
|
|
|
|
this.$cursor = $( '<div class="es-surfaceView-cursor"></div>' )
|
|
|
|
.appendTo( this.$ );
|
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,
|
2011-11-18 05:44:24 +00:00
|
|
|
keys: { shift: false }
|
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!
|
|
|
|
toolbarTop: $( '#es-toolbar' ).offset().top,
|
|
|
|
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
|
|
|
|
if ( selection.from !== selection.to ) {
|
|
|
|
_this.hideCursor();
|
|
|
|
} else {
|
|
|
|
_this.showCursor();
|
2011-11-02 21:00:55 +00:00
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
_this.documentView.drawSelection( selection );
|
2011-11-02 21:00:55 +00:00
|
|
|
} );
|
2011-11-23 00:36:46 +00:00
|
|
|
this.model.getDocument().on( 'update', function() {
|
|
|
|
_this.emit( 'update' );
|
|
|
|
} );
|
|
|
|
this.$.mousedown( function(e) {
|
|
|
|
return _this.onMouseDown( e );
|
|
|
|
} );
|
|
|
|
this.$input.on( {
|
|
|
|
'focus': function() {
|
|
|
|
// Make sure we aren't double-binding
|
2011-11-02 21:00:55 +00:00
|
|
|
$document.off( '.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
|
|
|
|
$document.on( {
|
2011-11-02 21:00:55 +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
|
|
|
},
|
|
|
|
'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 ) {
|
2011-11-18 05:44:24 +00:00
|
|
|
return _this.onKeyDown( e );
|
2011-11-02 21:00:55 +00:00
|
|
|
},
|
|
|
|
'keyup.es-surfaceView': function( e ) {
|
2011-11-18 05:44:24 +00:00
|
|
|
return _this.onKeyUp( e );
|
2011-11-02 21:00:55 +00:00
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
} );
|
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-02 21:00:55 +00:00
|
|
|
$document.off( '.es-surfaceView' );
|
2011-11-18 05:44:24 +00:00
|
|
|
_this.hideCursor();
|
2011-11-02 21:00:55 +00:00
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
} );
|
|
|
|
$window.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
|
2011-11-18 05:44:24 +00:00
|
|
|
_this.hideCursor();
|
2011-11-23 00:36:46 +00:00
|
|
|
_this.dimensions.height = $window.height();
|
2011-11-18 05:44:24 +00:00
|
|
|
var width = _this.$.width();
|
|
|
|
if ( _this.dimensions.width !== width ) {
|
|
|
|
_this.dimensions.width = width;
|
|
|
|
_this.documentView.renderContent();
|
2011-11-02 21:00:55 +00:00
|
|
|
}
|
|
|
|
} );
|
2011-11-23 00:36:46 +00:00
|
|
|
$window.scroll( function() {
|
|
|
|
// FIXME: Is this code in the right place?
|
|
|
|
_this.dimensions.scrollTop = $window.scrollTop();
|
2011-11-22 07:17:08 +00:00
|
|
|
if ( _this.dimensions.scrollTop >= _this.dimensions.toolbarTop ) {
|
|
|
|
$( '#es-toolbar' ).addClass( 'float' );
|
|
|
|
$( '#es-panes' ).css( 'padding-top', _this.dimensions.toolbarHeight );
|
|
|
|
} else {
|
|
|
|
$( '#es-toolbar' ).removeClass( 'float' );
|
|
|
|
$( '#es-panes' ).css( 'padding-top', 0 );
|
|
|
|
}
|
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!)
|
|
|
|
|
|
|
|
// 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-11-09 23:36:56 +00:00
|
|
|
es.SurfaceView.prototype.onMouseDown = function( e ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
// Only for left mouse button
|
|
|
|
if ( e.button === 0 ) {
|
|
|
|
var selection = this.currentSelection.clone(),
|
|
|
|
offset = this.documentView.getOffsetFromEvent( e );
|
|
|
|
|
|
|
|
// Single click
|
|
|
|
if ( e.originalEvent.detail === 1 ) {
|
|
|
|
// @see {es.SurfaceView.prototype.onMouseMove}
|
|
|
|
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
|
|
|
if ( selection.to !== selection.from ) {
|
|
|
|
// Clear the selection if there was any
|
2011-11-09 23:36:56 +00:00
|
|
|
this.documentView.clearSelection();
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
selection.from = selection.to = offset;
|
2011-11-15 01:30:39 +00:00
|
|
|
|
2011-11-10 23:30:29 +00:00
|
|
|
var position = es.Position.newFromEventPagePosition( e ),
|
|
|
|
nodeView = this.documentView.getNodeFromOffset( offset, false );
|
|
|
|
this.cursor.initialBias = position.left > nodeView.contentView.$.offset().left;
|
|
|
|
}
|
2011-11-15 01:30:39 +00:00
|
|
|
|
2011-11-23 00:36:46 +00:00
|
|
|
}
|
|
|
|
// Double click
|
|
|
|
else if ( e.originalEvent.detail === 2 ) {
|
|
|
|
// @see {es.SurfaceView.prototype.onMouseMove}
|
|
|
|
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-15 01:30:39 +00:00
|
|
|
|
2011-11-23 00:36:46 +00:00
|
|
|
}
|
|
|
|
// Triple click
|
|
|
|
else if ( e.originalEvent.detail >= 3 ) {
|
|
|
|
// @see {es.SurfaceView.prototype.onMouseMove}
|
|
|
|
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
|
|
|
}
|
2011-11-10 01:13:56 +00:00
|
|
|
|
2011-11-23 00:36:46 +00:00
|
|
|
// Reset the initial left position
|
|
|
|
this.cursor.initialLeft = null;
|
|
|
|
// Apply new selection
|
|
|
|
this.model.select( selection );
|
2011-11-03 20:27:57 +00:00
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
// If the inut isn't already focused, focus it and select it's contents
|
2011-11-02 21:00:55 +00:00
|
|
|
if ( !this.$input.is( ':focus' ) ) {
|
|
|
|
this.$input.focus().select();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2011-11-03 20:27:57 +00:00
|
|
|
es.SurfaceView.prototype.onMouseMove = function( e ) {
|
2011-11-23 00:36:46 +00:00
|
|
|
// Only with the left mouse button while in selecting mode
|
|
|
|
if ( e.button === 0 && this.mouse.selectingMode ) {
|
|
|
|
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 ) {
|
|
|
|
// @see {es.SurfaceView.prototype.onMouseMove}
|
|
|
|
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
|
|
|
);
|
2011-11-10 23:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
// Apply new selection
|
|
|
|
this.model.select( selection, true );
|
2011-11-03 20:27:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
es.SurfaceView.prototype.onMouseUp = function( e ) {
|
2011-11-10 23:30:29 +00:00
|
|
|
if ( e.button === 0 ) { // left mouse button
|
2011-11-10 01:13:56 +00:00
|
|
|
this.mouse.selectingMode = this.mouse.selectedRange = null;
|
2011-11-23 00:36:46 +00:00
|
|
|
this.model.select( this.currentSelection );
|
2011-11-03 20:27:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-02 21:00:55 +00:00
|
|
|
es.SurfaceView.prototype.onKeyDown = function( e ) {
|
2011-11-23 00:59:57 +00:00
|
|
|
var _this = this;
|
|
|
|
function handleInsert() {
|
|
|
|
if ( _this.keyboard.keydownTimeout ) {
|
|
|
|
clearTimeout( _this.keyboard.keydownTimeout );
|
|
|
|
}
|
|
|
|
_this.keyboard.keydownTimeout = setTimeout( function () {
|
|
|
|
_this.insertFromInput();
|
|
|
|
}, 10 );
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
switch ( e.keyCode ) {
|
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-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-11-21 22:55:48 +00:00
|
|
|
this.handleEnter();
|
2011-11-18 06:17:14 +00:00
|
|
|
e.preventDefault();
|
2011-11-02 21:00:55 +00:00
|
|
|
break;
|
2011-11-23 00:59:57 +00:00
|
|
|
// U (soft undo/soft redo)
|
|
|
|
case 85:
|
|
|
|
if ( e.metaKey || e.ctrlKey ) {
|
|
|
|
if ( this.keyboard.keys.shift ) {
|
|
|
|
this.model.redo( 1 );
|
|
|
|
} else {
|
|
|
|
this.model.undo( 1 );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
handleInsert();
|
|
|
|
break;
|
2011-11-23 00:36:46 +00:00
|
|
|
// Z (undo/redo)
|
|
|
|
case 90:
|
2011-11-21 23:54:57 +00:00
|
|
|
if ( e.metaKey || e.ctrlKey ) {
|
2011-11-22 00:26:50 +00:00
|
|
|
if ( this.keyboard.keys.shift ) {
|
2011-11-23 00:59:57 +00:00
|
|
|
this.model.redo( 1, true );
|
2011-11-22 00:26:50 +00:00
|
|
|
} else {
|
2011-11-23 00:59:57 +00:00
|
|
|
this.model.undo( 1, true );
|
2011-11-22 00:26:50 +00:00
|
|
|
}
|
2011-11-23 00:59:57 +00:00
|
|
|
return false;
|
2011-11-21 23:25:43 +00:00
|
|
|
}
|
2011-11-23 00:59:57 +00:00
|
|
|
handleInsert();
|
|
|
|
break;
|
2011-11-23 00:36:46 +00:00
|
|
|
// Insert content (maybe)
|
|
|
|
default:
|
2011-11-23 00:59:57 +00:00
|
|
|
handleInsert();
|
2011-11-02 21:00:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-11-05 00:49:48 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2011-11-02 21:00:55 +00:00
|
|
|
es.SurfaceView.prototype.onKeyUp = function( e ) {
|
|
|
|
switch ( e.keyCode ) {
|
|
|
|
case 16: // Shift
|
|
|
|
this.keyboard.keys.shift = false;
|
|
|
|
if ( this.keyboard.selecting ) {
|
|
|
|
this.keyboard.selecting = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2011-11-21 21:50:14 +00:00
|
|
|
es.SurfaceView.prototype.handleDelete = function( backspace ) {
|
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,
|
|
|
|
tx;
|
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() ) {
|
2011-11-21 22:43:56 +00:00
|
|
|
sourceSplitableNode = es.DocumentViewNode.getSplitableNode( sourceNode );
|
|
|
|
targetSplitableNode = es.DocumentViewNode.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;
|
|
|
|
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-11-22 22:59:05 +00:00
|
|
|
tx = this.model.getDocument().prepareRemoval(
|
2011-11-21 21:50:14 +00:00
|
|
|
new es.Range( targetOffset, sourceOffset )
|
|
|
|
);
|
2011-11-22 22:59:05 +00:00
|
|
|
this.model.transact( tx );
|
2011-11-21 21:50:14 +00:00
|
|
|
} else {
|
2011-11-22 22:59:05 +00:00
|
|
|
tx = this.model.getDocument().prepareInsertion(
|
2011-11-21 21:50:14 +00:00
|
|
|
targetOffset, sourceNode.model.getContent()
|
|
|
|
);
|
2011-11-22 22:59:05 +00:00
|
|
|
this.model.transact( tx );
|
2011-11-21 21:50:14 +00:00
|
|
|
|
|
|
|
var nodeToDelete = sourceNode;
|
|
|
|
es.DocumentNode.traverseUpstream( nodeToDelete, function( node ) {
|
|
|
|
if ( node.getParent().children.length === 1 ) {
|
2011-11-22 20:11:23 +00:00
|
|
|
nodeToDelete = node.getParent();
|
2011-11-21 21:50:14 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
var range = new es.Range();
|
|
|
|
range.from = this.documentView.getOffsetFromNode( nodeToDelete, false );
|
|
|
|
range.to = range.from + nodeToDelete.getElementLength();
|
2011-11-22 22:59:05 +00:00
|
|
|
tx = this.model.getDocument().prepareRemoval( range );
|
|
|
|
this.model.transact( tx );
|
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-11-22 22:59:05 +00:00
|
|
|
this.model.transact( tx );
|
2011-11-21 21:50:14 +00:00
|
|
|
this.documentView.clearSelection();
|
2011-11-23 00:36:46 +00:00
|
|
|
selection.from = selection.to = selection.start;
|
|
|
|
this.model.select( selection );
|
2011-11-21 21:50:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-21 22:55:48 +00:00
|
|
|
es.SurfaceView.prototype.handleEnter = function() {
|
2011-11-23 00:36:46 +00:00
|
|
|
var selection = this.currentSelection.clone(),
|
|
|
|
tx;
|
|
|
|
if ( selection.from !== selection.to ) {
|
2011-11-21 22:55:48 +00:00
|
|
|
this.handleDelete();
|
|
|
|
}
|
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 &&
|
2011-11-21 22:55:48 +00:00
|
|
|
node === es.DocumentViewNode.getSplitableNode( node )
|
|
|
|
) {
|
2011-11-23 00:36:46 +00:00
|
|
|
tx = this.documentView.model.prepareInsertion(
|
2011-11-21 22:55:48 +00:00
|
|
|
nodeOffset + node.getElementLength(),
|
|
|
|
[ { 'type': 'paragraph' }, { 'type': '/paragraph' } ]
|
|
|
|
);
|
2011-11-22 22:59:05 +00:00
|
|
|
this.model.transact( tx );
|
2011-11-23 00:36:46 +00:00
|
|
|
selection.from = selection.to = nodeOffset + node.getElementLength() + 1;
|
2011-11-21 22:55:48 +00:00
|
|
|
} else {
|
|
|
|
var stack = [],
|
|
|
|
splitable = false;
|
|
|
|
|
|
|
|
es.DocumentNode.traverseUpstream( node, function( node ) {
|
|
|
|
var elementType = node.model.getElementType();
|
|
|
|
if (
|
|
|
|
splitable === true &&
|
|
|
|
es.DocumentView.splitRules[ elementType ].children === true
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
stack.splice(
|
|
|
|
stack.length / 2,
|
|
|
|
0,
|
|
|
|
{ 'type': '/' + elementType },
|
|
|
|
{
|
|
|
|
'type': elementType,
|
|
|
|
'attributes': es.copyObject( node.model.element.attributes )
|
|
|
|
}
|
|
|
|
);
|
|
|
|
splitable = es.DocumentView.splitRules[ elementType ].self;
|
|
|
|
} );
|
2011-11-23 00:36:46 +00:00
|
|
|
tx = this.documentView.model.prepareInsertion( selection.to, stack );
|
2011-11-22 22:59:05 +00:00
|
|
|
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-11-23 00:36:46 +00:00
|
|
|
this.model.select( selection );
|
2011-11-21 22:55:48 +00:00
|
|
|
};
|
|
|
|
|
2011-11-18 05:44:24 +00:00
|
|
|
es.SurfaceView.prototype.insertFromInput = function() {
|
2011-11-23 00:36:46 +00:00
|
|
|
var selection = this.currentSelection.clone(),
|
|
|
|
val = this.$input.val();
|
2011-11-18 05:44:24 +00:00
|
|
|
this.$input.val( '' );
|
|
|
|
if ( val.length > 0 ) {
|
2011-11-21 22:43:56 +00:00
|
|
|
var tx;
|
2011-11-23 00:36:46 +00:00
|
|
|
if ( selection.from != selection.to ) {
|
|
|
|
tx = this.model.getDocument().prepareRemoval( selection );
|
2011-11-22 22:59:05 +00:00
|
|
|
this.model.transact( tx );
|
2011-11-18 05:51:50 +00:00
|
|
|
this.documentView.clearSelection();
|
2011-11-23 00:36:46 +00:00
|
|
|
selection.from = selection.to =
|
|
|
|
Math.min( selection.from, selection.to );
|
2011-11-18 05:51:50 +00:00
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
tx = this.model.getDocument().prepareInsertion( selection.from, val.split('') );
|
2011-11-22 22:59:05 +00:00
|
|
|
this.model.transact( tx );
|
2011-11-23 00:36:46 +00:00
|
|
|
selection.from += val.length;
|
|
|
|
selection.to += val.length;
|
|
|
|
this.model.select( selection );
|
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
|
|
|
|
*/
|
|
|
|
es.SurfaceView.prototype.moveCursor = function( direction, unit ) {
|
|
|
|
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-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-11-16 19:51:02 +00:00
|
|
|
function( node ) {
|
|
|
|
if ( toNode === null) {
|
|
|
|
toNode = node;
|
|
|
|
} else {
|
|
|
|
toNode = node;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
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;
|
|
|
|
}
|
|
|
|
var fakePosition = new es.Position( this.cursor.initialLeft, position.top ),
|
|
|
|
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 {
|
|
|
|
fakePosition.top += ++i * step;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
break;
|
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
|
|
|
if ( selection.from !== selection.to ) {
|
2011-11-04 23:32:46 +00:00
|
|
|
this.documentView.clearSelection();
|
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
selection.from = selection.to = to;
|
2011-11-02 21:00:55 +00:00
|
|
|
}
|
2011-11-23 00:36:46 +00:00
|
|
|
this.model.select( selection );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the cursor in a new position.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param offset {Integer} Position to show the cursor at
|
|
|
|
*/
|
2011-11-04 23:28:40 +00:00
|
|
|
es.SurfaceView.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,
|
|
|
|
'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,
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
es.SurfaceView.prototype.hideCursor = function() {
|
|
|
|
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 */
|
|
|
|
|
|
|
|
es.extendClass( es.SurfaceView, es.EventEmitter );
|