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 fragment.getAnnotations().containsComparable( annotation ) ? 'clear' : 'set', name, data
); );
} else { } else {
existingAnnotations = surfaceModel.getInsertionAnnotations().getComparableAnnotations( annotation ); existingAnnotations = surfaceModel
.getInsertionAnnotations().getComparableAnnotations( annotation );
if ( existingAnnotations.isEmpty() ) { if ( existingAnnotations.isEmpty() ) {
surfaceModel.addInsertionAnnotations( annotation ); surfaceModel.addInsertionAnnotations( annotation );
} else { } else {

View file

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

View file

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

View file

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

View file

@ -297,10 +297,11 @@ ve.dm.Surface.prototype.change = function ( transactions, selection ) {
if ( !this.enabled ) { if ( !this.enabled ) {
return; return;
} }
var i, len, offset, annotations, var i, len, left, right, leftAnnotations, rightAnnotations, insertionAnnotations,
selectedNodes = {}, selectedNodes = {},
selectionChange = false, selectionChange = false,
contextChange = false; contextChange = false,
dataModelData = this.documentModel.data;
// Stop observation polling, things changing right now are known already // Stop observation polling, things changing right now are known already
this.emit( 'lock' ); 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() ) { if ( this.selection.isCollapsed() ) {
// Get annotations from the left of the cursor // Get annotations from the left of the cursor
offset = this.documentModel.data.getNearestContentOffset( left = dataModelData.getNearestContentOffset( Math.max( 0, this.selection.start - 1 ), -1 );
Math.max( 0, this.selection.start - 1 ), -1 right = dataModelData.getNearestContentOffset( Math.max( 0, this.selection.start ) );
);
} else { } else {
// Get annotations from the first character of the selection // 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 // Document is empty, use empty set
annotations = new ve.dm.AnnotationSet( this.documentModel.getStore() ); insertionAnnotations = new ve.dm.AnnotationSet( this.documentModel.getStore() );
} else { } 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 // Only emit an annotations change event if there's a meaningful difference
if ( if (
!annotations.containsAllOf( this.insertionAnnotations ) || !insertionAnnotations.containsAllOf( this.insertionAnnotations ) ||
!this.insertionAnnotations.containsAllOf( annotations ) !this.insertionAnnotations.containsAllOf( insertionAnnotations )
) { ) {
this.setInsertionAnnotations( annotations ); this.setInsertionAnnotations( insertionAnnotations );
contextChange = true; contextChange = true;
} }