mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-16 19:09:29 +00:00
db3da5d16b
Formerly known as "The greatest commit in the history of the world*". * Within a 3 block radius of Drayton Park and Auburt Park, starting from July 30th at about 9pm or so. Bugs: * (bug 51404) Allow escaping out of the link inspector when in creation mode (no text is selected, text will be inserted based on link target) and the text input is empty * (bug 51065 and bug 51415) Keep model and view in sync when changing the link inspector's text input value and showing options in a menu * (bug 51523) Either restore selection at the time of close to what it was before opening the inspector (when using back) or to what it was before closing (might be changed by transactions processed during the close method) - this makes it simpler and more natural when clicking away from the link inspector, even when there are changes that must be saved by the link inspector on close Bonus: * Use only the light blue highlight color for menu widget items - the checkmark already displays the selected item, the dark blue is just masking the current highlight position and confusing the peoples * Remove links when the user deletes everything from the link inspector's text input and then closes the link inspector * Replace select menu's evil "silent" selectItem/highlightItem argument with a new method called initializeSelection which sets both selection and highlighting to an item without emitting events - this is needed when synchronizing the view with the model so the model isn't immediately told to change to a value it already has * Make the MWTitle lookup menu not flash like crazy as you type (this was caused by a copy-paste oversight overriding initializeLookupMenuSelection unnecessarily) Bug: 51404 Bug: 51065 Bug: 51415 Bug: 51523 Change-Id: I339d9253ad472c2f42c3179edc84a83d27561270
205 lines
5.8 KiB
JavaScript
205 lines
5.8 KiB
JavaScript
/*!
|
|
* VisualEditor UserInterface AnnotationInspector class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* Annotation inspector.
|
|
*
|
|
* @class
|
|
* @abstract
|
|
* @extends ve.ui.Inspector
|
|
*
|
|
* @constructor
|
|
* @param {ve.ui.Surface} surface
|
|
* @param {Object} [config] Config options
|
|
*/
|
|
ve.ui.AnnotationInspector = function VeUiAnnotationInspector( surface, config ) {
|
|
// Parent constructor
|
|
ve.ui.Inspector.call( this, surface, config );
|
|
|
|
// Properties
|
|
this.initialAnnotation = null;
|
|
this.initialAnnotationHash = null;
|
|
this.isNewAnnotation = false;
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.inheritClass( ve.ui.AnnotationInspector, ve.ui.Inspector );
|
|
|
|
/**
|
|
* Annotation models this inspector can edit.
|
|
*
|
|
* @static
|
|
* @inheritable
|
|
* @property {Function[]}
|
|
*/
|
|
ve.ui.AnnotationInspector.static.modelClasses = [];
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* 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
|
|
*
|
|
* @method
|
|
*/
|
|
ve.ui.AnnotationInspector.prototype.onSetup = function () {
|
|
var expandedFragment, trimmedFragment, truncatedFragment,
|
|
fragment = this.surface.getModel().getFragment( null, true ),
|
|
annotation = this.getMatchingAnnotations( fragment, true ).get( 0 );
|
|
|
|
// Parent method
|
|
ve.ui.Inspector.prototype.onSetup.call( this );
|
|
|
|
// Initialize range
|
|
if ( !annotation ) {
|
|
if ( fragment.getRange().isCollapsed() ) {
|
|
// Expand to nearest word
|
|
expandedFragment = fragment.expandRange( 'word' );
|
|
fragment = expandedFragment;
|
|
} else {
|
|
// Trim whitespace
|
|
trimmedFragment = fragment.trimRange();
|
|
fragment = trimmedFragment;
|
|
}
|
|
if ( !fragment.getRange().isCollapsed() ) {
|
|
// Create annotation from selection
|
|
truncatedFragment = fragment.truncateRange( 255 );
|
|
fragment = truncatedFragment;
|
|
annotation = this.getAnnotationFromText( fragment.getText() );
|
|
fragment.annotateContent( 'set', annotation );
|
|
this.isNewAnnotation = true;
|
|
}
|
|
} else {
|
|
// Expand range to cover annotation
|
|
expandedFragment = fragment.expandRange( 'annotation', annotation );
|
|
fragment = expandedFragment;
|
|
}
|
|
|
|
// Update selection
|
|
fragment.select();
|
|
};
|
|
|
|
/**
|
|
* Handle the inspector being opened.
|
|
*/
|
|
ve.ui.AnnotationInspector.prototype.onOpen = function () {
|
|
var fragment = this.surface.getModel().getFragment( null, true ),
|
|
// Note that we don't set the 'all' flag here so that any
|
|
// non-annotated content is annotated on close
|
|
initialAnnotation = this.getMatchingAnnotations( fragment ).get( 0 );
|
|
|
|
// Parent method
|
|
ve.ui.Inspector.prototype.onOpen.call( this );
|
|
|
|
// Initialization
|
|
this.initialAnnotation = initialAnnotation;
|
|
this.initialAnnotationHash = initialAnnotation ? ve.getHash( initialAnnotation ) : null;
|
|
};
|
|
|
|
/**
|
|
* Handle the inspector being closed.
|
|
*
|
|
* @param {string} action Action that caused the window to be closed
|
|
*/
|
|
ve.ui.AnnotationInspector.prototype.onClose = function ( action ) {
|
|
// Parent method
|
|
ve.ui.Inspector.prototype.onClose.call( this, action );
|
|
|
|
var i, len, annotations,
|
|
insert = false,
|
|
undo = false,
|
|
clear = false,
|
|
set = false,
|
|
target = this.targetInput.getValue(),
|
|
annotation = this.targetInput.getAnnotation(),
|
|
remove = target === '' || ( action === 'remove' && !!annotation ),
|
|
surfaceModel = this.surface.getModel(),
|
|
fragment = surfaceModel.getFragment( this.initialSelection, false ),
|
|
selection = surfaceModel.getSelection();
|
|
|
|
if ( remove ) {
|
|
clear = true;
|
|
} else {
|
|
if ( this.initialSelection.isCollapsed() ) {
|
|
insert = true;
|
|
}
|
|
if ( ve.getHash( annotation ) !== this.initialAnnotationHash ) {
|
|
if ( this.isNewAnnotation ) {
|
|
undo = true;
|
|
} else {
|
|
clear = true;
|
|
}
|
|
set = true;
|
|
}
|
|
}
|
|
if ( insert ) {
|
|
fragment.insertContent( target, false );
|
|
|
|
// Move cursor to the end of the inserted content, even if back button is used
|
|
this.previousSelection = new ve.Range( this.initialSelection.start + target.length );
|
|
}
|
|
if ( undo ) {
|
|
// Go back to before we added an annotation
|
|
this.surface.execute( 'history', 'undo' );
|
|
}
|
|
if ( clear ) {
|
|
// Clear all existing annotations
|
|
annotations = this.getMatchingAnnotations( fragment, true ).get();
|
|
for ( i = 0, len = annotations.length; i < len; i++ ) {
|
|
fragment.annotateContent( 'clear', annotations[i] );
|
|
}
|
|
}
|
|
if ( set && annotation ) {
|
|
// Apply new annotation
|
|
fragment.annotateContent( 'set', annotation );
|
|
}
|
|
if ( action === 'back' ) {
|
|
// Restore selection to what it was before we expanded it
|
|
selection = this.previousSelection;
|
|
}
|
|
this.surface.execute( 'content', 'select', selection );
|
|
// Reset state
|
|
this.isNewAnnotation = false;
|
|
};
|
|
|
|
/**
|
|
* Get an annotation object from text.
|
|
*
|
|
* @method
|
|
* @abstract
|
|
* @param {string} text Content text
|
|
* @returns {ve.dm.Annotation}
|
|
* @throws {Error} If not overriden in a subclass
|
|
*/
|
|
ve.ui.AnnotationInspector.prototype.getAnnotationFromText = function () {
|
|
throw new Error(
|
|
've.ui.AnnotationInspector.getAnnotationFromText not implemented in subclass'
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Get matching annotations within a fragment.
|
|
*
|
|
* @method
|
|
* @param {ve.dm.SurfaceFragment} fragment Fragment to get matching annotations within
|
|
* @param {boolean} [all] Get annotations which only cover some of the fragment
|
|
* @returns {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 ( annnotation ) {
|
|
return ve.isInstanceOfAny( annnotation, modelClasses );
|
|
} );
|
|
};
|