| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
( function () { |
| 21 |
const configData = require( './data.json' ), |
| 22 |
veactionToMode = { |
| 23 |
edit: 'visual', |
| 24 |
editsource: 'source' |
| 25 |
}, |
| 26 |
availableModes = []; |
| 27 |
let init = null, |
| 28 |
conf = null, |
| 29 |
tabMessages = null, |
| 30 |
pageExists = null, |
| 31 |
viewUrl = null, |
| 32 |
veEditUrl = null, |
| 33 |
tabPreference = null; |
| 34 |
let veEditSourceUrl, targetPromise, url, |
| 35 |
initialWikitext, oldId, |
| 36 |
isLoading, tempWikitextEditor, tempWikitextEditorData, |
| 37 |
$toolbarPlaceholder, $toolbarPlaceholderBar, |
| 38 |
contentTop, wasFloating, |
| 39 |
active = false, |
| 40 |
targetLoaded = false, |
| 41 |
plugins = [], |
| 42 |
welcomeDialogDisabled = false, |
| 43 |
educationPopupsDisabled = false, |
| 44 |
|
| 45 |
$targetContainer = null; |
| 46 |
|
| 47 |
if ( mw.config.get( 'wgMFMode' ) ) { |
| 48 |
mw.log.warn( 'Attempted to load desktop target on mobile.' ); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
function showLoading() { |
| 56 |
if ( isLoading ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
isLoading = true; |
| 61 |
|
| 62 |
$( 'html' ).addClass( 've-activated ve-loading' ); |
| 63 |
if ( !init.$loading ) { |
| 64 |
init.progressBar = new mw.libs.ve.ProgressBarWidget(); |
| 65 |
init.$loading = $( '<div>' ) |
| 66 |
.addClass( 've-init-mw-desktopArticleTarget-loading-overlay' ) |
| 67 |
.append( init.progressBar.$element ); |
| 68 |
} |
| 69 |
$( document ).on( 'keydown', onDocumentKeyDown ); |
| 70 |
|
| 71 |
$toolbarPlaceholderBar.append( init.$loading ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
function incrementLoadingProgress() { |
| 80 |
init.progressBar.incrementLoadingProgress(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
function clearLoading() { |
| 87 |
init.progressBar.clearLoading(); |
| 88 |
isLoading = false; |
| 89 |
$( document ).off( 'keydown', onDocumentKeyDown ); |
| 90 |
$( 'html' ).removeClass( 've-loading' ); |
| 91 |
if ( init.$loading ) { |
| 92 |
init.$loading.detach(); |
| 93 |
} |
| 94 |
|
| 95 |
if ( tempWikitextEditor ) { |
| 96 |
teardownTempWikitextEditor(); |
| 97 |
} |
| 98 |
hideToolbarPlaceholder(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
function onWindowScroll() { |
| 107 |
const scrollTop = $( document.documentElement ).scrollTop(); |
| 108 |
const floating = scrollTop > contentTop; |
| 109 |
if ( floating !== wasFloating ) { |
| 110 |
const width = $targetContainer.outerWidth(); |
| 111 |
$toolbarPlaceholder.toggleClass( 've-init-mw-desktopArticleTarget-toolbarPlaceholder-floating', floating ); |
| 112 |
$toolbarPlaceholderBar.css( 'width', width ); |
| 113 |
wasFloating = floating; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
const onWindowScrollListener = mw.util.throttle( onWindowScroll, 250 ); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
function showToolbarPlaceholder() { |
| 123 |
if ( !$toolbarPlaceholder ) { |
| 124 |
|
| 125 |
|
| 126 |
$toolbarPlaceholder = $( '<div>' ).addClass( 've-init-mw-desktopArticleTarget-toolbarPlaceholder' ); |
| 127 |
$toolbarPlaceholderBar = $( '<div>' ).addClass( 've-init-mw-desktopArticleTarget-toolbarPlaceholder-bar' ); |
| 128 |
$toolbarPlaceholder.append( $toolbarPlaceholderBar ); |
| 129 |
} |
| 130 |
|
| 131 |
contentTop = $targetContainer.offset().top; |
| 132 |
wasFloating = null; |
| 133 |
onWindowScroll(); |
| 134 |
|
| 135 |
const scrollTopBefore = $( document.documentElement ).scrollTop(); |
| 136 |
|
| 137 |
$targetContainer.prepend( $toolbarPlaceholder ); |
| 138 |
|
| 139 |
window.addEventListener( 'scroll', onWindowScrollListener, { passive: true } ); |
| 140 |
|
| 141 |
if ( wasFloating ) { |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
window.scrollTo( 0, scrollTopBefore + $toolbarPlaceholder.outerHeight() ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
setTimeout( () => { |
| 150 |
$toolbarPlaceholder.addClass( 've-init-mw-desktopArticleTarget-toolbarPlaceholder-open' ); |
| 151 |
} ); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
function hideToolbarPlaceholder() { |
| 158 |
if ( $toolbarPlaceholder ) { |
| 159 |
window.removeEventListener( 'scroll', onWindowScrollListener ); |
| 160 |
$toolbarPlaceholder.detach(); |
| 161 |
$toolbarPlaceholder.removeClass( 've-init-mw-desktopArticleTarget-toolbarPlaceholder-open' ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
function setupTempWikitextEditor( data ) { |
| 171 |
let wikitext = data.content; |
| 172 |
|
| 173 |
|
| 174 |
if ( wikitext ) { |
| 175 |
wikitext += '\n'; |
| 176 |
} |
| 177 |
tempWikitextEditor = new mw.libs.ve.MWTempWikitextEditorWidget( { value: wikitext } ); |
| 178 |
tempWikitextEditorData = data; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
$( '.ve-init-mw-desktopArticleTarget-targetContainer #firstHeading' ).addClass( 've-init-mw-desktopArticleTarget-uneditableContent' ); |
| 183 |
$( '#mw-content-text' ) |
| 184 |
.before( tempWikitextEditor.$element ) |
| 185 |
.addClass( 'oo-ui-element-hidden' ); |
| 186 |
$( 'html' ).addClass( 've-tempSourceEditing' ).removeClass( 've-loading' ); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
tempWikitextEditor.adjustSize().moveCursorToStart(); |
| 191 |
ve.track( 'editAttemptStep', { action: 'ready', mode: 'source', platform: 'desktop' } ); |
| 192 |
mw.libs.ve.tempWikitextEditor = tempWikitextEditor; |
| 193 |
mw.hook( 've.wikitextInteractive' ).fire(); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
function syncTempWikitextEditor() { |
| 200 |
let wikitext = tempWikitextEditor.getValue(); |
| 201 |
|
| 202 |
|
| 203 |
if ( wikitext.slice( -1 ) === '\n' ) { |
| 204 |
wikitext = wikitext.slice( 0, -1 ); |
| 205 |
} |
| 206 |
|
| 207 |
if ( wikitext !== tempWikitextEditorData.content ) { |
| 208 |
|
| 209 |
|
| 210 |
tempWikitextEditorData.content = wikitext; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
tempWikitextEditorData.fromEditedState = true; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
tempWikitextEditorData.initialSourceRange = tempWikitextEditor.getRange(); |
| 219 |
|
| 220 |
tempWikitextEditor.$element.prop( 'readonly', true ); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
function teardownTempWikitextEditor() { |
| 227 |
|
| 228 |
tempWikitextEditor.$element.remove(); |
| 229 |
mw.libs.ve.tempWikitextEditor = tempWikitextEditor = null; |
| 230 |
tempWikitextEditorData = null; |
| 231 |
|
| 232 |
$( '#mw-content-text' ).removeClass( 'oo-ui-element-hidden' ); |
| 233 |
$( 'html' ).removeClass( 've-tempSourceEditing' ); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
function abortLoading() { |
| 240 |
$( 'html' ).removeClass( 've-activated' ); |
| 241 |
active = false; |
| 242 |
updateTabs( false ); |
| 243 |
|
| 244 |
if ( $( '#ca-view a' ).length ) { |
| 245 |
history.pushState( { tag: 'visualeditor' }, '', $( '#ca-view a' ).attr( 'href' ) ); |
| 246 |
} |
| 247 |
clearLoading(); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
function onDocumentKeyDown( e ) { |
| 256 |
if ( e.which === 27 ) { |
| 257 |
abortLoading(); |
| 258 |
e.preventDefault(); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
function parseSection( section ) { |
| 272 |
|
| 273 |
if ( section && /^(new|\d+|T-\d+)$/.test( section ) ) { |
| 274 |
return section; |
| 275 |
} |
| 276 |
return null; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
function getTarget( mode, section ) { |
| 288 |
if ( !targetPromise ) { |
| 289 |
|
| 290 |
|
| 291 |
targetPromise = mw.loader.using( 'ext.visualEditor.targetLoader' ) |
| 292 |
.then( () => { |
| 293 |
mw.libs.ve.targetLoader.addPlugin( |
| 294 |
|
| 295 |
|
| 296 |
() => mw.loader.using( conf.preloadModules ).catch( |
| 297 |
() => $.Deferred().resolve() |
| 298 |
) |
| 299 |
); |
| 300 |
|
| 301 |
|
| 302 |
[ |
| 303 |
'ext.visualEditor.desktopArticleTarget', |
| 304 |
|
| 305 |
...plugins |
| 306 |
].forEach( mw.libs.ve.targetLoader.addPlugin ); |
| 307 |
plugins = []; |
| 308 |
return mw.libs.ve.targetLoader.loadModules( mode ); |
| 309 |
} ) |
| 310 |
.then( () => { |
| 311 |
if ( !active ) { |
| 312 |
|
| 313 |
|
| 314 |
targetPromise = null; |
| 315 |
return $.Deferred().reject().promise(); |
| 316 |
} |
| 317 |
|
| 318 |
const target = ve.init.mw.targetFactory.create( |
| 319 |
conf.contentModels[ mw.config.get( 'wgPageContentModel' ) ], { |
| 320 |
modes: availableModes, |
| 321 |
defaultMode: mode |
| 322 |
} |
| 323 |
); |
| 324 |
target.on( 'deactivate', () => { |
| 325 |
active = false; |
| 326 |
updateTabs( false ); |
| 327 |
} ); |
| 328 |
target.on( 'reactivate', () => { |
| 329 |
url = new URL( location.href ); |
| 330 |
activateTarget( |
| 331 |
getEditModeFromUrl( url ), |
| 332 |
parseSection( url.searchParams.get( 'section' ) ) |
| 333 |
); |
| 334 |
} ); |
| 335 |
target.setContainer( $targetContainer ); |
| 336 |
targetLoaded = true; |
| 337 |
return target; |
| 338 |
}, ( e ) => { |
| 339 |
mw.log.warn( 'VisualEditor failed to load: ' + e ); |
| 340 |
return $.Deferred().reject( e ).promise(); |
| 341 |
} ); |
| 342 |
} |
| 343 |
|
| 344 |
targetPromise.then( ( target ) => { |
| 345 |
target.section = section; |
| 346 |
} ); |
| 347 |
|
| 348 |
return targetPromise; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
function trackActivateStart( initData, linkUrl ) { |
| 357 |
if ( !linkUrl ) { |
| 358 |
linkUrl = url; |
| 359 |
} |
| 360 |
if ( linkUrl.searchParams.get( 'wvprov' ) === 'sticky-header' ) { |
| 361 |
initData.mechanism += '-sticky-header'; |
| 362 |
} |
| 363 |
ve.track( 'trace.activate.enter', { mode: initData.mode } ); |
| 364 |
initData.action = 'init'; |
| 365 |
initData.integration = 'page'; |
| 366 |
ve.track( 'editAttemptStep', initData ); |
| 367 |
mw.libs.ve.activationStart = ve.now(); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
function getTabMessage( tabMsg ) { |
| 377 |
let tabMsgKey = tabMessages[ tabMsg ]; |
| 378 |
const skinMsgKeys = { |
| 379 |
edit: 'edit', |
| 380 |
create: 'create', |
| 381 |
editlocaldescription: 'edit-local', |
| 382 |
createlocaldescription: 'create-local' |
| 383 |
}; |
| 384 |
const key = skinMsgKeys[ tabMsg ]; |
| 385 |
if ( !tabMsgKey && key ) { |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
tabMsgKey = mw.config.get( 'skin' ) + '-view-' + key; |
| 394 |
if ( !mw.message( tabMsgKey ).exists() ) { |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
tabMsgKey = 'skin-view-' + key; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
const msg = mw.message( tabMsgKey ); |
| 405 |
if ( !msg.isParseable() ) { |
| 406 |
mw.log.warn( 'VisualEditor: MediaWiki:' + tabMsgKey + ' contains unsupported syntax. ' + |
| 407 |
'https://www.mediawiki.org/wiki/Manual:Messages_API#Feature_support_in_JavaScript' ); |
| 408 |
return undefined; |
| 409 |
} |
| 410 |
return msg.text(); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
function setEditorPreference( editor ) { |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
if ( !init.isVisualAvailable ) { |
| 425 |
return $.Deferred().resolve().promise(); |
| 426 |
} |
| 427 |
|
| 428 |
if ( editor !== 'visualeditor' && editor !== 'wikitext' ) { |
| 429 |
throw new Error( 'setEditorPreference called with invalid option: ', editor ); |
| 430 |
} |
| 431 |
|
| 432 |
let key = pageExists ? 'edit' : 'create', |
| 433 |
sectionKey = 'editsection'; |
| 434 |
|
| 435 |
if ( |
| 436 |
mw.config.get( 'wgVisualEditorConfig' ).singleEditTab && |
| 437 |
tabPreference === 'remember-last' |
| 438 |
) { |
| 439 |
if ( $( '#ca-view-foreign' ).length ) { |
| 440 |
key += 'localdescription'; |
| 441 |
} |
| 442 |
if ( editor === 'wikitext' ) { |
| 443 |
key += 'source'; |
| 444 |
sectionKey += 'source'; |
| 445 |
} |
| 446 |
|
| 447 |
$( '#ca-edit a' ).text( getTabMessage( key ) ); |
| 448 |
$( '.mw-editsection a' ).text( getTabMessage( sectionKey ) ); |
| 449 |
} |
| 450 |
|
| 451 |
mw.cookie.set( 'VEE', editor, { path: '/', expires: 30 * 86400, prefix: '' } ); |
| 452 |
|
| 453 |
|
| 454 |
if ( |
| 455 |
mw.user.isNamed() && |
| 456 |
mw.user.options.get( 'visualeditor-editor' ) !== editor |
| 457 |
) { |
| 458 |
|
| 459 |
return new mw.Api().saveOption( 'visualeditor-editor', editor ).then( () => { |
| 460 |
mw.user.options.set( 'visualeditor-editor', editor ); |
| 461 |
} ); |
| 462 |
} |
| 463 |
return $.Deferred().resolve().promise(); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
function updateTabs( editing, mode, isNewSection ) { |
| 474 |
let $tab; |
| 475 |
|
| 476 |
if ( editing ) { |
| 477 |
if ( isNewSection ) { |
| 478 |
$tab = $( '#ca-addsection' ); |
| 479 |
} else if ( $( '#ca-ve-edit' ).length ) { |
| 480 |
if ( !mode || mode === 'visual' ) { |
| 481 |
$tab = $( '#ca-ve-edit' ); |
| 482 |
} else { |
| 483 |
$tab = $( '#ca-edit' ); |
| 484 |
} |
| 485 |
} else { |
| 486 |
|
| 487 |
$tab = $( '#ca-edit' ); |
| 488 |
} |
| 489 |
} else { |
| 490 |
$tab = $( '#ca-view' ); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
$( '#p-views' ).find( 'li.selected' ).removeClass( 'selected' ); |
| 496 |
|
| 497 |
|
| 498 |
$( '#ca-edit, #ca-ve-edit, #ca-addsection' ).not( $tab ).removeClass( 'selected' ); |
| 499 |
|
| 500 |
$tab.addClass( 'selected' ); |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
function scrollToSection( section ) { |
| 511 |
if ( section === '0' || section === 'new' ) { |
| 512 |
return; |
| 513 |
} |
| 514 |
|
| 515 |
let $heading; |
| 516 |
$( '#mw-content-text .mw-editsection a:not( .mw-editsection-visualeditor )' ).each( ( i, el ) => { |
| 517 |
const linkUrl = new URL( el.href ); |
| 518 |
if ( section === parseSection( linkUrl.searchParams.get( 'section' ) ) ) { |
| 519 |
$heading = $( el ).closest( '.mw-heading, h1, h2, h3, h4, h5, h6' ); |
| 520 |
return false; |
| 521 |
} |
| 522 |
} ); |
| 523 |
|
| 524 |
if ( !$heading || !$heading.length ) { |
| 525 |
return; |
| 526 |
} |
| 527 |
|
| 528 |
let offset = 0; |
| 529 |
const enableVisualSectionEditing = mw.config.get( 'wgVisualEditorConfig' ).enableVisualSectionEditing; |
| 530 |
if ( enableVisualSectionEditing === true || enableVisualSectionEditing === 'desktop' ) { |
| 531 |
|
| 532 |
|
| 533 |
offset = $( '#mw-content-text' ).offset().top; |
| 534 |
} else { |
| 535 |
|
| 536 |
offset = parseInt( $heading.css( 'margin-top' ) ) + $toolbarPlaceholder.outerHeight(); |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
const supportsSmoothScroll = 'scrollBehavior' in document.documentElement.style; |
| 544 |
const newScrollTop = $heading.offset().top - offset; |
| 545 |
if ( supportsSmoothScroll ) { |
| 546 |
window.scroll( { |
| 547 |
top: newScrollTop, |
| 548 |
behavior: 'smooth' |
| 549 |
} ); |
| 550 |
} else { |
| 551 |
|
| 552 |
|
| 553 |
const scrollContainer = document.documentElement; |
| 554 |
|
| 555 |
$( scrollContainer ).animate( { |
| 556 |
scrollTop: newScrollTop |
| 557 |
} ); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
function activateTarget( mode, section, tPromise, modified ) { |
| 577 |
let dataPromise; |
| 578 |
|
| 579 |
updateTabs( true, mode, section === 'new' ); |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
if ( !targetLoaded ) { |
| 585 |
|
| 586 |
|
| 587 |
dataPromise = mw.loader.using( 'ext.visualEditor.targetLoader' ) |
| 588 |
.then( () => mw.libs.ve.targetLoader.requestPageData( mode, mw.config.get( 'wgRelevantPageName' ), { |
| 589 |
sessionStore: true, |
| 590 |
section: section, |
| 591 |
oldId: oldId, |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
targetName: 'mwTarget', |
| 596 |
modified: modified, |
| 597 |
editintro: url.searchParams.get( 'editintro' ), |
| 598 |
preload: url.searchParams.get( 'preload' ), |
| 599 |
preloadparams: mw.util.getArrayParam( 'preloadparams', url.searchParams ), |
| 600 |
|
| 601 |
wikitext: mode === 'visual' && modified ? $( '#wpTextbox1' ).textSelection( 'getContents' ) : undefined |
| 602 |
} ) ); |
| 603 |
|
| 604 |
dataPromise |
| 605 |
.then( ( response ) => { |
| 606 |
if ( |
| 607 |
|
| 608 |
isLoading && |
| 609 |
|
| 610 |
mode === 'source' && section !== 'new' && |
| 611 |
|
| 612 |
!( response.visualeditor && response.visualeditor.recovered ) |
| 613 |
) { |
| 614 |
setupTempWikitextEditor( response.visualeditor ); |
| 615 |
} |
| 616 |
} ) |
| 617 |
.then( incrementLoadingProgress ); |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
showToolbarPlaceholder(); |
| 622 |
mw.hook( 've.activationStart' ).fire(); |
| 623 |
|
| 624 |
let visibleSection = null; |
| 625 |
let visibleSectionOffset = null; |
| 626 |
if ( section === null ) { |
| 627 |
let firstVisibleEditSection = null; |
| 628 |
$( '#firstHeading, #mw-content-text .mw-editsection' ).each( ( i, el ) => { |
| 629 |
const top = el.getBoundingClientRect().top; |
| 630 |
if ( top > 0 ) { |
| 631 |
firstVisibleEditSection = el; |
| 632 |
|
| 633 |
return false; |
| 634 |
} |
| 635 |
} ); |
| 636 |
|
| 637 |
if ( firstVisibleEditSection && firstVisibleEditSection.id !== 'firstHeading' ) { |
| 638 |
const firstVisibleSectionLink = firstVisibleEditSection.querySelector( 'a' ); |
| 639 |
const linkUrl = new URL( firstVisibleSectionLink.href ); |
| 640 |
visibleSection = parseSection( linkUrl.searchParams.get( 'section' ) ); |
| 641 |
|
| 642 |
const firstVisibleHeading = $( firstVisibleEditSection ).closest( '.mw-heading, h1, h2, h3, h4, h5, h6' )[ 0 ]; |
| 643 |
visibleSectionOffset = firstVisibleHeading.getBoundingClientRect().top; |
| 644 |
} |
| 645 |
} else if ( mode === 'visual' ) { |
| 646 |
scrollToSection( section ); |
| 647 |
} |
| 648 |
|
| 649 |
showLoading( mode ); |
| 650 |
incrementLoadingProgress(); |
| 651 |
active = true; |
| 652 |
|
| 653 |
tPromise = tPromise || getTarget( mode, section ); |
| 654 |
tPromise |
| 655 |
.then( ( target ) => { |
| 656 |
target.visibleSection = visibleSection; |
| 657 |
target.visibleSectionOffset = visibleSectionOffset; |
| 658 |
|
| 659 |
incrementLoadingProgress(); |
| 660 |
|
| 661 |
target.setDefaultMode( mode ); |
| 662 |
|
| 663 |
if ( tempWikitextEditor ) { |
| 664 |
syncTempWikitextEditor(); |
| 665 |
} |
| 666 |
|
| 667 |
const deactivating = target.deactivatingDeferred || $.Deferred().resolve(); |
| 668 |
return deactivating.then( () => { |
| 669 |
target.currentUrl = new URL( location.href ); |
| 670 |
const activatePromise = target.activate( dataPromise ); |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
target.toolbarSetupDeferred.done( () => { |
| 675 |
hideToolbarPlaceholder(); |
| 676 |
} ); |
| 677 |
|
| 678 |
return activatePromise; |
| 679 |
} ); |
| 680 |
} ) |
| 681 |
.then( () => { |
| 682 |
if ( mode === 'visual' ) { |
| 683 |
|
| 684 |
ve.track( 'editAttemptStep', { action: 'ready', mode: mode } ); |
| 685 |
} else if ( !tempWikitextEditor ) { |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
ve.track( 'editAttemptStep', { action: 'ready', mode: mode } ); |
| 690 |
mw.hook( 've.wikitextInteractive' ).fire(); |
| 691 |
} |
| 692 |
ve.track( 'editAttemptStep', { action: 'loaded', mode: mode } ); |
| 693 |
} ) |
| 694 |
.always( clearLoading ); |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
function activatePageTarget( mode, section, modified, linkUrl ) { |
| 705 |
trackActivateStart( { type: 'page', mechanism: mw.config.get( 'wgArticleId' ) ? 'click' : 'new', mode: mode }, linkUrl ); |
| 706 |
|
| 707 |
if ( !active ) { |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
history.replaceState( { tag: 'visualeditor' }, '', url ); |
| 712 |
|
| 713 |
|
| 714 |
history.pushState( { tag: 'visualeditor' }, '', linkUrl || ( mode === 'source' ? veEditSourceUrl : veEditUrl ) ); |
| 715 |
|
| 716 |
url = linkUrl || veEditUrl; |
| 717 |
|
| 718 |
activateTarget( mode, section, undefined, modified ); |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
function getLastEditor() { |
| 728 |
|
| 729 |
let editor = mw.cookie.get( 'VEE', '' ); |
| 730 |
… |
| 731 |
if ( |
| 732 |
n, |
| 733 |
mw.user.isNamed() || |
| 734 |
or |
| 735 |
!editor || |
| 736 |
|
| 737 |
!( editor === 'visualeditor' || editor === 'wikitext' ) |
| 738 |
) { |
| 739 |
editor = mw.user.options.get( 'visualeditor-editor' ); |
| 740 |
} |
| 741 |
return editor; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
function getEditPageEditor() { |
| 752 |
|
| 753 |
|
| 754 |
const isRedLink = !!+url.searchParams.get( 'redlink' ); |
| 755 |
|
| 756 |
|
| 757 |
if ( !mw.config.get( 'wgVisualEditorConfig' ).singleEditTab && !isRedLink ) { |
| 758 |
return 'wikitext'; |
| 759 |
} |
| 760 |
|
| 761 |
switch ( tabPreference ) { |
| 762 |
case 'prefer-ve': |
| 763 |
return 'visualeditor'; |
| 764 |
case 'prefer-wt': |
| 765 |
return 'wikitext'; |
| 766 |
case 'multi-tab': |
| 767 |
|
| 768 |
|
| 769 |
return isRedLink ? |
| 770 |
getLastEditor() : |
| 771 |
'wikitext'; |
| 772 |
case 'remember-last': |
| 773 |
default: |
| 774 |
return getLastEditor(); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
function getAvailableEditPageEditor() { |
| 784 |
switch ( getEditPageEditor() ) { |
| 785 |
case 'visualeditor': |
| 786 |
if ( init.isVisualAvailable ) { |
| 787 |
return 'visual'; |
| 788 |
} |
| 789 |
if ( init.isWikitextAvailable ) { |
| 790 |
return 'source'; |
| 791 |
} |
| 792 |
return null; |
| 793 |
|
| 794 |
case 'wikitext': |
| 795 |
default: |
| 796 |
return init.isWikitextAvailable ? 'source' : null; |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
function checkPreferenceOrStorage( prefName, storageKey, cookieName ) { |
| 809 |
storageKey = storageKey || prefName; |
| 810 |
cookieName = cookieName || storageKey; |
| 811 |
return !!( mw.user.options.get( prefName ) || |
| 812 |
( |
| 813 |
!mw.user.isNamed() && ( |
| 814 |
mw.storage.get( storageKey ) || |
| 815 |
mw.cookie.get( cookieName, '' ) |
| 816 |
) |
| 817 |
) |
| 818 |
); |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
function setPreferenceOrStorage( prefName, storageKey, cookieName ) { |
| 829 |
storageKey = storageKey || prefName; |
| 830 |
cookieName = cookieName || storageKey; |
| 831 |
if ( !mw.user.isNamed() ) { |
| 832 |
|
| 833 |
if ( !mw.storage.set( storageKey, 1 ) ) { |
| 834 |
mw.cookie.set( cookieName, 1, { path: '/', expires: 30 * 86400, prefix: '' } ); |
| 835 |
} |
| 836 |
} else { |
| 837 |
new mw.Api().saveOption( prefName, '1' ); |
| 838 |
mw.user.options.set( prefName, '1' ); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
conf = mw.config.get( 'wgVisualEditorConfig' ); |
| 843 |
tabMessages = conf.tabMessages; |
| 844 |
viewUrl = new URL( mw.util.getUrl( mw.config.get( 'wgRelevantPageName' ) ), location.href ); |
| 845 |
url = new URL( location.href ); |
| 846 |
|
| 847 |
|
| 848 |
oldId = mw.config.get( 'wgRevisionId' ) || $( 'input[name=parentRevId]' ).val(); |
| 849 |
if ( oldId === mw.config.get( 'wgCurRevisionId' ) || mw.config.get( 'wgEditLatestRevision' ) ) { |
| 850 |
|
| 851 |
|
| 852 |
oldId = undefined; |
| 853 |
} |
| 854 |
pageExists = !!mw.config.get( 'wgRelevantArticleId' ); |
| 855 |
const isViewPage = mw.config.get( 'wgIsArticle' ) && !url.searchParams.has( 'diff' ); |
| 856 |
const wgAction = mw.config.get( 'wgAction' ); |
| 857 |
const isEditPage = wgAction === 'edit' || wgAction === 'submit'; |
| 858 |
const pageCanLoadEditor = isViewPage || isEditPage; |
| 859 |
const pageIsProbablyEditable = mw.config.get( 'wgIsProbablyEditable' ) || |
| 860 |
mw.config.get( 'wgRelevantPageIsProbablyEditable' ); |
| 861 |
|
| 862 |
|
| 863 |
const enable = !!+mw.user.options.get( 'visualeditor-enable' ); |
| 864 |
const tempdisable = !!+mw.user.options.get( 'visualeditor-betatempdisable' ); |
| 865 |
const autodisable = !!+mw.user.options.get( 'visualeditor-autodisable' ); |
| 866 |
tabPreference = mw.user.options.get( 'visualeditor-tabs' ); |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
function isOnlyTabVE() { |
| 874 |
return conf.singleEditTab && getAvailableEditPageEditor() === 'visual'; |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
function isOnlyTabWikitext() { |
| 883 |
return conf.singleEditTab && getAvailableEditPageEditor() === 'source'; |
| 884 |
} |
| 885 |
|
| 886 |
init = { |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
addPlugin: function ( plugin ) { |
| 919 |
plugins.push( plugin ); |
| 920 |
}, |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
setupEditLinks: function () { |
| 931 |
|
| 932 |
if ( init.isWikitextAvailable && !isOnlyTabVE() ) { |
| 933 |
$( |
| 934 |
|
| 935 |
'.mw-editsection a:not( .mw-editsection-visualeditor ),' + |
| 936 |
|
| 937 |
'#ca-edit a,' + |
| 938 |
|
| 939 |
'#ca-addsection a' |
| 940 |
).each( ( i, el ) => { |
| 941 |
if ( !el.href ) { |
| 942 |
|
| 943 |
return; |
| 944 |
} |
| 945 |
|
| 946 |
const linkUrl = new URL( el.href ); |
| 947 |
if ( linkUrl.searchParams.has( 'action' ) ) { |
| 948 |
linkUrl.searchParams.delete( 'action' ); |
| 949 |
linkUrl.searchParams.set( 'veaction', 'editsource' ); |
| 950 |
$( el ).attr( 'href', linkUrl.toString() ); |
| 951 |
} |
| 952 |
} ); |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
if ( init.isAvailable ) { |
| 957 |
… |
| 958 |
if ( |
| 959 |
!init.isSingleEditTab && init.isVisualAvailable && |
| 960 |
|
| 961 |
!( conf.disableForAnons && mw.user.isAnon() ) |
| 962 |
) { |
| 963 |
s. |
| 964 |
init.setupMultiTabSkin(); |
| 965 |
} else if ( |
| 966 |
pageCanLoadEditor && ( |
| 967 |
( init.isVisualAvailable && isOnlyTabVE() ) || |
| 968 |
( init.isWikitextAvailable && isOnlyTabWikitext() ) |
| 969 |
) |
| 970 |
) { |
| 971 |
or |
| 972 |
|
| 973 |
$( '.mw-editsection a' ).off( '.ve-target' ).on( 'click.ve-target', ( e ) => { |
| 974 |
|
| 975 |
init.onEditSectionLinkClick( isOnlyTabVE() ? 'visual' : 'source', e ); |
| 976 |
} ); |
| 977 |
|
| 978 |
$( '#ca-edit' ).off( '.ve-target' ).on( 'click.ve-target', ( e ) => { |
| 979 |
init.onEditTabClick( isOnlyTabVE() ? 'visual' : 'source', e ); |
| 980 |
} ); |
| 981 |
} |
| 982 |
} |
| 983 |
}, |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
setupMultiTabSkin: function () { |
| 989 |
init.setupMultiTabs(); |
| 990 |
init.setupMultiSectionLinks(); |
| 991 |
}, |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
setupMultiTabs: function () { |
| 997 |
|
| 998 |
const $caEdit = $( '#ca-edit' ); |
| 999 |
const $caVeEdit = $( '#ca-ve-edit' ); |
| 1000 |
|
| 1001 |
if ( pageCanLoadEditor ) { |
| 1002 |
|
| 1003 |
$caVeEdit.off( '.ve-target' ).on( 'click.ve-target', init.onEditTabClick.bind( init, 'visual' ) ); |
| 1004 |
} |
| 1005 |
if ( pageCanLoadEditor ) { |
| 1006 |
|
| 1007 |
$caEdit.off( '.ve-target' ).on( 'click.ve-target', init.onEditTabClick.bind( init, 'source' ) ); |
| 1008 |
} |
| 1009 |
if ( pageCanLoadEditor && init.isWikitextAvailable ) { |
| 1010 |
|
| 1011 |
|
| 1012 |
$( '#ca-addsection' ).off( '.ve-target' ).on( 'click.ve-target', init.onEditTabClick.bind( init, 'source' ) ); |
| 1013 |
} |
| 1014 |
|
| 1015 |
if ( init.isVisualAvailable ) { |
| 1016 |
if ( conf.tabPosition === 'before' ) { |
| 1017 |
$caEdit.addClass( 'collapsible' ); |
| 1018 |
} else { |
| 1019 |
$caVeEdit.addClass( 'collapsible' ); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
}, |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
setupMultiSectionLinks: function () { |
| 1028 |
if ( pageCanLoadEditor ) { |
| 1029 |
const $editsections = $( '#mw-content-text .mw-editsection' ); |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
$editsections.find( '.mw-editsection-visualeditor' ) |
| 1037 |
.off( '.ve-target' ).on( 'click.ve-target', init.onEditSectionLinkClick.bind( init, 'visual' ) ); |
| 1038 |
if ( init.isWikitextAvailable ) { |
| 1039 |
|
| 1040 |
$editsections.find( 'a:not( .mw-editsection-visualeditor )' ) |
| 1041 |
.off( '.ve-target' ).on( 'click.ve-target', init.onEditSectionLinkClick.bind( init, 'source' ) ); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
}, |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
isUnmodifiedLeftClick: function ( e ) { |
| 1057 |
return e && ( ( |
| 1058 |
e.which && e.which === 1 && !( e.shiftKey || e.altKey || e.ctrlKey || e.metaKey ) |
| 1059 |
) || e.isTrigger ); |
| 1060 |
}, |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
onEditTabClick: function ( mode, e ) { |
| 1069 |
if ( !init.isUnmodifiedLeftClick( e ) ) { |
| 1070 |
return; |
| 1071 |
} |
| 1072 |
if ( !active && mode === 'source' && !init.isWikitextAvailable ) { |
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
return; |
| 1077 |
} |
| 1078 |
e.preventDefault(); |
| 1079 |
if ( isLoading ) { |
| 1080 |
return; |
| 1081 |
} |
| 1082 |
|
| 1083 |
const section = $( e.target ).closest( '#ca-addsection' ).length ? 'new' : null; |
| 1084 |
|
| 1085 |
if ( active ) { |
| 1086 |
targetPromise.done( ( target ) => { |
| 1087 |
if ( target.getDefaultMode() === 'source' ) { |
| 1088 |
if ( mode === 'visual' ) { |
| 1089 |
target.switchToVisualEditor(); |
| 1090 |
} else if ( mode === 'source' ) { |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
target.switchToWikitextSection( section ); |
| 1095 |
} |
| 1096 |
} else if ( target.getDefaultMode() === 'visual' ) { |
| 1097 |
if ( mode === 'source' ) { |
| 1098 |
if ( section ) { |
| 1099 |
|
| 1100 |
target.switchToWikitextSection( section ); |
| 1101 |
} else { |
| 1102 |
target.editSource(); |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
} |
| 1109 |
} ); |
| 1110 |
} else { |
| 1111 |
const link = $( e.target ).closest( 'a' )[ 0 ]; |
| 1112 |
const linkUrl = link && link.href ? new URL( link.href ) : null; |
| 1113 |
if ( section !== null ) { |
| 1114 |
init.activateVe( mode, linkUrl, section ); |
| 1115 |
} else { |
| 1116 |
|
| 1117 |
init.activateVe( mode, linkUrl ); |
| 1118 |
} |
| 1119 |
} |
| 1120 |
}, |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
activateVe: function ( mode, linkUrl, section ) { |
| 1130 |
const wikitext = $( '#wpTextbox1' ).textSelection( 'getContents' ), |
| 1131 |
modified = mw.config.get( 'wgAction' ) === 'submit' || |
| 1132 |
( |
| 1133 |
mw.config.get( 'wgAction' ) === 'edit' && |
| 1134 |
wikitext !== initialWikitext |
| 1135 |
); |
| 1136 |
|
| 1137 |
if ( section === undefined ) { |
| 1138 |
const sectionVal = $( 'input[name=wpSection]' ).val(); |
| 1139 |
section = sectionVal !== '' && sectionVal !== undefined ? sectionVal : null; |
| 1140 |
} |
| 1141 |
|
| 1142 |
|
| 1143 |
if ( $.fn.dialog ) { |
| 1144 |
$( '.ui-dialog-content' ).dialog( 'close' ); |
| 1145 |
} |
| 1146 |
|
| 1147 |
|
| 1148 |
$( window ).off( 'beforeunload.editwarning' ); |
| 1149 |
activatePageTarget( mode, section, modified, linkUrl ); |
| 1150 |
}, |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
onEditSectionLinkClick: function ( mode, e, section ) { |
| 1160 |
const link = $( e.target ).closest( 'a' )[ 0 ]; |
| 1161 |
if ( !link || !link.href ) { |
| 1162 |
|
| 1163 |
return; |
| 1164 |
} |
| 1165 |
|
| 1166 |
const linkUrl = new URL( link.href ); |
| 1167 |
const title = mw.Title.newFromText( linkUrl.searchParams.get( 'title' ) || '' ); |
| 1168 |
|
| 1169 |
if ( |
| 1170 |
|
| 1171 |
!init.isUnmodifiedLeftClick( e ) || |
| 1172 |
|
| 1173 |
!( linkUrl.searchParams.has( 'action' ) || linkUrl.searchParams.has( 'veaction' ) ) || |
| 1174 |
|
| 1175 |
linkUrl.host !== location.host || |
| 1176 |
|
| 1177 |
title && title.getPrefixedText() !== new mw.Title( mw.config.get( 'wgRelevantPageName' ) ).getPrefixedText() |
| 1178 |
) { |
| 1179 |
return; |
| 1180 |
} |
| 1181 |
e.preventDefault(); |
| 1182 |
if ( isLoading ) { |
| 1183 |
return; |
| 1184 |
} |
| 1185 |
|
| 1186 |
trackActivateStart( { type: 'section', mechanism: section === 'new' ? 'new' : 'click', mode: mode }, linkUrl ); |
| 1187 |
|
| 1188 |
if ( !active ) { |
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
history.replaceState( { tag: 'visualeditor' }, '', url ); |
| 1193 |
|
| 1194 |
history.pushState( { tag: 'visualeditor' }, '', linkUrl ); |
| 1195 |
|
| 1196 |
url = linkUrl; |
| 1197 |
|
| 1198 |
|
| 1199 |
if ( section === undefined ) { |
| 1200 |
section = parseSection( linkUrl.searchParams.get( 'section' ) ); |
| 1201 |
} |
| 1202 |
const tPromise = getTarget( mode, section ); |
| 1203 |
activateTarget( mode, section, tPromise ); |
| 1204 |
} |
| 1205 |
}, |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
shouldShowWelcomeDialog: function () { |
| 1217 |
return !( |
| 1218 |
|
| 1219 |
!mw.config.get( 'wgVisualEditorConfig' ).showBetaWelcome || |
| 1220 |
|
| 1221 |
this.isWelcomeDialogSuppressed() || |
| 1222 |
|
| 1223 |
url.searchParams.has( 'collabSession' ) || |
| 1224 |
|
| 1225 |
checkPreferenceOrStorage( 'visualeditor-hidebetawelcome', 've-beta-welcome-dialog' ) |
| 1226 |
); |
| 1227 |
}, |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
isWelcomeDialogSuppressed: function () { |
| 1235 |
return !!( |
| 1236 |
|
| 1237 |
welcomeDialogDisabled || |
| 1238 |
|
| 1239 |
new URL( location.href ).searchParams.has( 'vehidebetadialog' ) || |
| 1240 |
|
| 1241 |
new URL( location.href ).searchParams.has( 'hidewelcomedialog' ) |
| 1242 |
); |
| 1243 |
}, |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
stopShowingWelcomeDialog: function () { |
| 1252 |
setPreferenceOrStorage( 'visualeditor-hidebetawelcome', 've-beta-welcome-dialog' ); |
| 1253 |
}, |
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
disableWelcomeDialog: function () { |
| 1262 |
welcomeDialogDisabled = true; |
| 1263 |
}, |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
shouldShowEducationPopups: function () { |
| 1274 |
return !( |
| 1275 |
|
| 1276 |
educationPopupsDisabled || |
| 1277 |
|
| 1278 |
checkPreferenceOrStorage( 'visualeditor-hideusered', 've-hideusered' ) |
| 1279 |
); |
| 1280 |
}, |
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
stopShowingEducationPopups: function () { |
| 1289 |
setPreferenceOrStorage( 'visualeditor-hideusered', 've-hideusered' ); |
| 1290 |
}, |
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
disableEducationPopups: function () { |
| 1299 |
educationPopupsDisabled = true; |
| 1300 |
} |
| 1301 |
}; |
| 1302 |
|
| 1303 |
init.isSingleEditTab = conf.singleEditTab && tabPreference !== 'multi-tab'; |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
veEditUrl = new URL( pageCanLoadEditor ? url : viewUrl ); |
| 1308 |
if ( oldId ) { |
| 1309 |
veEditUrl.searchParams.set( 'oldid', oldId ); |
| 1310 |
} |
| 1311 |
veEditUrl.searchParams.delete( 'veaction' ); |
| 1312 |
veEditUrl.searchParams.delete( 'action' ); |
| 1313 |
if ( init.isSingleEditTab ) { |
| 1314 |
veEditUrl.searchParams.set( 'action', 'edit' ); |
| 1315 |
veEditSourceUrl = veEditUrl; |
| 1316 |
} else { |
| 1317 |
veEditSourceUrl = new URL( veEditUrl ); |
| 1318 |
veEditUrl.searchParams.set( 'veaction', 'edit' ); |
| 1319 |
veEditSourceUrl.searchParams.set( 'veaction', 'editsource' ); |
| 1320 |
} |
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
init.isAvailable = VisualEditorSupportCheck(); |
| 1325 |
|
| 1326 |
|
| 1327 |
const enabledForUser = ( |
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
( conf.isBeta ? enable : !tempdisable ) && !autodisable |
| 1332 |
); |
| 1333 |
|
| 1334 |
|
| 1335 |
init.isVisualAvailable = ( |
| 1336 |
init.isAvailable && |
| 1337 |
|
| 1338 |
|
| 1339 |
( url.searchParams.get( 'veaction' ) === 'edit' || ( |
| 1340 |
|
| 1341 |
conf.namespaces.indexOf( new mw.Title( mw.config.get( 'wgRelevantPageName' ) ).getNamespaceId() ) !== -1 && |
| 1342 |
|
| 1343 |
|
| 1344 |
enabledForUser |
| 1345 |
) ) && |
| 1346 |
|
| 1347 |
|
| 1348 |
Object.prototype.hasOwnProperty.call( conf.contentModels, mw.config.get( 'wgPageContentModel' ) ) |
| 1349 |
); |
| 1350 |
|
| 1351 |
|
| 1352 |
init.isWikitextAvailable = ( |
| 1353 |
init.isAvailable && |
| 1354 |
|
| 1355 |
|
| 1356 |
( url.searchParams.get( 'veaction' ) === 'editsource' || ( |
| 1357 |
|
| 1358 |
conf.enableWikitext && |
| 1359 |
|
| 1360 |
|
| 1361 |
mw.user.options.get( 'visualeditor-newwikitext' ) |
| 1362 |
) ) && |
| 1363 |
|
| 1364 |
|
| 1365 |
mw.config.get( 'wgPageContentModel' ) === 'wikitext' |
| 1366 |
); |
| 1367 |
|
| 1368 |
if ( init.isVisualAvailable ) { |
| 1369 |
availableModes.push( 'visual' ); |
| 1370 |
} |
| 1371 |
|
| 1372 |
if ( init.isWikitextAvailable ) { |
| 1373 |
availableModes.push( 'source' ); |
| 1374 |
} |
| 1375 |
|
| 1376 |
|
| 1377 |
init.setEditorPreference = setEditorPreference; |
| 1378 |
|
| 1379 |
init.updateTabs = updateTabs; |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
mw.libs.ve = $.extend( mw.libs.ve || {}, init ); |
| 1392 |
|
| 1393 |
if ( init.isVisualAvailable ) { |
| 1394 |
$( 'html' ).addClass( 've-available' ); |
| 1395 |
} else { |
| 1396 |
$( 'html' ).addClass( 've-not-available' ); |
| 1397 |
|
| 1398 |
|
| 1399 |
} |
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
function isSupportedEditPage( editUrl ) { |
| 1408 |
return configData.unsupportedEditParams.every( ( param ) => !editUrl.searchParams.has( param ) ); |
| 1409 |
} |
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
function getEditModeFromUrl( editUrl ) { |
| 1418 |
if ( mw.config.get( 'wgDiscussionToolsStartNewTopicTool' ) ) { |
| 1419 |
|
| 1420 |
return null; |
| 1421 |
} |
| 1422 |
if ( isViewPage && init.isAvailable ) { |
| 1423 |
|
| 1424 |
const mode = veactionToMode[ editUrl.searchParams.get( 'veaction' ) ] || |
| 1425 |
|
| 1426 |
( editUrl.searchParams.has( 'collabSession' ) ? 'visual' : null ); |
| 1427 |
if ( mode && availableModes.indexOf( mode ) !== -1 ) { |
| 1428 |
return mode; |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
if ( isEditPage && isSupportedEditPage( editUrl ) ) { |
| 1433 |
|
| 1434 |
if ( !enabledForUser || $( '#ca-viewsource' ).length || mw.config.get( 'wgAction' ) === 'submit' ) { |
| 1435 |
return null; |
| 1436 |
} |
| 1437 |
return getAvailableEditPageEditor(); |
| 1438 |
} |
| 1439 |
return null; |
| 1440 |
} |
| 1441 |
|
| 1442 |
$( () => { |
| 1443 |
$targetContainer = $( |
| 1444 |
document.querySelector( '[data-mw-ve-target-container]' ) || |
| 1445 |
document.getElementById( 'content' ) |
| 1446 |
); |
| 1447 |
if ( pageCanLoadEditor ) { |
| 1448 |
$targetContainer.addClass( 've-init-mw-desktopArticleTarget-targetContainer' ); |
| 1449 |
} |
| 1450 |
|
| 1451 |
let showWikitextWelcome = true; |
| 1452 |
const numEditButtons = $( '#ca-edit, #ca-ve-edit' ).length, |
| 1453 |
section = parseSection( url.searchParams.get( 'section' ) ); |
| 1454 |
|
| 1455 |
const requiredSkinElements = |
| 1456 |
$targetContainer.length && |
| 1457 |
$( '#mw-content-text' ).length && |
| 1458 |
|
| 1459 |
( isEditPage || numEditButtons ); |
| 1460 |
|
| 1461 |
if ( url.searchParams.get( 'action' ) === 'edit' && $( '#wpTextbox1' ).length ) { |
| 1462 |
initialWikitext = $( '#wpTextbox1' ).textSelection( 'getContents' ); |
| 1463 |
} |
| 1464 |
|
| 1465 |
if ( ( init.isVisualAvailable || init.isWikitextAvailable ) && |
| 1466 |
pageCanLoadEditor && |
| 1467 |
pageIsProbablyEditable && |
| 1468 |
!requiredSkinElements |
| 1469 |
) { |
| 1470 |
mw.log.warn( |
| 1471 |
'Your skin is incompatible with VisualEditor. ' + |
| 1472 |
'See https://www.mediawiki.org/wiki/Extension:VisualEditor/Skin_requirements for the requirements.' |
| 1473 |
); |
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
if ( numEditButtons > 0 ) { |
| 1478 |
const err = new Error( 'Incompatible with VisualEditor' ); |
| 1479 |
err.name = 'VeIncompatibleSkinWarning'; |
| 1480 |
mw.errorLogger.logError( err, 'error.visualeditor' ); |
| 1481 |
} |
| 1482 |
} else if ( init.isAvailable ) { |
| 1483 |
const mode = getEditModeFromUrl( url ); |
| 1484 |
if ( mode ) { |
| 1485 |
showWikitextWelcome = false; |
| 1486 |
trackActivateStart( { |
| 1487 |
type: section === null ? 'page' : 'section', |
| 1488 |
mechanism: ( section === 'new' || !mw.config.get( 'wgArticleId' ) ) ? 'url-new' : 'url', |
| 1489 |
mode: mode |
| 1490 |
} ); |
| 1491 |
activateTarget( mode, section ); |
| 1492 |
} else if ( |
| 1493 |
init.isVisualAvailable && |
| 1494 |
pageCanLoadEditor && |
| 1495 |
init.isSingleEditTab |
| 1496 |
) { |
| 1497 |
|
| 1498 |
|
| 1499 |
$( document.body ).append( |
| 1500 |
$( '<a>' ) |
| 1501 |
.attr( { accesskey: mw.msg( 'accesskey-ca-ve-edit' ), href: veEditUrl } ) |
| 1502 |
|
| 1503 |
.on( 'click.ve-target', init.onEditTabClick.bind( init, 'visual' ) ) |
| 1504 |
.addClass( 'oo-ui-element-hidden' ) |
| 1505 |
); |
| 1506 |
} |
| 1507 |
|
| 1508 |
|
| 1509 |
if ( |
| 1510 |
init.isVisualAvailable && |
| 1511 |
isEditPage && |
| 1512 |
$( '#wpTextbox1' ).length |
| 1513 |
) { |
| 1514 |
mw.loader.load( 'ext.visualEditor.switching' ); |
| 1515 |
mw.hook( 'wikiEditor.toolbarReady' ).add( ( $textarea ) => { |
| 1516 |
mw.loader.using( 'ext.visualEditor.switching' ).done( () => { |
| 1517 |
const showPopup = url.searchParams.has( 'veswitched' ) && !mw.user.options.get( 'visualeditor-hidesourceswitchpopup' ), |
| 1518 |
toolFactory = new OO.ui.ToolFactory(), |
| 1519 |
toolGroupFactory = new OO.ui.ToolGroupFactory(); |
| 1520 |
|
| 1521 |
toolFactory.register( mw.libs.ve.MWEditModeVisualTool ); |
| 1522 |
toolFactory.register( mw.libs.ve.MWEditModeSourceTool ); |
| 1523 |
const switchToolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory, { |
| 1524 |
classes: [ 've-init-mw-editSwitch' ] |
| 1525 |
} ); |
| 1526 |
|
| 1527 |
switchToolbar.on( 'switchEditor', ( m ) => { |
| 1528 |
if ( m === 'visual' ) { |
| 1529 |
$( '#wpTextbox1' ).trigger( 'wikiEditor-switching-visualeditor' ); |
| 1530 |
init.activateVe( 'visual' ); |
| 1531 |
} |
| 1532 |
} ); |
| 1533 |
|
| 1534 |
switchToolbar.setup( [ { |
| 1535 |
name: 'editMode', |
| 1536 |
type: 'list', |
| 1537 |
icon: 'edit', |
| 1538 |
title: mw.msg( 'visualeditor-mweditmode-tooltip' ), |
| 1539 |
label: mw.msg( 'visualeditor-mweditmode-tooltip' ), |
| 1540 |
invisibleLabel: true, |
| 1541 |
include: [ 'editModeVisual', 'editModeSource' ] |
| 1542 |
} ] ); |
| 1543 |
|
| 1544 |
const popup = new mw.libs.ve.SwitchPopupWidget( 'source' ); |
| 1545 |
|
| 1546 |
switchToolbar.tools.editModeVisual.toolGroup.$element.append( popup.$element ); |
| 1547 |
switchToolbar.emit( 'updateState' ); |
| 1548 |
|
| 1549 |
$textarea.wikiEditor( 'addToToolbar', { |
| 1550 |
section: 'secondary', |
| 1551 |
group: 'default', |
| 1552 |
tools: { |
| 1553 |
veEditSwitch: { |
| 1554 |
type: 'element', |
| 1555 |
element: switchToolbar.$element |
| 1556 |
} |
| 1557 |
} |
| 1558 |
} ); |
| 1559 |
|
| 1560 |
popup.toggle( showPopup ); |
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
if ( $( '#ca-edit' ).hasClass( 'visualeditor-showtabdialog' ) ) { |
| 1565 |
$( '#ca-edit' ).removeClass( 'visualeditor-showtabdialog' ); |
| 1566 |
|
| 1567 |
const windowManager = new OO.ui.WindowManager(); |
| 1568 |
$( OO.ui.getTeleportTarget() ).append( windowManager.$element ); |
| 1569 |
const editingTabDialog = new mw.libs.ve.EditingTabDialog(); |
| 1570 |
windowManager.addWindows( [ editingTabDialog ] ); |
| 1571 |
windowManager.openWindow( editingTabDialog ) |
| 1572 |
.closed.then( ( data ) => { |
| 1573 |
|
| 1574 |
windowManager.destroy(); |
| 1575 |
|
| 1576 |
if ( data && data.action === 'prefer-ve' ) { |
| 1577 |
location.href = veEditUrl; |
| 1578 |
} else if ( data && data.action === 'multi-tab' ) { |
| 1579 |
location.reload(); |
| 1580 |
} |
| 1581 |
} ); |
| 1582 |
} |
| 1583 |
} ); |
| 1584 |
} ); |
| 1585 |
|
| 1586 |
|
| 1587 |
mw.libs.ve.setEditorPreference( 'wikitext' ); |
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
mw.storage.session.remove( 've-docstate' ); |
| 1594 |
} |
| 1595 |
|
| 1596 |
init.setupEditLinks(); |
| 1597 |
} |
| 1598 |
|
| 1599 |
if ( |
| 1600 |
pageCanLoadEditor && |
| 1601 |
showWikitextWelcome && |
| 1602 |
|
| 1603 |
( init.isVisualAvailable || init.isWikitextAvailable || $( '#wpTextbox1' ).length ) && |
| 1604 |
isEditPage && |
| 1605 |
init.shouldShowWelcomeDialog() && |
| 1606 |
|
| 1607 |
pageIsProbablyEditable |
| 1608 |
) { |
| 1609 |
mw.loader.using( 'ext.visualEditor.welcome' ).done( () => { |
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
if ( !init.shouldShowWelcomeDialog() ) { |
| 1614 |
return; |
| 1615 |
} |
| 1616 |
const windowManager = new OO.ui.WindowManager(); |
| 1617 |
const welcomeDialog = new mw.libs.ve.WelcomeDialog(); |
| 1618 |
$( OO.ui.getTeleportTarget() ).append( windowManager.$element ); |
| 1619 |
windowManager.addWindows( [ welcomeDialog ] ); |
| 1620 |
windowManager.openWindow( |
| 1621 |
welcomeDialog, |
| 1622 |
{ |
| 1623 |
switchable: init.isVisualAvailable, |
| 1624 |
editor: 'source' |
| 1625 |
} |
| 1626 |
) |
| 1627 |
.closed.then( ( data ) => { |
| 1628 |
windowManager.destroy(); |
| 1629 |
if ( data && data.action === 'switch-ve' ) { |
| 1630 |
init.activateVe( 'visual' ); |
| 1631 |
} |
| 1632 |
} ); |
| 1633 |
|
| 1634 |
init.stopShowingWelcomeDialog(); |
| 1635 |
} ); |
| 1636 |
} |
| 1637 |
|
| 1638 |
if ( url.searchParams.has( 'venotify' ) ) { |
| 1639 |
url.searchParams.delete( 'venotify' ); |
| 1640 |
|
| 1641 |
history.replaceState( null, '', url ); |
| 1642 |
} |
| 1643 |
} ); |
| 1644 |
}() ); |