mediawiki-extensions-Visual.../modules/ve/actions/ve.AnnotationAction.js
Ed Sanders 8b09dd7650 The resurrection
By removing the transaction listeners from surface fragments we
no longer have to make sure they are always manually destroyed.

In order to retain the functionality of having fragments update
with transactions elsewhere we keep a pointer to a place in the
new complete history stack in the surface. The complete history
stack records all transactions, even undone ones.

Whenever getRange is called we replay all transactions in the
complete history (in the correct order) since the fragment was
last updated.

Also in this commit:
* Updated Format/IndentationAction to test undo(). This increases
  coverage of surface fragment behaviour.
* .range is always accessed by .getRange now, although as an
  optimisation we can use the noCopy mode when we a sure the
  returned range will not be modified.
* Added undo test to .update (previously .onTransact)

Bug: 47343
Change-Id: I9e9818da1baa8319a3002f6d74fd1aad6732a8f5
2013-04-22 12:50:23 +01:00

97 lines
2.4 KiB
JavaScript

/*!
* VisualEditor AnnotationAction class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Annotation action.
*
* @class
* @extends ve.Action
* @constructor
* @param {ve.Surface} surface Surface to act on
*/
ve.AnnotationAction = function VeAnnotationAction( surface ) {
// Parent constructor
ve.Action.call( this, surface );
};
/* Inheritance */
ve.inheritClass( ve.AnnotationAction, ve.Action );
/* Static Properties */
/**
* List of allowed methods for the action.
*
* @static
* @property
*/
ve.AnnotationAction.static.methods = ['set', 'clear', 'toggle', 'clearAll'];
/* Methods */
/**
* Set an annotation.
*
* @method
* @param {string} name Annotation name, for example: 'textStyle/bold'
* @param {Object} [data] Additional annotation data
*/
ve.AnnotationAction.prototype.set = function ( name, data ) {
this.surface.getModel().getFragment().annotateContent( 'set', name, data );
};
/**
* Clear an annotation.
*
* @method
* @param {string} name Annotation name, for example: 'textStyle/bold'
* @param {Object} [data] Additional annotation data
*/
ve.AnnotationAction.prototype.clear = function ( name, data ) {
this.surface.getModel().getFragment().annotateContent( 'clear', name, data );
};
/**
* Toggle an annotation.
*
* If the selected text is completely covered with the annotation already the annotation will be
* cleared. Otherwise the annotation will be set.
*
* @method
* @param {string} name Annotation name, for example: 'textStyle/bold'
* @param {Object} [data] Additional annotation data
*/
ve.AnnotationAction.prototype.toggle = function ( name, data ) {
var fragment = this.surface.getModel().getFragment();
fragment.annotateContent(
fragment.getAnnotations().hasAnnotationWithName( name ) ? 'clear' : 'set', name, data
);
};
/**
* Clear all annotations.
*
* @method
*/
ve.AnnotationAction.prototype.clearAll = function () {
var i, len, arr,
fragment = this.surface.getModel().getFragment(),
annotations = fragment.getAnnotations( true );
arr = annotations.get();
// TODO: Allow multiple annotations to be set or cleared by ve.dm.SurfaceFragment, probably
// using an annotation set and ideally building a single transaction
for ( i = 0, len = arr.length; i < len; i++ ) {
fragment.annotateContent( 'clear', arr[i].name, arr[i].data );
}
};
/* Registration */
ve.actionFactory.register( 'annotation', ve.AnnotationAction );