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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
|
/*!
* VisualEditor UserInterface LinkAnnotationInspector class.
*
* @copyright See AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Inspector for applying and editing labeled MediaWiki internal and external links.
*
* @class
* @extends ve.ui.LinkAnnotationInspector
*
* @constructor
* @param {Object} [config] Configuration options
*/
ve.ui.MWLinkAnnotationInspector = function VeUiMWLinkAnnotationInspector( config ) {
// Parent constructor
ve.ui.MWLinkAnnotationInspector.super.call( this, ve.extendObject( { padded: false }, config ) );
this.$element.addClass( 've-ui-mwLinkAnnotationInspector' );
};
/* Inheritance */
OO.inheritClass( ve.ui.MWLinkAnnotationInspector, ve.ui.LinkAnnotationInspector );
/* Static properties */
ve.ui.MWLinkAnnotationInspector.static.name = 'link';
ve.ui.MWLinkAnnotationInspector.static.modelClasses = [
ve.dm.MWExternalLinkAnnotation,
ve.dm.MWInternalLinkAnnotation
];
ve.ui.MWLinkAnnotationInspector.static.actions = [
...ve.ui.MWLinkAnnotationInspector.static.actions,
{
action: 'convert',
label: null, // see #updateActions
modes: [ 'edit', 'insert' ]
}
];
/* Methods */
/**
* @inheritdoc
*/
ve.ui.MWLinkAnnotationInspector.prototype.initialize = function () {
// Properties
this.allowProtocolInInternal = false;
this.internalAnnotationInput = this.createInternalAnnotationInput();
this.externalAnnotationInput = this.createExternalAnnotationInput();
this.linkTypeIndex = new OO.ui.IndexLayout( {
expanded: false,
framed: false
} );
this.linkTypeIndex.addTabPanels( [
new OO.ui.TabPanelLayout( 'internal', {
label: mw.config.get( 'wgSiteName' ),
expanded: false,
scrollable: false,
padded: true
} ),
new OO.ui.TabPanelLayout( 'external', {
label: ve.msg( 'visualeditor-linkinspector-button-link-external' ),
expanded: false,
scrollable: false,
padded: true
} )
] );
// Parent method
// Parent requires createAnnotationInput to be callable, but tries to move
// inputs in the DOM, so call this before we restructure the DOM.
ve.ui.MWLinkAnnotationInspector.super.prototype.initialize.call( this );
this.internalAnnotationField = this.annotationField;
this.externalAnnotationField = new OO.ui.FieldLayout(
this.externalAnnotationInput,
{
align: 'top',
label: ve.msg( 'visualeditor-linkinspector-title' )
}
);
this.onExternalLinkInputChangeDebounced = ve.debounce( this.onExternalLinkInputChange, 750 );
// Events
this.linkTypeIndex.connect( this, { set: 'onLinkTypeIndexSet' } );
this.labelInput.connect( this, { change: 'onLabelInputChange' } );
this.internalAnnotationInput.connect( this, { change: 'onInternalLinkChange' } );
this.externalAnnotationInput.connect( this, { change: 'onExternalLinkChange' } );
this.internalAnnotationInput.input.getResults().connect( this, { choose: 'onFormSubmit' } );
// Form submit only auto triggers on enter when there is one input
this.internalAnnotationInput.getTextInputWidget().connect( this, {
change: 'onInternalLinkInputChange',
enter: 'onLinkInputEnter'
} );
this.externalAnnotationInput.getTextInputWidget().connect( this, {
change: 'onExternalLinkInputChangeDebounced',
enter: 'onLinkInputEnter'
} );
// this.internalAnnotationInput is already bound by parent class
this.externalAnnotationInput.connect( this, { change: 'onAnnotationInputChange' } );
this.internalAnnotationInput.input.results.connect( this, {
add: 'onInternalLinkChangeResultsChange',
// Listening to remove causes a flicker, and is not required
// as 'add' is always trigger on a change too
choose: 'onInternalLinkSearchResultsChoose'
} );
// Initialization
// HACK: IndexLayout is absolutely positioned, so place actions inside it
this.linkTypeIndex.$content.append( this.$otherActions );
this.linkTypeIndex.getTabPanel( 'internal' ).$element.append( this.internalAnnotationField.$element );
this.linkTypeIndex.getTabPanel( 'external' ).$element.append( this.externalAnnotationField.$element );
// labelField gets moved between tabs when activated
if ( OO.ui.isMobile() ) {
this.linkTypeIndex.getTabPanel( 'internal' ).$element.prepend( this.labelField.$element );
}
this.form.$element.empty().append( this.linkTypeIndex.$element );
if ( !OO.ui.isMobile() ) {
this.externalAnnotationField.setLabel( null );
}
};
/**
* @return {ve.ui.MWInternalLinkAnnotationWidget}
*/
ve.ui.MWLinkAnnotationInspector.prototype.createInternalAnnotationInput = function () {
return new ve.ui.MWInternalLinkAnnotationWidget();
};
/**
* @return {ve.ui.MWExternalLinkAnnotationWidget}
*/
ve.ui.MWLinkAnnotationInspector.prototype.createExternalAnnotationInput = function () {
return new ve.ui.MWExternalLinkAnnotationWidget();
};
/**
* Check if the current input mode is for external links
*
* @return {boolean} Input mode is for external links
*/
ve.ui.MWLinkAnnotationInspector.prototype.isExternal = function () {
return this.linkTypeIndex.getCurrentTabPanelName() === 'external';
};
/**
* Handle change events on the label input
*
* @param {string} value
*/
ve.ui.MWLinkAnnotationInspector.prototype.onLabelInputChange = function () {
if ( this.isActive && !this.trackedLabelInputChange ) {
ve.track( 'activity.' + this.constructor.static.name, { action: 'label-input' } );
this.trackedLabelInputChange = true;
}
};
/**
* Handle change events on the internal link widget
*
* @param {ve.dm.MWInternalLinkAnnotation} annotation
*/
ve.ui.MWLinkAnnotationInspector.prototype.onInternalLinkChange = function () {
this.updateActions();
};
/**
* Handle list change events ('add') from the interal link's result widget
*
* @param {OO.ui.OptionWidget[]} items Added items
* @param {number} index Index of insertion point
*/
ve.ui.MWLinkAnnotationInspector.prototype.onInternalLinkChangeResultsChange = function () {
this.updateSize();
};
/**
* Handle choose events from the result widget
*
* @param {OO.ui.OptionWidget} item Chosen item
*/
ve.ui.MWLinkAnnotationInspector.prototype.onInternalLinkSearchResultsChoose = function () {
ve.track( 'activity.' + this.constructor.static.name, { action: 'search-pages-choose' } );
};
/**
* Handle change events on the external link widget
*
* @param {ve.dm.MWExternalLinkAnnotation} annotation
*/
ve.ui.MWLinkAnnotationInspector.prototype.onExternalLinkChange = function () {
this.updateActions();
};
/**
* Handle enter events on the external/internal link inputs
*
* @param {jQuery.Event} e Key press event
*/
ve.ui.MWLinkAnnotationInspector.prototype.onLinkInputEnter = function () {
if ( this.annotationInput.getTextInputWidget().getValue().trim() === '' ) {
this.executeAction( 'done' );
}
this.annotationInput.getTextInputWidget().getValidity()
.done( () => {
this.executeAction( 'done' );
} );
};
/**
* @inheritdoc
*/
ve.ui.MWLinkAnnotationInspector.prototype.updateActions = function () {
let msg = null;
ve.ui.MWLinkAnnotationInspector.super.prototype.updateActions.call( this );
// show/hide convert action
const content = this.fragment ? this.fragment.getText() : '';
const annotation = this.annotationInput.getAnnotation();
const href = annotation && annotation.getHref();
if ( href && ve.dm.MWMagicLinkNode.static.validateHref( content, href ) ) {
const type = ve.dm.MWMagicLinkType.static.fromContent( content ).type;
msg = 'visualeditor-linkinspector-convert-link-' + type.toLowerCase();
}
// Once we toggle the visibility of the ActionWidget, we can't filter
// it with `get` any more. So we have to use `forEach`:
this.actions.forEach( null, ( action ) => {
if ( action.getAction() === 'convert' ) {
if ( msg ) {
// The following messages are used here:
// * visualeditor-linkinspector-convert-link-isbn
// * visualeditor-linkinspector-convert-link-pmid
// * visualeditor-linkinspector-convert-link-rfc
action.setLabel( OO.ui.deferMsg( msg ) );
action.toggle( true );
} else {
action.toggle( false );
}
}
} );
};
/**
* Handle change events on the internal link widget's input
*
* @param {string} value Current value of input widget
*/
ve.ui.MWLinkAnnotationInspector.prototype.onInternalLinkInputChange = function ( value ) {
// If this looks like an external link, switch to the correct tabPanel.
// Note: We don't care here if it's a *valid* link, so we just
// check whether it looks like a URI -- i.e. whether it starts with
// something that appears to be a schema per RFC1630. Later the external
// link inspector will use getExternalLinkUrlProtocolsRegExp for validity
// checking.
// Note 2: RFC1630 might be too broad in practice. You don't really see
// schemas that use the full set of allowed characters, and we might get
// more false positives by checking for them.
// Note 3: We allow protocol-relative URIs here.
if ( this.internalAnnotationInput.getTextInputWidget().getValue() !== value ) {
return;
}
if ( this.isActive && !this.trackedInternalLinkInputChange && !this.switchingLinkTypes ) {
ve.track( 'activity.' + this.constructor.static.name, { action: 'search-pages-input' } );
this.trackedInternalLinkInputChange = true;
}
if (
!this.allowProtocolInInternal &&
( /^(?:[a-z][a-z0-9$\-_@.&!*"'(),]*:)?\/\//i ).test( value.trim() )
) {
this.linkTypeIndex.setTabPanel( 'external' );
// Changing tabPanel focuses and selects the input, so collapse the cursor back to the end.
this.externalAnnotationInput.getTextInputWidget().moveCursorToEnd();
}
this.internalAnnotationInput.getTextInputWidget().getValidity()
.then(
() => {
this.internalAnnotationField.setErrors( [] );
this.updateSize();
}, () => {
this.internalAnnotationField.setErrors( [ ve.msg( 'visualeditor-linkinspector-illegal-title' ) ] );
this.updateSize();
}
);
};
/**
* Handle change events on the external link widget's input
*
* @param {string} value Current value of input widget
*/
ve.ui.MWLinkAnnotationInspector.prototype.onExternalLinkInputChange = function () {
this.externalAnnotationInput.getValidity().then(
() => {
// clear any invalid-protocol errors
this.externalAnnotationField.setErrors( [] );
}, ( errortype ) => {
// Messages that can be used here:
// * visualeditor-linkinspector-invalid-blocked
// * visualeditor-linkinspector-invalid-external
this.externalAnnotationField.setErrors( [ ve.msg( 'visualeditor-linkinspector-' + errortype ) ] );
if ( errortype === 'invalid-blocked' ) {
// This has been quite async, so:
this.actions.forEach( { actions: [ 'done', 'insert' ] }, ( action ) => {
action.setDisabled( true );
} );
ve.track( 'activity.editCheckReliability', { action: 'link-blocked' } );
}
}
).always( () => {
this.updateSize();
} );
if ( this.isActive && !this.trackedExternalLinkInputChange && !this.switchingLinkTypes ) {
ve.track( 'activity.' + this.constructor.static.name, { action: 'external-link-input' } );
this.trackedExternalLinkInputChange = true;
}
};
/**
* @inheritdoc
*/
ve.ui.MWLinkAnnotationInspector.prototype.createAnnotationInput = function () {
return this.isExternal() ? this.externalAnnotationInput : this.internalAnnotationInput;
};
/**
* @inheritdoc
*/
ve.ui.MWLinkAnnotationInspector.prototype.getSetupProcess = function ( data ) {
return ve.ui.MWLinkAnnotationInspector.super.prototype.getSetupProcess.call( this, data )
.next( () => {
this.isReady = false;
const isReadOnly = this.isReadOnly();
this.linkTypeIndex.setTabPanel(
this.initialAnnotation instanceof ve.dm.MWExternalLinkAnnotation ? 'external' : 'internal'
);
this.annotationInput.setAnnotation( this.initialAnnotation );
this.internalAnnotationInput.setReadOnly( isReadOnly );
this.externalAnnotationInput.setReadOnly( isReadOnly );
this.trackedInternalLinkInputChange = false;
this.trackedExternalLinkInputChange = false;
this.isActive = true;
} );
};
/**
* @inheritdoc
*/
ve.ui.MWLinkAnnotationInspector.prototype.getReadyProcess = function ( data ) {
return ve.ui.MWLinkAnnotationInspector.super.prototype.getReadyProcess.call( this, data )
.next( () => {
this.isReady = true;
// Focus is skipped during setup. (T321026)
this.annotationInput.getTextInputWidget().focus();
} );
};
/**
* @inheritdoc
*/
ve.ui.MWLinkAnnotationInspector.prototype.getActionProcess = function ( action ) {
if ( action === 'convert' ) {
return new OO.ui.Process( () => {
this.close( { action: 'done', convert: true } );
} );
}
return ve.ui.MWLinkAnnotationInspector.super.prototype.getActionProcess.call( this, action );
};
/**
* @inheritdoc
*/
ve.ui.MWLinkAnnotationInspector.prototype.getTeardownProcess = function ( data ) {
let fragment;
return ve.ui.MWLinkAnnotationInspector.super.prototype.getTeardownProcess.call( this, data )
.first( () => {
// Save the original fragment for later.
fragment = this.getFragment();
this.isActive = false;
} )
.next( () => {
const selection = fragment && fragment.getSelection();
// Handle conversion to magic link.
if ( data && data.convert && selection instanceof ve.dm.LinearSelection ) {
const annotations = fragment.getDocument().data
.getAnnotationsFromRange( selection.getRange() )
// Remove link annotations
.filter( ( annotation ) => !/^link/.test( annotation.name ) );
const linearData = new ve.dm.ElementLinearData( annotations.store, [
{
type: 'link/mwMagic',
attributes: {
content: fragment.getText()
}
},
{
type: '/link/mwMagic'
}
] );
linearData.setAnnotationsAtOffset( 0, annotations );
fragment.insertContent( linearData.getData(), true );
}
// Clear dialog state.
this.allowProtocolInInternal = false;
// Make sure both inputs are cleared
this.internalAnnotationInput.setAnnotation( null );
this.externalAnnotationInput.setAnnotation( null );
} );
};
/**
* Handle set events from the linkTypeIndex layout
*
* @param {OO.ui.TabPanelLayout} tabPanel Current tabPanel
*/
ve.ui.MWLinkAnnotationInspector.prototype.onLinkTypeIndexSet = function ( tabPanel ) {
const text = this.annotationInput.getTextInputWidget().getValue(),
end = text.length,
isExternal = this.isExternal(),
inputHasProtocol = ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( text );
this.switchingLinkTypes = true;
this.annotationInput = isExternal ? this.externalAnnotationInput : this.internalAnnotationInput;
if ( OO.ui.isMobile() ) {
tabPanel.$element.prepend( this.labelField.$element );
}
this.updateSize();
// If the user manually switches to internal links with an external link in the input, remember this
if ( !isExternal && inputHasProtocol ) {
this.allowProtocolInInternal = true;
}
this.annotationInput.getTextInputWidget().setValue( text );
if ( this.isReady ) {
// Focussing an element that isn't visible yet triggers a
// bug in jQuery that prevents future focusses. (T321026)
this.annotationInput.getTextInputWidget().focus();
}
// Select entire link when switching, for ease of replacing entire contents.
// Most common case:
// 1. Inspector opened, internal-link shown with the selected-word prefilled
// 2. User clicks external link tab (unnecessary, because we'd auto-switch, but the user doesn't know that)
// 3. User pastes a link, intending to replace the existing prefilled link
this.annotationInput.getTextInputWidget().$input[ 0 ].setSelectionRange( 0, end );
// Focusing a TextInputWidget normally unsets validity. However, because
// we're kind of pretending this is the same input, just in a different
// mode, it doesn't make sense to the user that the focus behavior occurs.
this.annotationInput.getTextInputWidget().setValidityFlag();
this.onAnnotationInputChange();
if ( this.isActive ) {
ve.track( 'activity.' + this.constructor.static.name, { action: 'panel-switch' } );
}
this.switchingLinkTypes = false;
};
/**
* Gets an annotation object from a fragment.
*
* The type of link is automatically detected based on some crude heuristics.
*
* @param {ve.dm.SurfaceFragment} fragment Current selection
* @return {ve.dm.MWInternalLinkAnnotation|ve.dm.MWExternalLinkAnnotation|null}
*/
ve.ui.MWLinkAnnotationInspector.prototype.getAnnotationFromFragment = function ( fragment ) {
const target = fragment.getText(),
title = mw.Title.newFromText( target );
// Figure out if this is an internal or external link
if ( ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( target ) ) {
// External link
return this.newExternalLinkAnnotation( {
type: 'link/mwExternal',
attributes: {
href: target
}
} );
} else if ( title ) {
// Internal link
return this.newInternalLinkAnnotationFromTitle( title );
} else {
// Doesn't look like an external link and mw.Title considered it an illegal value,
// for an internal link.
return null;
}
};
/**
* @param {mw.Title} title The title to link to.
* @return {ve.dm.MWInternalLinkAnnotation} The annotation.
*/
ve.ui.MWLinkAnnotationInspector.prototype.newInternalLinkAnnotationFromTitle = function ( title ) {
return ve.dm.MWInternalLinkAnnotation.static.newFromTitle( title );
};
/**
* @param {Object} element
* @return {ve.dm.MWExternalLinkAnnotation} The annotation.
*/
ve.ui.MWLinkAnnotationInspector.prototype.newExternalLinkAnnotation = function ( element ) {
return new ve.dm.MWExternalLinkAnnotation( element );
};
/**
* @inheritdoc
*/
ve.ui.MWLinkAnnotationInspector.prototype.getInsertionText = function () {
// Prefer user input, not normalized annotation, to preserve case
const label = this.labelInput.getValue().trim();
if ( label ) {
return label;
} else if ( this.isNew && this.isExternal() ) {
return '';
} else {
return this.annotationInput.getTextInputWidget().getValue();
}
};
/**
* @inheritdoc
*/
ve.ui.MWLinkAnnotationInspector.prototype.getInsertionData = function () {
// If this is a new external link with no label, insert an autonumbered link instead of a link annotation
// (applying the annotation on this later does nothing because of disallowedAnnotationTypes).
// Otherwise call parent method to figure out the text to insert and annotate.
if ( this.isNew && this.isExternal() && !this.labelInput.getValue().trim() ) {
return [
{
type: 'link/mwNumberedExternal',
attributes: {
href: this.annotationInput.getHref()
}
},
{ type: '/link/mwNumberedExternal' }
];
} else {
return this.getInsertionText().split( '' );
}
};
// #getInsertionText call annotationInput#getHref, which returns the link title,
// so no custmisation is needed.
/* Registration */
ve.ui.windowFactory.register( ve.ui.MWLinkAnnotationInspector );
|