All files / src/ui/inspectors ve.ui.AnnotationInspector.js

95.96% Statements 119/124
86.51% Branches 77/89
100% Functions 15/15
95.9% Lines 117/122

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366                                1x   2x     2x 2x 2x         1x                 1x                       1x 2x                       1x 12x                   1x 2x               1x 1x     1x                         1x                 1x                 1x 54x   54x 31x             1x     72x                                 1x 15x   15x 15x   15x   15x     15x         15x     9x         7x 6x   6x 3x               2x       9x 4x 4x 4x 4x           15x 11x       15x 15x     15x 15x   15x 4x 4x 11x           9x       15x       15x     15x                 1x 15x 15x   15x 15x 15x 15x 15x 15x 15x 15x 15x 15x             6x 6x 4x       15x 3x 3x 2x   1x   12x 2x 2x 2x   2x   10x     10x         5x     10x 5x 5x 2x 2x 1x 1x   1x               5x 5x   5x 1x   4x       5x             11x   2x   9x     11x     11x 11x     11x     11x 11x           11x 9x         11x 1x         15x 15x 15x 15x      
/*!
 * VisualEditor UserInterface AnnotationInspector class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Inspector for working with content annotations.
 *
 * @class
 * @abstract
 * @extends ve.ui.FragmentInspector
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.AnnotationInspector = function VeUiAnnotationInspector() {
	// Parent constructor
	ve.ui.AnnotationInspector.super.apply( this, arguments );
 
	// Properties
	this.initialSelection = null;
	this.initialAnnotation = null;
	this.initialAnnotationIsCovering = false;
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.AnnotationInspector, ve.ui.FragmentInspector );
 
/**
 * Annotation models this inspector can edit.
 *
 * @static
 * @inheritable
 * @property {Function[]}
 */
ve.ui.AnnotationInspector.static.modelClasses = [];
 
/* Methods */
 
/**
 * Check if form is empty, which if saved should result in removing the annotation.
 *
 * Only override this if the form provides the user a way to blank out primary information, allowing
 * them to remove the annotation by clearing the form.
 *
 * @return {boolean} Form is empty
 */
ve.ui.AnnotationInspector.prototype.shouldRemoveAnnotation = function () {
	return false;
};
 
/**
 * Work out whether the teardown process should replace the current text of the fragment.
 *
 * Default behavior is to only do so if nothing was selected initially, in which case we
 * need *something* to apply the annotation to. If this returns true, getInsertionData had
 * better produce something.
 *
 * @return {boolean} Whether to insert text on teardown
 */
ve.ui.AnnotationInspector.prototype.shouldInsertText = function () {
	return !this.isEditing();
};
 
/**
 * Get data to insert if nothing was selected when the inspector opened.
 *
 * Defaults to using #getInsertionText.
 *
 * @return {string[]} Linear model content to insert
 */
ve.ui.AnnotationInspector.prototype.getInsertionData = function () {
	return this.getInsertionText().split( '' );
};
 
/**
 * Get text to insert if nothing was selected when the inspector opened.
 *
 * @return {string} Text to insert
 */
ve.ui.AnnotationInspector.prototype.getInsertionText = function () {
	Iif ( this.sourceMode ) {
		return OO.ui.resolveMsg( this.constructor.static.title );
	}
	return '';
};
 
/**
 * Get the annotation object to apply.
 *
 * This method is called when the inspector is closing, and should return the annotation to apply
 * to the text. If this method returns a falsey value like null, no annotation will be applied,
 * but existing annotations won't be removed either.
 *
 * @abstract
 * @return {ve.dm.Annotation} Annotation to apply
 */
ve.ui.AnnotationInspector.prototype.getAnnotation = null;
 
/**
 * Get an annotation object from a fragment.
 *
 * @abstract
 * @param {ve.dm.SurfaceFragment} fragment Surface fragment
 * @return {ve.dm.Annotation|null}
 */
ve.ui.AnnotationInspector.prototype.getAnnotationFromFragment = null;
 
/**
 * Get matching annotations within a fragment.
 *
 * @param {ve.dm.SurfaceFragment} fragment Fragment to get matching annotations within
 * @param {boolean} [all] Get annotations which only cover some of the fragment
 * @return {ve.dm.AnnotationSet} Matching annotations
 */
