Configurable insertion annotations

This makes it possible to use a static property to configure whether an
annotation should be applied to content added after it. This makes it
possible to do this for normal style stuff, but not for links.

TODO: Inez is going to add IE support for this since it inverts the
problem where the UI gets out of sync in all non-IE browsers to now make
it so it only gets out of sync in IE.

Bug: 48171

Change-Id: I5f279b06b098960be7bd4ad3f5e6f74b67e31d1a
This commit is contained in:
Trevor Parscal 2013-05-06 13:08:52 -07:00 committed by Gerrit Code Review
parent d23c10fd8e
commit c6e0eee837
5 changed files with 37 additions and 17 deletions

View file

@ -77,7 +77,8 @@ ve.AnnotationAction.prototype.toggle = function ( name, data ) {
fragment.getAnnotations().containsComparable( annotation ) ? 'clear' : 'set', name, data
);
} else {
existingAnnotations = surfaceModel.getInsertionAnnotations().getComparableAnnotations( annotation );
existingAnnotations = surfaceModel
.getInsertionAnnotations().getComparableAnnotations( annotation );
if ( existingAnnotations.isEmpty() ) {
surfaceModel.addInsertionAnnotations( annotation );
} else {

View file

@ -30,6 +30,8 @@ ve.dm.LinkAnnotation.static.name = 'link';
ve.dm.LinkAnnotation.static.matchTagNames = ['a'];
ve.dm.LinkAnnotation.static.applyToAppendedContent = false;
ve.dm.LinkAnnotation.static.toDataElement = function ( domElements ) {
return {
'type': 'link',

View file

@ -41,6 +41,13 @@ ve.inheritClass( ve.dm.Annotation, ve.dm.Model );
*/
ve.dm.Annotation.static.enableAboutGrouping = false;
/**
* Automatically apply annotation to content inserted after it.
*
* @type {boolean}
*/
ve.dm.Annotation.static.applyToAppendedContent = true;
/* Methods */
/**

View file

@ -82,12 +82,13 @@ ve.dm.AnnotationSet.prototype.hasAnnotationWithName = function ( name ) {
/**
* Get an annotation or all annotations from the set.
*
* set.get( 5 ) returns the annotation at index 5, set.get() returns an array with all annotations in
* the entire set.
* set.get( 5 ) returns the annotation at index 5, set.get() returns an array with all annotations
* in the entire set.
*
* @method
* @param {number} [index] If set, only get the annotation at the index
* @returns {Array|ve.dm.Annotation|undefined} The annotation at index, or an array of all annotation in the set
* @returns {Array|ve.dm.Annotation|undefined} The annotation at index, or an array of all
* annotation in the set
*/
ve.dm.AnnotationSet.prototype.get = function ( index ) {
if ( index !== undefined ) {

View file

@ -297,10 +297,11 @@ ve.dm.Surface.prototype.change = function ( transactions, selection ) {
if ( !this.enabled ) {
return;
}
var i, len, offset, annotations,
var i, len, left, right, leftAnnotations, rightAnnotations, insertionAnnotations,
selectedNodes = {},
selectionChange = false,
contextChange = false;
contextChange = false,
dataModelData = this.documentModel.data;
// Stop observation polling, things changing right now are known already
this.emit( 'lock' );
@ -360,28 +361,36 @@ ve.dm.Surface.prototype.change = function ( transactions, selection ) {
}
}
// Figure out which offset which we should get insertion annotations from
// Figure out which annotations to use for insertions
if ( this.selection.isCollapsed() ) {
// Get annotations from the left of the cursor
offset = this.documentModel.data.getNearestContentOffset(
Math.max( 0, this.selection.start - 1 ), -1
);
left = dataModelData.getNearestContentOffset( Math.max( 0, this.selection.start - 1 ), -1 );
right = dataModelData.getNearestContentOffset( Math.max( 0, this.selection.start ) );
} else {
// Get annotations from the first character of the selection
offset = this.documentModel.data.getNearestContentOffset( this.selection.start );
left = dataModelData.getNearestContentOffset( this.selection.start );
right = dataModelData.getNearestContentOffset( this.selection.end );
}
if ( offset === -1 ) {
if ( left === -1 ) {
// Document is empty, use empty set
annotations = new ve.dm.AnnotationSet( this.documentModel.getStore() );
insertionAnnotations = new ve.dm.AnnotationSet( this.documentModel.getStore() );
} else {
annotations = this.documentModel.data.getAnnotationsFromOffset( offset );
// Include annotations on the left that should be added to appended content, or ones that
// are on both the left and the right that should not
leftAnnotations = dataModelData.getAnnotationsFromOffset( left );
rightAnnotations = dataModelData.getAnnotationsFromOffset( right );
insertionAnnotations = leftAnnotations.filter( function ( annotation ) {
return annotation.constructor.static.applyToAppendedContent ||
rightAnnotations.containsComparable( annotation );
} );
}
// Only emit an annotations change event if there's a meaningful difference
if (
!annotations.containsAllOf( this.insertionAnnotations ) ||
!this.insertionAnnotations.containsAllOf( annotations )
!insertionAnnotations.containsAllOf( this.insertionAnnotations ) ||
!this.insertionAnnotations.containsAllOf( insertionAnnotations )
) {
this.setInsertionAnnotations( annotations );
this.setInsertionAnnotations( insertionAnnotations );
contextChange = true;
}