2013-07-03 22:14:52 +00:00
|
|
|
/*!
|
2015-07-29 13:41:30 +00:00
|
|
|
* VisualEditor MediaWiki DesktopArticleTarget init.
|
2013-07-03 22:14:52 +00:00
|
|
|
*
|
|
|
|
* This file must remain as widely compatible as the base compatibility
|
|
|
|
* for MediaWiki itself (see mediawiki/core:/resources/startup.js).
|
2017-04-06 21:02:18 +00:00
|
|
|
* Avoid use of: SVG, HTML5 DOM, ContentEditable etc.
|
2013-07-03 22:14:52 +00:00
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2013-07-03 22:14:52 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2019-02-20 20:23:43 +00:00
|
|
|
/* eslint-disable no-jquery/no-global-selector */
|
2019-07-22 20:33:15 +00:00
|
|
|
// TODO: ve.now and ve.track should be moved to mw.libs.ve
|
|
|
|
/* global ve */
|
2019-01-08 17:00:09 +00:00
|
|
|
|
2013-07-03 22:14:52 +00:00
|
|
|
/**
|
|
|
|
* Platform preparation for the MediaWiki view page. This loads (when user needs it) the
|
|
|
|
* actual MediaWiki integration and VisualEditor library.
|
|
|
|
*/
|
|
|
|
( function () {
|
2024-05-21 16:40:36 +00:00
|
|
|
const configData = require( './data.json' ),
|
2019-04-10 19:41:02 +00:00
|
|
|
veactionToMode = {
|
|
|
|
edit: 'visual',
|
|
|
|
editsource: 'source'
|
2016-09-03 00:30:06 +00:00
|
|
|
},
|
2024-05-21 16:40:36 +00:00
|
|
|
availableModes = [];
|
|
|
|
let init = null,
|
|
|
|
conf = null,
|
|
|
|
tabMessages = null,
|
|
|
|
pageExists = null,
|
|
|
|
viewUrl = null,
|
|
|
|
veEditUrl = null,
|
|
|
|
tabPreference = null;
|
|
|
|
let veEditSourceUrl, targetPromise, url,
|
|
|
|
initialWikitext, oldId,
|
|
|
|
isLoading, tempWikitextEditor, tempWikitextEditorData,
|
|
|
|
$toolbarPlaceholder, $toolbarPlaceholderBar,
|
|
|
|
contentTop, wasFloating,
|
2015-05-09 13:47:10 +00:00
|
|
|
active = false,
|
2016-07-26 03:04:47 +00:00
|
|
|
targetLoaded = false,
|
2020-04-17 21:30:29 +00:00
|
|
|
plugins = [],
|
2020-05-02 01:49:00 +00:00
|
|
|
welcomeDialogDisabled = false,
|
2022-06-21 18:01:30 +00:00
|
|
|
educationPopupsDisabled = false,
|
2022-09-02 17:05:21 +00:00
|
|
|
// Defined after document-ready below
|
|
|
|
$targetContainer = null;
|
2013-07-03 22:14:52 +00:00
|
|
|
|
2023-05-10 14:25:06 +00:00
|
|
|
if ( mw.config.get( 'wgMFMode' ) ) {
|
|
|
|
mw.log.warn( 'Attempted to load desktop target on mobile.' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Show the loading progress bar
|
|
|
|
*/
|
|
|
|
function showLoading() {
|
2016-08-23 16:27:30 +00:00
|
|
|
if ( isLoading ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isLoading = true;
|
|
|
|
|
2016-02-10 02:27:50 +00:00
|
|
|
$( 'html' ).addClass( 've-activated ve-loading' );
|
2015-02-21 05:09:50 +00:00
|
|
|
if ( !init.$loading ) {
|
2019-02-06 01:06:10 +00:00
|
|
|
init.progressBar = new mw.libs.ve.ProgressBarWidget();
|
|
|
|
init.$loading = $( '<div>' )
|
|
|
|
.addClass( 've-init-mw-desktopArticleTarget-loading-overlay' )
|
|
|
|
.append( init.progressBar.$element );
|
2015-02-15 11:42:21 +00:00
|
|
|
}
|
2017-10-04 12:52:05 +00:00
|
|
|
$( document ).on( 'keydown', onDocumentKeyDown );
|
2015-08-19 18:05:01 +00:00
|
|
|
|
2022-01-24 23:35:50 +00:00
|
|
|
$toolbarPlaceholderBar.append( init.$loading );
|
2015-02-15 11:42:21 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Increment loading progress by one step
|
|
|
|
*
|
|
|
|
* See mw.libs.ve.ProgressBarWidget for steps.
|
|
|
|
*/
|
2015-05-09 13:47:10 +00:00
|
|
|
function incrementLoadingProgress() {
|
2019-02-06 01:06:10 +00:00
|
|
|
init.progressBar.incrementLoadingProgress();
|
2015-05-09 13:47:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Clear and hide the loading progress bar
|
|
|
|
*/
|
2017-10-04 12:52:05 +00:00
|
|
|
function clearLoading() {
|
2019-02-06 01:06:10 +00:00
|
|
|
init.progressBar.clearLoading();
|
2016-08-23 16:27:30 +00:00
|
|
|
isLoading = false;
|
2017-10-04 12:52:05 +00:00
|
|
|
$( document ).off( 'keydown', onDocumentKeyDown );
|
2016-02-17 15:35:16 +00:00
|
|
|
$( 'html' ).removeClass( 've-loading' );
|
2015-02-21 05:09:50 +00:00
|
|
|
if ( init.$loading ) {
|
|
|
|
init.$loading.detach();
|
|
|
|
}
|
2022-01-24 23:35:50 +00:00
|
|
|
|
2017-10-18 22:50:31 +00:00
|
|
|
if ( tempWikitextEditor ) {
|
2022-01-24 23:35:50 +00:00
|
|
|
teardownTempWikitextEditor();
|
|
|
|
}
|
|
|
|
hideToolbarPlaceholder();
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Handle window scroll events
|
|
|
|
*
|
|
|
|
* @param {Event} e
|
|
|
|
*/
|
2022-01-24 23:35:50 +00:00
|
|
|
function onWindowScroll() {
|
2024-05-21 14:22:56 +00:00
|
|
|
const scrollTop = $( document.documentElement ).scrollTop();
|
|
|
|
const floating = scrollTop > contentTop;
|
2022-01-24 23:35:50 +00:00
|
|
|
if ( floating !== wasFloating ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const width = $targetContainer.outerWidth();
|
2022-01-24 23:35:50 +00:00
|
|
|
$toolbarPlaceholder.toggleClass( 've-init-mw-desktopArticleTarget-toolbarPlaceholder-floating', floating );
|
|
|
|
$toolbarPlaceholderBar.css( 'width', width );
|
|
|
|
wasFloating = floating;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const onWindowScrollListener = mw.util.throttle( onWindowScroll, 250 );
|
2022-01-24 23:35:50 +00:00
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Show a placeholder for the VE toolbar
|
|
|
|
*/
|
2022-01-24 23:35:50 +00:00
|
|
|
function showToolbarPlaceholder() {
|
|
|
|
if ( !$toolbarPlaceholder ) {
|
|
|
|
// Create an equal-height placeholder for the toolbar to avoid vertical jump
|
|
|
|
// when the real toolbar is ready.
|
|
|
|
$toolbarPlaceholder = $( '<div>' ).addClass( 've-init-mw-desktopArticleTarget-toolbarPlaceholder' );
|
|
|
|
$toolbarPlaceholderBar = $( '<div>' ).addClass( 've-init-mw-desktopArticleTarget-toolbarPlaceholder-bar' );
|
|
|
|
$toolbarPlaceholder.append( $toolbarPlaceholderBar );
|
|
|
|
}
|
2022-02-15 15:10:43 +00:00
|
|
|
// Toggle -floating class before append (if required) to avoid content moving later
|
2022-06-21 18:01:30 +00:00
|
|
|
contentTop = $targetContainer.offset().top;
|
2022-01-24 23:35:50 +00:00
|
|
|
wasFloating = null;
|
2022-02-15 15:10:43 +00:00
|
|
|
onWindowScroll();
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const scrollTopBefore = $( document.documentElement ).scrollTop();
|
2022-02-15 15:10:43 +00:00
|
|
|
|
2022-06-21 18:01:30 +00:00
|
|
|
$targetContainer.prepend( $toolbarPlaceholder );
|
2022-02-15 15:10:43 +00:00
|
|
|
|
2023-08-09 15:42:23 +00:00
|
|
|
window.addEventListener( 'scroll', onWindowScrollListener, { passive: true } );
|
2022-02-15 15:10:43 +00:00
|
|
|
|
|
|
|
if ( wasFloating ) {
|
|
|
|
// Browser might not support scroll anchoring:
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-anchor/Guide_to_scroll_anchoring
|
|
|
|
// ...so compute the new scroll offset ourselves.
|
|
|
|
window.scrollTo( 0, scrollTopBefore + $toolbarPlaceholder.outerHeight() );
|
|
|
|
}
|
2022-01-24 23:35:50 +00:00
|
|
|
|
|
|
|
// Add class for transition after first render
|
2024-04-30 16:44:25 +00:00
|
|
|
setTimeout( () => {
|
2022-01-24 23:35:50 +00:00
|
|
|
$toolbarPlaceholder.addClass( 've-init-mw-desktopArticleTarget-toolbarPlaceholder-open' );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Hide the placeholder for the VE toolbar
|
|
|
|
*/
|
2022-01-24 23:35:50 +00:00
|
|
|
function hideToolbarPlaceholder() {
|
|
|
|
if ( $toolbarPlaceholder ) {
|
2023-12-08 11:00:01 +00:00
|
|
|
window.removeEventListener( 'scroll', onWindowScrollListener );
|
2022-01-24 23:35:50 +00:00
|
|
|
$toolbarPlaceholder.detach();
|
|
|
|
$toolbarPlaceholder.removeClass( 've-init-mw-desktopArticleTarget-toolbarPlaceholder-open' );
|
2017-10-18 22:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Create a temporary `<textarea>` wikitext editor while source mode loads
|
|
|
|
*
|
|
|
|
* @param {Object} data Initialisation data for VE
|
|
|
|
*/
|
2017-10-18 22:50:31 +00:00
|
|
|
function setupTempWikitextEditor( data ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
let wikitext = data.content;
|
2018-02-01 20:25:11 +00:00
|
|
|
// Add trailing linebreak to non-empty wikitext documents for consistency
|
|
|
|
// with old editor and usability. Will be stripped on save. T156609
|
2021-06-08 08:52:38 +00:00
|
|
|
if ( wikitext ) {
|
|
|
|
wikitext += '\n';
|
2018-02-01 20:25:11 +00:00
|
|
|
}
|
2021-06-08 08:52:38 +00:00
|
|
|
tempWikitextEditor = new mw.libs.ve.MWTempWikitextEditorWidget( { value: wikitext } );
|
2018-01-23 20:51:11 +00:00
|
|
|
tempWikitextEditorData = data;
|
2017-10-18 22:50:31 +00:00
|
|
|
|
|
|
|
// Bring forward some transformations that show the editor is now ready
|
2022-11-09 17:23:12 +00:00
|
|
|
// Grey out the page title if it is below the editing toolbar (depending on skin), to show it is uneditable.
|
|
|
|
$( '.ve-init-mw-desktopArticleTarget-targetContainer #firstHeading' ).addClass( 've-init-mw-desktopArticleTarget-uneditableContent' );
|
2017-10-18 22:50:31 +00:00
|
|
|
$( '#mw-content-text' )
|
|
|
|
.before( tempWikitextEditor.$element )
|
|
|
|
.addClass( 'oo-ui-element-hidden' );
|
|
|
|
$( 'html' ).addClass( 've-tempSourceEditing' ).removeClass( 've-loading' );
|
|
|
|
|
|
|
|
// Resize the textarea to fit content. We could do this more often (e.g. on change)
|
|
|
|
// but hopefully this temporary textarea won't be visible for too long.
|
2018-01-22 00:49:49 +00:00
|
|
|
tempWikitextEditor.adjustSize().moveCursorToStart();
|
2023-03-18 02:34:32 +00:00
|
|
|
ve.track( 'editAttemptStep', { action: 'ready', mode: 'source', platform: 'desktop' } );
|
2018-01-22 18:27:43 +00:00
|
|
|
mw.libs.ve.tempWikitextEditor = tempWikitextEditor;
|
|
|
|
mw.hook( 've.wikitextInteractive' ).fire();
|
2017-10-18 22:50:31 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Synchronise state of temporary wikitexteditor back to the VE initialisation data object
|
|
|
|
*/
|
2018-01-23 20:51:11 +00:00
|
|
|
function syncTempWikitextEditor() {
|
2024-05-21 14:22:56 +00:00
|
|
|
let wikitext = tempWikitextEditor.getValue();
|
2018-01-23 20:51:11 +00:00
|
|
|
|
2018-02-01 20:25:11 +00:00
|
|
|
// Strip trailing linebreak. Will get re-added in ArticleTarget#parseDocument.
|
2021-06-08 08:52:38 +00:00
|
|
|
if ( wikitext.slice( -1 ) === '\n' ) {
|
|
|
|
wikitext = wikitext.slice( 0, -1 );
|
2018-02-01 20:25:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 08:52:38 +00:00
|
|
|
if ( wikitext !== tempWikitextEditorData.content ) {
|
2018-01-23 20:51:11 +00:00
|
|
|
// Write changes back to response data object,
|
|
|
|
// which will be used to construct the surface.
|
2021-06-08 08:52:38 +00:00
|
|
|
tempWikitextEditorData.content = wikitext;
|
2018-01-23 20:51:11 +00:00
|
|
|
// TODO: Consider writing changes using a
|
|
|
|
// transaction so they can be undone.
|
|
|
|
// For now, just mark surface as pre-modified
|
|
|
|
tempWikitextEditorData.fromEditedState = true;
|
|
|
|
}
|
2017-10-18 22:50:31 +00:00
|
|
|
|
2018-01-23 20:51:11 +00:00
|
|
|
// Store the last-seen selection and pass to the target
|
|
|
|
tempWikitextEditorData.initialSourceRange = tempWikitextEditor.getRange();
|
2017-10-18 22:50:31 +00:00
|
|
|
|
2018-01-23 20:51:11 +00:00
|
|
|
tempWikitextEditor.$element.prop( 'readonly', true );
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Teardown the temporary wikitext editor
|
|
|
|
*/
|
2018-01-23 20:51:11 +00:00
|
|
|
function teardownTempWikitextEditor() {
|
2017-10-18 22:50:31 +00:00
|
|
|
// Destroy widget and placeholder
|
|
|
|
tempWikitextEditor.$element.remove();
|
2018-01-22 18:27:43 +00:00
|
|
|
mw.libs.ve.tempWikitextEditor = tempWikitextEditor = null;
|
2018-01-23 20:51:11 +00:00
|
|
|
tempWikitextEditorData = null;
|
2017-10-18 22:50:31 +00:00
|
|
|
|
|
|
|
$( '#mw-content-text' ).removeClass( 'oo-ui-element-hidden' );
|
|
|
|
$( 'html' ).removeClass( 've-tempSourceEditing' );
|
2015-02-21 05:09:50 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Abort loading the editor
|
|
|
|
*/
|
2017-10-04 12:52:05 +00:00
|
|
|
function abortLoading() {
|
|
|
|
$( 'html' ).removeClass( 've-activated' );
|
|
|
|
active = false;
|
2022-08-10 16:11:16 +00:00
|
|
|
updateTabs( false );
|
2017-10-04 12:52:05 +00:00
|
|
|
// Push read tab URL to history
|
2022-12-12 19:47:32 +00:00
|
|
|
if ( $( '#ca-view a' ).length ) {
|
2022-12-17 15:44:56 +00:00
|
|
|
history.pushState( { tag: 'visualeditor' }, '', $( '#ca-view a' ).attr( 'href' ) );
|
2017-10-04 12:52:05 +00:00
|
|
|
}
|
|
|
|
clearLoading();
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Handle keydown events on the document
|
|
|
|
*
|
|
|
|
* @param {jQuery.Event} e Keydown event
|
|
|
|
*/
|
2017-10-04 12:52:05 +00:00
|
|
|
function onDocumentKeyDown( e ) {
|
|
|
|
if ( e.which === 27 /* OO.ui.Keys.ESCAPE */ ) {
|
|
|
|
abortLoading();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 14:53:53 +00:00
|
|
|
/**
|
|
|
|
* Parse a section value from a query string object
|
|
|
|
*
|
|
|
|
* @example
|
2022-12-17 15:44:56 +00:00
|
|
|
* parseSection( new URL( location.href ).searchParams.get( 'section' ) )
|
2022-02-16 14:53:53 +00:00
|
|
|
*
|
|
|
|
* @param {string|undefined} section Section value from query object
|
|
|
|
* @return {string|null} Section if valid, null otherwise
|
|
|
|
*/
|
2016-12-08 19:19:17 +00:00
|
|
|
function parseSection( section ) {
|
2016-12-15 00:07:22 +00:00
|
|
|
// Section must be a number, 'new' or 'T-' prefixed
|
2022-02-16 14:53:53 +00:00
|
|
|
if ( section && /^(new|\d+|T-\d+)$/.test( section ) ) {
|
2020-03-18 20:17:51 +00:00
|
|
|
return section;
|
2016-12-08 19:19:17 +00:00
|
|
|
}
|
2020-03-18 20:17:51 +00:00
|
|
|
return null;
|
2016-12-08 19:19:17 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 22:14:52 +00:00
|
|
|
/**
|
|
|
|
* Use deferreds to avoid loading and instantiating Target multiple times.
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
Load RL modules in one load.php request, rather than in two stages
This introduces TargetLoader, which manages plugins and RL modules
in a slightly more generic fashion so that Targets themselves don't
have to. This allows us to load all RL modules in one load.php
request, rather than first loading ViewPageTarget which then
loads the other modules.
TargetLoader loads in the bottom queue, so it will be loaded
as part of the main load.php request, but in VPT.init.js we
still have to wait for it with using() because it might not
have arrived yet. This also degrades gracefully on cached pages
where TargetLoader isn't in the bottom queue: it'll be loaded
as a separate request instead, which is suboptimal but no
worse that what we were doing before.
Right now TargetLoader is small enough that it could also be in
the top queue, but in the future we want to add things like
the action=visualeditor API request to it, and mw.Api is
relatively big.
Note: this also makes a breaking change to the plugin API:
plugin callbacks no longer receive the target instance
as a parameter, as they're now executed before the target
has been constructed rather than after. In the long term,
if we want to give plugins access to the target instance,
we could give them the target promise somehow. For now,
I've killed this feature because nothing used it and
the change from a direct object reference to a promise
would have been a breaking change anyway.
Also fixed incorrect documentation index for ve.init.mw.ViewPageTarget.init.
Bug: T53569
Change-Id: Ibfa6abbeaf872ae2aadc6ed9d5beba7473ea441a
2015-02-26 01:22:44 +00:00
|
|
|
* @private
|
2016-05-26 12:08:26 +00:00
|
|
|
* @param {string} mode Target mode: 'visual' or 'source'
|
2020-03-31 20:04:30 +00:00
|
|
|
* @param {string} section Section to edit
|
Load RL modules in one load.php request, rather than in two stages
This introduces TargetLoader, which manages plugins and RL modules
in a slightly more generic fashion so that Targets themselves don't
have to. This allows us to load all RL modules in one load.php
request, rather than first loading ViewPageTarget which then
loads the other modules.
TargetLoader loads in the bottom queue, so it will be loaded
as part of the main load.php request, but in VPT.init.js we
still have to wait for it with using() because it might not
have arrived yet. This also degrades gracefully on cached pages
where TargetLoader isn't in the bottom queue: it'll be loaded
as a separate request instead, which is suboptimal but no
worse that what we were doing before.
Right now TargetLoader is small enough that it could also be in
the top queue, but in the future we want to add things like
the action=visualeditor API request to it, and mw.Api is
relatively big.
Note: this also makes a breaking change to the plugin API:
plugin callbacks no longer receive the target instance
as a parameter, as they're now executed before the target
has been constructed rather than after. In the long term,
if we want to give plugins access to the target instance,
we could give them the target promise somehow. For now,
I've killed this feature because nothing used it and
the change from a direct object reference to a promise
would have been a breaking change anyway.
Also fixed incorrect documentation index for ve.init.mw.ViewPageTarget.init.
Bug: T53569
Change-Id: Ibfa6abbeaf872ae2aadc6ed9d5beba7473ea441a
2015-02-26 01:22:44 +00:00
|
|
|
* @return {jQuery.Promise}
|
2013-07-03 22:14:52 +00:00
|
|
|
*/
|
2016-12-08 19:19:17 +00:00
|
|
|
function getTarget( mode, section ) {
|
2014-09-07 22:47:58 +00:00
|
|
|
if ( !targetPromise ) {
|
Load RL modules in one load.php request, rather than in two stages
This introduces TargetLoader, which manages plugins and RL modules
in a slightly more generic fashion so that Targets themselves don't
have to. This allows us to load all RL modules in one load.php
request, rather than first loading ViewPageTarget which then
loads the other modules.
TargetLoader loads in the bottom queue, so it will be loaded
as part of the main load.php request, but in VPT.init.js we
still have to wait for it with using() because it might not
have arrived yet. This also degrades gracefully on cached pages
where TargetLoader isn't in the bottom queue: it'll be loaded
as a separate request instead, which is suboptimal but no
worse that what we were doing before.
Right now TargetLoader is small enough that it could also be in
the top queue, but in the future we want to add things like
the action=visualeditor API request to it, and mw.Api is
relatively big.
Note: this also makes a breaking change to the plugin API:
plugin callbacks no longer receive the target instance
as a parameter, as they're now executed before the target
has been constructed rather than after. In the long term,
if we want to give plugins access to the target instance,
we could give them the target promise somehow. For now,
I've killed this feature because nothing used it and
the change from a direct object reference to a promise
would have been a breaking change anyway.
Also fixed incorrect documentation index for ve.init.mw.ViewPageTarget.init.
Bug: T53569
Change-Id: Ibfa6abbeaf872ae2aadc6ed9d5beba7473ea441a
2015-02-26 01:22:44 +00:00
|
|
|
// The TargetLoader module is loaded in the bottom queue, so it should have been
|
|
|
|
// requested already but it might not have finished loading yet
|
|
|
|
targetPromise = mw.loader.using( 'ext.visualEditor.targetLoader' )
|
2024-04-30 16:44:25 +00:00
|
|
|
.then( () => {
|
|
|
|
mw.libs.ve.targetLoader.addPlugin(
|
2017-02-01 17:00:07 +00:00
|
|
|
// Run VisualEditorPreloadModules, but if they fail, we still want to continue
|
2015-04-03 20:38:39 +00:00
|
|
|
// loading, so convert failure to success
|
2024-04-30 16:44:25 +00:00
|
|
|
() => mw.loader.using( conf.preloadModules ).catch(
|
|
|
|
() => $.Deferred().resolve()
|
|
|
|
)
|
|
|
|
);
|
Load RL modules in one load.php request, rather than in two stages
This introduces TargetLoader, which manages plugins and RL modules
in a slightly more generic fashion so that Targets themselves don't
have to. This allows us to load all RL modules in one load.php
request, rather than first loading ViewPageTarget which then
loads the other modules.
TargetLoader loads in the bottom queue, so it will be loaded
as part of the main load.php request, but in VPT.init.js we
still have to wait for it with using() because it might not
have arrived yet. This also degrades gracefully on cached pages
where TargetLoader isn't in the bottom queue: it'll be loaded
as a separate request instead, which is suboptimal but no
worse that what we were doing before.
Right now TargetLoader is small enough that it could also be in
the top queue, but in the future we want to add things like
the action=visualeditor API request to it, and mw.Api is
relatively big.
Note: this also makes a breaking change to the plugin API:
plugin callbacks no longer receive the target instance
as a parameter, as they're now executed before the target
has been constructed rather than after. In the long term,
if we want to give plugins access to the target instance,
we could give them the target promise somehow. For now,
I've killed this feature because nothing used it and
the change from a direct object reference to a promise
would have been a breaking change anyway.
Also fixed incorrect documentation index for ve.init.mw.ViewPageTarget.init.
Bug: T53569
Change-Id: Ibfa6abbeaf872ae2aadc6ed9d5beba7473ea441a
2015-02-26 01:22:44 +00:00
|
|
|
// Add modules specific to desktop (modules shared between desktop
|
|
|
|
// and mobile are already added by TargetLoader)
|
2024-05-29 16:03:23 +00:00
|
|
|
[
|
|
|
|
'ext.visualEditor.desktopArticleTarget',
|
2017-02-27 16:13:56 +00:00
|
|
|
// Add requested plugins
|
2024-05-29 16:03:23 +00:00
|
|
|
...plugins
|
|
|
|
].forEach( mw.libs.ve.targetLoader.addPlugin );
|
Load RL modules in one load.php request, rather than in two stages
This introduces TargetLoader, which manages plugins and RL modules
in a slightly more generic fashion so that Targets themselves don't
have to. This allows us to load all RL modules in one load.php
request, rather than first loading ViewPageTarget which then
loads the other modules.
TargetLoader loads in the bottom queue, so it will be loaded
as part of the main load.php request, but in VPT.init.js we
still have to wait for it with using() because it might not
have arrived yet. This also degrades gracefully on cached pages
where TargetLoader isn't in the bottom queue: it'll be loaded
as a separate request instead, which is suboptimal but no
worse that what we were doing before.
Right now TargetLoader is small enough that it could also be in
the top queue, but in the future we want to add things like
the action=visualeditor API request to it, and mw.Api is
relatively big.
Note: this also makes a breaking change to the plugin API:
plugin callbacks no longer receive the target instance
as a parameter, as they're now executed before the target
has been constructed rather than after. In the long term,
if we want to give plugins access to the target instance,
we could give them the target promise somehow. For now,
I've killed this feature because nothing used it and
the change from a direct object reference to a promise
would have been a breaking change anyway.
Also fixed incorrect documentation index for ve.init.mw.ViewPageTarget.init.
Bug: T53569
Change-Id: Ibfa6abbeaf872ae2aadc6ed9d5beba7473ea441a
2015-02-26 01:22:44 +00:00
|
|
|
plugins = [];
|
2017-12-07 11:14:00 +00:00
|
|
|
return mw.libs.ve.targetLoader.loadModules( mode );
|
Load RL modules in one load.php request, rather than in two stages
This introduces TargetLoader, which manages plugins and RL modules
in a slightly more generic fashion so that Targets themselves don't
have to. This allows us to load all RL modules in one load.php
request, rather than first loading ViewPageTarget which then
loads the other modules.
TargetLoader loads in the bottom queue, so it will be loaded
as part of the main load.php request, but in VPT.init.js we
still have to wait for it with using() because it might not
have arrived yet. This also degrades gracefully on cached pages
where TargetLoader isn't in the bottom queue: it'll be loaded
as a separate request instead, which is suboptimal but no
worse that what we were doing before.
Right now TargetLoader is small enough that it could also be in
the top queue, but in the future we want to add things like
the action=visualeditor API request to it, and mw.Api is
relatively big.
Note: this also makes a breaking change to the plugin API:
plugin callbacks no longer receive the target instance
as a parameter, as they're now executed before the target
has been constructed rather than after. In the long term,
if we want to give plugins access to the target instance,
we could give them the target promise somehow. For now,
I've killed this feature because nothing used it and
the change from a direct object reference to a promise
would have been a breaking change anyway.
Also fixed incorrect documentation index for ve.init.mw.ViewPageTarget.init.
Bug: T53569
Change-Id: Ibfa6abbeaf872ae2aadc6ed9d5beba7473ea441a
2015-02-26 01:22:44 +00:00
|
|
|
} )
|
2024-04-30 16:44:25 +00:00
|
|
|
.then( () => {
|
2017-10-04 12:52:05 +00:00
|
|
|
if ( !active ) {
|
|
|
|
// Loading was aborted
|
|
|
|
// TODO: Make loaders abortable instead of waiting
|
|
|
|
targetPromise = null;
|
|
|
|
return $.Deferred().reject().promise();
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const target = ve.init.mw.targetFactory.create(
|
2016-10-27 22:29:38 +00:00
|
|
|
conf.contentModels[ mw.config.get( 'wgPageContentModel' ) ], {
|
2019-04-10 19:41:02 +00:00
|
|
|
modes: availableModes,
|
2017-01-19 13:27:30 +00:00
|
|
|
defaultMode: mode
|
2016-10-27 22:29:38 +00:00
|
|
|
}
|
2016-06-24 15:04:32 +00:00
|
|
|
);
|
2024-04-30 16:44:25 +00:00
|
|
|
target.on( 'deactivate', () => {
|
2022-02-16 14:41:04 +00:00
|
|
|
active = false;
|
2022-08-10 16:11:16 +00:00
|
|
|
updateTabs( false );
|
2022-02-16 14:41:04 +00:00
|
|
|
} );
|
2024-04-30 16:44:25 +00:00
|
|
|
target.on( 'reactivate', () => {
|
2022-12-17 15:44:56 +00:00
|
|
|
url = new URL( location.href );
|
2022-02-16 15:03:38 +00:00
|
|
|
activateTarget(
|
2022-12-17 15:44:56 +00:00
|
|
|
getEditModeFromUrl( url ),
|
|
|
|
parseSection( url.searchParams.get( 'section' ) )
|
2022-02-16 15:03:38 +00:00
|
|
|
);
|
|
|
|
} );
|
2022-06-21 18:01:30 +00:00
|
|
|
target.setContainer( $targetContainer );
|
2016-07-26 03:04:47 +00:00
|
|
|
targetLoaded = true;
|
2014-09-07 22:47:58 +00:00
|
|
|
return target;
|
2024-04-30 16:44:25 +00:00
|
|
|
}, ( e ) => {
|
2014-12-03 23:35:48 +00:00
|
|
|
mw.log.warn( 'VisualEditor failed to load: ' + e );
|
2024-09-18 00:31:37 +00:00
|
|
|
return $.Deferred().reject( e ).promise();
|
2014-09-07 22:47:58 +00:00
|
|
|
} );
|
2013-07-03 22:14:52 +00:00
|
|
|
}
|
2015-03-31 19:52:14 +00:00
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
targetPromise.then( ( target ) => {
|
2016-12-08 19:19:17 +00:00
|
|
|
target.section = section;
|
2015-03-31 19:52:14 +00:00
|
|
|
} );
|
|
|
|
|
2014-09-07 22:47:58 +00:00
|
|
|
return targetPromise;
|
2013-07-03 22:14:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 00:10:55 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {Object} initData
|
|
|
|
* @param {URL} [linkUrl]
|
|
|
|
*/
|
|
|
|
function trackActivateStart( initData, linkUrl ) {
|
|
|
|
if ( !linkUrl ) {
|
2022-12-17 15:44:56 +00:00
|
|
|
linkUrl = url;
|
2022-06-15 07:54:15 +00:00
|
|
|
}
|
2022-12-17 15:44:56 +00:00
|
|
|
if ( linkUrl.searchParams.get( 'wvprov' ) === 'sticky-header' ) {
|
2022-06-15 07:54:15 +00:00
|
|
|
initData.mechanism += '-sticky-header';
|
|
|
|
}
|
2017-12-07 11:14:00 +00:00
|
|
|
ve.track( 'trace.activate.enter', { mode: initData.mode } );
|
2023-03-18 02:34:32 +00:00
|
|
|
initData.action = 'init';
|
|
|
|
initData.integration = 'page';
|
|
|
|
ve.track( 'editAttemptStep', initData );
|
2016-10-28 00:22:30 +00:00
|
|
|
mw.libs.ve.activationStart = ve.now();
|
|
|
|
}
|
2015-10-14 23:34:34 +00:00
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Get the skin-specific message for an edit tab
|
|
|
|
*
|
|
|
|
* @param {string} tabMsg Base tab message key
|
|
|
|
* @return {string} Message text
|
|
|
|
*/
|
2023-10-12 18:03:39 +00:00
|
|
|
function getTabMessage( tabMsg ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
let tabMsgKey = tabMessages[ tabMsg ];
|
|
|
|
const skinMsgKeys = {
|
2023-10-12 18:03:39 +00:00
|
|
|
edit: 'edit',
|
|
|
|
create: 'create',
|
|
|
|
editlocaldescription: 'edit-local',
|
|
|
|
createlocaldescription: 'create-local'
|
|
|
|
};
|
2024-05-21 14:22:56 +00:00
|
|
|
const key = skinMsgKeys[ tabMsg ];
|
2023-10-12 18:03:39 +00:00
|
|
|
if ( !tabMsgKey && key ) {
|
|
|
|
// Some skins don't use the default skin message keys.
|
2019-11-01 16:20:22 +00:00
|
|
|
// The following messages can be used here:
|
|
|
|
// * vector-view-edit
|
|
|
|
// * vector-view-create
|
2023-10-12 18:03:39 +00:00
|
|
|
// * vector-view-edit-local
|
|
|
|
// * vector-view-create-local
|
2019-11-01 16:20:22 +00:00
|
|
|
// * messages for other skins
|
2023-10-12 18:03:39 +00:00
|
|
|
tabMsgKey = mw.config.get( 'skin' ) + '-view-' + key;
|
2019-11-01 16:20:22 +00:00
|
|
|
if ( !mw.message( tabMsgKey ).exists() ) {
|
2023-10-12 18:03:39 +00:00
|
|
|
// The following messages can be used here:
|
|
|
|
// * skin-view-edit
|
|
|
|
// * skin-view-create
|
|
|
|
// * skin-view-edit-local
|
|
|
|
// * skin-view-create-local
|
2022-06-13 18:21:32 +00:00
|
|
|
tabMsgKey = 'skin-view-' + key;
|
2019-05-13 21:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-12 18:47:01 +00:00
|
|
|
// eslint-disable-next-line mediawiki/msg-doc
|
2024-05-21 14:22:56 +00:00
|
|
|
const msg = mw.message( tabMsgKey );
|
2023-10-12 18:47:01 +00:00
|
|
|
if ( !msg.isParseable() ) {
|
|
|
|
mw.log.warn( 'VisualEditor: MediaWiki:' + tabMsgKey + ' contains unsupported syntax. ' +
|
|
|
|
'https://www.mediawiki.org/wiki/Manual:Messages_API#Feature_support_in_JavaScript' );
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return msg.text();
|
2019-05-13 21:51:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 20:22:18 +00:00
|
|
|
/**
|
|
|
|
* Set the user's new preferred editor
|
|
|
|
*
|
|
|
|
* @param {string} editor Preferred editor, 'visualeditor' or 'wikitext'
|
|
|
|
* @return {jQuery.Promise} Promise which resolves when the preference has been set
|
|
|
|
*/
|
2016-10-28 00:22:30 +00:00
|
|
|
function setEditorPreference( editor ) {
|
2020-02-26 20:22:18 +00:00
|
|
|
// If visual mode isn't available, don't set the editor preference as the
|
|
|
|
// user has expressed no choice by opening this editor. (T246259)
|
|
|
|
// Strictly speaking the same thing should happen if visual mode is
|
|
|
|
// available but source mode isn't, but that is never the case.
|
2023-03-08 15:12:07 +00:00
|
|
|
if ( !init.isVisualAvailable ) {
|
2020-02-26 20:22:18 +00:00
|
|
|
return $.Deferred().resolve().promise();
|
|
|
|
}
|
|
|
|
|
2016-10-28 00:22:30 +00:00
|
|
|
if ( editor !== 'visualeditor' && editor !== 'wikitext' ) {
|
|
|
|
throw new Error( 'setEditorPreference called with invalid option: ', editor );
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
let key = pageExists ? 'edit' : 'create',
|
2021-05-05 17:06:37 +00:00
|
|
|
sectionKey = 'editsection';
|
|
|
|
|
2016-10-28 00:22:30 +00:00
|
|
|
if (
|
|
|
|
mw.config.get( 'wgVisualEditorConfig' ).singleEditTab &&
|
|
|
|
tabPreference === 'remember-last'
|
|
|
|
) {
|
|
|
|
if ( $( '#ca-view-foreign' ).length ) {
|
|
|
|
key += 'localdescription';
|
|
|
|
}
|
|
|
|
if ( editor === 'wikitext' ) {
|
|
|
|
key += 'source';
|
|
|
|
sectionKey += 'source';
|
2015-10-23 16:29:56 +00:00
|
|
|
}
|
2015-10-14 23:34:34 +00:00
|
|
|
|
2019-11-01 16:20:22 +00:00
|
|
|
$( '#ca-edit a' ).text( getTabMessage( key ) );
|
|
|
|
$( '.mw-editsection a' ).text( getTabMessage( sectionKey ) );
|
2016-10-28 00:22:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 16:42:18 +00:00
|
|
|
mw.cookie.set( 'VEE', editor, { path: '/', expires: 30 * 86400, prefix: '' } );
|
2020-02-26 20:22:18 +00:00
|
|
|
|
|
|
|
// Save user preference if logged in
|
|
|
|
if (
|
2023-04-28 13:57:46 +00:00
|
|
|
mw.user.isNamed() &&
|
2020-02-26 20:22:18 +00:00
|
|
|
mw.user.options.get( 'visualeditor-editor' ) !== editor
|
|
|
|
) {
|
|
|
|
// Same as ve.init.target.getLocalApi()
|
2024-04-30 16:44:25 +00:00
|
|
|
return new mw.Api().saveOption( 'visualeditor-editor', editor ).then( () => {
|
2020-02-26 20:22:18 +00:00
|
|
|
mw.user.options.set( 'visualeditor-editor', editor );
|
|
|
|
} );
|
2015-10-14 23:34:34 +00:00
|
|
|
}
|
2020-02-26 20:22:18 +00:00
|
|
|
return $.Deferred().resolve().promise();
|
2015-10-14 23:34:34 +00:00
|
|
|
}
|
2015-12-12 00:51:32 +00:00
|
|
|
|
2022-08-10 16:11:16 +00:00
|
|
|
/**
|
|
|
|
* Update state of editing tabs
|
|
|
|
*
|
|
|
|
* @param {boolean} editing Whether the editor is loaded
|
|
|
|
* @param {string} [mode='visual'] Edit mode ('visual' or 'source')
|
|
|
|
* @param {boolean} [isNewSection] Adding a new section
|
|
|
|
*/
|
|
|
|
function updateTabs( editing, mode, isNewSection ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
let $tab;
|
2022-08-10 16:11:16 +00:00
|
|
|
|
|
|
|
if ( editing ) {
|
|
|
|
if ( isNewSection ) {
|
|
|
|
$tab = $( '#ca-addsection' );
|
|
|
|
} else if ( $( '#ca-ve-edit' ).length ) {
|
|
|
|
if ( !mode || mode === 'visual' ) {
|
|
|
|
$tab = $( '#ca-ve-edit' );
|
|
|
|
} else {
|
|
|
|
$tab = $( '#ca-edit' );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Single edit tab
|
|
|
|
$tab = $( '#ca-edit' );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$tab = $( '#ca-view' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deselect current mode (e.g. "view" or "history") in skins that have
|
|
|
|
// separate tab sections for content actions and namespaces, like Vector.
|
|
|
|
$( '#p-views' ).find( 'li.selected' ).removeClass( 'selected' );
|
|
|
|
// In skins like MonoBook that don't have the separate tab sections,
|
|
|
|
// deselect the known tabs for editing modes (when switching or exiting editor).
|
|
|
|
$( '#ca-edit, #ca-ve-edit, #ca-addsection' ).not( $tab ).removeClass( 'selected' );
|
|
|
|
|
|
|
|
$tab.addClass( 'selected' );
|
|
|
|
}
|
|
|
|
|
2022-01-23 01:11:51 +00:00
|
|
|
/**
|
|
|
|
* Scroll to a specific heading before VE loads
|
|
|
|
*
|
|
|
|
* Similar to ve.init.mw.ArticleTarget.prototype.scrollToHeading
|
|
|
|
*
|
|
|
|
* @param {string} section Parsed section (string)
|
|
|
|
*/
|
|
|
|
function scrollToSection( section ) {
|
|
|
|
if ( section === '0' || section === 'new' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
let $heading;
|
2024-05-01 11:36:18 +00:00
|
|
|
$( '#mw-content-text .mw-editsection a:not( .mw-editsection-visualeditor )' ).each( ( i, el ) => {
|
2024-05-21 14:22:56 +00:00
|
|
|
const linkUrl = new URL( el.href );
|
2022-12-17 15:44:56 +00:00
|
|
|
if ( section === parseSection( linkUrl.searchParams.get( 'section' ) ) ) {
|
2024-05-01 11:36:18 +00:00
|
|
|
$heading = $( el ).closest( '.mw-heading, h1, h2, h3, h4, h5, h6' );
|
2022-01-23 01:11:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
// When loading on action=edit URLs, there is no page content
|
|
|
|
if ( !$heading || !$heading.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
let offset = 0;
|
|
|
|
const enableVisualSectionEditing = mw.config.get( 'wgVisualEditorConfig' ).enableVisualSectionEditing;
|
2022-01-23 01:11:51 +00:00
|
|
|
if ( enableVisualSectionEditing === true || enableVisualSectionEditing === 'desktop' ) {
|
2022-11-04 18:53:43 +00:00
|
|
|
// Heading will jump to the top of the page in visual section editing.
|
|
|
|
// This measurement already includes the height of $toolbarPlaceholder.
|
2022-01-24 23:35:50 +00:00
|
|
|
offset = $( '#mw-content-text' ).offset().top;
|
2022-01-23 01:11:51 +00:00
|
|
|
} else {
|
|
|
|
// Align with top of heading margin. Doesn't apply in visual section editing as the margin collapses.
|
2022-11-04 18:53:43 +00:00
|
|
|
offset = parseInt( $heading.css( 'margin-top' ) ) + $toolbarPlaceholder.outerHeight();
|
2022-01-23 01:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Support for CSS `scroll-behavior: smooth;` and JS `window.scroll( { behavior: 'smooth' } )`
|
|
|
|
// is correlated:
|
|
|
|
// * https://caniuse.com/css-scroll-behavior
|
|
|
|
// * https://caniuse.com/mdn-api_window_scroll_options_behavior_parameter
|
2024-05-21 14:22:56 +00:00
|
|
|
const supportsSmoothScroll = 'scrollBehavior' in document.documentElement.style;
|
|
|
|
const newScrollTop = $heading.offset().top - offset;
|
2022-01-23 01:11:51 +00:00
|
|
|
if ( supportsSmoothScroll ) {
|
|
|
|
window.scroll( {
|
|
|
|
top: newScrollTop,
|
|
|
|
behavior: 'smooth'
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
// Ideally we would use OO.ui.Element.static.getRootScrollableElement here
|
|
|
|
// as it has slightly better browser support (Chrome < 60)
|
2024-05-21 14:22:56 +00:00
|
|
|
const scrollContainer = document.documentElement;
|
2022-01-23 01:11:51 +00:00
|
|
|
|
|
|
|
$( scrollContainer ).animate( {
|
|
|
|
scrollTop: newScrollTop
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 05:09:50 +00:00
|
|
|
/**
|
Load RL modules in one load.php request, rather than in two stages
This introduces TargetLoader, which manages plugins and RL modules
in a slightly more generic fashion so that Targets themselves don't
have to. This allows us to load all RL modules in one load.php
request, rather than first loading ViewPageTarget which then
loads the other modules.
TargetLoader loads in the bottom queue, so it will be loaded
as part of the main load.php request, but in VPT.init.js we
still have to wait for it with using() because it might not
have arrived yet. This also degrades gracefully on cached pages
where TargetLoader isn't in the bottom queue: it'll be loaded
as a separate request instead, which is suboptimal but no
worse that what we were doing before.
Right now TargetLoader is small enough that it could also be in
the top queue, but in the future we want to add things like
the action=visualeditor API request to it, and mw.Api is
relatively big.
Note: this also makes a breaking change to the plugin API:
plugin callbacks no longer receive the target instance
as a parameter, as they're now executed before the target
has been constructed rather than after. In the long term,
if we want to give plugins access to the target instance,
we could give them the target promise somehow. For now,
I've killed this feature because nothing used it and
the change from a direct object reference to a promise
would have been a breaking change anyway.
Also fixed incorrect documentation index for ve.init.mw.ViewPageTarget.init.
Bug: T53569
Change-Id: Ibfa6abbeaf872ae2aadc6ed9d5beba7473ea441a
2015-02-26 01:22:44 +00:00
|
|
|
* Load and activate the target.
|
|
|
|
*
|
|
|
|
* If you need to call methods on the target before activate is called, call getTarget()
|
|
|
|
* yourself, chain your work onto that promise, and pass that chained promise in as targetPromise.
|
|
|
|
* E.g. `activateTarget( getTarget().then( function( target ) { target.doAThing(); } ) );`
|
2015-02-21 05:09:50 +00:00
|
|
|
*
|
Load RL modules in one load.php request, rather than in two stages
This introduces TargetLoader, which manages plugins and RL modules
in a slightly more generic fashion so that Targets themselves don't
have to. This allows us to load all RL modules in one load.php
request, rather than first loading ViewPageTarget which then
loads the other modules.
TargetLoader loads in the bottom queue, so it will be loaded
as part of the main load.php request, but in VPT.init.js we
still have to wait for it with using() because it might not
have arrived yet. This also degrades gracefully on cached pages
where TargetLoader isn't in the bottom queue: it'll be loaded
as a separate request instead, which is suboptimal but no
worse that what we were doing before.
Right now TargetLoader is small enough that it could also be in
the top queue, but in the future we want to add things like
the action=visualeditor API request to it, and mw.Api is
relatively big.
Note: this also makes a breaking change to the plugin API:
plugin callbacks no longer receive the target instance
as a parameter, as they're now executed before the target
has been constructed rather than after. In the long term,
if we want to give plugins access to the target instance,
we could give them the target promise somehow. For now,
I've killed this feature because nothing used it and
the change from a direct object reference to a promise
would have been a breaking change anyway.
Also fixed incorrect documentation index for ve.init.mw.ViewPageTarget.init.
Bug: T53569
Change-Id: Ibfa6abbeaf872ae2aadc6ed9d5beba7473ea441a
2015-02-26 01:22:44 +00:00
|
|
|
* @private
|
2016-05-26 12:08:26 +00:00
|
|
|
* @param {string} mode Target mode: 'visual' or 'source'
|
2021-12-21 18:48:27 +00:00
|
|
|
* @param {string} [section] Section to edit.
|
|
|
|
* If visual section editing is not enabled, we will jump to the start of this section, and still
|
|
|
|
* the heading to prefix the edit summary.
|
2020-08-18 12:16:49 +00:00
|
|
|
* @param {jQuery.Promise} [tPromise] Promise that will be resolved with a ve.init.mw.DesktopArticleTarget
|
2023-05-29 00:10:55 +00:00
|
|
|
* @param {boolean} [modified=false] The page has been modified before loading (e.g. in source mode)
|
2015-02-21 05:09:50 +00:00
|
|
|
*/
|
2020-08-18 12:16:49 +00:00
|
|
|
function activateTarget( mode, section, tPromise, modified ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
let dataPromise;
|
2022-01-24 23:35:50 +00:00
|
|
|
|
2022-08-10 16:11:16 +00:00
|
|
|
updateTabs( true, mode, section === 'new' );
|
|
|
|
|
2016-07-26 03:04:47 +00:00
|
|
|
// Only call requestPageData early if the target object isn't there yet.
|
|
|
|
// If the target object is there, this is a second or subsequent load, and the
|
|
|
|
// internal state of the target object can influence the load request.
|
|
|
|
if ( !targetLoaded ) {
|
|
|
|
// The TargetLoader module is loaded in the bottom queue, so it should have been
|
|
|
|
// requested already but it might not have finished loading yet
|
|
|
|
dataPromise = mw.loader.using( 'ext.visualEditor.targetLoader' )
|
2024-04-30 16:44:25 +00:00
|
|
|
.then( () => mw.libs.ve.targetLoader.requestPageData( mode, mw.config.get( 'wgRelevantPageName' ), {
|
|
|
|
sessionStore: true,
|
|
|
|
section: section,
|
|
|
|
oldId: oldId,
|
|
|
|
// Should be ve.init.mw.DesktopArticleTarget.static.trackingName, but the
|
|
|
|
// class hasn't loaded yet.
|
|
|
|
// This is used for stats tracking, so do not change!
|
|
|
|
targetName: 'mwTarget',
|
|
|
|
modified: modified,
|
|
|
|
editintro: url.searchParams.get( 'editintro' ),
|
|
|
|
preload: url.searchParams.get( 'preload' ),
|
|
|
|
preloadparams: mw.util.getArrayParam( 'preloadparams', url.searchParams ),
|
|
|
|
// If switching to visual with modifications, check if we have wikitext to convert
|
|
|
|
wikitext: mode === 'visual' && modified ? $( '#wpTextbox1' ).textSelection( 'getContents' ) : undefined
|
|
|
|
} ) );
|
2018-02-21 22:58:50 +00:00
|
|
|
|
|
|
|
dataPromise
|
2024-04-30 16:44:25 +00:00
|
|
|
.then( ( response ) => {
|
2018-02-21 22:58:50 +00:00
|
|
|
if (
|
|
|
|
// Check target promise hasn't already failed (isLoading=false)
|
|
|
|
isLoading &&
|
|
|
|
// TODO: Support tempWikitextEditor when section=new (T185633)
|
|
|
|
mode === 'source' && section !== 'new' &&
|
|
|
|
// Can't use temp editor when recovering an autosave
|
|
|
|
!( response.visualeditor && response.visualeditor.recovered )
|
|
|
|
) {
|
2017-10-18 22:50:31 +00:00
|
|
|
setupTempWikitextEditor( response.visualeditor );
|
|
|
|
}
|
|
|
|
} )
|
2018-02-21 22:58:50 +00:00
|
|
|
.then( incrementLoadingProgress );
|
2016-07-26 03:04:47 +00:00
|
|
|
}
|
2015-03-16 16:07:14 +00:00
|
|
|
|
2022-02-15 15:10:43 +00:00
|
|
|
// Do this before section scrolling
|
|
|
|
showToolbarPlaceholder();
|
|
|
|
mw.hook( 've.activationStart' ).fire();
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
let visibleSection = null;
|
|
|
|
let visibleSectionOffset = null;
|
2021-12-21 18:48:27 +00:00
|
|
|
if ( section === null ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
let firstVisibleEditSection = null;
|
2024-05-01 11:36:18 +00:00
|
|
|
$( '#firstHeading, #mw-content-text .mw-editsection' ).each( ( i, el ) => {
|
2024-05-21 14:22:56 +00:00
|
|
|
const top = el.getBoundingClientRect().top;
|
2021-12-21 18:48:27 +00:00
|
|
|
if ( top > 0 ) {
|
2024-05-01 11:36:18 +00:00
|
|
|
firstVisibleEditSection = el;
|
2021-12-21 18:48:27 +00:00
|
|
|
// break
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2022-01-28 00:02:09 +00:00
|
|
|
if ( firstVisibleEditSection && firstVisibleEditSection.id !== 'firstHeading' ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const firstVisibleSectionLink = firstVisibleEditSection.querySelector( 'a' );
|
|
|
|
const linkUrl = new URL( firstVisibleSectionLink.href );
|
2022-12-17 15:44:56 +00:00
|
|
|
visibleSection = parseSection( linkUrl.searchParams.get( 'section' ) );
|
2021-12-21 18:48:27 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const firstVisibleHeading = $( firstVisibleEditSection ).closest( '.mw-heading, h1, h2, h3, h4, h5, h6' )[ 0 ];
|
2021-12-21 18:48:27 +00:00
|
|
|
visibleSectionOffset = firstVisibleHeading.getBoundingClientRect().top;
|
|
|
|
}
|
2022-01-23 01:11:51 +00:00
|
|
|
} else if ( mode === 'visual' ) {
|
|
|
|
scrollToSection( section );
|
2021-12-21 18:48:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 22:50:31 +00:00
|
|
|
showLoading( mode );
|
2015-05-09 13:47:10 +00:00
|
|
|
incrementLoadingProgress();
|
|
|
|
active = true;
|
2015-03-16 16:07:14 +00:00
|
|
|
|
2020-08-18 12:16:49 +00:00
|
|
|
tPromise = tPromise || getTarget( mode, section );
|
|
|
|
tPromise
|
2024-04-30 16:44:25 +00:00
|
|
|
.then( ( target ) => {
|
2021-12-21 18:48:27 +00:00
|
|
|
target.visibleSection = visibleSection;
|
|
|
|
target.visibleSectionOffset = visibleSectionOffset;
|
|
|
|
|
2015-05-09 13:47:10 +00:00
|
|
|
incrementLoadingProgress();
|
2016-11-12 16:11:59 +00:00
|
|
|
// If target was already loaded, ensure the mode is correct
|
2016-11-30 12:03:56 +00:00
|
|
|
target.setDefaultMode( mode );
|
2022-03-27 00:49:37 +00:00
|
|
|
// syncTempWikitextEditor modified the result object in the dataPromise
|
|
|
|
if ( tempWikitextEditor ) {
|
|
|
|
syncTempWikitextEditor();
|
|
|
|
}
|
2022-01-24 23:35:50 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const deactivating = target.deactivatingDeferred || $.Deferred().resolve();
|
2024-04-30 16:44:25 +00:00
|
|
|
return deactivating.then( () => {
|
2023-06-20 07:13:57 +00:00
|
|
|
target.currentUrl = new URL( location.href );
|
2024-05-21 14:22:56 +00:00
|
|
|
const activatePromise = target.activate( dataPromise );
|
2022-04-07 20:10:41 +00:00
|
|
|
|
|
|
|
// toolbarSetupDeferred resolves slightly before activatePromise, use done
|
|
|
|
// to run in the same paint cycle as the VE toolbar being drawn
|
2024-04-30 16:44:25 +00:00
|
|
|
target.toolbarSetupDeferred.done( () => {
|
2022-04-07 20:10:41 +00:00
|
|
|
hideToolbarPlaceholder();
|
|
|
|
} );
|
2022-01-24 23:35:50 +00:00
|
|
|
|
2022-04-07 20:10:41 +00:00
|
|
|
return activatePromise;
|
|
|
|
} );
|
2015-02-21 05:09:50 +00:00
|
|
|
} )
|
2024-04-30 16:44:25 +00:00
|
|
|
.then( () => {
|
2017-10-18 22:50:31 +00:00
|
|
|
if ( mode === 'visual' ) {
|
2023-03-18 02:34:32 +00:00
|
|
|
// `action: 'ready'` has already been fired for source mode in setupTempWikitextEditor
|
|
|
|
ve.track( 'editAttemptStep', { action: 'ready', mode: mode } );
|
2022-03-27 00:49:37 +00:00
|
|
|
} else if ( !tempWikitextEditor ) {
|
2018-05-02 16:34:48 +00:00
|
|
|
// We're in source mode, but skipped the
|
|
|
|
// tempWikitextEditor, so make sure we do relevant
|
|
|
|
// tracking / hooks:
|
2023-03-18 02:34:32 +00:00
|
|
|
ve.track( 'editAttemptStep', { action: 'ready', mode: mode } );
|
2018-01-22 18:27:43 +00:00
|
|
|
mw.hook( 've.wikitextInteractive' ).fire();
|
2017-10-18 22:50:31 +00:00
|
|
|
}
|
2023-03-18 02:34:32 +00:00
|
|
|
ve.track( 'editAttemptStep', { action: 'loaded', mode: mode } );
|
2015-02-21 05:09:50 +00:00
|
|
|
} )
|
2017-10-04 12:52:05 +00:00
|
|
|
.always( clearLoading );
|
2015-02-21 05:09:50 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 00:10:55 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {string} mode Target mode: 'visual' or 'source'
|
|
|
|
* @param {string} [section]
|
|
|
|
* @param {boolean} [modified=false] The page has been modified before loading (e.g. in source mode)
|
|
|
|
* @param {URL} [linkUrl] URL to navigate to, potentially with extra parameters
|
|
|
|
*/
|
|
|
|
function activatePageTarget( mode, section, modified, linkUrl ) {
|
|
|
|
trackActivateStart( { type: 'page', mechanism: mw.config.get( 'wgArticleId' ) ? 'click' : 'new', mode: mode }, linkUrl );
|
2016-06-30 01:11:13 +00:00
|
|
|
|
2016-10-28 00:22:30 +00:00
|
|
|
if ( !active ) {
|
2023-06-20 07:13:57 +00:00
|
|
|
// Replace the current state with one that is tagged as ours, to prevent the
|
|
|
|
// back button from breaking when used to exit VE. FIXME: there should be a better
|
|
|
|
// way to do this. See also similar code in the DesktopArticleTarget constructor.
|
|
|
|
history.replaceState( { tag: 'visualeditor' }, '', url );
|
|
|
|
// Set action=edit or veaction=edit/editsource
|
|
|
|
// Use linkUrl to preserve parameters like 'editintro' (T56029)
|
|
|
|
history.pushState( { tag: 'visualeditor' }, '', linkUrl || ( mode === 'source' ? veEditSourceUrl : veEditUrl ) );
|
|
|
|
// Update URL instance
|
|
|
|
url = linkUrl || veEditUrl;
|
2016-06-30 01:11:13 +00:00
|
|
|
|
2019-04-05 13:51:14 +00:00
|
|
|
activateTarget( mode, section, undefined, modified );
|
2016-03-08 23:12:39 +00:00
|
|
|
}
|
2015-12-12 00:51:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Get the last mode a user used
|
|
|
|
*
|
|
|
|
* @return {string|null} 'visualeditor', 'wikitext' or null
|
|
|
|
*/
|
2015-10-23 16:29:56 +00:00
|
|
|
function getLastEditor() {
|
2017-04-27 15:23:26 +00:00
|
|
|
// This logic matches VisualEditorHooks::getLastEditor
|
2024-05-21 14:22:56 +00:00
|
|
|
let editor = mw.cookie.get( 'VEE', '' );
|
2023-04-28 13:57:46 +00:00
|
|
|
// Set editor to user's preference or site's default (ignore the cookie) if …
|
2016-03-07 19:27:23 +00:00
|
|
|
if (
|
|
|
|
// … user is logged in,
|
2023-04-28 13:57:46 +00:00
|
|
|
mw.user.isNamed() ||
|
2016-03-07 19:27:23 +00:00
|
|
|
// … no cookie is set, or
|
|
|
|
!editor ||
|
|
|
|
// value is invalid.
|
|
|
|
!( editor === 'visualeditor' || editor === 'wikitext' )
|
|
|
|
) {
|
2015-10-23 16:29:56 +00:00
|
|
|
editor = mw.user.options.get( 'visualeditor-editor' );
|
|
|
|
}
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
2022-08-31 15:25:20 +00:00
|
|
|
/**
|
|
|
|
* Get the preferred editor for this edit page
|
|
|
|
*
|
|
|
|
* For the preferred *available* editor, use getAvailableEditPageEditor.
|
|
|
|
*
|
2024-03-13 22:33:27 +00:00
|
|
|
* @return {string|null} 'visualeditor', 'wikitext' or null
|
2022-08-31 15:25:20 +00:00
|
|
|
*/
|
2020-05-07 13:30:52 +00:00
|
|
|
function getEditPageEditor() {
|
|
|
|
// This logic matches VisualEditorHooks::getEditPageEditor
|
2019-05-19 12:54:32 +00:00
|
|
|
// !!+ casts '0' to false
|
2024-05-21 14:22:56 +00:00
|
|
|
const isRedLink = !!+url.searchParams.get( 'redlink' );
|
2019-05-19 12:54:32 +00:00
|
|
|
// On dual-edit-tab wikis, the edit page must mean the user wants wikitext,
|
|
|
|
// unless following a redlink
|
|
|
|
if ( !mw.config.get( 'wgVisualEditorConfig' ).singleEditTab && !isRedLink ) {
|
2017-05-15 11:33:40 +00:00
|
|
|
return 'wikitext';
|
|
|
|
}
|
|
|
|
|
2016-12-29 20:08:53 +00:00
|
|
|
switch ( tabPreference ) {
|
|
|
|
case 'prefer-ve':
|
|
|
|
return 'visualeditor';
|
|
|
|
case 'prefer-wt':
|
|
|
|
return 'wikitext';
|
2017-04-27 15:23:26 +00:00
|
|
|
case 'multi-tab':
|
|
|
|
// 'multi-tab'
|
|
|
|
// TODO: See VisualEditor.hooks.php
|
2019-05-19 12:54:32 +00:00
|
|
|
return isRedLink ?
|
|
|
|
getLastEditor() :
|
|
|
|
'wikitext';
|
2020-05-07 13:30:52 +00:00
|
|
|
case 'remember-last':
|
|
|
|
default:
|
|
|
|
return getLastEditor();
|
2016-12-29 20:08:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 15:25:20 +00:00
|
|
|
/**
|
|
|
|
* Get the preferred editor which is also available on this edit page
|
|
|
|
*
|
|
|
|
* @return {string} 'visual' or 'source'
|
|
|
|
*/
|
|
|
|
function getAvailableEditPageEditor() {
|
|
|
|
switch ( getEditPageEditor() ) {
|
|
|
|
case 'visualeditor':
|
|
|
|
if ( init.isVisualAvailable ) {
|
|
|
|
return 'visual';
|
|
|
|
}
|
|
|
|
if ( init.isWikitextAvailable ) {
|
|
|
|
return 'source';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case 'wikitext':
|
|
|
|
default:
|
|
|
|
return init.isWikitextAvailable ? 'source' : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Check if a boolean preference is set in user options, mw.storage or a cookie
|
|
|
|
*
|
|
|
|
* @param {string} prefName Preference name
|
|
|
|
* @param {string} storageKey mw.storage key
|
|
|
|
* @param {string} cookieName Cookie name
|
|
|
|
* @return {boolean} Preference is set
|
|
|
|
*/
|
2020-05-02 01:49:00 +00:00
|
|
|
function checkPreferenceOrStorage( prefName, storageKey, cookieName ) {
|
|
|
|
storageKey = storageKey || prefName;
|
|
|
|
cookieName = cookieName || storageKey;
|
2024-03-13 22:33:27 +00:00
|
|
|
return !!( mw.user.options.get( prefName ) ||
|
2020-05-02 01:49:00 +00:00
|
|
|
(
|
2023-04-28 13:57:46 +00:00
|
|
|
!mw.user.isNamed() && (
|
2020-05-02 01:49:00 +00:00
|
|
|
mw.storage.get( storageKey ) ||
|
2020-07-30 16:42:18 +00:00
|
|
|
mw.cookie.get( cookieName, '' )
|
2020-05-02 01:49:00 +00:00
|
|
|
)
|
2024-03-13 22:33:27 +00:00
|
|
|
)
|
|
|
|
);
|
2020-05-02 01:49:00 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Set a boolean preference to true in user options, mw.storage or a cookie
|
|
|
|
*
|
|
|
|
* @param {string} prefName Preference name
|
|
|
|
* @param {string} storageKey mw.storage key
|
|
|
|
* @param {string} cookieName Cookie name
|
|
|
|
*/
|
2020-05-02 01:49:00 +00:00
|
|
|
function setPreferenceOrStorage( prefName, storageKey, cookieName ) {
|
|
|
|
storageKey = storageKey || prefName;
|
|
|
|
cookieName = cookieName || storageKey;
|
2023-04-28 13:57:46 +00:00
|
|
|
if ( !mw.user.isNamed() ) {
|
2020-05-02 01:49:00 +00:00
|
|
|
// Try local storage first; if that fails, set a cookie
|
|
|
|
if ( !mw.storage.set( storageKey, 1 ) ) {
|
2020-07-30 16:42:18 +00:00
|
|
|
mw.cookie.set( cookieName, 1, { path: '/', expires: 30 * 86400, prefix: '' } );
|
2020-05-02 01:49:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
new mw.Api().saveOption( prefName, '1' );
|
|
|
|
mw.user.options.set( prefName, '1' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-03 22:14:52 +00:00
|
|
|
conf = mw.config.get( 'wgVisualEditorConfig' );
|
2013-08-02 20:25:44 +00:00
|
|
|
tabMessages = conf.tabMessages;
|
2022-12-17 15:44:56 +00:00
|
|
|
viewUrl = new URL( mw.util.getUrl( mw.config.get( 'wgRelevantPageName' ) ), location.href );
|
|
|
|
url = new URL( location.href );
|
|
|
|
// T156998: Don't trust 'oldid' query parameter, it'll be wrong if 'diff' or 'direction'
|
2017-03-23 21:32:00 +00:00
|
|
|
// is set to 'next' or 'prev'.
|
2017-09-11 15:03:18 +00:00
|
|
|
oldId = mw.config.get( 'wgRevisionId' ) || $( 'input[name=parentRevId]' ).val();
|
2023-09-06 17:21:12 +00:00
|
|
|
if ( oldId === mw.config.get( 'wgCurRevisionId' ) || mw.config.get( 'wgEditLatestRevision' ) ) {
|
2018-05-01 00:09:28 +00:00
|
|
|
// The page may have been edited by someone else after we loaded it, setting this to "undefined"
|
|
|
|
// indicates that we should load the actual latest revision.
|
2017-09-11 15:03:18 +00:00
|
|
|
oldId = undefined;
|
2017-04-07 10:49:30 +00:00
|
|
|
}
|
2014-11-19 22:40:41 +00:00
|
|
|
pageExists = !!mw.config.get( 'wgRelevantArticleId' );
|
2024-05-21 14:22:56 +00:00
|
|
|
const isViewPage = mw.config.get( 'wgIsArticle' ) && !url.searchParams.has( 'diff' );
|
|
|
|
const wgAction = mw.config.get( 'wgAction' );
|
|
|
|
const isEditPage = wgAction === 'edit' || wgAction === 'submit';
|
|
|
|
const pageCanLoadEditor = isViewPage || isEditPage;
|
|
|
|
const pageIsProbablyEditable = mw.config.get( 'wgIsProbablyEditable' ) ||
|
2022-08-11 06:07:39 +00:00
|
|
|
mw.config.get( 'wgRelevantPageIsProbablyEditable' );
|
2013-07-03 22:14:52 +00:00
|
|
|
|
2017-10-16 17:02:00 +00:00
|
|
|
// Cast "0" (T89513)
|
2024-05-21 14:22:56 +00:00
|
|
|
const enable = !!+mw.user.options.get( 'visualeditor-enable' );
|
|
|
|
const tempdisable = !!+mw.user.options.get( 'visualeditor-betatempdisable' );
|
|
|
|
const autodisable = !!+mw.user.options.get( 'visualeditor-autodisable' );
|
2017-10-16 17:02:00 +00:00
|
|
|
tabPreference = mw.user.options.get( 'visualeditor-tabs' );
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* The only edit tab shown to the user is for visual mode
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2017-10-16 17:02:00 +00:00
|
|
|
function isOnlyTabVE() {
|
2022-08-31 15:25:20 +00:00
|
|
|
return conf.singleEditTab && getAvailableEditPageEditor() === 'visual';
|
2017-10-16 17:02:00 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* The only edit tab shown to the user is for source mode
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2017-10-16 17:02:00 +00:00
|
|
|
function isOnlyTabWikitext() {
|
2022-08-31 15:25:20 +00:00
|
|
|
return conf.singleEditTab && getAvailableEditPageEditor() === 'source';
|
2017-10-16 17:02:00 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 22:14:52 +00:00
|
|
|
init = {
|
Infrastructure for loading plugins in the MW integration
Server-side, plugins can register themselves by adding to
$wgVisualEditorPluginModules. This is the recommended way for
MW extensions to extend VE. Client-side, plugins can register
themselves through mw.libs.ve.addPlugin(), which takes a string
(RL module name) or a callback.
When VisualEditor loads, we load the registered plugin modules in
parallel with ext.visualEditor.core. Note that they're loaded in
parallel, not after, and so the plugins should explicitly depend
on ext.visualEditor.core if they use or extend classes in VE core.
Once the modules finish loading and user and site scripts have run,
we execute the registered plugin callbacks. These callbacks can
optionally return a promise. We gather these promises and wait for
all of them to be resolved, then initialize the editor.
This allows Gadgets to extend VE by top-loading a small module that
depends on ext.visualEditor.viewPageTarget.init and calls
mw.libs.ve.addPlugin( 'ext.gadget.bottomHalfGadget' ); , the bottom
half being a hidden Gadget that depends on ext.visualEditor.core and
contains the actual code. The addPlugin() call needs to be in a
top-loading module because otherwise there's no guarantee that the
plugin will be registered before the user clicks edit and VE loads.
User and site scripts can extend VE by simply calling addPlugin()
directly, as mw.libs.ve is already present when user scripts run (since
it's top-loaded) and VE waits for 'user' and 'site' to run before
executing plugins.
If user/site scripts need to load additional JS files, they can load
these with $.getScript() and return the corresponding promise:
mw.libs.ve.addPlugin( function() { return $.getScript( 'URL' ); } );
For a diagram of all this, see
https://www.mediawiki.org/wiki/File:VE-plugin-infrastructure.jpg :)
VisualEditor.php:
* Add $wgVisualEditorPluginModules
VisualEditor.hooks.php:
* Expose $wgVisualEditorPluginModules in JS
ve.init.mw.ViewPageTarget.init.js:
* Add mw.libs.ve.addPlugin function that just stores the registered
values in an array and passes them into the mw.Target when it's
being initialized
ve.init.mw.Target.js:
* Add $wgVisualEditorPluginModules to the set of modules to load when
initializing VE
* Add a Deferred (this.modulesReady) to track module loading
* Add addPlugin() and addPlugins() methods that add to either
this.modules or this.pluginCallbacks
* In load(), instead of mw.loader.load()ing this.modules, use using()
to load this.modules plus user and site, and fire onModulesReady()
when they're loaded
* In onModulesReady(), execute the registered callbacks, gather the
returned promises, wait for all of them to be resolved, then resolve
this.modulesReady
* Fire onReady based on this.modulesReady being resolved, rather than
using a second using() call
Bug: 50514
Change-Id: Ib7d87a17eaac6ecdb8b0803b13840d7ee58902df
2013-07-22 20:34:28 +00:00
|
|
|
/**
|
|
|
|
* Add a plugin module or function.
|
|
|
|
*
|
|
|
|
* Plugins are run after VisualEditor is loaded, but before it is initialized. This allows
|
|
|
|
* plugins to add classes and register them with the factories and registries.
|
|
|
|
*
|
|
|
|
* The parameter to this function can be a ResourceLoader module name or a function.
|
|
|
|
*
|
|
|
|
* If it's a module name, it will be loaded together with the VisualEditor core modules when
|
|
|
|
* VE is loaded. No special care is taken to ensure that the module runs after the VE
|
|
|
|
* classes are loaded, so if this is desired, the module should depend on
|
|
|
|
* ext.visualEditor.core .
|
|
|
|
*
|
|
|
|
* If it's a function, it will be invoked once the VisualEditor core modules and any
|
|
|
|
* plugin modules registered through this function have been loaded, but before the editor
|
Load RL modules in one load.php request, rather than in two stages
This introduces TargetLoader, which manages plugins and RL modules
in a slightly more generic fashion so that Targets themselves don't
have to. This allows us to load all RL modules in one load.php
request, rather than first loading ViewPageTarget which then
loads the other modules.
TargetLoader loads in the bottom queue, so it will be loaded
as part of the main load.php request, but in VPT.init.js we
still have to wait for it with using() because it might not
have arrived yet. This also degrades gracefully on cached pages
where TargetLoader isn't in the bottom queue: it'll be loaded
as a separate request instead, which is suboptimal but no
worse that what we were doing before.
Right now TargetLoader is small enough that it could also be in
the top queue, but in the future we want to add things like
the action=visualeditor API request to it, and mw.Api is
relatively big.
Note: this also makes a breaking change to the plugin API:
plugin callbacks no longer receive the target instance
as a parameter, as they're now executed before the target
has been constructed rather than after. In the long term,
if we want to give plugins access to the target instance,
we could give them the target promise somehow. For now,
I've killed this feature because nothing used it and
the change from a direct object reference to a promise
would have been a breaking change anyway.
Also fixed incorrect documentation index for ve.init.mw.ViewPageTarget.init.
Bug: T53569
Change-Id: Ibfa6abbeaf872ae2aadc6ed9d5beba7473ea441a
2015-02-26 01:22:44 +00:00
|
|
|
* is intialized. The function can optionally return a jQuery.Promise . VisualEditor will
|
Infrastructure for loading plugins in the MW integration
Server-side, plugins can register themselves by adding to
$wgVisualEditorPluginModules. This is the recommended way for
MW extensions to extend VE. Client-side, plugins can register
themselves through mw.libs.ve.addPlugin(), which takes a string
(RL module name) or a callback.
When VisualEditor loads, we load the registered plugin modules in
parallel with ext.visualEditor.core. Note that they're loaded in
parallel, not after, and so the plugins should explicitly depend
on ext.visualEditor.core if they use or extend classes in VE core.
Once the modules finish loading and user and site scripts have run,
we execute the registered plugin callbacks. These callbacks can
optionally return a promise. We gather these promises and wait for
all of them to be resolved, then initialize the editor.
This allows Gadgets to extend VE by top-loading a small module that
depends on ext.visualEditor.viewPageTarget.init and calls
mw.libs.ve.addPlugin( 'ext.gadget.bottomHalfGadget' ); , the bottom
half being a hidden Gadget that depends on ext.visualEditor.core and
contains the actual code. The addPlugin() call needs to be in a
top-loading module because otherwise there's no guarantee that the
plugin will be registered before the user clicks edit and VE loads.
User and site scripts can extend VE by simply calling addPlugin()
directly, as mw.libs.ve is already present when user scripts run (since
it's top-loaded) and VE waits for 'user' and 'site' to run before
executing plugins.
If user/site scripts need to load additional JS files, they can load
these with $.getScript() and return the corresponding promise:
mw.libs.ve.addPlugin( function() { return $.getScript( 'URL' ); } );
For a diagram of all this, see
https://www.mediawiki.org/wiki/File:VE-plugin-infrastructure.jpg :)
VisualEditor.php:
* Add $wgVisualEditorPluginModules
VisualEditor.hooks.php:
* Expose $wgVisualEditorPluginModules in JS
ve.init.mw.ViewPageTarget.init.js:
* Add mw.libs.ve.addPlugin function that just stores the registered
values in an array and passes them into the mw.Target when it's
being initialized
ve.init.mw.Target.js:
* Add $wgVisualEditorPluginModules to the set of modules to load when
initializing VE
* Add a Deferred (this.modulesReady) to track module loading
* Add addPlugin() and addPlugins() methods that add to either
this.modules or this.pluginCallbacks
* In load(), instead of mw.loader.load()ing this.modules, use using()
to load this.modules plus user and site, and fire onModulesReady()
when they're loaded
* In onModulesReady(), execute the registered callbacks, gather the
returned promises, wait for all of them to be resolved, then resolve
this.modulesReady
* Fire onReady based on this.modulesReady being resolved, rather than
using a second using() call
Bug: 50514
Change-Id: Ib7d87a17eaac6ecdb8b0803b13840d7ee58902df
2013-07-22 20:34:28 +00:00
|
|
|
* only be initialized once all promises returned by plugin functions have been resolved.
|
|
|
|
*
|
|
|
|
* // Register ResourceLoader module
|
2014-12-01 19:59:53 +00:00
|
|
|
* mw.libs.ve.addPlugin( 'ext.gadget.foobar' );
|
Infrastructure for loading plugins in the MW integration
Server-side, plugins can register themselves by adding to
$wgVisualEditorPluginModules. This is the recommended way for
MW extensions to extend VE. Client-side, plugins can register
themselves through mw.libs.ve.addPlugin(), which takes a string
(RL module name) or a callback.
When VisualEditor loads, we load the registered plugin modules in
parallel with ext.visualEditor.core. Note that they're loaded in
parallel, not after, and so the plugins should explicitly depend
on ext.visualEditor.core if they use or extend classes in VE core.
Once the modules finish loading and user and site scripts have run,
we execute the registered plugin callbacks. These callbacks can
optionally return a promise. We gather these promises and wait for
all of them to be resolved, then initialize the editor.
This allows Gadgets to extend VE by top-loading a small module that
depends on ext.visualEditor.viewPageTarget.init and calls
mw.libs.ve.addPlugin( 'ext.gadget.bottomHalfGadget' ); , the bottom
half being a hidden Gadget that depends on ext.visualEditor.core and
contains the actual code. The addPlugin() call needs to be in a
top-loading module because otherwise there's no guarantee that the
plugin will be registered before the user clicks edit and VE loads.
User and site scripts can extend VE by simply calling addPlugin()
directly, as mw.libs.ve is already present when user scripts run (since
it's top-loaded) and VE waits for 'user' and 'site' to run before
executing plugins.
If user/site scripts need to load additional JS files, they can load
these with $.getScript() and return the corresponding promise:
mw.libs.ve.addPlugin( function() { return $.getScript( 'URL' ); } );
For a diagram of all this, see
https://www.mediawiki.org/wiki/File:VE-plugin-infrastructure.jpg :)
VisualEditor.php:
* Add $wgVisualEditorPluginModules
VisualEditor.hooks.php:
* Expose $wgVisualEditorPluginModules in JS
ve.init.mw.ViewPageTarget.init.js:
* Add mw.libs.ve.addPlugin function that just stores the registered
values in an array and passes them into the mw.Target when it's
being initialized
ve.init.mw.Target.js:
* Add $wgVisualEditorPluginModules to the set of modules to load when
initializing VE
* Add a Deferred (this.modulesReady) to track module loading
* Add addPlugin() and addPlugins() methods that add to either
this.modules or this.pluginCallbacks
* In load(), instead of mw.loader.load()ing this.modules, use using()
to load this.modules plus user and site, and fire onModulesReady()
when they're loaded
* In onModulesReady(), execute the registered callbacks, gather the
returned promises, wait for all of them to be resolved, then resolve
this.modulesReady
* Fire onReady based on this.modulesReady being resolved, rather than
using a second using() call
Bug: 50514
Change-Id: Ib7d87a17eaac6ecdb8b0803b13840d7ee58902df
2013-07-22 20:34:28 +00:00
|
|
|
*
|
|
|
|
* // Register a callback
|
2024-05-01 11:36:18 +00:00
|
|
|
* mw.libs.ve.addPlugin( ( target ) => {
|
Infrastructure for loading plugins in the MW integration
Server-side, plugins can register themselves by adding to
$wgVisualEditorPluginModules. This is the recommended way for
MW extensions to extend VE. Client-side, plugins can register
themselves through mw.libs.ve.addPlugin(), which takes a string
(RL module name) or a callback.
When VisualEditor loads, we load the registered plugin modules in
parallel with ext.visualEditor.core. Note that they're loaded in
parallel, not after, and so the plugins should explicitly depend
on ext.visualEditor.core if they use or extend classes in VE core.
Once the modules finish loading and user and site scripts have run,
we execute the registered plugin callbacks. These callbacks can
optionally return a promise. We gather these promises and wait for
all of them to be resolved, then initialize the editor.
This allows Gadgets to extend VE by top-loading a small module that
depends on ext.visualEditor.viewPageTarget.init and calls
mw.libs.ve.addPlugin( 'ext.gadget.bottomHalfGadget' ); , the bottom
half being a hidden Gadget that depends on ext.visualEditor.core and
contains the actual code. The addPlugin() call needs to be in a
top-loading module because otherwise there's no guarantee that the
plugin will be registered before the user clicks edit and VE loads.
User and site scripts can extend VE by simply calling addPlugin()
directly, as mw.libs.ve is already present when user scripts run (since
it's top-loaded) and VE waits for 'user' and 'site' to run before
executing plugins.
If user/site scripts need to load additional JS files, they can load
these with $.getScript() and return the corresponding promise:
mw.libs.ve.addPlugin( function() { return $.getScript( 'URL' ); } );
For a diagram of all this, see
https://www.mediawiki.org/wiki/File:VE-plugin-infrastructure.jpg :)
VisualEditor.php:
* Add $wgVisualEditorPluginModules
VisualEditor.hooks.php:
* Expose $wgVisualEditorPluginModules in JS
ve.init.mw.ViewPageTarget.init.js:
* Add mw.libs.ve.addPlugin function that just stores the registered
values in an array and passes them into the mw.Target when it's
being initialized
ve.init.mw.Target.js:
* Add $wgVisualEditorPluginModules to the set of modules to load when
initializing VE
* Add a Deferred (this.modulesReady) to track module loading
* Add addPlugin() and addPlugins() methods that add to either
this.modules or this.pluginCallbacks
* In load(), instead of mw.loader.load()ing this.modules, use using()
to load this.modules plus user and site, and fire onModulesReady()
when they're loaded
* In onModulesReady(), execute the registered callbacks, gather the
returned promises, wait for all of them to be resolved, then resolve
this.modulesReady
* Fire onReady based on this.modulesReady being resolved, rather than
using a second using() call
Bug: 50514
Change-Id: Ib7d87a17eaac6ecdb8b0803b13840d7ee58902df
2013-07-22 20:34:28 +00:00
|
|
|
* ve.dm.Foobar = .....
|
|
|
|
* } );
|
|
|
|
*
|
|
|
|
* // Register a callback that loads another script
|
2024-05-01 11:36:18 +00:00
|
|
|
* mw.libs.ve.addPlugin( () => $.getScript( 'http://example.com/foobar.js' ) );
|
Infrastructure for loading plugins in the MW integration
Server-side, plugins can register themselves by adding to
$wgVisualEditorPluginModules. This is the recommended way for
MW extensions to extend VE. Client-side, plugins can register
themselves through mw.libs.ve.addPlugin(), which takes a string
(RL module name) or a callback.
When VisualEditor loads, we load the registered plugin modules in
parallel with ext.visualEditor.core. Note that they're loaded in
parallel, not after, and so the plugins should explicitly depend
on ext.visualEditor.core if they use or extend classes in VE core.
Once the modules finish loading and user and site scripts have run,
we execute the registered plugin callbacks. These callbacks can
optionally return a promise. We gather these promises and wait for
all of them to be resolved, then initialize the editor.
This allows Gadgets to extend VE by top-loading a small module that
depends on ext.visualEditor.viewPageTarget.init and calls
mw.libs.ve.addPlugin( 'ext.gadget.bottomHalfGadget' ); , the bottom
half being a hidden Gadget that depends on ext.visualEditor.core and
contains the actual code. The addPlugin() call needs to be in a
top-loading module because otherwise there's no guarantee that the
plugin will be registered before the user clicks edit and VE loads.
User and site scripts can extend VE by simply calling addPlugin()
directly, as mw.libs.ve is already present when user scripts run (since
it's top-loaded) and VE waits for 'user' and 'site' to run before
executing plugins.
If user/site scripts need to load additional JS files, they can load
these with $.getScript() and return the corresponding promise:
mw.libs.ve.addPlugin( function() { return $.getScript( 'URL' ); } );
For a diagram of all this, see
https://www.mediawiki.org/wiki/File:VE-plugin-infrastructure.jpg :)
VisualEditor.php:
* Add $wgVisualEditorPluginModules
VisualEditor.hooks.php:
* Expose $wgVisualEditorPluginModules in JS
ve.init.mw.ViewPageTarget.init.js:
* Add mw.libs.ve.addPlugin function that just stores the registered
values in an array and passes them into the mw.Target when it's
being initialized
ve.init.mw.Target.js:
* Add $wgVisualEditorPluginModules to the set of modules to load when
initializing VE
* Add a Deferred (this.modulesReady) to track module loading
* Add addPlugin() and addPlugins() methods that add to either
this.modules or this.pluginCallbacks
* In load(), instead of mw.loader.load()ing this.modules, use using()
to load this.modules plus user and site, and fire onModulesReady()
when they're loaded
* In onModulesReady(), execute the registered callbacks, gather the
returned promises, wait for all of them to be resolved, then resolve
this.modulesReady
* Fire onReady based on this.modulesReady being resolved, rather than
using a second using() call
Bug: 50514
Change-Id: Ib7d87a17eaac6ecdb8b0803b13840d7ee58902df
2013-07-22 20:34:28 +00:00
|
|
|
*
|
|
|
|
* @param {string|Function} plugin Module name or callback that optionally returns a promise
|
|
|
|
*/
|
2013-12-06 02:34:44 +00:00
|
|
|
addPlugin: function ( plugin ) {
|
Infrastructure for loading plugins in the MW integration
Server-side, plugins can register themselves by adding to
$wgVisualEditorPluginModules. This is the recommended way for
MW extensions to extend VE. Client-side, plugins can register
themselves through mw.libs.ve.addPlugin(), which takes a string
(RL module name) or a callback.
When VisualEditor loads, we load the registered plugin modules in
parallel with ext.visualEditor.core. Note that they're loaded in
parallel, not after, and so the plugins should explicitly depend
on ext.visualEditor.core if they use or extend classes in VE core.
Once the modules finish loading and user and site scripts have run,
we execute the registered plugin callbacks. These callbacks can
optionally return a promise. We gather these promises and wait for
all of them to be resolved, then initialize the editor.
This allows Gadgets to extend VE by top-loading a small module that
depends on ext.visualEditor.viewPageTarget.init and calls
mw.libs.ve.addPlugin( 'ext.gadget.bottomHalfGadget' ); , the bottom
half being a hidden Gadget that depends on ext.visualEditor.core and
contains the actual code. The addPlugin() call needs to be in a
top-loading module because otherwise there's no guarantee that the
plugin will be registered before the user clicks edit and VE loads.
User and site scripts can extend VE by simply calling addPlugin()
directly, as mw.libs.ve is already present when user scripts run (since
it's top-loaded) and VE waits for 'user' and 'site' to run before
executing plugins.
If user/site scripts need to load additional JS files, they can load
these with $.getScript() and return the corresponding promise:
mw.libs.ve.addPlugin( function() { return $.getScript( 'URL' ); } );
For a diagram of all this, see
https://www.mediawiki.org/wiki/File:VE-plugin-infrastructure.jpg :)
VisualEditor.php:
* Add $wgVisualEditorPluginModules
VisualEditor.hooks.php:
* Expose $wgVisualEditorPluginModules in JS
ve.init.mw.ViewPageTarget.init.js:
* Add mw.libs.ve.addPlugin function that just stores the registered
values in an array and passes them into the mw.Target when it's
being initialized
ve.init.mw.Target.js:
* Add $wgVisualEditorPluginModules to the set of modules to load when
initializing VE
* Add a Deferred (this.modulesReady) to track module loading
* Add addPlugin() and addPlugins() methods that add to either
this.modules or this.pluginCallbacks
* In load(), instead of mw.loader.load()ing this.modules, use using()
to load this.modules plus user and site, and fire onModulesReady()
when they're loaded
* In onModulesReady(), execute the registered callbacks, gather the
returned promises, wait for all of them to be resolved, then resolve
this.modulesReady
* Fire onReady based on this.modulesReady being resolved, rather than
using a second using() call
Bug: 50514
Change-Id: Ib7d87a17eaac6ecdb8b0803b13840d7ee58902df
2013-07-22 20:34:28 +00:00
|
|
|
plugins.push( plugin );
|
|
|
|
},
|
|
|
|
|
2017-10-16 17:02:00 +00:00
|
|
|
/**
|
|
|
|
* Adjust edit page links in the current document
|
|
|
|
*
|
|
|
|
* This will run multiple times in a page lifecycle, notably when the
|
|
|
|
* page first loads and after post-save content replacement occurs. It
|
|
|
|
* needs to avoid doing anything which will cause problems if it's run
|
|
|
|
* twice or more.
|
|
|
|
*/
|
|
|
|
setupEditLinks: function () {
|
|
|
|
// NWE
|
|
|
|
if ( init.isWikitextAvailable && !isOnlyTabVE() ) {
|
|
|
|
$(
|
|
|
|
// Edit section links, except VE ones when both editors visible
|
|
|
|
'.mw-editsection a:not( .mw-editsection-visualeditor ),' +
|
|
|
|
// Edit tab
|
|
|
|
'#ca-edit a,' +
|
|
|
|
// Add section is currently a wikitext-only feature
|
|
|
|
'#ca-addsection a'
|
2024-05-01 11:36:18 +00:00
|
|
|
).each( ( i, el ) => {
|
|
|
|
if ( !el.href ) {
|
2023-02-13 21:57:23 +00:00
|
|
|
// Not a real link, probably added by a gadget or another extension (T328094)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const linkUrl = new URL( el.href );
|
2022-12-17 15:44:56 +00:00
|
|
|
if ( linkUrl.searchParams.has( 'action' ) ) {
|
|
|
|
linkUrl.searchParams.delete( 'action' );
|
|
|
|
linkUrl.searchParams.set( 'veaction', 'editsource' );
|
2024-05-01 11:36:18 +00:00
|
|
|
$( el ).attr( 'href', linkUrl.toString() );
|
2017-10-16 17:02:00 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the tabs appropriately if the user has VE on
|
2020-01-26 18:31:20 +00:00
|
|
|
if ( init.isAvailable ) {
|
2017-10-16 17:02:00 +00:00
|
|
|
// … on two-edit-tab wikis, or single-edit-tab wikis, where the user wants both …
|
2020-06-05 16:35:29 +00:00
|
|
|
if (
|
|
|
|
!init.isSingleEditTab && init.isVisualAvailable &&
|
|
|
|
// T253941: This option does not actually disable the editor, only leaves the tabs/links unchanged
|
2023-04-28 13:57:46 +00:00
|
|
|
!( conf.disableForAnons && mw.user.isAnon() )
|
2020-06-05 16:35:29 +00:00
|
|
|
) {
|
2017-10-16 17:02:00 +00:00
|
|
|
// … set the skin up with both tabs and both section edit links.
|
|
|
|
init.setupMultiTabSkin();
|
|
|
|
} else if (
|
|
|
|
pageCanLoadEditor && (
|
2020-01-26 18:53:57 +00:00
|
|
|
( init.isVisualAvailable && isOnlyTabVE() ) ||
|
2017-10-16 17:02:00 +00:00
|
|
|
( init.isWikitextAvailable && isOnlyTabWikitext() )
|
|
|
|
)
|
|
|
|
) {
|
2020-01-26 18:31:20 +00:00
|
|
|
// … on single-edit-tab wikis, where VE or NWE is the user's preferred editor
|
2017-10-16 17:02:00 +00:00
|
|
|
// Handle section edit link clicks
|
2024-04-30 16:44:25 +00:00
|
|
|
$( '.mw-editsection a' ).off( '.ve-target' ).on( 'click.ve-target', ( e ) => {
|
2017-10-16 17:02:00 +00:00
|
|
|
// isOnlyTabVE is computed on click as it may have changed since load
|
|
|
|
init.onEditSectionLinkClick( isOnlyTabVE() ? 'visual' : 'source', e );
|
|
|
|
} );
|
|
|
|
// Allow instant switching to edit mode, without refresh
|
2024-04-30 16:44:25 +00:00
|
|
|
$( '#ca-edit' ).off( '.ve-target' ).on( 'click.ve-target', ( e ) => {
|
2017-10-16 17:02:00 +00:00
|
|
|
init.onEditTabClick( isOnlyTabVE() ? 'visual' : 'source', e );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Setup multiple edit tabs and section links (edit + edit source)
|
|
|
|
*/
|
2016-11-08 12:47:30 +00:00
|
|
|
setupMultiTabSkin: function () {
|
|
|
|
init.setupMultiTabs();
|
|
|
|
init.setupMultiSectionLinks();
|
2013-07-05 06:01:31 +00:00
|
|
|
},
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Setup multiple edit tabs (edit + edit source)
|
|
|
|
*/
|
2016-11-08 12:47:30 +00:00
|
|
|
setupMultiTabs: function () {
|
2023-10-24 01:27:09 +00:00
|
|
|
// Minerva puts the '#ca-...' ids on <a> nodes, other skins put them on <li>
|
2024-05-21 14:22:56 +00:00
|
|
|
const $caEdit = $( '#ca-edit' );
|
|
|
|
const $caVeEdit = $( '#ca-ve-edit' );
|
2013-07-03 22:14:52 +00:00
|
|
|
|
2023-10-05 01:02:14 +00:00
|
|
|
if ( pageCanLoadEditor ) {
|
2015-11-06 18:23:44 +00:00
|
|
|
// Allow instant switching to edit mode, without refresh
|
2017-10-16 17:02:00 +00:00
|
|
|
$caVeEdit.off( '.ve-target' ).on( 'click.ve-target', init.onEditTabClick.bind( init, 'visual' ) );
|
2014-05-16 21:51:12 +00:00
|
|
|
}
|
2018-08-02 16:58:36 +00:00
|
|
|
if ( pageCanLoadEditor ) {
|
|
|
|
// Always bind "Edit source" tab, because we want to handle switching with changes
|
|
|
|
$caEdit.off( '.ve-target' ).on( 'click.ve-target', init.onEditTabClick.bind( init, 'source' ) );
|
|
|
|
}
|
2016-11-02 12:12:33 +00:00
|
|
|
if ( pageCanLoadEditor && init.isWikitextAvailable ) {
|
2018-08-02 16:58:36 +00:00
|
|
|
// Only bind "Add topic" tab if NWE is available, because VE doesn't support section
|
|
|
|
// so we never have to switch from it when editing a section
|
|
|
|
$( '#ca-addsection' ).off( '.ve-target' ).on( 'click.ve-target', init.onEditTabClick.bind( init, 'source' ) );
|
2016-05-26 12:08:26 +00:00
|
|
|
}
|
2014-05-16 21:51:12 +00:00
|
|
|
|
2016-09-20 22:37:00 +00:00
|
|
|
if ( init.isVisualAvailable ) {
|
2015-08-25 03:49:27 +00:00
|
|
|
if ( conf.tabPosition === 'before' ) {
|
|
|
|
$caEdit.addClass( 'collapsible' );
|
|
|
|
} else {
|
|
|
|
$caVeEdit.addClass( 'collapsible' );
|
|
|
|
}
|
2014-04-03 01:14:54 +00:00
|
|
|
}
|
2013-07-05 06:01:31 +00:00
|
|
|
},
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Setup multiple section links (edit + edit source)
|
|
|
|
*/
|
2016-11-08 12:47:30 +00:00
|
|
|
setupMultiSectionLinks: function () {
|
2016-11-02 12:12:33 +00:00
|
|
|
if ( pageCanLoadEditor ) {
|
2024-08-02 07:25:51 +00:00
|
|
|
const $editsections = $( '#mw-content-text .mw-editsection' );
|
|
|
|
|
2013-08-01 19:14:41 +00:00
|
|
|
// Only init without refresh if we're on a view page. Though section edit links
|
|
|
|
// are rarely shown on non-view pages, they appear in one other case, namely
|
|
|
|
// when on a diff against the latest version of a page. In that case we mustn't
|
2018-08-09 14:10:53 +00:00
|
|
|
// init without refresh as that'd initialise for the wrong rev id (T52925)
|
2013-08-01 19:14:41 +00:00
|
|
|
// and would preserve the wrong DOM with a diff on top.
|
2017-07-18 11:55:33 +00:00
|
|
|
$editsections.find( '.mw-editsection-visualeditor' )
|
2020-05-07 19:42:56 +00:00
|
|
|
.off( '.ve-target' ).on( 'click.ve-target', init.onEditSectionLinkClick.bind( init, 'visual' ) );
|
2016-10-24 22:36:44 +00:00
|
|
|
if ( init.isWikitextAvailable ) {
|
2017-07-18 11:55:33 +00:00
|
|
|
// TOOD: Make this less fragile
|
|
|
|
$editsections.find( 'a:not( .mw-editsection-visualeditor )' )
|
2020-05-07 19:42:56 +00:00
|
|
|
.off( '.ve-target' ).on( 'click.ve-target', init.onEditSectionLinkClick.bind( init, 'source' ) );
|
2016-09-06 19:16:55 +00:00
|
|
|
}
|
2013-08-01 19:14:41 +00:00
|
|
|
}
|
2013-07-05 06:01:31 +00:00
|
|
|
},
|
|
|
|
|
2016-08-22 19:40:34 +00:00
|
|
|
/**
|
|
|
|
* Check whether a jQuery event represents a plain left click, without
|
2021-10-19 19:02:49 +00:00
|
|
|
* any modifiers or a programmatically triggered click.
|
2016-08-22 19:40:34 +00:00
|
|
|
*
|
|
|
|
* This is a duplicate of a function in ve.utils, because this file runs
|
|
|
|
* before any of VE core or OOui has been loaded.
|
|
|
|
*
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {jQuery.Event} e
|
2016-08-22 19:40:34 +00:00
|
|
|
* @return {boolean} Whether it was an unmodified left click
|
|
|
|
*/
|
|
|
|
isUnmodifiedLeftClick: function ( e ) {
|
2021-10-19 19:02:49 +00:00
|
|
|
return e && ( (
|
|
|
|
e.which && e.which === 1 && !( e.shiftKey || e.altKey || e.ctrlKey || e.metaKey )
|
|
|
|
) || e.isTrigger );
|
2016-08-22 19:40:34 +00:00
|
|
|
},
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Handle click events on an edit tab
|
|
|
|
*
|
|
|
|
* @param {string} mode Edit mode, 'visual' or 'source'
|
|
|
|
* @param {Event} e Event
|
|
|
|
*/
|
2016-09-06 19:16:55 +00:00
|
|
|
onEditTabClick: function ( mode, e ) {
|
2016-08-22 19:40:34 +00:00
|
|
|
if ( !init.isUnmodifiedLeftClick( e ) ) {
|
2013-07-05 06:01:31 +00:00
|
|
|
return;
|
2013-07-03 22:14:52 +00:00
|
|
|
}
|
2018-08-02 16:58:36 +00:00
|
|
|
if ( !active && mode === 'source' && !init.isWikitextAvailable ) {
|
|
|
|
// We're not active so we don't need to manage a switch, and
|
|
|
|
// we don't have source mode available so we don't need to
|
|
|
|
// activate VE. Just follow the link.
|
|
|
|
return;
|
|
|
|
}
|
2015-11-04 10:10:28 +00:00
|
|
|
e.preventDefault();
|
2016-08-23 16:27:30 +00:00
|
|
|
if ( isLoading ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-08 19:24:10 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const section = $( e.target ).closest( '#ca-addsection' ).length ? 'new' : null;
|
2016-12-08 19:24:10 +00:00
|
|
|
|
2016-12-13 16:42:45 +00:00
|
|
|
if ( active ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
targetPromise.done( ( target ) => {
|
2018-06-27 23:25:36 +00:00
|
|
|
if ( target.getDefaultMode() === 'source' ) {
|
|
|
|
if ( mode === 'visual' ) {
|
|
|
|
target.switchToVisualEditor();
|
|
|
|
} else if ( mode === 'source' ) {
|
|
|
|
// Requested section may have changed --
|
|
|
|
// switchToWikitextSection will do nothing if the
|
|
|
|
// section is unchanged.
|
|
|
|
target.switchToWikitextSection( section );
|
|
|
|
}
|
|
|
|
} else if ( target.getDefaultMode() === 'visual' ) {
|
|
|
|
if ( mode === 'source' ) {
|
|
|
|
if ( section ) {
|
|
|
|
// switching from visual via the "add section" tab
|
|
|
|
target.switchToWikitextSection( section );
|
|
|
|
} else {
|
|
|
|
target.editSource();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Visual-to-visual doesn't need to do anything,
|
|
|
|
// because we don't have any section concerns. Just
|
|
|
|
// no-op it.
|
2016-09-07 00:02:48 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
} else {
|
2024-05-21 14:22:56 +00:00
|
|
|
const link = $( e.target ).closest( 'a' )[ 0 ];
|
|
|
|
const linkUrl = link && link.href ? new URL( link.href ) : null;
|
2016-12-13 16:42:45 +00:00
|
|
|
if ( section !== null ) {
|
2023-05-29 00:10:55 +00:00
|
|
|
init.activateVe( mode, linkUrl, section );
|
2016-12-13 16:42:45 +00:00
|
|
|
} else {
|
2023-02-13 21:28:06 +00:00
|
|
|
// Do not pass `section` to handle switching from section editing in WikiEditor if needed
|
2023-05-29 00:10:55 +00:00
|
|
|
init.activateVe( mode, linkUrl );
|
2016-12-13 16:42:45 +00:00
|
|
|
}
|
2016-08-23 16:27:30 +00:00
|
|
|
}
|
2015-11-04 10:10:28 +00:00
|
|
|
},
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Activate VE
|
2024-03-30 16:50:57 +00:00
|
|
|
*
|
2024-03-13 22:33:27 +00:00
|
|
|
* @param {string} mode Target mode: 'visual' or 'source'
|
|
|
|
* @param {URL} [linkUrl] URL to navigate to, potentially with extra parameters
|
|
|
|
* @param {string} [section]
|
|
|
|
*/
|
2023-05-29 00:10:55 +00:00
|
|
|
activateVe: function ( mode, linkUrl, section ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const wikitext = $( '#wpTextbox1' ).textSelection( 'getContents' ),
|
2019-04-18 18:38:15 +00:00
|
|
|
modified = mw.config.get( 'wgAction' ) === 'submit' ||
|
|
|
|
(
|
|
|
|
mw.config.get( 'wgAction' ) === 'edit' &&
|
|
|
|
wikitext !== initialWikitext
|
|
|
|
);
|
2013-07-05 06:01:31 +00:00
|
|
|
|
2023-02-13 21:28:06 +00:00
|
|
|
if ( section === undefined ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const sectionVal = $( 'input[name=wpSection]' ).val();
|
2023-02-13 21:28:06 +00:00
|
|
|
section = sectionVal !== '' && sectionVal !== undefined ? sectionVal : null;
|
|
|
|
}
|
|
|
|
|
2015-11-18 01:26:01 +00:00
|
|
|
// Close any open jQuery.UI dialogs (e.g. WikiEditor's find and replace)
|
|
|
|
if ( $.fn.dialog ) {
|
|
|
|
$( '.ui-dialog-content' ).dialog( 'close' );
|
|
|
|
}
|
|
|
|
|
2018-09-06 13:13:55 +00:00
|
|
|
// Release the edit warning on #wpTextbox1 which was setup in mediawiki.action.edit.editWarning.js
|
2023-06-24 10:21:30 +00:00
|
|
|
$( window ).off( 'beforeunload.editwarning' );
|
|
|
|
activatePageTarget( mode, section, modified, linkUrl );
|
2013-07-05 06:01:31 +00:00
|
|
|
},
|
|
|
|
|
2016-12-08 19:24:10 +00:00
|
|
|
/**
|
|
|
|
* Handle section edit links being clicked
|
|
|
|
*
|
|
|
|
* @param {string} mode Edit mode
|
|
|
|
* @param {jQuery.Event} e Click event
|
2020-03-31 20:04:30 +00:00
|
|
|
* @param {string} [section] Override edit section, taken from link URL if not specified
|
2016-12-08 19:24:10 +00:00
|
|
|
*/
|
|
|
|
onEditSectionLinkClick: function ( mode, e, section ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const link = $( e.target ).closest( 'a' )[ 0 ];
|
2023-06-20 07:48:35 +00:00
|
|
|
if ( !link || !link.href ) {
|
2023-02-13 21:57:23 +00:00
|
|
|
// Not a real link, probably added by a gadget or another extension (T328094)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const linkUrl = new URL( link.href );
|
|
|
|
const title = mw.Title.newFromText( linkUrl.searchParams.get( 'title' ) || '' );
|
2016-12-15 00:07:22 +00:00
|
|
|
|
2018-04-30 09:57:45 +00:00
|
|
|
if (
|
|
|
|
// Modified click (e.g. ctrl+click)
|
|
|
|
!init.isUnmodifiedLeftClick( e ) ||
|
|
|
|
// Not an edit action
|
2022-12-17 15:44:56 +00:00
|
|
|
!( linkUrl.searchParams.has( 'action' ) || linkUrl.searchParams.has( 'veaction' ) ) ||
|
2018-04-30 09:57:45 +00:00
|
|
|
// Edit target is on another host (e.g. commons file)
|
2022-12-17 15:44:56 +00:00
|
|
|
linkUrl.host !== location.host ||
|
2018-04-30 09:57:45 +00:00
|
|
|
// Title param doesn't match current page
|
|
|
|
title && title.getPrefixedText() !== new mw.Title( mw.config.get( 'wgRelevantPageName' ) ).getPrefixedText()
|
|
|
|
) {
|
2013-07-05 06:01:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-08-23 16:27:30 +00:00
|
|
|
e.preventDefault();
|
|
|
|
if ( isLoading ) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-05 06:01:31 +00:00
|
|
|
|
2023-05-29 00:10:55 +00:00
|
|
|
trackActivateStart( { type: 'section', mechanism: section === 'new' ? 'new' : 'click', mode: mode }, linkUrl );
|
2014-11-18 20:47:11 +00:00
|
|
|
|
2022-08-31 12:57:02 +00:00
|
|
|
if ( !active ) {
|
2023-06-20 07:13:57 +00:00
|
|
|
// Replace the current state with one that is tagged as ours, to prevent the
|
|
|
|
// back button from breaking when used to exit VE. FIXME: there should be a better
|
|
|
|
// way to do this. See also similar code in the DesktopArticleTarget constructor.
|
|
|
|
history.replaceState( { tag: 'visualeditor' }, '', url );
|
|
|
|
// Use linkUrl to preserve the 'section' parameter and others like 'editintro' (T56029)
|
|
|
|
history.pushState( { tag: 'visualeditor' }, '', linkUrl );
|
2022-12-17 15:44:56 +00:00
|
|
|
// Update URL instance
|
|
|
|
url = linkUrl;
|
2014-10-21 01:10:41 +00:00
|
|
|
|
2022-08-31 12:57:02 +00:00
|
|
|
// Use section from URL
|
|
|
|
if ( section === undefined ) {
|
2022-12-17 15:44:56 +00:00
|
|
|
section = parseSection( linkUrl.searchParams.get( 'section' ) );
|
2022-08-31 12:57:02 +00:00
|
|
|
}
|
2024-05-21 14:22:56 +00:00
|
|
|
const tPromise = getTarget( mode, section );
|
2023-06-20 07:30:57 +00:00
|
|
|
activateTarget( mode, section, tPromise );
|
2016-09-06 19:16:55 +00:00
|
|
|
}
|
2020-04-17 21:25:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the welcome dialog should be shown.
|
|
|
|
*
|
2020-04-17 21:30:29 +00:00
|
|
|
* The welcome dialog can be disabled in configuration; or by calling disableWelcomeDialog();
|
|
|
|
* or using a query string parameter; or if we've recorded that we've already shown it before
|
|
|
|
* in a user preference, local storage or a cookie.
|
2020-06-02 20:30:00 +00:00
|
|
|
*
|
2020-04-17 21:25:58 +00:00
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
shouldShowWelcomeDialog: function () {
|
|
|
|
return !(
|
|
|
|
// Disabled in config?
|
|
|
|
!mw.config.get( 'wgVisualEditorConfig' ).showBetaWelcome ||
|
2021-01-05 02:07:33 +00:00
|
|
|
// Disabled for the current request?
|
|
|
|
this.isWelcomeDialogSuppressed() ||
|
2024-03-13 22:05:45 +00:00
|
|
|
// Joining a collab session
|
|
|
|
url.searchParams.has( 'collabSession' ) ||
|
2021-01-05 02:07:33 +00:00
|
|
|
// Hidden using preferences, local storage or cookie?
|
|
|
|
checkPreferenceOrStorage( 'visualeditor-hidebetawelcome', 've-beta-welcome-dialog' )
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the welcome dialog is temporarily disabled.
|
2021-01-25 18:21:32 +00:00
|
|
|
*
|
2021-01-05 02:07:33 +00:00
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isWelcomeDialogSuppressed: function () {
|
|
|
|
return !!(
|
2020-04-17 21:30:29 +00:00
|
|
|
// Disabled by calling disableWelcomeDialog()?
|
|
|
|
welcomeDialogDisabled ||
|
2020-04-17 21:25:58 +00:00
|
|
|
// Hidden using URL parameter?
|
2022-12-17 15:44:56 +00:00
|
|
|
new URL( location.href ).searchParams.has( 'vehidebetadialog' ) ||
|
2020-04-17 21:25:58 +00:00
|
|
|
// Check for deprecated hidewelcomedialog parameter (T249954)
|
2022-12-17 15:44:56 +00:00
|
|
|
new URL( location.href ).searchParams.has( 'hidewelcomedialog' )
|
2020-04-17 21:25:58 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record that we've already shown the welcome dialog to this user, so that it won't be shown
|
|
|
|
* to them again.
|
|
|
|
*
|
|
|
|
* Uses a preference for logged-in users; uses local storage or a cookie for anonymous users.
|
|
|
|
*/
|
|
|
|
stopShowingWelcomeDialog: function () {
|
2020-05-02 01:49:00 +00:00
|
|
|
setPreferenceOrStorage( 'visualeditor-hidebetawelcome', 've-beta-welcome-dialog' );
|
2020-04-17 21:30:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevent the welcome dialog from being shown on this page view only.
|
|
|
|
*
|
|
|
|
* Causes shouldShowWelcomeDialog() to return false, but doesn't save anything to preferences
|
|
|
|
* or local storage, so future page views are not affected.
|
|
|
|
*/
|
|
|
|
disableWelcomeDialog: function () {
|
|
|
|
welcomeDialogDisabled = true;
|
2020-05-02 01:49:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2020-09-24 16:30:32 +00:00
|
|
|
* Check whether the user education popups (ve.ui.MWEducationPopupWidget) should be shown.
|
2020-05-02 01:49:00 +00:00
|
|
|
*
|
|
|
|
* The education popups can be disabled by calling disableWelcomeDialog(), or if we've
|
|
|
|
* recorded that we've already shown it before in a user preference, local storage or a cookie.
|
2020-06-02 20:30:00 +00:00
|
|
|
*
|
2020-05-02 01:49:00 +00:00
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
shouldShowEducationPopups: function () {
|
|
|
|
return !(
|
|
|
|
// Disabled by calling disableEducationPopups()?
|
|
|
|
educationPopupsDisabled ||
|
|
|
|
// Hidden using preferences, local storage, or cookie?
|
|
|
|
checkPreferenceOrStorage( 'visualeditor-hideusered', 've-hideusered' )
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record that we've already shown the education popups to this user, so that it won't be
|
|
|
|
* shown to them again.
|
|
|
|
*
|
|
|
|
* Uses a preference for logged-in users; uses local storage or a cookie for anonymous users.
|
|
|
|
*/
|
|
|
|
stopShowingEducationPopups: function () {
|
|
|
|
setPreferenceOrStorage( 'visualeditor-hideusered', 've-hideusered' );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevent the education popups from being shown on this page view only.
|
|
|
|
*
|
|
|
|
* Causes shouldShowEducationPopups() to return false, but doesn't save anything to
|
|
|
|
* preferences or local storage, so future page views are not affected.
|
|
|
|
*/
|
|
|
|
disableEducationPopups: function () {
|
|
|
|
educationPopupsDisabled = true;
|
2013-07-03 22:14:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-08 12:37:51 +00:00
|
|
|
init.isSingleEditTab = conf.singleEditTab && tabPreference !== 'multi-tab';
|
|
|
|
|
2022-12-17 15:44:56 +00:00
|
|
|
// On a view page, extend the current URL so extra parameters are carried over
|
|
|
|
// On a non-view page, use viewUrl
|
|
|
|
veEditUrl = new URL( pageCanLoadEditor ? url : viewUrl );
|
|
|
|
if ( oldId ) {
|
|
|
|
veEditUrl.searchParams.set( 'oldid', oldId );
|
|
|
|
}
|
|
|
|
veEditUrl.searchParams.delete( 'veaction' );
|
|
|
|
veEditUrl.searchParams.delete( 'action' );
|
2016-12-29 19:38:09 +00:00
|
|
|
if ( init.isSingleEditTab ) {
|
2022-12-17 15:44:56 +00:00
|
|
|
veEditUrl.searchParams.set( 'action', 'edit' );
|
|
|
|
veEditSourceUrl = veEditUrl;
|
2016-04-15 02:05:27 +00:00
|
|
|
} else {
|
2022-12-17 15:44:56 +00:00
|
|
|
veEditSourceUrl = new URL( veEditUrl );
|
|
|
|
veEditUrl.searchParams.set( 'veaction', 'edit' );
|
|
|
|
veEditSourceUrl.searchParams.set( 'veaction', 'editsource' );
|
2016-04-15 02:05:27 +00:00
|
|
|
}
|
2013-09-30 16:18:46 +00:00
|
|
|
|
2013-08-02 20:25:44 +00:00
|
|
|
// Whether VisualEditor should be available for the current user, page, wiki, mediawiki skin,
|
|
|
|
// browser etc.
|
2024-06-16 22:37:19 +00:00
|
|
|
init.isAvailable = VisualEditorSupportCheck();
|
|
|
|
// Extensions can disable VE in certain circumstances using the VisualEditorBeforeEditor hook (T174180)
|
2016-10-24 22:36:44 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const enabledForUser = (
|
2020-01-26 18:53:57 +00:00
|
|
|
// User has 'visualeditor-enable' preference enabled (for alpha opt-in)
|
|
|
|
// User has 'visualeditor-betatempdisable' preference disabled
|
|
|
|
// User has 'visualeditor-autodisable' preference disabled
|
2021-01-28 13:12:24 +00:00
|
|
|
( conf.isBeta ? enable : !tempdisable ) && !autodisable
|
2020-01-26 18:53:57 +00:00
|
|
|
);
|
|
|
|
|
2017-04-27 15:23:26 +00:00
|
|
|
// Duplicated in VisualEditor.hooks.php#isVisualAvailable()
|
2016-09-20 22:37:00 +00:00
|
|
|
init.isVisualAvailable = (
|
|
|
|
init.isAvailable &&
|
|
|
|
|
2020-01-26 18:53:57 +00:00
|
|
|
// If forced by the URL parameter, skip the namespace check (T221892) and preference check
|
2022-12-17 15:44:56 +00:00
|
|
|
( url.searchParams.get( 'veaction' ) === 'edit' || (
|
2019-05-09 20:37:28 +00:00
|
|
|
// Only in enabled namespaces
|
2020-01-26 18:53:57 +00:00
|
|
|
conf.namespaces.indexOf( new mw.Title( mw.config.get( 'wgRelevantPageName' ) ).getNamespaceId() ) !== -1 &&
|
|
|
|
|
|
|
|
// Enabled per user preferences
|
|
|
|
enabledForUser
|
|
|
|
) ) &&
|
2013-07-19 02:44:22 +00:00
|
|
|
|
2016-04-21 11:40:42 +00:00
|
|
|
// Only for pages with a supported content model
|
2018-07-06 00:10:20 +00:00
|
|
|
Object.prototype.hasOwnProperty.call( conf.contentModels, mw.config.get( 'wgPageContentModel' ) )
|
2013-07-19 02:44:22 +00:00
|
|
|
);
|
|
|
|
|
2017-04-27 15:23:26 +00:00
|
|
|
// Duplicated in VisualEditor.hooks.php#isWikitextAvailable()
|
2016-10-24 22:36:44 +00:00
|
|
|
init.isWikitextAvailable = (
|
|
|
|
init.isAvailable &&
|
|
|
|
|
2024-08-26 17:55:55 +00:00
|
|
|
// If forced by the URL parameter, skip the checks (T239796)
|
|
|
|
( url.searchParams.get( 'veaction' ) === 'editsource' || (
|
|
|
|
// Enabled on site
|
|
|
|
conf.enableWikitext &&
|
2016-10-24 22:36:44 +00:00
|
|
|
|
2024-08-26 17:55:55 +00:00
|
|
|
// User preference
|
|
|
|
mw.user.options.get( 'visualeditor-newwikitext' )
|
|
|
|
) ) &&
|
2016-10-24 22:36:44 +00:00
|
|
|
|
|
|
|
// Only on wikitext pages
|
|
|
|
mw.config.get( 'wgPageContentModel' ) === 'wikitext'
|
|
|
|
);
|
|
|
|
|
2019-04-10 19:41:02 +00:00
|
|
|
if ( init.isVisualAvailable ) {
|
|
|
|
availableModes.push( 'visual' );
|
|
|
|
}
|
|
|
|
|
2016-10-24 22:36:44 +00:00
|
|
|
if ( init.isWikitextAvailable ) {
|
2019-04-10 19:41:02 +00:00
|
|
|
availableModes.push( 'source' );
|
2016-10-24 22:36:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-12 00:51:32 +00:00
|
|
|
// FIXME: We should do this more elegantly
|
|
|
|
init.setEditorPreference = setEditorPreference;
|
|
|
|
|
2022-08-10 16:11:16 +00:00
|
|
|
init.updateTabs = updateTabs;
|
|
|
|
|
2024-06-16 22:37:19 +00:00
|
|
|
// Note: Though VisualEditor itself only needed this exposure for a very small reason
|
|
|
|
// (namely to access the old init.unsupportedList from the unit tests...) this has become one
|
|
|
|
// of the nicest ways to easily detect whether the VisualEditor initialisation code is present.
|
2013-08-03 04:33:03 +00:00
|
|
|
//
|
|
|
|
// The VE global was once available always, but now that platform integration initialisation
|
|
|
|
// is properly separated, it doesn't exist until the platform loads VisualEditor core.
|
|
|
|
//
|
2016-09-20 22:37:00 +00:00
|
|
|
// Most of mw.libs.ve is considered subject to change and private. An exception is that
|
|
|
|
// mw.libs.ve.isVisualAvailable is public, and indicates whether the VE editor itself can be loaded
|
2013-08-03 04:33:03 +00:00
|
|
|
// on this page. See above for why it may be false.
|
2015-08-05 19:20:31 +00:00
|
|
|
mw.libs.ve = $.extend( mw.libs.ve || {}, init );
|
2013-07-03 22:14:52 +00:00
|
|
|
|
2020-01-26 18:53:57 +00:00
|
|
|
if ( init.isVisualAvailable ) {
|
2013-09-04 17:57:29 +00:00
|
|
|
$( 'html' ).addClass( 've-available' );
|
|
|
|
} else {
|
2013-08-01 19:14:41 +00:00
|
|
|
$( 'html' ).addClass( 've-not-available' );
|
2013-09-04 17:57:29 +00:00
|
|
|
// Don't return here because we do want the skin setup to consistently happen
|
|
|
|
// for e.g. "Edit" > "Edit source" even when VE is not available.
|
2013-08-02 20:25:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 22:33:27 +00:00
|
|
|
/**
|
|
|
|
* Check if a URL doesn't contain any params which would prevent VE from loading, e.g. 'undo'
|
|
|
|
*
|
|
|
|
* @param {URL} editUrl
|
|
|
|
* @return {boolean} URL contains no unsupported params
|
|
|
|
*/
|
2022-12-17 15:44:56 +00:00
|
|
|
function isSupportedEditPage( editUrl ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
return configData.unsupportedEditParams.every( ( param ) => !editUrl.searchParams.has( param ) );
|
2022-02-16 14:53:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 15:25:20 +00:00
|
|
|
/**
|
2022-12-17 15:44:56 +00:00
|
|
|
* Get the edit mode for the given URL
|
2022-08-31 15:25:20 +00:00
|
|
|
*
|
2022-12-17 15:44:56 +00:00
|
|
|
* @param {URL} editUrl Edit URL
|
2024-03-13 22:05:45 +00:00
|
|
|
* @return {string|null} 'visual' or 'source', null if the editor is not being loaded
|
2022-08-31 15:25:20 +00:00
|
|
|
*/
|
2022-12-17 15:44:56 +00:00
|
|
|
function getEditModeFromUrl( editUrl ) {
|
2022-02-16 14:53:53 +00:00
|
|
|
if ( mw.config.get( 'wgDiscussionToolsStartNewTopicTool' ) ) {
|
|
|
|
// Avoid conflicts with DiscussionTools
|
2024-03-13 22:05:45 +00:00
|
|
|
return null;
|
2022-02-16 14:53:53 +00:00
|
|
|
}
|
2024-03-13 22:05:45 +00:00
|
|
|
if ( isViewPage && init.isAvailable ) {
|
|
|
|
// On view pages if veaction is correctly set
|
2024-05-21 14:22:56 +00:00
|
|
|
const mode = veactionToMode[ editUrl.searchParams.get( 'veaction' ) ] ||
|
2024-03-13 22:05:45 +00:00
|
|
|
// Always load VE visual mode if collabSession is set
|
|
|
|
( editUrl.searchParams.has( 'collabSession' ) ? 'visual' : null );
|
|
|
|
if ( mode && availableModes.indexOf( mode ) !== -1 ) {
|
|
|
|
return mode;
|
|
|
|
}
|
2022-02-16 14:53:53 +00:00
|
|
|
}
|
|
|
|
// Edit pages
|
2022-12-17 15:44:56 +00:00
|
|
|
if ( isEditPage && isSupportedEditPage( editUrl ) ) {
|
2022-02-16 14:53:53 +00:00
|
|
|
// User has disabled VE, or we are in view source only mode, or we have landed here with posted data
|
|
|
|
if ( !enabledForUser || $( '#ca-viewsource' ).length || mw.config.get( 'wgAction' ) === 'submit' ) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-08-31 15:25:20 +00:00
|
|
|
return getAvailableEditPageEditor();
|
2022-02-16 14:53:53 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
$( () => {
|
2022-09-02 17:05:21 +00:00
|
|
|
$targetContainer = $(
|
|
|
|
document.querySelector( '[data-mw-ve-target-container]' ) ||
|
|
|
|
document.getElementById( 'content' )
|
|
|
|
);
|
|
|
|
if ( pageCanLoadEditor ) {
|
|
|
|
$targetContainer.addClass( 've-init-mw-desktopArticleTarget-targetContainer' );
|
|
|
|
}
|
|
|
|
|
2024-05-21 16:40:36 +00:00
|
|
|
let showWikitextWelcome = true;
|
|
|
|
const numEditButtons = $( '#ca-edit, #ca-ve-edit' ).length,
|
2022-12-17 15:44:56 +00:00
|
|
|
section = parseSection( url.searchParams.get( 'section' ) );
|
2016-04-28 21:08:26 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const requiredSkinElements =
|
2022-06-21 18:01:30 +00:00
|
|
|
$targetContainer.length &&
|
2017-04-26 17:20:58 +00:00
|
|
|
$( '#mw-content-text' ).length &&
|
2018-09-25 12:06:01 +00:00
|
|
|
// A link to open the editor is technically not necessary if it's going to open itself
|
2022-10-24 23:44:20 +00:00
|
|
|
( isEditPage || numEditButtons );
|
2017-04-26 17:20:58 +00:00
|
|
|
|
2022-12-17 15:44:56 +00:00
|
|
|
if ( url.searchParams.get( 'action' ) === 'edit' && $( '#wpTextbox1' ).length ) {
|
2016-06-21 23:18:09 +00:00
|
|
|
initialWikitext = $( '#wpTextbox1' ).textSelection( 'getContents' );
|
2015-10-14 23:34:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 06:07:39 +00:00
|
|
|
if ( ( init.isVisualAvailable || init.isWikitextAvailable ) &&
|
|
|
|
pageCanLoadEditor &&
|
|
|
|
pageIsProbablyEditable &&
|
|
|
|
!requiredSkinElements
|
|
|
|
) {
|
2017-04-26 17:20:58 +00:00
|
|
|
mw.log.warn(
|
|
|
|
'Your skin is incompatible with VisualEditor. ' +
|
2020-08-24 13:46:49 +00:00
|
|
|
'See https://www.mediawiki.org/wiki/Extension:VisualEditor/Skin_requirements for the requirements.'
|
2017-04-26 17:20:58 +00:00
|
|
|
);
|
2022-11-02 22:34:57 +00:00
|
|
|
// If the edit buttons are not there it's likely a browser extension or gadget for anonymous user
|
|
|
|
// has removed them. We're not interested in errors from this scenario so don't log.
|
|
|
|
// If they exist log the error so we can address the problem.
|
|
|
|
if ( numEditButtons > 0 ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const err = new Error( 'Incompatible with VisualEditor' );
|
2022-11-02 22:34:57 +00:00
|
|
|
err.name = 'VeIncompatibleSkinWarning';
|
|
|
|
mw.errorLogger.logError( err, 'error.visualeditor' );
|
|
|
|
}
|
2017-04-26 17:20:58 +00:00
|
|
|
} else if ( init.isAvailable ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const mode = getEditModeFromUrl( url );
|
2017-04-25 15:36:30 +00:00
|
|
|
if ( mode ) {
|
|
|
|
showWikitextWelcome = false;
|
|
|
|
trackActivateStart( {
|
|
|
|
type: section === null ? 'page' : 'section',
|
2021-01-30 01:57:39 +00:00
|
|
|
mechanism: ( section === 'new' || !mw.config.get( 'wgArticleId' ) ) ? 'url-new' : 'url',
|
2017-12-07 11:14:00 +00:00
|
|
|
mode: mode
|
2017-04-25 15:36:30 +00:00
|
|
|
} );
|
|
|
|
activateTarget( mode, section );
|
|
|
|
} else if (
|
|
|
|
init.isVisualAvailable &&
|
|
|
|
pageCanLoadEditor &&
|
|
|
|
init.isSingleEditTab
|
|
|
|
) {
|
|
|
|
// In single edit tab mode we never have an edit tab
|
|
|
|
// with accesskey 'v' so create one
|
2018-12-13 16:44:25 +00:00
|
|
|
$( document.body ).append(
|
2017-04-25 15:36:30 +00:00
|
|
|
$( '<a>' )
|
2022-12-17 15:44:56 +00:00
|
|
|
.attr( { accesskey: mw.msg( 'accesskey-ca-ve-edit' ), href: veEditUrl } )
|
2017-04-25 15:36:30 +00:00
|
|
|
// Accesskey fires a click event
|
2017-10-16 17:02:00 +00:00
|
|
|
.on( 'click.ve-target', init.onEditTabClick.bind( init, 'visual' ) )
|
2017-10-18 22:50:31 +00:00
|
|
|
.addClass( 'oo-ui-element-hidden' )
|
2017-04-25 15:36:30 +00:00
|
|
|
);
|
|
|
|
}
|
2015-10-23 16:29:56 +00:00
|
|
|
|
2020-09-08 10:53:16 +00:00
|
|
|
// Add the switch button to WikiEditor on edit pages
|
2016-09-20 22:37:00 +00:00
|
|
|
if (
|
|
|
|
init.isVisualAvailable &&
|
2020-09-08 10:53:16 +00:00
|
|
|
isEditPage &&
|
2018-03-10 18:30:12 +00:00
|
|
|
$( '#wpTextbox1' ).length
|
2016-09-20 22:37:00 +00:00
|
|
|
) {
|
2015-10-23 16:29:56 +00:00
|
|
|
mw.loader.load( 'ext.visualEditor.switching' );
|
2024-04-30 16:44:25 +00:00
|
|
|
mw.hook( 'wikiEditor.toolbarReady' ).add( ( $textarea ) => {
|
|
|
|
mw.loader.using( 'ext.visualEditor.switching' ).done( () => {
|
2024-05-21 16:40:36 +00:00
|
|
|
const showPopup = url.searchParams.has( 'veswitched' ) && !mw.user.options.get( 'visualeditor-hidesourceswitchpopup' ),
|
2017-02-24 14:29:21 +00:00
|
|
|
toolFactory = new OO.ui.ToolFactory(),
|
|
|
|
toolGroupFactory = new OO.ui.ToolGroupFactory();
|
|
|
|
|
|
|
|
toolFactory.register( mw.libs.ve.MWEditModeVisualTool );
|
|
|
|
toolFactory.register( mw.libs.ve.MWEditModeSourceTool );
|
2024-05-21 16:40:36 +00:00
|
|
|
const switchToolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory, {
|
2017-02-24 14:29:21 +00:00
|
|
|
classes: [ 've-init-mw-editSwitch' ]
|
|
|
|
} );
|
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
switchToolbar.on( 'switchEditor', ( m ) => {
|
2020-08-18 12:16:49 +00:00
|
|
|
if ( m === 'visual' ) {
|
2019-06-27 21:44:20 +00:00
|
|
|
$( '#wpTextbox1' ).trigger( 'wikiEditor-switching-visualeditor' );
|
2023-07-13 14:32:55 +00:00
|
|
|
init.activateVe( 'visual' );
|
2015-11-10 23:01:49 +00:00
|
|
|
}
|
2017-02-24 14:29:21 +00:00
|
|
|
} );
|
2015-11-10 23:01:49 +00:00
|
|
|
|
2017-02-24 14:29:21 +00:00
|
|
|
switchToolbar.setup( [ {
|
2018-07-03 13:23:21 +00:00
|
|
|
name: 'editMode',
|
2017-02-24 14:29:21 +00:00
|
|
|
type: 'list',
|
2017-02-28 22:59:51 +00:00
|
|
|
icon: 'edit',
|
2017-02-24 14:29:21 +00:00
|
|
|
title: mw.msg( 'visualeditor-mweditmode-tooltip' ),
|
2020-04-03 05:53:10 +00:00
|
|
|
label: mw.msg( 'visualeditor-mweditmode-tooltip' ),
|
|
|
|
invisibleLabel: true,
|
2017-02-24 14:29:21 +00:00
|
|
|
include: [ 'editModeVisual', 'editModeSource' ]
|
|
|
|
} ] );
|
2015-11-10 23:01:49 +00:00
|
|
|
|
2024-05-21 16:40:36 +00:00
|
|
|
const popup = new mw.libs.ve.SwitchPopupWidget( 'source' );
|
2015-11-10 23:01:49 +00:00
|
|
|
|
2017-02-24 14:29:21 +00:00
|
|
|
switchToolbar.tools.editModeVisual.toolGroup.$element.append( popup.$element );
|
|
|
|
switchToolbar.emit( 'updateState' );
|
2015-11-10 23:01:49 +00:00
|
|
|
|
2024-01-30 15:32:43 +00:00
|
|
|
$textarea.wikiEditor( 'addToToolbar', {
|
|
|
|
section: 'secondary',
|
|
|
|
group: 'default',
|
|
|
|
tools: {
|
|
|
|
veEditSwitch: {
|
|
|
|
type: 'element',
|
|
|
|
element: switchToolbar.$element
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2017-02-24 14:29:21 +00:00
|
|
|
popup.toggle( showPopup );
|
2015-10-23 16:29:56 +00:00
|
|
|
|
|
|
|
// Duplicate of this code in ve.init.mw.DesktopArticleTarget.js
|
2019-08-01 01:48:24 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-class-state
|
2015-10-23 16:29:56 +00:00
|
|
|
if ( $( '#ca-edit' ).hasClass( 'visualeditor-showtabdialog' ) ) {
|
2016-05-20 01:36:25 +00:00
|
|
|
$( '#ca-edit' ).removeClass( 'visualeditor-showtabdialog' );
|
2015-10-23 16:29:56 +00:00
|
|
|
// Set up a temporary window manager
|
2024-05-21 16:40:36 +00:00
|
|
|
const windowManager = new OO.ui.WindowManager();
|
2023-10-31 21:06:04 +00:00
|
|
|
$( OO.ui.getTeleportTarget() ).append( windowManager.$element );
|
2024-05-21 16:40:36 +00:00
|
|
|
const editingTabDialog = new mw.libs.ve.EditingTabDialog();
|
2015-10-23 16:29:56 +00:00
|
|
|
windowManager.addWindows( [ editingTabDialog ] );
|
2016-04-13 21:46:14 +00:00
|
|
|
windowManager.openWindow( editingTabDialog )
|
2024-04-30 16:44:25 +00:00
|
|
|
.closed.then( ( data ) => {
|
2015-10-23 16:29:56 +00:00
|
|
|
// Detach the temporary window manager
|
|
|
|
windowManager.destroy();
|
|
|
|
|
|
|
|
if ( data && data.action === 'prefer-ve' ) {
|
2022-12-17 15:44:56 +00:00
|
|
|
location.href = veEditUrl;
|
2016-03-11 05:36:15 +00:00
|
|
|
} else if ( data && data.action === 'multi-tab' ) {
|
|
|
|
location.reload();
|
2015-10-23 16:29:56 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
} );
|
2016-03-07 20:55:07 +00:00
|
|
|
|
|
|
|
// Remember that the user wanted wikitext, at least this time
|
|
|
|
mw.libs.ve.setEditorPreference( 'wikitext' );
|
2018-03-10 18:30:12 +00:00
|
|
|
|
|
|
|
// If the user has loaded WikiEditor, clear any auto-save state they
|
|
|
|
// may have from a previous VE session
|
|
|
|
// We don't have access to the VE session storage methods, but invalidating
|
|
|
|
// the docstate is sufficient to prevent the data from being used.
|
|
|
|
mw.storage.session.remove( 've-docstate' );
|
2015-10-23 16:29:56 +00:00
|
|
|
}
|
2015-12-14 20:52:26 +00:00
|
|
|
|
2017-10-16 17:02:00 +00:00
|
|
|
init.setupEditLinks();
|
2013-07-03 22:14:52 +00:00
|
|
|
}
|
2013-11-26 09:46:42 +00:00
|
|
|
|
2016-04-28 21:08:26 +00:00
|
|
|
if (
|
2017-07-12 15:29:54 +00:00
|
|
|
pageCanLoadEditor &&
|
2016-04-28 21:08:26 +00:00
|
|
|
showWikitextWelcome &&
|
2018-08-23 13:19:52 +00:00
|
|
|
// At least one editor is available (T201928)
|
|
|
|
( init.isVisualAvailable || init.isWikitextAvailable || $( '#wpTextbox1' ).length ) &&
|
2020-09-08 10:53:16 +00:00
|
|
|
isEditPage &&
|
2020-04-17 21:25:58 +00:00
|
|
|
init.shouldShowWelcomeDialog() &&
|
2022-08-11 06:07:39 +00:00
|
|
|
// Not on protected pages
|
|
|
|
pageIsProbablyEditable
|
2016-04-28 21:08:26 +00:00
|
|
|
) {
|
2024-04-30 16:44:25 +00:00
|
|
|
mw.loader.using( 'ext.visualEditor.welcome' ).done( () => {
|
2020-06-01 20:10:49 +00:00
|
|
|
// Check shouldShowWelcomeDialog() again: any code that might have called
|
|
|
|
// stopShowingWelcomeDialog() wouldn't have had an opportunity to do that
|
|
|
|
// yet by the first time we checked
|
|
|
|
if ( !init.shouldShowWelcomeDialog() ) {
|
|
|
|
return;
|
|
|
|
}
|
2024-05-21 16:40:36 +00:00
|
|
|
const windowManager = new OO.ui.WindowManager();
|
|
|
|
const welcomeDialog = new mw.libs.ve.WelcomeDialog();
|
2023-10-31 21:06:04 +00:00
|
|
|
$( OO.ui.getTeleportTarget() ).append( windowManager.$element );
|
2016-04-28 21:08:26 +00:00
|
|
|
windowManager.addWindows( [ welcomeDialog ] );
|
|
|
|
windowManager.openWindow(
|
|
|
|
welcomeDialog,
|
|
|
|
{
|
2016-09-20 22:37:00 +00:00
|
|
|
switchable: init.isVisualAvailable,
|
2016-09-15 00:18:10 +00:00
|
|
|
editor: 'source'
|
2016-04-28 21:08:26 +00:00
|
|
|
}
|
|
|
|
)
|
2024-04-30 16:44:25 +00:00
|
|
|
.closed.then( ( data ) => {
|
2016-04-28 21:08:26 +00:00
|
|
|
windowManager.destroy();
|
|
|
|
if ( data && data.action === 'switch-ve' ) {
|
2016-05-26 12:08:26 +00:00
|
|
|
init.activateVe( 'visual' );
|
2016-04-28 21:08:26 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2020-04-17 21:25:58 +00:00
|
|
|
init.stopShowingWelcomeDialog();
|
2016-04-28 21:08:26 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-12-17 15:44:56 +00:00
|
|
|
if ( url.searchParams.has( 'venotify' ) ) {
|
|
|
|
url.searchParams.delete( 'venotify' );
|
2016-10-12 18:05:12 +00:00
|
|
|
// Get rid of the ?venotify= from the URL
|
2022-12-17 15:44:56 +00:00
|
|
|
history.replaceState( null, '', url );
|
2015-01-22 23:41:10 +00:00
|
|
|
}
|
2013-07-03 22:14:52 +00:00
|
|
|
} );
|
|
|
|
}() );
|