mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-25 14:56:20 +00:00
a5aeca3ff5
Objective: Refactor UI widgets, improve usability and accessibility of menus, general cleanup and style improvements. Extras: Fixed documentation in a few other files to make descriptions of jQuery event arguments more consistent, classes inherit correctly, and made use of the @cfg functionality in jsduck. Changes: .docs/config.json * Added window, HTMLDocument, HTMLElement, DocumentFragment and XMLHttpRequest to externals, so jsduck doesn't throw warnings when they are used demos/ve/index.php, modules/ve/test/index.php, VisualEditor.php * Moved widgets above tools (since tools use widgets) demos/ve/index.php * Refactored widget initialization to use options * Renamed variables to match widget names ve.init.mw.ViewPageTarget.css * Adjusted text sizes to make widgets work normally * Added margins for buttons in toolbar (since button widgets don't have any) * Removed styles for init buttons (button widgets now) ve.init.mw.ViewPageTarget.js * Switched to using button widgets (involved moving things around a bit) ve.ui.LinkInspector.js, ve.ui.MWLinkInspector.js * Renamed static property "inputWidget" to "linkTargetInputWidget" to better reflect the required base class for the properties value icons.ai, check.png, check.svg * Added "check" icon, used in menu right now to show which item is selected ve.ui.Icons-raster.css, ve.ui.Icons-vector.css * Added check icon * Removed :before pseudo selectors from most of the icon classes (not need by button tool anymore, makes them more reusable now) ve.ui.Tool.css * Adjusted drop down tool styles so menu appears below, instead of on top, of the label * Adjusted paragraph font size to better match actual content * Updated class names to still work with menu widget changes (items are their own widgets now) * Updated selectors as per changes in the structure of button tools ve.ui.Widget.css * Added styles for buttons and menu items * Adjusted menu styles ve.ui.*ButtonTool.js * Added config options argument passthrough ve.ui.ButtonTool.js * Moved var statement to the top inside constructor * Switched to using "a" tag to get cross-browser :active support * Added icon to inside of button to make icon styles more reusable * Removed disabled support (now provided by widget parent class) ve.ui.FormatDropDownTool.js * Updated options initialization to construct menu item objects * Modified handling of items to account for changes in menu and item classes * Optimized onUpdateState method a bit, adding early exit to inner loop ve.ui.ButtonTool.js, ve.ui.DropdownTool.js, ve.ui.Context.js, ve.ui.Frame, ve.ui.Tool.js, ve.ui.Widget.js * Added chain ability to non-getter methods ve.ui.DropdownTool.js * Removed items argument to constructor * Updated code as per changes in menu class * Fixed inconsistent naming of event handler methods * Removed item event handling (now handled by items directly) * Made use of this.$$ to ensure tool works in other frames ve.ui.Tool.js * Made tools inherit from widget * Moved trigger registry event handler to a method ve.ui.Context.js * Switched from using menu to contain toolbar to a simple wrapper ve.ui.js * Added get$$ method, a convenience function for binding jQuery to a specific document context ve.ui.*Widget.js * Switched to using a config options object instead of individual arguments * Added options * Factored out flags and labels into their own classes * Refactored value setting methods for inputs ve.ui.MenuWidget.js, ve.ui.MenuItemWidget.js * Broke items out into their own classes * Redesigned API * Updated code that uses these classes * Added support for keyboard interaction * Made items flash when selected (delaying the hiding of the menu for 200ms) ve.ui.LinkTargetInputWidget.js, ve.ui.MWLinkTargetInputWidget * Refactored annotation setting methods Change-Id: I7769bd5a5b79f1ab36f258ef9f2be583ca503ce6
180 lines
4.9 KiB
JavaScript
180 lines
4.9 KiB
JavaScript
/*!
|
|
* VisualEditor UserInterface LinkInspector class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* UserInterface link inspector.
|
|
*
|
|
* @class
|
|
* @extends ve.ui.Inspector
|
|
* @constructor
|
|
* @param context
|
|
*/
|
|
ve.ui.LinkInspector = function VeUiLinkInspector( context ) {
|
|
// Parent constructor
|
|
ve.ui.Inspector.call( this, context );
|
|
|
|
// Properties
|
|
this.context = context;
|
|
this.initialAnnotationHash = null;
|
|
this.isNewAnnotation = false;
|
|
this.targetInput = new this.constructor.static.linkTargetInputWidget( {
|
|
'$$': this.frame.$$, '$overlay': context.$overlay
|
|
} );
|
|
|
|
// Initialization
|
|
this.$form.append( this.targetInput.$ );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.inheritClass( ve.ui.LinkInspector, ve.ui.Inspector );
|
|
|
|
/* Static properties */
|
|
|
|
ve.ui.LinkInspector.static.icon = 'link';
|
|
|
|
ve.ui.LinkInspector.static.titleMessage = 'visualeditor-linkinspector-title';
|
|
|
|
ve.ui.LinkInspector.static.typePattern = /^link(\/|$)/;
|
|
|
|
ve.ui.LinkInspector.static.linkTargetInputWidget = ve.ui.LinkTargetInputWidget;
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Handle the inspector being initialized.
|
|
*
|
|
* 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-link text -> trim selection to remove leading/trailing whitespace
|
|
* * Selection covering link text -> expand selection to cover link
|
|
*
|
|
* @method
|
|
*/
|
|
ve.ui.LinkInspector.prototype.onInitialize = function () {
|
|
var fragment = this.context.getSurface().getModel().getFragment( null, true ),
|
|
annotation = this.getMatchingAnnotations( fragment ).get( 0 );
|
|
if ( !annotation ) {
|
|
if ( fragment.getRange().isCollapsed() ) {
|
|
// Expand to nearest word
|
|
fragment = fragment.expandRange( 'word' );
|
|
} else {
|
|
// Trim whitespace
|
|
fragment = fragment.trimRange();
|
|
}
|
|
if ( !fragment.getRange().isCollapsed() ) {
|
|
// Create annotation from selection
|
|
fragment.annotateContent(
|
|
'set', this.getAnnotationFromTarget( fragment.truncateRange( 255 ).getText() )
|
|
);
|
|
this.isNewAnnotation = true;
|
|
}
|
|
} else {
|
|
// Expand range to cover annotation
|
|
fragment = fragment.expandRange( 'annotation', annotation );
|
|
}
|
|
// Update selection
|
|
fragment.select();
|
|
};
|
|
|
|
/**
|
|
* Handle the inspector being opened.
|
|
*
|
|
* @method
|
|
*/
|
|
ve.ui.LinkInspector.prototype.onOpen = function () {
|
|
var fragment = this.context.getSurface().getModel().getFragment( null, true ),
|
|
annotation = this.getMatchingAnnotations( fragment ).get( 0 );
|
|
|
|
this.initialAnnotationHash = annotation && annotation.getHash();
|
|
this.targetInput.setAnnotation( annotation );
|
|
|
|
// Set focus on the location input
|
|
setTimeout( ve.bind( function () {
|
|
this.targetInput.$.focus().select();
|
|
}, this ) );
|
|
};
|
|
|
|
/**
|
|
* Handle the inspector being opened.
|
|
*
|
|
* @method
|
|
* @param {boolean} remove Annotation should be removed
|
|
*/
|
|
ve.ui.LinkInspector.prototype.onClose = function ( remove ) {
|
|
var i, len, annotations, selection,
|
|
insert = false,
|
|
undo = false,
|
|
clear = false,
|
|
set = false,
|
|
target = this.targetInput.getValue(),
|
|
annotation = this.targetInput.getAnnotation(),
|
|
surface = this.context.getSurface(),
|
|
fragment = surface.getModel().getFragment( this.initialSelection, false );
|
|
// Undefined annotation causes removal
|
|
if ( !annotation ) {
|
|
remove = true;
|
|
}
|
|
if ( remove ) {
|
|
clear = true;
|
|
} else {
|
|
if ( this.initialSelection.isCollapsed() ) {
|
|
insert = true;
|
|
}
|
|
if ( annotation.getHash() !== this.initialAnnotationHash ) {
|
|
if ( this.isNewAnnotation ) {
|
|
undo = true;
|
|
} else {
|
|
clear = true;
|
|
}
|
|
set = true;
|
|
}
|
|
}
|
|
if ( insert ) {
|
|
// Insert default text and select it
|
|
fragment = fragment.insertContent( target, false ).adjustRange( -target.length, 0 );
|
|
|
|
// Move cursor to the end of the inserted content
|
|
selection = new ve.Range( this.initialSelection.start + target.length );
|
|
}
|
|
if ( undo ) {
|
|
// Go back to before we added an annotation in an onInitialize handler
|
|
surface.execute( 'history', 'undo' );
|
|
}
|
|
if ( clear ) {
|
|
// Clear all existing annotations
|
|
annotations = this.getMatchingAnnotations( fragment ).get();
|
|
for ( i = 0, len = annotations.length; i < len; i++ ) {
|
|
fragment.annotateContent( 'clear', annotations[i] );
|
|
}
|
|
}
|
|
if ( set ) {
|
|
// Apply new annotation
|
|
fragment.annotateContent( 'set', annotation );
|
|
}
|
|
// Selection changes may have occured in the insertion and annotation hullabaloo - restore it
|
|
surface.execute( 'content', 'select', selection || new ve.Range( fragment.getRange().end ) );
|
|
// Reset state
|
|
this.isNewAnnotation = false;
|
|
};
|
|
|
|
/**
|
|
* Get an annotation object from a target.
|
|
*
|
|
* @method
|
|
* @param {string} target Link target
|
|
* @returns {ve.dm.LinkAnnotation}
|
|
*/
|
|
ve.ui.LinkInspector.prototype.getAnnotationFromTarget = function ( target ) {
|
|
return new ve.dm.LinkAnnotation( { 'href': target } );
|
|
};
|
|
|
|
/* Registration */
|
|
|
|
ve.ui.inspectorFactory.register( 'link', ve.ui.LinkInspector );
|