ve.ui.AnnotationInspector.prototype.getMatchingAnnotations = function ( fragment, all ) {
	var modelClasses = this.constructor.static.modelClasses;
 
	return fragment.getAnnotations( all ).filter( function ( annotation ) {
		return ve.isInstanceOfAny( annotation, modelClasses );
	} );
};
 
/**
 * @inheritdoc ve.ui.FragmentWindow
 */
ve.ui.AnnotationInspector.prototype.isEditing = function () {
	// If initialSelection isn't set yet, default to assume we are editing,
	// especially for the check in FragmentWindow#getSetupProcess
	return !this.initialSelection || !this.initialSelection.isCollapsed();
};
 
/**
 * Handle the inspector being setup.
 *
 * There are 4 scenarios:
 *
 * - Zero-length selection not near a word -> no change, text will be inserted on close
 * - Zero-length selection inside or adjacent to a word -> expand selection to cover word
 * - Selection covering non-annotated text -> trim selection to remove leading/trailing whitespace
 * - Selection covering annotated text -> expand selection to cover annotation
 *
 * @param {Object} [data] Inspector opening data
 * @param {boolean} [data.noExpand] Don't expand the selection when opening
 * @return {OO.ui.Process}
 */
ve.ui.AnnotationInspector.prototype.getSetupProcess = function ( data ) {
	return ve.ui.AnnotationInspector.super.prototype.getSetupProcess.call( this, data )
		.next( function () {
			var fragment = this.getFragment(),
				surfaceModel = fragment.getSurface(),
				// Partial annotations will be expanded later
				annotation = this.getMatchingAnnotations( fragment, true ).get( 0 );
 
			surfaceModel.pushStaging();
 
			// Only supports linear selections
			Iif ( !( this.initialFragment && this.initialFragment.getSelection() instanceof ve.dm.LinearSelection ) ) {
				return ve.createDeferred().reject().promise();
			}
 
			// Initialize range
			if ( !annotation ) {
				// No matching annotations found:
				// If collapsed and at a content offset, try to expand the selection
				if (
					fragment.getSelection().isCollapsed() &&
					fragment.getDocument().data.isContentOffset( fragment.getSelection().getRange().start )
				) {
					// Expand to nearest word
					if ( !data.noExpand ) {
						fragment = fragment.expandLinearSelection( 'word' );
						// If we expanded, check for matching annotations again
						if ( !fragment.getSelection().isCollapsed() ) {
							annotation = this.getMatchingAnnotations( fragment, true ).get( 0 );
						}
					}
					// TODO: We should review how getMatchingAnnotation works in light of the fact
					// that in the case of a collapsed range, the method falls back to retrieving
					// insertion annotations.
				} else {
					// New expanded selection: trim whitespace
					fragment = fragment.trimLinearSelection();
				}
 
				// Selection expanded, but still no annotation, create one from the selection
				if ( !fragment.getSelection().isCollapsed() && !annotation ) {
					this.isNew = true;
					annotation = this.getAnnotationFromFragment( fragment );
					Eif ( annotation ) {
						fragment.annotateContent( 'set', annotation );
					}
				}
			}
 
			// Existing annotation only partially selection: expand to cover annotation
			if ( annotation && !data.noExpand ) {
				fragment = fragment.expandLinearSelection( 'annotation', annotation );
			}
 
			// Update selection
			fragment.select();
			this.initialSelection = fragment.getSelection();
 
			// The initial annotation is the first matching annotation in the fragment
			this.initialAnnotation = this.getMatchingAnnotations( fragment, true ).get( 0 );
			var initialCoveringAnnotation = this.getMatchingAnnotations( fragment ).get( 0 );
			// Fallback to a default annotation
			if ( !this.initialAnnotation ) {
				this.isNew = true;
				this.initialAnnotation = this.getAnnotationFromFragment( fragment );
			} else if (
				initialCoveringAnnotation &&
				initialCoveringAnnotation.compareTo( this.initialAnnotation )
			) {
				// If the initial annotation doesn't cover the fragment, record this as we'll need
				// to forcefully apply it to the rest of the fragment later
				this.initialAnnotationIsCovering = true;
			}
 
			// Update fragment property
			this.fragment = fragment;
 
			// Duplicate calls from FragmentWindow#getSetupProcess after
			// changing the fragment
			this.actions.setMode( this.getMode() );
			// isEditing is true when we are applying a new annotation because a
			// stub is applied immediately, so use isNew instead
			Iif ( this.isNew && this.isReadOnly() ) {
				return ve.createDeferred().reject().promise();
			}
		}, this );
};
 
