2016-10-25 20:44:26 +00:00
|
|
|
/*!
|
2017-06-10 11:48:09 +00:00
|
|
|
* VisualEditor MediaWiki CollabTarget init.
|
2016-10-25 20:44:26 +00:00
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2016-10-25 20:44:26 +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-01-08 17:00:09 +00:00
|
|
|
|
2016-10-25 20:44:26 +00:00
|
|
|
( function () {
|
2017-05-20 19:22:44 +00:00
|
|
|
var target,
|
2018-09-16 18:53:24 +00:00
|
|
|
$specialTab = $( '#ca-nstab-special' ),
|
|
|
|
$padTab = $( '#ca-pad' ),
|
2016-10-25 20:44:26 +00:00
|
|
|
conf = mw.config.get( 'wgVisualEditorConfig' ),
|
2017-05-20 19:22:44 +00:00
|
|
|
pageName = mw.config.get( 'collabPadPageName' ) || '',
|
|
|
|
pageTitle = mw.Title.newFromText( pageName ),
|
2018-05-22 21:39:36 +00:00
|
|
|
modules = [ OO.ui.isMobile() ? 'ext.visualEditor.collabTarget.mobile' : 'ext.visualEditor.collabTarget.desktop' ]
|
2016-10-25 20:44:26 +00:00
|
|
|
// Add modules from $wgVisualEditorPluginModules
|
|
|
|
.concat( conf.pluginModules.filter( mw.loader.getState ) ),
|
2018-08-25 22:18:02 +00:00
|
|
|
modulePromise = mw.loader.using( modules ),
|
2017-05-20 19:22:44 +00:00
|
|
|
progressBar = OO.ui.infuse( $( '.ve-init-mw-collabTarget-loading' ) ),
|
|
|
|
documentNameInput = OO.ui.infuse( $( '.ve-init-mw-collabTarget-nameInput' ) ),
|
2018-05-18 09:43:29 +00:00
|
|
|
documentNameButton = OO.ui.infuse( $( '.ve-init-mw-collabTarget-nameButton' ) ),
|
2018-06-23 18:29:33 +00:00
|
|
|
importInput = OO.ui.infuse( $( '.ve-init-mw-collabTarget-importInput' ), {
|
2019-04-16 23:09:23 +00:00
|
|
|
showImages: mw.config.get( 'wgVisualEditorConfig' ).usePageImages,
|
|
|
|
showDescriptions: mw.config.get( 'wgVisualEditorConfig' ).usePageDescriptions
|
2018-06-23 18:29:33 +00:00
|
|
|
} ),
|
|
|
|
importButton = OO.ui.infuse( $( '.ve-init-mw-collabTarget-importButton' ) ),
|
|
|
|
// Infuse the form last to avoid recursive infusion with no config
|
2022-06-21 18:01:30 +00:00
|
|
|
form = OO.ui.infuse( $( '.ve-init-mw-collabTarget-form' ) ),
|
|
|
|
$targetContainer = $(
|
2022-06-23 14:49:07 +00:00
|
|
|
document.querySelector( '[data-mw-ve-target-container]' ) ||
|
2022-06-21 18:01:30 +00:00
|
|
|
document.getElementById( 'content' )
|
|
|
|
);
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
if ( !VisualEditorSupportCheck() ) {
|
|
|
|
// VE not supported - say something?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-10 11:48:09 +00:00
|
|
|
function setTitle( title ) {
|
|
|
|
$( '#firstHeading' ).text( title );
|
|
|
|
document.title = title;
|
|
|
|
}
|
|
|
|
|
2018-05-21 14:20:25 +00:00
|
|
|
function showPage( title, importTitle ) {
|
2018-09-16 18:53:24 +00:00
|
|
|
var specialTitle = mw.Title.newFromText( 'Special:CollabPad/' + title.toString() );
|
|
|
|
|
2017-06-10 11:48:09 +00:00
|
|
|
setTitle( mw.msg( 'collabpad-doctitle', title.getPrefixedText() ) );
|
2017-05-20 19:22:44 +00:00
|
|
|
|
2018-09-16 18:53:24 +00:00
|
|
|
mw.config.set( 'wgRelevantPageName', specialTitle.getPrefixedText() );
|
|
|
|
mw.config.set( 'wgPageName', specialTitle.getPrefixedText() );
|
|
|
|
if ( !$padTab.length ) {
|
|
|
|
$padTab = $( '<li>' ).attr( 'id', 'ca-pad' ).addClass( 'selected' ).append(
|
|
|
|
$( '<span>' ).append(
|
|
|
|
$( '<a>' ).attr( 'href', '' ).text( title.getPrefixedText() )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$padTab.insertAfter( $specialTab.removeClass( 'selected' ) );
|
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
progressBar.toggle( true );
|
2018-05-18 09:43:29 +00:00
|
|
|
form.toggle( false );
|
2017-05-20 19:22:44 +00:00
|
|
|
|
2018-08-25 22:18:02 +00:00
|
|
|
modulePromise.done( function () {
|
2018-05-21 14:20:25 +00:00
|
|
|
target = ve.init.mw.targetFactory.create( 'collab', title, conf.rebaserUrl, { importTitle: importTitle } );
|
2020-07-31 15:41:18 +00:00
|
|
|
// If the target emits a 'close' event (via the toolbar back button on mobile) then go to the landing page.
|
|
|
|
target.once( 'close', function () {
|
|
|
|
showForm( true );
|
|
|
|
} );
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
$( 'body' ).addClass( 've-activated ve-active' );
|
|
|
|
|
2022-06-21 18:01:30 +00:00
|
|
|
$targetContainer.prepend( target.$element );
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
target.transformPage();
|
|
|
|
$( '#firstHeading' ).addClass( 've-init-mw-desktopArticleTarget-uneditableContent' );
|
|
|
|
|
2018-08-25 22:18:02 +00:00
|
|
|
// Add a dummy surface while the doc is loading
|
2021-10-13 12:57:45 +00:00
|
|
|
var dummySurface = target.addSurface( ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( '' ) ) );
|
2019-02-08 19:18:22 +00:00
|
|
|
dummySurface.setReadOnly( true );
|
2018-08-25 22:18:02 +00:00
|
|
|
|
|
|
|
// TODO: Create the correct model surface type (ve.ui.Surface#createModel)
|
2021-10-13 12:57:45 +00:00
|
|
|
var surfaceModel = new ve.dm.Surface( ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( '' ) ) );
|
|
|
|
var username = mw.user.getName();
|
2018-08-25 22:18:02 +00:00
|
|
|
surfaceModel.createSynchronizer(
|
2019-12-17 14:55:05 +00:00
|
|
|
mw.config.get( 'wgWikiID' ) + '/' + title.toString(),
|
2018-08-25 22:18:02 +00:00
|
|
|
{
|
|
|
|
server: conf.rebaserUrl,
|
|
|
|
// TODO: server could communicate with MW (via oauth?) to know the
|
|
|
|
// current-user's name. Disable changing name if logged in?
|
|
|
|
// Communicate an I-am-a-valid-user flag to other clients?
|
2019-06-01 16:51:41 +00:00
|
|
|
defaultName: username
|
2018-08-25 22:18:02 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var progressDeferred = ve.createDeferred();
|
2018-08-25 22:18:02 +00:00
|
|
|
dummySurface.createProgress( progressDeferred.promise(), ve.msg( 'visualeditor-rebase-client-connecting' ), true );
|
|
|
|
|
2019-09-18 11:17:34 +00:00
|
|
|
surfaceModel.synchronizer.once( 'initDoc', function ( error ) {
|
2018-09-17 14:24:27 +00:00
|
|
|
var initPromise;
|
2018-08-25 22:18:02 +00:00
|
|
|
|
|
|
|
progressDeferred.resolve();
|
2018-09-17 14:24:27 +00:00
|
|
|
// Resolving the progress bar doesn't close the window in this cycle,
|
|
|
|
// so wait until we call clearSurfaces which destroys the window manager.
|
|
|
|
setTimeout( function () {
|
|
|
|
target.clearSurfaces();
|
|
|
|
// Don't add the surface until the history has been applied
|
|
|
|
target.addSurface( surfaceModel );
|
2019-09-18 11:17:34 +00:00
|
|
|
if ( error ) {
|
2021-10-25 15:51:29 +00:00
|
|
|
var $errorMsg = ve.htmlMsg( 'visualeditor-rebase-corrupted-document-error', $( '<pre>' ).text( error.stack ) );
|
2019-09-18 11:17:34 +00:00
|
|
|
OO.ui.alert(
|
2021-10-25 15:51:29 +00:00
|
|
|
$( '<p>' ).append( $errorMsg ),
|
2021-06-04 21:52:19 +00:00
|
|
|
{ title: ve.msg( 'visualeditor-rebase-corrupted-document-title' ), size: 'large' }
|
2019-09-18 11:17:34 +00:00
|
|
|
).then( function () {
|
|
|
|
showForm( true );
|
|
|
|
} );
|
|
|
|
return;
|
|
|
|
}
|
2018-09-17 14:24:27 +00:00
|
|
|
target.once( 'surfaceReady', function () {
|
|
|
|
initPromise.then( function () {
|
2021-11-27 00:38:54 +00:00
|
|
|
target.getSurface().getView().selectFirstSelectableContentOffset();
|
2021-10-13 12:57:45 +00:00
|
|
|
var isNewAuthor = !ve.init.platform.sessionStorage.get( 've-collab-author' );
|
2019-06-01 16:51:41 +00:00
|
|
|
// For new anon users, open the author list so they can set their name
|
|
|
|
if ( isNewAuthor && !username ) {
|
2020-12-04 14:57:52 +00:00
|
|
|
// Something (an animation?) steals focus during load, so wait a bit
|
|
|
|
// before opening and focusing the authorList.
|
|
|
|
setTimeout( function () {
|
|
|
|
target.actionsToolbar.tools.authorList.onSelect();
|
|
|
|
}, 500 );
|
2019-06-01 16:51:41 +00:00
|
|
|
}
|
2018-09-17 14:24:27 +00:00
|
|
|
} );
|
2018-09-14 23:19:54 +00:00
|
|
|
} );
|
2018-08-25 22:18:02 +00:00
|
|
|
|
2018-09-17 14:24:27 +00:00
|
|
|
if ( target.importTitle && !surfaceModel.getDocument().getCompleteHistoryLength() ) {
|
|
|
|
initPromise = mw.libs.ve.targetLoader.requestParsoidData( target.importTitle.toString(), { targetName: 'collabpad' } ).then( function ( response ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var data = response.visualeditor;
|
2018-09-17 14:24:27 +00:00
|
|
|
|
|
|
|
if ( data && data.content ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var doc = target.constructor.static.parseDocument( data.content );
|
|
|
|
var dmDoc = target.constructor.static.createModelFromDom( doc );
|
|
|
|
var fragment = surfaceModel.getLinearFragment( new ve.Range( 0, 2 ) );
|
2018-09-17 14:24:27 +00:00
|
|
|
fragment.insertDocument( dmDoc );
|
|
|
|
|
|
|
|
target.etag = data.etag;
|
|
|
|
target.baseTimeStamp = data.basetimestamp;
|
|
|
|
target.startTimeStamp = data.starttimestamp;
|
|
|
|
target.revid = data.oldid;
|
|
|
|
|
|
|
|
// Store the document metadata as a hidden meta item
|
|
|
|
fragment.collapseToEnd().insertContent( [
|
|
|
|
{
|
|
|
|
type: 'alienMeta',
|
|
|
|
attributes: {
|
|
|
|
importedDocument: {
|
|
|
|
title: target.importTitle.toString(),
|
|
|
|
etag: target.etag,
|
|
|
|
baseTimeStamp: target.baseTimeStamp,
|
|
|
|
startTimeStamp: target.startTimeStamp,
|
|
|
|
revid: target.revid
|
|
|
|
}
|
2018-08-25 22:18:02 +00:00
|
|
|
}
|
2018-09-17 14:24:27 +00:00
|
|
|
},
|
|
|
|
{ type: '/alienMeta' }
|
|
|
|
] );
|
|
|
|
} else {
|
|
|
|
// Import failed
|
2019-11-02 05:06:28 +00:00
|
|
|
return ve.createDeferred().reject( 'No content for ' + target.importTitle ).promise();
|
2018-09-17 14:24:27 +00:00
|
|
|
}
|
|
|
|
} );
|
2018-08-25 22:18:02 +00:00
|
|
|
} else {
|
2018-09-17 14:24:27 +00:00
|
|
|
// No import, or history already exists
|
2019-11-02 05:06:28 +00:00
|
|
|
initPromise = ve.createDeferred().resolve().promise();
|
2018-09-17 14:24:27 +00:00
|
|
|
|
|
|
|
// Look for import metadata in document
|
|
|
|
surfaceModel = target.getSurface().getModel();
|
|
|
|
surfaceModel.getMetaList().getItemsInGroup( 'misc' ).some( function ( item ) {
|
|
|
|
var importedDocument = item.getAttribute( 'importedDocument' );
|
|
|
|
if ( importedDocument ) {
|
|
|
|
target.importTitle = mw.Title.newFromText( importedDocument.title );
|
|
|
|
target.etag = importedDocument.etag;
|
|
|
|
target.baseTimeStamp = importedDocument.baseTimeStamp;
|
|
|
|
target.startTimeStamp = importedDocument.startTimeStamp;
|
|
|
|
target.revid = importedDocument.revid;
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-02 20:30:00 +00:00
|
|
|
return false;
|
2018-09-17 14:24:27 +00:00
|
|
|
} );
|
2018-08-25 22:18:02 +00:00
|
|
|
}
|
2018-09-17 14:24:27 +00:00
|
|
|
initPromise.fail( function ( err ) {
|
|
|
|
setTimeout( function () {
|
|
|
|
throw new Error( err );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
initPromise.always( function () {
|
|
|
|
progressDeferred.resolve();
|
|
|
|
} );
|
2018-08-25 22:18:02 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
} ).always( function () {
|
2018-05-18 09:43:29 +00:00
|
|
|
form.toggle( false );
|
2017-05-20 19:22:44 +00:00
|
|
|
progressBar.toggle( false );
|
2018-05-22 21:39:36 +00:00
|
|
|
} ).fail( function ( err ) {
|
|
|
|
mw.log.error( err );
|
2019-09-18 11:17:34 +00:00
|
|
|
showForm( true );
|
2016-10-25 20:44:26 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-03-04 13:25:26 +00:00
|
|
|
function showForm( pushState ) {
|
2018-09-16 18:53:24 +00:00
|
|
|
var specialTitle = mw.Title.newFromText( 'Special:CollabPad' );
|
2016-10-25 20:44:26 +00:00
|
|
|
|
2019-03-04 13:25:26 +00:00
|
|
|
if ( pushState ) {
|
2022-08-09 12:37:34 +00:00
|
|
|
history.pushState( { tag: 'collabTarget' }, '', specialTitle.getUrl() );
|
2019-03-04 13:25:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
if ( target ) {
|
|
|
|
$( '#firstHeading' ).removeClass( 've-init-mw-desktopArticleTarget-uneditableContent' );
|
|
|
|
target.restorePage();
|
|
|
|
target.destroy();
|
|
|
|
|
|
|
|
$( 'body' ).removeClass( 've-activated ve-active' );
|
|
|
|
}
|
|
|
|
|
2018-09-16 18:53:24 +00:00
|
|
|
setTitle( mw.msg( 'collabpad' ) );
|
|
|
|
mw.config.set( 'wgRelevantPageName', specialTitle.getPrefixedText() );
|
|
|
|
mw.config.set( 'wgPageName', specialTitle.getPrefixedText() );
|
|
|
|
if ( $padTab ) {
|
|
|
|
$padTab.detach();
|
|
|
|
}
|
|
|
|
$specialTab.addClass( 'selected' );
|
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
progressBar.toggle( false );
|
2018-05-18 09:43:29 +00:00
|
|
|
form.toggle( true );
|
2017-05-20 19:22:44 +00:00
|
|
|
}
|
2016-10-25 20:44:26 +00:00
|
|
|
|
2018-05-21 14:20:25 +00:00
|
|
|
function loadTitle( title, importTitle ) {
|
2018-05-18 09:43:29 +00:00
|
|
|
var specialTitle = mw.Title.newFromText( 'Special:CollabPad/' + title.toString() );
|
2022-12-12 19:47:32 +00:00
|
|
|
// TODO: Handle popstate
|
|
|
|
history.pushState( { tag: 'collabTarget', title: title.toString() }, '', specialTitle.getUrl() );
|
|
|
|
showPage( title, importTitle );
|
2018-05-18 09:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomTitle() {
|
|
|
|
return Math.random().toString( 36 ).slice( 2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
function onNameChange() {
|
2017-09-14 12:42:36 +00:00
|
|
|
documentNameInput.getValidity().then( function () {
|
2018-05-18 09:43:29 +00:00
|
|
|
documentNameButton.setDisabled( false );
|
2017-09-14 12:42:36 +00:00
|
|
|
}, function () {
|
2018-05-18 09:43:29 +00:00
|
|
|
documentNameButton.setDisabled( true );
|
2017-09-14 12:42:36 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-05-18 09:43:29 +00:00
|
|
|
function loadFromName() {
|
2017-05-20 19:22:44 +00:00
|
|
|
documentNameInput.getValidity().then( function () {
|
2018-05-18 09:43:29 +00:00
|
|
|
var title = mw.Title.newFromText(
|
|
|
|
documentNameInput.getValue().trim() || getRandomTitle()
|
|
|
|
);
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
if ( title ) {
|
2018-05-18 09:43:29 +00:00
|
|
|
loadTitle( title );
|
2016-10-25 20:44:26 +00:00
|
|
|
} else {
|
|
|
|
documentNameInput.focus();
|
|
|
|
}
|
2017-05-20 19:22:44 +00:00
|
|
|
} );
|
|
|
|
}
|
2016-10-25 20:44:26 +00:00
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
documentNameInput.setValidation( function ( value ) {
|
2018-01-08 15:32:15 +00:00
|
|
|
// Empty input will create a random document name, otherwise must be valid
|
|
|
|
return value === '' || !!mw.Title.newFromText( value );
|
2017-05-20 19:22:44 +00:00
|
|
|
} );
|
2018-05-18 09:43:29 +00:00
|
|
|
documentNameButton.setDisabled( false );
|
|
|
|
|
|
|
|
documentNameInput.on( 'change', onNameChange );
|
|
|
|
documentNameInput.on( 'enter', loadFromName );
|
|
|
|
documentNameButton.on( 'click', loadFromName );
|
|
|
|
onNameChange();
|
|
|
|
|
|
|
|
function onImportChange() {
|
2018-07-14 09:20:11 +00:00
|
|
|
importInput.getValidity().then( function () {
|
2018-05-18 09:43:29 +00:00
|
|
|
importButton.setDisabled( false );
|
|
|
|
}, function () {
|
|
|
|
importButton.setDisabled( true );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2020-08-18 12:16:49 +00:00
|
|
|
function onImportSubmit() {
|
2018-05-18 09:43:29 +00:00
|
|
|
importInput.getValidity().then( function () {
|
|
|
|
var title = mw.Title.newFromText( importInput.getValue().trim() );
|
|
|
|
|
|
|
|
if ( title ) {
|
2018-05-21 14:20:25 +00:00
|
|
|
loadTitle( mw.Title.newFromText( getRandomTitle() ), title );
|
2018-05-18 09:43:29 +00:00
|
|
|
} else {
|
|
|
|
documentNameInput.focus();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2016-10-25 20:44:26 +00:00
|
|
|
|
2018-05-18 09:43:29 +00:00
|
|
|
importInput.setValidation( function ( value ) {
|
|
|
|
// TODO: Check page exists?
|
|
|
|
return !!mw.Title.newFromText( value );
|
|
|
|
} );
|
2018-07-14 09:20:11 +00:00
|
|
|
importInput.on( 'change', onImportChange );
|
2020-08-18 12:16:49 +00:00
|
|
|
importInput.on( 'enter', onImportSubmit );
|
|
|
|
importButton.on( 'click', onImportSubmit );
|
2018-05-18 09:43:29 +00:00
|
|
|
onImportChange();
|
2016-10-25 20:44:26 +00:00
|
|
|
|
|
|
|
if ( pageTitle ) {
|
2022-12-12 19:49:26 +00:00
|
|
|
var url = new URL( location.href ),
|
|
|
|
importTitleText = url.searchParams.get( 'import' ),
|
2022-05-06 09:37:54 +00:00
|
|
|
importTitleParam = ( importTitleText ? mw.Title.newFromText( importTitleText ) : null );
|
|
|
|
showPage( pageTitle, importTitleParam );
|
2016-10-25 20:44:26 +00:00
|
|
|
} else {
|
|
|
|
showForm();
|
|
|
|
}
|
2017-05-20 19:22:44 +00:00
|
|
|
|
2019-02-28 19:20:14 +00:00
|
|
|
$specialTab.on( 'click', function ( e ) {
|
2019-03-04 13:25:26 +00:00
|
|
|
showForm( true );
|
2019-02-28 19:20:14 +00:00
|
|
|
e.preventDefault();
|
|
|
|
} );
|
|
|
|
|
2017-05-20 19:22:44 +00:00
|
|
|
// Tag current state
|
2022-12-12 19:47:32 +00:00
|
|
|
history.replaceState( { tag: 'collabTarget', title: pageName }, '', location.href );
|
2017-05-20 19:22:44 +00:00
|
|
|
window.addEventListener( 'popstate', function ( e ) {
|
|
|
|
if ( e.state && e.state.tag === 'collabTarget' ) {
|
|
|
|
if ( e.state.title ) {
|
|
|
|
showPage( mw.Title.newFromText( e.state.title ) );
|
|
|
|
} else {
|
|
|
|
showForm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
2016-10-25 20:44:26 +00:00
|
|
|
}() );
|