| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.MWLinkAnnotationInspector = function VeUiMWLinkAnnotationInspector( config ) { |
| 18 |
|
| 19 |
ve.ui.MWLinkAnnotationInspector.super.call( this, ve.extendObject( { padded: false }, config ) ); |
| 20 |
|
| 21 |
this.$element.addClass( 've-ui-mwLinkAnnotationInspector' ); |
| 22 |
}; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
OO.inheritClass( ve.ui.MWLinkAnnotationInspector, ve.ui.LinkAnnotationInspector ); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
ve.ui.MWLinkAnnotationInspector.static.name = 'link'; |
| 31 |
|
| 32 |
ve.ui.MWLinkAnnotationInspector.static.modelClasses = [ |
| 33 |
ve.dm.MWExternalLinkAnnotation, |
| 34 |
ve.dm.MWInternalLinkAnnotation |
| 35 |
]; |
| 36 |
|
| 37 |
ve.ui.MWLinkAnnotationInspector.static.actions = [ |
| 38 |
...ve.ui.MWLinkAnnotationInspector.static.actions, |
| 39 |
{ |
| 40 |
action: 'convert', |
| 41 |
label: null, |
| 42 |
modes: [ 'edit', 'insert' ] |
| 43 |
} |
| 44 |
]; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
ve.ui.MWLinkAnnotationInspector.prototype.initialize = function () { |
| 52 |
|
| 53 |
this.allowProtocolInInternal = false; |
| 54 |
this.internalAnnotationInput = this.createInternalAnnotationInput(); |
| 55 |
this.externalAnnotationInput = this.createExternalAnnotationInput(); |
| 56 |
|
| 57 |
this.linkTypeIndex = new OO.ui.IndexLayout( { |
| 58 |
expanded: false, |
| 59 |
framed: false |
| 60 |
} ); |
| 61 |
|
| 62 |
this.linkTypeIndex.addTabPanels( [ |
| 63 |
new OO.ui.TabPanelLayout( 'internal', { |
| 64 |
label: mw.config.get( 'wgSiteName' ), |
| 65 |
expanded: false, |
| 66 |
scrollable: false, |
| 67 |
padded: true |
| 68 |
} ), |
| 69 |
new OO.ui.TabPanelLayout( 'external', { |
| 70 |
label: ve.msg( 'visualeditor-linkinspector-button-link-external' ), |
| 71 |
expanded: false, |
| 72 |
scrollable: false, |
| 73 |
padded: true |
| 74 |
} ) |
| 75 |
] ); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
ve.ui.MWLinkAnnotationInspector.super.prototype.initialize.call( this ); |
| 81 |
|
| 82 |
this.internalAnnotationField = this.annotationField; |
| 83 |
this.externalAnnotationField = new OO.ui.FieldLayout( |
| 84 |
this.externalAnnotationInput, |
| 85 |
{ |
| 86 |
align: 'top', |
| 87 |
label: ve.msg( 'visualeditor-linkinspector-title' ) |
| 88 |
} |
| 89 |
); |
| 90 |
|
| 91 |
this.onExternalLinkInputChangeDebounced = ve.debounce( this.onExternalLinkInputChange, 750 ); |
| 92 |
|
| 93 |
|
| 94 |
this.linkTypeIndex.connect( this, { set: 'onLinkTypeIndexSet' } ); |
| 95 |
this.labelInput.connect( this, { change: 'onLabelInputChange' } ); |
| 96 |
this.internalAnnotationInput.connect( this, { change: 'onInternalLinkChange' } ); |
| 97 |
this.externalAnnotationInput.connect( this, { change: 'onExternalLinkChange' } ); |
| 98 |
this.internalAnnotationInput.input.getResults().connect( this, { choose: 'onFormSubmit' } ); |
| 99 |
|
| 100 |
this.internalAnnotationInput.getTextInputWidget().connect( this, { |
| 101 |
change: 'onInternalLinkInputChange', |
| 102 |
enter: 'onLinkInputEnter' |
| 103 |
} ); |
| 104 |
this.externalAnnotationInput.getTextInputWidget().connect( this, { |
| 105 |
change: 'onExternalLinkInputChangeDebounced', |
| 106 |
enter: 'onLinkInputEnter' |
| 107 |
} ); |
| 108 |
|
| 109 |
this.externalAnnotationInput.connect( this, { change: 'onAnnotationInputChange' } ); |
| 110 |
|
| 111 |
this.internalAnnotationInput.input.results.connect( this, { |
| 112 |
add: 'onInternalLinkChangeResultsChange', |
| 113 |
|
| 114 |
|
| 115 |
choose: 'onInternalLinkSearchResultsChoose' |
| 116 |
} ); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
this.linkTypeIndex.$content.append( this.$otherActions ); |
| 121 |
this.linkTypeIndex.getTabPanel( 'internal' ).$element.append( this.internalAnnotationField.$element ); |
| 122 |
this.linkTypeIndex.getTabPanel( 'external' ).$element.append( this.externalAnnotationField.$element ); |
| 123 |
|
| 124 |
if ( OO.ui.isMobile() ) { |
| 125 |
this.linkTypeIndex.getTabPanel( 'internal' ).$element.prepend( this.labelField.$element ); |
| 126 |
} |
| 127 |
this.form.$element.empty().append( this.linkTypeIndex.$element ); |
| 128 |
if ( !OO.ui.isMobile() ) { |
| 129 |
this.externalAnnotationField.setLabel( null ); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
ve.ui.MWLinkAnnotationInspector.prototype.createInternalAnnotationInput = function () { |
| 137 |
return new ve.ui.MWInternalLinkAnnotationWidget(); |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
ve.ui.MWLinkAnnotationInspector.prototype.createExternalAnnotationInput = function () { |
| 144 |
return new ve.ui.MWExternalLinkAnnotationWidget(); |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
ve.ui.MWLinkAnnotationInspector.prototype.isExternal = function () { |
| 153 |
return this.linkTypeIndex.getCurrentTabPanelName() === 'external'; |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
ve.ui.MWLinkAnnotationInspector.prototype.onLabelInputChange = function () { |
| 162 |
if ( this.isActive && !this.trackedLabelInputChange ) { |
| 163 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'label-input' } ); |
| 164 |
this.trackedLabelInputChange = true; |
| 165 |
} |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
ve.ui.MWLinkAnnotationInspector.prototype.onInternalLinkChange = function () { |
| 174 |
this.updateActions(); |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
ve.ui.MWLinkAnnotationInspector.prototype.onInternalLinkChangeResultsChange = function () { |
| 184 |
this.updateSize(); |
| 185 |
}; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
ve.ui.MWLinkAnnotationInspector.prototype.onInternalLinkSearchResultsChoose = function () { |
| 193 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'search-pages-choose' } ); |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
ve.ui.MWLinkAnnotationInspector.prototype.onExternalLinkChange = function () { |
| 202 |
this.updateActions(); |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
ve.ui.MWLinkAnnotationInspector.prototype.onLinkInputEnter = function () { |
| 211 |
if ( this.annotationInput.getTextInputWidget().getValue().trim() === '' ) { |
| 212 |
this.executeAction( 'done' ); |
| 213 |
} |
| 214 |
this.annotationInput.getTextInputWidget().getValidity() |
| 215 |
.done( () => { |
| 216 |
this.executeAction( 'done' ); |
| 217 |
} ); |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
ve.ui.MWLinkAnnotationInspector.prototype.updateActions = function () { |
| 224 |
let msg = null; |
| 225 |
|
| 226 |
ve.ui.MWLinkAnnotationInspector.super.prototype.updateActions.call( this ); |
| 227 |
|
| 228 |
|
| 229 |
const content = this.fragment ? this.fragment.getText() : ''; |
| 230 |
const annotation = this.annotationInput.getAnnotation(); |
| 231 |
const href = annotation && annotation.getHref(); |
| 232 |
if ( href && ve.dm.MWMagicLinkNode.static.validateHref( content, href ) ) { |
| 233 |
const type = ve.dm.MWMagicLinkType.static.fromContent( content ).type; |
| 234 |
msg = 'visualeditor-linkinspector-convert-link-' + type.toLowerCase(); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
this.actions.forEach( null, ( action ) => { |
| 240 |
if ( action.getAction() === 'convert' ) { |
| 241 |
if ( msg ) { |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
action.setLabel( OO.ui.deferMsg( msg ) ); |
| 247 |
action.toggle( true ); |
| 248 |
} else { |
| 249 |
action.toggle( false ); |
| 250 |
} |
| 251 |
} |
| 252 |
} ); |
| 253 |
}; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
ve.ui.MWLinkAnnotationInspector.prototype.onInternalLinkInputChange = function ( value ) { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if ( this.internalAnnotationInput.getTextInputWidget().getValue() !== value ) { |
| 272 |
return; |
| 273 |
} |
| 274 |
if ( this.isActive && !this.trackedInternalLinkInputChange && !this.switchingLinkTypes ) { |
| 275 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'search-pages-input' } ); |
| 276 |
this.trackedInternalLinkInputChange = true; |
| 277 |
} |
| 278 |
if ( |
| 279 |
!this.allowProtocolInInternal && |
| 280 |
( /^(?:[a-z][a-z0-9$\-_@.&!*"'(),]*:)?\/\//i ).test( value.trim() ) |
| 281 |
) { |
| 282 |
this.linkTypeIndex.setTabPanel( 'external' ); |
| 283 |
|
| 284 |
this.externalAnnotationInput.getTextInputWidget().moveCursorToEnd(); |
| 285 |
} |
| 286 |
|
| 287 |
this.internalAnnotationInput.getTextInputWidget().getValidity() |
| 288 |
.then( |
| 289 |
() => { |
| 290 |
this.internalAnnotationField.setErrors( [] ); |
| 291 |
this.updateSize(); |
| 292 |
}, () => { |
| 293 |
this.internalAnnotationField.setErrors( [ ve.msg( 'visualeditor-linkinspector-illegal-title' ) ] ); |
| 294 |
this.updateSize(); |
| 295 |
} |
| 296 |
); |
| 297 |
|
| 298 |
}; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
ve.ui.MWLinkAnnotationInspector.prototype.onExternalLinkInputChange = function () { |
| 306 |
this.externalAnnotationInput.getValidity().then( |
| 307 |
() => { |
| 308 |
|
| 309 |
this.externalAnnotationField.setErrors( [] ); |
| 310 |
}, ( errortype ) => { |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
this.externalAnnotationField.setErrors( [ ve.msg( 'visualeditor-linkinspector-' + errortype ) ] ); |
| 315 |
if ( errortype === 'invalid-blocked' ) { |
| 316 |
|
| 317 |
this.actions.forEach( { actions: [ 'done', 'insert' ] }, ( action ) => { |
| 318 |
action.setDisabled( true ); |
| 319 |
} ); |
| 320 |
ve.track( 'activity.editCheckReliability', { action: 'link-blocked' } ); |
| 321 |
} |
| 322 |
} |
| 323 |
).always( () => { |
| 324 |
this.updateSize(); |
| 325 |
} ); |
| 326 |
|
| 327 |
if ( this.isActive && !this.trackedExternalLinkInputChange && !this.switchingLinkTypes ) { |
| 328 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'external-link-input' } ); |
| 329 |
this.trackedExternalLinkInputChange = true; |
| 330 |
} |
| 331 |
}; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
ve.ui.MWLinkAnnotationInspector.prototype.createAnnotationInput = function () { |
| 337 |
return this.isExternal() ? this.externalAnnotationInput : this.internalAnnotationInput; |
| 338 |
}; |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
ve.ui.MWLinkAnnotationInspector.prototype.getSetupProcess = function ( data ) { |
| 344 |
return ve.ui.MWLinkAnnotationInspector.super.prototype.getSetupProcess.call( this, data ) |
| 345 |
.next( () => { |
| 346 |
this.isReady = false; |
| 347 |
|
| 348 |
const isReadOnly = this.isReadOnly(); |
| 349 |
this.linkTypeIndex.setTabPanel( |
| 350 |
this.initialAnnotation instanceof ve.dm.MWExternalLinkAnnotation ? 'external' : 'internal' |
| 351 |
); |
| 352 |
this.annotationInput.setAnnotation( this.initialAnnotation ); |
| 353 |
this.internalAnnotationInput.setReadOnly( isReadOnly ); |
| 354 |
this.externalAnnotationInput.setReadOnly( isReadOnly ); |
| 355 |
|
| 356 |
this.trackedInternalLinkInputChange = false; |
| 357 |
this.trackedExternalLinkInputChange = false; |
| 358 |
this.isActive = true; |
| 359 |
} ); |
| 360 |
}; |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
ve.ui.MWLinkAnnotationInspector.prototype.getReadyProcess = function ( data ) { |
| 366 |
return ve.ui.MWLinkAnnotationInspector.super.prototype.getReadyProcess.call( this, data ) |
| 367 |
.next( () => { |
| 368 |
this.isReady = true; |
| 369 |
|
| 370 |
this.annotationInput.getTextInputWidget().focus(); |
| 371 |
} ); |
| 372 |
}; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
ve.ui.MWLinkAnnotationInspector.prototype.getActionProcess = function ( action ) { |
| 378 |
if ( action === 'convert' ) { |
| 379 |
return new OO.ui.Process( () => { |
| 380 |
this.close( { action: 'done', convert: true } ); |
| 381 |
} ); |
| 382 |
} |
| 383 |
return ve.ui.MWLinkAnnotationInspector.super.prototype.getActionProcess.call( this, action ); |
| 384 |
}; |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
ve.ui.MWLinkAnnotationInspector.prototype.getTeardownProcess = function ( data ) { |
| 390 |
let fragment; |
| 391 |
return ve.ui.MWLinkAnnotationInspector.super.prototype.getTeardownProcess.call( this, data ) |
| 392 |
.first( () => { |
| 393 |
|
| 394 |
fragment = this.getFragment(); |
| 395 |
|
| 396 |
this.isActive = false; |
| 397 |
} ) |
| 398 |
.next( () => { |
| 399 |
const selection = fragment && fragment.getSelection(); |
| 400 |
|
| 401 |
|
| 402 |
if ( data && data.convert && selection instanceof ve.dm.LinearSelection ) { |
| 403 |
const annotations = fragment.getDocument().data |
| 404 |
.getAnnotationsFromRange( selection.getRange() ) |
| 405 |
|
| 406 |
.filter( ( annotation ) => !/^link/.test( annotation.name ) ); |
| 407 |
const linearData = new ve.dm.ElementLinearData( annotations.store, [ |
| 408 |
{ |
| 409 |
type: 'link/mwMagic', |
| 410 |
attributes: { |
| 411 |
content: fragment.getText() |
| 412 |
} |
| 413 |
}, |
| 414 |
{ |
| 415 |
type: '/link/mwMagic' |
| 416 |
} |
| 417 |
] ); |
| 418 |
linearData.setAnnotationsAtOffset( 0, annotations ); |
| 419 |
fragment.insertContent( linearData.getData(), true ); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
this.allowProtocolInInternal = false; |
| 424 |
|
| 425 |
this.internalAnnotationInput.setAnnotation( null ); |
| 426 |
this.externalAnnotationInput.setAnnotation( null ); |
| 427 |
} ); |
| 428 |
}; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
ve.ui.MWLinkAnnotationInspector.prototype.onLinkTypeIndexSet = function ( tabPanel ) { |
| 436 |
const text = this.annotationInput.getTextInputWidget().getValue(), |
| 437 |
end = text.length, |
| 438 |
isExternal = this.isExternal(), |
| 439 |
inputHasProtocol = ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( text ); |
| 440 |
|
| 441 |
this.switchingLinkTypes = true; |
| 442 |
|
| 443 |
this.annotationInput = isExternal ? this.externalAnnotationInput : this.internalAnnotationInput; |
| 444 |
|
| 445 |
if ( OO.ui.isMobile() ) { |
| 446 |
tabPanel.$element.prepend( this.labelField.$element ); |
| 447 |
} |
| 448 |
|
| 449 |
this.updateSize(); |
| 450 |
|
| 451 |
|
| 452 |
if ( !isExternal && inputHasProtocol ) { |
| 453 |
this.allowProtocolInInternal = true; |
| 454 |
} |
| 455 |
|
| 456 |
this.annotationInput.getTextInputWidget().setValue( text ); |
| 457 |
if ( this.isReady ) { |
| 458 |
|
| 459 |
|
| 460 |
this.annotationInput.getTextInputWidget().focus(); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
this.annotationInput.getTextInputWidget().$input[ 0 ].setSelectionRange( 0, end ); |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
this.annotationInput.getTextInputWidget().setValidityFlag(); |
| 472 |
|
| 473 |
this.onAnnotationInputChange(); |
| 474 |
|
| 475 |
if ( this.isActive ) { |
| 476 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'panel-switch' } ); |
| 477 |
} |
| 478 |
|
| 479 |
this.switchingLinkTypes = false; |
| 480 |
}; |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
ve.ui.MWLinkAnnotationInspector.prototype.getAnnotationFromFragment = function ( fragment ) { |
| 491 |
const target = fragment.getText(), |
| 492 |
title = mw.Title.newFromText( target ); |
| 493 |
|
| 494 |
|
| 495 |
if ( ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( target ) ) { |
| 496 |
|
| 497 |
return this.newExternalLinkAnnotation( { |
| 498 |
type: 'link/mwExternal', |
| 499 |
attributes: { |
| 500 |
href: target |
| 501 |
} |
| 502 |
} ); |
| 503 |
} else if ( title ) { |
| 504 |
|
| 505 |
return this.newInternalLinkAnnotationFromTitle( title ); |
| 506 |
} else { |
| 507 |
|
| 508 |
|
| 509 |
return null; |
| 510 |
} |
| 511 |
}; |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
ve.ui.MWLinkAnnotationInspector.prototype.newInternalLinkAnnotationFromTitle = function ( title ) { |
| 518 |
return ve.dm.MWInternalLinkAnnotation.static.newFromTitle( title ); |
| 519 |
}; |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
ve.ui.MWLinkAnnotationInspector.prototype.newExternalLinkAnnotation = function ( element ) { |
| 526 |
return new ve.dm.MWExternalLinkAnnotation( element ); |
| 527 |
}; |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
ve.ui.MWLinkAnnotationInspector.prototype.getInsertionText = function () { |
| 533 |
|
| 534 |
const label = this.labelInput.getValue().trim(); |
| 535 |
if ( label ) { |
| 536 |
return label; |
| 537 |
} else if ( this.isNew && this.isExternal() ) { |
| 538 |
return ''; |
| 539 |
} else { |
| 540 |
return this.annotationInput.getTextInputWidget().getValue(); |
| 541 |
} |
| 542 |
}; |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
ve.ui.MWLinkAnnotationInspector.prototype.getInsertionData = function () { |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
if ( this.isNew && this.isExternal() && !this.labelInput.getValue().trim() ) { |
| 552 |
return [ |
| 553 |
{ |
| 554 |
type: 'link/mwNumberedExternal', |
| 555 |
attributes: { |
| 556 |
href: this.annotationInput.getHref() |
| 557 |
} |
| 558 |
}, |
| 559 |
{ type: '/link/mwNumberedExternal' } |
| 560 |
]; |
| 561 |
} else { |
| 562 |
return this.getInsertionText().split( '' ); |
| 563 |
} |
| 564 |
}; |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
ve.ui.windowFactory.register( ve.ui.MWLinkAnnotationInspector ); |