/**
 * @inheritdoc
 */
ve.ui.AnnotationInspector.prototype.getTeardownProcess = function ( data ) {
	data = data || {};
	return ve.ui.AnnotationInspector.super.prototype.getTeardownProcess.call( this, data )
		.first( function () {
			var inspector = this,
				insertionAnnotation = false,
				replace = false,
				annotation = this.getAnnotation(),
				remove = data.action === 'done' && this.shouldRemoveAnnotation(),
				surfaceModel = this.fragment.getSurface(),
				surfaceView = this.manager.getSurface().getView(),
				fragment = surfaceModel.getFragment( this.initialSelection, false ),
				isEditing = this.isEditing(),
				insertText = !remove && this.shouldInsertText();
 
			var annotations;
			var insertion;
 
			function clear() {
				// Clear all existing annotations
				annotations = inspector.getMatchingAnnotations( fragment, true ).get();
				for ( var i = 0, len = annotations.length; i < len; i++ ) {
					fragment.annotateContent( 'clear', annotations[ i ] );
				}
			}
 
			if ( remove ) {
				surfaceModel.popStaging();
				if ( !isEditing ) {
					return;
				}
				clear();
			} else {
				if ( data.action !== 'done' ) {
					surfaceModel.popStaging();
					Eif ( this.initialFragment ) {
						this.initialFragment.select();
					}
					return;
				}
				Eif ( annotation ) {
					// Check if the initial annotation has changed, or didn't cover the whole fragment
					// to begin with
					if (
						!this.initialAnnotationIsCovering ||
						!this.initialAnnotation ||
						!this.initialAnnotation.compareTo( annotation )
					) {
						replace = true;
					}
				}
				if ( replace || insertText ) {
					surfaceModel.popStaging();
					if ( insertText ) {
						insertion = this.getInsertionData();
						if ( insertion.length ) {
							fragment.insertContent( insertion, true );
							if ( !isEditing ) {
								// Move cursor to the end of the inserted content, even if back button is used
								this.initialFragment = fragment.collapseToEnd();
							} else E{
								this.initialFragment = fragment;
							}
						}
					}
					// If we are setting a new annotation, clear any annotations the inspector may have
					// applied up to this point. Otherwise keep them.
					Eif ( replace ) {
						clear();
						// Apply new annotation
						if ( fragment.getSelection().isCollapsed() ) {
							insertionAnnotation = true;
						} else {
							fragment.annotateContent( 'set', annotation );
						}
					}
				} else {
					surfaceModel.applyStaging();
				}
			}
 
			// HACK: ui.WindowAction unsets initialFragment in source mode,
			// so we can't rely on it existing.
			var selection;
			if ( this.initialFragment && ( !data.action || insertText ) ) {
				// Restore selection to what it was before we expanded it
				selection = this.initialFragment.getSelection();
			} else {
				selection = fragment.getSelection();
			}
 
			Eif ( data.action ) {
				// Place the selection after the inserted text. If the inserted content is actually an
				// element and not text, keep it selected, so that the context menu for it appears.
				Eif ( !( insertion && insertion.length && ve.dm.LinearData.static.isElementData( insertion[ 0 ] ) ) ) {
					surfaceModel.setSelection( selection );
				}
				// Update active annotations from model as the document may be deactivated
				surfaceView.updateActiveAnnotations( true );
				// Update previousActiveAnnotations so the annotation stays active
				// after re-activation
				surfaceView.previousActiveAnnotations = surfaceView.activeAnnotations;
				Iif ( OO.ui.isMobile() ) {
					// Restore context-only view on mobile
					surfaceView.deactivate( false, false, true );
				} else {
					// We can't rely on the selection being placed inside the annotation
					// so force it based on the model annotations. T265166
					surfaceView.selectAnnotation( function ( annView ) {
						return ve.isInstanceOfAny( annView.getModel(), inspector.constructor.static.modelClasses );
					} );
				}
			}
 
			if ( insertionAnnotation ) {
				surfaceModel.addInsertionAnnotations( annotation );
			}
		}, this )
		.next( function () {
			// Reset state
			this.initialSelection = null;
			this.initialAnnotation = null;
			this.initialAnnotationIsCovering = false;
			this.isNew = false;
		}, this );
};