mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-09-26 19:56:49 +00:00
Refactored annotation of content - now there's an annotate method in surface view
This commit is contained in:
parent
ee1caca875
commit
b1f54c5ae5
|
@ -173,7 +173,7 @@ es.DocumentModel.getIndexOfAnnotation = function( annotations, annotation ) {
|
|||
if ( typeof annotations[i] === 'string' ) {
|
||||
continue;
|
||||
}
|
||||
if ( annotations[i].hash === annotation.hash ) {
|
||||
if ( annotations[i].hash === ( annotation.hash || es.DocumentModel.getHash( annotation ) ) ) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,36 +14,23 @@ es.AnnotationButtonTool = function( toolbar, name, data ) {
|
|||
|
||||
// Properties
|
||||
this.annotation = data;
|
||||
this.active = false;
|
||||
};
|
||||
|
||||
/* Methods */
|
||||
|
||||
es.AnnotationButtonTool.prototype.onClick = function() {
|
||||
var method;
|
||||
if ( this.$.hasClass( 'es-toolbarButtonTool-down' ) ) {
|
||||
method = 'clear';
|
||||
this.toolbar.surfaceView.removeInsertionAnnotation( this.annotation );
|
||||
} else {
|
||||
method = 'set';
|
||||
this.toolbar.surfaceView.addInsertionAnnotation( this.annotation );
|
||||
}
|
||||
|
||||
var tx = this.toolbar.surfaceView.model.getDocument().prepareContentAnnotation(
|
||||
this.toolbar.surfaceView.currentSelection,
|
||||
method,
|
||||
this.annotation
|
||||
);
|
||||
this.toolbar.surfaceView.model.transact( tx );
|
||||
this.toolbar.getSurfaceView().annotate( this.active ? 'clear' : 'set', this.annotation );
|
||||
};
|
||||
|
||||
es.AnnotationButtonTool.prototype.updateState = function( annotations ) {
|
||||
for ( var i = 0; i < annotations.full.length; i++ ) {
|
||||
if ( annotations.full[i].type === this.annotation.type ) {
|
||||
this.$.addClass( 'es-toolbarButtonTool-down' );
|
||||
return;
|
||||
}
|
||||
es.AnnotationButtonTool.prototype.updateState = function( annotations, nodes ) {
|
||||
if ( es.DocumentModel.getIndexOfAnnotation( annotations.full, this.annotation ) !== -1 ) {
|
||||
this.$.addClass( 'es-toolbarButtonTool-down' );
|
||||
this.active = true;
|
||||
return;
|
||||
}
|
||||
this.$.removeClass( 'es-toolbarButtonTool-down' );
|
||||
this.active = false;
|
||||
};
|
||||
|
||||
/* Registration */
|
||||
|
|
|
@ -153,26 +153,52 @@ es.SurfaceView = function( $container, model ) {
|
|||
};
|
||||
|
||||
/* Methods */
|
||||
|
||||
es.SurfaceView.prototype.annotate = function( method, annotation ) {
|
||||
if ( method === 'toggle' ) {
|
||||
var annotations = this.getAnnotations();
|
||||
if ( es.DocumentModel.getIndexOfAnnotation( annotations.full, annotation ) !== -1 ) {
|
||||
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 );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
es.SurfaceView.prototype.getAnnotations = function() {
|
||||
return this.currentSelection.getLength() ?
|
||||
this.model.getDocument().getAnnotationsFromRange( this.currentSelection ) :
|
||||
{
|
||||
'full': this.insertionAnnotations,
|
||||
'partial': [],
|
||||
'all': this.insertionAnnotations
|
||||
};
|
||||
};
|
||||
|
||||
es.SurfaceView.prototype.emitCursor = function() {
|
||||
if ( this.emitCursorTimeout ) {
|
||||
clearTimeout( this.emitCursorTimeout );
|
||||
}
|
||||
var _this = this;
|
||||
this.emitCursorTimeout = setTimeout( function() {
|
||||
var annotations,
|
||||
var annotations = _this.getAnnotations(),
|
||||
nodes = [],
|
||||
model = _this.documentView.model;
|
||||
|
||||
if( _this.currentSelection.from === _this.currentSelection.to ) {
|
||||
var insertionAnnotations = _this.getInsertionAnnotations();
|
||||
annotations = {
|
||||
'full': insertionAnnotations,
|
||||
'partial': [],
|
||||
'all': insertionAnnotations
|
||||
};
|
||||
if ( _this.currentSelection.from === _this.currentSelection.to ) {
|
||||
nodes.push( model.getNodeFromOffset( _this.currentSelection.from ) );
|
||||
} else {
|
||||
annotations = model.getAnnotationsFromRange( _this.currentSelection );
|
||||
var startNode = model.getNodeFromOffset( _this.currentSelection.start ),
|
||||
endNode = model.getNodeFromOffset( _this.currentSelection.end );
|
||||
if ( startNode === endNode ) {
|
||||
|
@ -496,52 +522,52 @@ es.SurfaceView.prototype.onKeyDown = function( e ) {
|
|||
this.handleEnter();
|
||||
e.preventDefault();
|
||||
break;
|
||||
// Z (undo/redo)
|
||||
case 90:
|
||||
if ( e.metaKey || e.ctrlKey ) {
|
||||
if ( this.keyboard.keys.shift ) {
|
||||
this.model.redo( 1 );
|
||||
} else {
|
||||
this.model.undo( 1 );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
handleInsert();
|
||||
break;
|
||||
|
||||
// a (select all)
|
||||
case 65:
|
||||
if ( e.metaKey || e.ctrlKey ) {
|
||||
this.model.select( new es.Range(
|
||||
this.model.getDocument().getRelativeContentOffset( 0, 1 ),
|
||||
this.model.getDocument().getRelativeContentOffset(
|
||||
this.model.getDocument().getContentLength(), -1
|
||||
)
|
||||
), true );
|
||||
break;
|
||||
}
|
||||
// Insert content (maybe)
|
||||
default:
|
||||
if ( !e.ctrlKey || ( e.ctrlKey && e.keyCode === 86 ) ) {
|
||||
handleInsert();
|
||||
// Control/command + character combos
|
||||
if ( e.metaKey || e.ctrlKey ) {
|
||||
switch ( e.keyCode ) {
|
||||
// z (undo/redo)
|
||||
case 90:
|
||||
if ( this.keyboard.keys.shift ) {
|
||||
this.model.redo( 1 );
|
||||
} else {
|
||||
this.model.undo( 1 );
|
||||
}
|
||||
return false;
|
||||
// a (select all)
|
||||
case 65:
|
||||
this.model.select( new es.Range(
|
||||
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;
|
||||
}
|
||||
}
|
||||
// Regular text insertion
|
||||
handleInsert();
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
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;
|
||||
if ( e.keyCode === 16 ) {
|
||||
this.keyboard.keys.shift = false;
|
||||
if ( this.keyboard.selecting ) {
|
||||
this.keyboard.selecting = false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
es.SurfaceView.prototype.handleDelete = function( backspace, isPartial ) {
|
||||
|
@ -815,12 +841,10 @@ es.SurfaceView.prototype.moveCursor = function( direction, unit ) {
|
|||
break;
|
||||
default:
|
||||
throw new Error( 'unrecognized cursor movement unit' );
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new Error( 'unrecognized cursor direction' );
|
||||
break;
|
||||
}
|
||||
|
||||
if( direction != 'up' && direction != 'down' ) {
|
||||
|
|
|
@ -56,6 +56,12 @@ es.ToolbarView = function( $container, surfaceView, config ) {
|
|||
this.setup();
|
||||
};
|
||||
|
||||
/* Methods */
|
||||
|
||||
es.ToolbarView.prototype.getSurfaceView = function() {
|
||||
return this.surfaceView;
|
||||
};
|
||||
|
||||
es.ToolbarView.prototype.setup = function() {
|
||||
for ( var i = 0; i < this.config.length; i++ ) {
|
||||
var $group = $( '<div>' )
|
||||
|
|
Loading…
Reference in a new issue