mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-09-28 12:46:45 +00:00
bf254f44da
Objective: Make it possible for inspectors to inspect nodes or annotations, rather than only annotations. Meanwhile, also make it possible for dialogs to edit an annotation. Strategy: Switch from using type patterns to associate inspectors with annotations to using arrays of classes, similar to how dialogs already work. Introduce a view registry which provides lookups for relationships between models and views. This is more centralized and less repetitive than implement matching functions for both annotations and nodes in both the dialog and inspector factories. Changes: *.php * Added links to new file ve.AnnotationAction.js * Removed unused parameter to filter annotations using a string or regexp ve.dm.AnnotationSet.js * Switched from property/value arguments to callbacks ve.ui.*(Dialog|Inspector).js * Replaced type patterns with class lists * Added class to view registry ve.ui.*Tool.js, ve.ui.Context.js * Updated model/view relationship lookup ve.ui.*Factory.js * Removed overly-specific lookup functions ve.ui.Inspector.js * Removed typePattern property * Updated model/view relationship lookup ve.ui.ViewRegistry.js * New class! * Migrated node and annotation lookup functions from factories Change-Id: Ic2bbcf072fdd87e5ce8a03fe1ae3e6d8d50e2593
100 lines
2.5 KiB
JavaScript
100 lines
2.5 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 ).destroy();
|
|
};
|
|
|
|
/**
|
|
* 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 ).destroy();
|
|
};
|
|
|
|
/**
|
|
* 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
|
|
)
|
|
.destroy();
|
|
};
|
|
|
|
/**
|
|
* 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 );
|
|
}
|
|
fragment.destroy();
|
|
};
|
|
|
|
/* Registration */
|
|
|
|
ve.actionFactory.register( 'annotation', ve.AnnotationAction );
|